Management of VBUS sensing for USB device design
From a USB specification, a USB device should use VBUS sensing detection:
When a host connection is detected by the device, the device connects the pull-up resistor either to D+ or to the D- data signal. This allows the host to detect device presence on the bus.
There are two use cases:
- If the USB device is bus-powered, VBUS sensing is NOT mandatory: USB is connected all the time when the device is powered
- If the USB device is self-powered, VBUS sensing is mandatory
On STM32F2/F4/F7/L4, peripheral USB OTG_FS (OTG_HS without external PHY) is natively dedicated for VBUS sensing with pin PA9 (PB13 for HS), which is a +5 V tolerant pin.
In the datasheet DS10314 section 6.2, table 11, the "Absolute maximum rating" (AMR) table states that a +5 V tolerant pin voltage can be maximally VDD+4 V. So, the situation where the MCU is not powered and 5 V VBUS is connected to PA9 must be avoided because it is violating AMR.

1. Solution one: with a dedicated VBUS pin (PA9 or PB13)
To reduce the voltage on PA9 below 4 V, the usage of a voltage divider is recommended, so that AMR cannot be violated. But the internal VBUS detection block inside the OTG peripheral has a certain current consumption.
You can find this mentioned, for example, in STM32F411 datasheet (section 6.3.19 p. 113) or STM32F446 datasheet (section 6.3.20 p. 136):
“When VBUS sensing feature is enabled, PA9 should be left at their default state (floating input), not as alternate function. A typical 200 µA current consumption of the embedded sensing block (current to voltage conversion to determine the different sessions) can be observed on PA9 when the feature is enabled.”
The recommended resistor bridge is "vbus-- R1=4.7k -- R2=10k -- gnd" and then VBUS detection (PA9) can be taken between R1 and R2.
2. Solution two: without usage of a dedicated VBUS pin
Any 5V tolerant pin with external interrupt functionality can be used with an external resistor divider, to fulfill AMR conditions. In this case, the recommended values are 2x100 kOhms.
However, the disadvantage of this solution is that the software handling of VBUS presence event needs to be implemented by the user. This is not managed within our STM32 USB library for the time being.
Related links
For further reading on VBUS sensing and USB HW implementation, please refer to the AN4879.
