@@ -40,9 +40,6 @@ extern "C" {
40
40
#include " arduino/Adafruit_TinyUSB_API.h"
41
41
#include " tusb.h"
42
42
43
- // USB processing will be a periodic timer task
44
- #define USB_TASK_INTERVAL 1000
45
-
46
43
// SDK >= 1.4.0 need to dynamically request the IRQ to avoid conflicts
47
44
#if (PICO_SDK_VERSION_MAJOR * 100 + PICO_SDK_VERSION_MINOR) < 104
48
45
#define USB_TASK_IRQ 31
@@ -63,34 +60,15 @@ static unsigned int USB_TASK_IRQ;
63
60
// mbed use old pico-sdk does not have unique_id
64
61
#ifdef ARDUINO_ARCH_MBED
65
62
66
- #include " mbed.h"
67
- static mbed::Ticker _usb_ticker;
68
-
69
63
#define get_unique_id (_serial ) flash_get_unique_id(_serial)
70
64
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
-
77
65
#else
78
66
79
67
#include " pico/unique_id.h"
80
-
81
68
#define get_unique_id (_serial ) \
82
69
pico_get_unique_board_id ((pico_unique_board_id_t *)_serial);
83
70
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
94
72
95
73
// --------------------------------------------------------------------+
96
74
// Porting API
@@ -100,7 +78,7 @@ static void setup_periodic_usb_hanlder(uint64_t us) {
100
78
// have multiple cores updating the TUSB state in parallel
101
79
mutex_t __usb_mutex;
102
80
103
- static void usb_irq ( ) {
81
+ static void usb_task_irq ( void ) {
104
82
// if the mutex is already owned, then we are in user code
105
83
// in this file which will do a tud_task itself, so we'll just do nothing
106
84
// until the next tick; we won't starve
@@ -110,26 +88,24 @@ static void usb_irq() {
110
88
}
111
89
}
112
90
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 ) { irq_set_pending (USB_TASK_IRQ); }
115
93
94
+ void TinyUSB_Port_InitDevice (uint8_t rhport) {
116
95
mutex_init (&__usb_mutex);
117
96
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 ();
97
+ tud_init (rhport);
125
98
126
- // soft irq for periodically task runner
99
+ // soft irq for task runner
127
100
#if (PICO_SDK_VERSION_MAJOR * 100 + PICO_SDK_VERSION_MINOR) >= 104
128
101
USB_TASK_IRQ = user_irq_claim_unused (true );
129
102
#endif
130
- irq_set_exclusive_handler (USB_TASK_IRQ, usb_irq );
103
+ irq_set_exclusive_handler (USB_TASK_IRQ, usb_task_irq );
131
104
irq_set_enabled (USB_TASK_IRQ, true );
132
- setup_periodic_usb_hanlder (USB_TASK_INTERVAL);
105
+
106
+ // add shared irq to trigger task runner
107
+ irq_add_shared_handler (USBCTRL_IRQ, usb_task_trigger_irq,
108
+ PICO_SHARED_IRQ_HANDLER_LOWEST_ORDER_PRIORITY);
133
109
}
134
110
135
111
void TinyUSB_Port_EnterDFU (void ) {
0 commit comments