8000 shared/tinyusb: Schedule TinyUSB task function from dcd_event_handler. · ArduinoTM/micropython@2d363a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d363a2

Browse files
projectgusdpgeorge
authored andcommitted
shared/tinyusb: Schedule TinyUSB task function from dcd_event_handler.
dcd_event_handler() is called from the IRQ when a new DCD event is queued for processing by the TinyUSB thread mode task. This lets us queue the handler to run immediately when MicroPython resumes. Currently this relies on a linker --wrap hack to work, but a PR has been submitted to TinyUSB to allow the function to be called inline from dcd_event_handler() itself. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent bbc5a18 commit 2d363a2

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

shared/tinyusb/mp_usbd.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,40 @@
2727
#include <stdlib.h>
2828

2929
#include "py/mpconfig.h"
30+
#include "py/runtime.h"
3031

3132
#if MICROPY_HW_ENABLE_USBDEV
3233

3334
#ifndef NO_QSTR
3435
#include "tusb.h" // TinyUSB is not available when running the string preprocessor
36+
#include "device/dcd.h"
3537
#include "device/usbd.h"
3638
#include "device/usbd_pvt.h"
3739
#endif
3840

41+
// Legacy TinyUSB task function wrapper, called by some ports from the interpreter hook
3942
void usbd_task(void) {
4043
tud_task_ext(0, false);
4144
}
4245

46+
// TinyUSB task function wrapper, as scheduled from the USB IRQ
47+
static void mp_usbd_task(mp_sched_node_t *node);
48+
49+
extern void __real_dcd_event_handler(dcd_event_t const *event, bool in_isr);
50+
51+
// If -Wl,--wrap=dcd_event_handler is passed to the linker, then this wrapper
52+
// will be called and allows MicroPython to schedule the TinyUSB task when
53+
// dcd_event_handler() is called from an ISR.
54+
TU_ATTR_FAST_FUNC void __wrap_dcd_event_handler(dcd_event_t const *event, bool in_isr) {
55+
static mp_sched_node_t usbd_task_node;
56+
57+
__real_dcd_event_handler(event, in_isr);
58+
mp_sched_schedule_node(&usbd_task_node, mp_usbd_task);
59+
}
60+
61+
static void mp_usbd_task(mp_sched_node_t *node) {
62+
(void)node;
63+
tud_task_ext(0, false);
64+
}
65+
4366
#endif

shared/tinyusb/mp_usbd.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929

3030
#include "py/obj.h"
3131

32-
// Call instead of tud_task()
33-
void mp_usbd_task(void);
34-
3532
// Function to be implemented in port code.
3633
// Can write a string up to MICROPY_HW_USB_DESC_STR_MAX characters long, plus terminating byte.
3734
extern void mp_usbd_port_get_serial_number(char *buf);

0 commit comments

Comments
 (0)
0