From 22f1d766334f96f48aaaa04a72faeb9a2a37b595 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Thu, 22 May 2025 17:38:22 +1000 Subject: [PATCH] shared/tinyusb: Use device event hook to schedule USB task. Previously MicroPython ports would linker-wrap dcd_event_handler in order to schedule the USB task callback to run when needed. TinyUSB 0.16 added proper support for an event hook to do the same thing without the hacky linker wrapping. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton --- ports/alif/alif.mk | 4 ---- ports/esp32/esp32_common.cmake | 8 -------- ports/nrf/Makefile | 1 - ports/renesas-ra/Makefile | 3 --- ports/rp2/CMakeLists.txt | 1 - ports/samd/Makefile | 2 -- shared/tinyusb/mp_usbd.c | 13 ++----------- 7 files changed, 2 insertions(+), 30 deletions(-) diff --git a/ports/alif/alif.mk b/ports/alif/alif.mk index eee27b3b7d..bb07a3aa20 100644 --- a/ports/alif/alif.mk +++ b/ports/alif/alif.mk @@ -103,10 +103,6 @@ CFLAGS += -Wl,-T$(BUILD)/ensemble.ld \ -Wl,--print-memory-usage \ -Wl,--no-warn-rwx-segment -ifeq ($(MCU_CORE),M55_HP) -CFLAGS += -Wl,--wrap=dcd_event_handler -endif - ################################################################################ # Source files and libraries diff --git a/ports/esp32/esp32_common.cmake b/ports/esp32/esp32_common.cmake index 2e7b95b385..09b1203913 100644 --- a/ports/esp32/esp32_common.cmake +++ b/ports/esp32/esp32_common.cmake @@ -97,10 +97,6 @@ if(MICROPY_PY_TINYUSB) list(APPEND MICROPY_INC_TINYUSB ${MICROPY_DIR}/shared/tinyusb/ ) - - list(APPEND MICROPY_LINK_TINYUSB - -Wl,--wrap=dcd_event_handler - ) endif() list(APPEND MICROPY_SOURCE_PORT @@ -261,10 +257,6 @@ target_compile_options(${MICROPY_TARGET} PUBLIC -Wno-missing-field-initializers ) -target_link_options(${MICROPY_TARGET} PUBLIC - ${MICROPY_LINK_TINYUSB} -) - # Additional include directories needed for private NimBLE headers. target_include_directories(${MICROPY_TARGET} PUBLIC ${IDF_PATH}/components/bt/host/nimble/nimble diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 59e74dce42..d3d747186f 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -268,7 +268,6 @@ SRC_C += $(addprefix lib/tinyusb/src/,\ portable/nordic/nrf5x/dcd_nrf5x.c \ ) -LDFLAGS += -Wl,--wrap=dcd_event_handler endif DRIVERS_SRC_C += $(addprefix modules/,\ diff --git a/ports/renesas-ra/Makefile b/ports/renesas-ra/Makefile index ec47510d98..fd74c60a85 100644 --- a/ports/renesas-ra/Makefile +++ b/ports/renesas-ra/Makefile @@ -157,9 +157,6 @@ LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)" LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" endif -# Hook tinyusb USB interrupt if used to service usb task. -LDFLAGS += --wrap=dcd_event_handler - # Options for mpy-cross MPY_CROSS_FLAGS += -march=armv7m diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 7cb0c60aa2..f0b278df2b 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -525,7 +525,6 @@ target_compile_options(${MICROPY_TARGET} PRIVATE target_link_options(${MICROPY_TARGET} PRIVATE -Wl,--defsym=__micropy_c_heap_size__=${MICROPY_C_HEAP_SIZE} - -Wl,--wrap=dcd_event_handler -Wl,--wrap=runtime_init_clocks ) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 005664f178..bec530d803 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -113,8 +113,6 @@ LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)" LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" endif -LDFLAGS += --wrap=dcd_event_handler - MPY_CROSS_FLAGS += -march=$(MPY_CROSS_MCU_ARCH) SRC_C += \ diff --git a/shared/tinyusb/mp_usbd.c b/shared/tinyusb/mp_usbd.c index 7ccfa7018d..f03f7f7db4 100644 --- a/shared/tinyusb/mp_usbd.c +++ b/shared/tinyusb/mp_usbd.c @@ -30,10 +30,6 @@ #include "mp_usbd.h" -#ifndef NO_QSTR -#include "device/dcd.h" -#endif - #if !MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE void mp_usbd_task(void) { @@ -47,13 +43,8 @@ void mp_usbd_task_callback(mp_sched_node_t *node) { #endif // !MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE -extern void __real_dcd_event_handler(dcd_event_t const *event, bool in_isr); - -// If -Wl,--wrap=dcd_event_handler is passed to the linker, then this wrapper -// will be called and allows MicroPython to schedule the TinyUSB task when -// dcd_event_handler() is called from an ISR. -TU_ATTR_FAST_FUNC void __wrap_dcd_event_handler(dcd_event_t const *event, bool in_isr) { - __real_dcd_event_handler(event, in_isr); +// Schedule the TinyUSB task on demand, when there is a new USB device event +TU_ATTR_FAST_FUNC void tud_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr) { mp_usbd_schedule_task(); mp_hal_wake_main_task_from_isr(); }