USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Sat Dec 16 10:26:48 2017 +0000
Revision:
11:b3f2a8bdac4d
Parent:
10:41552d038a69
A copy for D.S;

Who changed what in which revision?

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