Skip to content

Commit 938716c

Browse files
authored
Merge pull request #11114 from mikeysklar/tusb-time-millis-api-ram
raspberrypi: make tusb_time_millis_api RAM-resident; core1 calls it
2 parents 50f1bd3 + 73a51c4 commit 938716c

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

  • ports/raspberrypi/supervisor
  • supervisor/shared/usb

ports/raspberrypi/supervisor/usb.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,30 @@
66

77
#include "lib/tinyusb/src/device/usbd.h"
88
#include "supervisor/background_callback.h"
9+
#include "supervisor/linker.h"
910
#include "supervisor/usb.h"
1011
#include "hardware/irq.h"
12+
#include "hardware/timer.h"
1113
#include "pico/platform.h"
1214
#include "hardware/regs/intctrl.h"
1315

1416
void init_usb_hardware(void) {
1517
}
1618

19+
// Override the shared implementation with one that is safe to call from core1.
20+
// TinyUSB's tuh_task_event_ready() calls this, and usb_host polls
21+
// tuh_task_event_ready() from core1's PIO-USB frame loop (see
22+
// common-hal/usb_host/Port.c). tuh_task_event_ready() is deliberately placed in
23+
// RAM by link-rp2*.ld, and everything it calls must be RAM-resident too: on
24+
// RP2350, core1 sets an MPU region that makes flash execute-never, and on
25+
// RP2040 flash may be unavailable while core0 writes to it. time_us_32() is a
26+
// static-inline register read. >>10 yields ~1.024 ms units rather than exact
27+
// milliseconds, which is fine because TinyUSB only uses this API for relative
28+
// delay arithmetic and every use goes through this same function.
29+
uint32_t PLACE_IN_ITCM(tusb_time_millis_api)(void) {
30+
return time_us_32() >> 10;
31+
}
32+
1733
static void _usb_irq_wrapper(void) {
1834
usb_irq_handler(0);
1935
}

supervisor/shared/usb/usb.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ void usb_background(void) {
184184
}
185185
}
186186

187-
uint32_t tusb_time_millis_api(void) {
187+
// Ports may override this with a RAM-resident version when TinyUSB code that
188+
// calls it must not execute from flash (e.g. raspberrypi polls
189+
// tuh_task_event_ready(), which calls this, from core1).
190+
MP_WEAK uint32_t tusb_time_millis_api(void) {
188191
return supervisor_ticks_ms32();
189192
}
190193

0 commit comments

Comments
 (0)