USBAudio example using a microphone

Dependencies:   USBDevice mbed

Committer:
samux
Date:
Fri Dec 16 12:31:41 2011 +0000
Revision:
0:539ec61e1fbb
works with m0 and m3 (sinus) but the code is different to have the same result...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:539ec61e1fbb 1 /* USBDevice.h */
samux 0:539ec61e1fbb 2 /* Generic USB device */
samux 0:539ec61e1fbb 3 /* Copyright (c) 2011 ARM Limited. All rights reserved. */
samux 0:539ec61e1fbb 4
samux 0:539ec61e1fbb 5 #ifndef USBDEVICE_H
samux 0:539ec61e1fbb 6 #define USBDEVICE_H
samux 0:539ec61e1fbb 7
samux 0:539ec61e1fbb 8 #include "mbed.h"
samux 0:539ec61e1fbb 9 #include "USBDevice_Types.h"
samux 0:539ec61e1fbb 10 #include "USBBusInterface.h"
samux 0:539ec61e1fbb 11
samux 0:539ec61e1fbb 12
samux 0:539ec61e1fbb 13
samux 0:539ec61e1fbb 14 class USBDevice: public USBHAL
samux 0:539ec61e1fbb 15 {
samux 0:539ec61e1fbb 16 public:
samux 0:539ec61e1fbb 17 USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
samux 0:539ec61e1fbb 18
samux 0:539ec61e1fbb 19 /*
samux 0:539ec61e1fbb 20 * Check if the device is configured
samux 0:539ec61e1fbb 21 *
samux 0:539ec61e1fbb 22 * @returns true if configured, false otherwise
samux 0:539ec61e1fbb 23 */
samux 0:539ec61e1fbb 24 bool configured(void);
samux 0:539ec61e1fbb 25
samux 0:539ec61e1fbb 26 /*
samux 0:539ec61e1fbb 27 * Connect a device
samux 0:539ec61e1fbb 28 */
samux 0:539ec61e1fbb 29 void connect(void);
samux 0:539ec61e1fbb 30
samux 0:539ec61e1fbb 31 /*
samux 0:539ec61e1fbb 32 * Disconnect a device
samux 0:539ec61e1fbb 33 */
samux 0:539ec61e1fbb 34 void disconnect(void);
samux 0:539ec61e1fbb 35
samux 0:539ec61e1fbb 36 /*
samux 0:539ec61e1fbb 37 * Add an endpoint
samux 0:539ec61e1fbb 38 *
samux 0:539ec61e1fbb 39 * @param endpoint endpoint which will be added
samux 0:539ec61e1fbb 40 * @param maxPacket Maximum size of a packet which can be sent for this endpoint
samux 0:539ec61e1fbb 41 * @returns true if successful, false otherwise
samux 0:539ec61e1fbb 42 */
samux 0:539ec61e1fbb 43 bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);
samux 0:539ec61e1fbb 44
samux 0:539ec61e1fbb 45 /*
samux 0:539ec61e1fbb 46 * Start a reading on a certain endpoint.
samux 0:539ec61e1fbb 47 * You can access the result of the reading by USBDevice_read
samux 0:539ec61e1fbb 48 *
samux 0:539ec61e1fbb 49 * @param endpoint endpoint which will be read
samux 0:539ec61e1fbb 50 * @param maxSize the maximum length that can be read
samux 0:539ec61e1fbb 51 * @return true if successful
samux 0:539ec61e1fbb 52 */
samux 0:539ec61e1fbb 53 bool readStart(uint8_t endpoint, uint16_t maxSize);
samux 0:539ec61e1fbb 54
samux 0:539ec61e1fbb 55 /*
samux 0:539ec61e1fbb 56 * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
samux 0:539ec61e1fbb 57 * must be called.
samux 0:539ec61e1fbb 58 *
samux 0:539ec61e1fbb 59 * Warning: blocking
samux 0:539ec61e1fbb 60 *
samux 0:539ec61e1fbb 61 * @param endpoint endpoint which will be read
samux 0:539ec61e1fbb 62 * @param buffer buffer will be filled with the data received
samux 0:539ec61e1fbb 63 * @param size the number of bytes read will be stored in *size
samux 0:539ec61e1fbb 64 * @param maxSize the maximum length that can be read
samux 0:539ec61e1fbb 65 * @returns true if successful
samux 0:539ec61e1fbb 66 */
samux 0:539ec61e1fbb 67 bool readEP(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize);
samux 0:539ec61e1fbb 68
samux 0:539ec61e1fbb 69 /*
samux 0:539ec61e1fbb 70 * Read a certain endpoint.
samux 0:539ec61e1fbb 71 *
samux 0:539ec61e1fbb 72 * Warning: non blocking
samux 0:539ec61e1fbb 73 *
samux 0:539ec61e1fbb 74 * @param endpoint endpoint which will be read
samux 0:539ec61e1fbb 75 * @param buffer buffer will be filled with the data received (if data are available)
samux 0:539ec61e1fbb 76 * @param size the number of bytes read will be stored in *size
samux 0:539ec61e1fbb 77 * @param maxSize the maximum length that can be read
samux 0:539ec61e1fbb 78 * @returns true if successful
samux 0:539ec61e1fbb 79 */
samux 0:539ec61e1fbb 80 bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize);
samux 0:539ec61e1fbb 81
samux 0:539ec61e1fbb 82 /*
samux 0:539ec61e1fbb 83 * Write a certain endpoint.
samux 0:539ec61e1fbb 84 *
samux 0:539ec61e1fbb 85 * Warning: blocking
samux 0:539ec61e1fbb 86 *
samux 0:539ec61e1fbb 87 * @param endpoint endpoint to write
samux 0:539ec61e1fbb 88 * @param buffer data contained in buffer will be write
samux 0:539ec61e1fbb 89 * @param size the number of bytes to write
samux 0:539ec61e1fbb 90 * @param maxSize the maximum length that can be written on this endpoint
samux 0:539ec61e1fbb 91 */
samux 0:539ec61e1fbb 92 bool write(uint8_t endpoint, uint8_t * buffer, uint16_t size, uint16_t maxSize);
samux 0:539ec61e1fbb 93
samux 0:539ec61e1fbb 94
samux 0:539ec61e1fbb 95 /*
samux 0:539ec61e1fbb 96 * Write a certain endpoint.
samux 0:539ec61e1fbb 97 *
samux 0:539ec61e1fbb 98 * Warning: non blocking
samux 0:539ec61e1fbb 99 *
samux 0:539ec61e1fbb 100 * @param endpoint endpoint to write
samux 0:539ec61e1fbb 101 * @param buffer data contained in buffer will be write
samux 0:539ec61e1fbb 102 * @param size the number of bytes to write
samux 0:539ec61e1fbb 103 * @param maxSize the maximum length that can be written on this endpoint
samux 0:539ec61e1fbb 104 */
samux 0:539ec61e1fbb 105 bool writeNB(uint8_t endpoint, uint8_t * buffer, uint16_t size, uint16_t maxSize);
samux 0:539ec61e1fbb 106
samux 0:539ec61e1fbb 107
samux 0:539ec61e1fbb 108 /*
samux 0:539ec61e1fbb 109 * Called by USBDevice layer on bus reset. Warning: Called in ISR context
samux 0:539ec61e1fbb 110 *
samux 0:539ec61e1fbb 111 * May be used to reset state
samux 0:539ec61e1fbb 112 */
samux 0:539ec61e1fbb 113 virtual void USBCallback_busReset(void) {};
samux 0:539ec61e1fbb 114
samux 0:539ec61e1fbb 115 /*
samux 0:539ec61e1fbb 116 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
samux 0:539ec61e1fbb 117 * This is used to handle extensions to standard requests
samux 0:539ec61e1fbb 118 * and class specific requests
samux 0:539ec61e1fbb 119 *
samux 0:539ec61e1fbb 120 * @returns true if class handles this request
samux 0:539ec61e1fbb 121 */
samux 0:539ec61e1fbb 122 virtual bool USBCallback_request() { return false; };
samux 0:539ec61e1fbb 123
samux 0:539ec61e1fbb 124 /*
samux 0:539ec61e1fbb 125 * Called by USBDevice on Endpoint0 request completion
samux 0:539ec61e1fbb 126 * if the 'notify' flag has been set to true. Warning: Called in ISR context
samux 0:539ec61e1fbb 127 *
samux 0:539ec61e1fbb 128 * In this case it is used to indicate that a HID report has
samux 0:539ec61e1fbb 129 * been received from the host on endpoint 0
samux 0:539ec61e1fbb 130 *
samux 0:539ec61e1fbb 131 * @param buf buffer received on endpoint 0
samux 0:539ec61e1fbb 132 * @param length length of this buffer
samux 0:539ec61e1fbb 133 */
samux 0:539ec61e1fbb 134 virtual void USBCallback_requestCompleted(uint8_t * buf, uint16_t length) {};
samux 0:539ec61e1fbb 135
samux 0:539ec61e1fbb 136 /*
samux 0:539ec61e1fbb 137 * Called by USBDevice layer. Set configuration of the device.
samux 0:539ec61e1fbb 138 * For instance, you can add all endpoints that you need on this function.
samux 0:539ec61e1fbb 139 *
samux 0:539ec61e1fbb 140 * @param configuration Number of the configuration
samux 0:539ec61e1fbb 141 */
samux 0:539ec61e1fbb 142 virtual bool USBCallback_setConfiguration(uint8_t configuration) { return false; };
samux 0:539ec61e1fbb 143
samux 0:539ec61e1fbb 144 /*
samux 0:539ec61e1fbb 145 * Called by USBDevice layer. Set interface/alternate of the device.
samux 0:539ec61e1fbb 146 *
samux 0:539ec61e1fbb 147 * @param interface Number of the interface to be configured
samux 0:539ec61e1fbb 148 * @param alternate Number of the alternate to be configured
samux 0:539ec61e1fbb 149 * @returns true if class handles this request
samux 0:539ec61e1fbb 150 */
samux 0:539ec61e1fbb 151 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
samux 0:539ec61e1fbb 152
samux 0:539ec61e1fbb 153 /*
samux 0:539ec61e1fbb 154 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
samux 0:539ec61e1fbb 155 *
samux 0:539ec61e1fbb 156 * @returns pointer to the device descriptor
samux 0:539ec61e1fbb 157 */
samux 0:539ec61e1fbb 158 virtual uint8_t * deviceDesc();
samux 0:539ec61e1fbb 159
samux 0:539ec61e1fbb 160 /*
samux 0:539ec61e1fbb 161 * Get configuration descriptor
samux 0:539ec61e1fbb 162 *
samux 0:539ec61e1fbb 163 * @returns pointer to the configuration descriptor
samux 0:539ec61e1fbb 164 */
samux 0:539ec61e1fbb 165 virtual uint8_t * configurationDesc(){return NULL;};
samux 0:539ec61e1fbb 166
samux 0:539ec61e1fbb 167 /*
samux 0:539ec61e1fbb 168 * Get string lang id descriptor
samux 0:539ec61e1fbb 169 *
samux 0:539ec61e1fbb 170 * @return pointer to the string lang id descriptor
samux 0:539ec61e1fbb 171 */
samux 0:539ec61e1fbb 172 virtual uint8_t * stringLangidDesc();
samux 0:539ec61e1fbb 173
samux 0:539ec61e1fbb 174 /*
samux 0:539ec61e1fbb 175 * Get string manufacturer descriptor
samux 0:539ec61e1fbb 176 *
samux 0:539ec61e1fbb 177 * @returns pointer to the string manufacturer descriptor
samux 0:539ec61e1fbb 178 */
samux 0:539ec61e1fbb 179 virtual uint8_t * stringImanufacturerDesc();
samux 0:539ec61e1fbb 180
samux 0:539ec61e1fbb 181 /*
samux 0:539ec61e1fbb 182 * Get string product descriptor
samux 0:539ec61e1fbb 183 *
samux 0:539ec61e1fbb 184 * @returns pointer to the string product descriptor
samux 0:539ec61e1fbb 185 */
samux 0:539ec61e1fbb 186 virtual uint8_t * stringIproductDesc();
samux 0:539ec61e1fbb 187
samux 0:539ec61e1fbb 188 /*
samux 0:539ec61e1fbb 189 * Get string serial descriptor
samux 0:539ec61e1fbb 190 *
samux 0:539ec61e1fbb 191 * @returns pointer to the string serial descriptor
samux 0:539ec61e1fbb 192 */
samux 0:539ec61e1fbb 193 virtual uint8_t * stringIserialDesc();
samux 0:539ec61e1fbb 194
samux 0:539ec61e1fbb 195 /*
samux 0:539ec61e1fbb 196 * Get string configuration descriptor
samux 0:539ec61e1fbb 197 *
samux 0:539ec61e1fbb 198 * @returns pointer to the string configuration descriptor
samux 0:539ec61e1fbb 199 */
samux 0:539ec61e1fbb 200 virtual uint8_t * stringIConfigurationDesc();
samux 0:539ec61e1fbb 201
samux 0:539ec61e1fbb 202 /*
samux 0:539ec61e1fbb 203 * Get string interface descriptor
samux 0:539ec61e1fbb 204 *
samux 0:539ec61e1fbb 205 * @returns pointer to the string interface descriptor
samux 0:539ec61e1fbb 206 */
samux 0:539ec61e1fbb 207 virtual uint8_t * stringIinterfaceDesc();
samux 0:539ec61e1fbb 208
samux 0:539ec61e1fbb 209 /*
samux 0:539ec61e1fbb 210 * Get the length of the report descriptor
samux 0:539ec61e1fbb 211 *
samux 0:539ec61e1fbb 212 * @returns length of the report descriptor
samux 0:539ec61e1fbb 213 */
samux 0:539ec61e1fbb 214 virtual uint16_t reportDescLength() { return 0; };
samux 0:539ec61e1fbb 215
samux 0:539ec61e1fbb 216
samux 0:539ec61e1fbb 217
samux 0:539ec61e1fbb 218 protected:
samux 0:539ec61e1fbb 219 virtual void busReset(void);
samux 0:539ec61e1fbb 220 virtual void EP0setupCallback(void);
samux 0:539ec61e1fbb 221 virtual void EP0out(void);
samux 0:539ec61e1fbb 222 virtual void EP0in(void);
samux 0:539ec61e1fbb 223 virtual void connectStateChanged(unsigned int connected);
samux 0:539ec61e1fbb 224 virtual void suspendStateChanged(unsigned int suspended);
samux 0:539ec61e1fbb 225 uint8_t * findDescriptor(uint8_t descriptorType);
samux 0:539ec61e1fbb 226 CONTROL_TRANSFER * getTransferPtr(void);
samux 0:539ec61e1fbb 227
samux 0:539ec61e1fbb 228 uint16_t VENDOR_ID;
samux 0:539ec61e1fbb 229 uint16_t PRODUCT_ID;
samux 0:539ec61e1fbb 230 uint16_t PRODUCT_RELEASE;
samux 0:539ec61e1fbb 231
samux 0:539ec61e1fbb 232 private:
samux 0:539ec61e1fbb 233 bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);
samux 0:539ec61e1fbb 234 bool requestGetDescriptor(void);
samux 0:539ec61e1fbb 235 bool controlOut(void);
samux 0:539ec61e1fbb 236 bool controlIn(void);
samux 0:539ec61e1fbb 237 bool requestSetAddress(void);
samux 0:539ec61e1fbb 238 bool requestSetConfiguration(void);
samux 0:539ec61e1fbb 239 bool requestSetFeature(void);
samux 0:539ec61e1fbb 240 bool requestClearFeature(void);
samux 0:539ec61e1fbb 241 bool requestGetStatus(void);
samux 0:539ec61e1fbb 242 bool requestSetup(void);
samux 0:539ec61e1fbb 243 bool controlSetup(void);
samux 0:539ec61e1fbb 244 void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet);
samux 0:539ec61e1fbb 245 bool requestGetConfiguration(void);
samux 0:539ec61e1fbb 246 bool requestGetInterface(void);
samux 0:539ec61e1fbb 247 bool requestSetInterface(void);
samux 0:539ec61e1fbb 248
samux 0:539ec61e1fbb 249 CONTROL_TRANSFER transfer;
samux 0:539ec61e1fbb 250 USB_DEVICE device;
samux 0:539ec61e1fbb 251
samux 0:539ec61e1fbb 252 uint16_t currentInterface;
samux 0:539ec61e1fbb 253 uint8_t currentAlternate;
samux 0:539ec61e1fbb 254 };
samux 0:539ec61e1fbb 255
samux 0:539ec61e1fbb 256
samux 0:539ec61e1fbb 257 #endif