KL46Z-lab2

Dependencies:   SLCD- mbed TSI MMA8451Q USBDevice MAG3110

Committer:
Lokkus
Date:
Thu Feb 21 07:41:00 2019 +0000
Revision:
0:29277ae50860
KL46Z-lab2

Who changed what in which revision?

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