8000 rp2/machine_uart: Make it so TX is done only when no longer busy. · micropython/micropython@97af100 · GitHub
[go: up one dir, main page]

Skip to content

Commit 97af100

Browse files
committed
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 <damien@micropython.org>
1 parent 1b89c50 commit 97af100

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

docs/library/machine.UART.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Methods
160160

161161
.. note::
162162

163-
For the rp2, esp8266 and nrf ports the call returns while the last byte is sent.
163+
For the esp8266 and nrf ports the call returns while the last byte is sent.
164164
If required, a one character wait time has to be added in the calling script.
165165

166166
Availability: rp2, esp32, e 8000 sp8266, mimxrt, cc3200, stm32, nrf ports, renesas-ra
@@ -172,7 +172,7 @@ Methods
172172

173173
.. note::
174174

175-
For the rp2, esp8266 and nrf ports the call may return ``True`` even if the last byte
175+
For the esp8266 and nrf ports the call may return ``True`` even if the last byte
176176
of a transfer is still being sent. If required, a one character wait time has to be
177177
added in the calling script.
178178

ports/rp2/machine_uart.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,9 @@ static mp_int_t mp_machine_uart_any(machine_uart_obj_t *self) {
491491
}
492492

493493
static bool mp_machine_uart_txdone(machine_uart_obj_t *self) {
494+
// TX is done when: nothing in the ringbuf, TX FIFO is empty, TX output is not busy.
494495
return ringbuf_avail(&self->write_buffer) == 0
495-
&& (uart_get_hw(self->uart)->fr & UART_UARTFR_TXFE_BITS);
496+
&& (uart_get_hw(self->uart)->fr & (UART_UARTFR_TXFE_BITS | UART_UARTFR_BUSY_BITS)) == UART_UARTFR_TXFE_BITS;
496497
}
497498

498499
static void mp_machine_uart_sendbreak(machine_uart_obj_t *self) {

tests/extmod/machine_uart_tx.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
uart_id = 0
1515
tx_pin = "GPIO0"
1616
rx_pin = "GPIO1"
17+
timing_margin_us = 180
1718
else:
1819
print("SKIP")
1920
raise SystemExit
@@ -31,4 +32,5 @@
3132
# 1(startbit) + 8(bits) + 1(stopbit) + 0(parity)
3233
bits_per_char = 10
3334
expect_us = (len(text)) * bits_per_char * 1_000_000 // bits_per_s
34-
print(bits_per_s, duration_us <= expect_us)
35+
delta_us = abs(duration_us - expect_us)
36+
print(bits_per_s, delta_us <= timing_margin_us or delta_us)

0 commit comments

Comments
 (0)
0