STM32 USB HOST HID

This is another tutorial in STM32 USB series, and today we will see How to use STM32 as a USB HOST to interface Human Interface Devices (HID). I will use both the Mouse and the Keyboard for this tutorial, and their results will be printed on the serial console.

Let’s start by setting up the cubeMX

CubeMX Setup

First of all we need to select the USB_OTG_FS in Host Only mode. Also make sure you activate the VBUS, as the USB devices mostly don’t have the Power supply, and that’s why VBUS will provide the required power to such devices.


Next, select the USB_HOST and select the class as HID CLASS. Leave everything here to default.


Setup the Uart so that we can see the data output on a serial console.


And now we need to enable the voltage supply to the VBUS pin. To do that you need to look at the user manual for your board. I am using STM32F4 discovery board, and it have the following diagram for the USB

As you can see above, the VBUS is powered from the PC0 pin. But PC0 is connected to the EN Pin, which is an active Low pin. This means in order to supply the voltage to the VBUS, we must Pull down the PC0 Pin, or basically Set it LOW.


The final PINOUT is as shown above

  • UART2 is connected to PC, so that we can see the output for the debugging purpose
  • PC0 is set as output, to enable the voltage to the VBUS
  • USB Pins are automatically selected, when you select the USB HOST


Some insight into the CODE

void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
	if(USBH_HID_GetDeviceType(phost) == HID_MOUSE)  // if the HID is Mouse
	{
		HID_MOUSE_Info_TypeDef *Mouse_Info;
		Mouse_Info = USBH_HID_GetMouseInfo(phost);  // Get the info
		int X_Val = Mouse_Info->x;  // get the x value
		int Y_Val = Mouse_Info->y;  // get the y value
		if (X_Val > 127) X_Val -= 255;
		if (Y_Val > 127) Y_Val -= 255;
		int len = sprintf (Uart_Buf, "X=%d, Y=%d, Button1=%d, Button2=%d, Button3=%d\n", X_Val, Y_Val, \
				                                Mouse_Info->buttons[0],Mouse_Info->buttons[1], Mouse_Info->buttons[2]);
		HAL_UART_Transmit(&huart2, (uint8_t *) Uart_Buf, len, 100);
	}

	if(USBH_HID_GetDeviceType(phost) == HID_KEYBOARD)  // if the HID is Mouse
	{
		uint8_t key;
		HID_KEYBD_Info_TypeDef *Keyboard_Info;
		Keyboard_Info = USBH_HID_GetKeybdInfo(phost);  // get the info
		key = USBH_HID_GetASCIICode(Keyboard_Info);  // get the key pressed
		int len = sprintf (Uart_Buf, "Key Pressed = %c\n", key);
		HAL_UART_Transmit(&huart2, (uint8_t *) Uart_Buf, len, 100);
	}
}

Whenever a HID Data event is detected, HID_EventCallback function will be called, and our entire program lies inside it

  • In case if the device is HID MOUSE, we will print it’s movement on the serial console. Along with the buttons we press.
  • If the device is HID keyboard, then the key pressed will be sent to the serial console


Result

Check out the Video Below




Info

You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.

Subscribe
Notify of

16 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
reza
1 year ago

Hi
thanks for your complete and transparent tutorial
i have a question
MX_USB_HOST_Process(); line didnt in your video. does is nessesary run in while?
does is take many TICKS to run?
Thanks a lot

hako
1 year ago

Hi admin!
I am communicating an HID standard device. Configure the STM32 usb hid host to communicate with the device that reads the data (the device requested to send the new cmd ={0x9b,0x01,0x1c} command for data output I tried the USBH_CtlSendData functions) and got no response. Looking forward to admin help

Alexandr
2 years ago

Perfect! Simple and clear explanation!

Everton Trento Junior
2 years ago

it cant connect to a stm32f407vet6 board. these board doesnt have any VBus protection as like the stm32f4 descovery board. how can i make it work?

Everton Trento Junior
Reply to  admin
2 years ago

the module that i use dont have the protection circuit, so its only connect to the PA9 pin as the Vbuss output? and how i do the cubeMX setup?

Josip
3 years ago

I tried USB keyboard which run good, but when I debug usb mouse, no response(when move mouse, program can’t break in callback)

Felipe de Jesús Duarte López
Reply to  Josip
3 years ago

Same Here
Felipe DaurtDuarte

Felipe de Jesús Duarte López
Reply to  Josip
3 years ago

I have 2 mouses one of them is working well, try with other one

janki
4 years ago

This code is not working with STM32F249ZITx (Nucleo-F429ZI). when I connect just OTG cable at USB port the PG6 pin is going low (In your case PC0). Except this pin all other pins are same. So there is no change in code.
Another thing is as and when I connect OTG cable, my another UART also stopped working.
Can you help me to solve the issue

janki
Reply to  admin
4 years ago

wired mouse and wired keyboard both tried

janki
Reply to  admin
4 years ago

I have submitted query with mail ID in “Contact US” Tab.

minorengineer
Reply to  janki
2 years ago

any solution please? I also have the same card and got stuck totally