When designing USB device, how VBUS sensing shall be managed?
From USB specification, USB device shall use VBUS sensing detection:
When host connection is detected by the device, device connects pull-up resistor either to D+ or to D- data signal, allowing host to detect device presence on the bus.
There are two use cases:
- If USB device is bus-powered, VBUS sensing is NOT mandatory: USB is connected all the time when device is powered,
- If 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 +5V tolerant pin.
In datasheet, Absolute Maximal Rating (AMR) table is stating that a +5V tolerant pin voltage can be maximally VDD+4V. So the situation where the MCU is not powered and 5V VBUS is connected to PA9 must be avoided because it is violating AMR.
Two solutions can be proposed:
1. Solution with a dedicated VBUS pin (PA9 or PB13)
In order to reduce the voltage on PA9 below 4V, the usage of a voltage divider is recommended, so that AMR cannot be violated. But internal VBUS detection block inside OTG peripheral has certain current consumption.
You can find this mentioned for example in STM32F411 datasheet (section 6.3.19 p. 112) or STM32F446 datasheet (section 6.3.20 p. 139) :
“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.”
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 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, 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 customer, since this is not managed within our STM32 USB library for the time being.