Fork of the original Sammy USB_Device library; may not work well;

Fork of USBDevice by Samuel Mokrani

Committer:
Ugly_Kid
Date:
Thu May 04 13:35:50 2017 +0000
Revision:
1:dc16be71dc2b
Parent:
0:140cdf8e2d60
Pequenas altera??es na biblioteca original.

Who changed what in which revision?

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