USB复合设备构建CDC+HID鼠标键盘套装

最近需要做一个小工具,要用到USB CDC+HID设备。又重新研究了一下USB协议和STM32的USB驱动库,也踩了不少坑,因此把代码修改过程记录一下。

开发环境:
ST-LINK v2
STM32H743开发板
PC windows 11
cubeMX v6.9.2
cubeIDE v1.13.2
cubeprogramer v2.14.0

参考资料:

  1. STMicroelectronics/stm32_mw_usb_device: Provides the USB Device library part of the STM32Cube MCU Component “middleware” for all STM32xx series. (github.com)

  2. STM32实现USB复合设备CDC MSC的正确实现方式-物联沃-IOTWORD物联网
    基于STM32CubeMx的USB CDC+MSC复合设备 - CodeBuug

  3. CDC + MSC USB Composite Device on STM32 HAL / Sudo Null IT News

  4. STMicroelectronics (github.com)

  5. Introduction to USB with STM32 - stm32mcu (stmicroelectronics.cn)

  6. ST官方USB培训课程
    MOOC - STM32 USB training - YouTube
    码农的自我修养 - USB键盘和鼠标的数据包格式_键盘协议-CSDN博客

  7. 基于STM32CUBE的USB键盘例程 | MCU起航 (mcublog.cn)

  8. 基于STM32CUBE的USB鼠标键盘二合一 | MCU起航 (mcublog.cn)

  9. USB调试工具大全 - USB中文网 (usbzh.com)

  10. USB键盘实现——带指示灯的键盘(九)-CSDN博客

  11. USB 键盘_tyustli的博客-CSDN博客

  12. 【经验分享】USB CDC 类入门培训 (stmicroelectronics.cn)

  13. STM32 USB如何配置多个CDC设备—5个CDC设备 - 知乎 (zhihu.com)

  14. 【精选】基于STM32实现USB复合设备CDC+MSC正确实现方式_stm32 usb复合设备-CSDN博客

  15. wenyezhong/usb_composite (github.com)

  16. CDC + MSC USB Composite Device on STM32 HAL / Sudo Null IT News

  17. Introduction to USB with STM32 - stm32mcu (stmicroelectronics.cn)

  18. Defined Class Codes | USB-IF

要在一个USB接口外设上实现多个设备,较方便的方式就是构建复合设备,英文称做"composite device“。只要在配置描述符中把每个设备的每一组接口都描述清楚,PC就能分别实现对每个设备功能驱动了。 ST官方提供了一个项目仓库stm32_mw_usb_device/Class/CompositeBuilder/Src at master · STMicroelectronics/stm32_mw_usb_device (github.com),里面包含了各种设备类的中间层代码,其中包含一个符合设备类,它不是一个USB设备类,而是提供了一个将其它设备整合到一起的程序入口,我们就基于此进行修改。最终我实现了一个VCP+HID键鼠套装复合设备。

操作步骤:

1.cubeMX生成工程

USB复合设备构建CDC+HID鼠标键盘套装_第1张图片
USB复合设备构建CDC+HID鼠标键盘套装_第2张图片
USB复合设备构建CDC+HID鼠标键盘套装_第3张图片

2.放入复合设备中间层代码

2.1 放入代码

把从ST提供的中间层代码库中的“CompositeBuilder”文件夹复制到工程目录的USB Class路径下。如下:
USB复合设备构建CDC+HID鼠标键盘套装_第4张图片

2.2 在工程配置中添加USE_USBD_COMPOSITE宏定义
USB复合设备构建CDC+HID鼠标键盘套装_第5张图片

2.3 修改代码BUG
虽然是ST官方提供的代码,但是用起来仍然是一堆BUG和很多未完成的功能,一个一个来把它解掉。这里我把代码修改的解释直接写到注释中,请看代码:
usbd_composite_builder.h

@@ -30,6 +30,7 @@ extern "C" {
/*这里增加几个宏定义,打开我们需要的设备类,CDC & HID,复合设备的描述符需要使用IAD描述符进行包装,所以USB_IAD宏也要打开*/
+#define USBD_CMPSIT_ACTIVATE_HID                           1U
+#define USBD_CMPSIT_ACTIVATE_CDC                           1U
+#define USBD_COMPOSITE_USE_IAD                             1U
+#define USBD_CMPST_MAX_INST_NUM                            2U


 #if USBD_CMPSIT_ACTIVATE_HID == 1U
@@ -223,7 +224,7 @@ typedef struct

 #endif /* (USBD_CMPSIT_ACTIVATE_CDC == 1) || (USBD_CMPSIT_ACTIVATE_RNDIS == 1)  || (USBD_CMPSIT_ACTIVATE_CDC_ECM == 1)*/
/*我实际使用了芯片上的两个USB外设一个HS和一个FS,所以这个变量需要2套,用于放置各自的描述符函数指针,因此我这里要改成一个数据,如果只使用一个USB外设,就不需要改*/
-extern USBD_ClassTypeDef  USBD_CMPSIT;
+extern USBD_ClassTypeDef  USBD_CMPSIT[];

 /* Exported functions prototypes ---------------------------------------------*/
 uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev);

usbd_composite_builder.c

