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.rhkb.cn/news/197735.html

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

相关文章

SpringBoot中日志的使用log4j

SpringBoot中日志的使用log4j 项目中日志系统是必不可少的,目前比较流行的日志框架有 log4j、logback 等,这两个框架的作者是同一个 人,Logback 旨在作为流行的 log4j 项目的后续版本,从而恢复 log4j 离开的位置。 另外 slf4j(…

【React】React 基础

1. 搭建环境 npx create-react-app react-basic-demo2. 基本使用 JSX 中使用 {} 识别 JavaScript 中的表达式,比如变量、函数调用、方法调用等。 if、switch、变量声明等属于语句,不是表达式。 列表渲染使用 map 。 事件绑定用;on 事件名称…

Azure Machine Learning - Azure AI 搜索中的集成数据分块和嵌入

在基于索引器的索引编制中,Azure AI _集成矢量化_将数据分块和文本到矢量嵌入添加到技能中,它还为查询添加文本到矢量的转换。 关注TechLead,分享AI全维度知识。作者拥有10年互联网服务架构、AI产品研发经验、团队管理经验,同济本…

webstorm/idea配置leetcode刷题

File -> settings -> Plugins -> 搜索leetcode 安装插件(截图显示我已经安装过了),安装完成后点击OK操作,在编辑器四个边角就会出现一个leetcode的插件 File -> settings -> Tools-> Leetcode plugin 点击…

[C/C++]数据结构 栈和队列()

一:栈 1.1 栈的概念及结构 栈是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作,进行数据插入和删除操作的一端称为栈顶,另一端称为栈底,栈中的数据元素遵守先进后出的原则. 压栈:栈的插入操作叫做进栈/压栈/入栈,将数据插入栈顶 出栈:栈的删除操作也叫出…

正版软件|Soundop 专业音频编辑器,实现无缝的音频制作工作流程

关于Soundop Soundop 音频编辑器 直观而专业的音频编辑软件,用于录制、编辑、混合和掌握音频内容。 Soundop 是一款适用于 Windows 的专业音频编辑器,可在具有高级功能的直观灵活的工作区中录制、编辑和掌握音频并混音轨道。音频文件编辑器支持波形和频谱…

Unity 场景烘培 ——unity Post-Processing后处理1(四)

提示:文章有错误的地方,还望诸位大神不吝指教! 文章目录 前言一、Post-Processing是什么?二、安装使用Post-Processing1.安装Post-Processing2.使用Post-Processing(1).添加Post-process Volume&#xff08…

车载通信架构 —— 新车载总线类型下(以太网)的通信架构

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你的不…

java拼图游戏(待优化)

启动类 package com.yx.ui;public class App { //启动入口public static void main(String[] args) {//如果想要开启一个界面,就创建谁的对象 // new DengJFrame(); // new ZCJFrame();new GameJFrame();}}游戏类 package com.yx.ui;import java.awt.event.KeyEv…

嵌入式 Linux 移植与系统启动方法

1、Linux系统启动与U-Boot 所谓移植就是把程序代码从一种运行环境转移到另一种运行环境。对于内核移植来说,主要是从一种硬件平台转移到另一种硬件平台上运行。 体系结构级别的移植是指在不同体系结构平台上Linux内核的移植,例如,在ARM、MI…

Evil靶场

Evil 1.主机发现 使用命令探测存活主机,80.139是kali的地址,所以靶机地址就是80.134 fping -gaq 192.168.80.0/242.端口扫描 开放80,22端口 nmap -Pn -sV -p- -A 192.168.80.1343.信息收集 访问web界面 路径扫描 gobuster dir -u http…

ForkLift:macOS文件管理器/FTP客户端

ForkLift 是一款macOS下双窗口的文件管理器,可以代替本地的访达。ForkLift同时具备连接Ftp、SFtp、WebDav以及云服务器。 ForkLift还具备访达不具备的小功能,比如从文件夹位置打开终端,显示隐藏文件,制作替换等功能。ForkLift 是一…

解决k8s node节点报错: Failed to watch *v1.Secret: unknown

现象: 这个现象是发生在k8s集群证书过期,重新续签证书以后。 记得master节点的/etc/kubernetes/kubelet.conf文件已经复制到node节点了。 但是为什么还是报这个错,然后运行证书检查命令看一下: 看样子是差/etc/kubernetes/pki/…

八股文-TCP的四次挥手

TCP(Transmission Control Protocol)是一种面向连接的、可靠的传输协议,它的连接的建立和关闭过程都是经过精心设计的。在TCP连接关闭时,使用四次挥手来保证数据的完整传输和连接的正常终止。 漫画TCP的四次挥手 第一次挥手&#…

redis安装(Windows和linux)

如何实现Redis安装与使用的详细教程 Redis 简介 Redis是一个使用C语言编写的开源、高性能、非关系型的键值对存储数据库。它支持多种数据结构,包括字符串、列表、集合、有序集合、哈希表等。Redis的内存操作能力极强,其读写性能非常优秀,且…

PyCharm:PyCharm新建.py文件时自动带出指定内容

在pycharm中加上指定内容,每次新建.py文件都会自动带出指定内容 操作: File—Setting—Editor----File and Code Templates--Python Script 在右侧窗口中加上如下信息 # encoding: utf-8 # author: Jeffrey # file: ${NAME}.py # time: ${DATE} ${TI…

ControlNet原理及应用

《Adding Conditional Control to Text-to-Image Diffusion Models》 目录 1.背景介绍 2.原理详解 2.1 Controlnet 2.2 用于Stable Diffusion的ControlNet 2.3 训练 2.4 推理 3.实验结果 3.1 定性结果 3.2 消融实验 3.3 和之前结果比较 3.4 数据集大小的影响 4.结…

聚观早报 |联想集团Q2财季业绩;小鹏汽车Q3营收

【聚观365】11月17日消息 联想集团Q2财季业绩 小鹏汽车Q3营收 微软发布两款自研AI芯片 FAA批准SpaceX再次发射星际飞船 2023 OPPO开发者大会 联想集团Q2财季业绩 全球数字经济领导企业联想集团公布截至2023年9月30日的2023/24财年第二财季业绩:整体营收达到10…

【ARM Trace32(劳特巴赫) 使用介绍 2.2 -- TRACE32 进阶命令之 DIAG 弹框命令】

请阅读【ARM Coresight SoC-400/SoC-600 专栏导读】 上篇文章:【ARM Trace32(劳特巴赫) 使用介绍 2.1 – TRACE32 Practice 脚本 cmm 脚本学习】 下篇文章:【ARM Trace32(劳特巴赫) 使用介绍 3 - trace32 访问运行时的内存】 文章目录 DIALOG.OK 命令DIA…

react之基于@reduxjs/toolkit使用react-redux

react之基于reduxjs/toolkit使用react-redux 一、配置基础环境二、使用React Toolkit 创建 counterStore三、为React注入store四、React组件使用store中的数据五、实现效果六、提交action传递参数七、异步状态操作 一、配置基础环境 1.使用cra快速创建一个react项目 npx crea…