Library 31- USB HID Host for STM32F4

USB HID (or Human Input Device) is a library, that allows you to read mouse or keyboard data with your STM32F4xx device. So you can use keyboard to write characters to STM32F4xx or use mouse for LCD or any other stuff.

Library

Features

  • Interface with USB mouse
    • 3 buttons and movement supported
  • Interface with USB keyboard
    • QWERTY or AZERTY supported

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx EXTI
    • MISC
  • TM
    • defines.h
  • USB
    • USB HID Host 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

Technically, Data+ and Data- are enough for USB communication in any way. But, STM32F4- and STM32F429- Discovery boards uses ID and VBUS pins for activate USB communication (STMPS2151 chip for USB). If you are using STM32F4 or STM32F429 Discovery boards, you need at least VBUS pin. If you are working on custom application, you can disable ID and VBUS pins. To disable pins, open project’s defines.h file and add lines below:

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:

By default, AZERTY keyboard format is in use. If you will work with QWERTY format, add lines below in defines.h file:

That’s all. You are now ready to work with USB HID devices.

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

Functions and enumerations

Example

Example below works with USB mouse and USB keyboard:

  • If you connect USB mouse, RED led will be ON to indicate this
    • RED led will be always on.
    • If you move mouse, then GREEN led should BLINK
    • If you press left button, GREEN led will be ON
    • If you press right button, GREEN led will be OFF
  • If you connect USB keyboard, GREEN led will be ON to indicate this
    • If keyboard is inserted, then GREEN led will be ON
    • If you press lower b, RED led will be ON
    • If you press lower v, RED led will be OFF
  • If both leds blinks, then USB device is not recignized
  • If not leds on, then device is not inserted

Project is available on 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...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!