最近移植了一下 STM32F103 MutiDevice的功能 实现一个 SD读卡器 和 CDC串口同时实现的功能
其实整个过程是融合2部分的代码
USB程序的架构是这样的
HW层 -》 USBEndPoint组 -》 回调函数
HW层两部分的代码完全一样不需要关心
主要过程是配置Endpoint 和 USB 相关的回调函数
USBEndPoint 一共有可配置的 EP 0-7 8个端点
其中EP0 用于传输枚举 及其它控制信息不可以动
其他端点EP 均可以自由配置
CDC 和 Mass占用端点如下
CDC | Mass Storage | |
ENDP0 TX/RX |
枚举和控制 | 枚举和控制 |
ENDP1 TX |
CDC 串口输入 |
Mass数据输入 |
ENDP2 TX |
CDC 中断输入 |
未使用 |
ENDP2 RX |
未使用 | Mass数据输出 |
ENDP3 RX |
CDC 串口输出 |
未使用 |
。。。。这个表格真不好用
现在重新分配如下
EP0 : 不动
EP1TX:不动 串口输入
EP2TX:不动 CDC中断
EP3RX:不动 串口输出
EP4TX:Mass 数据输入
EP5RX:Mass 数据输出
/* USB Standard Device Descriptor */ const u8 Composite_DeviceDescriptor[] = { 0x12, /* bLength */ USB_DEVICE_DESCRIPTOR_TYPE, /* bDescriptorType */ 0x00, 0x02, /* bcdUSB = 2.00 */ 0x00,//0xEF,//0x02, /* bDeviceClass: CDC */ 0x00,//0X02,//0x00, /* bDeviceSubClass */ 0x00,//0X01,//0x00, /* bDeviceProtocol */ 0x40, /* bMaxPacketSize0 */ 0x83, 0x04, /* idVendor = 0x0483 */ 0x41, 0xaa, /* idProduct = 0x7540 */ 0x00, 0x02, /* bcdDevice = 2.00 */ 1, /* Index of string descriptor describing manufacturer */ 2, /* Index of string descriptor describing product */ 3, /* Index of string descriptor describing the device's serial number */ 0x01 /* bNumConfigurations */ };主要修改USB子类 修改为 EF 02 01 或者 00 00 00 似乎都可以
#define WBVAL(x) (x & 0xFF),((x >> 8) & 0xFF) // Interface numbers enum { USB_CDC_CIF_NUM0, USB_CDC_DIF_NUM0, USB_MAS_CIF_NUM0, // USB_MAS_DIF_NUM2,CDC 实现最好添加一个 IAD 的头 不过测试似乎不加也没关系USB_NUM_INTERFACES // number of interfaces
};
#define USB_CONFIGUARTION_DESC_SIZE 9
#define USB_IAD_DESC_SIZE 8
#define USB_INTERFACE_DESC_SIZE 9
#define USB_ENDPOINT_DESC_SIZE 7#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
#define CDC_COMMUNICATION_INTERFACE_SUBCLASS 0x00
#define CDC_COMMUNICATION_INTERFACE_PROTOCOL 0x00
#define CDC_DATA_INTERFACE_CLASS 0x0A
#define CDC_CS_INTERFACE 0x24
#define CDC_HEADER 0x00
#define CDC_CALL_MANAGEMENT 0x01
#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
#define CDC_UNION 0x06#define USB_ENDPOINT_TYPE_BULK 0x02
#define USB_ENDPOINT_TYPE_INTERRUPT 0x03const u8 Composite_ConfigDescriptor[] =
{
/Configuation Descriptor/
USB_CONFIGUARTION_DESC_SIZE, /* bLength: Configuation Descriptor size /
USB_CONFIGURATION_DESCRIPTOR_TYPE, / bDescriptorType: Configuration /
//Composite_SIZ_CONFIG_DESC, / wTotalLength:no of returned bytes /
//0x00,
WBVAL( / wTotalLength /
Composite_SIZ_CONFIG_DESC
),
USB_NUM_INTERFACES, / bNumInterfaces: 2 interface /
0x01, / bConfigurationValue: Configuration value /
0x00, / iConfiguration: Index of string descriptor describing the configuration /
0xC0, / bmAttributes: self powered /
0x32, / MaxPower 0 mA */USB_IAD_DESC_SIZE, USB_IAD_DESCRIPTOR_TYPE, USB_CDC_CIF_NUM0, /* bFirstInterface */ 0x02, /* bInterfaceCount */ CDC_COMMUNICATION_INTERFACE_CLASS, /* bFunctionClass */ CDC_COMMUNICATION_INTERFACE_SUBCLASS, /* bFunctionSubClass */ CDC_COMMUNICATION_INTERFACE_PROTOCOL, /* bFunctionProcotol */ 0x00, /* iInterface */
// //CDC
/Interface Descriptor/
USB_INTERFACE_DESC_SIZE, /* bLength: Interface Descriptor size /
USB_INTERFACE_DESCRIPTOR_TYPE, / bDescriptorType: Interface /
/ Interface descriptor type /
USB_CDC_CIF_NUM0, / bInterfaceNumber: Number of Interface /
0x00, / bAlternateSetting: Alternate setting /
0x01, / bNumEndpoints: One endpoints used /
0x02, / bInterfaceClass: Communication Interface Class /
0x02, / bInterfaceSubClass: Abstract Control Model /
0x01, / bInterfaceProtocol: Common AT commands /
0x00, / iInterface: /
/Header Functional Descriptor/
0x05, / bLength: Endpoint Descriptor size /
0x24, / bDescriptorType: CS_INTERFACE /
0x00, / bDescriptorSubtype: Header Func Desc /
0x10, / bcdCDC: spec release number /
0x01,
/Call Managment Functional Descriptor/
0x05, / bFunctionLength /
0x24, / bDescriptorType: CS_INTERFACE /
0x01, / bDescriptorSubtype: Call Management Func Desc /
0x00, / bmCapabilities: D0+D1 /
USB_CDC_DIF_NUM0, / bDataInterface: 1 /
/ACM Functional Descriptor/
0x04, / bFunctionLength /
0x24, / bDescriptorType: CS_INTERFACE /
0x02, / bDescriptorSubtype: Abstract Control Management desc /
0x02, / bmCapabilities /
/Union Functional Descriptor/
0x05, / bFunctionLength /
0x24, / bDescriptorType: CS_INTERFACE /
0x06, / bDescriptorSubtype: Union func desc /
USB_CDC_CIF_NUM0, / bMasterInterface: Communication class interface /
USB_CDC_DIF_NUM0, / bSlaveInterface0: Data Class Interface /
/Endpoint 2 Descriptor/
USB_ENDPOINT_DESC_SIZE, / bLength: Endpoint Descriptor size /
USB_ENDPOINT_DESCRIPTOR_TYPE, / bDescriptorType: Endpoint /
0x82, / bEndpointAddress: (IN2) /
USB_ENDPOINT_TYPE_INTERRUPT, / bmAttributes: Interrupt /
WBVAL(Composite_INT_SIZE), / wMaxPacketSize: /
0xFF, / bInterval: /
/Data class interface descriptor/
USB_INTERFACE_DESC_SIZE, / bLength: Endpoint Descriptor size /
USB_INTERFACE_DESCRIPTOR_TYPE, / bDescriptorType: /
USB_CDC_DIF_NUM0, / bInterfaceNumber: Number of Interface /
0x00, / bAlternateSetting: Alternate setting /
0x02, / bNumEndpoints: Two endpoints used /
CDC_DATA_INTERFACE_CLASS, / bInterfaceClass: CDC /
0x00, / bInterfaceSubClass: /
0x00, / bInterfaceProtocol: /
0x00, / iInterface: /
/Endpoint 3 Descriptor/
USB_ENDPOINT_DESC_SIZE, / bLength: Endpoint Descriptor size /
USB_ENDPOINT_DESCRIPTOR_TYPE, / bDescriptorType: Endpoint /
0x03, / bEndpointAddress: (OUT3) /
USB_ENDPOINT_TYPE_BULK, / bmAttributes: Bulk /
WBVAL(Composite_DATA_SIZE), / wMaxPacketSize: /
0x00, / bInterval: ignore for Bulk transfer /
/Endpoint 1 Descriptor/
USB_ENDPOINT_DESC_SIZE, / bLength: Endpoint Descriptor size /
USB_ENDPOINT_DESCRIPTOR_TYPE, / bDescriptorType: Endpoint /
0x81, / bEndpointAddress: (IN1) /
USB_ENDPOINT_TYPE_BULK, / bmAttributes: Bulk /
WBVAL(Composite_DATA_SIZE), / wMaxPacketSize: /
0x00, / bInterval /
/******************* Descriptor of Mass Storage interface ***************/
/ 09 /
0x09, / bLength: Interface Descriptor size /
0x04, / bDescriptorType: /
/ Interface descriptor type /
USB_MAS_CIF_NUM0, / bInterfaceNumber: Number of Interface /
0x00, / bAlternateSetting: Alternate setting /
0x02, / bNumEndpoints/
0x08, / bInterfaceClass: MASS STORAGE Class /
0x06, / bInterfaceSubClass : SCSI transparent/
0x50, / nInterfaceProtocol /
1, / iInterface: /
/ 18 */
0x07, /Endpoint descriptor length = 7/
0x05, /*Endpoint descriptor type */
0x84,//0x82, /*Endpoint address (IN, address 2) */
0x02, /*Bulk endpoint type */
0x40, /*Maximum packet size (64 bytes) */
0x00,
0x00, /*Polling interval in milliseconds /
/ 25 */
0x07, /*Endpoint descriptor length = 7 */
0x05, /*Endpoint descriptor type */
0x05,//0x02, /*Endpoint address (OUT, address 2) */
0x02, /*Bulk endpoint type */
0x40, /*Maximum packet size (64 bytes) */
0x00,
0x00 /Polling interval in milliseconds/
/32/
};
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于