-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
mp_usbd_cdc.c: Skip writing to an uninitialized USB device. #15481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #15481 +/- ##
=======================================
Coverage 98.42% 98.42%
=======================================
Files 161 161
Lines 21252 21252
=======================================
Hits 20918 20918
Misses 334 334 ☔ View full report in Codecov by Sentry. |
Code size report:
|
This looks like probably a regression from #14485 when MicroPython started buffering output to send to CDC on connect. Combined with not initialising TinyUSB until after boot.py finishes as a way to support clean runtime USB initialisation. I think any early print output was probably lost anyhow, as the TinyUSB CDC driver zeroes its data structures on driver init. So I think this is a good way to fix it, thanks @robert-hh . cc @andrewleech |
shared/tinyusb/mp_usbd.h
Outdated
@@ -67,6 +67,8 @@ extern const uint8_t mp_usbd_builtin_desc_cfg[MP_USBD_BUILTIN_DESC_CFG_LEN]; | |||
|
|||
void mp_usbd_task_callback(mp_sched_node_t *node); | |||
|
|||
bool tusb_inited(void); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be needed, it's already defined in tusb.h
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. For the same reason, the declaration of tusb_init()
further down is also not needed.
Ah yes, previously the stdout would skip USB if it's not connected, but now it tries to write to the USB buffer. I hadn't considered situations before USB init occurred; I hadn't realised that was possible! Yep it's certainly reasonable to add this check to skip if it's not init, that's much safer. |
During execution of `boot.py` the USB device is not yet initialized. Any attempt to write to the CDC (eg calling `print()`) would lock up the device. This commit skips writing when the USB device is not initialized. Any output from `boot.py` is lost, but the device does not lock up. Also removed unnecessary declaration of `tusb_init()`. Signed-off-by: robert-hh <[email protected]>
I tested this by adding a print to The alternative would be to somehow pre-initialise TinyUSB's CDC buffers very early on (before |
Thanks. I started looking into TinyUSB for the change, but then I preferred the simple patch in the MicroPython Domain. Changing TinyUSB would require to update at it's source repository. I'm sure Hatach would accept it, but it would take more time. |
During execution of boot.py the USB device is not yet initialized. Any attempt to write would lock up the device. This change skips writing. Any output from boot.py is lost, but the device does not lock up.
Attempt to fix issue #15471.