Skip to content

Commit 779dc3f

Browse files
committed
update rp2040 port
1 parent 124763e commit 779dc3f

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

examples/Composite/mouse_ramdisk/mouse_ramdisk.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ uint8_t const desc_hid_report[] =
3131
Adafruit_USBD_HID usb_hid;
3232
Adafruit_USBD_MSC usb_msc;
3333

34-
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
34+
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS || defined ARDUINO_NRF52840_CIRCUITPLAY
3535
const int pin = 4; // Left Button
3636
bool activeState = true;
37-
#elif defined ARDUINO_NRF52840_FEATHER
38-
const int pin = 7; // UserSw
37+
#elif defined PIN_BUTTON1
38+
const int pin = PIN_BUTTON1;
3939
bool activeState = false;
4040
#else
4141
const int pin = 12;
@@ -128,4 +128,4 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
128128
void msc_flush_cb (void)
129129
{
130130
// nothing to do
131-
}
131+
}

src/arduino/Adafruit_TinyUSB_API.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ void TinyUSB_Device_Init(uint8_t rhport)
3939
USBDevice.begin(rhport);
4040
}
4141

42+
// RP2040 has its own implementation since it needs mutex for dual core
43+
#ifndef ARDUINO_ARCH_RP2040
4244
void TinyUSB_Device_Task(void)
4345
{
44-
USBDevice.task();
46+
tud_task();
4547
}
48+
#endif
4649

4750
void TinyUSB_Device_FlushCDC(void)
4851
{

src/arduino/Adafruit_TinyUSB_API.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void TinyUSB_Device_FlushCDC(void) __attribute__((weak));
6161
void TinyUSB_Port_EnterDFU(void);
6262

6363
// Init device hardware.
64-
// Called by USBDevice.begin()
64+
// Called by TinyUSB_Device_Init()
6565
void TinyUSB_Port_InitDevice(uint8_t rhport);
6666

6767
// Get unique serial number, needed for Serial String Descriptor

src/arduino/ports/Adafruit_TinyUSB_rp2040.cpp

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@
2727
#if defined ARDUINO_ARCH_RP2040 & TUSB_OPT_DEVICE_ENABLED
2828

2929
#include "Arduino.h"
30-
#include "arduino/Adafruit_USBD_Device.h"
31-
3230
#include "pico/bootrom.h"
31+
#include "pico/time.h"
32+
#include "hardware/irq.h"
33+
#include "pico/mutex.h"
3334
#include "pico/unique_id.h"
3435

36+
#include "tusb.h"
37+
#include "arduino/Adafruit_TinyUSB_API.h"
38+
3539
//--------------------------------------------------------------------+
3640
// Forward USB interrupt events to TinyUSB IRQ Handler
3741
// rp2040 implementation will install approriate handler when initializing
@@ -42,12 +46,40 @@
4246
// Porting API
4347
//--------------------------------------------------------------------+
4448

49+
// USB processing will be a periodic timer task
50+
#define USB_TASK_INTERVAL 1000
51+
#define USB_TASK_IRQ 31
52+
53+
// Big, global USB mutex, shared with all USB devices to make sure we don't
54+
// have multiple cores updating the TUSB state in parallel
55+
mutex_t __usb_mutex;
56+
57+
static void usb_irq() {
58+
// if the mutex is already owned, then we are in user code
59+
// in this file which will do a tud_task itself, so we'll just do nothing
60+
// until the next tick; we won't starve
61+
if (mutex_try_enter(&__usb_mutex, NULL)) {
62+
tud_task();
63+
mutex_exit(&__usb_mutex);
64+
}
65+
}
66+
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+
4572
void TinyUSB_Port_InitDevice(uint8_t rhport)
4673
{
4774
(void) rhport;
4875

49-
// no specific hardware initialization
50-
// TOOD maybe set up sdtio usb
76+
mutex_init(&__usb_mutex);
77+
tusb_init();
78+
79+
irq_set_exclusive_handler(USB_TASK_IRQ, usb_irq);
80+
irq_set_enabled(USB_TASK_IRQ, true);
81+
82+
add_alarm_in_us(USB_TASK_INTERVAL, timer_task, NULL, true);
5183
}
5284

5385
void TinyUSB_Port_EnterDFU(void)
@@ -62,4 +94,25 @@ uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16])
6294
return PICO_UNIQUE_BOARD_ID_SIZE_BYTES;
6395
}
6496

97+
//--------------------------------------------------------------------+
98+
// Core API
99+
// Implement Core API since rp2040 need mutex for calling tud_task in
100+
// IRQ context
101+
//--------------------------------------------------------------------+
102+
103+
extern "C"
104+
{
105+
106+
void TinyUSB_Device_Task(void)
107+
{
108+
// Since tud_task() is also invoked in ISR, we need to get the mutex first
109+
if (mutex_try_enter(&__usb_mutex, NULL))
110+
{
111+
tud_task();
112+
mutex_exit(&__usb_mutex);
113+
}
114+
}
115+
116+
}
117+
65118
#endif // USE_TINYUSB

0 commit comments

Comments
 (0)