From 26d5032980335ccdfe81234ad7a24030a580f6d3 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 1 Nov 2023 15:30:05 +1100 Subject: [PATCH] samd: Switch TinyUSB to run via a scheduled task. Previously the TinyUSB task was run in the ISR immediately after the interrupt handler. This approach gives very similar performance (no change in CDC throughput tests) but reduces the amount of time spent in the ISR, and allows TinyUSB callbacks to run in thread mode. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton --- ports/samd/Makefile | 3 +++ ports/samd/samd_isr.c | 8 ++++---- ports/samd/samd_soc.h | 4 ---- ports/samd/tusb_port.c | 27 --------------------------- 4 files changed, 7 insertions(+), 35 deletions(-) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 291a894ab7..12223e6ded 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -93,6 +93,8 @@ 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 += \ @@ -131,6 +133,7 @@ SHARED_SRC_C += \ shared/runtime/sys_stdio_mphal.c \ shared/timeutils/timeutils.c \ shared/tinyusb/mp_cdc_common.c \ + shared/tinyusb/mp_usbd.c ASF4_SRC_C += $(addprefix lib/asf4/$(MCU_SERIES_LOWER)/,\ hal/src/hal_atomic.c \ diff --git a/ports/samd/samd_isr.c b/ports/samd/samd_isr.c index a12140313b..7c4c1d060f 100644 --- a/ports/samd/samd_isr.c +++ b/ports/samd/samd_isr.c @@ -310,10 +310,10 @@ const ISR isr_vector[] __attribute__((section(".isr_vector"))) = { &Sercom7_Handler, // 77 Serial Communication Interface 7 (SERCOM7): SERCOM7_3 - 6 0, // 78 Control Area Network 0 (CAN0) 0, // 79 Control Area Network 1 (CAN1) - &USB_0_Handler_wrapper, // 80 Universal Serial Bus (USB): USB_EORSM_DNRS, ... - &USB_1_Handler_wrapper, // 81 Universal Serial Bus (USB): USB_SOF_HSOF - &USB_2_Handler_wrapper, // 82 Universal Serial Bus (USB): USB_TRCPT0_0 - _7 - &USB_3_Handler_wrapper, // 83 Universal Serial Bus (USB): USB_TRCPT1_0 - _7 + &USB_Handler_wrapper, // 80 Universal Serial Bus (USB): USB_EORSM_DNRS, ... + &USB_Handler_wrapper, // 81 Universal Serial Bus (USB): USB_SOF_HSOF + &USB_Handler_wrapper, // 82 Universal Serial Bus (USB): USB_TRCPT0_0 - _7 + &USB_Handler_wrapper, // 83 Universal Serial Bus (USB): USB_TRCPT1_0 - _7 0, // 84 Ethernet MAC (GMAC) 0, // 85 Timer Counter Control 0 (TCC0): TCC0_CNT_A ... 0, // 86 Timer Counter Control 0 (TCC0): TCC0_MC_0 diff --git a/ports/samd/samd_soc.h b/ports/samd/samd_soc.h index 1848cf7db1..90a5a57ffa 100644 --- a/ports/samd/samd_soc.h +++ b/ports/samd/samd_soc.h @@ -36,10 +36,6 @@ void samd_init(void); void samd_main(void); void USB_Handler_wrapper(void); -void USB_0_Handler_wrapper(void); -void USB_1_Handler_wrapper(void); -void USB_2_Handler_wrapper(void); -void USB_3_Handler_wrapper(void); void sercom_enable(Sercom *spi, int state); void sercom_register_irq(int sercom_id, void (*sercom_irq_handler)); diff --git a/ports/samd/tusb_port.c b/ports/samd/tusb_port.c index 2098334fba..e56ef0fd6a 100644 --- a/ports/samd/tusb_port.c +++ b/ports/samd/tusb_port.c @@ -117,33 +117,6 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { return desc_str; } -#if defined(MCU_SAMD21) - void USB_Handler_wrapper(void) { tud_int_handler(0); - tud_task(); } - -#elif defined(MCU_SAMD51) - -void USB_0_Handler_wrapper(void) { - tud_int_handler(0); - tud_task(); -} - -void USB_1_Handler_wrapper(void) { - tud_int_handler(0); - tud_task(); -} - -void USB_2_Handler_wrapper(void) { - tud_int_handler(0); - tud_task(); -} - -void USB_3_Handler_wrapper(void) { - tud_int_handler(0); - tud_task(); -} - -#endif