首先硬件准备工作,需要你有两个USB-TypeC的数据线,一个用于下载使用,另一个用于USB的功能使用,下载使用的是板子的stlinkV3的芯片,和H503没有直接连在一起,所以不可能公用一个数据线,
然后软件工作,我使用的是Carnac.exe软件,这个软件可以显示和记录键盘按下的按键动作和组合键,非常好用直观。
然后最重要的就是配置USB的枚举了
配置系统时钟,SYS->TIM6
打开USB功能,打开中断
打开THREADX操作系统配置
配置ThreadX的USB库
编译生产代码ARM工程
使用keil打开工程
修改ux_device_keyboard.c代码
增加定义,并在.h里面进行声明
UX_SLAVE_CLASS_HID * hid_keyboard; //键盘设备句柄
修改USBD_HID_Keyboard_Activate()
VOID USBD_HID_Keyboard_Activate(VOID *hid_instance) { /* USER CODE BEGIN USBD_HID_Keyboard_Activate */ //UX_PARAMETER_NOT_USED(hid_instance); hid_keyboard = (UX_SLAVE_CLASS_HID*)hid_instance; //保存濿活的实例句柄 /* USER CODE END USBD_HID_Keyboard_Activate */ return; }
8.修改app_usbx_device.c代码:
1.增加头文件 /* USER CODE BEGIN Includes */ #include "usb.h" #include "paj7620.h" /* USER CODE END Includes */ 2.增加参数定义和函数声明 /* Private variables ---------------------------------------------------------*/ static ULONG hid_keyboard_interface_number; static ULONG hid_keyboard_configuration_number; static UX_SLAVE_CLASS_HID_PARAMETER hid_keyboard_parameter; static TX_THREAD ux_device_app_thread; /* USER CODE BEGIN PV */ static TX_THREAD ux_hid_read_thread; //添加线程的定乿 /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ static VOID app_ux_device_thread_entry(ULONG thread_input); /* USER CODE BEGIN PFP */ static VOID usbx_hidkeyboard_thread_entry(ULONG thread_input); //函数定义 VOID USBX_APP_Device_Init(VOID); /* USER CODE END PFP */ 3.在MX_USBX_Device_Init中增加任务 /* USER CODE BEGIN MX_USBX_Device_Init1 */ /* Allocate the stack for usbx hid thread */ //分配栈 if (tx_byte_allocate(byte_pool, (VOID **) &pointer, 1024, TX_NO_WAIT) != TX_SUCCESS) { return TX_POOL_ERROR; } /* Create the usbx hid thread */ if (tx_thread_create(&ux_hid_read_thread, "hid_acm_read_usbx_app_thread_entry", usbx_hidkeyboard_thread_entry, 1, pointer, 1024, 20, 20, TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { return TX_THREAD_ERROR; } /* USER CODE END MX_USBX_Device_Init1 */ 4.增加初始化任务 static VOID app_ux_device_thread_entry(ULONG thread_input) { /* USER CODE BEGIN app_ux_device_thread_entry */ //TX_PARAMETER_NOT_USED(thread_input); //初始化USB设备 USBX_APP_Device_Init(); /* USER CODE END app_ux_device_thread_entry */ } 5.USBX初始化 VOID USBX_APP_Device_Init(VOID) { /* USER CODE BEGIN USB_Device_Init_PreTreatment_0 */ /* USER CODE END USB_Device_Init_PreTreatment_0 */ /* initialize the device controller HAL driver */ MX_USB_PCD_Init(); /* USER CODE BEGIN USB_Device_Init_PreTreatment_1 */ HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x00, PCD_SNG_BUF, 0x14); HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x80, PCD_SNG_BUF, 0x54); HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x81, PCD_SNG_BUF, 0x94); HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x01, PCD_SNG_BUF, 0xD4); HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x82, PCD_SNG_BUF, 0x114); /* USER CODE END USB_Device_Init_PreTreatment_1 */ /* Initialize and link controller HAL driver */ ux_dcd_stm32_initialize((ULONG)USB_DRD_FS, (ULONG)&hpcd_USB_DRD_FS); /* Start the USB device */ HAL_PCD_Start(&hpcd_USB_DRD_FS); /* USER CODE BEGIN USB_Device_Init_PostTreatment */ /* USER CODE END USB_Device_Init_PostTreatment */ } 6.实现usbx hidkeyboard线程入口的函数 static VOID usbx_hidkeyboard_thread_entry(ULONG thread_input) { /* USER CODE BEGIN app_ux_device_thread_entry */ UX_SLAVE_DEVICE *device; UX_SLAVE_CLASS_HID_EVENT hid_event; TX_PARAMETER_NOT_USED(thread_input); device = &_ux_system_slave->ux_system_slave_device; ux_utility_memory_set(&hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT)); while(1) { hid_event.ux_device_class_hid_event_buffer[2] = 0x4B; ux_device_class_hid_event_set(hid_keyboard, &hid_event); HAL_Delay(100); HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); } /* USER CODE END app_ux_device_thread_entry */ }
9,编译下载,连接TypeC
在电脑设备管理器中,键盘,多出来一个设备,说明,USB功能初始化成功,因为测试一直发送0x4B,所以会一直会不停的有上翻的信号