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