Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

71%
+3 −0
Q&A 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...

2 answers  ·  posted 4mo ago by Nick Alexeev‭  ·  last activity 3mo ago by Nick Alexeev‭

Question USB firmware STM32
#2: Post edited by user avatar Nick Alexeev‭ · 2024-06-12T01:40:21Z (4 months ago)
#1: Initial revision by user avatar Nick Alexeev‭ · 2024-06-12T01:32:25Z (4 months ago)
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?