mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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