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 builderSize 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_HSUSBD_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_HSbreak;
@@ -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_HSUSBD_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_HSUSBD_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_HSUSBD_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_HSUSBD_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_HSUSBD_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_HSUSBD_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_HSUSBD_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_HSUSBD_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_HSUSBD_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_HSUSBD_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, __IOpIadDesc->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_HSpCmpstHSConfDesc = 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 structextern 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_COMPOSITEstatic 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 *recase 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.mzph.cn/news/143748.shtml

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

相关文章

Feature Pyramid Networks for Object Detection(2017.4)

文章目录 Abstract1. Introduction3. Feature Pyramid NetworksBottom-up pathwayTop-down pathway and lateral connections 7. Conclusion FPN Abstract 特征金字塔是识别系统中检测不同尺度物体的基本组成部分。但最近的深度学习对象检测器避免了金字塔表示,部分…

VS Code画流程图:draw.io插件

文章目录 简介快捷键 简介 Draw.io是著名的流程图绘制软件,开源免费,对标Visio,用过的都说好。而且除了提供常规的桌面软件之外,直接访问draw.io就可以在线使用,堪称百分之百跨平台,便捷性直接拉满。 那么…

重生之我是一名程序员 31

大家晚上好!前面给大家分享了指针与数组的知识,所以今天要给大家分享的知识是——指针数组 相信大家在这里都会有疑问,指针数组是指针还是数组? 在这我们可以类⽐⼀下其他类型的数组,比如整型数组是存放整型的数组&am…

python科研绘图:绘制X-bar图

