Skip to content

Commit 2475a52

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

File tree

4 files changed

+10
-66
lines changed

4 files changed

+10
-66
lines changed

ports/mimxrt/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ SHARED_SRC_C += \
232232
shared/runtime/stdout_helpers.c \
233233
shared/runtime/sys_stdio_mphal.c \
234234
shared/timeutils/timeutils.c \
235+
shared/tinyusb/mp_usbd.c \
236+
shared/tinyusb/mp_usbd_cdc.c \
235237

236238
# Set flash driver name, base address and internal flash flag, based on the flash type.
237239
ifeq ($(MICROPY_HW_FLASH_TYPE),$(filter $(MICROPY_HW_FLASH_TYPE),qspi_nor_flash))

ports/mimxrt/mpconfigport.h

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ uint32_t trng_random_u32(void);
148148
#endif
149149

150150
#define MICROPY_HW_ENABLE_USBDEV (1)
151+
#define MICROPY_HW_USB_CDC (1)
151152

152153
// Hooks to add builtins
153154

ports/mimxrt/mphalport.c

+6-66
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "py/mphal.h"
3131
#include "shared/timeutils/timeutils.h"
3232
#include "shared/runtime/interrupt_char.h"
33+
#include "shared/tinyusb/mp_usbd_cdc.h"
3334
#include "extmod/misc.h"
3435
#include "ticks.h"
3536
#include "tusb.h"
@@ -44,51 +45,9 @@
4445
static uint8_t stdin_ringbuf_array[MICROPY_HW_STDIN_BUFFER_LEN];
4546
ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0};
4647

47-
uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll
48-
49-
void poll_cdc_interfaces(void) {
50-
// any CDC interfaces left to poll?
51-
if (cdc_itf_pending && ringbuf_free(&stdin_ringbuf)) {
52-
for (uint8_t itf = 0; itf < 8; ++itf) {
53-
if (cdc_itf_pending & (1 << itf)) {
54-
tud_cdc_rx_cb(itf);
55-
if (!cdc_itf_pending) {
56-
break;
57-
}
58-
}
59-
}
60-
}
61-
}
62-
63-
64-
void tud_cdc_rx_cb(uint8_t itf) {
65-
// consume pending USB data immediately to free usb buffer and keep the endpoint from stalling.
66-
// in case the ringbuffer is full, mark the CDC interface that need attention later on for polling
67-
cdc_itf_pending &= ~(1 << itf);
68-
for (uint32_t bytes_avail = tud_cdc_n_available(itf); bytes_avail > 0; --bytes_avail) {
69-
if (ringbuf_free(&stdin_ringbuf)) {
70-
int data_char = tud_cdc_read_char();
71-
if (data_char == mp_interrupt_char) {
72-
mp_sched_keyboard_interrupt();
73-
} else {
74-
ringbuf_put(&stdin_ringbuf, data_char);
75-
}
76-
} else {
77-
cdc_itf_pending |= (1 << itf);
78-
return;
79-
}
80-
}
81-
}
82-
8348
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
8449
uintptr_t ret = 0;
85-
poll_cdc_interfaces();
86-
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
87-
ret |= MP_STREAM_POLL_RD;
88-
}
89-
if ((poll_flags & MP_STREAM_POLL_WR) && tud_cdc_connected() && tud_cdc_write_available() > 0) {
90-
ret |= MP_STREAM_POLL_WR;
91-
}
50+
ret |= mp_usbd_cdc_poll_interfaces(poll_flags);
9251
#if MICROPY_PY_OS_DUPTERM
9352
ret |= mp_os_dupterm_poll(poll_flags);
9453
#endif
@@ -97,7 +56,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
9756

9857
int mp_hal_stdin_rx_chr(void) {
9958
for (;;) {
100-
poll_cdc_interfaces();
59+
mp_usbd_cdc_poll_interfaces(0);
10160
int c = ringbuf_get(&stdin_ringbuf);
10261
if (c != -1) {
10362
return c;
@@ -115,29 +74,10 @@ int mp_hal_stdin_rx_chr(void) {
11574
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
11675
mp_uint_t ret = len;
11776
bool did_write = false;
118-
if (tud_cdc_connected()) {
119-
size_t i = 0;
120-
while (i < len) {
121-
uint32_t n = len - i;
122-
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
123-
n = CFG_TUD_CDC_EP_BUFSIZE;
124-
}
125-
uint64_t timeout = ticks_us64() + (uint64_t)(MICROPY_HW_USB_CDC_TX_TIMEOUT * 1000);
126-
// Wait with a max of USC_CDC_TIMEOUT ms
127-
while (n > tud_cdc_write_available() && ticks_us64() < timeout) {
128-
MICROPY_EVENT_POLL_HOOK
129-
}
130-
if (ticks_us64() >= timeout) {
131-
ret = i;
132-
break;
133-
}
134-
135-
uint32_t n2 = tud_cdc_write(str + i, n);
136-
tud_cdc_write_flush();
137-
i += n2;
138-
}
77+
mp_uint_t cdc_res = mp_usbd_cdc_tx_strn(str, len);
78+
if (cdc_res > 0) {
13979
did_write = true;
140-
ret = MIN(i, ret);
80+
ret = MIN(cdc_res, ret);
14181
}
14282
#if MICROPY_PY_OS_DUPTERM
14383
int dupterm_res = mp_os_dupterm_tx_strn(str, len);

ports/mimxrt/mphalport.h

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#define MP_HAL_PIN_TRIGGER_RISE kGPIO_IntRisingEdge
7373
#define MP_HAL_PIN_TRIGGER_RISE_FALL kGPIO_IntRisingOrFallingEdge
7474

75+
extern int mp_interrupt_char;
7576
extern ringbuf_t stdin_ringbuf;
7677

7778
// Define an alias for systick_ms, because the shared softtimer.c uses

0 commit comments

Comments
 (0)