在 usbd_custom_hid_if.c 的 CUSTOM_HID_ReportDesc_HS 或 CUSTOM_HID_ReportDesc_FS
__ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_HS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END =
{
/* USER CODE BEGIN 1 */
0x05,0x8c, /* USAGE_PAGE (ST Page) */
0x09,0x01, /* USAGE (Demo Kit) */
0xa1,0x01, /* COLLECTION (Application) */
// The Input report
0x09,0x03, // USAGE ID - Vendor defined
0x15,0x00, // LOGICAL_MINIMUM (0)
0x26,0x00, 0xFF, // LOGICAL_MAXIMUM (255)
0x75,0x08, // REPORT_SIZE (8bit)
0x95,0x40, // REPORT_COUNT (64Byte)
0x81,0x02, // INPUT (Data,Var,Abs)
// The Output report
0x09,0x04, // USAGE ID - Vendor defined
0x15,0x00, // LOGICAL_MINIMUM (0)
0x26,0x00,0xFF, // LOGICAL_MAXIMUM (255)
0x75,0x08, // REPORT_SIZE (8bit)
0x95,0x40, // REPORT_COUNT (64Byte)
0x91,0x02, // OUTPUT (Data,Var,Abs)
/* USER CODE END 1 */
0xC0 /* END_COLLECTION */
};
在 usbd_desc.c 中修改
#define USBD_VID 123
#define USBD_LANGID_STRING 1033
#define USBD_MANUFACTURER_STRING "STM32"
#define USBD_PID_HS 33222
#define USBD_PRODUCT_STRING_HS "HID name"
#define USBD_CONFIGURATION_STRING_HS "Custom HID Config"
#define USBD_INTERFACE_STRING_HS "Custom HID Interface"
#define USB_SIZ_BOS_DESC 0x0C
在 usbd_customhid.h 中
#ifndef CUSTOM_HID_HS_BINTERVAL
#define CUSTOM_HID_HS_BINTERVAL 0x01U
#endif /* CUSTOM_HID_HS_BINTERVAL */
#ifndef CUSTOM_HID_FS_BINTERVAL
#define CUSTOM_HID_FS_BINTERVAL 0x01U
#endif /* CUSTOM_HID_FS_BINTERVAL */
在 usbd_conf.h 中
#define USBD_MAX_NUM_INTERFACES 1U
/*---------- -----------*/
#define USBD_MAX_NUM_CONFIGURATION 1U
/*---------- -----------*/
#define USBD_MAX_STR_DESC_SIZ 512U
/*---------- -----------*/
#define USBD_DEBUG_LEVEL 0U
/*---------- -----------*/
#define USBD_LPM_ENABLED 0U
/*---------- -----------*/
#define USBD_SELF_POWERED 1U
/*---------- -----------*/
#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 64U
/*---------- -----------*/
#define USBD_CUSTOM_HID_REPORT_DESC_SIZE 33U
/*---------- -----------*/
#define CUSTOM_HID_HS_BINTERVAL 0x1U
在 usbd_customhid.h 中
#ifndef USBD_CUSTOMHID_OUTREPORT_BUF_SIZE
#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 0x40U
#endif /* USBD_CUSTOMHID_OUTREPORT_BUF_SIZE */
#ifndef USBD_CUSTOM_HID_REPORT_DESC_SIZE
#define USBD_CUSTOM_HID_REPORT_DESC_SIZE 33U
#endif /* USBD_CUSTOM_HID_REPORT_DESC_SIZE */
HID报文每次收发必须为64字节
在 usbd_customhid.c 中定义的函数
uint8_t USBD_CUSTOM_HID_SendReport(USBD_HandleTypeDef *pdev,
uint8_t *report, uint16_t len);
extern USBD_HandleTypeDef hUsbDeviceHS;//声明外部定义的USB句柄
USBD_CUSTOM_HID_SendReport(&hUsbDeviceHS,usbtx,64);//将usbtx数组中的64字节数据发出去
在 usbd_custom_hid_if.c 中
static int8_t CUSTOM_HID_OutEvent_HS(uint8_t event_idx, uint8_t state)
{
/* USER CODE BEGIN 10 */
UNUSED(event_idx);
UNUSED(state);
//获取接收指针
USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef *)(hUsbDeviceHS.pClassData);
//将数据copy到接收缓存
for(int i=0;i<64;i++)
{
usbrx[i]=hhid->Report_buf[i];
}
//处理,或者在其他地方处理
processData();
/* Start next USB packet transfer once data processing is completed */
if (USBD_CUSTOM_HID_ReceivePacket(&hUsbDeviceHS) != (uint8_t)USBD_OK)
{
return -1;
}
return (USBD_OK);
/* USER CODE END 10 */
}