USB CDC (serial) and USB MSC (strage) Composite Device. http://mbed.org/users/okini3939/notebook/USB_Device/

Dependencies:   ChaNFSSD mbed ChaNFS

Committer:
okini3939
Date:
Fri Dec 23 16:37:58 2011 +0000
Revision:
2:5db90410bb90
Parent:
0:9b1d17d54055

        

Who changed what in which revision?

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