Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
USBDevice/USBHID/USBHID.cpp@0:505207de8566, 2011-11-29 (annotated)
- Committer:
- znuh
- Date:
- Tue Nov 29 21:26:20 2011 +0000
- Revision:
- 0:505207de8566
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| znuh | 0:505207de8566 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
| znuh | 0:505207de8566 | 2 | * |
| znuh | 0:505207de8566 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| znuh | 0:505207de8566 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
| znuh | 0:505207de8566 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
| znuh | 0:505207de8566 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
| znuh | 0:505207de8566 | 7 | * Software is furnished to do so, subject to the following conditions: |
| znuh | 0:505207de8566 | 8 | * |
| znuh | 0:505207de8566 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
| znuh | 0:505207de8566 | 10 | * substantial portions of the Software. |
| znuh | 0:505207de8566 | 11 | * |
| znuh | 0:505207de8566 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| znuh | 0:505207de8566 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| znuh | 0:505207de8566 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| znuh | 0:505207de8566 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| znuh | 0:505207de8566 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| znuh | 0:505207de8566 | 17 | */ |
| znuh | 0:505207de8566 | 18 | |
| znuh | 0:505207de8566 | 19 | #include "stdint.h" |
| znuh | 0:505207de8566 | 20 | #include "USBBusInterface.h" |
| znuh | 0:505207de8566 | 21 | #include "USBHID.h" |
| znuh | 0:505207de8566 | 22 | |
| znuh | 0:505207de8566 | 23 | |
| znuh | 0:505207de8566 | 24 | USBHID::USBHID(uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) |
| znuh | 0:505207de8566 | 25 | { |
| znuh | 0:505207de8566 | 26 | output_length = output_report_length; |
| znuh | 0:505207de8566 | 27 | input_length = input_report_length; |
| znuh | 0:505207de8566 | 28 | USBDevice::connect(); |
| znuh | 0:505207de8566 | 29 | } |
| znuh | 0:505207de8566 | 30 | |
| znuh | 0:505207de8566 | 31 | |
| znuh | 0:505207de8566 | 32 | bool USBHID::send(HID_REPORT *report) |
| znuh | 0:505207de8566 | 33 | { |
| znuh | 0:505207de8566 | 34 | return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE); |
| znuh | 0:505207de8566 | 35 | } |
| znuh | 0:505207de8566 | 36 | |
| znuh | 0:505207de8566 | 37 | |
| znuh | 0:505207de8566 | 38 | bool USBHID::read(HID_REPORT *report) |
| znuh | 0:505207de8566 | 39 | { |
| znuh | 0:505207de8566 | 40 | uint16_t bytesRead = 0; |
| znuh | 0:505207de8566 | 41 | bool result; |
| znuh | 0:505207de8566 | 42 | result = USBDevice::read(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE); |
| znuh | 0:505207de8566 | 43 | if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE)) |
| znuh | 0:505207de8566 | 44 | return false; |
| znuh | 0:505207de8566 | 45 | report->length = bytesRead; |
| znuh | 0:505207de8566 | 46 | return result; |
| znuh | 0:505207de8566 | 47 | } |
| znuh | 0:505207de8566 | 48 | |
| znuh | 0:505207de8566 | 49 | |
| znuh | 0:505207de8566 | 50 | bool USBHID::readNB(HID_REPORT *report) |
| znuh | 0:505207de8566 | 51 | { |
| znuh | 0:505207de8566 | 52 | uint16_t bytesRead = 0; |
| znuh | 0:505207de8566 | 53 | bool result; |
| znuh | 0:505207de8566 | 54 | result = USBDevice::readNB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE); |
| znuh | 0:505207de8566 | 55 | report->length = bytesRead; |
| znuh | 0:505207de8566 | 56 | if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE)) |
| znuh | 0:505207de8566 | 57 | return false; |
| znuh | 0:505207de8566 | 58 | return result; |
| znuh | 0:505207de8566 | 59 | } |
| znuh | 0:505207de8566 | 60 | |
| znuh | 0:505207de8566 | 61 | |
| znuh | 0:505207de8566 | 62 | uint16_t USBHID::reportDescLength() { |
| znuh | 0:505207de8566 | 63 | reportDesc(); |
| znuh | 0:505207de8566 | 64 | return reportLength; |
| znuh | 0:505207de8566 | 65 | } |
| znuh | 0:505207de8566 | 66 | |
| znuh | 0:505207de8566 | 67 | |
| znuh | 0:505207de8566 | 68 | |
| znuh | 0:505207de8566 | 69 | // |
| znuh | 0:505207de8566 | 70 | // Route callbacks from lower layers to class(es) |
| znuh | 0:505207de8566 | 71 | // |
| znuh | 0:505207de8566 | 72 | |
| znuh | 0:505207de8566 | 73 | |
| znuh | 0:505207de8566 | 74 | // Called in ISR context |
| znuh | 0:505207de8566 | 75 | // Called by USBDevice on Endpoint0 request |
| znuh | 0:505207de8566 | 76 | // This is used to handle extensions to standard requests |
| znuh | 0:505207de8566 | 77 | // and class specific requests |
| znuh | 0:505207de8566 | 78 | // Return true if class handles this request |
| znuh | 0:505207de8566 | 79 | bool USBHID::USBCallback_request() { |
| znuh | 0:505207de8566 | 80 | bool success = false; |
| znuh | 0:505207de8566 | 81 | CONTROL_TRANSFER * transfer = getTransferPtr(); |
| znuh | 0:505207de8566 | 82 | uint8_t *hidDescriptor; |
| znuh | 0:505207de8566 | 83 | |
| znuh | 0:505207de8566 | 84 | // Process additional standard requests |
| znuh | 0:505207de8566 | 85 | |
| znuh | 0:505207de8566 | 86 | if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE)) |
| znuh | 0:505207de8566 | 87 | { |
| znuh | 0:505207de8566 | 88 | switch (transfer->setup.bRequest) |
| znuh | 0:505207de8566 | 89 | { |
| znuh | 0:505207de8566 | 90 | case GET_DESCRIPTOR: |
| znuh | 0:505207de8566 | 91 | switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) |
| znuh | 0:505207de8566 | 92 | { |
| znuh | 0:505207de8566 | 93 | case REPORT_DESCRIPTOR: |
| znuh | 0:505207de8566 | 94 | if ((reportDesc() != NULL) \ |
| znuh | 0:505207de8566 | 95 | && (reportDescLength() != 0)) |
| znuh | 0:505207de8566 | 96 | { |
| znuh | 0:505207de8566 | 97 | transfer->remaining = reportDescLength(); |
| znuh | 0:505207de8566 | 98 | transfer->ptr = reportDesc(); |
| znuh | 0:505207de8566 | 99 | transfer->direction = DEVICE_TO_HOST; |
| znuh | 0:505207de8566 | 100 | success = true; |
| znuh | 0:505207de8566 | 101 | } |
| znuh | 0:505207de8566 | 102 | break; |
| znuh | 0:505207de8566 | 103 | case HID_DESCRIPTOR: |
| znuh | 0:505207de8566 | 104 | // Find the HID descriptor, after the configuration descriptor |
| znuh | 0:505207de8566 | 105 | hidDescriptor = findDescriptor(HID_DESCRIPTOR); |
| znuh | 0:505207de8566 | 106 | if (hidDescriptor != NULL) |
| znuh | 0:505207de8566 | 107 | { |
| znuh | 0:505207de8566 | 108 | transfer->remaining = HID_DESCRIPTOR_LENGTH; |
| znuh | 0:505207de8566 | 109 | transfer->ptr = hidDescriptor; |
| znuh | 0:505207de8566 | 110 | transfer->direction = DEVICE_TO_HOST; |
| znuh | 0:505207de8566 | 111 | success = true; |
| znuh | 0:505207de8566 | 112 | } |
| znuh | 0:505207de8566 | 113 | break; |
| znuh | 0:505207de8566 | 114 | |
| znuh | 0:505207de8566 | 115 | default: |
| znuh | 0:505207de8566 | 116 | break; |
| znuh | 0:505207de8566 | 117 | } |
| znuh | 0:505207de8566 | 118 | break; |
| znuh | 0:505207de8566 | 119 | default: |
| znuh | 0:505207de8566 | 120 | break; |
| znuh | 0:505207de8566 | 121 | } |
| znuh | 0:505207de8566 | 122 | } |
| znuh | 0:505207de8566 | 123 | |
| znuh | 0:505207de8566 | 124 | // Process class-specific requests |
| znuh | 0:505207de8566 | 125 | |
| znuh | 0:505207de8566 | 126 | if (transfer->setup.bmRequestType.Type == CLASS_TYPE) |
| znuh | 0:505207de8566 | 127 | { |
| znuh | 0:505207de8566 | 128 | switch (transfer->setup.bRequest) |
| znuh | 0:505207de8566 | 129 | { |
| znuh | 0:505207de8566 | 130 | case SET_REPORT: |
| znuh | 0:505207de8566 | 131 | // First byte will be used for report ID |
| znuh | 0:505207de8566 | 132 | outputReport.data[0] = transfer->setup.wValue & 0xff; |
| znuh | 0:505207de8566 | 133 | outputReport.length = transfer->setup.wLength + 1; |
| znuh | 0:505207de8566 | 134 | |
| znuh | 0:505207de8566 | 135 | transfer->remaining = sizeof(outputReport.data) - 1; |
| znuh | 0:505207de8566 | 136 | transfer->ptr = &outputReport.data[1]; |
| znuh | 0:505207de8566 | 137 | transfer->direction = HOST_TO_DEVICE; |
| znuh | 0:505207de8566 | 138 | transfer->notify = true; |
| znuh | 0:505207de8566 | 139 | success = true; |
| znuh | 0:505207de8566 | 140 | default: |
| znuh | 0:505207de8566 | 141 | break; |
| znuh | 0:505207de8566 | 142 | } |
| znuh | 0:505207de8566 | 143 | } |
| znuh | 0:505207de8566 | 144 | |
| znuh | 0:505207de8566 | 145 | return success; |
| znuh | 0:505207de8566 | 146 | } |
| znuh | 0:505207de8566 | 147 | |
| znuh | 0:505207de8566 | 148 | |
| znuh | 0:505207de8566 | 149 | // Called in ISR context |
| znuh | 0:505207de8566 | 150 | // Called by USBDevice on Endpoint0 request completion |
| znuh | 0:505207de8566 | 151 | // if the 'notify' flag has been set to true |
| znuh | 0:505207de8566 | 152 | // In this case it is used to indicate that a HID report has |
| znuh | 0:505207de8566 | 153 | // been received from the host on endpoint 0 |
| znuh | 0:505207de8566 | 154 | void USBHID::USBCallback_requestCompleted() { |
| znuh | 0:505207de8566 | 155 | HID_callbackSetReport(&outputReport); |
| znuh | 0:505207de8566 | 156 | } |
| znuh | 0:505207de8566 | 157 | |
| znuh | 0:505207de8566 | 158 | #define DEFAULT_CONFIGURATION (1) |
| znuh | 0:505207de8566 | 159 | |
| znuh | 0:505207de8566 | 160 | |
| znuh | 0:505207de8566 | 161 | // Called in ISR context |
| znuh | 0:505207de8566 | 162 | // Set configuration. Return false if the |
| znuh | 0:505207de8566 | 163 | // configuration is not supported |
| znuh | 0:505207de8566 | 164 | bool USBHID::USBCallback_setConfiguration(uint8_t configuration) { |
| znuh | 0:505207de8566 | 165 | if (configuration != DEFAULT_CONFIGURATION) { |
| znuh | 0:505207de8566 | 166 | return false; |
| znuh | 0:505207de8566 | 167 | } |
| znuh | 0:505207de8566 | 168 | |
| znuh | 0:505207de8566 | 169 | // Configure endpoints > 0 |
| znuh | 0:505207de8566 | 170 | addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT); |
| znuh | 0:505207de8566 | 171 | addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT); |
| znuh | 0:505207de8566 | 172 | |
| znuh | 0:505207de8566 | 173 | // We activate the endpoint to be able to recceive data |
| znuh | 0:505207de8566 | 174 | readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT); |
| znuh | 0:505207de8566 | 175 | return true; |
| znuh | 0:505207de8566 | 176 | } |
| znuh | 0:505207de8566 | 177 | |
| znuh | 0:505207de8566 | 178 | uint8_t * USBHID::stringIinterfaceDesc() { |
| znuh | 0:505207de8566 | 179 | static uint8_t stringIinterfaceDescriptor[] = { |
| znuh | 0:505207de8566 | 180 | 0x08, //bLength |
| znuh | 0:505207de8566 | 181 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
| znuh | 0:505207de8566 | 182 | 'H',0,'I',0,'D',0, //bString iInterface - HID |
| znuh | 0:505207de8566 | 183 | }; |
| znuh | 0:505207de8566 | 184 | return stringIinterfaceDescriptor; |
| znuh | 0:505207de8566 | 185 | } |
| znuh | 0:505207de8566 | 186 | |
| znuh | 0:505207de8566 | 187 | uint8_t * USBHID::stringIproductDesc() { |
| znuh | 0:505207de8566 | 188 | static uint8_t stringIproductDescriptor[] = { |
| znuh | 0:505207de8566 | 189 | 0x16, //bLength |
| znuh | 0:505207de8566 | 190 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
| znuh | 0:505207de8566 | 191 | 'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device |
| znuh | 0:505207de8566 | 192 | }; |
| znuh | 0:505207de8566 | 193 | return stringIproductDescriptor; |
| znuh | 0:505207de8566 | 194 | } |
| znuh | 0:505207de8566 | 195 | |
| znuh | 0:505207de8566 | 196 | |
| znuh | 0:505207de8566 | 197 | |
| znuh | 0:505207de8566 | 198 | uint8_t * USBHID::reportDesc() { |
| znuh | 0:505207de8566 | 199 | static uint8_t reportDescriptor[] = { |
| znuh | 0:505207de8566 | 200 | 0x06, LSB(0xFFAB), MSB(0xFFAB), |
| znuh | 0:505207de8566 | 201 | 0x0A, LSB(0x0200), MSB(0x0200), |
| znuh | 0:505207de8566 | 202 | 0xA1, 0x01, // Collection 0x01 |
| znuh | 0:505207de8566 | 203 | 0x75, 0x08, // report size = 8 bits |
| znuh | 0:505207de8566 | 204 | 0x15, 0x00, // logical minimum = 0 |
| znuh | 0:505207de8566 | 205 | 0x26, 0xFF, 0x00, // logical maximum = 255 |
| znuh | 0:505207de8566 | 206 | 0x95, input_length, // report count |
| znuh | 0:505207de8566 | 207 | 0x09, 0x01, // usage |
| znuh | 0:505207de8566 | 208 | 0x81, 0x02, // Input (array) |
| znuh | 0:505207de8566 | 209 | 0x95, output_length, // report count |
| znuh | 0:505207de8566 | 210 | 0x09, 0x02, // usage |
| znuh | 0:505207de8566 | 211 | 0x91, 0x02, // Output (array) |
| znuh | 0:505207de8566 | 212 | 0xC0 // end collection |
| znuh | 0:505207de8566 | 213 | |
| znuh | 0:505207de8566 | 214 | }; |
| znuh | 0:505207de8566 | 215 | reportLength = sizeof(reportDescriptor); |
| znuh | 0:505207de8566 | 216 | return reportDescriptor; |
| znuh | 0:505207de8566 | 217 | } |
| znuh | 0:505207de8566 | 218 | |
| znuh | 0:505207de8566 | 219 | #define DEFAULT_CONFIGURATION (1) |
| znuh | 0:505207de8566 | 220 | #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ |
| znuh | 0:505207de8566 | 221 | + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ |
| znuh | 0:505207de8566 | 222 | + (1 * HID_DESCRIPTOR_LENGTH) \ |
| znuh | 0:505207de8566 | 223 | + (2 * ENDPOINT_DESCRIPTOR_LENGTH)) |
| znuh | 0:505207de8566 | 224 | |
| znuh | 0:505207de8566 | 225 | uint8_t * USBHID::configurationDesc() { |
| znuh | 0:505207de8566 | 226 | static uint8_t configurationDescriptor[] = { |
| znuh | 0:505207de8566 | 227 | CONFIGURATION_DESCRIPTOR_LENGTH,// bLength |
| znuh | 0:505207de8566 | 228 | CONFIGURATION_DESCRIPTOR, // bDescriptorType |
| znuh | 0:505207de8566 | 229 | LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) |
| znuh | 0:505207de8566 | 230 | MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) |
| znuh | 0:505207de8566 | 231 | 0x01, // bNumInterfaces |
| znuh | 0:505207de8566 | 232 | DEFAULT_CONFIGURATION, // bConfigurationValue |
| znuh | 0:505207de8566 | 233 | 0x00, // iConfiguration |
| znuh | 0:505207de8566 | 234 | C_RESERVED | C_SELF_POWERED, // bmAttributes |
| znuh | 0:505207de8566 | 235 | C_POWER(0), // bMaxPower |
| znuh | 0:505207de8566 | 236 | |
| znuh | 0:505207de8566 | 237 | INTERFACE_DESCRIPTOR_LENGTH, // bLength |
| znuh | 0:505207de8566 | 238 | INTERFACE_DESCRIPTOR, // bDescriptorType |
| znuh | 0:505207de8566 | 239 | 0x00, // bInterfaceNumber |
| znuh | 0:505207de8566 | 240 | 0x00, // bAlternateSetting |
| znuh | 0:505207de8566 | 241 | 0x02, // bNumEndpoints |
| znuh | 0:505207de8566 | 242 | HID_CLASS, // bInterfaceClass |
| znuh | 0:505207de8566 | 243 | HID_SUBCLASS_NONE, // bInterfaceSubClass |
| znuh | 0:505207de8566 | 244 | HID_PROTOCOL_NONE, // bInterfaceProtocol |
| znuh | 0:505207de8566 | 245 | 0x00, // iInterface |
| znuh | 0:505207de8566 | 246 | |
| znuh | 0:505207de8566 | 247 | HID_DESCRIPTOR_LENGTH, // bLength |
| znuh | 0:505207de8566 | 248 | HID_DESCRIPTOR, // bDescriptorType |
| znuh | 0:505207de8566 | 249 | LSB(HID_VERSION_1_11), // bcdHID (LSB) |
| znuh | 0:505207de8566 | 250 | MSB(HID_VERSION_1_11), // bcdHID (MSB) |
| znuh | 0:505207de8566 | 251 | 0x00, // bCountryCode |
| znuh | 0:505207de8566 | 252 | 0x01, // bNumDescriptors |
| znuh | 0:505207de8566 | 253 | REPORT_DESCRIPTOR, // bDescriptorType |
| znuh | 0:505207de8566 | 254 | LSB(this->reportDescLength()), // wDescriptorLength (LSB) |
| znuh | 0:505207de8566 | 255 | MSB(this->reportDescLength()), // wDescriptorLength (MSB) |
| znuh | 0:505207de8566 | 256 | |
| znuh | 0:505207de8566 | 257 | ENDPOINT_DESCRIPTOR_LENGTH, // bLength |
| znuh | 0:505207de8566 | 258 | ENDPOINT_DESCRIPTOR, // bDescriptorType |
| znuh | 0:505207de8566 | 259 | PHY_TO_DESC(EPINT_IN), // bEndpointAddress |
| znuh | 0:505207de8566 | 260 | E_INTERRUPT, // bmAttributes |
| znuh | 0:505207de8566 | 261 | LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) |
| znuh | 0:505207de8566 | 262 | MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) |
| znuh | 0:505207de8566 | 263 | 10, // bInterval (milliseconds) |
| znuh | 0:505207de8566 | 264 | |
| znuh | 0:505207de8566 | 265 | ENDPOINT_DESCRIPTOR_LENGTH, // bLength |
| znuh | 0:505207de8566 | 266 | ENDPOINT_DESCRIPTOR, // bDescriptorType |
| znuh | 0:505207de8566 | 267 | PHY_TO_DESC(EPINT_OUT), // bEndpointAddress |
| znuh | 0:505207de8566 | 268 | E_INTERRUPT, // bmAttributes |
| znuh | 0:505207de8566 | 269 | LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) |
| znuh | 0:505207de8566 | 270 | MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) |
| znuh | 0:505207de8566 | 271 | 10, // bInterval (milliseconds) |
| znuh | 0:505207de8566 | 272 | }; |
| znuh | 0:505207de8566 | 273 | return configurationDescriptor; |
| znuh | 0:505207de8566 | 274 | } |