From 2d3f1a10bf90ecd54b15c7d725e8dc89f42048b0 Mon Sep 17 00:00:00 2001 From: Paul Kierstead Date: Sat, 1 Sep 2018 17:07:30 +0000 Subject: [PATCH] When UART timeout of zero is given, make read() return data already available --- ports/atmel-samd/common-hal/busio/UART.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/atmel-samd/common-hal/busio/UART.c b/ports/atmel-samd/common-hal/busio/UART.c index 1be245b4b6..d2333e9619 100644 --- a/ports/atmel-samd/common-hal/busio/UART.c +++ b/ports/atmel-samd/common-hal/busio/UART.c @@ -249,7 +249,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t uint64_t start_ticks = ticks_ms; // Busy-wait until timeout or until we've read enough chars. - while (ticks_ms - start_ticks < self->timeout_ms) { + while (ticks_ms - start_ticks <= self->timeout_ms) { // Read as many chars as we can right now, up to len. size_t num_read = io_read(io, data, len); @@ -268,6 +268,10 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t #ifdef MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_LOOP #endif + // If we are zero timeout, make sure we don't loop again (in the event + // we read in under 1ms) + if (self->timeout_ms == 0) + break; } if (total_read == 0) {