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

news2024/11/26 7:37:30

最近需要做一个小工具,要用到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];
 }

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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

BIO、NIO、AIO三者的区别及其应用场景(结合生活例子,简单易懂)

再解释三者之前我们需要先了解几个概念: 阻塞、非阻塞:是相较于线程来说的,如果是阻塞则线程无法往下执行,不阻塞,则线程可以继续往下 执行。同步、异步:是相较于IO来说的,同步需要等待IO操作完…

HTTP1.1协议详解

目录 协议介绍协议的特点存在的问题协议优化方案与HTTP 1.0协议的区别 协议介绍 HTTP 1.1是一种基于文本的互联网实体信息交互协议,是Web上任何数据交换和客户端-服务器交互的基础。它允许获取各种类型的资源,如HTML文档,并支持在互联网上交…

CocosCreator3.8神秘面纱 CocosCreator 项目结构说明及编辑器的简单使用

我们通过Dashboard 创建一个2d项目,来演示CocosCreator 的项目结构。 等待创建完成后,会得到以下项目工程: 一、assets文件夹 assets文件夹:为资源目录,用来存储所有的本地资源,如各种图片,脚本…

零小时零信任:数据标记如何加速实施

现在是零信任的零小时。 虽然这个概念已经存在多年,但现在联邦政府实施它的时间已经紧迫。 拜登政府备忘录被誉为以战斗速度安全交付关键任务数据的解决方案,要求联邦机构在 2024 财年年底前实现具体的零信任安全目标。 此外,国防部正在努…

从0开始学习JavaScript--JavaScript DOM操作与事件处理

在前端开发中,DOM(文档对象模型)是一个至关重要的概念,它为JavaScript提供了一种与HTML和XML文档交互的方法。本文将深入探讨DOM的概念与作用,以及JavaScript与DOM之间的密切关系。 DOM的概念与作用 DOM是什么&#…

【接口自动化测试】Postman(一) 介绍和安装

一.Postman介绍 Postman是一款非常流行的接口调试工具,它使用简单,而且功能也很强大。不仅测试人员会使用,开发人员也会 经常使用。 主要特点 1. 简单易用的图形用户界面 2. 可以保存接口请求的历史记录 3. 使用测试集Collections可以更…

编程的简单实例,编程零基础入门教程,中文编程开发语言工具下载

编程的简单实例,编程零基础入门教程,中文编程开发语言工具下载 给大家分享一款中文编程工具,零基础轻松学编程,不需英语基础,编程工具可下载。 这款工具不但可以连接部分硬件,而且可以开发大型的软件&…

websocket学习笔记【springboot+websocket聊天室demo】

文章目录 WebSocket是什么?为什么需要WebSocket?WebSocket和Http连接的区别WebSocket的工作原理基本交互过程: Java中的WebSocket支持WebSocket的优势springboot websocket themlef 一个聊天室demopom.xmlWebSocketConfigChatControllerWebController…

__builtin_expect(x,0)

As opposed to the C code, above we can see bar case precedes foo case. Since foo case is unlikely, and instructions of bar are pushed to the pipeline, thrashing the pipeline is unlikely. This is a good exploitation of a modern CPU

RK3588平台开发系列讲解(项目篇)嵌入式AI的学习步骤

文章目录 一、嵌入式AI的学习步骤1.1、入门Linux1.2、入门AI 二、瑞芯微嵌入式AI2.1、瑞芯微的嵌入式AI关键词2.2、AI模型部署流程 沉淀、分享、成长,让自己和他人都能有所收获!😄 📢 本篇将给大家介绍什么是嵌入式AI。 一、嵌入…

debian 修改镜像源为阿里云【详细步骤】

文章目录 修改步骤第 1 步:安装 vim 软件第 2 步:备份源第 3 步:修改为阿里云镜像参考👉 背景:在 Docker 中安装了 jenkins 容器。查看系统,发现是 debian 11(bullseye)。 👉 目标:修改 debian bullseye 的镜像为阿里云镜像,加速软件安装。 修改步骤 第 1 步:…

Debian/Ubuntu 安装 NodeJS【详细步骤】

文章目录 NodeSource 简介Debian/Ubuntu 安装 NodeJS第 1 步:进入 jenkins 容器第 2 步:下载和导入 NodeSource第 3 步:创建 deb 仓库第 4 步:安装 NodeJS第 5 步:卸载NodeJS参考👉 背景:在 Docker 中安装了 Jenkins,Jenkins 镜像为 Debian 11 bullseye。 👉 目标:…

【工具流】WSL2安装

一些废话 最近看到了PKU出品的cs自学指南,想要跟着里面的自学路径学国外的优质课程,无奈大多数pre教程里面都是直接Linux环境下的操作,并且我在CSwiki看到了那个熟悉的上学期学了一点的missing-semester课。 上学期自学missing-semester的时候…

【Ubuntu】Windows访问Ubuntu时“需要认证”界面卡住

情况描述 基本情况 本地电脑:Microsoft Windows [版本 10.0.19045.3570] 远程电脑:Ubuntu 20.04.6 LTS 远程电脑安装辅助远程工具:xrdp 0.9.12 问题描述:认证页面输入密码,点击认证以后认证按钮不可点击,无…

MR外包团队:MR、XR混合现实技术应用于游戏、培训,心理咨询、教育成为一种创新的各行业MR、XR形式!

随着VR、AR、XR、MR混合现实等技术逐渐应用于游戏开发、心理咨询、培训、教育各个领域,为教育、培训、心理咨询等行业带来了全新的可能性。MR、XR游戏开发、心理咨询是利用虚拟现实技术模拟真实场景,让学生身临其境地参与学习和体验,从而提高…

深入探索 Django Channels

概要 随着 Web 应用的发展,实时功能如即时消息、实时通知等变得越来越重要。Django Channels 是 Django 的一个扩展,它使得在 Django 中构建实时功能变得可能。本文将深入探讨 Django Channels 的核心概念、架构以及如何实现一个实时应用。 1. Django C…

VN5620以太网测试——DoIP配置

文章目录 前言一、DoIP简介二、Vector Hardware Configuration三、Diagnostics/ISO TP Configuration四、Diagnostic Console五、添加Ethernet Packet Builder前言 CANoe(CAN open environment)VN5620 :是一个紧凑而强大的接口,用于以太网网络的分析、仿真、测试和验证。 V…

uniapp 实现微信小程序手机号一键登录

app 和 h5 手机号一键登录&#xff0c;参考文档&#xff1a;uni-app官网 以下是uniapp 实现微信小程序手机号一键登录 1、布局 <template><view class"mainContent"><image class"closeImg" click"onCloseClick"src"quic…

LeetCode(17)罗马数字转整数【数组/字符串】【简单】

目录 1.题目2.答案3.提交结果截图 链接&#xff1a; 13. 罗马数字转整数 1.题目 罗马数字包含以下七种字符: I&#xff0c; V&#xff0c; X&#xff0c; L&#xff0c;C&#xff0c;D 和 M。 字符 数值 I 1 V 5 X 10 L …

LeetCode【560】和为k的子数组

题目&#xff1a; 思路&#xff1a; 转化为前缀和问题&#xff0c;和为k&#xff0c;即为&#xff1a;前缀和差值为k的情况统计&#xff1b; 为什么要转化为前缀和呢&#xff1f;因为和为k的子数组可能有n个元素&#xff0c;但是前缀和差值为k&#xff0c;只有两个元素&#…