USB Composite support
Dependents: mbed_cdc_hid_composite
Fork of USBDevice by
USBDevice/USBComposite.cpp@55:7c559fcb1d17, 2015-05-31 (annotated)
- Committer:
- steeven
- Date:
- Sun May 31 15:36:50 2015 +0000
- Revision:
- 55:7c559fcb1d17
Make USBDevice support Composite
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
steeven | 55:7c559fcb1d17 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
steeven | 55:7c559fcb1d17 | 2 | * |
steeven | 55:7c559fcb1d17 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
steeven | 55:7c559fcb1d17 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
steeven | 55:7c559fcb1d17 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
steeven | 55:7c559fcb1d17 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
steeven | 55:7c559fcb1d17 | 7 | * Software is furnished to do so, subject to the following conditions: |
steeven | 55:7c559fcb1d17 | 8 | * |
steeven | 55:7c559fcb1d17 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
steeven | 55:7c559fcb1d17 | 10 | * substantial portions of the Software. |
steeven | 55:7c559fcb1d17 | 11 | * |
steeven | 55:7c559fcb1d17 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
steeven | 55:7c559fcb1d17 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
steeven | 55:7c559fcb1d17 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
steeven | 55:7c559fcb1d17 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
steeven | 55:7c559fcb1d17 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
steeven | 55:7c559fcb1d17 | 17 | */ |
steeven | 55:7c559fcb1d17 | 18 | |
steeven | 55:7c559fcb1d17 | 19 | #include "stdint.h" |
steeven | 55:7c559fcb1d17 | 20 | #include "USBComposite.h" |
steeven | 55:7c559fcb1d17 | 21 | |
steeven | 55:7c559fcb1d17 | 22 | #define DEFAULT_CONFIGURATION (1) |
steeven | 55:7c559fcb1d17 | 23 | |
steeven | 55:7c559fcb1d17 | 24 | USBComposite::USBComposite(bool connect_blocking, uint16_t vendor_id, uint16_t product_id , uint16_t product_release): |
steeven | 55:7c559fcb1d17 | 25 | USBDevice(vendor_id, product_id, product_release) { |
steeven | 55:7c559fcb1d17 | 26 | // USBDevice::connect(connect_blocking); |
steeven | 55:7c559fcb1d17 | 27 | } |
steeven | 55:7c559fcb1d17 | 28 | |
steeven | 55:7c559fcb1d17 | 29 | bool USBComposite::USBCallback_request(void) { |
steeven | 55:7c559fcb1d17 | 30 | /* Called in ISR context */ |
steeven | 55:7c559fcb1d17 | 31 | uint8_t i; |
steeven | 55:7c559fcb1d17 | 32 | |
steeven | 55:7c559fcb1d17 | 33 | for (i = 0; i < sizeof(_intfs)/sizeof(_intfs[0]); ++i) { |
steeven | 55:7c559fcb1d17 | 34 | if (_intfs[i] != NULL && _intfs[i]->USBCallback_request() == true) { |
steeven | 55:7c559fcb1d17 | 35 | return true; |
steeven | 55:7c559fcb1d17 | 36 | } |
steeven | 55:7c559fcb1d17 | 37 | } |
steeven | 55:7c559fcb1d17 | 38 | |
steeven | 55:7c559fcb1d17 | 39 | return false; |
steeven | 55:7c559fcb1d17 | 40 | } |
steeven | 55:7c559fcb1d17 | 41 | |
steeven | 55:7c559fcb1d17 | 42 | void USBComposite::USBCallback_requestCompleted(uint8_t *buf, uint32_t length) { |
steeven | 55:7c559fcb1d17 | 43 | /* Called in ISR context */ |
steeven | 55:7c559fcb1d17 | 44 | uint8_t i; |
steeven | 55:7c559fcb1d17 | 45 | |
steeven | 55:7c559fcb1d17 | 46 | for (i = 0; i < sizeof(_intfs)/sizeof(_intfs[0]); ++i) { |
steeven | 55:7c559fcb1d17 | 47 | if (_intfs[i] != NULL) { |
steeven | 55:7c559fcb1d17 | 48 | _intfs[i]->USBCallback_requestCompleted(buf, length); |
steeven | 55:7c559fcb1d17 | 49 | } |
steeven | 55:7c559fcb1d17 | 50 | } |
steeven | 55:7c559fcb1d17 | 51 | } |
steeven | 55:7c559fcb1d17 | 52 | |
steeven | 55:7c559fcb1d17 | 53 | // Called in ISR context |
steeven | 55:7c559fcb1d17 | 54 | // Set configuration. Return false if the |
steeven | 55:7c559fcb1d17 | 55 | // configuration is not supported. |
steeven | 55:7c559fcb1d17 | 56 | bool USBComposite::USBCallback_setConfiguration(uint8_t configuration) { |
steeven | 55:7c559fcb1d17 | 57 | if (configuration != DEFAULT_CONFIGURATION) { |
steeven | 55:7c559fcb1d17 | 58 | return false; |
steeven | 55:7c559fcb1d17 | 59 | } |
steeven | 55:7c559fcb1d17 | 60 | |
steeven | 55:7c559fcb1d17 | 61 | // // Configure endpoints > 0 |
steeven | 55:7c559fcb1d17 | 62 | // _usb->addEndpoint(_ep_int_in, MAX_PACKET_SIZE_EPINT); |
steeven | 55:7c559fcb1d17 | 63 | // _usb->addEndpoint(_ep_bulk_in, MAX_PACKET_SIZE_EPBULK); |
steeven | 55:7c559fcb1d17 | 64 | // _usb->addEndpoint(_ep_bulk_out, MAX_PACKET_SIZE_EPBULK); |
steeven | 55:7c559fcb1d17 | 65 | // |
steeven | 55:7c559fcb1d17 | 66 | // // We activate the endpoint to be able to recceive data |
steeven | 55:7c559fcb1d17 | 67 | // _usb->readStart(_ep_bulk_out, MAX_PACKET_SIZE_EPBULK); |
steeven | 55:7c559fcb1d17 | 68 | return true; |
steeven | 55:7c559fcb1d17 | 69 | } |
steeven | 55:7c559fcb1d17 | 70 | |
steeven | 55:7c559fcb1d17 | 71 | uint8_t * USBComposite::deviceDesc() { |
steeven | 55:7c559fcb1d17 | 72 | static uint8_t deviceDescriptor[] = { |
steeven | 55:7c559fcb1d17 | 73 | 18, // bLength |
steeven | 55:7c559fcb1d17 | 74 | 1, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 75 | 0x10, 0x01, // bcdUSB |
steeven | 55:7c559fcb1d17 | 76 | 2, // bDeviceClass |
steeven | 55:7c559fcb1d17 | 77 | 0, // bDeviceSubClass |
steeven | 55:7c559fcb1d17 | 78 | 0, // bDeviceProtocol |
steeven | 55:7c559fcb1d17 | 79 | MAX_PACKET_SIZE_EP0, // bMaxPacketSize0 |
steeven | 55:7c559fcb1d17 | 80 | (uint8_t)(LSB(VENDOR_ID)), (uint8_t)(MSB(VENDOR_ID)), // idVendor |
steeven | 55:7c559fcb1d17 | 81 | (uint8_t)(LSB(PRODUCT_ID)), (uint8_t)(MSB(PRODUCT_ID)),// idProduct |
steeven | 55:7c559fcb1d17 | 82 | 0x00, 0x01, // bcdDevice |
steeven | 55:7c559fcb1d17 | 83 | 1, // iManufacturer |
steeven | 55:7c559fcb1d17 | 84 | 2, // iProduct |
steeven | 55:7c559fcb1d17 | 85 | 3, // iSerialNumber |
steeven | 55:7c559fcb1d17 | 86 | 1 // bNumConfigurations |
steeven | 55:7c559fcb1d17 | 87 | }; |
steeven | 55:7c559fcb1d17 | 88 | return deviceDescriptor; |
steeven | 55:7c559fcb1d17 | 89 | } |
steeven | 55:7c559fcb1d17 | 90 | |
steeven | 55:7c559fcb1d17 | 91 | uint8_t * USBComposite::stringIinterfaceDesc() { |
steeven | 55:7c559fcb1d17 | 92 | static uint8_t stringIinterfaceDescriptor[] = { |
steeven | 55:7c559fcb1d17 | 93 | 0x08, |
steeven | 55:7c559fcb1d17 | 94 | STRING_DESCRIPTOR, |
steeven | 55:7c559fcb1d17 | 95 | 'C',0,'D',0,'C',0, |
steeven | 55:7c559fcb1d17 | 96 | }; |
steeven | 55:7c559fcb1d17 | 97 | return stringIinterfaceDescriptor; |
steeven | 55:7c559fcb1d17 | 98 | } |
steeven | 55:7c559fcb1d17 | 99 | |
steeven | 55:7c559fcb1d17 | 100 | uint8_t * USBComposite::stringIproductDesc() { |
steeven | 55:7c559fcb1d17 | 101 | static uint8_t stringIproductDescriptor[] = { |
steeven | 55:7c559fcb1d17 | 102 | 0x16, |
steeven | 55:7c559fcb1d17 | 103 | STRING_DESCRIPTOR, |
steeven | 55:7c559fcb1d17 | 104 | 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 |
steeven | 55:7c559fcb1d17 | 105 | }; |
steeven | 55:7c559fcb1d17 | 106 | return stringIproductDescriptor; |
steeven | 55:7c559fcb1d17 | 107 | } |
steeven | 55:7c559fcb1d17 | 108 | |
steeven | 55:7c559fcb1d17 | 109 | |
steeven | 55:7c559fcb1d17 | 110 | #define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7) |
steeven | 55:7c559fcb1d17 | 111 | |
steeven | 55:7c559fcb1d17 | 112 | uint8_t * USBComposite::configurationDesc() { |
steeven | 55:7c559fcb1d17 | 113 | static uint8_t configDescriptor[] = { |
steeven | 55:7c559fcb1d17 | 114 | // configuration descriptor |
steeven | 55:7c559fcb1d17 | 115 | 9, // bLength |
steeven | 55:7c559fcb1d17 | 116 | 2, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 117 | LSB(CONFIG1_DESC_SIZE), // wTotalLength |
steeven | 55:7c559fcb1d17 | 118 | MSB(CONFIG1_DESC_SIZE), |
steeven | 55:7c559fcb1d17 | 119 | 2, // bNumInterfaces |
steeven | 55:7c559fcb1d17 | 120 | 1, // bConfigurationValue |
steeven | 55:7c559fcb1d17 | 121 | 0, // iConfiguration |
steeven | 55:7c559fcb1d17 | 122 | 0x80, // bmAttributes |
steeven | 55:7c559fcb1d17 | 123 | 50, // bMaxPower |
steeven | 55:7c559fcb1d17 | 124 | |
steeven | 55:7c559fcb1d17 | 125 | // IAD to associate the two CDC interfaces |
steeven | 55:7c559fcb1d17 | 126 | 0x08, // bLength |
steeven | 55:7c559fcb1d17 | 127 | 0x0b, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 128 | 0x00, // bFirstInterface |
steeven | 55:7c559fcb1d17 | 129 | 0x02, // bInterfaceCount |
steeven | 55:7c559fcb1d17 | 130 | 0x02, // bFunctionClass |
steeven | 55:7c559fcb1d17 | 131 | 0x02, // bFunctionSubClass |
steeven | 55:7c559fcb1d17 | 132 | 0, // bFunctionProtocol |
steeven | 55:7c559fcb1d17 | 133 | 0, // iFunction |
steeven | 55:7c559fcb1d17 | 134 | |
steeven | 55:7c559fcb1d17 | 135 | // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 |
steeven | 55:7c559fcb1d17 | 136 | 9, // bLength |
steeven | 55:7c559fcb1d17 | 137 | 4, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 138 | 0, // bInterfaceNumber |
steeven | 55:7c559fcb1d17 | 139 | 0, // bAlternateSetting |
steeven | 55:7c559fcb1d17 | 140 | 1, // bNumEndpoints |
steeven | 55:7c559fcb1d17 | 141 | 0x02, // bInterfaceClass |
steeven | 55:7c559fcb1d17 | 142 | 0x02, // bInterfaceSubClass |
steeven | 55:7c559fcb1d17 | 143 | 0x01, // bInterfaceProtocol |
steeven | 55:7c559fcb1d17 | 144 | 0, // iInterface |
steeven | 55:7c559fcb1d17 | 145 | |
steeven | 55:7c559fcb1d17 | 146 | // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26 |
steeven | 55:7c559fcb1d17 | 147 | 5, // bFunctionLength |
steeven | 55:7c559fcb1d17 | 148 | 0x24, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 149 | 0x00, // bDescriptorSubtype |
steeven | 55:7c559fcb1d17 | 150 | 0x10, 0x01, // bcdCDC |
steeven | 55:7c559fcb1d17 | 151 | |
steeven | 55:7c559fcb1d17 | 152 | // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27 |
steeven | 55:7c559fcb1d17 | 153 | 5, // bFunctionLength |
steeven | 55:7c559fcb1d17 | 154 | 0x24, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 155 | 0x01, // bDescriptorSubtype |
steeven | 55:7c559fcb1d17 | 156 | 0x03, // bmCapabilities |
steeven | 55:7c559fcb1d17 | 157 | 1, // bDataInterface |
steeven | 55:7c559fcb1d17 | 158 | |
steeven | 55:7c559fcb1d17 | 159 | // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28 |
steeven | 55:7c559fcb1d17 | 160 | 4, // bFunctionLength |
steeven | 55:7c559fcb1d17 | 161 | 0x24, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 162 | 0x02, // bDescriptorSubtype |
steeven | 55:7c559fcb1d17 | 163 | 0x06, // bmCapabilities |
steeven | 55:7c559fcb1d17 | 164 | |
steeven | 55:7c559fcb1d17 | 165 | // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33 |
steeven | 55:7c559fcb1d17 | 166 | 5, // bFunctionLength |
steeven | 55:7c559fcb1d17 | 167 | 0x24, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 168 | 0x06, // bDescriptorSubtype |
steeven | 55:7c559fcb1d17 | 169 | 0, // bMasterInterface |
steeven | 55:7c559fcb1d17 | 170 | 1, // bSlaveInterface0 |
steeven | 55:7c559fcb1d17 | 171 | |
steeven | 55:7c559fcb1d17 | 172 | // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 |
steeven | 55:7c559fcb1d17 | 173 | ENDPOINT_DESCRIPTOR_LENGTH, // bLength |
steeven | 55:7c559fcb1d17 | 174 | ENDPOINT_DESCRIPTOR, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 175 | // PHY_TO_DESC(_ep_int_in), // bEndpointAddress |
steeven | 55:7c559fcb1d17 | 176 | E_INTERRUPT, // bmAttributes (0x03=intr) |
steeven | 55:7c559fcb1d17 | 177 | LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) |
steeven | 55:7c559fcb1d17 | 178 | MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) |
steeven | 55:7c559fcb1d17 | 179 | 16, // bInterval |
steeven | 55:7c559fcb1d17 | 180 | |
steeven | 55:7c559fcb1d17 | 181 | |
steeven | 55:7c559fcb1d17 | 182 | |
steeven | 55:7c559fcb1d17 | 183 | |
steeven | 55:7c559fcb1d17 | 184 | // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 |
steeven | 55:7c559fcb1d17 | 185 | 9, // bLength |
steeven | 55:7c559fcb1d17 | 186 | 4, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 187 | 1, // bInterfaceNumber |
steeven | 55:7c559fcb1d17 | 188 | 0, // bAlternateSetting |
steeven | 55:7c559fcb1d17 | 189 | 2, // bNumEndpoints |
steeven | 55:7c559fcb1d17 | 190 | 0x0A, // bInterfaceClass |
steeven | 55:7c559fcb1d17 | 191 | 0x00, // bInterfaceSubClass |
steeven | 55:7c559fcb1d17 | 192 | 0x00, // bInterfaceProtocol |
steeven | 55:7c559fcb1d17 | 193 | 0, // iInterface |
steeven | 55:7c559fcb1d17 | 194 | |
steeven | 55:7c559fcb1d17 | 195 | // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 |
steeven | 55:7c559fcb1d17 | 196 | ENDPOINT_DESCRIPTOR_LENGTH, // bLength |
steeven | 55:7c559fcb1d17 | 197 | ENDPOINT_DESCRIPTOR, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 198 | // PHY_TO_DESC(_ep_bulk_in), // bEndpointAddress |
steeven | 55:7c559fcb1d17 | 199 | E_BULK, // bmAttributes (0x02=bulk) |
steeven | 55:7c559fcb1d17 | 200 | LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB) |
steeven | 55:7c559fcb1d17 | 201 | MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB) |
steeven | 55:7c559fcb1d17 | 202 | 0, // bInterval |
steeven | 55:7c559fcb1d17 | 203 | |
steeven | 55:7c559fcb1d17 | 204 | // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 |
steeven | 55:7c559fcb1d17 | 205 | ENDPOINT_DESCRIPTOR_LENGTH, // bLength |
steeven | 55:7c559fcb1d17 | 206 | ENDPOINT_DESCRIPTOR, // bDescriptorType |
steeven | 55:7c559fcb1d17 | 207 | // PHY_TO_DESC(_ep_bulk_out), // bEndpointAddress |
steeven | 55:7c559fcb1d17 | 208 | E_BULK, // bmAttributes (0x02=bulk) |
steeven | 55:7c559fcb1d17 | 209 | LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB) |
steeven | 55:7c559fcb1d17 | 210 | MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB) |
steeven | 55:7c559fcb1d17 | 211 | 0 // bInterval |
steeven | 55:7c559fcb1d17 | 212 | }; |
steeven | 55:7c559fcb1d17 | 213 | return configDescriptor; |
steeven | 55:7c559fcb1d17 | 214 | } |