Skip to content

Commit 6ee916c

Browse files
committed
supervisor: factor out, Handle USB via background callback
1 parent 36b4646 commit 6ee916c

File tree

9 files changed

+30
-12
lines changed

9 files changed

+30
-12
lines changed

ports/atmel-samd/background.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ void run_background_tasks(void) {
8585
network_module_background();
8686
#endif
8787
filesystem_background();
88-
usb_background();
8988
running_background_tasks = false;
9089
assert_heap_ok();
9190

ports/atmel-samd/supervisor/usb.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "hpl/gclk/hpl_gclk_base.h"
3030
#include "hal_gpio.h"
3131
#include "lib/tinyusb/src/device/usbd.h"
32+
#include "supervisor/background_callback.h"
33+
#include "supervisor/usb.h"
3234

3335
void init_usb_hardware(void) {
3436
#ifdef SAMD21
@@ -61,24 +63,24 @@ void init_usb_hardware(void) {
6163

6264
#ifdef SAMD21
6365
void USB_Handler(void) {
64-
tud_int_handler(0);
66+
usb_irq_handler();
6567
}
6668
#endif
6769

6870
#ifdef SAM_D5X_E5X
6971
void USB_0_Handler (void) {
70-
tud_int_handler(0);
72+
usb_irq_handler();
7173
}
7274

7375
void USB_1_Handler (void) {
74-
tud_int_handler(0);
76+
usb_irq_handler();
7577
}
7678

7779
void USB_2_Handler (void) {
78-
tud_int_handler(0);
80+
usb_irq_handler();
7981
}
8082

8183
void USB_3_Handler (void) {
82-
tud_int_handler(0);
84+
usb_irq_handler();
8385
}
8486
#endif

ports/litex/mphalport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void isr(void) {
4949

5050
#ifdef CFG_TUSB_MCU
5151
if (irqs & (1 << USB_INTERRUPT))
52-
tud_int_handler(0);
52+
usb_irq_handler();
5353
#endif
5454
if (irqs & (1 << TIMER0_INTERRUPT))
5555
SysTick_Handler();

ports/mimxrt10xx/supervisor/usb.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "fsl_clock.h"
2929
#include "tusb.h"
30+
#include "supervisor/usb.h"
3031

3132
void init_usb_hardware(void) {
3233
CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
@@ -56,5 +57,5 @@ void init_usb_hardware(void) {
5657
}
5758

5859
void USB_OTG1_IRQHandler(void) {
59-
tud_int_handler(0);
60+
usb_irq_handler();
6061
}

ports/nrf/background.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ void run_background_tasks(void) {
5959
}
6060
running_background_tasks = true;
6161
filesystem_background();
62-
usb_background();
6362
#if CIRCUITPY_AUDIOPWMIO
6463
audiopwmout_background();
6564
#endif

ports/nrf/supervisor/usb.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "lib/utils/interrupt_char.h"
3131
#include "lib/mp-readline/readline.h"
3232
#include "lib/tinyusb/src/device/usbd.h"
33+
#include "supervisor/background_callback.h"
3334

3435
#ifdef SOFTDEVICE_PRESENT
3536
#include "nrf_sdm.h"
@@ -42,7 +43,9 @@ extern void tusb_hal_nrf_power_event(uint32_t event);
4243

4344
void init_usb_hardware(void) {
4445

45-
// 2 is max priority (0, 1 are reserved for SD)
46+
// 2 is max priority (0, 1, and 4 are reserved for SD)
47+
// 5 is max priority that still allows calling SD functions such as
48+
// sd_softdevice_is_enabled
4649
NVIC_SetPriority(USBD_IRQn, 2);
4750

4851
// USB power may already be ready at this time -> no event generated
@@ -89,5 +92,5 @@ void init_usb_hardware(void) {
8992
}
9093

9194
void USBD_IRQHandler(void) {
92-
tud_int_handler(0);
95+
usb_irq_handler();
9396
}

ports/stm/supervisor/usb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,5 @@ void init_usb_hardware(void) {
130130
}
131131

132132
void OTG_FS_IRQHandler(void) {
133-
tud_int_handler(0);
133+
usb_irq_handler();
134134
}

supervisor/shared/usb/usb.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "py/objstr.h"
2828
#include "shared-bindings/microcontroller/Processor.h"
2929
#include "shared-module/usb_midi/__init__.h"
30+
#include "supervisor/background_callback.h"
3031
#include "supervisor/port.h"
3132
#include "supervisor/usb.h"
3233
#include "lib/utils/interrupt_char.h"
@@ -82,6 +83,16 @@ void usb_background(void) {
8283
}
8384
}
8485

86+
static background_callback_t callback;
87+
static void usb_background_do(void* unused) {
88+
usb_background();
89+
}
90+
91+
void usb_irq_handler(void) {
92+
tud_int_handler(0);
93+
background_callback_add(&callback, usb_background_do, NULL);
94+
}
95+
8596
//--------------------------------------------------------------------+
8697
// tinyusb callbacks
8798
//--------------------------------------------------------------------+

supervisor/usb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
// alive and responsive.
3434
void usb_background(void);
3535

36+
// Ports must call this from their particular USB IRQ handler
37+
void usb_irq_handler(void);
38+
3639
// Only inits the USB peripheral clocks and pins. The peripheral will be initialized by
3740
// TinyUSB.
3841
void init_usb_hardware(void);

0 commit comments

Comments
 (0)