Skip to content

shared/tinyusb: Implement tusb_time_delay_ms_api using mp_hal_delay_ms.#19213

Open
andrewleech wants to merge 1 commit into
micropython:masterfrom
andrewleech:stm32-tusb-time-delay
Open

shared/tinyusb: Implement tusb_time_delay_ms_api using mp_hal_delay_ms.#19213
andrewleech wants to merge 1 commit into
micropython:masterfrom
andrewleech:stm32-tusb-time-delay

Conversation

@andrewleech
Copy link
Copy Markdown
Contributor

Summary

Found while testing #19201: PYBD_SF3 fails to link with TinyUSB enabled, with an undefined reference to tusb_time_millis_api. The root cause (identified by @dpgeorge) is that tusb_time_delay_ms_api() - a weak function in TinyUSB whose default implementation busy-loops on tusb_time_millis_api() - gets pulled into the binary on boards with an internal USB HS PHY (USB_HS_PHYC). The dwc2 driver calls it to wait 2ms for the PHYC PLL to stabilise during init, and PYBD_SF3 (STM32F722) is currently the only MicroPython board where this code path is reached.

Rather than implementing tusb_time_millis_api() and keeping the busy-wait, this overrides tusb_time_delay_ms_api() directly using mp_hal_delay_ms(), which handles both IRQ-enabled and IRQ-disabled contexts correctly.

Testing

Built PYBD_SF3 with CFLAGS_EXTRA=-DMICROPY_HW_TINYUSB_STACK=1 (previously failed to link, now builds cleanly). Also built all 79 defined stm32 boards with TinyUSB forced on to confirm no regressions.

Trade-offs and Alternatives

Could implement tusb_time_millis_api() instead, but that leaves the busy-wait in place. tusb_time_delay_ms_api() is the right override since it's the actual call site.

Generative AI

I used generative AI tools when creating this PR, but a human has checked the code and is responsible for the description above.

TinyUSB declares tusb_time_delay_ms_api() as a weak function, defaulting
to a busy-wait loop via tusb_time_millis_api(). Provide an implementation
using mp_hal_delay_ms() to avoid the busy-wait.

This is required by the dwc2 synopsys driver for STM32 boards with an
internal USB HS PHY (USB_HS_PHYC), which calls tusb_time_delay_ms_api()
to wait 2ms for the PHYC PLL to stabilise during init.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.47%. Comparing base (670d775) to head (297f02a).
⚠️ Report is 11 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19213   +/-   ##
=======================================
  Coverage   98.47%   98.47%           
=======================================
  Files         176      176           
  Lines       22831    22845   +14     
=======================================
+ Hits        22483    22497   +14     
  Misses        348      348           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread shared/tinyusb/mp_usbd.c

// Required by TinyUSB drivers that need a blocking delay (e.g. dwc2 USB_HS_PHYC PLL init).
void tusb_time_delay_ms_api(uint32_t ms) {
mp_hal_delay_ms(ms);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is the right thing to do. This pumps the event loop and may raise an exception.

Really it wants to call something like HAL_Delay(), but that's only available on stm32. So maybe this function needs to be implemented per port?

@github-actions
Copy link
Copy Markdown

Code size report:

Reference:  tests/ports/unix: Add gc_info_fast test. [936596c]
Comparison: shared/tinyusb: Implement tusb_time_delay_ms_api using mp_hal_delay_ms. [merge of 297f02a]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

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.

3 participants