library

Dependents:   USB_CDC_MSD_Hello

Committer:
sherckuith
Date:
Fri Aug 24 02:01:51 2012 +0000
Revision:
0:d5bb9a9c3e24
[mbed] converted /USB_CDC_MSD_Hello/USBDevice

Who changed what in which revision?

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