Kalibriersoftware Stromwerte

Dependencies:   Matrix mbed

Committer:
Racer01014
Date:
Mon Nov 23 16:09:54 2015 +0000
Revision:
0:5e35c180ed4a
-

Who changed what in which revision?

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