A copy of the mbed USBDevice with USBSerial library

Dependents:   STM32L0_LoRa Smartage STM32L0_LoRa Turtle_RadioShuttle

Committer:
Helmut Tschemernjak
Date:
Sun Feb 24 14:52:33 2019 +0100
Revision:
8:961423d1da74
Parent:
0:a3ea811f80f2
Added sleep manager support to avoids sleeps while a USB CDC
connection is active

Who changed what in which revision?

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