shared/tinyusb: Implement tusb_time_delay_ms_api using mp_hal_delay_ms.#19213
Open
andrewleech wants to merge 1 commit into
Open
shared/tinyusb: Implement tusb_time_delay_ms_api using mp_hal_delay_ms.#19213andrewleech wants to merge 1 commit into
andrewleech wants to merge 1 commit into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
dpgeorge
reviewed
May 12, 2026
|
|
||
| // 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); |
Member
There was a problem hiding this comment.
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?
|
Code size report: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
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 thattusb_time_delay_ms_api()- a weak function in TinyUSB whose default implementation busy-loops ontusb_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 overridestusb_time_delay_ms_api()directly usingmp_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.