Post History
I've got a is self-powered USB device, which means that I have to do VBUS detection. The device is built around STM32G474. Unlike some other STM32s, the STM32G4 don't have a dedicated pin for VBUS...
#1: Initial revision
STM32. What should be done in firmware for VBUS detection through a GPIO pin?
I've got a is self-powered USB device, which means that I have to do VBUS detection. The device is built around STM32G474. Unlike some other STM32s, the STM32G4 don'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. What should be done in firmware for VBUS detection through a GPIO pin? I checked the [code examples for STM32G4s](https://github.com/STMicroelectronics/STM32CubeG4/tree/master/Projects), 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 knowledgebase article on VBUS detection](https://community.st.com/t5/stm32-mcus/management-of-vbus-sensing-for-usb-device-design/ta-p/49485) which is a collection of excerpts from AN4879 (and I’ve read the app note too). > Any 5V tolerant pin with external interrupt functionality... [from [knowledgebase article](https://community.st.com/t5/stm32-mcus/management-of-vbus-sensing-for-usb-device-design/ta-p/49485)] 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. [from [the same knowledgebase article](https://community.st.com/t5/stm32-mcus/management-of-vbus-sensing-for-usb-device-design/ta-p/49485)] I have 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?