June Feather
STM32 USB mutidevice 实现 CDC串口 + Mass内存盘 (一)
STM32 USB mutidevice 实现 CDC串口 + Mass内存盘 (一)

STM32 USB mutidevice 实现 CDC串口 + Mass内存盘 (一)

最近移植了一下 STM32F103 MutiDevice的功能 实现一个 SD读卡器 和 CDC串口同时实现的功能

其实整个过程是融合2部分的代码

USB程序的架构是这样的

HW层 -》 USBEndPoint组 -》 回调函数

HW层两部分的代码完全一样不需要关心

主要过程是配置Endpoint 和 USB 相关的回调函数

USBEndPoint 一共有可配置的 EP 0-7 8个端点

其中EP0 用于传输枚举 及其它控制信息不可以动

其他端点EP 均可以自由配置

CDC 和 Mass占用端点如下

 CDCMass 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 数据输出

确定了这个分配之后,就要用代码实现:

实现需要修改3处:

1.端点描述符和设备描述符
在usbdesc.c中修改
首先修改设备描述符为Muti设备

/* 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,
    
    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             0x03
 
const 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*/    
  };

CDC实现最好添加一个 IAD的头 不过测试似乎不加也没关系

修改主要是这些部分
1.修改 USB类型 同时增加 IAD头
2.重新分配一下Interface,可以用枚举类型方式实现
CDC 需要 2个 Mass需要一个
3.设置好Interface的 ENDPoint
4.重新设置一下 这个描述符的长度

这时运行 就可以看到描述的设备了 不过可能出现感叹号

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注