看视频,老师用了threadx和USB库,其实并没有怎么看懂,但是主打一个听话,跟着操作使用stm32cubemx配置
配置USB
添加threadx
添加USBX库
点点点,跟着老师和论坛的朋友们的脚步,直接生成,简单修改,齐活
主要的相关程序如下
/* USER CODE BEGIN 1 */ /** * @brief 实现usbx hidkeyboard线程入口的函数 * @param 线程输入:用户线程输入参数 * @retval none */ static VOID usbx_hidkeyboard_thread_entry(ULONG thread_input) { /* USER CODE BEGIN usb_app_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) { /* Check if the device state already configured */ if ((device->ux_slave_device_state == UX_DEVICE_CONFIGURED) && (hid_keyboard != UX_NULL)) { /* sleep for 10ms */ tx_thread_sleep(MS_TO_TICK(100)); hid_event.ux_device_class_hid_event_length = 8; hid_event.ux_device_class_hid_event_buffer[0] = 0; hid_event.ux_device_class_hid_event_buffer[1] = 0; uint16_t gesture_Data = getGesture(); usart_print_gesture(gesture_Data); // 串口打印 if (gesture_Data != 0) { switch (gesture_Data) { case PAJ_UP: hid_event.ux_device_class_hid_event_buffer[2] = 0x4B; break; // Keyboard PageUp case PAJ_DOWN: hid_event.ux_device_class_hid_event_buffer[2] = 0x4E; break; // Keyboard PageDown case PAJ_LEFT: hid_event.ux_device_class_hid_event_buffer[2] = 0x50; break; // Keyboard LeftArrow case PAJ_RIGHT: hid_event.ux_device_class_hid_event_buffer[2] = 0x4F; break; // Keyboard RightArrow case PAJ_FORWARD: hid_event.ux_device_class_hid_event_buffer[2] = 0x4A; break; // Keyboard Home case PAJ_BACKWARD: hid_event.ux_device_class_hid_event_buffer[2] = 0x4D; break; // Keyboard End case PAJ_CLOCKWISE: hid_event.ux_device_class_hid_event_buffer[2] = 0x3E; break; // Keyboard F5 case PAJ_COUNT_CLOCKWISE: hid_event.ux_device_class_hid_event_buffer[2] = 0x29; break; // Keyboard Esc case PAJ_WAVE: break; default: break; } ux_device_class_hid_event_set(hid_keyboard, &hid_event); } else { hid_event.ux_device_class_hid_event_buffer[2] = 0; ux_device_class_hid_event_set(hid_keyboard, &hid_event); } } else { /* sleep for 10ms */ tx_thread_sleep(MS_TO_TICK(10)); } HAL_Delay(100); // HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); } /* USER CODE END usb_app_thread_entry */ }