works!

Dependencies:   mbed

Committer:
mjoun
Date:
Fri Feb 21 20:40:56 2020 +0000
Revision:
1:6e512faaa17c
works

Who changed what in which revision?

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