nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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