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