一、开发环境
硬件平台:stm32f767
软件平台:keil5,cubmx
二、cubemx创建工程
1、配置RCC
2、USB_OTG_FS,选择Device_Only模式
3、Middleware——USB_DEVICE
4、配置一个外部中断按键作为输入
5、配置时钟与生成代码
三、工程代码修改
1、打开usbd_custom_hid_if.c 文件,把CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] 数组中的描述符修改为
__ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END =
{
/* USER CODE BEGIN 0 */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x00, // USAGE (Undefined)
0xa1, 0x01, // COLLECTION (Application)
0x09, 0x00, // USAGE (Undefined)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0x09, 0x00, // USAGE (Undefined)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x91, 0x00, // OUTPUT (Data,Ary,Abs)
/* USER CODE END 0 */
0xC0 /* END_COLLECTION */
};
修改宏USBD_CUSTOM_HID_REPORT_DESC_SIZE的长度为29
2、打开此函数的注释
int8_t USBD_CUSTOM_HID_SendReport_FS(uint8_t *report, uint16_t len)
{
return USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, report, len);
}
3、在按键中断中发送字符
uint8_t send_data = 1;
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == KEY0_Pin)
{
USBD_CUSTOM_HID_SendReport_FS(&send_data, 1);
}
}
4、完整工程代码
https://download.csdn.net/download/huhuandk/11980721