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.
Fork of USBDevice by
USBCDC.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 "USBCDC.h" 00021 00022 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08}; 00023 00024 #define DEFAULT_CONFIGURATION (1) 00025 00026 #define CDC_SET_LINE_CODING 0x20 00027 #define CDC_GET_LINE_CODING 0x21 00028 #define CDC_SET_CONTROL_LINE_STATE 0x22 00029 00030 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK 00031 00032 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) { 00033 terminal_connected = false; 00034 USBDevice::connect(); 00035 } 00036 00037 bool USBCDC::USBCallback_request(void) { 00038 /* Called in ISR context */ 00039 00040 bool success = false; 00041 CONTROL_TRANSFER * transfer = getTransferPtr(); 00042 00043 /* Process class-specific requests */ 00044 00045 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { 00046 switch (transfer->setup.bRequest) { 00047 case CDC_GET_LINE_CODING: 00048 transfer->remaining = 7; 00049 transfer->ptr = cdc_line_coding; 00050 transfer->direction = DEVICE_TO_HOST; 00051 success = true; 00052 break; 00053 case CDC_SET_LINE_CODING: 00054 transfer->remaining = 7; 00055 success = true; 00056 terminal_connected = true; 00057 break; 00058 case CDC_SET_CONTROL_LINE_STATE: 00059 terminal_connected = false; 00060 success = true; 00061 break; 00062 default: 00063 break; 00064 } 00065 } 00066 00067 return success; 00068 } 00069 00070 00071 // Called in ISR context 00072 // Set configuration. Return false if the 00073 // configuration is not supported. 00074 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) { 00075 if (configuration != DEFAULT_CONFIGURATION) { 00076 return false; 00077 } 00078 00079 // Configure endpoints > 0 00080 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT); 00081 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK); 00082 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); 00083 00084 // We activate the endpoint to be able to recceive data 00085 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); 00086 return true; 00087 } 00088 00089 bool USBCDC::send(uint8_t * buffer, uint32_t size) { 00090 return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE); 00091 } 00092 00093 bool USBCDC::readEP(uint8_t * buffer, uint32_t * size) { 00094 if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE)) 00095 return false; 00096 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE)) 00097 return false; 00098 return true; 00099 } 00100 00101 bool USBCDC::readEP_NB(uint8_t * buffer, uint32_t * size) { 00102 if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE)) 00103 return false; 00104 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE)) 00105 return false; 00106 return true; 00107 } 00108 00109 00110 uint8_t * USBCDC::deviceDesc() { 00111 static uint8_t deviceDescriptor[] = { 00112 18, // bLength 00113 1, // bDescriptorType 00114 0x10, 0x01, // bcdUSB 00115 2, // bDeviceClass 00116 0, // bDeviceSubClass 00117 0, // bDeviceProtocol 00118 MAX_PACKET_SIZE_EP0, // bMaxPacketSize0 00119 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor 00120 LSB(PRODUCT_ID), MSB(PRODUCT_ID),// idProduct 00121 0x00, 0x01, // bcdDevice 00122 1, // iManufacturer 00123 2, // iProduct 00124 3, // iSerialNumber 00125 1 // bNumConfigurations 00126 }; 00127 return deviceDescriptor; 00128 } 00129 00130 uint8_t * USBCDC::stringIinterfaceDesc() { 00131 static uint8_t stringIinterfaceDescriptor[] = { 00132 0x08, 00133 STRING_DESCRIPTOR, 00134 'C',0,'D',0,'C',0, 00135 }; 00136 return stringIinterfaceDescriptor; 00137 } 00138 00139 uint8_t * USBCDC::stringIproductDesc() { 00140 static uint8_t stringIproductDescriptor[] = { 00141 0x16, 00142 STRING_DESCRIPTOR, 00143 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 00144 }; 00145 return stringIproductDescriptor; 00146 } 00147 00148 00149 #define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7) 00150 00151 uint8_t * USBCDC::configurationDesc() { 00152 static uint8_t configDescriptor[] = { 00153 // configuration descriptor 00154 9, // bLength 00155 2, // bDescriptorType 00156 LSB(CONFIG1_DESC_SIZE), // wTotalLength 00157 MSB(CONFIG1_DESC_SIZE), 00158 2, // bNumInterfaces 00159 1, // bConfigurationValue 00160 0, // iConfiguration 00161 0x80, // bmAttributes 00162 50, // bMaxPower 00163 00164 // IAD to associate the two CDC interfaces 00165 0x08, // bLength 00166 0x0b, // bDescriptorType 00167 0x00, // bFirstInterface 00168 0x02, // bInterfaceCount 00169 0x02, // bFunctionClass 00170 0x02, // bFunctionSubClass 00171 0, // bFunctionProtocol 00172 0, // iFunction 00173 00174 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 00175 9, // bLength 00176 4, // bDescriptorType 00177 0, // bInterfaceNumber 00178 0, // bAlternateSetting 00179 1, // bNumEndpoints 00180 0x02, // bInterfaceClass 00181 0x02, // bInterfaceSubClass 00182 0x01, // bInterfaceProtocol 00183 0, // iInterface 00184 00185 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26 00186 5, // bFunctionLength 00187 0x24, // bDescriptorType 00188 0x00, // bDescriptorSubtype 00189 0x10, 0x01, // bcdCDC 00190 00191 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27 00192 5, // bFunctionLength 00193 0x24, // bDescriptorType 00194 0x01, // bDescriptorSubtype 00195 0x03, // bmCapabilities 00196 1, // bDataInterface 00197 00198 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28 00199 4, // bFunctionLength 00200 0x24, // bDescriptorType 00201 0x02, // bDescriptorSubtype 00202 0x06, // bmCapabilities 00203 00204 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33 00205 5, // bFunctionLength 00206 0x24, // bDescriptorType 00207 0x06, // bDescriptorSubtype 00208 0, // bMasterInterface 00209 1, // bSlaveInterface0 00210 00211 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 00212 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00213 ENDPOINT_DESCRIPTOR, // bDescriptorType 00214 PHY_TO_DESC(EPINT_IN), // bEndpointAddress 00215 E_INTERRUPT, // bmAttributes (0x03=intr) 00216 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) 00217 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) 00218 16, // bInterval 00219 00220 00221 00222 00223 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 00224 9, // bLength 00225 4, // bDescriptorType 00226 1, // bInterfaceNumber 00227 0, // bAlternateSetting 00228 2, // bNumEndpoints 00229 0x0A, // bInterfaceClass 00230 0x00, // bInterfaceSubClass 00231 0x00, // bInterfaceProtocol 00232 0, // iInterface 00233 00234 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 00235 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00236 ENDPOINT_DESCRIPTOR, // bDescriptorType 00237 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress 00238 E_BULK, // bmAttributes (0x02=bulk) 00239 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB) 00240 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB) 00241 0, // bInterval 00242 00243 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 00244 ENDPOINT_DESCRIPTOR_LENGTH, // bLength 00245 ENDPOINT_DESCRIPTOR, // bDescriptorType 00246 PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress 00247 E_BULK, // bmAttributes (0x02=bulk) 00248 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB) 00249 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB) 00250 0 // bInterval 00251 }; 00252 return configDescriptor; 00253 }
Generated on Wed Jul 20 2022 00:01:16 by
