dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
cyberjoey
Date:
Sat Oct 22 01:31:58 2016 +0000
Revision:
9:6bb35cef007d
Parent:
1:55a6170b404f
WORKING

Who changed what in which revision?

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