This is a data logger program to be implemented with an instrument amplifier.

Dependencies:   mbed

Committer:
KISScientific
Date:
Tue Apr 04 18:01:11 2017 +0000
Revision:
0:d75ca4e39672
This is a data logger program.

Who changed what in which revision?

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