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