summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniël van de Giessen <daniel@dvdgiessen.nl>2023-08-25 15:40:46 +0200
committerDamien George <damien@micropython.org>2023-10-31 11:59:15 +1100
commitf74131134c7af52638348c65ecf1be5e769a5f4b (patch)
tree63d6f042dda50f51377f1a079cda7ae707ae4992
parent1cf3085c575095f7f027171759f112033b4060af (diff)
esp32: Poll serial/JTAG for unread data to prevent blocking.
If data is pushed over serial/JTAG too fast we may fill up stdin_ringbuf and not be able to read all the data out of the serial/JTAG buffer. Thus we need to explicitly poll and read the serial/JTAG RX buffer to prevent blocking (since if the serial/JTAG buffer is already filled, we will not get another interrupt to transfer it to the stdin ringbuffer). Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
-rw-r--r--ports/esp32/mphalport.c6
-rw-r--r--ports/esp32/usb_serial_jtag.c46
-rw-r--r--ports/esp32/usb_serial_jtag.h1
3 files changed, 41 insertions, 12 deletions
diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c
index 538b3e405..79ddcd482 100644
--- a/ports/esp32/mphalport.c
+++ b/ports/esp32/mphalport.c
@@ -100,6 +100,9 @@ void check_esp_err_(esp_err_t code, const char *func, const int line, const char
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
+ #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
+ usb_serial_jtag_poll_rx();
+ #endif
if ((poll_flags & MP_STREAM_POLL_RD) && stdin_ringbuf.iget != stdin_ringbuf.iput) {
ret |= MP_STREAM_POLL_RD;
}
@@ -111,6 +114,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
int mp_hal_stdin_rx_chr(void) {
for (;;) {
+ #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
+ usb_serial_jtag_poll_rx();
+ #endif
int c = ringbuf_get(&stdin_ringbuf);
if (c != -1) {
return c;
diff --git a/ports/esp32/usb_serial_jtag.c b/ports/esp32/usb_serial_jtag.c
index 3289a1b5c..f4b4b7cb9 100644
--- a/ports/esp32/usb_serial_jtag.c
+++ b/ports/esp32/usb_serial_jtag.c
@@ -33,12 +33,39 @@
#include "hal/usb_serial_jtag_ll.h"
#include "esp_intr_alloc.h"
#include "soc/periph_defs.h"
+#include "freertos/portmacro.h"
#define USB_SERIAL_JTAG_BUF_SIZE (64)
+static DRAM_ATTR portMUX_TYPE rx_mux = portMUX_INITIALIZER_UNLOCKED;
static uint8_t rx_buf[USB_SERIAL_JTAG_BUF_SIZE];
static volatile bool terminal_connected = false;
+static void usb_serial_jtag_handle_rx(void) {
+ if (xPortInIsrContext()) {
+ portENTER_CRITICAL_ISR(&rx_mux);
+ } else {
+ portENTER_CRITICAL(&rx_mux);
+ }
+ size_t req_len = ringbuf_free(&stdin_ringbuf);
+ if (req_len > USB_SERIAL_JTAG_BUF_SIZE) {
+ req_len = USB_SERIAL_JTAG_BUF_SIZE;
+ }
+ size_t len = usb_serial_jtag_ll_read_rxfifo(rx_buf, req_len);
+ for (size_t i = 0; i < len; ++i) {
+ if (rx_buf[i] == mp_interrupt_char) {
+ mp_sched_keyboard_interrupt();
+ } else {
+ ringbuf_put(&stdin_ringbuf, rx_buf[i]);
+ }
+ }
+ if (xPortInIsrContext()) {
+ portEXIT_CRITICAL_ISR(&rx_mux);
+ } else {
+ portEXIT_CRITICAL(&rx_mux);
+ }
+}
+
static void usb_serial_jtag_isr_handler(void *arg) {
uint32_t flags = usb_serial_jtag_ll_get_intsts_mask();
@@ -48,18 +75,7 @@ static void usb_serial_jtag_isr_handler(void *arg) {
if (flags & USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT) {
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
- size_t req_len = ringbuf_free(&stdin_ringbuf);
- if (req_len > USB_SERIAL_JTAG_BUF_SIZE) {
- req_len = USB_SERIAL_JTAG_BUF_SIZE;
- }
- size_t len = usb_serial_jtag_ll_read_rxfifo(rx_buf, req_len);
- for (size_t i = 0; i < len; ++i) {
- if (rx_buf[i] == mp_interrupt_char) {
- mp_sched_keyboard_interrupt();
- } else {
- ringbuf_put(&stdin_ringbuf, rx_buf[i]);
- }
- }
+ usb_serial_jtag_handle_rx();
mp_hal_wake_main_task_from_isr();
}
}
@@ -73,6 +89,12 @@ void usb_serial_jtag_init(void) {
usb_serial_jtag_isr_handler, NULL, NULL));
}
+void usb_serial_jtag_poll_rx(void) {
+ if (usb_serial_jtag_ll_rxfifo_data_available() && ringbuf_free(&stdin_ringbuf)) {
+ usb_serial_jtag_handle_rx();
+ }
+}
+
void usb_serial_jtag_tx_strn(const char *str, size_t len) {
while (len) {
size_t l = len;
diff --git a/ports/esp32/usb_serial_jtag.h b/ports/esp32/usb_serial_jtag.h
index 4eebb19e4..9e108e82c 100644
--- a/ports/esp32/usb_serial_jtag.h
+++ b/ports/esp32/usb_serial_jtag.h
@@ -27,6 +27,7 @@
#define MICROPY_INCLUDED_ESP32_USB_SERIAL_JTAG_H
void usb_serial_jtag_init(void);
+void usb_serial_jtag_poll_rx(void);
void usb_serial_jtag_tx_strn(const char *str, size_t len);
#endif // MICROPY_INCLUDED_ESP32_USB_SERIAL_JTAG_H