@@ -158,7 +163,7 @@ static void  USBD_CMPSIT_MTPDesc(USBD_HandleTypeDef *pdev, uint32_t pConf, __IO
   * @{
   */
 /* This structure is used only for the Configuration descriptors and Device Qualifier */
 /*我使用了两套USB外设,所以这个数据结构要两套*/
-USBD_ClassTypeDef  USBD_CMPSIT =
+USBD_ClassTypeDef  USBD_CMPSIT[USBD_CMPST_MAX_INST_NUM] = {
 {
   NULL, /* Init, */
   NULL, /* DeInit, */
@@ -181,14 +186,36 @@ USBD_ClassTypeDef  USBD_CMPSIT =
 #if (USBD_SUPPORT_USER_STRING_DESC == 1U)
   NULL,
 #endif /* USBD_SUPPORT_USER_STRING_DESC */
-};
-
+},
+{
+  NULL, /* Init, */
+  NULL, /* DeInit, */
+  NULL, /* Setup, */
+  NULL, /* EP0_TxSent, */
+  NULL, /* EP0_RxReady, */
+  NULL, /* DataIn, */
+  NULL, /* DataOut, */
+  NULL, /* SOF,  */
+  NULL,
+  NULL,
+#ifdef USE_USB_HS
+  USBD_CMPSIT_GetHSCfgDesc,
+#else
+  NULL,
+#endif /* USE_USB_HS */
+  USBD_CMPSIT_GetFSCfgDescHS,
+  USBD_CMPSIT_GetOtherSpeedCfgDescHS,
+  USBD_CMPSIT_GetDeviceQualifierDescriptorHS,
+#if (USBD_SUPPORT_USER_STRING_DESC == 1U)
+  NULL,
+#endif /* USBD_SUPPORT_USER_STRING_DESC */
+}};
 /* The generic configuration descriptor buffer that will be filled by builder
    Size of the buffer is the maximum possible configuration descriptor size. */
/*申明两套配置描述符的memory空间*/
-__ALIGN_BEGIN static uint8_t USBD_CMPSIT_FSCfgDesc[USBD_CMPST_MAX_CONFDESC_SZ]  __ALIGN_END = {0};
-static uint8_t *pCmpstFSConfDesc = USBD_CMPSIT_FSCfgDesc;
+__ALIGN_BEGIN static uint8_t USBD_CMPSIT_FSCfgDesc[USBD_CMPST_MAX_INST_NUM][USBD_CMPST_MAX_CONFDESC_SZ]  __ALIGN_END = {0};
+static uint8_t *pCmpstFSConfDesc = USBD_CMPSIT_FSCfgDesc[0];
 /* Variable that dynamically holds the current size of the configuration descriptor */
-static __IO uint32_t CurrFSConfDescSz = 0U;
+static __IO uint32_t CurrFSConfDescSz[USBD_CMPST_MAX_INST_NUM] = {0U};

 #ifdef USE_USB_HS
 __ALIGN_BEGIN static uint8_t USBD_CMPSIT_HSCfgDesc[USBD_CMPST_MAX_CONFDESC_SZ]  __ALIGN_END = {0};
@@ -266,12 +293,12 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
 {
   uint8_t idxIf = 0U;
   uint8_t iEp = 0U;
   /*两套设备通过pdef->id变量进行区分,它是在设备初始化时被赋值*/
-
+  pCmpstFSConfDesc = USBD_CMPSIT_FSCfgDesc[pdev->id];
   /* For the first class instance, start building the config descriptor common part */
   if (pdev->classId == 0U)
   {
     /* Add configuration and IAD descriptors */
-    USBD_CMPSIT_AddConfDesc((uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz);
+    USBD_CMPSIT_AddConfDesc((uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id]);
 #ifdef USE_USB_HS
     USBD_CMPSIT_AddConfDesc((uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz);
 #endif /* USE_USB_HS */
@@ -290,7 +317,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       pdev->tclasslist[pdev->classId].Ifs[0] = idxIf;

       /* Assign endpoint numbers */
       /*这里是HID描述符字段,一般是直接在描述符数据中修改,使用这个官方库接口就要在这里修改,它会自动计算描述符大小。这里把HID端点数量改成2个*/
-      pdev->tclasslist[pdev->classId].NumEps = 1U; /* EP1_IN */
+      pdev->tclasslist[pdev->classId].NumEps = 2U; /* EP1_IN */

       /* Set IN endpoint slot */
       iEp = pdev->tclasslist[pdev->classId].EpAdd[0];
@@ -298,11 +325,15 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       /* Assign IN Endpoint */
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_INTR, pdev->tclasslist[pdev->classId].CurrPcktSze);
/*仿照输入端点的配置,分配输出端点*/
+      /* Set OUT endpoint slot */
+      iEp = pdev->tclasslist[pdev->classId].EpAdd[1];
+      USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_INTR, pdev->tclasslist[pdev->classId].CurrPcktSze);
+
       /* Configure and Append the Descriptor */
       /* 生成HID设备的描述符,包括IAD,interface,EPIN,EPOUT */
-      USBD_CMPSIT_HIDMouseDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_HIDKBMouseDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       break;
@@ -330,7 +361,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_BULK, pdev->tclasslist[pdev->classId].CurrPcktSze);

       /* Configure and Append the Descriptor */
-      USBD_CMPSIT_MSCDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_MSCDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       USBD_CMPSIT_MSCDesc(pdev, (uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz, (uint8_t)USBD_SPEED_HIGH);
@@ -366,7 +397,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_INTR, CDC_CMD_PACKET_SIZE);

       /* Configure and Append the Descriptor */
-      USBD_CMPSIT_CDCDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_CDCDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       USBD_CMPSIT_CDCDesc(pdev, (uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz, (uint8_t)USBD_SPEED_HIGH);
@@ -389,7 +420,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       pdev->tclasslist[pdev->classId].NumEps = 0U; /* only EP0 is used */
       /* Configure and Append the Descriptor */
-      USBD_CMPSIT_DFUDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_DFUDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       USBD_CMPSIT_DFUDesc(pdev, (uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz, (uint8_t)USBD_SPEED_HIGH);
@@ -425,7 +456,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_INTR, CDC_RNDIS_CMD_PACKET_SIZE);

       /* Configure and Append the Descriptor */
-      USBD_CMPSIT_RNDISDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_RNDISDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       USBD_CMPSIT_RNDISDesc(pdev, (uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz, (uint8_t)USBD_SPEED_HIGH);
@@ -461,7 +492,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_INTR, CDC_ECM_CMD_PACKET_SIZE);

       /* Configure and Append the Descriptor */
-      USBD_CMPSIT_CDC_ECMDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_CDC_ECMDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       USBD_CMPSIT_CDC_ECMDesc(pdev, (uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz, (uint8_t)USBD_SPEED_HIGH);
@@ -491,7 +522,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_ISOC, pdev->tclasslist[pdev->classId].CurrPcktSze);

       /* Configure and Append the Descriptor (only FS mode supported) */
-      USBD_CMPSIT_AUDIODesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_AUDIODesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

       break;
 #endif /* USBD_CMPSIT_ACTIVATE_AUDIO */
@@ -518,7 +549,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_INTR, pdev->tclasslist[pdev->classId].CurrPcktSze);

       /* Configure and Append the Descriptor */
-      USBD_CMPSIT_CUSTOMHIDDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_CUSTOMHIDDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       USBD_CMPSIT_CUSTOMHIDDesc(pdev, (uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz, (uint8_t)USBD_SPEED_HIGH);
@@ -548,7 +579,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_ISOC, pdev->tclasslist[pdev->classId].CurrPcktSze);

       /* Configure and Append the Descriptor */
-      USBD_CMPSIT_VIDEODesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_VIDEODesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       USBD_CMPSIT_VIDEODesc(pdev, (uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz, (uint8_t)USBD_SPEED_HIGH);
@@ -580,7 +611,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_BULK, pdev->tclasslist[pdev->classId].CurrPcktSze);

       /* Configure and Append the Descriptor */
-      USBD_CMPSIT_PRNTDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_PRNTDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       USBD_CMPSIT_PRNTDesc(pdev, (uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz, (uint8_t)USBD_SPEED_HIGH);
@@ -616,7 +647,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_INTR, CCID_CMD_PACKET_SIZE);

       /* Configure and Append the Descriptor */
-      USBD_CMPSIT_CCIDDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_CCIDDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       USBD_CMPSIT_CCIDDesc(pdev, (uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz, (uint8_t)USBD_SPEED_HIGH);
@@ -652,7 +683,7 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
       USBD_CMPSIT_AssignEp(pdev, iEp, USBD_EP_TYPE_INTR, MTP_CMD_PACKET_SIZE);

       /* Configure and Append the Descriptor */
-      USBD_CMPSIT_MTPDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz, (uint8_t)USBD_SPEED_FULL);
+      USBD_CMPSIT_MTPDesc(pdev, (uint32_t)pCmpstFSConfDesc, &CurrFSConfDescSz[pdev->id], (uint8_t)USBD_SPEED_FULL);

 #ifdef USE_USB_HS
       USBD_CMPSIT_MTPDesc(pdev, (uint32_t)pCmpstHSConfDesc, &CurrHSConfDescSz, (uint8_t)USBD_SPEED_HIGH);
@@ -680,9 +711,23 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
   */
 uint8_t  *USBD_CMPSIT_GetFSCfgDesc(uint16_t *length)
 {
@@ -680,9 +711,23 @@ uint8_t  USBD_CMPSIT_AddToConfDesc(USBD_HandleTypeDef *pdev)
   */
   /*由于我实际使用了两套设备,所以这里的描述符数据都有两份,返回地址时需要加上索引,后文类似,不再赘述*/
 uint8_t  *USBD_CMPSIT_GetFSCfgDesc(uint16_t *length)
 {
-  *length = (uint16_t)CurrFSConfDescSz;
+  *length = (uint16_t)CurrFSConfDescSz[DEVICE_FS];
+
+  return USBD_CMPSIT_FSCfgDesc[DEVICE_FS];
+}

 #ifdef USE_USB_HS
@@ -708,9 +753,22 @@ uint8_t  *USBD_CMPSIT_GetHSCfgDesc(uint16_t *length)
   */
 uint8_t  *USBD_CMPSIT_GetOtherSpeedCfgDesc(uint16_t *length)
 {
-  *length = (uint16_t)CurrFSConfDescSz;
+  *length = (uint16_t)CurrFSConfDescSz[DEVICE_FS];
+
+  return USBD_CMPSIT_FSCfgDesc[DEVICE_FS];
+}


 /**
@@ -725,6 +783,12 @@ uint8_t  *USBD_CMPSIT_GetDeviceQualifierDescriptor(uint16_t *length)
   return USBD_CMPSIT_DeviceQualifierDesc;
 }

+uint8_t  *USBD_CMPSIT_GetDeviceQualifierDescriptorHS(uint16_t *length)
+{
+  *length = (uint16_t)(sizeof(USBD_CMPSIT_DeviceQualifierDesc));
+  return USBD_CMPSIT_DeviceQualifierDesc;
+}
+
 /**
   * @brief  USBD_CMPSIT_FindFreeIFNbr
   *         Find the first interface available slot
@@ -808,39 +872,60 @@ static void  USBD_CMPSIT_AssignEp(USBD_HandleTypeDef *pdev, uint8_t Add, uint8_t
 }

 #if USBD_CMPSIT_ACTIVATE_HID == 1
+
 /**
-  * @brief  USBD_CMPSIT_HIDMouseDesc
+  * @brief  USBD_CMPSIT_HIDKBMouseDesc
   *         Configure and Append the HID Mouse Descriptor
   * @param  pdev: device instance
   * @param  pConf: Configuration descriptor pointer
   * @param  Sze: pointer to the current configuration descriptor size
   * @retval None
   */
/*这里需要重点关注,官方代码中没有IAD描述符(好像不要也行)。*/
-static void  USBD_CMPSIT_HIDMouseDesc(USBD_HandleTypeDef *pdev, uint32_t pConf,
+static void  USBD_CMPSIT_HIDKBMouseDesc(USBD_HandleTypeDef *pdev, uint32_t pConf,
                                       __IO uint32_t *Sze, uint8_t speed)
 {
   static USBD_IfDescTypeDef *pIfDesc;
   static USBD_EpDescTypeDef *pEpDesc;
-  static USBD_HIDDescTypeDef *pHidMouseDesc;
+  static USBD_HIDDescTypeDef *pHidKBMouseDesc;
+
+#if USBD_COMPOSITE_USE_IAD == 1
+  static USBD_IadDescTypeDef              *pIadDesc;
+#endif /* USBD_COMPOSITE_USE_IAD == 1 */

+#if USBD_COMPOSITE_USE_IAD == 1
+  pIadDesc                          = ((USBD_IadDescTypeDef *)(pConf + *Sze));
+  pIadDesc->bLength                 = (uint8_t)sizeof(USBD_IadDescTypeDef);
+  pIadDesc->bDescriptorType         = USB_DESC_TYPE_IAD; /* IAD descriptor */
+  pIadDesc->bFirstInterface         = pdev->tclasslist[pdev->classId].Ifs[0];
+  pIadDesc->bInterfaceCount         = 1U;    /* 1 interfaces */
+  pIadDesc->bFunctionClass          = 0x03U;
+  pIadDesc->bFunctionSubClass       = 0x00U;
+  pIadDesc->bFunctionProtocol       = 0x00U;
+  pIadDesc->iFunction               = 0U; /* String Index */
+  *Sze                              += (uint32_t)sizeof(USBD_IadDescTypeDef);
+#endif /* USBD_COMPOSITE_USE_IAD == 1 */
   /* Append HID Interface descriptor to Configuration descriptor */
   __USBD_CMPSIT_SET_IF(pdev->tclasslist[pdev->classId].Ifs[0], 0U, \
   /*鼠标的配置(0x02),改成键盘(0x01)的*/
-                       (uint8_t)(pdev->tclasslist[pdev->classId].NumEps), 0x03U, 0x01U, 0x02U, 0U);
+                       (uint8_t)(pdev->tclasslist[pdev->classId].NumEps), 0x03U, 0x01U, 0x01U, 0U);

   /* Append HID Functional descriptor to Configuration descriptor */
-  pHidMouseDesc = ((USBD_HIDDescTypeDef *)(pConf + *Sze));
-  pHidMouseDesc->bLength = (uint8_t)sizeof(USBD_HIDDescTypeDef);
-  pHidMouseDesc->bDescriptorType = HID_DESCRIPTOR_TYPE;
-  pHidMouseDesc->bcdHID = 0x0111U;
-  pHidMouseDesc->bCountryCode = 0x00U;
-  pHidMouseDesc->bNumDescriptors = 0x01U;
-  pHidMouseDesc->bHIDDescriptorType = 0x22U;
-  pHidMouseDesc->wItemLength = HID_MOUSE_REPORT_DESC_SIZE;
+  pHidKBMouseDesc = ((USBD_HIDDescTypeDef *)(pConf + *Sze));
+  pHidKBMouseDesc->bLength = (uint8_t)sizeof(USBD_HIDDescTypeDef);
+  pHidKBMouseDesc->bDescriptorType = HID_DESCRIPTOR_TYPE;
+  pHidKBMouseDesc->bcdHID = 0x0111U;
+  pHidKBMouseDesc->bCountryCode = 0x00U;
+  pHidKBMouseDesc->bNumDescriptors = 0x01U;
+  pHidKBMouseDesc->bHIDDescriptorType = 0x22U;
+  pHidKBMouseDesc->wItemLength = HID_KB_MOUSE_REPORT_DESC_SIZE;
   *Sze += (uint32_t)sizeof(USBD_HIDDescTypeDef);

   /* Append Endpoint descriptor to Configuration descriptor */
   __USBD_CMPSIT_SET_EP(pdev->tclasslist[pdev->classId].Eps[0].add, USBD_EP_TYPE_INTR, HID_EPIN_SIZE, \
-                       HID_HS_BINTERVAL, HID_FS_BINTERVAL);
+                 HID_FS_BINTERVAL, HID_FS_BINTERVAL);
+/*增加对输出端点的描述符生成代码*/
+  /* Append Endpoint descriptor to Configuration descriptor */
+  __USBD_CMPSIT_SET_EP(pdev->tclasslist[pdev->classId].Eps[1].add, USBD_EP_TYPE_INTR, HID_EPOUT_SIZE, \
+                 HID_FS_BINTERVAL, HID_FS_BINTERVAL);

   /* Update Config Descriptor and IAD descriptor */
   ((USBD_ConfigDescTypeDef *)pConf)->bNumInterfaces += 1U;
@@ -915,7 +1000,7 @@ static void  USBD_CMPSIT_CDCDesc(USBD_HandleTypeDef *pdev, uint32_t pConf, __IO
   pIadDesc->bFunctionClass          = 0x02U;
   pIadDesc->bFunctionSubClass       = 0x02U;
   pIadDesc->bFunctionProtocol       = 0x01U;
-  pIadDesc->iFunction               = 0U; /* String Index */
+  pIadDesc->iFunction               = 2U; /* String Index */
   *Sze                              += (uint32_t)sizeof(USBD_IadDescTypeDef);
 #endif /* USBD_COMPOSITE_USE_IAD == 1 */

@@ -1848,8 +1933,8 @@ uint8_t USBD_CMPST_ClearConfDesc(USBD_HandleTypeDef *pdev)
   UNUSED(pdev);

   /* Reset the configuration descriptor pointer to default value and its size to zero */
-  pCmpstFSConfDesc = USBD_CMPSIT_FSCfgDesc;
-  CurrFSConfDescSz = 0U;
+  pCmpstFSConfDesc = USBD_CMPSIT_FSCfgDesc[pdev->id];
+  CurrFSConfDescSz[pdev->id] = 0U;

 #ifdef USE_USB_HS
   pCmpstHSConfDesc = USBD_CMPSIT_HSCfgDesc;

3.修改代码

修改usbd_hid.h

将鼠标报告的4字节buffer宽度改成16字节,并且增加输出节点端点地址和buffer宽度定义,输出节点用于接收键盘LED状态。声明一个HID接收数据的buffer,这个buffer在usbd_hid.c中定义。

@@ -43,11 +43,15 @@ extern "C" {
 #ifndef HID_EPIN_ADDR
 #define HID_EPIN_ADDR                              0x81U
 #endif /* HID_EPIN_ADDR */
-#define HID_EPIN_SIZE                              0x04U
+#ifndef HID_EPOUT_ADDR
+#define HID_EPOUT_ADDR                             0x01U
+#endif /* HID_EPIN_ADDR */
+#define HID_EPIN_SIZE                              0x10U
+#define HID_EPOUT_SIZE                             0x08U

 #define USB_HID_CONFIG_DESC_SIZ                    34U
 #define USB_HID_DESC_SIZ                           9U
-#define HID_MOUSE_REPORT_DESC_SIZE                 74U
+#define HID_KB_MOUSE_REPORT_DESC_SIZE              (76U+65U)

 #define HID_DESCRIPTOR_TYPE                        0x21U
 #define HID_REPORT_DESC                            0x22U
@@ -127,6 +131,7 @@ typedef struct

 extern USBD_ClassTypeDef USBD_HID;
 #define USBD_HID_CLASS &USBD_HID
+extern uint8_t HidRxBuffer[];

修改usbd_hid.c

1.增加out端点打开和关闭的处理过程
2.增加out端点数据接收函数
3.修改鼠标HID描述符,变成键盘鼠标复合设备的HID描述符,并修改相关宏定义

@@ -91,6 +91,7 @@ static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
 static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
 static uint8_t USBD_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
 static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
+static uint8_t USBD_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
 #ifndef USE_USBD_COMPOSITE
 static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length);
 static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length);
@@ -113,7 +114,7 @@ USBD_ClassTypeDef USBD_HID =
   NULL,              /* EP0_TxSent */
   NULL,              /* EP0_RxReady */
   USBD_HID_DataIn,   /* DataIn */
-  NULL,              /* DataOut */
+  USBD_HID_DataOut,  /* DataOut */
   NULL,              /* SOF */
   NULL,
   NULL,
@@ -196,7 +197,7 @@ __ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END =
   0x00,                                               /* bCountryCode: Hardware target country */
   0x01,                                               /* bNumDescriptors: Number of HID class descriptors to follow */
   0x22,                                               /* bDescriptorType */
-  HID_MOUSE_REPORT_DESC_SIZE,                         /* wItemLength: Total length of Report descriptor */
+  HID_KB_MOUSE_REPORT_DESC_SIZE,                         /* wItemLength: Total length of Report descriptor */
   0x00,
 };

@@ -217,50 +218,87 @@ __ALIGN_BEGIN static uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_
 };
 #endif /* USE_USBD_COMPOSITE  */

-__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END =
+__ALIGN_BEGIN static uint8_t HID_KB_MOUSE_ReportDesc[HID_KB_MOUSE_REPORT_DESC_SIZE]  __ALIGN_END =
 {
-  0x05, 0x01,        /* Usage Page (Generic Desktop Ctrls)     */
-  0x09, 0x02,        /* Usage (Mouse)                          */
-  0xA1, 0x01,        /* Collection (Application)               */
-  0x09, 0x01,        /*   Usage (Pointer)                      */
-  0xA1, 0x00,        /*   Collection (Physical)                */
-  0x05, 0x09,        /*     Usage Page (Button)                */
-  0x19, 0x01,        /*     Usage Minimum (0x01)               */
-  0x29, 0x03,        /*     Usage Maximum (0x03)               */
-  0x15, 0x00,        /*     Logical Minimum (0)                */
-  0x25, 0x01,        /*     Logical Maximum (1)                */
-  0x95, 0x03,        /*     Report Count (3)                   */
-  0x75, 0x01,        /*     Report Size (1)                    */
-  0x81, 0x02,        /*     Input (Data,Var,Abs)               */
-  0x95, 0x01,        /*     Report Count (1)                   */
-  0x75, 0x05,        /*     Report Size (5)                    */
-  0x81, 0x01,        /*     Input (Const,Array,Abs)            */
-  0x05, 0x01,        /*     Usage Page (Generic Desktop Ctrls) */
-  0x09, 0x30,        /*     Usage (X)                          */
-  0x09, 0x31,        /*     Usage (Y)                          */
-  0x09, 0x38,        /*     Usage (Wheel)                      */
-  0x15, 0x81,        /*     Logical Minimum (-127)             */
-  0x25, 0x7F,        /*     Logical Maximum (127)              */
-  0x75, 0x08,        /*     Report Size (8)                    */
-  0x95, 0x03,        /*     Report Count (3)                   */
-  0x81, 0x06,        /*     Input (Data,Var,Rel)               */
-  0xC0,              /*   End Collection                       */
-  0x09, 0x3C,        /*   Usage (Motion Wakeup)                */
-  0x05, 0xFF,        /*   Usage Page (Reserved 0xFF)           */
-  0x09, 0x01,        /*   Usage (0x01)                         */
-  0x15, 0x00,        /*   Logical Minimum (0)                  */
-  0x25, 0x01,        /*   Logical Maximum (1)                  */
-  0x75, 0x01,        /*   Report Size (1)                      */
-  0x95, 0x02,        /*   Report Count (2)                     */
-  0xB1, 0x22,        /*   Feature (Data,Var,Abs,NoWrp)         */
-  0x75, 0x06,        /*   Report Size (6)                      */
-  0x95, 0x01,        /*   Report Count (1)                     */
-  0xB1, 0x01,        /*   Feature (Const,Array,Abs,NoWrp)      */
-  0xC0               /* End Collection                         */
+               0x05, 0x01, // USAGE_PAGE (Generic Desktop)
+               0x09, 0x06, // USAGE (Keyboard)
+               0xa1, 0x01, // COLLECTION (Application)
+           0x85, 0x01, // Report ID (1)
+               0x05, 0x07, //   USAGE_PAGE (Keyboard/Keypad)
+               0x19, 0xe0, //   USAGE_MINIMUM (Keyboard LeftControl)
+               0x29, 0xe7, //   USAGE_MAXIMUM (Keyboard Right GUI)
+               0x15, 0x00, //   LOGICAL_MINIMUM (0)
+               0x25, 0x01, //   LOGICAL_MAXIMUM (1)
+               0x95, 0x08, //   REPORT_COUNT (8)
+               0x75, 0x01, //   REPORT_SIZE (1)
+               0x81, 0x02, //   INPUT (Data,Var,Abs)
+               0x95, 0x01, //   REPORT_COUNT (1)
+               0x75, 0x08, //   REPORT_SIZE (8)
+               0x81, 0x03, //   INPUT (Cnst,Var,Abs)
+               0x95, 0x06, //   REPORT_COUNT (6)
+               0x75, 0x08, //   REPORT_SIZE (8)
+               0x15, 0x00, //   LOGICAL_MINIMUM (0)
+               0x25, 0xFF, //   LOGICAL_MAXIMUM (255)
+               0x19, 0x00, //   USAGE_MINIMUM (Reserved (no event indicated))
+               0x29, 0x65, //   USAGE_MAXIMUM (Keyboard Application)
+               0x81, 0x00, //   INPUT (Data,Ary,Abs)
+               0x25, 0x01, //   LOGICAL_MAXIMUM (1)
+               0x95, 0x03, //   REPORT_COUNT (2)
+               0x75, 0x01, //   REPORT_SIZE (1)
+               0x05, 0x08, //   USAGE_PAGE (LEDs)
+               0x19, 0x01, //   USAGE_MINIMUM (Num Lock)
+               0x29, 0x03, //   USAGE_MAXIMUM (Scroll Lock)
+               0x91, 0x02, //   OUTPUT (Data,Var,Abs)
+               0x95, 0x01, //   REPORT_COUNT (1)
+               0x75, 0x05, //   REPORT_SIZE (6)
+               0x91, 0x03, //   OUTPUT (Cnst,Var,Abs)
+               0xc0,        // END_COLLECTION
+                              // 65 bytes
+               0x05, 0x01,        // Usage Page (Generic Desktop Ctrls)
+               0x09, 0x02,        // Usage (Mouse)
+               0xA1, 0x01,        // Collection (Application)
+               0x85, 0x02,        //   Report ID (2)
+               0x09, 0x01,        //   Usage (Pointer)
+               0xA1, 0x00,        //   Collection (Physical)
+               0x05, 0x09,        //     Usage Page (Button)
+               0x19, 0x01,        //     Usage Minimum (0x01)
+               0x29, 0x03,        //     Usage Maximum (0x03)
+               0x15, 0x00,        //     Logical Minimum (0)
+               0x25, 0x01,        //     Logical Maximum (1)
+               0x95, 0x03,        //     Report Count (3)
+               0x75, 0x01,        //     Report Size (1)
+               0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
+               0x95, 0x01,        //     Report Count (1)
+               0x75, 0x05,        //     Report Size (5)
+               0x81, 0x01,        //     Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
+               0x05, 0x01,        //     Usage Page (Generic Desktop Ctrls)
+               0x09, 0x30,        //     Usage (X)
+               0x09, 0x31,        //     Usage (Y)
+               0x09, 0x38,        //     Usage (Wheel)
+               0x15, 0x81,        //     Logical Minimum (-127)
+               0x25, 0x7F,        //     Logical Maximum (127)
+               0x75, 0x08,        //     Report Size (8)
+               0x95, 0x03,        //     Report Count (3)
+               0x81, 0x06,        //     Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
+               0xC0,              //   End Collection
+               0x09, 0x3C,        //   Usage (Motion Wakeup)
+               0x05, 0xFF,        //   Usage Page (Reserved 0xFF)
+               0x09, 0x01,        //   Usage (0x01)
+               0x15, 0x00,        //   Logical Minimum (0)
+               0x25, 0x01,        //   Logical Maximum (1)
+               0x75, 0x01,        //   Report Size (1)
+               0x95, 0x02,        //   Report Count (2)
+               0xB1, 0x22,        //   Feature (Data,Var,Abs,No Wrap,Linear,No Preferred State,No Null Position,Non-volatile)
+               0x75, 0x06,        //   Report Size (6)
+               0x95, 0x01,        //   Report Count (1)
+               0xB1, 0x01,        //   Feature (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
+               0xC0,              // End Collection
+                                                  // 76 + 65 bytes
 };

+static uint8_t HIDOutEpAdd = HID_EPOUT_ADDR;
+uint8_t HidRxBuffer[8] = {0};  //for keyboard LEDs
 /**
   * @}
   */
@@ -296,23 +334,32 @@ static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
 #ifdef USE_USBD_COMPOSITE
   /* Get the Endpoints addresses allocated for this class instance */
   HIDInEpAdd  = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR, (uint8_t)pdev->classId);
