Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 1 commit into from
Jul 19, 2024

Conversation

robert-hh
Copy link
Contributor

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.

Copy link

codecov bot commented Jul 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.42%. Comparing base (847ee20) to head (a734ee9).

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.
📢 Have feedback on the report? Share it here.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:   +24 +0.007% TEENSY40
        rp2:   -24 -0.003% RPI_PICO_W
       samd:   +12 +0.005% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@projectgus
Copy link
Contributor

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

@@ -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);
Copy link
Member

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.

Copy link
Contributor Author

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.

@andrewleech
Copy link
Contributor

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]>
@dpgeorge
Copy link
Member

I tested this by adding a print to boot.py: before this PR the Pico W would not start up, and with this PR it does start up (but the print is lost). So that's good.

The alternative would be to somehow pre-initialise TinyUSB's CDC buffers very early on (before boot.py) and then not touch them when doing a proper tusb_init(), to retain any data printed. But that would most likely require changes to TinyUSB (which we could eventually do...).

@dpgeorge dpgeorge added the shared Relates to shared/ directory in source label Jul 19, 2024
@dpgeorge dpgeorge merged commit a734ee9 into micropython:master Jul 19, 2024
63 of 64 checks passed
@robert-hh
Copy link
Contributor Author

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.

@robert-hh robert-hh deleted the mp_usbd_tx branch July 19, 2024 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
shared Relates to shared/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants