使用開發板為STM32F105開發板。 原本想將具體步驟給寫出來。時間有限。直接將STM32F105_USBH_HID.IOC上傳files.cnblogs.com/files/libra13179/STM32F105_USBH_HID.zip [需要註意開發板使用的外部晶振為25mhz] 下麵給出 ...
使用開發板為STM32F105開發板。
原本想將具體步驟給寫出來。時間有限。直接將STM32F105_USBH_HID.IOC上傳files.cnblogs.com/files/libra13179/STM32F105_USBH_HID.zip
[需要註意開發板使用的外部晶振為25mhz]
下麵給出主要修改地方
usbh_conf.c中修改
/** * @brief USBH_LL_DriverVBUS * Drive VBUS. * @param phost: Host handle * @param state : VBUS state * This parameter can be one of the these values: * 0 : VBUS Active * 1 : VBUS Inactive * @retval Status */ USBH_StatusTypeDef USBH_LL_DriverVBUS (USBH_HandleTypeDef *phost, uint8_t state) { /* USER CODE BEGIN 0 */ /* USER CODE END 0*/ if (phost->id == HOST_FS) { if (state == 0) { /* Drive high Charge pump */ /* ToDo: Add IOE driver control */ /* USER CODE BEGIN DRIVE_HIGH_CHARGE_FOR_FS */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET); /* USER CODE END DRIVE_HIGH_CHARGE_FOR_FS */ } else { /* Drive low Charge pump */ /* ToDo: Add IOE driver control */ /* USER CODE BEGIN DRIVE_LOW_CHARGE_FOR_FS */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET); /* USER CODE END DRIVE_LOW_CHARGE_FOR_FS */ } } HAL_Delay(200); return USBH_OK; }
在 usbh_conf.H修改使用rtt來列印信息
/* DEBUG macros */ #ifdef RTT_LOG_ENABLED #include "rtt_log.h" #if (USBH_DEBUG_LEVEL > 0) #define USBH_UsrLog logi #else #define USBH_UsrLog(...) #endif #if (USBH_DEBUG_LEVEL > 1) #define USBH_ErrLog logw #else #define USBH_ErrLog(...) #endif #if (USBH_DEBUG_LEVEL > 2) #define USBH_DbgLog loge #else #define USBH_DbgLog(...) #endif #else #if (USBH_DEBUG_LEVEL > 0) #define USBH_UsrLog(...) printf(__VA_ARGS__);\ printf("\n"); #else #define USBH_UsrLog(...) #endif #if (USBH_DEBUG_LEVEL > 1) #define USBH_ErrLog(...) printf("ERROR: ") ;\ printf(__VA_ARGS__);\ printf("\n"); #else #define USBH_ErrLog(...) #endif #if (USBH_DEBUG_LEVEL > 2) #define USBH_DbgLog(...) printf("DEBUG : ") ;\ printf(__VA_ARGS__);\ printf("\n"); #else #define USBH_DbgLog(...) #endif #endif //RTT_LOG_ENABLED
好了現在下載測試
使用otg線將鍵盤和開發板連接
好了現在開始我們添加一下怎麼識別鍵盤的按鍵信息。
在usb_host.c中添加下麵代碼
/* USER CODE BEGIN 1 */ void USBH_HID_EventCallback(USBH_HandleTypeDef *phost) { HID_TypeTypeDef type = HID_UNKNOWN; USBH_ErrLog("USBH_HID_EventCallback"); type = USBH_HID_GetDeviceType(phost); switch(type) { case HID_KEYBOARD: { HID_KEYBD_Info_TypeDef *keyboard_info; char ascii; keyboard_info = USBH_HID_GetKeybdInfo(phost); if( keyboard_info != NULL ) { ascii = USBH_HID_GetASCIICode(keyboard_info); if( ascii != 0 ) { USBH_UsrLog("%c",ascii); } } } break; case HID_MOUSE: { //HID_MOUSE_Info_TypeDef *USBH_HID_GetMouseInfo(USBH_HandleTypeDef *phost) } break; default: break; } } /* USER CODE END 1 */
測試一下
現在測試滑鼠
先在usb_host.c中添加下麵代碼
/* USER CODE BEGIN 1 */ void USBH_HID_EventCallback(USBH_HandleTypeDef *phost) { HID_TypeTypeDef type = HID_UNKNOWN; USBH_ErrLog("USBH_HID_EventCallback"); type = USBH_HID_GetDeviceType(phost); switch(type) { case HID_KEYBOARD: { HID_KEYBD_Info_TypeDef *keyboard_info; char ascii; keyboard_info = USBH_HID_GetKeybdInfo(phost); if( keyboard_info != NULL ) { ascii = USBH_HID_GetASCIICode(keyboard_info); if( ascii != 0 ) { USBH_UsrLog("%c",ascii); } } } break; case HID_MOUSE: { HID_MOUSE_Info_TypeDef *mouse_info; mouse_info = USBH_HID_GetMouseInfo(phost); USBH_UsrLog("mouse_info X%d, Y%d ,button %d %d %d",mouse_info->x,mouse_info->y,mouse_info->buttons[2],mouse_info->buttons[1],mouse_info->buttons[0]); } break; default: break; } } /* USER CODE END 1 */