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