cancel
Showing results for 
Search instead for 
Did you mean: 

What should be done in firmware for VBUS detection through a GPIO pin?

Nick Alexeev
Associate III

Colleagues,

What should be done in firmware for VBUS detection through a GPIO pin?  My device is self-powered, so I have to do VBUS detection. The device is built around STM32G474, and it doesn’t have a dedicated pin for VBUS detection, so I have to do it through a GPIO pin and firmware.  VBUS is connected to the GPIO pin PA10 through a 2x 100kΩ divider.

I checked the code examples for STM32G4, but I couldn’t find examples for USB.  If somebody could point me to a code example, that would be great.

I’ve read this FAQ on VBUS detection which is a collection of excerpts from AN4879 (and I’ve read the app note too).

 

[without dedicated hardware VBUS detection pin] Any 5V tolerant pin with external interrupt functionality [...]

Does the VBUS detection have to be done in an interrupt? Or can it be done in the main loop? If it can be done in the main loop, then are there timing constraints?

 

"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.”

I'd like to pick up where that knowledgebase article left off.  What should the firmware do when it detects that VBUS (re)appeared?  What should the firmware do when VBUS has disappeared?

Best regards,
- Nick

1 ACCEPTED SOLUTION

Accepted Solutions
Nick Alexeev
Associate III

I received several answers from from the ST’s support, and arrived at the solution. 

Details here, including my code snippets.

View solution in original post

2 REPLIES 2
STOne-32
ST Employee

Dear @Nick Alexeev 

For VBUS detection through a GPIO pin on a self-powered device like your case built around the STM32G474, you will need to implement the VBUS sensing in firmware since your device does not have a dedicated VBUS detection pin. In case of Bus Powered ( it is not required  - Obvious as VDD will disappear).

Try to Implement an ISR that will be called when the VBUS presence change is detected.
In the ISR, check the state of the GPIO pin to determine if VBUS is present or not.


Handling VBUS Presence:

  1. When VBUS is detected (GPIO pin is high), the firmware should initialize the USB peripheral if it is not already initialized.
  2. Connect the pull-up resistor on the D+ line to signal to the host that the device is present on the bus, there is a function ready for that.

Handling VBUS Absence:

  1. When VBUS is no longer present (GPIO pin is low), the firmware should de-initialize the USB peripheral.
  2. Disconnect the pull-up resistor on the D+  to signal to the host that the device has been removed from the bus.

Hope it helps you.

STOne-32.

Nick Alexeev
Associate III

I received several answers from from the ST’s support, and arrived at the solution. 

Details here, including my code snippets.