+  HIDOutEpAdd  = USBD_CoreGetEPAdd(pdev, USBD_EP_OUT, USBD_EP_TYPE_INTR, (uint8_t)pdev->classId);
 #endif /* USE_USBD_COMPOSITE */

   if (pdev->dev_speed == USBD_SPEED_HIGH)
   {
     pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = HID_HS_BINTERVAL;
+    pdev->ep_out[HIDOutEpAdd & 0xFU].bInterval = HID_HS_BINTERVAL;
+
   }
   else   /* LOW and FULL-speed endpoints */
   {
     pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = HID_FS_BINTERVAL;
+    pdev->ep_out[HIDOutEpAdd & 0xFU].bInterval = HID_FS_BINTERVAL;
   }

   /* Open EP IN */
   (void)USBD_LL_OpenEP(pdev, HIDInEpAdd, USBD_EP_TYPE_INTR, HID_EPIN_SIZE);
   pdev->ep_in[HIDInEpAdd & 0xFU].is_used = 1U;
+  (void)USBD_LL_OpenEP(pdev, HIDOutEpAdd, USBD_EP_TYPE_INTR, HID_EPOUT_SIZE);
+  pdev->ep_out[HIDOutEpAdd & 0xFU].is_used = 1U;

   hhid->state = USBD_HID_IDLE;
