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