partly working USB Device lib for STM32F746NG Discovery both Interface are working

Dependents:   DISCO-F746NG-USB_Device McLighTT project_Keyboard_to_the_Keyboard MIDIInstrumentPADProject ... more

Committer:
DieterGraef
Date:
Sun Jul 31 17:47:35 2016 +0000
Revision:
0:0a2eaa300982
partly working USB Device library - serial and MIDI is working

Who changed what in which revision?

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