/*这句得加上,互联网上所有教程都没有提这里,不加会导致收不到PC下发的LED状态通知收不到*/
+  /* Prepare Out endpoint to receive 1st packet */
+  USBD_LL_PrepareReceive(pdev,HIDOutEpAdd,HidRxBuffer,2);
+
   return (uint8_t)USBD_OK;
 }

@@ -330,12 +377,16 @@ static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
 #ifdef USE_USBD_COMPOSITE
   /* Get the Endpoints addresses allocated for this class instance */
   HIDInEpAdd  = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR, (uint8_t)pdev->classId);
+  HIDOutEpAdd  = USBD_CoreGetEPAdd(pdev, USBD_EP_OUT, USBD_EP_TYPE_INTR, (uint8_t)pdev->classId);
 #endif /* USE_USBD_COMPOSITE */

   /* Close HID EPs */
   (void)USBD_LL_CloseEP(pdev, HIDInEpAdd);
   pdev->ep_in[HIDInEpAdd & 0xFU].is_used = 0U;
   pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = 0U;
+  (void)USBD_LL_CloseEP(pdev, HIDOutEpAdd);
+  pdev->ep_out[HIDOutEpAdd & 0xFU].is_used = 0U;
+  pdev->ep_out[HIDOutEpAdd & 0xFU].bInterval = 0U;

   /* Free allocated memory */
   if (pdev->pClassDataCmsit[pdev->classId] != NULL)
