Skip to content

Comments

stm32-usb: Use the susprdy bit to show disconnect#3944

Closed
drbartling wants to merge 2 commits intoembassy-rs:mainfrom
drbartling:stm32g0-usb-disconnect
Closed

stm32-usb: Use the susprdy bit to show disconnect#3944
drbartling wants to merge 2 commits intoembassy-rs:mainfrom
drbartling:stm32g0-usb-disconnect

Conversation

@drbartling
Copy link

The USB peripheral does not require a VBUS connection. When USB is unplugged from a self-powered USB device, the existing drivers and hardware set the susprdy and the suspen bits.

It's also notable that the frame number stops counting.

I chose to use the susprdy bit as a way to identify when USB is unplugged to allow USB writes or reads to return an error.

references:
#3740
https://www.st.com/resource/en/reference_manual/rm0444-stm32g0x1-advanced-armbased-32bit-mcus-stmicroelectronics.pdf p. 1285

Ryan Bartling added 2 commits March 3, 2025 19:58
The USB peripheral does not require a VBUS connection.
When USB is unplugged from a self-powered USB device, the existing
drivers and hardware set the susprdy and the suspen bits.

It's also notable that the frame number stops counting.

I chose to use the susprdy bit as a way to identify when USB is
unplugged to allow USB writes or reads to return an error.

references:
embassy-rs#3740
https://www.st.com/resource/en/reference_manual/rm0444-stm32g0x1-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
p. 1285
@Dirbaio
Copy link
Member

Dirbaio commented Mar 11, 2025

This is not correct, the SUSPRDY bit indicates suspend, not disconnect. in USB, "suspend" and "disconnect" are different things. It's only on actual disconnect that we should disable endpoints. On suspend, the host expects everything to stay as-is when it unsuspends.

Here's how you tell which state you're in:

VBUS present, activity on the bus -> USB is active
VBUS present, no activity on the bus -> USB is suspended
VBUS no present -> USB is disconnected

the USB peripheral only looks at the DP/DM lines so it can only tell whether there's activity on the bus or not. To distinguish "suspended" and "disconnected" states you need to wire VBUS to a GPIO. Some boards don't wire it, in which case it's simply impossible to support the "disconnected" state, so it's simply impossible to implement a self-powered usb device in a compliant way.

The correct fix would be to add "vbus detection" support to the driver via gpio.

see previous discusison in #3629 (for rp, but the problem is exactly the same as here)

@Dirbaio Dirbaio closed this Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants