Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
USBDevice/USBSERIAL/.svn/text-base/USBCDC.cpp.svn-base@0:505207de8566, 2011-11-29 (annotated)
- Committer:
- znuh
- Date:
- Tue Nov 29 21:26:20 2011 +0000
- Revision:
- 0:505207de8566
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| znuh | 0:505207de8566 | 1 | /* USBCDC.c */ |
| znuh | 0:505207de8566 | 2 | /* CDC class */ |
| znuh | 0:505207de8566 | 3 | /* Copyright (c) 2011 ARM Limited. All rights reserved. */ |
| znuh | 0:505207de8566 | 4 | |
| znuh | 0:505207de8566 | 5 | #include "stdint.h" |
| znuh | 0:505207de8566 | 6 | #include "USBCDC.h" |
| znuh | 0:505207de8566 | 7 | #include "USBBusInterface.h" |
| znuh | 0:505207de8566 | 8 | |
| znuh | 0:505207de8566 | 9 | static uint8_t cdc_line_coding[7]={0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08}; |
| znuh | 0:505207de8566 | 10 | |
| znuh | 0:505207de8566 | 11 | USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) { |
| znuh | 0:505207de8566 | 12 | } |
| znuh | 0:505207de8566 | 13 | |
| znuh | 0:505207de8566 | 14 | bool USBCDC::USBCallback_request(void) { |
| znuh | 0:505207de8566 | 15 | /* Called in ISR context */ |
| znuh | 0:505207de8566 | 16 | |
| znuh | 0:505207de8566 | 17 | bool success = false; |
| znuh | 0:505207de8566 | 18 | CONTROL_TRANSFER * transfer = getTransferPtr(); |
| znuh | 0:505207de8566 | 19 | |
| znuh | 0:505207de8566 | 20 | /* Process class-specific requests */ |
| znuh | 0:505207de8566 | 21 | |
| znuh | 0:505207de8566 | 22 | if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { |
| znuh | 0:505207de8566 | 23 | switch (transfer->setup.bRequest) { |
| znuh | 0:505207de8566 | 24 | case CDC_GET_LINE_CODING: |
| znuh | 0:505207de8566 | 25 | transfer->remaining = 7; |
| znuh | 0:505207de8566 | 26 | transfer->ptr = cdc_line_coding; |
| znuh | 0:505207de8566 | 27 | transfer->direction = DEVICE_TO_HOST; |
| znuh | 0:505207de8566 | 28 | success = true; |
| znuh | 0:505207de8566 | 29 | break; |
| znuh | 0:505207de8566 | 30 | case CDC_SET_LINE_CODING: |
| znuh | 0:505207de8566 | 31 | transfer->remaining = 7; |
| znuh | 0:505207de8566 | 32 | success = true; |
| znuh | 0:505207de8566 | 33 | break; |
| znuh | 0:505207de8566 | 34 | default: |
| znuh | 0:505207de8566 | 35 | break; |
| znuh | 0:505207de8566 | 36 | } |
| znuh | 0:505207de8566 | 37 | } |
| znuh | 0:505207de8566 | 38 | |
| znuh | 0:505207de8566 | 39 | return success; |
| znuh | 0:505207de8566 | 40 | } |
| znuh | 0:505207de8566 | 41 | |
| znuh | 0:505207de8566 | 42 | /* |
| znuh | 0:505207de8566 | 43 | * Route callbacks from lower layers to class(es) |
| znuh | 0:505207de8566 | 44 | */ |
| znuh | 0:505207de8566 | 45 | |
| znuh | 0:505207de8566 | 46 | |
| znuh | 0:505207de8566 | 47 | bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) { |
| znuh | 0:505207de8566 | 48 | // Called in ISR context |
| znuh | 0:505207de8566 | 49 | // Set configuration. Return false if the |
| znuh | 0:505207de8566 | 50 | // configuration is not supported. |
| znuh | 0:505207de8566 | 51 | if (configuration != DEFAULT_CONFIGURATION) { |
| znuh | 0:505207de8566 | 52 | return false; |
| znuh | 0:505207de8566 | 53 | } |
| znuh | 0:505207de8566 | 54 | |
| znuh | 0:505207de8566 | 55 | // Configure endpoints > 0 |
| znuh | 0:505207de8566 | 56 | addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT); |
| znuh | 0:505207de8566 | 57 | addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK); |
| znuh | 0:505207de8566 | 58 | addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
| znuh | 0:505207de8566 | 59 | |
| znuh | 0:505207de8566 | 60 | // We activate the endpoint to be able to recceive data |
| znuh | 0:505207de8566 | 61 | readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
| znuh | 0:505207de8566 | 62 | return true; |
| znuh | 0:505207de8566 | 63 | } |
| znuh | 0:505207de8566 | 64 | |
| znuh | 0:505207de8566 | 65 | bool USBCDC::send(uint8_t endpoint, uint8_t * buffer, uint16_t size) |
| znuh | 0:505207de8566 | 66 | { |
| znuh | 0:505207de8566 | 67 | return write(endpoint, buffer, size, MAX_CDC_REPORT_SIZE); |
| znuh | 0:505207de8566 | 68 | } |
| znuh | 0:505207de8566 | 69 | |
| znuh | 0:505207de8566 | 70 | bool USBCDC::read(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize) |
| znuh | 0:505207de8566 | 71 | { |
| znuh | 0:505207de8566 | 72 | if(!USBDevice::read(endpoint, buffer, size, maxSize)) |
| znuh | 0:505207de8566 | 73 | return false; |
| znuh | 0:505207de8566 | 74 | if(!readStart(endpoint, maxSize)) |
| znuh | 0:505207de8566 | 75 | return false; |
| znuh | 0:505207de8566 | 76 | return true; |
| znuh | 0:505207de8566 | 77 | } |
| znuh | 0:505207de8566 | 78 | |
| znuh | 0:505207de8566 | 79 | bool USBCDC::readNB(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize) |
| znuh | 0:505207de8566 | 80 | { |
| znuh | 0:505207de8566 | 81 | if(!USBDevice::readNB(endpoint, buffer, size, MAX_CDC_REPORT_SIZE)) |
| znuh | 0:505207de8566 | 82 | return false; |
| znuh | 0:505207de8566 | 83 | if(!readStart(endpoint, MAX_CDC_REPORT_SIZE)) |
| znuh | 0:505207de8566 | 84 | return false; |
| znuh | 0:505207de8566 | 85 | return true; |
| znuh | 0:505207de8566 | 86 | } |
| znuh | 0:505207de8566 | 87 | |
| znuh | 0:505207de8566 | 88 | |
| znuh | 0:505207de8566 | 89 | uint8_t * USBCDC::deviceDesc() { |
| znuh | 0:505207de8566 | 90 | static uint8_t deviceDescriptor[] = { |
| znuh | 0:505207de8566 | 91 | 18, // bLength |
| znuh | 0:505207de8566 | 92 | 1, // bDescriptorType |
| znuh | 0:505207de8566 | 93 | 0x00, 0x02, // bcdUSB |
| znuh | 0:505207de8566 | 94 | 2, // bDeviceClass |
| znuh | 0:505207de8566 | 95 | 0, // bDeviceSubClass |
| znuh | 0:505207de8566 | 96 | 0, // bDeviceProtocol |
| znuh | 0:505207de8566 | 97 | MAX_PACKET_SIZE_EP0, // bMaxPacketSize0 |
| znuh | 0:505207de8566 | 98 | LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor |
| znuh | 0:505207de8566 | 99 | LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct |
| znuh | 0:505207de8566 | 100 | 0x00, 0x01, // bcdDevice |
| znuh | 0:505207de8566 | 101 | 1, // iManufacturer |
| znuh | 0:505207de8566 | 102 | 2, // iProduct |
| znuh | 0:505207de8566 | 103 | 3, // iSerialNumber |
| znuh | 0:505207de8566 | 104 | 1 // bNumConfigurations |
| znuh | 0:505207de8566 | 105 | }; |
| znuh | 0:505207de8566 | 106 | return deviceDescriptor; |
| znuh | 0:505207de8566 | 107 | } |
| znuh | 0:505207de8566 | 108 | |
| znuh | 0:505207de8566 | 109 | uint8_t * USBCDC::stringIinterfaceDesc() { |
| znuh | 0:505207de8566 | 110 | static uint8_t stringIinterfaceDescriptor[] = { |
| znuh | 0:505207de8566 | 111 | 0x08, /*bLength*/ |
| znuh | 0:505207de8566 | 112 | STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ |
| znuh | 0:505207de8566 | 113 | 'C',0,'D',0,'C',0, /*bString iInterface - HID*/ |
| znuh | 0:505207de8566 | 114 | }; |
| znuh | 0:505207de8566 | 115 | return stringIinterfaceDescriptor; |
| znuh | 0:505207de8566 | 116 | } |
| znuh | 0:505207de8566 | 117 | |
| znuh | 0:505207de8566 | 118 | uint8_t * USBCDC::stringIproductDesc() { |
| znuh | 0:505207de8566 | 119 | static uint8_t stringIproductDescriptor[] = { |
| znuh | 0:505207de8566 | 120 | 0x16, /*bLength*/ |
| znuh | 0:505207de8566 | 121 | STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ |
| znuh | 0:505207de8566 | 122 | 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 /*bString iProduct - HID device*/ |
| znuh | 0:505207de8566 | 123 | }; |
| znuh | 0:505207de8566 | 124 | return stringIproductDescriptor; |
| znuh | 0:505207de8566 | 125 | } |
| znuh | 0:505207de8566 | 126 | |
| znuh | 0:505207de8566 | 127 | |
| znuh | 0:505207de8566 | 128 | #define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7) |
| znuh | 0:505207de8566 | 129 | |
| znuh | 0:505207de8566 | 130 | uint8_t * USBCDC::configurationDesc() { |
| znuh | 0:505207de8566 | 131 | static uint8_t configDescriptor[] = { |
| znuh | 0:505207de8566 | 132 | // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10 |
| znuh | 0:505207de8566 | 133 | 9, // bLength; |
| znuh | 0:505207de8566 | 134 | 2, // bDescriptorType; |
| znuh | 0:505207de8566 | 135 | LSB(CONFIG1_DESC_SIZE), // wTotalLength |
| znuh | 0:505207de8566 | 136 | MSB(CONFIG1_DESC_SIZE), |
| znuh | 0:505207de8566 | 137 | 2, // bNumInterfaces |
| znuh | 0:505207de8566 | 138 | 1, // bConfigurationValue |
| znuh | 0:505207de8566 | 139 | 0, // iConfiguration |
| znuh | 0:505207de8566 | 140 | 0xC0, // bmAttributes |
| znuh | 0:505207de8566 | 141 | 50, // bMaxPower |
| znuh | 0:505207de8566 | 142 | |
| znuh | 0:505207de8566 | 143 | // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 |
| znuh | 0:505207de8566 | 144 | 9, // bLength |
| znuh | 0:505207de8566 | 145 | 4, // bDescriptorType |
| znuh | 0:505207de8566 | 146 | 0, // bInterfaceNumber |
| znuh | 0:505207de8566 | 147 | 0, // bAlternateSetting |
| znuh | 0:505207de8566 | 148 | 1, // bNumEndpoints |
| znuh | 0:505207de8566 | 149 | 0x02, // bInterfaceClass |
| znuh | 0:505207de8566 | 150 | 0x02, // bInterfaceSubClass |
| znuh | 0:505207de8566 | 151 | 0x01, // bInterfaceProtocol |
| znuh | 0:505207de8566 | 152 | 0, // iInterface |
| znuh | 0:505207de8566 | 153 | |
| znuh | 0:505207de8566 | 154 | // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26 |
| znuh | 0:505207de8566 | 155 | 5, // bFunctionLength |
| znuh | 0:505207de8566 | 156 | 0x24, // bDescriptorType |
| znuh | 0:505207de8566 | 157 | 0x00, // bDescriptorSubtype |
| znuh | 0:505207de8566 | 158 | 0x10, 0x01, // bcdCDC |
| znuh | 0:505207de8566 | 159 | |
| znuh | 0:505207de8566 | 160 | // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27 |
| znuh | 0:505207de8566 | 161 | 5, // bFunctionLength |
| znuh | 0:505207de8566 | 162 | 0x24, // bDescriptorType |
| znuh | 0:505207de8566 | 163 | 0x01, // bDescriptorSubtype |
| znuh | 0:505207de8566 | 164 | 0x01, // bmCapabilities |
| znuh | 0:505207de8566 | 165 | 1, // bDataInterface |
| znuh | 0:505207de8566 | 166 | |
| znuh | 0:505207de8566 | 167 | // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28 |
| znuh | 0:505207de8566 | 168 | 4, // bFunctionLength |
| znuh | 0:505207de8566 | 169 | 0x24, // bDescriptorType |
| znuh | 0:505207de8566 | 170 | 0x02, // bDescriptorSubtype |
| znuh | 0:505207de8566 | 171 | 0x06, // bmCapabilities |
| znuh | 0:505207de8566 | 172 | |
| znuh | 0:505207de8566 | 173 | // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33 |
| znuh | 0:505207de8566 | 174 | 5, // bFunctionLength |
| znuh | 0:505207de8566 | 175 | 0x24, // bDescriptorType |
| znuh | 0:505207de8566 | 176 | 0x06, // bDescriptorSubtype |
| znuh | 0:505207de8566 | 177 | 0, // bMasterInterface |
| znuh | 0:505207de8566 | 178 | 1, // bSlaveInterface0 |
| znuh | 0:505207de8566 | 179 | |
| znuh | 0:505207de8566 | 180 | // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 |
| znuh | 0:505207de8566 | 181 | ENDPOINT_DESCRIPTOR_LENGTH, // bLength |
| znuh | 0:505207de8566 | 182 | ENDPOINT_DESCRIPTOR, // bDescriptorType |
| znuh | 0:505207de8566 | 183 | PHY_TO_DESC(EPINT_IN), // bEndpointAddress |
| znuh | 0:505207de8566 | 184 | E_INTERRUPT, // bmAttributes (0x03=intr) |
| znuh | 0:505207de8566 | 185 | LSB(MAX_PACKET_SIZE_EPINT), /* wMaxPacketSize (LSB) */ |
| znuh | 0:505207de8566 | 186 | MSB(MAX_PACKET_SIZE_EPINT), /* wMaxPacketSize (MSB) */ |
| znuh | 0:505207de8566 | 187 | 64, // bInterval |
| znuh | 0:505207de8566 | 188 | |
| znuh | 0:505207de8566 | 189 | |
| znuh | 0:505207de8566 | 190 | |
| znuh | 0:505207de8566 | 191 | |
| znuh | 0:505207de8566 | 192 | // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 |
| znuh | 0:505207de8566 | 193 | 9, // bLength |
| znuh | 0:505207de8566 | 194 | 4, // bDescriptorType |
| znuh | 0:505207de8566 | 195 | 1, // bInterfaceNumber |
| znuh | 0:505207de8566 | 196 | 0, // bAlternateSetting |
| znuh | 0:505207de8566 | 197 | 2, // bNumEndpoints |
| znuh | 0:505207de8566 | 198 | 0x0A, // bInterfaceClass |
| znuh | 0:505207de8566 | 199 | 0x00, // bInterfaceSubClass |
| znuh | 0:505207de8566 | 200 | 0x00, // bInterfaceProtocol |
| znuh | 0:505207de8566 | 201 | 0, // iInterface |
| znuh | 0:505207de8566 | 202 | |
| znuh | 0:505207de8566 | 203 | // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 |
| znuh | 0:505207de8566 | 204 | 7, // bLength |
| znuh | 0:505207de8566 | 205 | 5, // bDescriptorType |
| znuh | 0:505207de8566 | 206 | PHY_TO_DESC(EPBULK_IN), // bEndpointAddress |
| znuh | 0:505207de8566 | 207 | 0x02, // bmAttributes (0x02=bulk) |
| znuh | 0:505207de8566 | 208 | LSB(MAX_PACKET_SIZE_EPBULK), /* wMaxPacketSize (LSB) */ |
| znuh | 0:505207de8566 | 209 | MSB(MAX_PACKET_SIZE_EPBULK), /* wMaxPacketSize (MSB) */ |
| znuh | 0:505207de8566 | 210 | 0, // bInterval |
| znuh | 0:505207de8566 | 211 | |
| znuh | 0:505207de8566 | 212 | // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 |
| znuh | 0:505207de8566 | 213 | 7, // bLength |
| znuh | 0:505207de8566 | 214 | 5, // bDescriptorType |
| znuh | 0:505207de8566 | 215 | PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress |
| znuh | 0:505207de8566 | 216 | 0x02, // bmAttributes (0x02=bulk) |
| znuh | 0:505207de8566 | 217 | LSB(MAX_PACKET_SIZE_EPBULK), /* wMaxPacketSize (LSB) */ |
| znuh | 0:505207de8566 | 218 | MSB(MAX_PACKET_SIZE_EPBULK), /* wMaxPacketSize (MSB) */ |
| znuh | 0:505207de8566 | 219 | 0 // bInterval |
| znuh | 0:505207de8566 | 220 | }; |
| znuh | 0:505207de8566 | 221 | return configDescriptor; |
| znuh | 0:505207de8566 | 222 | } |