USB device stack

Fork of USBDevice by mbed official

Committer:
nikitamere
Date:
Fri Sep 16 01:03:54 2016 +0000
Revision:
66:b6940e641a15
Modified

Who changed what in which revision?

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