|
| 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") |
0 commit comments