USBDevice stack

Dependents:   erg USBSerial_HelloWorld Slingshot SuperScanner ... more

Committer:
samux
Date:
Wed May 30 18:28:11 2012 +0000
Revision:
0:140cdf8e2d60

        

Who changed what in which revision?

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