Skip to content

Commit 1eaa562

Browse files
andrewleechdpgeorge
authored andcommitted
rp2/mphalport: Refactor to use shared TinyUSB CDC functions.
Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent c98789a commit 1eaa562

File tree

1 file changed

+7
-82
lines changed

1 file changed

+7
-82
lines changed

ports/rp2/mphalport.c

+7-82
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "shared/runtime/softtimer.h"
3333
#include "shared/timeutils/timeutils.h"
3434
#include "shared/tinyusb/mp_usbd.h"
35+
#include "shared/tinyusb/mp_usbd_cdc.h"
3536
#include "pendsv.h"
3637
#include "tusb.h"
3738
#include "uart.h"
@@ -58,68 +59,14 @@ ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array) };
5859

5960
#endif
6061

61-
#if MICROPY_HW_USB_CDC
62-
63-
uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll
64-
65-
void poll_cdc_interfaces(void) {
66-
if (!cdc_itf_pending) {
67-
// Explicitly run the USB stack as the scheduler may be locked (eg we are in
68-
// an interrupt handler) while there is data pending.
69-
mp_usbd_task();
70-
}
71-
72-
// any CDC interfaces left to poll?
73-
if (cdc_itf_pending && ringbuf_free(&stdin_ringbuf)) {
74-
for (uint8_t itf = 0; itf < 8; ++itf) {
75-
if (cdc_itf_pending & (1 << itf)) {
76-
tud_cdc_rx_cb(itf);
77-
if (!cdc_itf_pending) {
78-
break;
79-
}
80-
}
81-
}
82-
}
83-
}
84-
85-
void tud_cdc_rx_cb(uint8_t itf) {
86-
// consume pending USB data immediately to free usb buffer and keep the endpoint from stalling.
87-
// in case the ringbuffer is full, mark the CDC interface that need attention later on for polling
88-
cdc_itf_pending &= ~(1 << itf);
89-
for (uint32_t bytes_avail = tud_cdc_n_available(itf); bytes_avail > 0; --bytes_avail) {
90-
if (ringbuf_free(&stdin_ringbuf)) {
91-
int data_char = tud_cdc_read_char();
92-
if (data_char == mp_interrupt_char) {
93-
mp_sched_keyboard_interrupt();
94-
} else {
95-
ringbuf_put(&stdin_ringbuf, data_char);
96-
}
97-
} else {
98-
cdc_itf_pending |= (1 << itf);
99-
return;
100-
}
101-
}
102-
}
103-
104-
#endif
105-
10662
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
10763
uintptr_t ret = 0;
10864
#if MICROPY_HW_USB_CDC
109-
poll_cdc_interfaces();
65+
ret |= mp_usbd_cdc_poll_interfaces(poll_flags);
11066
#endif
111-
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC
112-
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
113-
ret |= MP_STREAM_POLL_RD;
114-
}
67+
#if MICROPY_HW_ENABLE_UART_REPL
11568
if (poll_flags & MP_STREAM_POLL_WR) {
116-
#if MICROPY_HW_ENABLE_UART_REPL
11769
ret |= MP_STREAM_POLL_WR;
118-
#else
119-
if (tud_cdc_connected() && tud_cdc_write_available() > 0) {
120-
ret |= MP_STREAM_POLL_WR;
121-
}
122-
#endif
12370
}
12471
#endif
12572
#if MICROPY_PY_OS_DUPTERM
@@ -132,7 +79,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
13279
int mp_hal_stdin_rx_chr(void) {
13380
for (;;) {
13481
#if MICROPY_HW_USB_CDC
135-
poll_cdc_interfaces();
82+
mp_usbd_cdc_poll_interfaces(0);
13683
#endif
13784

13885
int c = ringbuf_get(&stdin_ringbuf);
@@ -159,32 +106,10 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
159106
#endif
160107

161108
#if MICROPY_HW_USB_CDC
162-
if (tud_cdc_connected()) {
163-
size_t i = 0;
164-
while (i < len) {
165-
uint32_t n = len - i;
166-
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
167-
n = CFG_TUD_CDC_EP_BUFSIZE;
168-
}
169-
int timeout = 0;
170-
// Wait with a max of USC_CDC_TIMEOUT ms
171-
while (n > tud_cdc_write_available() && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) {
172-
mp_event_wait_ms(1);
173-
174-
// Explicitly run the USB stack as the scheduler may be locked (eg we
175-
// are in an interrupt handler), while there is data pending.
176-
mp_usbd_task();
177-
}
178-
if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) {
179-
ret = i;
180-
break;
181-
}
182-
uint32_t n2 = tud_cdc_write(str + i, n);
183-
tud_cdc_write_flush();
184-
i += n2;
185-
}
186-
ret = MIN(i, ret);
109+
mp_uint_t cdc_res = mp_usbd_cdc_tx_strn(str, len);
110+
if (cdc_res > 0) {
187111
did_write = true;
112+
ret = MIN(cdc_res, ret);
188113
}
189114
#endif
190115

0 commit comments

Comments
 (0)