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 #include "stdint.h"
DieterGraef 0:0a2eaa300982 20 #include "USBHAL.h"
DieterGraef 0:0a2eaa300982 21 #include "USBHID.h"
DieterGraef 0:0a2eaa300982 22
DieterGraef 0:0a2eaa300982 23
DieterGraef 0:0a2eaa300982 24 USBHID::USBHID(uint16_t HW_Interface,uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect): USBDevice( HW_Interface,vendor_id, product_id, product_release)
DieterGraef 0:0a2eaa300982 25 {
DieterGraef 0:0a2eaa300982 26 output_length = output_report_length;
DieterGraef 0:0a2eaa300982 27 input_length = input_report_length;
DieterGraef 0:0a2eaa300982 28 if(connect) {
DieterGraef 0:0a2eaa300982 29 USBDevice::connect();
DieterGraef 0:0a2eaa300982 30 }
DieterGraef 0:0a2eaa300982 31 }
DieterGraef 0:0a2eaa300982 32
DieterGraef 0:0a2eaa300982 33
DieterGraef 0:0a2eaa300982 34 bool USBHID::send(HID_REPORT *report)
DieterGraef 0:0a2eaa300982 35 {
DieterGraef 0:0a2eaa300982 36 return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
DieterGraef 0:0a2eaa300982 37 }
DieterGraef 0:0a2eaa300982 38
DieterGraef 0:0a2eaa300982 39 bool USBHID::sendNB(HID_REPORT *report)
DieterGraef 0:0a2eaa300982 40 {
DieterGraef 0:0a2eaa300982 41 return writeNB(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
DieterGraef 0:0a2eaa300982 42 }
DieterGraef 0:0a2eaa300982 43
DieterGraef 0:0a2eaa300982 44
DieterGraef 0:0a2eaa300982 45 bool USBHID::read(HID_REPORT *report)
DieterGraef 0:0a2eaa300982 46 {
DieterGraef 0:0a2eaa300982 47 uint32_t bytesRead = 0;
DieterGraef 0:0a2eaa300982 48 bool result;
DieterGraef 0:0a2eaa300982 49 result = USBDevice::readEP(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
DieterGraef 0:0a2eaa300982 50 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
DieterGraef 0:0a2eaa300982 51 return false;
DieterGraef 0:0a2eaa300982 52 report->length = bytesRead;
DieterGraef 0:0a2eaa300982 53 return result;
DieterGraef 0:0a2eaa300982 54 }
DieterGraef 0:0a2eaa300982 55
DieterGraef 0:0a2eaa300982 56
DieterGraef 0:0a2eaa300982 57 bool USBHID::readNB(HID_REPORT *report)
DieterGraef 0:0a2eaa300982 58 {
DieterGraef 0:0a2eaa300982 59 uint32_t bytesRead = 0;
DieterGraef 0:0a2eaa300982 60 bool result;
DieterGraef 0:0a2eaa300982 61 result = USBDevice::readEP_NB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
DieterGraef 0:0a2eaa300982 62 // if readEP_NB did not succeed, does not issue a readStart
DieterGraef 0:0a2eaa300982 63 if (!result)
DieterGraef 0:0a2eaa300982 64 return false;
DieterGraef 0:0a2eaa300982 65 report->length = bytesRead;
DieterGraef 0:0a2eaa300982 66 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
DieterGraef 0:0a2eaa300982 67 return false;
DieterGraef 0:0a2eaa300982 68 return result;
DieterGraef 0:0a2eaa300982 69 }
DieterGraef 0:0a2eaa300982 70
DieterGraef 0:0a2eaa300982 71
DieterGraef 0:0a2eaa300982 72 uint16_t USBHID::reportDescLength() {
DieterGraef 0:0a2eaa300982 73 reportDesc();
DieterGraef 0:0a2eaa300982 74 return reportLength;
DieterGraef 0:0a2eaa300982 75 }
DieterGraef 0:0a2eaa300982 76
DieterGraef 0:0a2eaa300982 77
DieterGraef 0:0a2eaa300982 78
DieterGraef 0:0a2eaa300982 79 //
DieterGraef 0:0a2eaa300982 80 // Route callbacks from lower layers to class(es)
DieterGraef 0:0a2eaa300982 81 //
DieterGraef 0:0a2eaa300982 82
DieterGraef 0:0a2eaa300982 83
DieterGraef 0:0a2eaa300982 84 // Called in ISR context
DieterGraef 0:0a2eaa300982 85 // Called by USBDevice on Endpoint0 request
DieterGraef 0:0a2eaa300982 86 // This is used to handle extensions to standard requests
DieterGraef 0:0a2eaa300982 87 // and class specific requests
DieterGraef 0:0a2eaa300982 88 // Return true if class handles this request
DieterGraef 0:0a2eaa300982 89 bool USBHID::USBCallback_request() {
DieterGraef 0:0a2eaa300982 90 bool success = false;
DieterGraef 0:0a2eaa300982 91 CONTROL_TRANSFER * transfer = getTransferPtr();
DieterGraef 0:0a2eaa300982 92 uint8_t *hidDescriptor;
DieterGraef 0:0a2eaa300982 93
DieterGraef 0:0a2eaa300982 94 // Process additional standard requests
DieterGraef 0:0a2eaa300982 95
DieterGraef 0:0a2eaa300982 96 if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))
DieterGraef 0:0a2eaa300982 97 {
DieterGraef 0:0a2eaa300982 98 switch (transfer->setup.bRequest)
DieterGraef 0:0a2eaa300982 99 {
DieterGraef 0:0a2eaa300982 100 case GET_DESCRIPTOR:
DieterGraef 0:0a2eaa300982 101 switch (DESCRIPTOR_TYPE(transfer->setup.wValue))
DieterGraef 0:0a2eaa300982 102 {
DieterGraef 0:0a2eaa300982 103 case REPORT_DESCRIPTOR:
DieterGraef 0:0a2eaa300982 104 if ((reportDesc() != NULL) \
DieterGraef 0:0a2eaa300982 105 && (reportDescLength() != 0))
DieterGraef 0:0a2eaa300982 106 {
DieterGraef 0:0a2eaa300982 107 transfer->remaining = reportDescLength();
DieterGraef 0:0a2eaa300982 108 transfer->ptr = reportDesc();
DieterGraef 0:0a2eaa300982 109 transfer->direction = DEVICE_TO_HOST;
DieterGraef 0:0a2eaa300982 110 success = true;
DieterGraef 0:0a2eaa300982 111 }
DieterGraef 0:0a2eaa300982 112 break;
DieterGraef 0:0a2eaa300982 113 case HID_DESCRIPTOR:
DieterGraef 0:0a2eaa300982 114 // Find the HID descriptor, after the configuration descriptor
DieterGraef 0:0a2eaa300982 115 hidDescriptor = findDescriptor(HID_DESCRIPTOR);
DieterGraef 0:0a2eaa300982 116 if (hidDescriptor != NULL)
DieterGraef 0:0a2eaa300982 117 {
DieterGraef 0:0a2eaa300982 118 transfer->remaining = HID_DESCRIPTOR_LENGTH;
DieterGraef 0:0a2eaa300982 119 transfer->ptr = hidDescriptor;
DieterGraef 0:0a2eaa300982 120 transfer->direction = DEVICE_TO_HOST;
DieterGraef 0:0a2eaa300982 121 success = true;
DieterGraef 0:0a2eaa300982 122 }
DieterGraef 0:0a2eaa300982 123 break;
DieterGraef 0:0a2eaa300982 124
DieterGraef 0:0a2eaa300982 125 default:
DieterGraef 0:0a2eaa300982 126 break;
DieterGraef 0:0a2eaa300982 127 }
DieterGraef 0:0a2eaa300982 128 break;
DieterGraef 0:0a2eaa300982 129 default:
DieterGraef 0:0a2eaa300982 130 break;
DieterGraef 0:0a2eaa300982 131 }
DieterGraef 0:0a2eaa300982 132 }
DieterGraef 0:0a2eaa300982 133
DieterGraef 0:0a2eaa300982 134 // Process class-specific requests
DieterGraef 0:0a2eaa300982 135
DieterGraef 0:0a2eaa300982 136 if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
DieterGraef 0:0a2eaa300982 137 {
DieterGraef 0:0a2eaa300982 138 switch (transfer->setup.bRequest)
DieterGraef 0:0a2eaa300982 139 {
DieterGraef 0:0a2eaa300982 140 case SET_REPORT:
DieterGraef 0:0a2eaa300982 141 // First byte will be used for report ID
DieterGraef 0:0a2eaa300982 142 outputReport.data[0] = transfer->setup.wValue & 0xff;
DieterGraef 0:0a2eaa300982 143 outputReport.length = transfer->setup.wLength + 1;
DieterGraef 0:0a2eaa300982 144
DieterGraef 0:0a2eaa300982 145 transfer->remaining = sizeof(outputReport.data) - 1;
DieterGraef 0:0a2eaa300982 146 transfer->ptr = &outputReport.data[1];
DieterGraef 0:0a2eaa300982 147 transfer->direction = HOST_TO_DEVICE;
DieterGraef 0:0a2eaa300982 148 transfer->notify = true;
DieterGraef 0:0a2eaa300982 149 success = true;
DieterGraef 0:0a2eaa300982 150 default:
DieterGraef 0:0a2eaa300982 151 break;
DieterGraef 0:0a2eaa300982 152 }
DieterGraef 0:0a2eaa300982 153 }
DieterGraef 0:0a2eaa300982 154
DieterGraef 0:0a2eaa300982 155 return success;
DieterGraef 0:0a2eaa300982 156 }
DieterGraef 0:0a2eaa300982 157
DieterGraef 0:0a2eaa300982 158
DieterGraef 0:0a2eaa300982 159 #define DEFAULT_CONFIGURATION (1)
DieterGraef 0:0a2eaa300982 160
DieterGraef 0:0a2eaa300982 161
DieterGraef 0:0a2eaa300982 162 // Called in ISR context
DieterGraef 0:0a2eaa300982 163 // Set configuration. Return false if the
DieterGraef 0:0a2eaa300982 164 // configuration is not supported
DieterGraef 0:0a2eaa300982 165 bool USBHID::USBCallback_setConfiguration(uint8_t configuration) {
DieterGraef 0:0a2eaa300982 166 if (configuration != DEFAULT_CONFIGURATION) {
DieterGraef 0:0a2eaa300982 167 return false;
DieterGraef 0:0a2eaa300982 168 }
DieterGraef 0:0a2eaa300982 169
DieterGraef 0:0a2eaa300982 170 // Configure endpoints > 0
DieterGraef 0:0a2eaa300982 171 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
DieterGraef 0:0a2eaa300982 172 addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
DieterGraef 0:0a2eaa300982 173
DieterGraef 0:0a2eaa300982 174 // We activate the endpoint to be able to recceive data
DieterGraef 0:0a2eaa300982 175 readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
DieterGraef 0:0a2eaa300982 176 return true;
DieterGraef 0:0a2eaa300982 177 }
DieterGraef 0:0a2eaa300982 178
DieterGraef 0:0a2eaa300982 179
DieterGraef 0:0a2eaa300982 180 uint8_t * USBHID::stringIinterfaceDesc() {
DieterGraef 0:0a2eaa300982 181 static uint8_t stringIinterfaceDescriptor[] = {
DieterGraef 0:0a2eaa300982 182 0x08, //bLength
DieterGraef 0:0a2eaa300982 183 STRING_DESCRIPTOR, //bDescriptorType 0x03
DieterGraef 0:0a2eaa300982 184 'H',0,'I',0,'D',0, //bString iInterface - HID
DieterGraef 0:0a2eaa300982 185 };
DieterGraef 0:0a2eaa300982 186 return stringIinterfaceDescriptor;
DieterGraef 0:0a2eaa300982 187 }
DieterGraef 0:0a2eaa300982 188
DieterGraef 0:0a2eaa300982 189 uint8_t * USBHID::stringIproductDesc() {
DieterGraef 0:0a2eaa300982 190 static uint8_t stringIproductDescriptor[] = {
DieterGraef 0:0a2eaa300982 191 0x16, //bLength
DieterGraef 0:0a2eaa300982 192 STRING_DESCRIPTOR, //bDescriptorType 0x03
DieterGraef 0:0a2eaa300982 193 'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device
DieterGraef 0:0a2eaa300982 194 };
DieterGraef 0:0a2eaa300982 195 return stringIproductDescriptor;
DieterGraef 0:0a2eaa300982 196 }
DieterGraef 0:0a2eaa300982 197
DieterGraef 0:0a2eaa300982 198
DieterGraef 0:0a2eaa300982 199
DieterGraef 0:0a2eaa300982 200 uint8_t * USBHID::reportDesc() {
DieterGraef 0:0a2eaa300982 201 static uint8_t reportDescriptor[] = {
DieterGraef 0:0a2eaa300982 202 0x06, LSB(0xFFAB), MSB(0xFFAB),
DieterGraef 0:0a2eaa300982 203 0x0A, LSB(0x0200), MSB(0x0200),
DieterGraef 0:0a2eaa300982 204 0xA1, 0x01, // Collection 0x01
DieterGraef 0:0a2eaa300982 205 0x75, 0x08, // report size = 8 bits
DieterGraef 0:0a2eaa300982 206 0x15, 0x00, // logical minimum = 0
DieterGraef 0:0a2eaa300982 207 0x26, 0xFF, 0x00, // logical maximum = 255
DieterGraef 0:0a2eaa300982 208 0x95, input_length, // report count
DieterGraef 0:0a2eaa300982 209 0x09, 0x01, // usage
DieterGraef 0:0a2eaa300982 210 0x81, 0x02, // Input (array)
DieterGraef 0:0a2eaa300982 211 0x95, output_length,// report count
DieterGraef 0:0a2eaa300982 212 0x09, 0x02, // usage
DieterGraef 0:0a2eaa300982 213 0x91, 0x02, // Output (array)
DieterGraef 0:0a2eaa300982 214 0xC0 // end collection
DieterGraef 0:0a2eaa300982 215
DieterGraef 0:0a2eaa300982 216 };
DieterGraef 0:0a2eaa300982 217 reportLength = sizeof(reportDescriptor);
DieterGraef 0:0a2eaa300982 218 return reportDescriptor;
DieterGraef 0:0a2eaa300982 219 }
DieterGraef 0:0a2eaa300982 220
DieterGraef 0:0a2eaa300982 221 #define DEFAULT_CONFIGURATION (1)
DieterGraef 0:0a2eaa300982 222 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
DieterGraef 0:0a2eaa300982 223 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
DieterGraef 0:0a2eaa300982 224 + (1 * HID_DESCRIPTOR_LENGTH) \
DieterGraef 0:0a2eaa300982 225 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
DieterGraef 0:0a2eaa300982 226
DieterGraef 0:0a2eaa300982 227 uint8_t * USBHID::configurationDesc() {
DieterGraef 0:0a2eaa300982 228 static uint8_t configurationDescriptor[] = {
DieterGraef 0:0a2eaa300982 229 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
DieterGraef 0:0a2eaa300982 230 CONFIGURATION_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 231 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
DieterGraef 0:0a2eaa300982 232 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
DieterGraef 0:0a2eaa300982 233 0x01, // bNumInterfaces
DieterGraef 0:0a2eaa300982 234 DEFAULT_CONFIGURATION, // bConfigurationValue
DieterGraef 0:0a2eaa300982 235 0x00, // iConfiguration
DieterGraef 0:0a2eaa300982 236 C_RESERVED | C_SELF_POWERED, // bmAttributes
DieterGraef 0:0a2eaa300982 237 C_POWER(0), // bMaxPower
DieterGraef 0:0a2eaa300982 238
DieterGraef 0:0a2eaa300982 239 INTERFACE_DESCRIPTOR_LENGTH, // bLength
DieterGraef 0:0a2eaa300982 240 INTERFACE_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 241 0x00, // bInterfaceNumber
DieterGraef 0:0a2eaa300982 242 0x00, // bAlternateSetting
DieterGraef 0:0a2eaa300982 243 0x02, // bNumEndpoints
DieterGraef 0:0a2eaa300982 244 HID_CLASS, // bInterfaceClass
DieterGraef 0:0a2eaa300982 245 HID_SUBCLASS_NONE, // bInterfaceSubClass
DieterGraef 0:0a2eaa300982 246 HID_PROTOCOL_NONE, // bInterfaceProtocol
DieterGraef 0:0a2eaa300982 247 0x00, // iInterface
DieterGraef 0:0a2eaa300982 248
DieterGraef 0:0a2eaa300982 249 HID_DESCRIPTOR_LENGTH, // bLength
DieterGraef 0:0a2eaa300982 250 HID_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 251 LSB(HID_VERSION_1_11), // bcdHID (LSB)
DieterGraef 0:0a2eaa300982 252 MSB(HID_VERSION_1_11), // bcdHID (MSB)
DieterGraef 0:0a2eaa300982 253 0x00, // bCountryCode
DieterGraef 0:0a2eaa300982 254 0x01, // bNumDescriptors
DieterGraef 0:0a2eaa300982 255 REPORT_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 256 (uint8_t)(LSB(this->reportDescLength())), // wDescriptorLength (LSB)
DieterGraef 0:0a2eaa300982 257 (uint8_t)(MSB(this->reportDescLength())), // wDescriptorLength (MSB)
DieterGraef 0:0a2eaa300982 258
DieterGraef 0:0a2eaa300982 259 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
DieterGraef 0:0a2eaa300982 260 ENDPOINT_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 261 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
DieterGraef 0:0a2eaa300982 262 E_INTERRUPT, // bmAttributes
DieterGraef 0:0a2eaa300982 263 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
DieterGraef 0:0a2eaa300982 264 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
DieterGraef 0:0a2eaa300982 265 1, // bInterval (milliseconds)
DieterGraef 0:0a2eaa300982 266
DieterGraef 0:0a2eaa300982 267 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
DieterGraef 0:0a2eaa300982 268 ENDPOINT_DESCRIPTOR, // bDescriptorType
DieterGraef 0:0a2eaa300982 269 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
DieterGraef 0:0a2eaa300982 270 E_INTERRUPT, // bmAttributes
DieterGraef 0:0a2eaa300982 271 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
DieterGraef 0:0a2eaa300982 272 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
DieterGraef 0:0a2eaa300982 273 1, // bInterval (milliseconds)
DieterGraef 0:0a2eaa300982 274 };
DieterGraef 0:0a2eaa300982 275 return configurationDescriptor;
DieterGraef 0:0a2eaa300982 276 }