27
27
#if defined ARDUINO_ARCH_RP2040 && TUSB_OPT_DEVICE_ENABLED
28
28
29
29
#include " Arduino.h"
30
+
31
+ // mbed old pico-sdk need to wrap with cpp
32
+ extern " C" {
30
33
#include " hardware/irq.h"
31
34
#include " pico/bootrom.h"
32
35
#include " pico/mutex.h"
33
36
#include " pico/time.h"
34
- #include " pico/unique_id.h"
37
+ #include " hardware/flash.h"
38
+ }
35
39
36
40
#include " arduino/Adafruit_TinyUSB_API.h"
37
41
#include " tusb.h"
38
42
43
+ // USB processing will be a periodic timer task
44
+ #define USB_TASK_INTERVAL 1000
45
+ #define USB_TASK_IRQ 31
46
+
39
47
// --------------------------------------------------------------------+
40
48
// Forward USB interrupt events to TinyUSB IRQ Handler
41
- // rp2040 implementation will install approriate handler when initializing
49
+ // rp2040 implementation will install appropriate handler when initializing
42
50
// tinyusb. There is no need to forward IRQ from application
43
51
// --------------------------------------------------------------------+
44
52
45
53
// --------------------------------------------------------------------+
46
- // Porting API
54
+ // Earle Philhower and mbed specific
47
55
// --------------------------------------------------------------------+
48
56
49
- // USB processing will be a periodic timer task
50
- #define USB_TASK_INTERVAL 1000
51
- #define USB_TASK_IRQ 31
57
+ // mbed use old pico-sdk does not have unique_id
58
+ #ifdef ARDUINO_ARCH_MBED
59
+
60
+ #include " mbed.h"
61
+ static mbed::Ticker _usb_ticker;
62
+
63
+ #define get_unique_id (_serial ) flash_get_unique_id(_serial)
64
+
65
+ static void ticker_task (void )
66
+ {
67
+ irq_set_pending (USB_TASK_IRQ);
68
+ }
69
+
70
+ static void setup_periodic_usb_hanlder (uint64_t us)
71
+ {
72
+ _usb_ticker.attach (ticker_task, (std::chrono::microseconds) us);
73
+ }
74
+
75
+ #else
76
+
77
+ #include " pico/unique_id.h"
78
+
79
+ #define get_unique_id (_serial ) pico_get_unique_board_id((pico_unique_board_id_t *)_serial);
80
+
81
+ static int64_t timer_task (__unused alarm_id_t id, __unused void *user_data) {
82
+ irq_set_pending (USB_TASK_IRQ);
83
+ return USB_TASK_INTERVAL;
84
+ }
85
+
86
+ static void setup_periodic_usb_hanlder (uint64_t us)
87
+ {
88
+ add_alarm_in_us (us, timer_task, NULL , true );
89
+ }
90
+
91
+ #endif // mbed
92
+
93
+ // --------------------------------------------------------------------+
94
+ // Porting API
95
+ // --------------------------------------------------------------------+
52
96
53
97
// Big, global USB mutex, shared with all USB devices to make sure we don't
54
98
// have multiple cores updating the TUSB state in parallel
@@ -64,21 +108,23 @@ static void usb_irq() {
64
108
}
65
109
}
66
110
67
- static int64_t timer_task (__unused alarm_id_t id, __unused void *user_data) {
68
- irq_set_pending (USB_TASK_IRQ);
69
- return USB_TASK_INTERVAL;
70
- }
71
-
72
111
void TinyUSB_Port_InitDevice (uint8_t rhport) {
73
112
(void )rhport;
74
113
75
114
mutex_init (&__usb_mutex);
115
+
116
+ irq_set_enabled (USBCTRL_IRQ, false );
117
+ irq_handler_t current_handler = irq_get_exclusive_handler (USBCTRL_IRQ);
118
+ if (current_handler) {
119
+ irq_remove_handler (USBCTRL_IRQ, current_handler);
120
+ }
121
+
76
122
tusb_init ();
77
123
124
+ // soft irq for periodically task runner
78
125
irq_set_exclusive_handler (USB_TASK_IRQ, usb_irq);
79
126
irq_set_enabled (USB_TASK_IRQ, true );
80
-
81
- add_alarm_in_us (USB_TASK_INTERVAL, timer_task, NULL , true );
127
+ setup_periodic_usb_hanlder (USB_TASK_INTERVAL);
82
128
}
83
129
84
130
void TinyUSB_Port_EnterDFU (void ) {
@@ -88,8 +134,8 @@ void TinyUSB_Port_EnterDFU(void) {
88
134
}
89
135
90
136
uint8_t TinyUSB_Port_GetSerialNumber (uint8_t serial_id[16 ]) {
91
- pico_get_unique_board_id (( pico_unique_board_id_t *) serial_id);
92
- return PICO_UNIQUE_BOARD_ID_SIZE_BYTES ;
137
+ get_unique_id ( serial_id);
138
+ return FLASH_UNIQUE_ID_SIZE_BYTES ;
93
139
}
94
140
95
141
// --------------------------------------------------------------------+
@@ -109,4 +155,4 @@ void TinyUSB_Device_Task(void) {
109
155
}
110
156
}
111
157
112
- #endif // USE_TINYUSB
158
+ #endif
0 commit comments