一、环境准备
电脑系统:Windows 10 专业版 20H2
IDE:Keil v5.35、STM32CubeMX v6.5.0
测试硬件:正点原子阿波罗STM32H743
二、测试步骤
1、使用用例工程
配置STM32H743定时器功能-CSDN博客https://blog.csdn.net/horse_2007s/article/details/139175169
2、配置USB引脚
在“Connectivity”中,选择“USB_OTG_FS”,如图,即选择PA11与PA12对应的USB,且为FS类型
3、配置中间件HID从设备
在“Middleware”中,选择“USB_DEVICE”,设置“FS”的IP核为HID
设置通讯间隔
设备PID VID
4、设置USB时钟(48M倍数)
5、生成工程代码代码
完成初始化
6、修改USB描述符
在 usbd_custom_hid_if.c 中
/** Usb HID report descriptor. */
__ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_FS[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 report0x09,0x03, // USAGE ID - Vendor defined0x15,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 report0x09,0x04, // USAGE ID - Vendor defined0x15,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 */
};
7、修改通讯帧长度
usbd_customhid.h中
8、声明外部定义的USB句柄
extern USBD_HandleTypeDef hUsbDeviceFS;
uint8_t usbrx[64];
9、在接收事件中添加代码
static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)
{/* USER CODE BEGIN 6 */UNUSED(event_idx);UNUSED(state);//获取接收指针USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef *)(hUsbDeviceFS.pClassData);//将数据copy到接收缓存for(int i=0;i<64;i++) {usbrx[i]=hhid->Report_buf[i]; } //处理,或者在其他地方处理USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS,usbrx,64); /* Start next USB packet transfer once data processing is completed */if (USBD_CUSTOM_HID_ReceivePacket(&hUsbDeviceFS) != (uint8_t)USBD_OK){return -1;}return (USBD_OK);/* USER CODE END 6 */
}
10、编译运行
三、运行效果
USB Tree检测正常
调试助手通讯正常
四、工程资料下载
2-USBHID.rar (访问密码: 1666)https://url48.ctfile.com/f/33868548-1250138927-85bc77?p=1666