Skip to content

Commit 2d33071

Browse files
andrewleechdpgeorge
authored andcommitted
renesas-ra/mphalport: Refactor to use shared TinyUSB CDC functions.
Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent 2475a52 commit 2d33071

File tree

2 files changed

+8
-69
lines changed

2 files changed

+8
-69
lines changed

ports/renesas-ra/mphalport.c

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "py/ringbuf.h"
3535
#include "extmod/misc.h"
3636
#include "shared/runtime/interrupt_char.h"
37+
#include "shared/tinyusb/mp_usbd_cdc.h"
3738
#include "tusb.h"
3839
#include "uart.h"
3940

@@ -64,62 +65,16 @@ NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
6465
mp_raise_OSError(mp_hal_status_to_errno_table[status]);
6566
}
6667

67-
#if MICROPY_HW_USB_CDC
68-
6968
uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll
7069

71-
void poll_cdc_interfaces(void) {
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-
10670
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
10771
uintptr_t ret = 0;
10872
#if MICROPY_HW_USB_CDC
109-
poll_cdc_interfaces();
73+
ret |= mp_usbd_cdc_poll_interfaces(poll_flags);
11074
#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-
}
75+
#if MICROPY_HW_ENABLE_UART_REPL
11576
if (poll_flags & MP_STREAM_POLL_WR) {
116-
#if MICROPY_HW_ENABLE_UART_REPL
11777
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
12378
}
12479
#endif
12580
#if MICROPY_PY_OS_DUPTERM
@@ -132,7 +87,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
13287
int mp_hal_stdin_rx_chr(void) {
13388
for (;;) {
13489
#if MICROPY_HW_USB_CDC
135-
poll_cdc_interfaces();
90+
mp_usbd_cdc_poll_interfaces(0);
13691
#endif
13792

13893
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
@@ -165,27 +120,10 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
165120
#endif
166121

167122
#if MICROPY_HW_USB_CDC
168-
if (tud_cdc_connected()) {
169-
size_t i = 0;
170-
while (i < len) {
171-
uint32_t n = len - i;
172-
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
173-
n = CFG_TUD_CDC_EP_BUFSIZE;
174-
}
175-
int timeout = 0;
176-
// Wait with a max of USC_CDC_TIMEOUT ms
177-
while (n > tud_cdc_write_available() && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) {
178-
MICROPY_EVENT_POLL_HOOK
179-
}
180-
if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) {
181-
break;
182-
}
183-
uint32_t n2 = tud_cdc_write(str + i, n);
184-
tud_cdc_write_flush();
185-
i += n2;
186-
}
187-
ret = MIN(i, ret);
123+
mp_uint_t cdc_res = mp_usbd_cdc_tx_strn(str, len);
124+
if (cdc_res > 0) {
188125
did_write = true;
126+
ret = MIN(cdc_res, ret);
189127
}
190128
#endif
191129

ports/renesas-ra/mphalport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#define MICROPY_HW_USB_CDC_TX_TIMEOUT (500)
4343

4444
extern const unsigned char mp_hal_status_to_errno_table[4];
45+
extern int mp_interrupt_char;
4546
extern ringbuf_t stdin_ringbuf;
4647

4748
static inline int mp_hal_status_to_neg_errno(HAL_StatusTypeDef status) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy