TM STM32Fxxx HAL Libraries  v1.0.0
Libraries for STM32Fxxx (F0, F4 and F7 series) devices based on HAL drivers from ST from Tilen Majerle

USB library for STM32Fxxx devices - http://stm32f4-discovery.com/2015/08/hal-library-21-multi-purpose-usb-library-for-stm32fxxx/. More...

Modules

 TM_USB_Macros
 Library defines.
 
 TM_USB_Typedefs
 Library Typedefs.
 
 TM_USB_Functions
 Library Functions.
 

Detailed Description

USB library for STM32Fxxx devices - http://stm32f4-discovery.com/2015/08/hal-library-21-multi-purpose-usb-library-for-stm32fxxx/.

This library is main library for all my other USB implementations. Its main purpose is to handle all common settings, like initializing GPIO pins and handling interrupts

Note
None of these functions should be directly called from user. They are called from my other libraries like TM_USB_HOST and TM_USB_DEVICE implementations.
Library features

This library is used to set some settings for your USB work when needed. These are:

- Enabling USB pins when needed
- Setting up configuration for specific board used for USB
- Driving VBUS when device acts like HOST
- Handling USB interrupts
- Library supports different USB configurations:
  - Both FS and HS ports defined as HOST mode
  - Both FS and HS ports defined as DEVICE mode
  - FS port device, HS port HOST
  - HS port device, FS port HOST
Setting up main configurations

By default, you will have to enable some configurations in order to use USB properly. This can be done in defines.h project settings. Open file, copy/edit things you need.

//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

For example, you want to use FS port for USB HID host and HS port for USB CDC device. You will need at least these defines:

//Enable FS mode functionality
#define USB_USE_FS
//Enable HS mode functionality
#define USB_USE_HS
//Enable HOST mode functionality
#define USB_USE_HOST
//Enable DEVICE mode functionality
#define USB_USE_DEVICE
USB FS mode settings

When using USB FS mode, STM32Fxxx needs 48MHz clock, provided from PLL, so you have to make sure that your PLL clock is set correct. PLL_M parameter should be the same as value of external crystal frequency in MHz. For example, if HSE_VALUE is 25000000, then PLL_M should be set to 25

USB HS mode settings

For USB HS mode with external ULPI PHY, you don't need any specific clock settings, because ULPI will do this for you. However, if you are using HS in FS mode, you have to setup 48MHz clock the same as when using FS mode.

STM32F4/7xx boards and configurations
Note
If you follow my description in TM_DISCO library on how to select your used board, then this library will automatically select your "target".

You will need to set bottom defines in case you don't use my TM_DISCO library configuration for board used.

//Enable default USB configurations for STM32F7-Discovery
#define USB_USE_STM32F7_DISCOVERY
//Enable default USB configurations for STM32F429-Discovery
#define USB_USE_STM32F29_DISCOVERY
//Enable default USB configurations for STM32F4-Discovery
#define USB_USE_STM32F4_DISCOVERY
STM32F4-Discovery pinout

Micro USB connector is on USB FS mode

PA12 <-> Data +
PA11 <-> Data -
PA10 <-> ID    
PA9  <-> VBUS  
PC0  <-> VBUS_EN
STM32F429-Discovery pinout

Micro USB connector is on USB HS in FS mode, because there is no external PHY for real USB HS mode

PB15 <-> Data +
PB14 <-> Data -
PB12 <-> ID    
PB13 <-> VBUS  
PC4  <-> VBUS_EN
STM32F7-Discovery pinout

Micro USB connectors are on USB FS mode and on real USB HS mode with external PHY chip

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 |
Enabling USB output

When USB port is declared as HOST mode, then you has ability to control (if you have external control for that) if external componentas allows you that. All discovery board has STMPS2151 chip which is designed for USB purposes. It has enable pin and allows up to 500mA current. If more current want to flow, chip will automaticall disable output and output pin will go low to indicate short circuit.

So, when using discovery board in USB port without external PHY, you have to enable USB output to give power on USB pin.

As mentioned above, library has feature to control this output. When you enable USB with TM_USB_Init function, output is also enabled if needed. To tell library, which output should be used, some defines should be set. Open defines.h file and copy/edit settings.

Note
If you are using some of my predefined boards, then this settings are automatically done and you don't need to do anything here.
//Use ENABLE pin on USB HS mode if needed
#define USB_HS_USE_ENABLE_PIN
#define USB_HS_ENABLE_PORT GPIOC //Enable PORT
#define USB_HS_ENABLE_PIN GPIO_PIN_4 //Enable PIN
#define USB_HS_ENABLE_STATE 0 //State of pin when output is enabled
//Use ENABLE pin on USB FS mode if needed
#define USB_FS_USE_ENABLE_PIN
#define USB_FS_ENABLE_PORT GPIOC //Enable PORT
#define USB_FS_ENABLE_PIN GPIO_PIN_4 //Enable PIN
#define USB_FS_ENABLE_STATE 0 //State of pin when output is enabled
Note
If enable pin is one pin on STM32F4/7xx device, then after you make defines, you are done with worrings.
If enable pin is like on STM32F439-EVAL, where I2C IO expander is used, then you can use TM_USB_DriveVBUSCallback callback function which will be called when there should be set output for USB. Check function for more information.
Changelog
 Version 1.0
  - First release
Dependencies
 - STM32Fxxx HAL
 - defines.h
 - TM GPIO
 - USB CORE