Skip to content

Commit c41b421

Browse files
robert-hhdpgeorge
authored andcommitted
tests/extmod/machine_uart_tx.py: Add a test for timing of UART.flush().
Currently only runs on rp2 but could be extended to run on other targets. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent 07472d0 commit c41b421

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

tests/extmod/machine_uart_tx.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Test machine.UART transmission.
2+
# Does not require any external connections.
3+
4+
try:
5+
from machine import UART
6+
except ImportError:
7+
print("SKIP")
8+
raise SystemExit
9+
10+
import time, sys
11+
12+
# Configure pins based on the target.
13+
if "rp2" in sys.platform:
14+
uart_id = 0
15+
tx_pin = "GPIO0"
16+
rx_pin = "GPIO1"
17+
else:
18+
print("SKIP")
19+
raise SystemExit
20+
21+
# Test that write+flush takes the expected amount of time to execute.
22+
for bits_per_s in (2400, 9600, 115200):
23+
text = "Hello World"
24+
uart = UART(uart_id, bits_per_s, bits=8, parity=None, stop=1, tx=tx_pin, rx=rx_pin)
25+
26+
start_us = time.ticks_us()
27+
uart.write(text)
28+
uart.flush()
29+
duration_us = time.ticks_diff(time.ticks_us(), start_us)
30+
31+
# 1(startbit) + 8(bits) + 1(stopbit) + 0(parity)
32+
bits_per_char = 10
33+
expect_us = (len(text)) * bits_per_char * 1_000_000 // bits_per_s
34+
print(bits_per_s, duration_us <= expect_us)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2400 True
2+
9600 True
3+
115200 True

0 commit comments

Comments
 (0)