Use STM32CubeMX software to generate USB_HOST_HID to connect mouse and keyboard scanner

1. Test platform:
MCU: STM32F429IGT6
Tool: STM32CubeMX software
Compilation software: MDK

2. Configuration steps
(1). Open the STM32CubeMX software, create a new project file, and generate a serial port 1 routine with a freertos operating system. The routine for generating a serial port will not be introduced in detail here.
(2). Configure USB_OTG_HS. Since the USB pins on the circuit board are connected to PB14 and PB15, configure USB_OTG_HS here, configure Host Only for Internal FS Phy, and enable interrupts at the same time.
insert image description here
(3). Configure USB_HOST. Since the mouse and keyboard belong to USB HID devices, the Class For HS IP selects Human Interface Host Class (HID). The task stack in CMSIS_RTOS needs to be configured as 512. The default is 128, which will cause the program to enter HardFault.
insert image description here
(4). After the configuration is complete, click GENERATE CODE to generate the code, open the project file, and replace the USBH_UsrLog(…) macro definition with

#define USBH_UsrLog() do {
      
      
printf(“USBH_UsrLog:) ;
printf(VA_ARGS);
printf(”\n”);
} while (0)

insert image description here
(5). After the compilation is completed, burn it to the circuit board, insert the keyboard, and it can be enumerated to the keyboard
insert image description here
(6). Insert the mouse, and it can be enumerated to the mouse
insert image description here
(7). As you can see, no matter whether you operate the mouse or the keyboard, The serial port assistant will not have any information output, because the event callback function code of the mouse and keyboard has not been added yet. Add the following code in the usb_host.c file

/* 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 */

insert image description here
(8). After adding the code, compile and burn it into the circuit board, insert the mouse, and move the mouse to see that the coordinates are changing. (9). Insert the keyboard, click the button
insert image description here
on the keyboard, and you can see the value of the clicked keyboard.
insert image description here

Guess you like

Origin blog.csdn.net/qizhi321123/article/details/126389227