AT32 WorkBench常用功能14—USB Device CustomHID
USB CustomHID工程建立
本文主要介绍如何基于AT32 WorkBench配置USB CustomHID
硬件环境
- AT-START-F405
- USB线缆
软件环境
- AT32 Workbench
- MDK5
- USB HID Demo上位机软件
配置步骤:
- 打开AT32 WorkBench新建AT32F405RCT7工程
- 在外设中打开USB_OTGHS1并选择设备,视情况选择是否勾选相关信号,本案例不勾选
- 可以看到时钟配置菜单栏打了红叉 ,这是因为缺少12M外部晶振为OTGHS PHY提供时钟,所以需要在外设CRM中打开高速外部晶振。
- 在中间件USB_DEVICE中选择OTGHS1设备类为Custom hid,HS设备描述符和端点FIFO可根据需要修改,本案例使用默认数值。
- 点击生成代码,然后生成MDK_V5工程即可完成CustomHID基础工程的建立
- 使用MDK_V5打开项目,对wk_usb_app_task()函数做如下修改,编译并下载到开发板
void wk_usb_app_task(void) { /* add user code begin usb_app_task 0 */ /* add user code end usb_app_task 0 */ /* add user code begin usb_app_task 1 */ /* add user code end usb_app_task 1 */ /* hs device custom hid */ /* after the the usb connected, user can use the 'custom_hid_class_send_report' function to report keyboard events, for example, to report a char led on/off event as follows:
ALIGNED_HEAD static uint8_t report_buf[64] ALIGNED_TAIL; if(usbd_connect_state_get(&otg_core_struct_hs.dev) == USB_CONN_STATE_CONFIGURED) { report_buf[0] = HID_REPORT_ID_5; report_buf[1] = 0; custom_hid_class_send_report(&otg_core_struct_hs.dev, report_buf, 64); usb_delay_ms(100); report_buf[0] = HID_REPORT_ID_5; report_buf[1] = 1; custom_hid_class_send_report(&otg_core_struct_hs.dev, report_buf, 64); usb_delay_ms(100); } */ /* add user code begin usb_app_task 2 */ ALIGNED_HEAD static uint8_t report_buf[64] ALIGNED_TAIL; if(usbd_connect_state_get(&otg_core_struct_hs.dev) == USB_CONN_STATE_CONFIGURED) { report_buf[0] = HID_REPORT_ID_5; report_buf[1] = 0; custom_hid_class_send_report(&otg_core_struct_hs.dev, report_buf, 64); usb_delay_ms(100); report_buf[0] = HID_REPORT_ID_5; report_buf[1] = 1; custom_hid_class_send_report(&otg_core_struct_hs.dev, report_buf, 64); usb_delay_ms(100); } /* add user code end usb_app_task 2 */ } |
- 将开发板HS_Device口连接到PC并复位,打开USB HID Demo上位机软件,可以看到Button 1 Report ID 5所指示的灯泡图标在不停的闪烁,这是因为CustomHID设备在不停的给上位机软件发送指令。
注意:USB HID Demo上位机软件下载方法是:雅特力官网->产品讯息->任一MCU详情界面->USB Tool and Driver->USB HID Demo 上位机软件
注意:对于USB_OTGFS来说,步骤相似,但可以不开启12M高速外部时钟,且输入到OTGFS控制器的时钟必须是48MHz。若选择hick to usbfs则可以解锁外设AAC功能,可以在AT32 WorkBench时钟配置界面进行确认。