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