STM32Cube  Version 2.1.0
Create Projects for STM32U5 Series with STM32Cube HAL and STM32CubeMX
USB Host

Revision History

  • Version 1.1
    • Updated PowerControl function (removed unnecessary NVIC handling)
  • Version 1.0
    • Initial release

Capabilities

This driver supports USB Full and Low speeds.

Instances

Hardware resource relating to driver instance is shown in the table below:

Driver Instance Hardware Resource
Driver_USBH0 USB_OTG_FS

Configuration

Compile-time

Definitions used for compile-time configuration of this driver are shown in the table below:

Definition Default value Max value Description
USBH_MAX_PIPE_NUM 12 12 Defines maximum number of Pipes that the driver will support. This value impacts driver memory requirements.
USBH_BULK_IN_NAK_DELAY 0 65535 Delay between NAKs on the Bulk IN pipe (in SOFs), value 0 = not used (no delay between NAKs)
Note
To change the default value just provide a desired define with a desired value when compiling the driver

STM32CubeMX

The USB Host driver requires:

  • USB peripheral clock at 48 MHz
  • USB_OTG_FS peripheral configured as Host_Only
  • USB DP and DM pins and all other pins required by other necessary peripherals
  • pin or interface for external chip for driving the USB Port Power on the USB connector
  • optional fault detection mechanism, for faults like over-current

Source Code

Depending on the required peripherals it might be required to make some source code changes to support additional functionality.

For example, for the B-U585I-IOT02A board, to enable USB Power Delivery, which is necessary for power sourcing as well as fault handling it is necessary to make some source code changes.

B-U585I-IOT02A Board Configuration

These settings are relevant for this board, for different board please refer to the board schematic for proper configuration.

Link to B-U585I-IOT02A board schematic.

STM32CubeMX

Required peripherals for the B-U585I-IOT02A board are listed in the table below:

Peripheral Mode Description
USB_OTG_FS Host_Only USB Controller
PA11 Alternate Function Push Pull USB DM Pin
PA12 Alternate Function Push Pull USB DP Pin
I2C2 I2C USB-C Port Protection interface
PH4 Alternate Function Open Drain I2C2 SCL Pin
PH5 Alternate Function Open Drain I2C2 SDA Pin
UCPD1 Dual Role USB-C Power Delivery interface
PA15 Analog mode USB-C PD CC1 Pin
PB15 Analog mode USB-C PD CC2 Pin
PB5 Output Push Pull USB-C PP Power Pin
PD13 Analog mode USB-C PP VBUS current analog measurement
PE8 External Interrupt Mode with Falling edge trigger detection USB-C PP Fault Detect Pin
PF14 Analog mode VBUS Sense
EXTI Line 8 . External Interrupt
Note
USB-C PD stands for USB Type-C Power Delivery on-chip peripheral
USB-C PP stands for USB Type-C Port Protection for Dual Role Power application, on the B-U585I-IOT02A board it is the TCPP03-M20 chip
For the custom VBUS handling, please provide custom implementation of the USBH_VbusOnOff function
For the custom over-current detection, please provide custom implementation of the USBH_GetOC function
Note
All settings have to be configured as described in the procedure below. Important settings, typically the ones different from default, are emphasized in bold.

