,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Zaitsev 10:41552d038a69 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Zaitsev 10:41552d038a69 2 *
Zaitsev 10:41552d038a69 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Zaitsev 10:41552d038a69 4 * and associated documentation files (the "Software"), to deal in the Software without
Zaitsev 10:41552d038a69 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Zaitsev 10:41552d038a69 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Zaitsev 10:41552d038a69 7 * Software is furnished to do so, subject to the following conditions:
Zaitsev 10:41552d038a69 8 *
Zaitsev 10:41552d038a69 9 * The above copyright notice and this permission notice shall be included in all copies or
Zaitsev 10:41552d038a69 10 * substantial portions of the Software.
Zaitsev 10:41552d038a69 11 *
Zaitsev 10:41552d038a69 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Zaitsev 10:41552d038a69 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Zaitsev 10:41552d038a69 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Zaitsev 10:41552d038a69 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Zaitsev 10:41552d038a69 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Zaitsev 10:41552d038a69 17 */
Zaitsev 10:41552d038a69 18
Zaitsev 10:41552d038a69 19 #include "stdint.h"
Zaitsev 10:41552d038a69 20 #include "USBCDC.h"
Zaitsev 10:41552d038a69 21
Zaitsev 10:41552d038a69 22 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
Zaitsev 10:41552d038a69 23
Zaitsev 10:41552d038a69 24 #define DEFAULT_CONFIGURATION (1)
Zaitsev 10:41552d038a69 25
Zaitsev 10:41552d038a69 26 #define CDC_SET_LINE_CODING 0x20
Zaitsev 10:41552d038a69 27 #define CDC_GET_LINE_CODING 0x21
Zaitsev 10:41552d038a69 28 #define CDC_SET_CONTROL_LINE_STATE 0x22
Zaitsev 10:41552d038a69 29
Zaitsev 10:41552d038a69 30 // Control Line State bits
Zaitsev 10:41552d038a69 31 #define CLS_DTR (1 << 0)
Zaitsev 10:41552d038a69 32 #define CLS_RTS (1 << 1)
Zaitsev 10:41552d038a69 33
Zaitsev 10:41552d038a69 34 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
Zaitsev 10:41552d038a69 35
Zaitsev 10:41552d038a69 36 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect_blocking): USBDevice(vendor_id, product_id, product_release) {
Zaitsev 10:41552d038a69 37 terminal_connected = false;
Zaitsev 10:41552d038a69 38 USBDevice::connect(connect_blocking);
Zaitsev 10:41552d038a69 39 }
Zaitsev 10:41552d038a69 40
Zaitsev 10:41552d038a69 41 bool USBCDC::USBCallback_request(void) {
Zaitsev 10:41552d038a69 42 /* Called in ISR context */
Zaitsev 10:41552d038a69 43
Zaitsev 10:41552d038a69 44 bool success = false;
Zaitsev 10:41552d038a69 45 CONTROL_TRANSFER * transfer = getTransferPtr();
Zaitsev 10:41552d038a69 46
Zaitsev 10:41552d038a69 47 /* Process class-specific requests */
Zaitsev 10:41552d038a69 48
Zaitsev 10:41552d038a69 49 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
Zaitsev 10:41552d038a69 50 switch (transfer->setup.bRequest) {
Zaitsev 10:41552d038a69 51 case CDC_GET_LINE_CODING:
Zaitsev 10:41552d038a69 52 transfer->remaining = 7;
Zaitsev 10:41552d038a69 53 transfer->ptr = cdc_line_coding;
Zaitsev 10:41552d038a69 54 transfer->direction = DEVICE_TO_HOST;
Zaitsev 10:41552d038a69 55 success = true;
Zaitsev 10:41552d038a69 56 break;
Zaitsev 10:41552d038a69 57 case CDC_SET_LINE_CODING:
Zaitsev 10:41552d038a69 58 transfer->remaining = 7;
Zaitsev 10:41552d038a69 59 transfer->notify = true;
Zaitsev 10:41552d038a69 60 success = true;
Zaitsev 10:41552d038a69 61 break;
Zaitsev 10:41552d038a69 62 case CDC_SET_CONTROL_LINE_STATE:
Zaitsev 10:41552d038a69 63 if (transfer->setup.wValue & CLS_DTR) {
Zaitsev 10:41552d038a69 64 terminal_connected = true;
Zaitsev 10:41552d038a69 65 } else {
Zaitsev 10:41552d038a69 66 terminal_connected = false;
Zaitsev 10:41552d038a69 67 }
Zaitsev 10:41552d038a69 68 success = true;
Zaitsev 10:41552d038a69 69 break;
Zaitsev 10:41552d038a69 70 default:
Zaitsev 10:41552d038a69 71 break;
Zaitsev 10:41552d038a69 72 }
Zaitsev 10:41552d038a69 73 }
Zaitsev 10:41552d038a69 74
Zaitsev 10:41552d038a69 75 return success;
Zaitsev 10:41552d038a69 76 }
Zaitsev 10:41552d038a69 77
Zaitsev 10:41552d038a69 78 void USBCDC::USBCallback_requestCompleted(uint8_t *buf, uint32_t length) {
Zaitsev 10:41552d038a69 79 // Request of setting line coding has 7 bytes
Zaitsev 10:41552d038a69 80 if (length != 7) {
Zaitsev 10:41552d038a69 81 return;
Zaitsev 10:41552d038a69 82 }
Zaitsev 10:41552d038a69 83
Zaitsev 10:41552d038a69 84 CONTROL_TRANSFER * transfer = getTransferPtr();
Zaitsev 10:41552d038a69 85
Zaitsev 10:41552d038a69 86 /* Process class-specific requests */
Zaitsev 10:41552d038a69 87 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
Zaitsev 10:41552d038a69 88 if (transfer->setup.bRequest == CDC_SET_LINE_CODING) {
Zaitsev 10:41552d038a69 89 if (memcmp(cdc_line_coding, buf, 7)) {
Zaitsev 10:41552d038a69 90 memcpy(cdc_line_coding, buf, 7);
Zaitsev 10:41552d038a69 91
Zaitsev 10:41552d038a69 92 int baud = buf[0] + (buf[1] << 8)
Zaitsev 10:41552d038a69 93 + (buf[2] << 16) + (buf[3] << 24);
Zaitsev 10:41552d038a69 94 int stop = buf[4];
Zaitsev 10:41552d038a69 95 int bits = buf[6];
Zaitsev 10:41552d038a69 96 int parity = buf[5];
Zaitsev 10:41552d038a69 97
Zaitsev 10:41552d038a69 98 lineCodingChanged(baud, bits, parity, stop);
Zaitsev 10:41552d038a69 99 }
Zaitsev 10:41552d038a69 100 }
Zaitsev 10:41552d038a69 101 }
Zaitsev 10:41552d038a69 102 }
Zaitsev 10:41552d038a69 103
Zaitsev 10:41552d038a69 104 // Called in ISR context
Zaitsev 10:41552d038a69 105 // Set configuration. Return false if the
Zaitsev 10:41552d038a69 106 // configuration is not supported.
Zaitsev 10:41552d038a69 107 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
Zaitsev 10:41552d038a69 108 if (configuration != DEFAULT_CONFIGURATION) {
Zaitsev 10:41552d038a69 109 return false;
Zaitsev 10:41552d038a69 110 }
Zaitsev 10:41552d038a69 111
Zaitsev 10:41552d038a69 112 // Configure endpoints > 0
Zaitsev 10:41552d038a69 113 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
Zaitsev 10:41552d038a69 114 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
Zaitsev 10:41552d038a69 115 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Zaitsev 10:41552d038a69 116
Zaitsev 10:41552d038a69 117 // We activate the endpoint to be able to recceive data
Zaitsev 10:41552d038a69 118 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Zaitsev 10:41552d038a69 119 return true;
Zaitsev 10:41552d038a69 120 }
Zaitsev 10:41552d038a69 121
Zaitsev 10:41552d038a69 122 bool USBCDC::send(uint8_t * buffer, uint32_t size) {
Zaitsev 10:41552d038a69 123 return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
Zaitsev 10:41552d038a69 124 }
Zaitsev 10:41552d038a69 125
Zaitsev 10:41552d038a69 126 bool USBCDC::readEP(uint8_t * buffer, uint32_t * size) {
Zaitsev 10:41552d038a69 127 if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
Zaitsev 10:41552d038a69 128 return false;
Zaitsev 10:41552d038a69 129 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
Zaitsev 10:41552d038a69 130 return false;
Zaitsev 10:41552d038a69 131 return true;
Zaitsev 10:41552d038a69 132 }
Zaitsev 10:41552d038a69 133
Zaitsev 10:41552d038a69 134 bool USBCDC::readEP_NB(uint8_t * buffer, uint32_t * size) {
Zaitsev 10:41552d038a69 135 if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
Zaitsev 10:41552d038a69 136 return false;
Zaitsev 10:41552d038a69 137 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
Zaitsev 10:41552d038a69 138 return false;
Zaitsev 10:41552d038a69 139 return true;
Zaitsev 10:41552d038a69 140 }
Zaitsev 10:41552d038a69 141
Zaitsev 10:41552d038a69 142
Zaitsev 10:41552d038a69 143 uint8_t * USBCDC::deviceDesc() {
Zaitsev 10:41552d038a69 144 static uint8_t deviceDescriptor[] = {
Zaitsev 10:41552d038a69 145 18, // bLength
Zaitsev 10:41552d038a69 146 1, // bDescriptorType
Zaitsev 10:41552d038a69 147 0x10, 0x01, // bcdUSB
Zaitsev 10:41552d038a69 148 2, // bDeviceClass
Zaitsev 10:41552d038a69 149 0, // bDeviceSubClass
Zaitsev 10:41552d038a69 150 0, // bDeviceProtocol
Zaitsev 10:41552d038a69 151 MAX_PACKET_SIZE_EP0, // bMaxPacketSize0
Zaitsev 10:41552d038a69 152 (uint8_t)(LSB(VENDOR_ID)), (uint8_t)(MSB(VENDOR_ID)), // idVendor
Zaitsev 10:41552d038a69 153 (uint8_t)(LSB(PRODUCT_ID)), (uint8_t)(MSB(PRODUCT_ID)),// idProduct
Zaitsev 10:41552d038a69 154 0x00, 0x01, // bcdDevice
Zaitsev 10:41552d038a69 155 1, // iManufacturer
Zaitsev 10:41552d038a69 156 2, // iProduct
Zaitsev 10:41552d038a69 157 3, // iSerialNumber
Zaitsev 10:41552d038a69 158 1 // bNumConfigurations
Zaitsev 10:41552d038a69 159 };
Zaitsev 10:41552d038a69 160 return deviceDescriptor;
Zaitsev 10:41552d038a69 161 }
Zaitsev 10:41552d038a69 162
Zaitsev 10:41552d038a69 163 uint8_t * USBCDC::stringIinterfaceDesc() {
Zaitsev 10:41552d038a69 164 static uint8_t stringIinterfaceDescriptor[] = {
Zaitsev 10:41552d038a69 165 0x08,
Zaitsev 10:41552d038a69 166 STRING_DESCRIPTOR,
Zaitsev 10:41552d038a69 167 'C',0,'D',0,'C',0,
Zaitsev 10:41552d038a69 168 };
Zaitsev 10:41552d038a69 169 return stringIinterfaceDescriptor;
Zaitsev 10:41552d038a69 170 }
Zaitsev 10:41552d038a69 171
Zaitsev 10:41552d038a69 172 uint8_t * USBCDC::stringIproductDesc() {
Zaitsev 10:41552d038a69 173 static uint8_t stringIproductDescriptor[] = {
Zaitsev 10:41552d038a69 174 0x16,
Zaitsev 10:41552d038a69 175 STRING_DESCRIPTOR,
Zaitsev 10:41552d038a69 176 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
Zaitsev 10:41552d038a69 177 };
Zaitsev 10:41552d038a69 178 return stringIproductDescriptor;
Zaitsev 10:41552d038a69 179 }
Zaitsev 10:41552d038a69 180
Zaitsev 10:41552d038a69 181
Zaitsev 10:41552d038a69 182 #define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7)
Zaitsev 10:41552d038a69 183
Zaitsev 10:41552d038a69 184 uint8_t * USBCDC::configurationDesc() {
Zaitsev 10:41552d038a69 185 static uint8_t configDescriptor[] = {
Zaitsev 10:41552d038a69 186 // configuration descriptor
Zaitsev 10:41552d038a69 187 9, // bLength
Zaitsev 10:41552d038a69 188 2, // bDescriptorType
Zaitsev 10:41552d038a69 189 LSB(CONFIG1_DESC_SIZE), // wTotalLength
Zaitsev 10:41552d038a69 190 MSB(CONFIG1_DESC_SIZE),
Zaitsev 10:41552d038a69 191 2, // bNumInterfaces
Zaitsev 10:41552d038a69 192 1, // bConfigurationValue
Zaitsev 10:41552d038a69 193 0, // iConfiguration
Zaitsev 10:41552d038a69 194 0x80, // bmAttributes
Zaitsev 10:41552d038a69 195 50, // bMaxPower
Zaitsev 10:41552d038a69 196
Zaitsev 10:41552d038a69 197 // IAD to associate the two CDC interfaces
Zaitsev 10:41552d038a69 198 0x08, // bLength
Zaitsev 10:41552d038a69 199 0x0b, // bDescriptorType
Zaitsev 10:41552d038a69 200 0x00, // bFirstInterface
Zaitsev 10:41552d038a69 201 0x02, // bInterfaceCount
Zaitsev 10:41552d038a69 202 0x02, // bFunctionClass
Zaitsev 10:41552d038a69 203 0x02, // bFunctionSubClass
Zaitsev 10:41552d038a69 204 0, // bFunctionProtocol
Zaitsev 10:41552d038a69 205 0, // iFunction
Zaitsev 10:41552d038a69 206
Zaitsev 10:41552d038a69 207 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
Zaitsev 10:41552d038a69 208 9, // bLength
Zaitsev 10:41552d038a69 209 4, // bDescriptorType
Zaitsev 10:41552d038a69 210 0, // bInterfaceNumber
Zaitsev 10:41552d038a69 211 0, // bAlternateSetting
Zaitsev 10:41552d038a69 212 1, // bNumEndpoints
Zaitsev 10:41552d038a69 213 0x02, // bInterfaceClass
Zaitsev 10:41552d038a69 214 0x02, // bInterfaceSubClass
Zaitsev 10:41552d038a69 215 0x01, // bInterfaceProtocol
Zaitsev 10:41552d038a69 216 0, // iInterface
Zaitsev 10:41552d038a69 217
Zaitsev 10:41552d038a69 218 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
Zaitsev 10:41552d038a69 219 5, // bFunctionLength
Zaitsev 10:41552d038a69 220 0x24, // bDescriptorType
Zaitsev 10:41552d038a69 221 0x00, // bDescriptorSubtype
Zaitsev 10:41552d038a69 222 0x10, 0x01, // bcdCDC
Zaitsev 10:41552d038a69 223
Zaitsev 10:41552d038a69 224 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
Zaitsev 10:41552d038a69 225 5, // bFunctionLength
Zaitsev 10:41552d038a69 226 0x24, // bDescriptorType
Zaitsev 10:41552d038a69 227 0x01, // bDescriptorSubtype
Zaitsev 10:41552d038a69 228 0x03, // bmCapabilities
Zaitsev 10:41552d038a69 229 1, // bDataInterface
Zaitsev 10:41552d038a69 230
Zaitsev 10:41552d038a69 231 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
Zaitsev 10:41552d038a69 232 4, // bFunctionLength
Zaitsev 10:41552d038a69 233 0x24, // bDescriptorType
Zaitsev 10:41552d038a69 234 0x02, // bDescriptorSubtype
Zaitsev 10:41552d038a69 235 0x06, // bmCapabilities
Zaitsev 10:41552d038a69 236
Zaitsev 10:41552d038a69 237 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
Zaitsev 10:41552d038a69 238 5, // bFunctionLength
Zaitsev 10:41552d038a69 239 0x24, // bDescriptorType
Zaitsev 10:41552d038a69 240 0x06, // bDescriptorSubtype
Zaitsev 10:41552d038a69 241 0, // bMasterInterface
Zaitsev 10:41552d038a69 242 1, // bSlaveInterface0
Zaitsev 10:41552d038a69 243
Zaitsev 10:41552d038a69 244 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Zaitsev 10:41552d038a69 245 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Zaitsev 10:41552d038a69 246 ENDPOINT_DESCRIPTOR, // bDescriptorType
Zaitsev 10:41552d038a69 247 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
Zaitsev 10:41552d038a69 248 E_INTERRUPT, // bmAttributes (0x03=intr)
Zaitsev 10:41552d038a69 249 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Zaitsev 10:41552d038a69 250 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Zaitsev 10:41552d038a69 251 16, // bInterval
Zaitsev 10:41552d038a69 252
Zaitsev 10:41552d038a69 253
Zaitsev 10:41552d038a69 254
Zaitsev 10:41552d038a69 255
Zaitsev 10:41552d038a69 256 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
Zaitsev 10:41552d038a69 257 9, // bLength
Zaitsev 10:41552d038a69 258 4, // bDescriptorType
Zaitsev 10:41552d038a69 259 1, // bInterfaceNumber
Zaitsev 10:41552d038a69 260 0, // bAlternateSetting
Zaitsev 10:41552d038a69 261 2, // bNumEndpoints
Zaitsev 10:41552d038a69 262 0x0A, // bInterfaceClass
Zaitsev 10:41552d038a69 263 0x00, // bInterfaceSubClass
Zaitsev 10:41552d038a69 264 0x00, // bInterfaceProtocol
Zaitsev 10:41552d038a69 265 0, // iInterface
Zaitsev 10:41552d038a69 266
Zaitsev 10:41552d038a69 267 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Zaitsev 10:41552d038a69 268 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Zaitsev 10:41552d038a69 269 ENDPOINT_DESCRIPTOR, // bDescriptorType
Zaitsev 10:41552d038a69 270 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
Zaitsev 10:41552d038a69 271 E_BULK, // bmAttributes (0x02=bulk)
Zaitsev 10:41552d038a69 272 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Zaitsev 10:41552d038a69 273 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Zaitsev 10:41552d038a69 274 0, // bInterval
Zaitsev 10:41552d038a69 275
Zaitsev 10:41552d038a69 276 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Zaitsev 10:41552d038a69 277 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Zaitsev 10:41552d038a69 278 ENDPOINT_DESCRIPTOR, // bDescriptorType
Zaitsev 10:41552d038a69 279 PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress
Zaitsev 10:41552d038a69 280 E_BULK, // bmAttributes (0x02=bulk)
Zaitsev 10:41552d038a69 281 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Zaitsev 10:41552d038a69 282 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Zaitsev 10:41552d038a69 283 0 // bInterval
Zaitsev 10:41552d038a69 284 };
Zaitsev 10:41552d038a69 285 return configDescriptor;
Zaitsev 10:41552d038a69 286 }