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