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
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
- CDC device for acting like Virtual COM Port (VCP) to your computer
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Use this define, if you want to enable USB functionality for USB FS (Full speed) mode in your project //By default it is disabled to save some RAM #define USB_USE_FS //Use this define, if you want to enable USB functionality for USB HS (High speed) mode in your project //By default it is disabled to save some RAM #define USB_USE_HS //Use this define, if you are using external PHY chip for USB on HS port //If you don't use it, then HS mode will be USB HS in FS (High Speed in Full speed) mode //This mode is automatically enabled for HS mode if using STM32F7-Discovery board #define USB_USE_ULPI_PHY //Use this define if you will use FS or HS USB mode in HOST mode #define USB_USE_HOST //Use this define if you will use FS or HS USB mode in DEVICE mode #define USB_USE_DEVICE |
USB pinout can be found in table below for some different STM32F4xx and STM32F7 boards.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
STM32F4-Discovery pinout (USB FS mode only) PA12 <-> Data + PA11 <-> Data - PA10 <-> ID PA9 <-> VBUS PC0 <-> VBUS_EN STM32F429-Discovery pinout (USB HS in FS mode only) PB15 <-> Data + PB14 <-> Data - PB12 <-> ID PB13 <-> VBUS PC4 <-> VBUS_EN STM32F7-Discovery pinout (USB FS and HS modes) USB FS mode | USB HS mode | PA12 <-> Data + | PA3 <-> D0 | PB10 <-> D4 | PC0 <-> STP PA11 <-> Data - | PB0 <-> D1 | PB11 <-> D5 | PA5 <-> CLK PA10 <-> ID | PB1 <-> D2 | PB12 <-> D6 | PH4 <-> NXT PA9 <-> VBUS | PB5 <-> D3 | PB13 <-> D7 | PC2 <-> DIR PD5 <-> VBUS_EN | |
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.
STM32 libraries based on STM32Fxxx HAL drivers.
Recent comments