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