目录 1.X-bar 图的基本概念 2.X-bar 图的绘制过程 3.X-bar 图的优势 4.X-bar 图的绘制 1.X-bar 图的基本概念 X-bar控制图是一种统计工具,用于监控和控制生产过程中的质量变量。它是过程能力分析和统计过程控制(SPC,Statistical Process…

SystemVerilog学习 (5)——接口

一、概述 验证一个设计需要经过几个步骤: 生成输入激励捕获输出响应决定对错和衡量进度 但是,我们首先需要一个合适的测试平台,并将它连接到设计上。 测试平台包裹着设计,发送激励并且捕获设计的输出。测试平台组成了设计周围的“真实世界”,…

Python---数据序列中的公共方法

公共方法就是 支持大部分 数据 序列。 常见公共方法---简单 运算符描述支持的容器类型合并字符串、列表、元组*复制字符串、列表、元组in元素是否存在字符串、列表、元组、字典not in元素是否不存在字符串、列表、元组、字典 案例: 合并 代码: # …

【Nginx】nginx | 微信小程序验证域名配置

【Nginx】nginx | 微信小程序验证域名配置 一、说明二、域名管理 一、说明 小程序需要添加头条的功能,内容涉及到富文本内容显示图片资源存储在minio中,域名访问。微信小程序需要验证才能显示。 二、域名管理 服务器是阿里云,用的宝塔管理…

【探索Linux】—— 强大的命令行工具 P.15(进程间通信 —— system V共享内存)

阅读导航 引言一、system V的概念二、共享内存(1) 概念(2) 共享内存示意图(3) 共享内存数据结构 三、共享内存的使用1. 共享内存的使用步骤(1)包含头文件(2)获取键值(ftok函数)(3)创…

计算机视觉基础(7)——相机基础

前言 从这一节开始,我们来学习几何视觉。中层视觉包括相机模型、单目几何视觉、对极几何视觉和多目立体视觉等。在学习几何视觉最开始,我们先来学习一下相机模型,了解相机的基本原理,了解相机如何记录影像。 一、数字相机 1.1 基…

nodejs+vue黄河风景线旅游网站的设计与实现-微信小程序-安卓-python-PHP-计算机毕业设计

本文首先对该系统进行了详细地描述,然后对该系统进行了详细的描述。管理人员增加了系统首页、个人中心、用户管理、景点分类管理、景点简介管理、旅游路线管理、文章分类管理、公告文章管理、系统管理理等功能。这套黄河风景线旅游网站是根据当前的现实需要&#xf…

如何在 Nginx Proxy Manager(NPM)上部署静态网站

前言 众所周知,我们在之前介绍过 Nginx Proxy Manager(以下简称 NPM) 这个反向代理的神器,对于一些 Docker 搭建的 Web 项目,NPM 能够很轻松地给他们做反向代理。 然而对于一些静态网站,小伙伴们可能不知道怎么用 NP…

Unity反编译:IL2CPP 打包输出的cpp文件和dll(程序集)位置、Mono打包输出的dll(程序集)位置

目录 如题:IL2CPP 打包输出的cpp文件和dll位置(并不会出现在APK里) 如题:Mono打包输出的dll位置 校验平台:Android 如题:IL2CPP 打包输出的cpp文件和dll位置(并不会出现在APK里) Unity Assets同级目录下 Temp/StagingArea/Il2…

Vue CLI脚手架安装、搭建、配置 和 CLI项目分析

目录 一、CLI快速入门 1. 官方介绍 : 2.安装Vue CLI : 3.搭建Vue CLI : 4.IDEA配置Vue CLI : 二、Vue CLI项目分析 1.结构分析 : 1.1 config 1.2 node_modules 1.3 src 1.4 static 2.流程分析 : 2.1 main.js 2.2 router/index.js 2.3 components/HelloWorld.vue 2.4 A…

《QT从基础到进阶·二十七》进度条QProgressBar

ui.ProgressBar.setValue(45); //45% ui.ProgressBar.setMin(0); ui.ProgressBar.setMax(255);0到100分为255份,值为215时,进度条为100/255*215 84% 点击主界面弹出进度条QProgressDialog 常用功能: setWindowFlags(Qt::Dialog | Qt::Cu…

【C++】泛型编程 ② ( 函数模板与普通函数区别 )

文章目录 一、函数模板与普通函数区别1、函数模板与普通函数区别2、代码示例 - 函数模板与普通函数区别 一、函数模板与普通函数区别 1、函数模板与普通函数区别 函数模板与普通函数区别 : 主要区别在于它们能够处理的 数据类型数量 和 灵活性 ; 自动类型转换 : 函数模板 不允许…

嵌入式Linux开发,NFS文件系统挂载

在嵌入式linix的开发中,经常会需要在pc端和板端互相传输文件,优先可选择ftp传输,但是有些嵌入式板端不支持,只能使用nfs这种方式,即pc端作为服务端,板端作为客户端,将pc端的某个文件夹挂载到板端…

Redis 连接不上 WRONGPASS invalid username-password pair

1.我的RedisDesktopManager 可以连接 但是 Springboot远程使用Redis就是连不上 2.我的密码是 abc123.. 多了英文的 ..符号 在Springboot过不了,所以Redis密码尽量字母数字,不要其他符号

炒现货黄金怎么做?挖掘黄金的投资机会

黄金一直以来都是备受投资者追捧的避险资产,其价值和潜力是无法忽视的。而炒现货黄金作为一种快速获取收益的投资方式,备受关注。那么,如何在炒现货黄金中找到投资机会呢?为您详细解析,简单易懂,帮助您开启…

032-从零搭建微服务-定时服务(一)

写在最前 如果这个项目让你有所收获,记得 Star 关注哦,这对我是非常不错的鼓励与支持。 源码地址(后端):mingyue: 🎉 基于 Spring Boot、Spring Cloud & Alibaba 的分布式微服务架构基础服务中心 源…

自然语言处理(NLP)-spacy简介以及安装指南(语言库zh_core_web_sm)

spacy 简介 spacy 是 Python 自然语言处理软件包,可以对自然语言文本做词性分析、命名实体识别、依赖关系刻画,以及词嵌入向量的计算和可视化等。 1.安装 spacy 使用 “pip install spacy" 报错, 或者安装完 spacy,无法正…