Kalibriersoftware Stromwerte

Dependencies:   Matrix mbed

Committer:
Racer01014
Date:
Sat Nov 28 13:50:31 2015 +0000
Revision:
1:ea5f520dcdc1
Parent:
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 "USBCDC.h"
Racer01014 0:5e35c180ed4a 21
Racer01014 0:5e35c180ed4a 22 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
Racer01014 0:5e35c180ed4a 23
Racer01014 0:5e35c180ed4a 24 #define DEFAULT_CONFIGURATION (1)
Racer01014 0:5e35c180ed4a 25
Racer01014 0:5e35c180ed4a 26 #define CDC_SET_LINE_CODING 0x20
Racer01014 0:5e35c180ed4a 27 #define CDC_GET_LINE_CODING 0x21
Racer01014 0:5e35c180ed4a 28 #define CDC_SET_CONTROL_LINE_STATE 0x22
Racer01014 0:5e35c180ed4a 29
Racer01014 0:5e35c180ed4a 30 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
Racer01014 0:5e35c180ed4a 31
Racer01014 0:5e35c180ed4a 32 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
Racer01014 0:5e35c180ed4a 33 terminal_connected = false;
Racer01014 0:5e35c180ed4a 34 USBDevice::connect();
Racer01014 0:5e35c180ed4a 35 }
Racer01014 0:5e35c180ed4a 36
Racer01014 0:5e35c180ed4a 37 bool USBCDC::USBCallback_request(void) {
Racer01014 0:5e35c180ed4a 38 /* Called in ISR context */
Racer01014 0:5e35c180ed4a 39
Racer01014 0:5e35c180ed4a 40 bool success = false;
Racer01014 0:5e35c180ed4a 41 CONTROL_TRANSFER * transfer = getTransferPtr();
Racer01014 0:5e35c180ed4a 42
Racer01014 0:5e35c180ed4a 43 /* Process class-specific requests */
Racer01014 0:5e35c180ed4a 44
Racer01014 0:5e35c180ed4a 45 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
Racer01014 0:5e35c180ed4a 46 switch (transfer->setup.bRequest) {
Racer01014 0:5e35c180ed4a 47 case CDC_GET_LINE_CODING:
Racer01014 0:5e35c180ed4a 48 transfer->remaining = 7;
Racer01014 0:5e35c180ed4a 49 transfer->ptr = cdc_line_coding;
Racer01014 0:5e35c180ed4a 50 transfer->direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 51 success = true;
Racer01014 0:5e35c180ed4a 52 break;
Racer01014 0:5e35c180ed4a 53 case CDC_SET_LINE_CODING:
Racer01014 0:5e35c180ed4a 54 transfer->remaining = 7;
Racer01014 0:5e35c180ed4a 55 success = true;
Racer01014 0:5e35c180ed4a 56 terminal_connected = true;
Racer01014 0:5e35c180ed4a 57 break;
Racer01014 0:5e35c180ed4a 58 case CDC_SET_CONTROL_LINE_STATE:
Racer01014 0:5e35c180ed4a 59 terminal_connected = false;
Racer01014 0:5e35c180ed4a 60 success = true;
Racer01014 0:5e35c180ed4a 61 break;
Racer01014 0:5e35c180ed4a 62 default:
Racer01014 0:5e35c180ed4a 63 break;
Racer01014 0:5e35c180ed4a 64 }
Racer01014 0:5e35c180ed4a 65 }
Racer01014 0:5e35c180ed4a 66
Racer01014 0:5e35c180ed4a 67 return success;
Racer01014 0:5e35c180ed4a 68 }
Racer01014 0:5e35c180ed4a 69
Racer01014 0:5e35c180ed4a 70
Racer01014 0:5e35c180ed4a 71 // Called in ISR context
Racer01014 0:5e35c180ed4a 72 // Set configuration. Return false if the
Racer01014 0:5e35c180ed4a 73 // configuration is not supported.
Racer01014 0:5e35c180ed4a 74 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
Racer01014 0:5e35c180ed4a 75 if (configuration != DEFAULT_CONFIGURATION) {
Racer01014 0:5e35c180ed4a 76 return false;
Racer01014 0:5e35c180ed4a 77 }
Racer01014 0:5e35c180ed4a 78
Racer01014 0:5e35c180ed4a 79 // Configure endpoints > 0
Racer01014 0:5e35c180ed4a 80 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
Racer01014 0:5e35c180ed4a 81 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
Racer01014 0:5e35c180ed4a 82 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Racer01014 0:5e35c180ed4a 83
Racer01014 0:5e35c180ed4a 84 // We activate the endpoint to be able to recceive data
Racer01014 0:5e35c180ed4a 85 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Racer01014 0:5e35c180ed4a 86 return true;
Racer01014 0:5e35c180ed4a 87 }
Racer01014 0:5e35c180ed4a 88
Racer01014 0:5e35c180ed4a 89 bool USBCDC::send(uint8_t * buffer, uint32_t size) {
Racer01014 0:5e35c180ed4a 90 return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
Racer01014 0:5e35c180ed4a 91 }
Racer01014 0:5e35c180ed4a 92
Racer01014 0:5e35c180ed4a 93 bool USBCDC::readEP(uint8_t * buffer, uint32_t * size) {
Racer01014 0:5e35c180ed4a 94 if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
Racer01014 0:5e35c180ed4a 95 return false;
Racer01014 0:5e35c180ed4a 96 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
Racer01014 0:5e35c180ed4a 97 return false;
Racer01014 0:5e35c180ed4a 98 return true;
Racer01014 0:5e35c180ed4a 99 }
Racer01014 0:5e35c180ed4a 100
Racer01014 0:5e35c180ed4a 101 bool USBCDC::readEP_NB(uint8_t * buffer, uint32_t * size) {
Racer01014 0:5e35c180ed4a 102 if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
Racer01014 0:5e35c180ed4a 103 return false;
Racer01014 0:5e35c180ed4a 104 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
Racer01014 0:5e35c180ed4a 105 return false;
Racer01014 0:5e35c180ed4a 106 return true;
Racer01014 0:5e35c180ed4a 107 }
Racer01014 0:5e35c180ed4a 108
Racer01014 0:5e35c180ed4a 109
Racer01014 0:5e35c180ed4a 110 uint8_t * USBCDC::deviceDesc() {
Racer01014 0:5e35c180ed4a 111 static uint8_t deviceDescriptor[] = {
Racer01014 0:5e35c180ed4a 112 18, // bLength
Racer01014 0:5e35c180ed4a 113 1, // bDescriptorType
Racer01014 0:5e35c180ed4a 114 0x10, 0x01, // bcdUSB
Racer01014 0:5e35c180ed4a 115 2, // bDeviceClass
Racer01014 0:5e35c180ed4a 116 0, // bDeviceSubClass
Racer01014 0:5e35c180ed4a 117 0, // bDeviceProtocol
Racer01014 0:5e35c180ed4a 118 MAX_PACKET_SIZE_EP0, // bMaxPacketSize0
Racer01014 0:5e35c180ed4a 119 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
Racer01014 0:5e35c180ed4a 120 LSB(PRODUCT_ID), MSB(PRODUCT_ID),// idProduct
Racer01014 0:5e35c180ed4a 121 0x00, 0x01, // bcdDevice
Racer01014 0:5e35c180ed4a 122 1, // iManufacturer
Racer01014 0:5e35c180ed4a 123 2, // iProduct
Racer01014 0:5e35c180ed4a 124 3, // iSerialNumber
Racer01014 0:5e35c180ed4a 125 1 // bNumConfigurations
Racer01014 0:5e35c180ed4a 126 };
Racer01014 0:5e35c180ed4a 127 return deviceDescriptor;
Racer01014 0:5e35c180ed4a 128 }
Racer01014 0:5e35c180ed4a 129
Racer01014 0:5e35c180ed4a 130 uint8_t * USBCDC::stringIinterfaceDesc() {
Racer01014 0:5e35c180ed4a 131 static uint8_t stringIinterfaceDescriptor[] = {
Racer01014 0:5e35c180ed4a 132 0x08,
Racer01014 0:5e35c180ed4a 133 STRING_DESCRIPTOR,
Racer01014 0:5e35c180ed4a 134 'C',0,'D',0,'C',0,
Racer01014 0:5e35c180ed4a 135 };
Racer01014 0:5e35c180ed4a 136 return stringIinterfaceDescriptor;
Racer01014 0:5e35c180ed4a 137 }
Racer01014 0:5e35c180ed4a 138
Racer01014 0:5e35c180ed4a 139 uint8_t * USBCDC::stringIproductDesc() {
Racer01014 0:5e35c180ed4a 140 static uint8_t stringIproductDescriptor[] = {
Racer01014 0:5e35c180ed4a 141 0x16,
Racer01014 0:5e35c180ed4a 142 STRING_DESCRIPTOR,
Racer01014 0:5e35c180ed4a 143 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
Racer01014 0:5e35c180ed4a 144 };
Racer01014 0:5e35c180ed4a 145 return stringIproductDescriptor;
Racer01014 0:5e35c180ed4a 146 }
Racer01014 0:5e35c180ed4a 147
Racer01014 0:5e35c180ed4a 148
Racer01014 0:5e35c180ed4a 149 #define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7)
Racer01014 0:5e35c180ed4a 150
Racer01014 0:5e35c180ed4a 151 uint8_t * USBCDC::configurationDesc() {
Racer01014 0:5e35c180ed4a 152 static uint8_t configDescriptor[] = {
Racer01014 0:5e35c180ed4a 153 // configuration descriptor
Racer01014 0:5e35c180ed4a 154 9, // bLength
Racer01014 0:5e35c180ed4a 155 2, // bDescriptorType
Racer01014 0:5e35c180ed4a 156 LSB(CONFIG1_DESC_SIZE), // wTotalLength
Racer01014 0:5e35c180ed4a 157 MSB(CONFIG1_DESC_SIZE),
Racer01014 0:5e35c180ed4a 158 2, // bNumInterfaces
Racer01014 0:5e35c180ed4a 159 1, // bConfigurationValue
Racer01014 0:5e35c180ed4a 160 0, // iConfiguration
Racer01014 0:5e35c180ed4a 161 0x80, // bmAttributes
Racer01014 0:5e35c180ed4a 162 50, // bMaxPower
Racer01014 0:5e35c180ed4a 163
Racer01014 0:5e35c180ed4a 164 // IAD to associate the two CDC interfaces
Racer01014 0:5e35c180ed4a 165 0x08, // bLength
Racer01014 0:5e35c180ed4a 166 0x0b, // bDescriptorType
Racer01014 0:5e35c180ed4a 167 0x00, // bFirstInterface
Racer01014 0:5e35c180ed4a 168 0x02, // bInterfaceCount
Racer01014 0:5e35c180ed4a 169 0x02, // bFunctionClass
Racer01014 0:5e35c180ed4a 170 0x02, // bFunctionSubClass
Racer01014 0:5e35c180ed4a 171 0, // bFunctionProtocol
Racer01014 0:5e35c180ed4a 172 0, // iFunction
Racer01014 0:5e35c180ed4a 173
Racer01014 0:5e35c180ed4a 174 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
Racer01014 0:5e35c180ed4a 175 9, // bLength
Racer01014 0:5e35c180ed4a 176 4, // bDescriptorType
Racer01014 0:5e35c180ed4a 177 0, // bInterfaceNumber
Racer01014 0:5e35c180ed4a 178 0, // bAlternateSetting
Racer01014 0:5e35c180ed4a 179 1, // bNumEndpoints
Racer01014 0:5e35c180ed4a 180 0x02, // bInterfaceClass
Racer01014 0:5e35c180ed4a 181 0x02, // bInterfaceSubClass
Racer01014 0:5e35c180ed4a 182 0x01, // bInterfaceProtocol
Racer01014 0:5e35c180ed4a 183 0, // iInterface
Racer01014 0:5e35c180ed4a 184
Racer01014 0:5e35c180ed4a 185 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
Racer01014 0:5e35c180ed4a 186 5, // bFunctionLength
Racer01014 0:5e35c180ed4a 187 0x24, // bDescriptorType
Racer01014 0:5e35c180ed4a 188 0x00, // bDescriptorSubtype
Racer01014 0:5e35c180ed4a 189 0x10, 0x01, // bcdCDC
Racer01014 0:5e35c180ed4a 190
Racer01014 0:5e35c180ed4a 191 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
Racer01014 0:5e35c180ed4a 192 5, // bFunctionLength
Racer01014 0:5e35c180ed4a 193 0x24, // bDescriptorType
Racer01014 0:5e35c180ed4a 194 0x01, // bDescriptorSubtype
Racer01014 0:5e35c180ed4a 195 0x03, // bmCapabilities
Racer01014 0:5e35c180ed4a 196 1, // bDataInterface
Racer01014 0:5e35c180ed4a 197
Racer01014 0:5e35c180ed4a 198 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
Racer01014 0:5e35c180ed4a 199 4, // bFunctionLength
Racer01014 0:5e35c180ed4a 200 0x24, // bDescriptorType
Racer01014 0:5e35c180ed4a 201 0x02, // bDescriptorSubtype
Racer01014 0:5e35c180ed4a 202 0x06, // bmCapabilities
Racer01014 0:5e35c180ed4a 203
Racer01014 0:5e35c180ed4a 204 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
Racer01014 0:5e35c180ed4a 205 5, // bFunctionLength
Racer01014 0:5e35c180ed4a 206 0x24, // bDescriptorType
Racer01014 0:5e35c180ed4a 207 0x06, // bDescriptorSubtype
Racer01014 0:5e35c180ed4a 208 0, // bMasterInterface
Racer01014 0:5e35c180ed4a 209 1, // bSlaveInterface0
Racer01014 0:5e35c180ed4a 210
Racer01014 0:5e35c180ed4a 211 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Racer01014 0:5e35c180ed4a 212 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Racer01014 0:5e35c180ed4a 213 ENDPOINT_DESCRIPTOR, // bDescriptorType
Racer01014 0:5e35c180ed4a 214 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
Racer01014 0:5e35c180ed4a 215 E_INTERRUPT, // bmAttributes (0x03=intr)
Racer01014 0:5e35c180ed4a 216 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Racer01014 0:5e35c180ed4a 217 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Racer01014 0:5e35c180ed4a 218 16, // bInterval
Racer01014 0:5e35c180ed4a 219
Racer01014 0:5e35c180ed4a 220
Racer01014 0:5e35c180ed4a 221
Racer01014 0:5e35c180ed4a 222
Racer01014 0:5e35c180ed4a 223 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
Racer01014 0:5e35c180ed4a 224 9, // bLength
Racer01014 0:5e35c180ed4a 225 4, // bDescriptorType
Racer01014 0:5e35c180ed4a 226 1, // bInterfaceNumber
Racer01014 0:5e35c180ed4a 227 0, // bAlternateSetting
Racer01014 0:5e35c180ed4a 228 2, // bNumEndpoints
Racer01014 0:5e35c180ed4a 229 0x0A, // bInterfaceClass
Racer01014 0:5e35c180ed4a 230 0x00, // bInterfaceSubClass
Racer01014 0:5e35c180ed4a 231 0x00, // bInterfaceProtocol
Racer01014 0:5e35c180ed4a 232 0, // iInterface
Racer01014 0:5e35c180ed4a 233
Racer01014 0:5e35c180ed4a 234 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Racer01014 0:5e35c180ed4a 235 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Racer01014 0:5e35c180ed4a 236 ENDPOINT_DESCRIPTOR, // bDescriptorType
Racer01014 0:5e35c180ed4a 237 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
Racer01014 0:5e35c180ed4a 238 E_BULK, // bmAttributes (0x02=bulk)
Racer01014 0:5e35c180ed4a 239 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Racer01014 0:5e35c180ed4a 240 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Racer01014 0:5e35c180ed4a 241 0, // bInterval
Racer01014 0:5e35c180ed4a 242
Racer01014 0:5e35c180ed4a 243 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Racer01014 0:5e35c180ed4a 244 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Racer01014 0:5e35c180ed4a 245 ENDPOINT_DESCRIPTOR, // bDescriptorType
Racer01014 0:5e35c180ed4a 246 PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress
Racer01014 0:5e35c180ed4a 247 E_BULK, // bmAttributes (0x02=bulk)
Racer01014 0:5e35c180ed4a 248 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Racer01014 0:5e35c180ed4a 249 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Racer01014 0:5e35c180ed4a 250 0 // bInterval
Racer01014 0:5e35c180ed4a 251 };
Racer01014 0:5e35c180ed4a 252 return configDescriptor;
Racer01014 0:5e35c180ed4a 253 }