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