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