8000 rp2/mphalport: Run TinyUSB stack while waiting for CDC input/output. · micropython/micropython@ddf737c · GitHub
[go: up one dir, main page]

Skip to content

Commit ddf737c

Browse files
committed
rp2/mphalport: Run TinyUSB stack while waiting for CDC input/output.
The recent change in bcbdee2 meant that TinyUSB can't no longer be run from within a soft (or hard) IRQ handler, ie when the scheduler is locked. That means that Python code that calls `print(...)` from within a scheduled function may block indefinitely if the USB CDC buffers are full. This commit fixes that problem by explicitly running the TinyUSB stack when waiting within stdio tx/rx functions. Signed-off-by: Damien George <damien@micropython.org>
1 parent f3bbedb commit ddf737c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

ports/rp2/mphalport.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "extmod/misc.h"
3131
#include "shared/runtime/interrupt_char.h"
3232
#include "shared/timeutils/timeutils.h"
33+
#include "shared/tinyusb/mp_usbd.h"
3334
#include "tusb.h"
3435
#include "uart.h"
3536
#include "hardware/rtc.h"
@@ -54,6 +55,19 @@ ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array) };
5455

5556
#endif
5657

58+
#if MICROPY_HW_USB_CDC
59+
// Explicitly run the USB stack in case the scheduler is locked (eg we are in an
60+
// interrupt handler) and there is in/out data pending on the USB CDC interface.
61+
#define MICROPY_EVENT_POLL_HOOK_WITH_USB \
62+
do { \
63+
MICROPY_EVENT_POLL_HOOK; \
64+
mp_usbd_task(); \
65+
} while (0)
66+
67+
#else
68+
#define MICROPY_EVENT_POLL_HOOK_WITH_USB MICROPY_EVENT_POLL_HOOK
69+
#endif
70+
5771
#if MICROPY_HW_USB_CDC
5872

5973
uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll
@@ -135,7 +149,7 @@ int mp_hal_stdin_rx_chr(void) {
135149
return dupterm_c;
136150
}
137151
#endif
138-
MICROPY_EVENT_POLL_HOOK
152+
MICROPY_EVENT_POLL_HOOK_WITH_USB;
139153
}
140154
}
141155

@@ -155,7 +169,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
155169
int timeout = 0;
156170
// Wait with a max of USC_CDC_TIMEOUT ms
157171
while (n > tud_cdc_write_available() && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) {
158-
MICROPY_EVENT_POLL_HOOK
172+
MICROPY_EVENT_POLL_HOOK_WITH_USB;
159173
}
160174
if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) {
161175
break;

0 commit comments

Comments
 (0)
0