Skip to content

Commit 09d070a

Browse files
committed
tests/extmod_hardware: Add tests for machine.UART.IRQ_RX/RXIDLE/BREAK.
These all require hardware connections, so live in a different directory. Except for the IRQ_BREAK test of ESP32 devices a single UART with loopback is sufficient. General: SAMD21: Due to the limited flash size only SAMD21 devices with external flash support uart.irq(). IRQ_BREAK: ESP32 needs different UART devices for creating and sensing a break. Lacking a second UART the test is skipped for ESP32S2 and ESP32C3. RP2 does not pass the test reliable at 115200 baud, reason to be found. Thus the upper limit is set to 57600 Baud. Coverage: esp32 pass when different UART devices are used. rp2 pass up to 57600 baud IRQ_RX: SAMD21: Being a slow device it needs data to be sent byte-by-byte at 9600 baud, since the IRQ callback is scheduled delayed and then the flags do not match any more. The data matches since it is queued in the FIFO resp. ringbuffer. CC3200: The test cannot be performed since no calls are accepted in the IRQ handler like u.read(). Skipped. Coverage: cc3200 fail due to major differences in the implementation. esp32 pass nrf pass renesas-ra pass samd pass see the notes. stm32 pass IRQ_RXIDLE: STM32: With PyBoard the IRQ is called several times, but only once with the flag IRQ_RXIDLE set. Coverage: esp32 pass mimxrt pass renesas-ra pass rp2 pass samd pass for both SAMD21 and SAMD51 stm32 fail. see notes. Signed-off-by: Damien George <[email protected]> Signed-off-by: robert-hh <[email protected]>
1 parent b8513e6 commit 09d070a

6 files changed

