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