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

usb otg 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.


usb host setup

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


uart setup

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

usb vbus setup

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.


usb host pinout

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

when mouse moves down
when mouse moves left
keys pressed on keyboard

Check out the Video Below










Info

You can help with the development by DONATING
To download the code, click DOWNLOAD button and view the Ad. The project will download after the Ad is finished.

16 Comments. Leave new

  • 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

    Reply
  • 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

    Reply
  • Perfect! Simple and clear explanation!

    Reply
  • Everton Trento Junior
    May 6, 2022 12:23 AM

    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?

    Reply
    • They all have the VBUS. Check schematics, I guess it’s PA9

      Reply
      • Everton Trento Junior
        May 6, 2022 5:55 PM

        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?

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

    Reply
  • 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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

keyboard_arrow_up

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add controllerstech.com to your ad blocking whitelist or disable your adblocking software.

×