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.
pull/7587/head
robert-hh 2021-07-23 14:04:36 +02:00 zatwierdzone przez Damien George
rodzic 2e62e13455
commit ee49ae8f82
1 zmienionych plików z 7 dodań i 4 usunięć

Wyświetl plik

@ -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;