mimxrt/mphalport: Fix USB CDC RX handling to not block when unprocessed.

Changes in this commit:
- Fix USB CDC RX handling to not block when unprocessed.  The fix follows
  5873390226.
- Fix dupterm rx.
- Remove some obsolete lines.
This commit is contained in:
robert-hh 2022-03-27 08:50:34 +02:00 committed by Damien George
parent 752fe75910
commit ca41eda281
2 changed files with 48 additions and 36 deletions

View File

@ -256,6 +256,7 @@ SRC_C += \
shared/netutils/dhcpserver.c \
shared/readline/readline.c \
shared/runtime/gchelper_native.c \
shared/runtime/interrupt_char.c \
shared/runtime/mpirq.c \
shared/runtime/pyexec.c \
shared/runtime/stdout_helpers.c \

View File

@ -29,68 +29,82 @@
#include "py/stream.h"
#include "py/mphal.h"
#include "shared/timeutils/timeutils.h"
#include "shared/runtime/interrupt_char.h"
#include "extmod/misc.h"
#include "ticks.h"
#include "tusb.h"
#include "fsl_snvs_lp.h"
#if FSL_COMMON_DRIVER_VERSION != 0x020001
#include "fsl_ocotp.h"
#else
void OCOTP_Init(OCOTP_Type *base, uint32_t srcClock_Hz);
#ifndef MICROPY_HW_STDIN_BUFFER_LEN
#define MICROPY_HW_STDIN_BUFFER_LEN 512
#endif
#include CPU_HEADER_H
STATIC uint8_t stdin_ringbuf_array[260];
STATIC uint8_t stdin_ringbuf_array[MICROPY_HW_STDIN_BUFFER_LEN];
ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0};
#if MICROPY_KBD_EXCEPTION
uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll
int mp_interrupt_char = -1;
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) {
(void)itf;
(void)wanted_char;
tud_cdc_read_char(); // discard interrupt char
mp_sched_keyboard_interrupt();
void poll_cdc_interfaces(void) {
// any CDC interfaces left to poll?
if (cdc_itf_pending && ringbuf_free(&stdin_ringbuf)) {
for (uint8_t itf = 0; itf < 8; ++itf) {
if (cdc_itf_pending & (1 << itf)) {
tud_cdc_rx_cb(itf);
if (!cdc_itf_pending) {
break;
}
}
}
}
}
void mp_hal_set_interrupt_char(int c) {
mp_interrupt_char = c;
tud_cdc_set_wanted_char(c);
}
#endif
void tud_cdc_rx_cb(uint8_t itf) {
// consume pending USB data immediately to free usb buffer and keep the endpoint from stalling.
// in case the ringbuffer is full, mark the CDC interface that need attention later on for polling
cdc_itf_pending &= ~(1 << itf);
for (uint32_t bytes_avail = tud_cdc_n_available(itf); bytes_avail > 0; --bytes_avail) {
if (ringbuf_free(&stdin_ringbuf)) {
int data_char = tud_cdc_read_char();
if (data_char == mp_interrupt_char) {
mp_sched_keyboard_interrupt();
} else {
ringbuf_put(&stdin_ringbuf, data_char);
}
} else {
cdc_itf_pending |= (1 << itf);
return;
}
}
}
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
poll_cdc_interfaces();
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
ret |= MP_STREAM_POLL_RD;
}
if (tud_cdc_connected() && tud_cdc_available()) {
ret |= MP_STREAM_POLL_RD;
}
#if MICROPY_PY_OS_DUPTERM
ret |= mp_uos_dupterm_poll(poll_flags);
#endif
return ret;
}
int mp_hal_stdin_rx_chr(void) {
for (;;) {
// TODO
// if (USARTx->USART.INTFLAG.bit.RXC) {
// return USARTx->USART.DATA.bit.DATA;
// }
poll_cdc_interfaces();
int c = ringbuf_get(&stdin_ringbuf);
if (c != -1) {
return c;
}
if (tud_cdc_connected() && tud_cdc_available()) {
uint8_t buf[1];
uint32_t count = tud_cdc_read(buf, sizeof(buf));
if (count) {
return buf[0];
}
#if MICROPY_PY_OS_DUPTERM
int dupterm_c = mp_uos_dupterm_rx_chr();
if (dupterm_c >= 0) {
return dupterm_c;
}
#endif
MICROPY_EVENT_POLL_HOOK
}
}
@ -110,12 +124,9 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
i += n2;
}
}
#if MICROPY_PY_OS_DUPTERM
mp_uos_dupterm_tx_strn(str, len);
// TODO
// while (len--) {
// while (!(USARTx->USART.INTFLAG.bit.DRE)) { }
// USARTx->USART.DATA.bit.DATA = *str++;
// }
#endif
}
uint64_t mp_hal_time_ns(void) {