nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 1:55a6170b404f 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
nexpaq 1:55a6170b404f 2 *
nexpaq 1:55a6170b404f 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
nexpaq 1:55a6170b404f 4 * and associated documentation files (the "Software"), to deal in the Software without
nexpaq 1:55a6170b404f 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
nexpaq 1:55a6170b404f 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
nexpaq 1:55a6170b404f 7 * Software is furnished to do so, subject to the following conditions:
nexpaq 1:55a6170b404f 8 *
nexpaq 1:55a6170b404f 9 * The above copyright notice and this permission notice shall be included in all copies or
nexpaq 1:55a6170b404f 10 * substantial portions of the Software.
nexpaq 1:55a6170b404f 11 *
nexpaq 1:55a6170b404f 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
nexpaq 1:55a6170b404f 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
nexpaq 1:55a6170b404f 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
nexpaq 1:55a6170b404f 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
nexpaq 1:55a6170b404f 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
nexpaq 1:55a6170b404f 17 */
nexpaq 1:55a6170b404f 18
nexpaq 1:55a6170b404f 19 #ifndef USBAudio_H
nexpaq 1:55a6170b404f 20 #define USBAudio_H
nexpaq 1:55a6170b404f 21
nexpaq 1:55a6170b404f 22 /* These headers are included for child class. */
nexpaq 1:55a6170b404f 23 #include "USBEndpoints.h"
nexpaq 1:55a6170b404f 24 #include "USBDescriptor.h"
nexpaq 1:55a6170b404f 25 #include "USBDevice_Types.h"
nexpaq 1:55a6170b404f 26
nexpaq 1:55a6170b404f 27 #include "USBDevice.h"
nexpaq 1:55a6170b404f 28 #include "Callback.h"
nexpaq 1:55a6170b404f 29
nexpaq 1:55a6170b404f 30 /**
nexpaq 1:55a6170b404f 31 * USBAudio example
nexpaq 1:55a6170b404f 32 *
nexpaq 1:55a6170b404f 33 * @code
nexpaq 1:55a6170b404f 34 * #include "mbed.h"
nexpaq 1:55a6170b404f 35 * #include "USBAudio.h"
nexpaq 1:55a6170b404f 36 *
nexpaq 1:55a6170b404f 37 * Serial pc(USBTX, USBRX);
nexpaq 1:55a6170b404f 38 *
nexpaq 1:55a6170b404f 39 * // frequency: 48 kHz
nexpaq 1:55a6170b404f 40 * #define FREQ 48000
nexpaq 1:55a6170b404f 41 *
nexpaq 1:55a6170b404f 42 * // 1 channel: mono
nexpaq 1:55a6170b404f 43 * #define NB_CHA 1
nexpaq 1:55a6170b404f 44 *
nexpaq 1:55a6170b404f 45 * // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1
nexpaq 1:55a6170b404f 46 * #define AUDIO_LENGTH_PACKET 48 * 2 * 1
nexpaq 1:55a6170b404f 47 *
nexpaq 1:55a6170b404f 48 * // USBAudio
nexpaq 1:55a6170b404f 49 * USBAudio audio(FREQ, NB_CHA);
nexpaq 1:55a6170b404f 50 *
nexpaq 1:55a6170b404f 51 * int main() {
nexpaq 1:55a6170b404f 52 * int16_t buf[AUDIO_LENGTH_PACKET/2];
nexpaq 1:55a6170b404f 53 *
nexpaq 1:55a6170b404f 54 * while (1) {
nexpaq 1:55a6170b404f 55 * // read an audio packet
nexpaq 1:55a6170b404f 56 * audio.read((uint8_t *)buf);
nexpaq 1:55a6170b404f 57 *
nexpaq 1:55a6170b404f 58 *
nexpaq 1:55a6170b404f 59 * // print packet received
nexpaq 1:55a6170b404f 60 * pc.printf("recv: ");
nexpaq 1:55a6170b404f 61 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
nexpaq 1:55a6170b404f 62 * pc.printf("%d ", buf[i]);
nexpaq 1:55a6170b404f 63 * }
nexpaq 1:55a6170b404f 64 * pc.printf("\r\n");
nexpaq 1:55a6170b404f 65 * }
nexpaq 1:55a6170b404f 66 * }
nexpaq 1:55a6170b404f 67 * @endcode
nexpaq 1:55a6170b404f 68 */
nexpaq 1:55a6170b404f 69 class USBAudio: public USBDevice {
nexpaq 1:55a6170b404f 70 public:
nexpaq 1:55a6170b404f 71
nexpaq 1:55a6170b404f 72 /**
nexpaq 1:55a6170b404f 73 * Constructor
nexpaq 1:55a6170b404f 74 *
nexpaq 1:55a6170b404f 75 * @param frequency_in frequency in Hz (default: 48000)
nexpaq 1:55a6170b404f 76 * @param channel_nb_in channel number (1 or 2) (default: 1)
nexpaq 1:55a6170b404f 77 * @param frequency_out frequency in Hz (default: 8000)
nexpaq 1:55a6170b404f 78 * @param channel_nb_out_in channel number (1 or 2) (default: 1)
nexpaq 1:55a6170b404f 79 * @param vendor_id Your vendor_id
nexpaq 1:55a6170b404f 80 * @param product_id Your product_id
nexpaq 1:55a6170b404f 81 * @param product_release Your preoduct_release
nexpaq 1:55a6170b404f 82 */
nexpaq 1:55a6170b404f 83 USBAudio(uint32_t frequency_in = 48000, uint8_t channel_nb_in = 1, uint32_t frequency_out = 8000, uint8_t channel_nb_out = 1, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100);
nexpaq 1:55a6170b404f 84
nexpaq 1:55a6170b404f 85 /**
nexpaq 1:55a6170b404f 86 * Get current volume between 0.0 and 1.0
nexpaq 1:55a6170b404f 87 *
nexpaq 1:55a6170b404f 88 * @returns volume
nexpaq 1:55a6170b404f 89 */
nexpaq 1:55a6170b404f 90 float getVolume();
nexpaq 1:55a6170b404f 91
nexpaq 1:55a6170b404f 92 /**
nexpaq 1:55a6170b404f 93 * Read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Blocking
nexpaq 1:55a6170b404f 94 *
nexpaq 1:55a6170b404f 95 * @param buf pointer on a buffer which will be filled with an audio packet
nexpaq 1:55a6170b404f 96 *
nexpaq 1:55a6170b404f 97 * @returns true if successfull
nexpaq 1:55a6170b404f 98 */
nexpaq 1:55a6170b404f 99 bool read(uint8_t * buf);
nexpaq 1:55a6170b404f 100
nexpaq 1:55a6170b404f 101 /**
nexpaq 1:55a6170b404f 102 * Try to read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Non Blocking
nexpaq 1:55a6170b404f 103 *
nexpaq 1:55a6170b404f 104 * @param buf pointer on a buffer which will be filled if an audio packet is available
nexpaq 1:55a6170b404f 105 *
nexpaq 1:55a6170b404f 106 * @returns true if successfull
nexpaq 1:55a6170b404f 107 */
nexpaq 1:55a6170b404f 108 bool readNB(uint8_t * buf);
nexpaq 1:55a6170b404f 109
nexpaq 1:55a6170b404f 110 /**
nexpaq 1:55a6170b404f 111 * Write an audio packet. During a frame, only a single writing (you can't write and read an audio packet during the same frame)can be done using this method.
nexpaq 1:55a6170b404f 112 *
nexpaq 1:55a6170b404f 113 * @param buf pointer on the audio packet which will be sent
nexpaq 1:55a6170b404f 114 * @returns true if successful
nexpaq 1:55a6170b404f 115 */
nexpaq 1:55a6170b404f 116 bool write(uint8_t * buf);
nexpaq 1:55a6170b404f 117
nexpaq 1:55a6170b404f 118 /**
nexpaq 1:55a6170b404f 119 * Write and read an audio packet at the same time (on the same frame)
nexpaq 1:55a6170b404f 120 *
nexpaq 1:55a6170b404f 121 * @param buf_read pointer on a buffer which will be filled with an audio packet
nexpaq 1:55a6170b404f 122 * @param buf_write pointer on the audio packet which will be sent
nexpaq 1:55a6170b404f 123 * @returns true if successful
nexpaq 1:55a6170b404f 124 */
nexpaq 1:55a6170b404f 125 bool readWrite(uint8_t * buf_read, uint8_t * buf_write);
nexpaq 1:55a6170b404f 126
nexpaq 1:55a6170b404f 127
nexpaq 1:55a6170b404f 128 /** attach a handler to update the volume
nexpaq 1:55a6170b404f 129 *
nexpaq 1:55a6170b404f 130 * @param function Function to attach
nexpaq 1:55a6170b404f 131 *
nexpaq 1:55a6170b404f 132 */
nexpaq 1:55a6170b404f 133 void attach(void(*fptr)(void)) {
nexpaq 1:55a6170b404f 134 updateVol.attach(fptr);
nexpaq 1:55a6170b404f 135 }
nexpaq 1:55a6170b404f 136
nexpaq 1:55a6170b404f 137 /** Attach a nonstatic void/void member function to update the volume
nexpaq 1:55a6170b404f 138 *
nexpaq 1:55a6170b404f 139 * @param tptr Object pointer
nexpaq 1:55a6170b404f 140 * @param mptr Member function pointer
nexpaq 1:55a6170b404f 141 *
nexpaq 1:55a6170b404f 142 */
nexpaq 1:55a6170b404f 143 template<typename T>
nexpaq 1:55a6170b404f 144 void attach(T *tptr, void(T::*mptr)(void)) {
nexpaq 1:55a6170b404f 145 updateVol.attach(tptr, mptr);
nexpaq 1:55a6170b404f 146 }
nexpaq 1:55a6170b404f 147
nexpaq 1:55a6170b404f 148
nexpaq 1:55a6170b404f 149 protected:
nexpaq 1:55a6170b404f 150
nexpaq 1:55a6170b404f 151 /*
nexpaq 1:55a6170b404f 152 * Called by USBDevice layer. Set configuration of the device.
nexpaq 1:55a6170b404f 153 * For instance, you can add all endpoints that you need on this function.
nexpaq 1:55a6170b404f 154 *
nexpaq 1:55a6170b404f 155 * @param configuration Number of the configuration
nexpaq 1:55a6170b404f 156 * @returns true if class handles this request
nexpaq 1:55a6170b404f 157 */
nexpaq 1:55a6170b404f 158 virtual bool USBCallback_setConfiguration(uint8_t configuration);
nexpaq 1:55a6170b404f 159
nexpaq 1:55a6170b404f 160 /*
nexpaq 1:55a6170b404f 161 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
nexpaq 1:55a6170b404f 162 * This is used to handle extensions to standard requests
nexpaq 1:55a6170b404f 163 * and class specific requests
nexpaq 1:55a6170b404f 164 *
nexpaq 1:55a6170b404f 165 * @returns true if class handles this request
nexpaq 1:55a6170b404f 166 */
nexpaq 1:55a6170b404f 167 virtual bool USBCallback_request();
nexpaq 1:55a6170b404f 168
nexpaq 1:55a6170b404f 169 /*
nexpaq 1:55a6170b404f 170 * Get string product descriptor
nexpaq 1:55a6170b404f 171 *
nexpaq 1:55a6170b404f 172 * @returns pointer to the string product descriptor
nexpaq 1:55a6170b404f 173 */
nexpaq 1:55a6170b404f 174 virtual uint8_t * stringIproductDesc();
nexpaq 1:55a6170b404f 175
nexpaq 1:55a6170b404f 176 /*
nexpaq 1:55a6170b404f 177 * Get string interface descriptor
nexpaq 1:55a6170b404f 178 *
nexpaq 1:55a6170b404f 179 * @returns pointer to the string interface descriptor
nexpaq 1:55a6170b404f 180 */
nexpaq 1:55a6170b404f 181 virtual uint8_t * stringIinterfaceDesc();
nexpaq 1:55a6170b404f 182
nexpaq 1:55a6170b404f 183 /*
nexpaq 1:55a6170b404f 184 * Get configuration descriptor
nexpaq 1:55a6170b404f 185 *
nexpaq 1:55a6170b404f 186 * @returns pointer to the configuration descriptor
nexpaq 1:55a6170b404f 187 */
nexpaq 1:55a6170b404f 188 virtual uint8_t * configurationDesc();
nexpaq 1:55a6170b404f 189
nexpaq 1:55a6170b404f 190 /*
nexpaq 1:55a6170b404f 191 * Called by USBDevice layer. Set interface/alternate of the device.
nexpaq 1:55a6170b404f 192 *
nexpaq 1:55a6170b404f 193 * @param interface Number of the interface to be configured
nexpaq 1:55a6170b404f 194 * @param alternate Number of the alternate to be configured
nexpaq 1:55a6170b404f 195 * @returns true if class handles this request
nexpaq 1:55a6170b404f 196 */
nexpaq 1:55a6170b404f 197 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
nexpaq 1:55a6170b404f 198
nexpaq 1:55a6170b404f 199 /*
nexpaq 1:55a6170b404f 200 * Called by USBDevice on Endpoint0 request completion
nexpaq 1:55a6170b404f 201 * if the 'notify' flag has been set to true. Warning: Called in ISR context
nexpaq 1:55a6170b404f 202 *
nexpaq 1:55a6170b404f 203 * In this case it is used to indicate that a HID report has
nexpaq 1:55a6170b404f 204 * been received from the host on endpoint 0
nexpaq 1:55a6170b404f 205 *
nexpaq 1:55a6170b404f 206 * @param buf buffer received on endpoint 0
nexpaq 1:55a6170b404f 207 * @param length length of this buffer
nexpaq 1:55a6170b404f 208 */
nexpaq 1:55a6170b404f 209 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
nexpaq 1:55a6170b404f 210
nexpaq 1:55a6170b404f 211 /*
nexpaq 1:55a6170b404f 212 * Callback called on each Start of Frame event
nexpaq 1:55a6170b404f 213 */
nexpaq 1:55a6170b404f 214 virtual void SOF(int frameNumber);
nexpaq 1:55a6170b404f 215
nexpaq 1:55a6170b404f 216 /*
nexpaq 1:55a6170b404f 217 * Callback called when a packet is received
nexpaq 1:55a6170b404f 218 */
nexpaq 1:55a6170b404f 219 virtual bool EPISO_OUT_callback();
nexpaq 1:55a6170b404f 220
nexpaq 1:55a6170b404f 221 /*
nexpaq 1:55a6170b404f 222 * Callback called when a packet has been sent
nexpaq 1:55a6170b404f 223 */
nexpaq 1:55a6170b404f 224 virtual bool EPISO_IN_callback();
nexpaq 1:55a6170b404f 225
nexpaq 1:55a6170b404f 226 private:
nexpaq 1:55a6170b404f 227
nexpaq 1:55a6170b404f 228 // stream available ?
nexpaq 1:55a6170b404f 229 volatile bool available;
nexpaq 1:55a6170b404f 230
nexpaq 1:55a6170b404f 231 // interrupt OUT has been received
nexpaq 1:55a6170b404f 232 volatile bool interruptOUT;
nexpaq 1:55a6170b404f 233
nexpaq 1:55a6170b404f 234 // interrupt IN has been received
nexpaq 1:55a6170b404f 235 volatile bool interruptIN;
nexpaq 1:55a6170b404f 236
nexpaq 1:55a6170b404f 237 // audio packet has been written
nexpaq 1:55a6170b404f 238 volatile bool writeIN;
nexpaq 1:55a6170b404f 239
nexpaq 1:55a6170b404f 240 // FREQ
nexpaq 1:55a6170b404f 241 uint32_t FREQ_OUT;
nexpaq 1:55a6170b404f 242 uint32_t FREQ_IN;
nexpaq 1:55a6170b404f 243
nexpaq 1:55a6170b404f 244 // size of the maximum packet for the isochronous endpoint
nexpaq 1:55a6170b404f 245 uint32_t PACKET_SIZE_ISO_IN;
nexpaq 1:55a6170b404f 246 uint32_t PACKET_SIZE_ISO_OUT;
nexpaq 1:55a6170b404f 247
nexpaq 1:55a6170b404f 248 // mono, stereo,...
nexpaq 1:55a6170b404f 249 uint8_t channel_nb_in;
nexpaq 1:55a6170b404f 250 uint8_t channel_nb_out;
nexpaq 1:55a6170b404f 251
nexpaq 1:55a6170b404f 252 // channel config: master, left, right
nexpaq 1:55a6170b404f 253 uint8_t channel_config_in;
nexpaq 1:55a6170b404f 254 uint8_t channel_config_out;
nexpaq 1:55a6170b404f 255
nexpaq 1:55a6170b404f 256 // mute state
nexpaq 1:55a6170b404f 257 uint8_t mute;
nexpaq 1:55a6170b404f 258
nexpaq 1:55a6170b404f 259 // Volume Current Value
nexpaq 1:55a6170b404f 260 uint16_t volCur;
nexpaq 1:55a6170b404f 261
nexpaq 1:55a6170b404f 262 // Volume Minimum Value
nexpaq 1:55a6170b404f 263 uint16_t volMin;
nexpaq 1:55a6170b404f 264
nexpaq 1:55a6170b404f 265 // Volume Maximum Value
nexpaq 1:55a6170b404f 266 uint16_t volMax;
nexpaq 1:55a6170b404f 267
nexpaq 1:55a6170b404f 268 // Volume Resolution
nexpaq 1:55a6170b404f 269 uint16_t volRes;
nexpaq 1:55a6170b404f 270
nexpaq 1:55a6170b404f 271 // Buffer containing one audio packet (to be read)
nexpaq 1:55a6170b404f 272 volatile uint8_t * buf_stream_in;
nexpaq 1:55a6170b404f 273
nexpaq 1:55a6170b404f 274 // Buffer containing one audio packet (to be written)
nexpaq 1:55a6170b404f 275 volatile uint8_t * buf_stream_out;
nexpaq 1:55a6170b404f 276
nexpaq 1:55a6170b404f 277 // callback to update volume
nexpaq 1:55a6170b404f 278 Callback<void()> updateVol;
nexpaq 1:55a6170b404f 279
nexpaq 1:55a6170b404f 280 // boolean showing that the SOF handler has been called. Useful for readNB.
nexpaq 1:55a6170b404f 281 volatile bool SOF_handler;
nexpaq 1:55a6170b404f 282
nexpaq 1:55a6170b404f 283 volatile float volume;
nexpaq 1:55a6170b404f 284
nexpaq 1:55a6170b404f 285 };
nexpaq 1:55a6170b404f 286
nexpaq 1:55a6170b404f 287 #endif