@@ -412,8 +463,8 @@ static uint8_t USBD_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *re
         case USB_REQ_GET_DESCRIPTOR:
           if ((req->wValue >> 8) == HID_REPORT_DESC)
           {
-            len = MIN(HID_MOUSE_REPORT_DESC_SIZE, req->wLength);
-            pbuf = HID_MOUSE_ReportDesc;
+            len = MIN(HID_KB_MOUSE_REPORT_DESC_SIZE, req->wLength);
+            pbuf = HID_KB_MOUSE_ReportDesc;
           }
           else if ((req->wValue >> 8) == HID_DESCRIPTOR_TYPE)
           {
@@ -620,6 +671,20 @@ static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
   return (uint8_t)USBD_OK;
 }
+/**
+   *USB_HID_DataOut buffer
+   */
+static uint8_t USBD_HID_DataOut (USBD_HandleTypeDef *pdev,
+                              uint8_t epnum)
+{
+  USBD_LL_PrepareReceive(pdev,epnum,HidRxBuffer,2);
+  log_printf("USBD_HID_DataOut\r\n");
+  /* Ensure that the FIFO is empty before a new transfer, this condition could
+  be caused by  a new transfer before the end of the previous transfer */
+  ((USBD_HID_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId])->state = USBD_HID_IDLE;
+  return USBD_OK;
+}
+
usbd_device.c