Pinout & Configuration tab

  1. In the Pinout view window click on a pin and select it's functionality:
    Pin Functionality
    PA11 USB_OTG_FS_DM
    PA12 USB_OTG_FS_DP
    PH4 I2C2_SCL
    PH5 I2C2_SDA
    PA15 UCPD1_CC1
    PB15 UCPD1_CC2
    PB5 GPIO_Output
    PD13 GPIO_Analog + ADC4_IN17
    PE8 GPIO_EXTI8
    PF14 GPIO_Analog + ADC4_IN5

  2. Under Categories: Connectivity select USB_OTG_FS:

    Mode:

    • Mode: Host_Only
    • Activate_VBUS: Disable
    • Activate_SOF: unchecked
    • Activate_NOE: unchecked

    Configuration:

    • Parameter Settings: not used
    • User Constants: not used
    • NVIC Settings: configured in later step (under Category: System Core: NVIC)
    • GPIO Settings:
      Pin Name Signal on Pin Pin Context.. Pin Priv.. GPIO output.. GPIO mode GPIO Pull-up/Pull.. Maximum out.. Fast Mode User Label
      PA11 USB_OTG_FS_DM n/a n/a n/a Alternate Function Push Pull No pull-up and no.. High n/a USB.C_N
      PA12 USB_OTG_FS_DP n/a n/a n/a Alternate Function Push Pull No pull-up and no.. High n/a USB.C_P

  3. Under Categories: Connectivity select I2C2:

    Mode:

    • I2C: I2C

    Configuration:

    • Parameter Settings: not used
    • User Constants: not used
    • NVIC Settings: configured in later step (under Category: System Core: NVIC)
    • DMA Settings: not used
    • GPIO Settings:
      Pin Name Signal on Pin Pin Context.. Pin Priv.. GPIO output.. GPIO mode GPIO Pull-up/Pull.. Maximum out.. Fast Mode User Label
      PH4 I2C2_SCL n/a n/a n/a Alternate Function Open Drain Pull-up Low n/a .
      PH5 I2C2_SDA n/a n/a n/a Alternate Function Open Drain Pull-up Low n/a .

  4. Under Categories: Connectivity select UCPD1:

    Mode:

    • UCPD Mode: Dual Role
    • Dead Battery Signals: unchecked
    • FRSTX: unchecked

    Configuration:

    • Parameter Settings: not used
    • User Constants: not used
    • NVIC Settings: configured in later step (under Category: System Core: NVIC)
    • DMA Settings: not used
    • GPIO Settings:
      Pin Name Signal on Pin Pin Context.. Pin Priv.. GPIO output.. GPIO mode GPIO Pull-up/Pull.. Maximum out.. Fast Mode User Label
      PA15 UCPD1_CC1 n/a n/a n/a Analog mode No pull-up and no.. n/a n/a USB.UCPD_CC1
      PB15 UCPD1_CC2 n/a n/a n/a Analog mode No pull-up and no.. n/a n/a USB.UCPD_CC2

  5. Under Categories: System Core select GPIO:

    Configuration:

    • GPIO:
      Pin Name Signal on Pin Pin Context.. Pin Priv.. GPIO output.. GPIO mode GPIO Pull-up/Pull.. Maximum out.. Fast Mode User Label
      PB5 n/a n/a n/a Low Output Push Pull No pull-up and no.. Low n/a UCPD_PWR
      PD13 GPIO_Analog n/a n/a n/a Analog mode No pull-up and no.. n/a n/a USB.IANA
      PE8 n/a n/a Non-priv.. n/a Ext Int Falling e.. Pull-up n/a n/a USB.UCPD_FLT
      PF14 GPIO_Analog n/a n/a n/a Analog mode No pull-up and no.. n/a n/a USB.VBUS_SENSE

  6. Under Categories: System Core select NVIC:

    Configuration:

    • NVIC:
      NVIC Interrupt Table Enabled Preemption Priority Sub Priority
      USB OTG FS global interrupt checked 0 0
      I2C2 Event interrupt unchecked 0 0
      I2C2 Error interrupt unchecked 0 0
      UCPD1 global interrupt unchecked 0 0
      EXTI Line8 interrupt checked 0 0
    • Code generation:
      Enabled interrupt table Select for.. Generate Enable in.. Generate IRQ h.. Call HAL handler
      USB OTG FS global interrupt unchecked checked unchecked n/a
      EXTI Line8 interrupt unchecked unchecked checked checked

Clock Configuration tab

  1. Configure To USB (MHz): 48

Project Manager tab

  1. Under Advanced Settings:

    Generated Function Calls:

    Generate Code Function Name Peripheral Inst.. Do not generate .. Visibility (Static)
    checked MX_I2C2_Init I2C2 unchecked checked
    checked MX_UCPD1_Init UCPD1 unchecked checked
    checked MX_USB_OTG_FS_HCD_Init USB_OTG_FS unchecked checked

Generate Code

Generate source code by clicking on the GENERATE CODE button on the toolbar.

Source Code

To enable USB Power Delivery, which is necessary for power sourcing as well as fault handling, please include b_u585i_iot02a_usbpd_pwr.c board driver into your project and make the following changes to your source code (in your source module, preferably main.c):

Include the b_u585i_iot02a_usbpd_pwr.h:

#include "b_u585i_iot02a_usbpd_pwr.h"

Add the code for initialization and enabling of the USB PD:

BSP_USBPD_PWR_Init (USBPD_PWR_TYPE_C_PORT_1);
BSP_USBPD_PWR_SetRole (USBPD_PWR_TYPE_C_PORT_1, POWER_ROLE_SOURCE);
BSP_USBPD_PWR_SetPowerMode(USBPD_PWR_TYPE_C_PORT_1, USBPD_PWR_MODE_NORMAL);
BSP_USBPD_PWR_VBUSInit (USBPD_PWR_TYPE_C_PORT_1);

Add the code for VBUS driving:

///
/// \fn void USBH_VbusOnOff (bool vbus)
/// \brief Drive VBUS on/off.
/// \param[in] vbus
/// - \b false VBUS off
/// - \b true VBUS on
/// \return none
///
void USBH_VbusOnOff (bool vbus) {
if (vbus) { // VBUS power on
BSP_USBPD_PWR_VBUSOn (0U);
} else { // VBUS power off
BSP_USBPD_PWR_VBUSOff(0U);
}
}

Add the code for USB PD fault handling:

///
/// \fn void HAL_GPIO_EXTI_Falling_Callback (uint16_t GPIO_Pin)
/// \brief EXTI line falling detection callback.
/// \param[in] GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
/// \return none
///
void HAL_GPIO_EXTI_Falling_Callback (uint16_t GPIO_Pin) {
if (GPIO_Pin == GPIO_PIN_8) { // If pin is PE8
// Call BSP USBPD PWR callback
BSP_USBPD_PWR_EventCallback(USBPD_PWR_TYPE_C_PORT_1);
}
}
Note
Initially when an USB Device (USB Keyboard or USB Mass Storage device) is connected to the USB Host an over-current fault will be signaled due to voltage changes during the connection, so USB PD has to re-enable power on the VBUS to continue using the device