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