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生成工程

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

2.1 放入代码

把从ST提供的中间层代码库中的“CompositeBuilder”文件夹复制到工程目录的USB Class路径下。如下:
在这里插入图片描述

2.2 在工程配置中添加USE_USBD_COMPOSITE宏定义
在这里插入图片描述

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];
 }

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/150936.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Google 向中国开发者开放数百份 TensorFlow 资源

Google 的机器学习框架 TensorFlow 自 2015 年开源后,已然成为 AI 领域最受欢迎的框架。 据统计,在广受欢迎的 Python 编程语言在线软件知识库 PyPi 上,TensorFlow 的下载次数已超过 90 万,其中有 15% 来自中国。谷歌官方博客也表…

11.10~11.15置信区间,均值、方差假设检验,正态,t,卡方,F分布,第一第二类错误

置信度,置信区间 给定一个置信度,就可以算出一个置信区间。 如果给的置信度越大,那么阿尔法就越小 给的置信度越小,那么α就越大,那么 考虑精确性,希望区间长度尽可能小,所以是取正态的中间…

Postman的常规断言/动态参数断言/全局断言

近期在复习Postman的基础知识,在小破站上跟着百里老师系统复习了一遍,也做了一些笔记,希望可以给大家一点点启发。 断言,包括状态码断言和业务断言,状态码断言有一个,业务断言有多个。 一)常规的…

Python数据容器之(元组)

我们前面所了解的列表是可以修改的,但如果想要传递的信息,不被篡改,列表就不合适了。 元组同列表一样,都是可以封装多个、不同类型的元素在内。 但最大的不同点在于: 元组一旦定义完成,就不可修改 所以…

exce常用

一、冻结 同时冻结行和列 打开一个Excel表格,点击选择需要冻结的行和列交接处对应的单元格(例如:需要同时冻结1、2行和A、B列,则选中行列交接对应的C3单元格)), 即下一行 和下一列的交接点。 …

虹科方案 | 从概念到生产的自动驾驶软件在环(SiL)测试解决方案

来源:雅名特自动驾驶 虹科方案 | 从概念到生产的自动驾驶软件在环(SiL)测试解决方案 自动驾驶软件在环(SiL)测试解决方案 自动驾驶软件在环(SiL)测试解决方案能够研究和验证高历程实验和恶劣驾…

Xocde 升级15 或者 iOS17报错:

错误: Assertion failed: (false && "compact unwind compressed function offset doesnt fit in 24 bits"), function operator(), file Layout.cpp, line 5758. 翻译: 断言失败:(false&&“压缩展开…

计算机网络之网络体系结构

计算机网络体系结构 一、常见的计算机体系结构 1.1 OSI标准以及TCP/IP体系结构 OSI标准失败的原因: OSI的专家们缺乏实际经验,他们在完成OSI标准时没有商业驱动力OSI的协议实现起来过分复杂,而且运行效率很低OSI标准的制定周期太长&#x…

Python中sys模块详解:常用方法与变量

更多Python学习内容:ipengtao.com 大家好,我是涛哥,今天为大家分享 Python中sys模块详解:常用方法与变量,文章2500字,阅读大约8分钟,大家enjoy~~ sys 模块是 Python 标准库中的一个核心模块&…

【C语言 | 数组】C语言数组详解(经典,超详细)

😁博客主页😁:🚀https://blog.csdn.net/wkd_007🚀 🤑博客内容🤑:🍭嵌入式开发、Linux、C语言、C、数据结构、音视频🍭 🤣本文内容🤣&a…

服务名无效。 请键入 NET HELPMSG 2185以获得更多的帮助

遇到的问题是MySQL服务没有。 因为net start 服务名,启动的是win下注册的服务。此时,我系统中并没有注册mysql到服务中。即下面没有mysql服务。 mysqld --install net start mysql

Linux_包管理_apt和apt-get、apt upgrade会自动升级内核

1、apt和apt-get 这篇文章说的很详细,【精选】一文搞清apt与apt-get的异同_apt和aptget-CSDN博客,来自于英语原文,Difference Between apt and apt-get Commands [Explained]。 简单来说,apt更容易使用(比如显示下载…

武汉凯迪正大—锂电池均衡维护仪

产品概况 KDZD885C 电池容量平衡测试系统,主要用于锂电池箱充放电测试及均衡维护,解决锂电池包单芯电压不均衡的痛点,用于快速解决锂电池电压不一致的难题,适用于各锂电池模组电压等级,集单芯放电,充电,均…

git服务器搭建

P1 kube-node2服务器 互联网已经存在的大型git服务器【版本控制服务器】 https://github.com【国外网站,速度慢】 https://about. gitlub.com/【国外网站,速度慢】 https://gitee.com【国内阿里的git网站,可以注册使用】 环境准备,使用之前的kube-node2主机&…

zabbix中监控数据,报错返回给钉钉查看

### 在钉钉中创建群聊(同组的同学创建一个群聊),在群里面添加自定义机器人 1.通过自定义webhook接入自定义服务 webhook:记住webhook的地址 安全设置:设置加签,只有信息内容包含签才会被机器人发送。 2.配置钉钉告警脚本 #### **安…

从零搭建微服务架构:Spring Boot与Nacos完美整合

🎏:你只管努力,剩下的交给时间 🏠 :小破站 从零搭建微服务架构:Spring Boot与Nacos完美整合 前言第一:服务注册与发现第二:配置中心第三:报错问题解决第四:什…

Solidity案例详解(四)投票智能合约

该合约为原创合约,功能要求如下 在⼀定时间能进⾏投票超过时间投票截⽌,并投赞同票超过50%则为通过。 使⽤safeMath库,使⽤Owner 第三⽅库拥有参与投票权的⽤户在创建合约时确定Voter 结构 要有时间戳、投票是否同意等;struct 结构…

MATLAB中uiresume函数用法

目录 语法 说明 示例 按下按钮后恢复执行 使用函数调用恢复执行 uiresume函数的功能是恢复暂停程序的执行。 语法 uiresume uiresume(f) 说明 uiresume 恢复与当前图窗 (gcf) 关联的对应 uiwait 调用暂停的程序执行。 uiresume(f) 恢复与图窗 f 关联的对应 uiwait 调用…

PbootCMS 应用快速迁移至浪潮信息 KeyarchOS 云服务器

本文我们以 PbootCMS 应用为例,详细介绍如何使用 X2Keyarch 迁移工具将其从 CentOS 系统迁移到浪潮信息 KeyarchOS 系统。 背景介绍 众所周知,CentOS 是最流行的红帽克隆版,因为免费,所以它的安装量甚至比红帽本身要大得多。本来…

人工智能如何重塑体验为先的汽车行业

面向汽车行业用户体验的 AI 人工智能的影响力继续在各个主要行业中迅速蔓延,全球各地的公司都开始大力投资 AI 技术,以提高自身的竞争优势。未来的趋势表明,企业如果不立即采用人工智能战略,就可能会远远落后于竞争对手。 AI 和…