335 final proj

Dependencies:   mbed USBDevice

Committer:
jresnik
Date:
Mon Nov 23 19:44:11 2020 +0000
Revision:
0:aee369fac3bd
Initial commit

Who changed what in which revision?

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