Modified a little for 32k byte buffer.
Dependents: Peach_AudioChannelDividerAndCompensator
Doku_USBAudio/USBAudio.h@60:68924f211e71, 2015-10-11 (annotated)
- Committer:
- dokunewon
- Date:
- Sun Oct 11 07:37:39 2015 +0000
- Revision:
- 60:68924f211e71
- Child:
- 61:ff0388dee94f
Change buffer size 32k.
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 | 60:68924f211e71 | 105 | int USBAudio::read32kSZ(uint32_t *buf); |
dokunewon | 60:68924f211e71 | 106 | |
dokunewon | 60:68924f211e71 | 107 | /** |
dokunewon | 60:68924f211e71 | 108 | * 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 | 109 | * |
dokunewon | 60:68924f211e71 | 110 | * @param buf pointer on a buffer which will be filled if an audio packet is available |
dokunewon | 60:68924f211e71 | 111 | * |
dokunewon | 60:68924f211e71 | 112 | * @returns true if successfull |
dokunewon | 60:68924f211e71 | 113 | */ |
dokunewon | 60:68924f211e71 | 114 | bool readNB(uint8_t * buf); |
dokunewon | 60:68924f211e71 | 115 | |
dokunewon | 60:68924f211e71 | 116 | /** |
dokunewon | 60:68924f211e71 | 117 | * 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 | 118 | * |
dokunewon | 60:68924f211e71 | 119 | * @param buf pointer on the audio packet which will be sent |
dokunewon | 60:68924f211e71 | 120 | * @returns true if successful |
dokunewon | 60:68924f211e71 | 121 | */ |
dokunewon | 60:68924f211e71 | 122 | bool write(uint8_t * buf); |
dokunewon | 60:68924f211e71 | 123 | |
dokunewon | 60:68924f211e71 | 124 | /** |
dokunewon | 60:68924f211e71 | 125 | * Write and read an audio packet at the same time (on the same frame) |
dokunewon | 60:68924f211e71 | 126 | * |
dokunewon | 60:68924f211e71 | 127 | * @param buf_read pointer on a buffer which will be filled with an audio packet |
dokunewon | 60:68924f211e71 | 128 | * @param buf_write pointer on the audio packet which will be sent |
dokunewon | 60:68924f211e71 | 129 | * @returns true if successful |
dokunewon | 60:68924f211e71 | 130 | */ |
dokunewon | 60:68924f211e71 | 131 | bool readWrite(uint8_t * buf_read, uint8_t * buf_write); |
dokunewon | 60:68924f211e71 | 132 | |
dokunewon | 60:68924f211e71 | 133 | |
dokunewon | 60:68924f211e71 | 134 | /** attach a handler to update the volume |
dokunewon | 60:68924f211e71 | 135 | * |
dokunewon | 60:68924f211e71 | 136 | * @param function Function to attach |
dokunewon | 60:68924f211e71 | 137 | * |
dokunewon | 60:68924f211e71 | 138 | */ |
dokunewon | 60:68924f211e71 | 139 | void attach(void(*fptr)(void)) { |
dokunewon | 60:68924f211e71 | 140 | updateVol.attach(fptr); |
dokunewon | 60:68924f211e71 | 141 | } |
dokunewon | 60:68924f211e71 | 142 | |
dokunewon | 60:68924f211e71 | 143 | /** Attach a nonstatic void/void member function to update the volume |
dokunewon | 60:68924f211e71 | 144 | * |
dokunewon | 60:68924f211e71 | 145 | * @param tptr Object pointer |
dokunewon | 60:68924f211e71 | 146 | * @param mptr Member function pointer |
dokunewon | 60:68924f211e71 | 147 | * |
dokunewon | 60:68924f211e71 | 148 | */ |
dokunewon | 60:68924f211e71 | 149 | template<typename T> |
dokunewon | 60:68924f211e71 | 150 | void attach(T *tptr, void(T::*mptr)(void)) { |
dokunewon | 60:68924f211e71 | 151 | updateVol.attach(tptr, mptr); |
dokunewon | 60:68924f211e71 | 152 | } |
dokunewon | 60:68924f211e71 | 153 | |
dokunewon | 60:68924f211e71 | 154 | |
dokunewon | 60:68924f211e71 | 155 | protected: |
dokunewon | 60:68924f211e71 | 156 | |
dokunewon | 60:68924f211e71 | 157 | /* |
dokunewon | 60:68924f211e71 | 158 | * Called by USBDevice layer. Set configuration of the device. |
dokunewon | 60:68924f211e71 | 159 | * For instance, you can add all endpoints that you need on this function. |
dokunewon | 60:68924f211e71 | 160 | * |
dokunewon | 60:68924f211e71 | 161 | * @param configuration Number of the configuration |
dokunewon | 60:68924f211e71 | 162 | * @returns true if class handles this request |
dokunewon | 60:68924f211e71 | 163 | */ |
dokunewon | 60:68924f211e71 | 164 | virtual bool USBCallback_setConfiguration(uint8_t configuration); |
dokunewon | 60:68924f211e71 | 165 | |
dokunewon | 60:68924f211e71 | 166 | /* |
dokunewon | 60:68924f211e71 | 167 | * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context |
dokunewon | 60:68924f211e71 | 168 | * This is used to handle extensions to standard requests |
dokunewon | 60:68924f211e71 | 169 | * and class specific requests |
dokunewon | 60:68924f211e71 | 170 | * |
dokunewon | 60:68924f211e71 | 171 | * @returns true if class handles this request |
dokunewon | 60:68924f211e71 | 172 | */ |
dokunewon | 60:68924f211e71 | 173 | virtual bool USBCallback_request(); |
dokunewon | 60:68924f211e71 | 174 | |
dokunewon | 60:68924f211e71 | 175 | /* |
dokunewon | 60:68924f211e71 | 176 | * Get string product descriptor |
dokunewon | 60:68924f211e71 | 177 | * |
dokunewon | 60:68924f211e71 | 178 | * @returns pointer to the string product descriptor |
dokunewon | 60:68924f211e71 | 179 | */ |
dokunewon | 60:68924f211e71 | 180 | virtual uint8_t * stringIproductDesc(); |
dokunewon | 60:68924f211e71 | 181 | |
dokunewon | 60:68924f211e71 | 182 | /* |
dokunewon | 60:68924f211e71 | 183 | * Get string interface descriptor |
dokunewon | 60:68924f211e71 | 184 | * |
dokunewon | 60:68924f211e71 | 185 | * @returns pointer to the string interface descriptor |
dokunewon | 60:68924f211e71 | 186 | */ |
dokunewon | 60:68924f211e71 | 187 | virtual uint8_t * stringIinterfaceDesc(); |
dokunewon | 60:68924f211e71 | 188 | |
dokunewon | 60:68924f211e71 | 189 | /* |
dokunewon | 60:68924f211e71 | 190 | * Get configuration descriptor |
dokunewon | 60:68924f211e71 | 191 | * |
dokunewon | 60:68924f211e71 | 192 | * @returns pointer to the configuration descriptor |
dokunewon | 60:68924f211e71 | 193 | */ |
dokunewon | 60:68924f211e71 | 194 | virtual uint8_t * configurationDesc(); |
dokunewon | 60:68924f211e71 | 195 | |
dokunewon | 60:68924f211e71 | 196 | /* |
dokunewon | 60:68924f211e71 | 197 | * Called by USBDevice layer. Set interface/alternate of the device. |
dokunewon | 60:68924f211e71 | 198 | * |
dokunewon | 60:68924f211e71 | 199 | * @param interface Number of the interface to be configured |
dokunewon | 60:68924f211e71 | 200 | * @param alternate Number of the alternate to be configured |
dokunewon | 60:68924f211e71 | 201 | * @returns true if class handles this request |
dokunewon | 60:68924f211e71 | 202 | */ |
dokunewon | 60:68924f211e71 | 203 | virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate); |
dokunewon | 60:68924f211e71 | 204 | |
dokunewon | 60:68924f211e71 | 205 | /* |
dokunewon | 60:68924f211e71 | 206 | * Called by USBDevice on Endpoint0 request completion |
dokunewon | 60:68924f211e71 | 207 | * if the 'notify' flag has been set to true. Warning: Called in ISR context |
dokunewon | 60:68924f211e71 | 208 | * |
dokunewon | 60:68924f211e71 | 209 | * In this case it is used to indicate that a HID report has |
dokunewon | 60:68924f211e71 | 210 | * been received from the host on endpoint 0 |
dokunewon | 60:68924f211e71 | 211 | * |
dokunewon | 60:68924f211e71 | 212 | * @param buf buffer received on endpoint 0 |
dokunewon | 60:68924f211e71 | 213 | * @param length length of this buffer |
dokunewon | 60:68924f211e71 | 214 | */ |
dokunewon | 60:68924f211e71 | 215 | virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length); |
dokunewon | 60:68924f211e71 | 216 | |
dokunewon | 60:68924f211e71 | 217 | /* |
dokunewon | 60:68924f211e71 | 218 | * Callback called on each Start of Frame event |
dokunewon | 60:68924f211e71 | 219 | */ |
dokunewon | 60:68924f211e71 | 220 | virtual void SOF(int frameNumber); |
dokunewon | 60:68924f211e71 | 221 | |
dokunewon | 60:68924f211e71 | 222 | /* |
dokunewon | 60:68924f211e71 | 223 | * Callback called when a packet is received |
dokunewon | 60:68924f211e71 | 224 | */ |
dokunewon | 60:68924f211e71 | 225 | virtual bool EPISO_OUT_callback(); |
dokunewon | 60:68924f211e71 | 226 | |
dokunewon | 60:68924f211e71 | 227 | /* |
dokunewon | 60:68924f211e71 | 228 | * Callback called when a packet has been sent |
dokunewon | 60:68924f211e71 | 229 | */ |
dokunewon | 60:68924f211e71 | 230 | virtual bool EPISO_IN_callback(); |
dokunewon | 60:68924f211e71 | 231 | |
dokunewon | 60:68924f211e71 | 232 | private: |
dokunewon | 60:68924f211e71 | 233 | |
dokunewon | 60:68924f211e71 | 234 | // stream available ? |
dokunewon | 60:68924f211e71 | 235 | volatile bool available; |
dokunewon | 60:68924f211e71 | 236 | |
dokunewon | 60:68924f211e71 | 237 | // interrupt OUT has been received |
dokunewon | 60:68924f211e71 | 238 | volatile bool interruptOUT; |
dokunewon | 60:68924f211e71 | 239 | |
dokunewon | 60:68924f211e71 | 240 | // interrupt IN has been received |
dokunewon | 60:68924f211e71 | 241 | volatile bool interruptIN; |
dokunewon | 60:68924f211e71 | 242 | |
dokunewon | 60:68924f211e71 | 243 | // audio packet has been written |
dokunewon | 60:68924f211e71 | 244 | volatile bool writeIN; |
dokunewon | 60:68924f211e71 | 245 | |
dokunewon | 60:68924f211e71 | 246 | // FREQ |
dokunewon | 60:68924f211e71 | 247 | uint32_t FREQ_OUT; |
dokunewon | 60:68924f211e71 | 248 | uint32_t FREQ_IN; |
dokunewon | 60:68924f211e71 | 249 | |
dokunewon | 60:68924f211e71 | 250 | // size of the maximum packet for the isochronous endpoint |
dokunewon | 60:68924f211e71 | 251 | uint32_t PACKET_SIZE_ISO_IN; |
dokunewon | 60:68924f211e71 | 252 | uint32_t PACKET_SIZE_ISO_OUT; |
dokunewon | 60:68924f211e71 | 253 | |
dokunewon | 60:68924f211e71 | 254 | // mono, stereo,... |
dokunewon | 60:68924f211e71 | 255 | uint8_t channel_nb_in; |
dokunewon | 60:68924f211e71 | 256 | uint8_t channel_nb_out; |
dokunewon | 60:68924f211e71 | 257 | |
dokunewon | 60:68924f211e71 | 258 | // channel config: master, left, right |
dokunewon | 60:68924f211e71 | 259 | uint8_t channel_config_in; |
dokunewon | 60:68924f211e71 | 260 | uint8_t channel_config_out; |
dokunewon | 60:68924f211e71 | 261 | |
dokunewon | 60:68924f211e71 | 262 | // mute state |
dokunewon | 60:68924f211e71 | 263 | uint8_t mute; |
dokunewon | 60:68924f211e71 | 264 | |
dokunewon | 60:68924f211e71 | 265 | // Volume Current Value |
dokunewon | 60:68924f211e71 | 266 | uint16_t volCur; |
dokunewon | 60:68924f211e71 | 267 | |
dokunewon | 60:68924f211e71 | 268 | // Volume Minimum Value |
dokunewon | 60:68924f211e71 | 269 | uint16_t volMin; |
dokunewon | 60:68924f211e71 | 270 | |
dokunewon | 60:68924f211e71 | 271 | // Volume Maximum Value |
dokunewon | 60:68924f211e71 | 272 | uint16_t volMax; |
dokunewon | 60:68924f211e71 | 273 | |
dokunewon | 60:68924f211e71 | 274 | // Volume Resolution |
dokunewon | 60:68924f211e71 | 275 | uint16_t volRes; |
dokunewon | 60:68924f211e71 | 276 | |
dokunewon | 60:68924f211e71 | 277 | // Buffer containing one audio packet (to be read) |
dokunewon | 60:68924f211e71 | 278 | volatile uint8_t * buf_stream_in; |
dokunewon | 60:68924f211e71 | 279 | |
dokunewon | 60:68924f211e71 | 280 | // Buffer containing one audio packet (to be written) |
dokunewon | 60:68924f211e71 | 281 | volatile uint8_t * buf_stream_out; |
dokunewon | 60:68924f211e71 | 282 | |
dokunewon | 60:68924f211e71 | 283 | // callback to update volume |
dokunewon | 60:68924f211e71 | 284 | FunctionPointer updateVol; |
dokunewon | 60:68924f211e71 | 285 | |
dokunewon | 60:68924f211e71 | 286 | // boolean showing that the SOF handler has been called. Useful for readNB. |
dokunewon | 60:68924f211e71 | 287 | volatile bool SOF_handler; |
dokunewon | 60:68924f211e71 | 288 | |
dokunewon | 60:68924f211e71 | 289 | volatile float volume; |
dokunewon | 60:68924f211e71 | 290 | |
dokunewon | 60:68924f211e71 | 291 | }; |
dokunewon | 60:68924f211e71 | 292 | |
dokunewon | 60:68924f211e71 | 293 | #endif |