Pinscape Controller version 1 fork. This is a fork to allow for ongoing bug fixes to the original controller version, from before the major changes for the expansion board project.

Dependencies:   FastIO FastPWM SimpleDMA mbed

Fork of Pinscape_Controller by Mike R

Committer:
mjr
Date:
Mon Feb 15 23:03:55 2016 +0000
Revision:
68:edfecf67a931
Parent:
52:63f0a9b45f0c
Finalize USB library updates to fix compatibility problems introduced in USBHAL_KL25Z overhaul.

Who changed what in which revision?

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