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 <angus@redyak.com.au>
This commit is contained in:
Angus Gratton
2025-05-22 17:38:22 +10:00
committed by Damien George
parent 49f81d5046
commit 22f1d76633
7 changed files with 2 additions and 30 deletions

View File

@@ -103,10 +103,6 @@ CFLAGS += -Wl,-T$(BUILD)/ensemble.ld \
-Wl,--print-memory-usage \ -Wl,--print-memory-usage \
-Wl,--no-warn-rwx-segment -Wl,--no-warn-rwx-segment
ifeq ($(MCU_CORE),M55_HP)
CFLAGS += -Wl,--wrap=dcd_event_handler
endif
################################################################################ ################################################################################
# Source files and libraries # Source files and libraries

View File

@@ -97,10 +97,6 @@ if(MICROPY_PY_TINYUSB)
list(APPEND MICROPY_INC_TINYUSB list(APPEND MICROPY_INC_TINYUSB
${MICROPY_DIR}/shared/tinyusb/ ${MICROPY_DIR}/shared/tinyusb/
) )
list(APPEND MICROPY_LINK_TINYUSB
-Wl,--wrap=dcd_event_handler
)
endif() endif()
list(APPEND MICROPY_SOURCE_PORT list(APPEND MICROPY_SOURCE_PORT
@@ -261,10 +257,6 @@ target_compile_options(${MICROPY_TARGET} PUBLIC
-Wno-missing-field-initializers -Wno-missing-field-initializers
) )
target_link_options(${MICROPY_TARGET} PUBLIC
${MICROPY_LINK_TINYUSB}
)
# Additional include directories needed for private NimBLE headers. # Additional include directories needed for private NimBLE headers.
target_include_directories(${MICROPY_TARGET} PUBLIC target_include_directories(${MICROPY_TARGET} PUBLIC
${IDF_PATH}/components/bt/host/nimble/nimble ${IDF_PATH}/components/bt/host/nimble/nimble

View File

@@ -268,7 +268,6 @@ SRC_C += $(addprefix lib/tinyusb/src/,\
portable/nordic/nrf5x/dcd_nrf5x.c \ portable/nordic/nrf5x/dcd_nrf5x.c \
) )
LDFLAGS += -Wl,--wrap=dcd_event_handler
endif endif
DRIVERS_SRC_C += $(addprefix modules/,\ DRIVERS_SRC_C += $(addprefix modules/,\

View File

@@ -157,9 +157,6 @@ LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)"
LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))"
endif endif
# Hook tinyusb USB interrupt if used to service usb task.
LDFLAGS += --wrap=dcd_event_handler
# Options for mpy-cross # Options for mpy-cross
MPY_CROSS_FLAGS += -march=armv7m MPY_CROSS_FLAGS += -march=armv7m

View File

@@ -525,7 +525,6 @@ target_compile_options(${MICROPY_TARGET} PRIVATE
target_link_options(${MICROPY_TARGET} PRIVATE target_link_options(${MICROPY_TARGET} PRIVATE
-Wl,--defsym=__micropy_c_heap_size__=${MICROPY_C_HEAP_SIZE} -Wl,--defsym=__micropy_c_heap_size__=${MICROPY_C_HEAP_SIZE}
-Wl,--wrap=dcd_event_handler
-Wl,--wrap=runtime_init_clocks -Wl,--wrap=runtime_init_clocks
) )

View File

@@ -113,8 +113,6 @@ LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)"
LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))" LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))"
endif endif
LDFLAGS += --wrap=dcd_event_handler
MPY_CROSS_FLAGS += -march=$(MPY_CROSS_MCU_ARCH) MPY_CROSS_FLAGS += -march=$(MPY_CROSS_MCU_ARCH)
SRC_C += \ SRC_C += \

View File

@@ -30,10 +30,6 @@
#include "mp_usbd.h" #include "mp_usbd.h"
#ifndef NO_QSTR
#include "device/dcd.h"
#endif
#if !MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE #if !MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
void mp_usbd_task(void) { 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 #endif // !MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
extern void __real_dcd_event_handler(dcd_event_t const *event, bool 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) {
// 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);
mp_usbd_schedule_task(); mp_usbd_schedule_task();
mp_hal_wake_main_task_from_isr(); mp_hal_wake_main_task_from_isr();
} }