这里就看出使用ST官方提供的代码的好处,我们要构建一个复合设备,只需要在这里连续注册两次就可以了。

@@ -28,7 +28,7 @@
 #include "usbd_hid.h"

 /* USER CODE BEGIN Includes */
-
+#include "usbd_composite_builder.h"
 /* USER CODE END Includes */

 /* USER CODE BEGIN PV */
@@ -43,7 +43,11 @@

 /* USB Device Core handle declaration. */
 USBD_HandleTypeDef hUsbDeviceFS;
+uint8_t epnums_CDC_FS[] = {0x81U, 0x01U, 0x82U};
+uint8_t epnums_HID_FS[] = {0x83U, 0x03U};

 /*
  * -- Insert your variables declaration here --
@@ -74,14 +78,32 @@ void MX_USB_DEVICE_Init(void)
   {
     Error_Handler();
   }
-  if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_HID) != USBD_OK)
+  if (USBD_RegisterClassComposite(&hUsbDeviceFS, &USBD_CDC, CLASS_TYPE_CDC, &epnums_CDC_FS[0]) != USBD_OK)
+  {
+    Error_Handler();
+  }
+  hUsbDeviceFS.classId--;
+  if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK)
+  {
+    Error_Handler();
+  }
+  hUsbDeviceFS.classId++;
+  if (USBD_RegisterClassComposite(&hUsbDeviceFS, &USBD_HID, CLASS_TYPE_HID, &epnums_HID_FS[0]) != USBD_OK)
   {
     Error_Handler();
+//  if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_HID) != USBD_OK)
+//  {
+//    Error_Handler();
+//  }
   if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
   {
     Error_Handler();
usbd_desc.c

修改配置描述符,设置成符合设备类。

@@ -156,9 +156,9 @@ __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
   USB_DESC_TYPE_DEVICE,       /*bDescriptorType*/
   0x00,                       /*bcdUSB */
   0x02,
