Library 34- STM32F4 as USB HID Device

With USB HID Device library, you can turn STM32F4 to be a keyboard, mouse or gamepad device. It also supports all three settings at the same time. Device is shown to computer as “Keyboard; Mouse; Game controller“. This library allows you to use 2 gamepads at the same time, one keyboard and one mouse.

For mouse you can use left, middle and right buttons, X and Y cursor axes and wheel vertical rotation.

For gamepads, you can implement 16 buttons and 2 joysticks, like on PS2 gamepad device.

Also, if you use keyboard, you have enabled all special keys (SHIFT, CTRL, ALT and GUI), in combination with other keys.

You can of cource make a device, which implement everything listed here in one piece. Your imagination is the limit.

Library

Features

  • Interface STM32F4 with computer as keyboard, mouse or gamepads
  • Supports up to 2 gamepads, 1 keyboard and 1 mouse
  • Keyboard supports all special keys
  • Mouse supports 3 main buttons, cursor movement and wheel vertical rotation
  • Each of 2 gamepads supports up to 16 buttons and 2 joysticks
  • Library works in USB FS or USB HS in FS mode
  • Just plug USB in computer and works. No need for drivers

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx EXTI
    • misc.h
  • TM
    • defines.h
  • USB HID Device stack provided by STMicroelectronics (included in library)
USB FS MODE HS IN FS MODE DESCRIPTION
Data + PA12 PB15 USB Data+ line
Data – PA11 PB14 USB Data- line
ID PA10 PB12 USB ID pin
VBUS PA9 PB13 USB activate

By default, USB FS mode is used, also used on STM32F4-Discovery board. If you want to enable USB HS in FS mode for STM32F429 Discovery board, open project’s defines.h file and add lines below:

That’s all. You are now ready to work as USB HID device.

Clock was set down to 168MHz for STM32F429 because you can not get 48MHz for USB from 180MHz core clock.

Description

USB HID Device library is configured to support keyboard, mouse and 2 gamepads at the same time. You can send HID report for for mouse and keyboard one by one at the same time. Also, this library allows you to create a device to support 2 gamepads and create a console where you and your friend can play games with one STM32F4 device.

You have to know that when you send a report when some button is pressed, computer will detect button pressed. But after you release the button or everything, you have to also send HID report that you have released button. In one word: You have to send HID report every time each button is changed (pressed or released) or joysticks are moved.

Everywhere with joystick and movement (mouse, gamepad) where you have X and Y axis, you send each time a relative movement. So if you send one time cursor movement, and if you next time send the same value, cursor will be moved for twice length. You always sends relative movement according to position right now. There is no absolute movement for cursor.

Below, you have structure on how reports looks for USB HID report. Everywhere you have buttons, you have to set bit to 1 when button is pressed or 0 when button is released.

Also, keyboard buttons ARE NOT ascii values. To get proper HEX value for pressed key, you should look here.

  1. Keyboard
    Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
    Byte 0 Report ID = 0x01
    Byte 1 Right GUI Right ALT Right SHIFT Right CTRL Left GUI Left ALT Left SHIFT Left CTRL
    Byte 2 Padding = Always 0x00
    BYTE 3 Key 1
    BYTE 4 Key 2
    BYTE 5 Key 3
    BYTE 6 Key 4
    BYTE 7 Key 5
    BYTE 8 Key 6
  2. Mouse
    Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
    Byte 0 Report ID = 0x02
    Byte 1 Middle button Right button Left button
    Byte 2 Cursor movement X axis
    BYTE 3 Cursor movement Y axis
    BYTE 4 Wheel vertical movement
  3. Gamepad 1
    Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
    Byte 0 Report ID = 0x03
    Byte 1 Button 8 Button 7 Button 6 Button 5 Button 4 Button 3 Button 2 Button 1
    Byte 1 Button 16 Button 15 Button 14 Button 13 Button 12 Button 11 Button 10 Button 9
    BYTE 3 Left joystick X axis
    BYTE 4 Left joystick Y axis
    BYTE 5 Right joystick X axis
    BYTE 6 Right joystick Y axis
  4. Gamepad 2
    Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
    Byte 0 Report ID = 0x04
    Byte 1 Button 8 Button 7 Button 6 Button 5 Button 4 Button 3 Button 2 Button 1
    Byte 1 Button 16 Button 15 Button 14 Button 13 Button 12 Button 11 Button 10 Button 9
    BYTE 3 Left joystick X axis
    BYTE 4 Left joystick Y axis
    BYTE 5 Right joystick X axis
    BYTE 6 Right joystick Y axis

Functions and enumerations

Example

This is one simple example. If you press the blue button, then “WIN+R” command will be send to computer. In picture below, new device is found under Devices and Printers section.

USB HID Device result

USB HID Device result


Sketches

This sketches are made for copy/paste inside project, where you have comments “Simple sketch start” and “Simple sketch end“.

Everything you can test, if you open Control panel and the Devices and printers. If you have properly set values you will see a new device. If you use sketch for gamepad simulation, then make a right click on new device and hit “Game Controller settings“. You will get a new windows when you have 2 devices with same name. First is gamepad 1 and second is gamepad 2. Select one and click “Properties“. You will get a new window, where you can look, which buttons are pressed when you press the button on discovery board.

  1. This sketch is used in main example. If you press the button od Discovery board, it will send command “WIN + R” and when you release the button, it will send command that all buttons are released.
  2. If you press the button on discovery board, then you simulate left mouse key. If you hold button, and move your mouse around, you will see option “Select“.
  3. Each time you press the button, you will write “Tilen” to the some file or somewhere else in your computer.
  4. Next example simulates 2 gamepads. If you press the button, you activate buttons 3, 10 on gamepad 1 and left joystick rotation. Also, if you set window for gamepad 2, you will activate button 5 and 13 with left and right joysticks.

  5. If you press the button, you will move file, or website, or everything used with mouse wheel for 10 units.

Project is available at my Github account, download library below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...