USB Composite support

Dependents:   mbed_cdc_hid_composite

Fork of USBDevice by mbed official

Committer:
steeven
Date:
Sun May 31 15:36:50 2015 +0000
Revision:
55:7c559fcb1d17
Parent:
25:7c72828865f3
Make USBDevice support Composite

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 2:34856a6adb5b 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 2:34856a6adb5b 2 *
samux 2:34856a6adb5b 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 2:34856a6adb5b 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 2:34856a6adb5b 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 2:34856a6adb5b 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 2:34856a6adb5b 7 * Software is furnished to do so, subject to the following conditions:
samux 2:34856a6adb5b 8 *
samux 2:34856a6adb5b 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 2:34856a6adb5b 10 * substantial portions of the Software.
samux 2:34856a6adb5b 11 *
samux 2:34856a6adb5b 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 2:34856a6adb5b 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 2:34856a6adb5b 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 2:34856a6adb5b 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 2:34856a6adb5b 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 2:34856a6adb5b 17 */
samux 2:34856a6adb5b 18
samux 2:34856a6adb5b 19 #ifndef USBDEVICE_H
samux 2:34856a6adb5b 20 #define USBDEVICE_H
samux 2:34856a6adb5b 21
samux 2:34856a6adb5b 22 #include "mbed.h"
samux 2:34856a6adb5b 23 #include "USBDevice_Types.h"
samux 2:34856a6adb5b 24 #include "USBHAL.h"
steeven 55:7c559fcb1d17 25 #include "USBInterface.h"
steeven 55:7c559fcb1d17 26
samux 2:34856a6adb5b 27
samux 2:34856a6adb5b 28 class USBDevice: public USBHAL
samux 2:34856a6adb5b 29 {
samux 2:34856a6adb5b 30 public:
steeven 55:7c559fcb1d17 31 USBDevice(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001);
mbed_official 25:7c72828865f3 32
samux 2:34856a6adb5b 33 /*
samux 2:34856a6adb5b 34 * Check if the device is configured
samux 2:34856a6adb5b 35 *
samux 2:34856a6adb5b 36 * @returns true if configured, false otherwise
samux 2:34856a6adb5b 37 */
samux 2:34856a6adb5b 38 bool configured(void);
mbed_official 25:7c72828865f3 39
samux 2:34856a6adb5b 40 /*
samux 2:34856a6adb5b 41 * Connect a device
mbed_official 25:7c72828865f3 42 *
mbed_official 18:78bdbce94509 43 * @param blocking: block if not configured
samux 2:34856a6adb5b 44 */
mbed_official 18:78bdbce94509 45 void connect(bool blocking = true);
mbed_official 25:7c72828865f3 46
samux 2:34856a6adb5b 47 /*
samux 2:34856a6adb5b 48 * Disconnect a device
samux 2:34856a6adb5b 49 */
samux 2:34856a6adb5b 50 void disconnect(void);
mbed_official 25:7c72828865f3 51
samux 2:34856a6adb5b 52 /*
samux 2:34856a6adb5b 53 * Add an endpoint
samux 2:34856a6adb5b 54 *
samux 2:34856a6adb5b 55 * @param endpoint endpoint which will be added
samux 2:34856a6adb5b 56 * @param maxPacket Maximum size of a packet which can be sent for this endpoint
samux 2:34856a6adb5b 57 * @returns true if successful, false otherwise
samux 2:34856a6adb5b 58 */
samux 2:34856a6adb5b 59 bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);
samux 2:34856a6adb5b 60
samux 2:34856a6adb5b 61 /*
samux 2:34856a6adb5b 62 * Start a reading on a certain endpoint.
samux 2:34856a6adb5b 63 * You can access the result of the reading by USBDevice_read
samux 2:34856a6adb5b 64 *
samux 2:34856a6adb5b 65 * @param endpoint endpoint which will be read
samux 2:34856a6adb5b 66 * @param maxSize the maximum length that can be read
samux 2:34856a6adb5b 67 * @return true if successful
samux 2:34856a6adb5b 68 */
samux 2:34856a6adb5b 69 bool readStart(uint8_t endpoint, uint32_t maxSize);
mbed_official 25:7c72828865f3 70
samux 2:34856a6adb5b 71 /*
samux 2:34856a6adb5b 72 * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
samux 2:34856a6adb5b 73 * must be called.
samux 2:34856a6adb5b 74 *
samux 2:34856a6adb5b 75 * Warning: blocking
samux 2:34856a6adb5b 76 *
samux 2:34856a6adb5b 77 * @param endpoint endpoint which will be read
samux 2:34856a6adb5b 78 * @param buffer buffer will be filled with the data received
samux 2:34856a6adb5b 79 * @param size the number of bytes read will be stored in *size
samux 2:34856a6adb5b 80 * @param maxSize the maximum length that can be read
samux 2:34856a6adb5b 81 * @returns true if successful
samux 2:34856a6adb5b 82 */
samux 2:34856a6adb5b 83 bool readEP(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
mbed_official 25:7c72828865f3 84
samux 2:34856a6adb5b 85 /*
samux 2:34856a6adb5b 86 * Read a certain endpoint.
samux 2:34856a6adb5b 87 *
samux 2:34856a6adb5b 88 * Warning: non blocking
samux 2:34856a6adb5b 89 *
samux 2:34856a6adb5b 90 * @param endpoint endpoint which will be read
mbed_official 25:7c72828865f3 91 * @param buffer buffer will be filled with the data received (if data are available)
samux 2:34856a6adb5b 92 * @param size the number of bytes read will be stored in *size
samux 2:34856a6adb5b 93 * @param maxSize the maximum length that can be read
samux 2:34856a6adb5b 94 * @returns true if successful
samux 2:34856a6adb5b 95 */
samux 2:34856a6adb5b 96 bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
mbed_official 25:7c72828865f3 97
samux 2:34856a6adb5b 98 /*
samux 2:34856a6adb5b 99 * Write a certain endpoint.
samux 2:34856a6adb5b 100 *
samux 2:34856a6adb5b 101 * Warning: blocking
samux 2:34856a6adb5b 102 *
samux 2:34856a6adb5b 103 * @param endpoint endpoint to write
samux 2:34856a6adb5b 104 * @param buffer data contained in buffer will be write
samux 2:34856a6adb5b 105 * @param size the number of bytes to write
samux 2:34856a6adb5b 106 * @param maxSize the maximum length that can be written on this endpoint
samux 2:34856a6adb5b 107 */
samux 2:34856a6adb5b 108 bool write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
mbed_official 25:7c72828865f3 109
mbed_official 25:7c72828865f3 110
samux 2:34856a6adb5b 111 /*
samux 2:34856a6adb5b 112 * Write a certain endpoint.
samux 2:34856a6adb5b 113 *
samux 2:34856a6adb5b 114 * Warning: non blocking
samux 2:34856a6adb5b 115 *
samux 2:34856a6adb5b 116 * @param endpoint endpoint to write
samux 2:34856a6adb5b 117 * @param buffer data contained in buffer will be write
samux 2:34856a6adb5b 118 * @param size the number of bytes to write
samux 2:34856a6adb5b 119 * @param maxSize the maximum length that can be written on this endpoint
samux 2:34856a6adb5b 120 */
samux 2:34856a6adb5b 121 bool writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
samux 2:34856a6adb5b 122
mbed_official 25:7c72828865f3 123
samux 2:34856a6adb5b 124 /*
samux 2:34856a6adb5b 125 * Called by USBDevice layer on bus reset. Warning: Called in ISR context
samux 2:34856a6adb5b 126 *
samux 2:34856a6adb5b 127 * May be used to reset state
samux 2:34856a6adb5b 128 */
samux 2:34856a6adb5b 129 virtual void USBCallback_busReset(void) {};
mbed_official 25:7c72828865f3 130
steeven 55:7c559fcb1d17 131
samux 2:34856a6adb5b 132 /*
samux 2:34856a6adb5b 133 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
samux 2:34856a6adb5b 134 * This is used to handle extensions to standard requests
samux 2:34856a6adb5b 135 * and class specific requests
samux 2:34856a6adb5b 136 *
samux 2:34856a6adb5b 137 * @returns true if class handles this request
samux 2:34856a6adb5b 138 */
steeven 55:7c559fcb1d17 139 virtual bool USBCallback_request() {
steeven 55:7c559fcb1d17 140 if (_intf)
steeven 55:7c559fcb1d17 141 return _intf->USBCallback_request();
steeven 55:7c559fcb1d17 142 else
steeven 55:7c559fcb1d17 143 return false;
steeven 55:7c559fcb1d17 144 };
mbed_official 25:7c72828865f3 145
samux 2:34856a6adb5b 146 /*
samux 2:34856a6adb5b 147 * Called by USBDevice on Endpoint0 request completion
samux 2:34856a6adb5b 148 * if the 'notify' flag has been set to true. Warning: Called in ISR context
samux 2:34856a6adb5b 149 *
samux 2:34856a6adb5b 150 * In this case it is used to indicate that a HID report has
samux 2:34856a6adb5b 151 * been received from the host on endpoint 0
samux 2:34856a6adb5b 152 *
samux 2:34856a6adb5b 153 * @param buf buffer received on endpoint 0
samux 2:34856a6adb5b 154 * @param length length of this buffer
samux 2:34856a6adb5b 155 */
steeven 55:7c559fcb1d17 156 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {
steeven 55:7c559fcb1d17 157 if (_intf)
steeven 55:7c559fcb1d17 158 _intf->USBCallback_requestCompleted(buf, length);
steeven 55:7c559fcb1d17 159 };
mbed_official 25:7c72828865f3 160
samux 2:34856a6adb5b 161 /*
samux 2:34856a6adb5b 162 * Called by USBDevice layer. Set configuration of the device.
samux 2:34856a6adb5b 163 * For instance, you can add all endpoints that you need on this function.
samux 2:34856a6adb5b 164 *
samux 2:34856a6adb5b 165 * @param configuration Number of the configuration
samux 2:34856a6adb5b 166 */
steeven 55:7c559fcb1d17 167 virtual bool USBCallback_setConfiguration(uint8_t configuration) {
steeven 55:7c559fcb1d17 168 if (_intf)
steeven 55:7c559fcb1d17 169 return _intf->USBCallback_setConfiguration(configuration);
steeven 55:7c559fcb1d17 170 return false;
steeven 55:7c559fcb1d17 171 };
mbed_official 25:7c72828865f3 172
samux 2:34856a6adb5b 173 /*
samux 2:34856a6adb5b 174 * Called by USBDevice layer. Set interface/alternate of the device.
samux 2:34856a6adb5b 175 *
samux 2:34856a6adb5b 176 * @param interface Number of the interface to be configured
samux 2:34856a6adb5b 177 * @param alternate Number of the alternate to be configured
samux 2:34856a6adb5b 178 * @returns true if class handles this request
samux 2:34856a6adb5b 179 */
samux 2:34856a6adb5b 180 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
samux 2:34856a6adb5b 181
samux 2:34856a6adb5b 182 /*
samux 2:34856a6adb5b 183 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
samux 2:34856a6adb5b 184 *
samux 2:34856a6adb5b 185 * @returns pointer to the device descriptor
samux 2:34856a6adb5b 186 */
samux 2:34856a6adb5b 187 virtual uint8_t * deviceDesc();
mbed_official 25:7c72828865f3 188
samux 2:34856a6adb5b 189 /*
samux 2:34856a6adb5b 190 * Get configuration descriptor
samux 2:34856a6adb5b 191 *
samux 2:34856a6adb5b 192 * @returns pointer to the configuration descriptor
samux 2:34856a6adb5b 193 */
steeven 55:7c559fcb1d17 194 virtual uint8_t * configurationDesc(){
steeven 55:7c559fcb1d17 195 if (_intf)
steeven 55:7c559fcb1d17 196 return _intf->configurationDesc();
steeven 55:7c559fcb1d17 197 return NULL;
steeven 55:7c559fcb1d17 198 };
mbed_official 25:7c72828865f3 199
samux 2:34856a6adb5b 200 /*
samux 2:34856a6adb5b 201 * Get string lang id descriptor
samux 2:34856a6adb5b 202 *
samux 2:34856a6adb5b 203 * @return pointer to the string lang id descriptor
samux 2:34856a6adb5b 204 */
samux 2:34856a6adb5b 205 virtual uint8_t * stringLangidDesc();
mbed_official 25:7c72828865f3 206
samux 2:34856a6adb5b 207 /*
samux 2:34856a6adb5b 208 * Get string manufacturer descriptor
samux 2:34856a6adb5b 209 *
samux 2:34856a6adb5b 210 * @returns pointer to the string manufacturer descriptor
samux 2:34856a6adb5b 211 */
samux 2:34856a6adb5b 212 virtual uint8_t * stringImanufacturerDesc();
mbed_official 25:7c72828865f3 213
samux 2:34856a6adb5b 214 /*
samux 2:34856a6adb5b 215 * Get string product descriptor
samux 2:34856a6adb5b 216 *
samux 2:34856a6adb5b 217 * @returns pointer to the string product descriptor
samux 2:34856a6adb5b 218 */
samux 2:34856a6adb5b 219 virtual uint8_t * stringIproductDesc();
mbed_official 25:7c72828865f3 220
samux 2:34856a6adb5b 221 /*
samux 2:34856a6adb5b 222 * Get string serial descriptor
samux 2:34856a6adb5b 223 *
samux 2:34856a6adb5b 224 * @returns pointer to the string serial descriptor
samux 2:34856a6adb5b 225 */
samux 2:34856a6adb5b 226 virtual uint8_t * stringIserialDesc();
mbed_official 25:7c72828865f3 227
samux 2:34856a6adb5b 228 /*
samux 2:34856a6adb5b 229 * Get string configuration descriptor
samux 2:34856a6adb5b 230 *
samux 2:34856a6adb5b 231 * @returns pointer to the string configuration descriptor
samux 2:34856a6adb5b 232 */
samux 2:34856a6adb5b 233 virtual uint8_t * stringIConfigurationDesc();
mbed_official 25:7c72828865f3 234
samux 2:34856a6adb5b 235 /*
samux 2:34856a6adb5b 236 * Get string interface descriptor
samux 2:34856a6adb5b 237 *
samux 2:34856a6adb5b 238 * @returns pointer to the string interface descriptor
samux 2:34856a6adb5b 239 */
samux 2:34856a6adb5b 240 virtual uint8_t * stringIinterfaceDesc();
mbed_official 25:7c72828865f3 241
samux 2:34856a6adb5b 242 /*
samux 2:34856a6adb5b 243 * Get the length of the report descriptor
samux 2:34856a6adb5b 244 *
samux 2:34856a6adb5b 245 * @returns length of the report descriptor
samux 2:34856a6adb5b 246 */
samux 2:34856a6adb5b 247 virtual uint16_t reportDescLength() { return 0; };
mbed_official 25:7c72828865f3 248
steeven 55:7c559fcb1d17 249 CONTROL_TRANSFER * getTransferPtr(void);
samux 2:34856a6adb5b 250
steeven 55:7c559fcb1d17 251 virtual bool bind(USBInterface *intf) {
steeven 55:7c559fcb1d17 252 _intf = intf;
steeven 55:7c559fcb1d17 253 return true;
steeven 55:7c559fcb1d17 254 }
steeven 55:7c559fcb1d17 255
steeven 55:7c559fcb1d17 256 uint16_t VENDOR_ID;
steeven 55:7c559fcb1d17 257 uint16_t PRODUCT_ID;
steeven 55:7c559fcb1d17 258 uint16_t PRODUCT_RELEASE;
samux 2:34856a6adb5b 259
samux 2:34856a6adb5b 260 protected:
samux 2:34856a6adb5b 261 virtual void busReset(void);
samux 2:34856a6adb5b 262 virtual void EP0setupCallback(void);
samux 2:34856a6adb5b 263 virtual void EP0out(void);
samux 2:34856a6adb5b 264 virtual void EP0in(void);
samux 2:34856a6adb5b 265 virtual void connectStateChanged(unsigned int connected);
samux 2:34856a6adb5b 266 virtual void suspendStateChanged(unsigned int suspended);
samux 2:34856a6adb5b 267 uint8_t * findDescriptor(uint8_t descriptorType);
mbed_official 25:7c72828865f3 268
samux 2:34856a6adb5b 269
samux 2:34856a6adb5b 270 private:
samux 2:34856a6adb5b 271 bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);
samux 2:34856a6adb5b 272 bool requestGetDescriptor(void);
samux 2:34856a6adb5b 273 bool controlOut(void);
samux 2:34856a6adb5b 274 bool controlIn(void);
samux 2:34856a6adb5b 275 bool requestSetAddress(void);
samux 2:34856a6adb5b 276 bool requestSetConfiguration(void);
samux 2:34856a6adb5b 277 bool requestSetFeature(void);
samux 2:34856a6adb5b 278 bool requestClearFeature(void);
samux 2:34856a6adb5b 279 bool requestGetStatus(void);
samux 2:34856a6adb5b 280 bool requestSetup(void);
samux 2:34856a6adb5b 281 bool controlSetup(void);
samux 2:34856a6adb5b 282 void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet);
samux 2:34856a6adb5b 283 bool requestGetConfiguration(void);
samux 2:34856a6adb5b 284 bool requestGetInterface(void);
samux 2:34856a6adb5b 285 bool requestSetInterface(void);
samux 2:34856a6adb5b 286
samux 2:34856a6adb5b 287 CONTROL_TRANSFER transfer;
samux 2:34856a6adb5b 288 USB_DEVICE device;
mbed_official 25:7c72828865f3 289
samux 2:34856a6adb5b 290 uint16_t currentInterface;
samux 2:34856a6adb5b 291 uint8_t currentAlternate;
steeven 55:7c559fcb1d17 292
steeven 55:7c559fcb1d17 293 USBInterface *_intf;
samux 2:34856a6adb5b 294 };
samux 2:34856a6adb5b 295
samux 2:34856a6adb5b 296
samux 2:34856a6adb5b 297 #endif