USB Composite support
Dependents: mbed_cdc_hid_composite
Fork of USBDevice by
USBComposite.cpp
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "stdint.h" 00020 #include "USBComposite.h" 00021 00022 #define DEFAULT_CONFIGURATION (1) 00023 00024 USBComposite::USBComposite(bool connect_blocking, uint16_t vendor_id, uint16_t product_id , uint16_t product_release): 00025 USBDevice(vendor_id, product_id, product_release) { 00026 // USBDevice::connect(connect_blocking); 00027 } 00028 00029 bool USBComposite::USBCallback_request(void) { 00030 /* Called in ISR context */ 00031 uint8_t i; 00032 00033 for (i = 0; i < sizeof(_intfs)/sizeof(_intfs[0]); ++i) { 00034 if (_intfs[i] != NULL && _intfs[i]->USBCallback_request() == true) { 00035 return true; 00036 } 00037 } 00038 00039 return false; 00040 } 00041 00042 void USBComposite::USBCallback_requestCompleted(uint8_t *buf, uint32_t length) { 00043 /* Called in ISR context */ 00044 uint8_t i; 00045 00046 for (i = 0; i < sizeof(_intfs)/sizeof(_intfs[0]); ++i) { 00047 if (_intfs[i] != NULL) { 00048 _intfs[i]->USBCallback_requestCompleted(buf, length); 00049 } 00050 } 00051 } 00052 00053 // Called in ISR context 00054 // Set configuration. Return false if the 00055 // configuration is not supported. 00056 bool USBComposite::USBCallback_setConfiguration(uint8_t configuration) { 00057 if (configuration != DEFAULT_CONFIGURATION) { 00058 return false; 00059 } 00060 00061 // // Configure endpoints > 0 00062 // _usb->addEndpoint(_ep_int_in, MAX_PACKET_SIZE_EPINT); 00063 // _usb->addEndpoint(_ep_bulk_in, MAX_PACKET_SIZE_EPBULK); 00064 // _usb->addEndpoint(_ep_bulk_out, MAX_PACKET_SIZE_EPBULK); 00065 // 00066 // // We activate the endpoint to be able to recceive data 00067 // _usb->readStart(_ep_bulk_out, MAX_PACKET_SIZE_EPBULK); 00068 return true; 00069 } 00070 00071 uint8_t * USBComposite::deviceDesc() { 00072 static uint8_t deviceDescriptor[] = { 00073 18, // bLength 00074 1, // bDescriptorType 00075 0x10, 0x01, // bcdUSB 00076 2, // bDeviceClass 00077 0, // bDeviceSubClass 00078 0, // bDeviceProtocol 00079 MAX_PACKET_SIZE_EP0, // bMaxPacketSize0 00080 (uint8_t)(LSB(VENDOR_ID)), (uint8_t)(MSB(VENDOR_ID)), // idVendor 00081 (uint8_t)(LSB(PRODUCT_ID)), (uint8_t)(MSB(PRODUCT_ID)),// idProduct 00082 0x00, 0x01, // bcdDevice 00083 1, // iManufacturer 00084 2, // iProduct 00085 3, // iSerialNumber 00086 1 // bNumConfigurations 00087 }; 00088 return deviceDescriptor; 00089 } 00090 00091 uint8_t * USBComposite::stringIinterfaceDesc() { 00092 static uint8_t stringIinterfaceDescriptor[] = { 00093 0x08, 00094 STRING_DESCRIPTOR, 00095 'C',0,'D',0,'C',0, 00096 }; 00097 return stringIinterfaceDescriptor; 00098 } 00099 00100 uint8_t * USBComposite::stringIproductDesc() { 00101 static uint8_t stringIproductDescriptor[] = { 00102 0x16, 00103 STRING_DESCRIPTOR, 00104 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 00105 }; 00106 return stringIproductDescriptor; 00107 } 00108 00109 00110 #define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7) 00111 00112 uint8_t * USBComposite::configurationDesc() { 00113 static uint8_t configDescriptor[] = { 00114 // configuration descriptor 00115 9, // bLength 00116 2, // bDescriptorType 00117 LSB(CONFIG1_DESC_SIZE), // wTotalLength 00118 MSB(CONFIG1_DESC_SIZE), 00119 2, // bNumInterfaces 00120 1, // bConfigurationValue 00121 0, // iConfiguration 00122 0x80, // bmAttributes 00123 50, // bMaxPower 00124 00125 // IAD to associate the two CDC interfaces 00126 0x08, // bLength 00127 0x0b, // bDescriptorType 00128 0x00, // bFirstInterface 00129 0x02, // bInterfaceCount 00130 0x02, // bFunctionClass 00131 0x02, // bFunctionSubClass 00132 0, // bFunctionProtocol 00133 0, // iFunction 00134 00135 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 00136 9, // bLength 00137 4, // bDescriptorType 00138 0, // bInterfaceNumber 00139 0, // bAlternateSetting 00140 1, // bNumEndpoints 00141 0x02, // bInterfaceClass 00142 0x02, // bInterfaceSubClass 00143 0x01, // bInterfaceProtocol 00144 0, // iInterface 00145 00146 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26 00147 5, // bFunctionLength 00148 0x24, // bDescriptorType 00149 0x00, // bDescriptorSubtype 00150 0x10, 0x01, // bcdCDC 00151 00152 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27 00153 5, // bFunctionLength 00154 0x24, // bDescriptorType 00155 0x01, // bDescriptorSubtype 00156 0x03, // bmCapabilities 00157 1, // bDataInterface 00158 00159 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28 00160 4, // bFunctionLength 00161 0x24, // bDescriptorType 00162 0x02, // bDescriptorSubtype 00163 0x06, // bmCapabilities 00164 00165 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33 00166 5, // bFunctionLength 00167 0x24, // bDescriptorType 00168 0x06, // bDescriptorSubtype 00169 0, // bMasterInterface 00170 1, // bSlaveInterface0 00171 00172 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 00173 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00174 ENDPOINT_DESCRIPTOR, // bDescriptorType 00175 // PHY_TO_DESC(_ep_int_in), // bEndpointAddress 00176 E_INTERRUPT, // bmAttributes (0x03=intr) 00177 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) 00178 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) 00179 16, // bInterval 00180 00181 00182 00183 00184 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 00185 9, // bLength 00186 4, // bDescriptorType 00187 1, // bInterfaceNumber 00188 0, // bAlternateSetting 00189 2, // bNumEndpoints 00190 0x0A, // bInterfaceClass 00191 0x00, // bInterfaceSubClass 00192 0x00, // bInterfaceProtocol 00193 0, // iInterface 00194 00195 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 00196 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00197 ENDPOINT_DESCRIPTOR, // bDescriptorType 00198 // PHY_TO_DESC(_ep_bulk_in), // bEndpointAddress 00199 E_BULK, // bmAttributes (0x02=bulk) 00200 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB) 00201 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB) 00202 0, // bInterval 00203 00204 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 00205 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00206 ENDPOINT_DESCRIPTOR, // bDescriptorType 00207 // PHY_TO_DESC(_ep_bulk_out), // bEndpointAddress 00208 E_BULK, // bmAttributes (0x02=bulk) 00209 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB) 00210 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB) 00211 0 // bInterval 00212 }; 00213 return configDescriptor; 00214 }
Generated on Tue Jul 12 2022 20:44:58 by 1.7.2