+249
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Test machine.UART.IRQ_BREAK firing after a break is received.
2+
#
3+
# IMPORTANT: This test requires hardware connections: the UART TX and RX
4+
# pins must be wired together.
5+
6+
try:
7+
from machine import UART
8+
9+
UART.IRQ_BREAK
10+
except (ImportError, AttributeError):
11+
print("SKIP")
12+
raise SystemExit
13+
14+
import time, sys
15+
16+
# Configure pins based on the target.
17+
if "esp32" in sys.platform:
18+
if "ESP32S2" in sys.implementation._machine or "ESP32C3" in sys.implementation._machine:
19+
print("SKIP")
20+
raise SystemExit
21+
# ESP32 needs separate UART instances for the test
22+
recv_uart_id = 1
23+
recv_tx_pin = 14
24+
recv_rx_pin = 5
25+
send_uart_id = 2
26+
send_tx_pin = 4
27+
send_rx_pin = 12
28+
elif "rp2" in sys.platform:
29+
recv_uart_id = 0
30+
send_uart_id = 0
31+
recv_tx_pin = "GPIO0"
32+
recv_rx_pin = "GPIO1"
33+
else:
34+
print("Please add support for this test on this platform.")
35+
raise SystemExit
36+
37+
38+
def irq(u):
39+
print("IRQ_BREAK:", bool(u.irq().flags() & u.IRQ_BREAK), "data:", u.read(1))
40+
41+
42+
# Test that the IRQ is called for each break received.
43+
for bits_per_s in (2400, 9600, 57600):
44+
recv_uart = UART(recv_uart_id, bits_per_s, tx=recv_tx_pin, rx=recv_rx_pin)
45+
if recv_uart_id != send_uart_id:
46+
send_uart = UART(send_uart_id, bits_per_s, tx=send_tx_pin, rx=send_rx_pin)
47+
else:
48+
send_uart = recv_uart
49+
50+
recv_uart.irq(irq, recv_uart.IRQ_BREAK)
51+
52+
print("write", bits_per_s)
53+
for i in range(3):
54+
send_uart.write(str(i))
55+
send_uart.flush()
56+
time.sleep_ms(10)
57+
send_uart.sendbreak()
58+
time.sleep_ms(10)
59+
if "esp32" in sys.platform:
60+
# On esp32 a read is needed to read in the break byte.
61+
recv_uart.read()
62+
print("done")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
write 2400
2+
IRQ_BREAK: True data: b'0'
3+
IRQ_BREAK: True data: b'1'
4+
IRQ_BREAK: True data: b'2'
5+
done
6+
write 9600
7+
IRQ_BREAK: True data: b'0'
8+
IRQ_BREAK: True data: b'1'
9+
IRQ_BREAK: True data: b'2'
10+
done
11+
write 57600
12+
IRQ_BREAK: True data: b'0'
13+
IRQ_BREAK: True data: b'1'
14+
IRQ_BREAK: True data: b'2'
15+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Test machine.UART.IRQ_RX firing for each character received.
2+
#
3+
# IMPORTANT: This test requires hardware connections: the UART TX and RX
4+
# pins must be wired together.
5+
6+
try:
7+
from machine import UART
8+
9+
UART.IRQ_RX
10+
except (ImportError, AttributeError):
11+
print("SKIP")
12+
raise SystemExit
13+
14+
import time, sys
15+
16+
byte_by_byte = False
17+
# Configure pins based on the target.
18+
if "esp32" in sys.platform:
19+
uart_id = 1
20+
tx_pin = 4
21+
rx_pin = 5
22+
elif "pyboard" in sys.platform:
23+
uart_id = 4
24+
tx_pin = None # PA0
25+
rx_pin = None # PA1
26+
elif "samd" in sys.platform and "ItsyBitsy M0" in sys.implementation._machine:
27+
uart_id = 0
28+
tx_pin = "D1"
29+
rx_pin = "D0"
30+
byte_by_byte = True
31+
elif "samd" in sys.platform and "ItsyBitsy M4" in sys.implementation._machine:
32+
uart_id = 3
33+
tx_pin = "D1"
34+
rx_pin = "D0"
35+
elif "nrf" in sys.platform:
36+
uart_id = 0
37+
tx_pin = None
38+
rx_pin = None
39+
elif "renesas-ra" in sys.platform:
40+
uart_id = 9
41+
tx_pin = None # P602 @ RA6M2
42+
rx_pin = None # P601 @ RA6M2
43+
elif "CC3200" in sys.implementation._machine:
44+
# CC3200 doesn't work because it's too slow and has an allocation error in the handler.
45+
print("SKIP")
46+
raise SystemExit
47+
else:
48+
print("Please add support for this test on this platform.")
49+
raise SystemExit
50+
51+
52+
def irq(u):
53+
print("IRQ_RX:", bool(u.irq().flags() & u.IRQ_RX), "data:", u.read(1))
54+
55+
56+
text = "1234"
57+
58+
# Test that the IRQ is called for each byte received.
59+
# Use slow baudrates so that the IRQ has time to run.
60+
for bits_per_s in (2400, 9600):
61+
if tx_pin is None:
62+
uart = UART(uart_id, bits_per_s)
63+
else:
64+
uart = UART(uart_id, bits_per_s, tx=tx_pin, rx=rx_pin)
65+
66+
uart.irq(irq, uart.IRQ_RX)
67+
68+
print("write", bits_per_s)
69+
if byte_by_byte:
70+
# slow devices need data to be sent slow
71+
for c in text:
72+
uart.write(c)
73+
uart.flush()
74+
else:
75+
uart.write(text)
76+
uart.flush()
77+
time.sleep_ms(100)
78+
print("done")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
write 2400
2+
IRQ_RX: True data: b'1'
3+
IRQ_RX: True data: b'2'
4+
IRQ_RX: True data: b'3'
5+
IRQ_RX: True data: b'4'
6+
done
7+
write 9600
8+
IRQ_RX: True data: b'1'
9+
IRQ_RX: True data: b'2'
10+
IRQ_RX: True data: b'3'
11+
IRQ_RX: True data: b'4'
12+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Test machine.UART.IRQ_RXIDLE firing after a set of characters are received.
2+
#
3+
# IMPORTANT: This test requires hardware connections: the UART TX and RX
4+
# pins must be wired together.
5+
6+
try:
7+
from machine import UART
8+
9+
UART.IRQ_RXIDLE
10+
except (ImportError, AttributeError):
11+
print("SKIP")
12+
raise SystemExit
13+
14+
import time, sys
15+
16+
# Configure pins based on the target.
17+
if "esp32" in sys.platform:
18+
uart_id = 1
19+
tx_pin = 4
20+
rx_pin = 5
21+
elif "mimxrt" in sys.platform:
22+
uart_id = 1
23+
tx_pin = None
24+
elif "pyboard" in sys.platform:
25+
uart_id = 4
26+
tx_pin = None # PA0
27+
rx_pin = None # PA1
28+
elif "renesas-ra" in sys.platform:
29+
uart_id = 9
30+
tx_pin = None # P602 @ RA6M2
31+
rx_pin = None # P601 @ RA6M2
32+
elif "rp2" in sys.platform:
33+
uart_id = 0
34+
tx_pin = "GPIO0"
35+
rx_pin = "GPIO1"
36+
elif "samd" in sys.platform and "ItsyBitsy M0" in sys.implementation._machine:
37+
uart_id = 0
38+
tx_pin = "D1"
39+
rx_pin = "D0"
40+
byte_by_byte = True
41+
elif "samd" in sys.platform and "ItsyBitsy M4" in sys.implementation._machine:
42+
uart_id = 3
43+
tx_pin = "D1"
44+
rx_pin = "D0"
45+
else:
46+
print("Please add support for this test on this platform.")
47+
raise SystemExit
48+
49+
50+
def irq(u):
51+
print("IRQ_RXIDLE:", bool(u.irq().flags() & u.IRQ_RXIDLE), "data:", u.read())
52+
53+
54+
text = "12345678"
55+
56+
# Test that the IRQ is called for each set of byte received.
57+
for bits_per_s in (2400, 9600, 115200):
58+
if tx_pin is None:
59+
uart = UART(uart_id, bits_per_s)
60+
else:
61+
uart = UART(uart_id, bits_per_s, tx=tx_pin, rx=rx_pin)
62+
63+
uart.irq(irq, uart.IRQ_RXIDLE)
64+
65+
print("write", bits_per_s)
66+
uart.write(text)
67+
uart.flush()
68+
print("ready")
69+
time.sleep_ms(100)
70+
print("done")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
write 2400
2+
ready
3+
IRQ_RXIDLE: True data: b'12345678'
4+
done
5+
write 9600
6+
ready
7+
IRQ_RXIDLE: True data: b'12345678'
8+
done
9+
write 115200
10+
ready
11+
IRQ_RXIDLE: True data: b'12345678'
12+
done

0 commit comments

Comments
 (0)