USBDevice

Dependents:   QEI_X1_LCD_test3 macnica_test

Committer:
toucyy
Date:
Thu Apr 18 07:49:37 2013 +0000
Revision:
0:2d8d0b73e1ff
[mbed] converted /QEI_HelloWorld/USBDevice

Who changed what in which revision?

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