USB device stack - modified

Dependents:   shaun_larada

Fork of USBDevice by mbed official

Committer:
setcom_001
Date:
Mon Jul 22 21:16:27 2013 +0000
Revision:
12:a9671b78d24e
docs update

Who changed what in which revision?

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