From 97af1001ae07c573bf432b9923dcdf78055a508c Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 21 Oct 2024 16:32:24 +1100 Subject: [PATCH] rp2/machine_uart: Make it so TX is done only when no longer busy. Prior to this commit, when flushing a UART on the rp2 port, it returns just before the last character is sent out the wire. Fix this by waiting until the BUSY flag is cleared. This also fixes the behaviour of `UART.txdone()` to return `True` only when the last byte has gone out. Updated docs and tests to match. The test now checks that UART TX time is very close to the expected time (prior, it was just testing that the TX time was less than the expected time). Signed-off-by: Damien George --- docs/library/machine.UART.rst | 4 ++-- ports/rp2/machine_uart.c | 3 ++- tests/extmod/machine_uart_tx.py | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/library/machine.UART.rst b/docs/library/machine.UART.rst index 0f3e77ec471a9..4dcb4a1e7c4a6 100644 --- a/docs/library/machine.UART.rst +++ b/docs/library/machine.UART.rst @@ -160,7 +160,7 @@ Methods .. note:: - For the rp2, esp8266 and nrf ports the call returns while the last byte is sent. + For the esp8266 and nrf ports the call returns while the last byte is sent. If required, a one character wait time has to be added in the calling script. Availability: rp2, esp32, esp8266, mimxrt, cc3200, stm32, nrf ports, renesas-ra @@ -172,7 +172,7 @@ Methods .. note:: - For the rp2, esp8266 and nrf ports the call may return ``True`` even if the last byte + For the esp8266 and nrf ports the call may return ``True`` even if the last byte of a transfer is still being sent. If required, a one character wait time has to be added in the calling script. diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index 54791cdc59436..334c6fda328f3 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -491,8 +491,9 @@ static mp_int_t mp_machine_uart_any(machine_uart_obj_t *self) { } static bool mp_machine_uart_txdone(machine_uart_obj_t *self) { + // TX is done when: nothing in the ringbuf, TX FIFO is empty, TX output is not busy. return ringbuf_avail(&self->write_buffer) == 0 - && (uart_get_hw(self->uart)->fr & UART_UARTFR_TXFE_BITS); + && (uart_get_hw(self->uart)->fr & (UART_UARTFR_TXFE_BITS | UART_UARTFR_BUSY_BITS)) == UART_UARTFR_TXFE_BITS; } static void mp_machine_uart_sendbreak(machine_uart_obj_t *self) { diff --git a/tests/extmod/machine_uart_tx.py b/tests/extmod/machine_uart_tx.py index 54c0af2b2314e..25bb834929d1a 100644 --- a/tests/extmod/machine_uart_tx.py +++ b/tests/extmod/machine_uart_tx.py @@ -14,6 +14,7 @@ uart_id = 0 tx_pin = "GPIO0" rx_pin = "GPIO1" + timing_margin_us = 180 else: print("SKIP") raise SystemExit @@ -31,4 +32,5 @@ # 1(startbit) + 8(bits) + 1(stopbit) + 0(parity) bits_per_char = 10 expect_us = (len(text)) * bits_per_char * 1_000_000 // bits_per_s - print(bits_per_s, duration_us <= expect_us) + delta_us = abs(duration_us - expect_us) + print(bits_per_s, delta_us <= timing_margin_us or delta_us)