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