diff --git a/ports/atmel-samd/background.c b/ports/atmel-samd/background.c index b903456ee8..5c499ab3a8 100644 --- a/ports/atmel-samd/background.c +++ b/ports/atmel-samd/background.c @@ -85,7 +85,6 @@ void run_background_tasks(void) { network_module_background(); #endif filesystem_background(); - usb_background(); running_background_tasks = false; assert_heap_ok(); diff --git a/ports/atmel-samd/supervisor/usb.c b/ports/atmel-samd/supervisor/usb.c index 650ed6a397..ea940f8988 100644 --- a/ports/atmel-samd/supervisor/usb.c +++ b/ports/atmel-samd/supervisor/usb.c @@ -29,6 +29,8 @@ #include "hpl/gclk/hpl_gclk_base.h" #include "hal_gpio.h" #include "lib/tinyusb/src/device/usbd.h" +#include "supervisor/background_callback.h" +#include "supervisor/usb.h" void init_usb_hardware(void) { #ifdef SAMD21 @@ -61,24 +63,24 @@ void init_usb_hardware(void) { #ifdef SAMD21 void USB_Handler(void) { - tud_int_handler(0); + usb_irq_handler(); } #endif #ifdef SAM_D5X_E5X void USB_0_Handler (void) { - tud_int_handler(0); + usb_irq_handler(); } void USB_1_Handler (void) { - tud_int_handler(0); + usb_irq_handler(); } void USB_2_Handler (void) { - tud_int_handler(0); + usb_irq_handler(); } void USB_3_Handler (void) { - tud_int_handler(0); + usb_irq_handler(); } #endif diff --git a/ports/litex/mphalport.c b/ports/litex/mphalport.c index 84a5467951..862f163939 100644 --- a/ports/litex/mphalport.c +++ b/ports/litex/mphalport.c @@ -31,6 +31,7 @@ #include "py/mphal.h" #include "py/mpstate.h" #include "py/gc.h" +#include "supervisor/usb.h" #include "csr.h" #include "generated/soc.h" @@ -49,7 +50,7 @@ void isr(void) { #ifdef CFG_TUSB_MCU if (irqs & (1 << USB_INTERRUPT)) - tud_int_handler(0); + usb_irq_handler(); #endif if (irqs & (1 << TIMER0_INTERRUPT)) SysTick_Handler(); diff --git a/ports/mimxrt10xx/supervisor/usb.c b/ports/mimxrt10xx/supervisor/usb.c index 1bc7ea9b56..af259405a3 100644 --- a/ports/mimxrt10xx/supervisor/usb.c +++ b/ports/mimxrt10xx/supervisor/usb.c @@ -27,6 +27,7 @@ #include "fsl_clock.h" #include "tusb.h" +#include "supervisor/usb.h" void init_usb_hardware(void) { CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U); @@ -56,5 +57,5 @@ void init_usb_hardware(void) { } void USB_OTG1_IRQHandler(void) { - tud_int_handler(0); + usb_irq_handler(); } diff --git a/ports/nrf/background.c b/ports/nrf/background.c index 966c56e0b7..bc7c0d7d32 100644 --- a/ports/nrf/background.c +++ b/ports/nrf/background.c @@ -59,7 +59,6 @@ void run_background_tasks(void) { } running_background_tasks = true; filesystem_background(); - usb_background(); #if CIRCUITPY_AUDIOPWMIO audiopwmout_background(); #endif diff --git a/ports/nrf/supervisor/usb.c b/ports/nrf/supervisor/usb.c index 3d2527faaa..771e86ce03 100644 --- a/ports/nrf/supervisor/usb.c +++ b/ports/nrf/supervisor/usb.c @@ -30,6 +30,7 @@ #include "lib/utils/interrupt_char.h" #include "lib/mp-readline/readline.h" #include "lib/tinyusb/src/device/usbd.h" +#include "supervisor/background_callback.h" #ifdef SOFTDEVICE_PRESENT #include "nrf_sdm.h" @@ -42,7 +43,9 @@ extern void tusb_hal_nrf_power_event(uint32_t event); void init_usb_hardware(void) { - // 2 is max priority (0, 1 are reserved for SD) + // 2 is max priority (0, 1, and 4 are reserved for SD) + // 5 is max priority that still allows calling SD functions such as + // sd_softdevice_is_enabled NVIC_SetPriority(USBD_IRQn, 2); // USB power may already be ready at this time -> no event generated @@ -89,5 +92,5 @@ void init_usb_hardware(void) { } void USBD_IRQHandler(void) { - tud_int_handler(0); + usb_irq_handler(); } diff --git a/ports/stm/supervisor/usb.c b/ports/stm/supervisor/usb.c index 3dd0acafd0..3d53fa3749 100644 --- a/ports/stm/supervisor/usb.c +++ b/ports/stm/supervisor/usb.c @@ -130,5 +130,5 @@ void init_usb_hardware(void) { } void OTG_FS_IRQHandler(void) { - tud_int_handler(0); + usb_irq_handler(); } diff --git a/supervisor/shared/usb/usb.c b/supervisor/shared/usb/usb.c index edf8101188..472be96d52 100644 --- a/supervisor/shared/usb/usb.c +++ b/supervisor/shared/usb/usb.c @@ -27,6 +27,7 @@ #include "py/objstr.h" #include "shared-bindings/microcontroller/Processor.h" #include "shared-module/usb_midi/__init__.h" +#include "supervisor/background_callback.h" #include "supervisor/port.h" #include "supervisor/usb.h" #include "lib/utils/interrupt_char.h" @@ -82,6 +83,16 @@ void usb_background(void) { } } +static background_callback_t callback; +static void usb_background_do(void* unused) { + usb_background(); +} + +void usb_irq_handler(void) { + tud_int_handler(0); + background_callback_add(&callback, usb_background_do, NULL); +} + //--------------------------------------------------------------------+ // tinyusb callbacks //--------------------------------------------------------------------+ diff --git a/supervisor/usb.h b/supervisor/usb.h index 29280c725b..1bed9bbb4b 100644 --- a/supervisor/usb.h +++ b/supervisor/usb.h @@ -33,6 +33,9 @@ // alive and responsive. void usb_background(void); +// Ports must call this from their particular USB IRQ handler +void usb_irq_handler(void); + // Only inits the USB peripheral clocks and pins. The peripheral will be initialized by // TinyUSB. void init_usb_hardware(void);