HAL Library 21- Multi purpose USB library for STM32Fxxx

This USB library is designed in a ways that can easily be used with different modes. That’s why I’ve made a single library for everything what I support now and which support I will add in future.

The main purpose of library is to support USB FS and HS modes on STM32Fxxx devices at the same time, without any problems.

So I designed a library, which allows users to have all these possibilities:

  • Use both USB modes as hosts
  • Use both USB modes as devices
  • Use one USB as host and another as device and vice-versa
  • Use one USB as device and/or host at the same time (needs switching)

Library

Read more about new HAL libraries

Features

  • Use USB on STM32Fxxx devices
  • Supports USB FS and HS ports
    • HS mode can use external PHY or embedded phy featuring USB HS in FS mode
  • Supports both USB modes at the same time
  • Supports different HOST or DEVICE modes at a time
  • Supports changing USB mode (host/device) on the fly on single USB FS or HS mode
  • Built on ST’s stack for USB Host/Device
  • Predefined support for different STM32F4/7xx Discovery boards for easily use
  • Supports different USB classes for host mode
    • MSC host for operating with USB flash memory
    • HID for reading external keyboard and mouse
  • Supports different USB classes for device mode
    • CDC device for acting like Virtual COM Port (VCP) to your computer
      • Requires ST’s Virtual COM port drivers
    • MSC device for acting like SDCARD reader when SDCARD is connected to SDIO port
  • Supports one USB class on both USB FS or HS ports at a time if needed
    • Example MSC: You can connect 2 flash drives at a time to make copy/paste files between them
    • Example HID: Read keyboard and mouse at the same time on both USB ports

Dependencies

  • HAL
  • TM
    • STM32Fxxx HAL
    • defines.h
    • ST USB Host stack
    • ST USB Device stack
    • TM BUFFER

USB Common settings

Both USB types (Host or Device) have some settings in common. These are at least defines for library use and GPIO pinouts for USB. For USB settings, you will have to make some defines before you will be able to run any example. Settings which can be defined are included below.

USB pinout can be found in table below for some different STM32F4xx and STM32F7 boards.

USB Common functions

To see all functions, which are common to all USB based libraries, check API description.

USB Host

USB Host library is a main library, to support Host mode of USB. It allows you to use FS, HS or both modes at the same time, if needed.

For detailed description about HOST library, you should look at API description.

USB MSC Host Class

USB MSC Host class is designed for operating with USB Flash memory based devices. It can read/write to flash memory using FATFS module.

For detailed implementation for USB HOST MSC, check API description.

For detailed explanation on how to use USB with FATFS, check my FATFS library.

Examples

For USB MSC Host class, I’ve made 2 examples, which shows how to use MSC on single USB port or on dual ports.

  • Example 1: DUAL MSC Host
    • Shows, how to use MSC Host on both USB FS and HS ports at the same time.
  • Example 2: MSC HOST and HID HOST at the same time
    • Shows, how to use USB MSC HOST and USB HID HOST at the same time. One port is for HID, another for MSC

Examples are available on my Github repository.

USB HID Host Class

USB HID Host class is designed for operating with HID keyboard or HID mouse. It allows you to use FS, HS or both modes at the same time, if needed.

For detailed implementation for USB HOST HID, check API documentation.

Examples

For USB HID Host class, I’ve made 4 examples, which shows how to use HID in different configurations

  • Example 1: DUAL HID Host
    • Shows how you can use HID Host on both USB ports, FS and HS at the same time. This allows you for example connecting mouse and keyboard at the same time if needed
  • Example 2: MSC HOST and HID HOST at the same time
    • Shows, how to use USB MSC HOST and USB HID HOST at the same time. One port is for HID, another for MSC
  • Example 3: HID Host and CDC Device
    • Example shows how you can have one USB port for CDC device mode and another for HID Host mode. This allows you to connect your board to computer and still reading keyboard/mouse on another USB port
  • Example 4: HID Host and CDC Device with dynamic change
    • Example shows how you can have single USB port (HS is used in example) for multi purposes. With button press, you can change active USB mode on the fly when needed

Examples are available on my Github repository.

USB Device

USB Device library is main library for USB Device support for STM32Fxxx device.

For detailed explanation about USB Device library, you should look API documentation.

USB CDC Device Class

USB CDC device class is mainly used for communication between computer and STM32Fxxx device over Virtual COM Port (VCP).

For detailed implementation for USB DEVICE CDC, check API documentation.

Examples

For USB CDC Device, I’ve made 4 examples, which shows how to use CDC in different configurations.

  • Example 1: Basic CDC example
    • Shows how to use CDC in simple configuration for first time
  • Example 2: HID Host and CDC Device with dynamic change
    • Example shows how you can have single USB port (HS is used in example) for multi purposes. With button press, you can change active USB mode on the fly when needed
  • Example 3: HID Host and CDC Device
    • Example shows how you can have one USB port for CDC device mode and another for HID Host mode. This allows you to connect your board to computer and still reading keyboard/mouse on another USB port
  • Example 4: USB to UART converted
    • Shows, how you can init USART based on user selected values from terminal on computer

Examples are available on my Github repository.

USB MSC Device Class

USB MSC device class is mainly used for communication between SDCARD and PC. STM32Fxxx acts like SDCARD reader and sends/receives data to/from PC. Device is shown to user as Mass Storage Device on computer.

For detailed explanation for USB DEVICE MSC, check API documentation.

Examples

  • Example 1: Basic MSC example
    • SDCARD is connected to SDIO port on STM32Fxxx, and on USB HS port, device is shown to computer as Mass Storage Device.

Compiled ALL examples

I’ve precompile all examples which can also be found and download from my github account for those, who don’t have Keil uVision or can’t compile it. Download is available below.

Download all libraries 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!