Base station firmware

Committer:
Perijah
Date:
Tue May 24 13:16:55 2016 +0000
Revision:
0:ecc3925d3f8c
Base station firmware

Who changed what in which revision?

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