From ee49ae8f82c22b43777aa9e73a1b7a6825304d13 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 23 Jul 2021 14:04:36 +0200 Subject: [PATCH] rp2/machine_uart: Fix read when FIFO has chars but ringbuf doesn't. Prior to this fix, if the UART hardware FIFO had a few chars but still below the FIFO trigger threshold, and the ringbuf was empty, the read function would timeout if timeout==0 (the default timeout). This fix follows the suggestion of @iabdalkader. --- ports/rp2/machine_uart.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index 2431f496eb..a8ec149f4b 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -407,6 +407,13 @@ STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t siz for (size_t i = 0; i < size; i++) { // Wait for the first/next character while (ringbuf_avail(&self->read_buffer) == 0) { + if (uart_is_readable(self->uart)) { + // Force a few incoming bytes to the buffer + self->read_lock = true; + uart_drain_rx_fifo(self); + self->read_lock = false; + break; + } if (time_us_64() > t) { // timed out if (i <= 0) { *errcode = MP_EAGAIN; @@ -416,10 +423,6 @@ STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t siz } } MICROPY_EVENT_POLL_HOOK - // Force a few incoming bytes to the buffer - self->read_lock = true; - uart_drain_rx_fifo(self); - self->read_lock = false; } *dest++ = ringbuf_get(&(self->read_buffer)); t = time_us_64() + timeout_char_us;