esp32: Re-initialise USB after soft reset. #18375
Open
+6
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Necessary if runtime
USBDevicehas been active on ESP32-S3 along with the default USB-CDC REPL. Without this fix, two things happen after a soft reset:Testing
Note this PR needs to be cherry-picked onto #18332 if PSRAM is enabled.
mpremote a0 run pr18332.py- where the file has the sample code from esp32/boards: Configure S2/S3 PSRAM boards to use USB in IRQ mode. #18332mpremote runthen mpremote will error out as it disconnects to change USB device config.mpremote a0Without fix: USB-CDC disappears along with the keyboard device, never comes back. Connecting to the UART REPL and interacting with it will eventually slow UART TX down to a trickle (quickest way to trigger is
print("a"*500).)With fix: USB-CDC disappears and re-enumerates again as plain USB-CDC device.
Related bugs
There is still a related bug here, because calling TinyUSB
tud_disconnect()on ESP32-S3 doesn't trigger anyDCD_EVENT_UNPLUGGEDorDCD_EVENT_BUS_RESETevent. This means that the USB device driver state goes out of sync (i.e. TinyUSB thinks it's still connected).This PR avoids this in soft reset because the host will reconfigure the USB device and the state will correct itself, but there's still a problem if Python code does something like this:
mpremote a0 run pr18332.pympremote a0import machine; u = machine.USBDevice(); u.active(0)In this situation the USB device disappears from the host as expected, but the issue with very slow stdout will happen as the CDC device driver still thinks it's connected and configured.
I've opened a Discussion on the TinyUSB repo to try and find out the expected behaviour: hathach/tinyusb#3331
This work was funded through GitHub Sponsors.