-  0x00,                       /*bDeviceClass*/
-  0x00,                       /*bDeviceSubClass*/
-  0x00,                       /*bDeviceProtocol*/
+  0xEF,                       /*bDeviceClass*/
+  0x02,                       /*bDeviceSubClass*/
+  0x01,                       /*bDeviceProtocol*/
   USB_MAX_EP0_SIZE,           /*bMaxPacketSize*/
   LOBYTE(USBD_VID),           /*idVendor*/
   HIBYTE(USBD_VID),           /*idVendor*/
修改usbd_conf.c

由于我们使用到端点3,默认工程中是没有给端点3设置fifo的,所以加上。

@@ -24,6 +24,7 @@
 #include "usbd_def.h"
 #include "usbd_core.h"
 #include "usbd_hid.h"
+#include "usbd_cdc.h"

 /* USER CODE BEGIN Includes */

@@ -423,7 +424,9 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
   /* USER CODE BEGIN TxRx_Configuration */
   HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_FS, 0x80);
   HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 0, 0x40);
-  HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x80);
+  HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x40);
+  HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 3, 0x40);
   /* USER CODE END TxRx_Configuration */
   }
   if (pdev->id == DEVICE_HS) {

当我们使用复合设备时,下面这个函数也要改一下,裸机代码中没法动态分配内存,只能使用静态变量空间替换一下。初始程序中只支持一个设备,空间不够,我们需要把它扩容一下。

 void *USBD_static_malloc(uint32_t size)
 {
-  UNUSED(size);
-  static uint32_t mem[(sizeof(USBD_HID_HandleTypeDef)/4)+1];/* On 32-bit boundary */
-  return mem;
+//  UNUSED(size);
+  static uint32_t pcurmempos=0;
+  static uint32_t usedmemsz=0;
+  static uint32_t mem[(sizeof(USBD_HID_HandleTypeDef)+sizeof(USBD_CDC_HandleTypeDef))/4+1];/* On 32-bit boundary */
+  pcurmempos = usedmemsz;
+  usedmemsz += size/4;
+  return &mem[pcurmempos];
 }

具体的代码改动没法全部贴出来,但是所有要改动的点都点到了。过程中在互联网上搜了大量资料,要么太旧,要么不全或者说的不清不楚。所以还是自己摸索了一遍,把过程记录在这里做个备忘。

你可能感兴趣的:(嵌入式开发,计算机外设,stm32)