Skip to content

Commit 292611d

Browse files
committed
use shared usb irq to trigger task runner
fix #187
1 parent 5ff7bd7 commit 292611d

File tree

1 file changed

+13
-36
lines changed

1 file changed

+13
-36
lines changed

src/arduino/ports/rp2040/Adafruit_TinyUSB_rp2040.cpp

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ extern "C" {
4040
#include "arduino/Adafruit_TinyUSB_API.h"
4141
#include "tusb.h"
4242

43-
// USB processing will be a periodic timer task
44-
#define USB_TASK_INTERVAL 1000
45-
4643
// SDK >= 1.4.0 need to dynamically request the IRQ to avoid conflicts
4744
#if (PICO_SDK_VERSION_MAJOR * 100 + PICO_SDK_VERSION_MINOR) < 104
4845
#define USB_TASK_IRQ 31
@@ -63,34 +60,15 @@ static unsigned int USB_TASK_IRQ;
6360
// mbed use old pico-sdk does not have unique_id
6461
#ifdef ARDUINO_ARCH_MBED
6562

66-
#include "mbed.h"
67-
static mbed::Ticker _usb_ticker;
68-
6963
#define get_unique_id(_serial) flash_get_unique_id(_serial)
7064

71-
static void ticker_task(void) { irq_set_pending(USB_TASK_IRQ); }
72-
73-
static void setup_periodic_usb_hanlder(uint64_t us) {
74-
_usb_ticker.attach(ticker_task, (std::chrono::microseconds)us);
75-
}
76-
7765
#else
7866

7967
#include "pico/unique_id.h"
80-
8168
#define get_unique_id(_serial) \
8269
pico_get_unique_board_id((pico_unique_board_id_t *)_serial);
8370

84-
static int64_t timer_task(__unused alarm_id_t id, __unused void *user_data) {
85-
irq_set_pending(USB_TASK_IRQ);
86-
return USB_TASK_INTERVAL;
87-
}
88-
89-
static void setup_periodic_usb_hanlder(uint64_t us) {
90-
add_alarm_in_us(us, timer_task, NULL, true);
91-
}
92-
93-
#endif // mbed
71+
#endif
9472

9573
//--------------------------------------------------------------------+
9674
// Porting API
@@ -100,7 +78,7 @@ static void setup_periodic_usb_hanlder(uint64_t us) {
10078
// have multiple cores updating the TUSB state in parallel
10179
mutex_t __usb_mutex;
10280

103-
static void usb_irq() {
81+
static void usb_task_irq(void) {
10482
// if the mutex is already owned, then we are in user code
10583
// in this file which will do a tud_task itself, so we'll just do nothing
10684
// until the next tick; we won't starve
@@ -110,26 +88,25 @@ static void usb_irq() {
11088
}
11189
}
11290

113-
void TinyUSB_Port_InitDevice(uint8_t rhport) {
114-
(void)rhport;
91+
// invoked when there is hardware usb irq, trigger task runner later
92+
static void usb_task_trigger_irq(void) {
93+
irq_set_pending(USB_TASK_IRQ);
94+
}
11595

96+
void TinyUSB_Port_InitDevice(uint8_t rhport) {
11697
mutex_init(&__usb_mutex);
11798

118-
irq_set_enabled(USBCTRL_IRQ, false);
119-
irq_handler_t current_handler = irq_get_exclusive_handler(USBCTRL_IRQ);
120-
if (current_handler) {
121-
irq_remove_handler(USBCTRL_IRQ, current_handler);
122-
}
123-
124-
tusb_init();
99+
tud_init(rhport);
125100

126-
// soft irq for periodically task runner
101+
// soft irq for task runner
127102
#if (PICO_SDK_VERSION_MAJOR * 100 + PICO_SDK_VERSION_MINOR) >= 104
128103
USB_TASK_IRQ = user_irq_claim_unused(true);
129104
#endif
130-
irq_set_exclusive_handler(USB_TASK_IRQ, usb_irq);
105+
irq_set_exclusive_handler(USB_TASK_IRQ, usb_task_irq);
131106
irq_set_enabled(USB_TASK_IRQ, true);
132-
setup_periodic_usb_hanlder(USB_TASK_INTERVAL);
107+
108+
// add shared irq to trigger task runner
109+
irq_add_shared_handler(USBCTRL_IRQ, usb_task_trigger_irq, PICO_SHARED_IRQ_HANDLER_LOWEST_ORDER_PRIORITY);
133110
}
134111

135112
void TinyUSB_Port_EnterDFU(void) {

0 commit comments

Comments
 (0)