|
| 1 | +# Test machine.UART.IRQ_TXIDLE firing after a transmission. |
| 2 | +# Does not require any external connections. |
| 3 | + |
| 4 | +try: |
| 5 | + from machine import UART |
| 6 | + |
| 7 | + UART.IRQ_TXIDLE |
| 8 | +except (ImportError, AttributeError): |
| 9 | + print("SKIP") |
| 10 | + raise SystemExit |
| 11 | + |
| 12 | +import time, sys |
| 13 | + |
| 14 | +# Configure pins based on the target. |
| 15 | +if "rp2" in sys.platform: |
| 16 | + uart_id = 0 |
| 17 | + tx_pin = "GPIO0" |
| 18 | + rx_pin = "GPIO1" |
| 19 | + min_window = 1 |
| 20 | +elif "samd" in sys.platform and "ItsyBitsy M0" in sys.implementation._machine: |
| 21 | + uart_id = 0 |
| 22 | + tx_pin = "D1" |
| 23 | + rx_pin = "D0" |
| 24 | + # For SAMD delay_ms has to be used for the trailing window, and the |
| 25 | + # mininmal time is 11 ms to allow for scheduling. |
| 26 | + min_window = 11 |
| 27 | +elif "samd" in sys.platform and "ItsyBitsy M4" in sys.implementation._machine: |
| 28 | + uart_id = 3 |
| 29 | + tx_pin = "D1" |
| 30 | + rx_pin = "D0" |
| 31 | + min_window = 11 |
| 32 | +elif "mimxrt" in sys.platform: |
| 33 | + uart_id = 1 |
| 34 | + tx_pin = None |
| 35 | + min_window = 0 |
| 36 | +elif "nrf" in sys.platform: |
| 37 | + uart_id = 0 |
| 38 | + tx_pin = None |
| 39 | + min_window = 0 |
| 40 | +else: |
| 41 | + print("Please add support for this test on this platform.") |
| 42 | + raise SystemExit |
| 43 | + |
| 44 | + |
| 45 | +def irq(u): |
| 46 | + print("IRQ_TXIDLE:", u.irq().flags() == u.IRQ_TXIDLE) |
| 47 | + |
| 48 | + |
| 49 | +text = "Hello World" * 20 |
| 50 | + |
| 51 | +# Test that the IRQ is called after the write has completed. |
| 52 | +for bits_per_s in (2400, 9600, 115200): |
| 53 | + if tx_pin is None: |
| 54 | + uart = UART(uart_id, bits_per_s) |
| 55 | + else: |
| 56 | + uart = UART(uart_id, bits_per_s, tx=tx_pin, rx=rx_pin) |
| 57 | + |
| 58 | + uart.irq(irq, uart.IRQ_TXIDLE) |
| 59 | + |
| 60 | + # The IRQ_TXIDLE shall trigger after the message has been sent. Thus |
| 61 | + # the test marks a time window close to the expected of the sending |
| 62 | + # and the time at which the IRQ should have been fired. |
| 63 | + # It is just a rough estimation of 10 characters before and |
| 64 | + # 20 characters after the data's end, unless there is a need to |
| 65 | + # wait a minimal time, like for SAMD devices. |
| 66 | + |
| 67 | + bits_per_char = 10 # 1(startbit) + 8(bits) + 1(stopbit) + 0(parity) |
| 68 | + start_time_us = (len(text) - 10) * bits_per_char * 1_000_000 // bits_per_s |
| 69 | + window_ms = max(min_window, 20 * bits_per_char * 1_000 // bits_per_s + 1) |
| 70 | + |
| 71 | + print("write", bits_per_s) |
| 72 | + uart.write(text) |
| 73 | + time.sleep_us(start_time_us) |
| 74 | + print("ready") |
| 75 | + time.sleep_ms(window_ms) |
| 76 | + print("done") |
0 commit comments