|
9 | 9 |
|
10 | 10 | import time, sys
|
11 | 11 |
|
| 12 | +initial_delay_ms = 0 |
| 13 | +bit_margin = 0 |
| 14 | +timing_margin_us = 100 |
| 15 | + |
12 | 16 | # Configure pins based on the target.
|
13 |
| -if "rp2" in sys.platform: |
| 17 | +if "esp32" in sys.platform: |
| 18 | + uart_id = 1 |
| 19 | + pins = {} |
| 20 | + timing_margin_us = 400 |
| 21 | +elif "mimxrt" in sys.platform: |
| 22 | + uart_id = 1 |
| 23 | + pins = {} |
| 24 | + initial_delay_ms = 20 # UART sends idle frame after init, so wait for that |
| 25 | + bit_margin = 1 |
| 26 | +elif "pyboard" in sys.platform: |
| 27 | + uart_id = 4 |
| 28 | + pins = {} |
| 29 | + initial_delay_ms = 50 # UART sends idle frame after init, so wait for that |
| 30 | + bit_margin = 1 # first start-bit must wait to sync with the UART clock |
| 31 | +elif "rp2" in sys.platform: |
14 | 32 | uart_id = 0
|
15 |
| - tx_pin = "GPIO0" |
16 |
| - rx_pin = "GPIO1" |
| 33 | + pins = {"tx": "GPIO0", "rx": "GPIO1"} |
17 | 34 | timing_margin_us = 180
|
| 35 | +elif "samd" in sys.platform: |
| 36 | + uart_id = 2 |
| 37 | + pins = {"tx": "D1", "rx": "D0"} |
| 38 | + timing_margin_us = 300 |
| 39 | + bit_margin = 1 |
18 | 40 | else:
|
19 | 41 | print("SKIP")
|
20 | 42 | raise SystemExit
|
21 | 43 |
|
22 | 44 | # Test that write+flush takes the expected amount of time to execute.
|
23 | 45 | for bits_per_s in (2400, 9600, 115200):
|
24 | 46 | text = "Hello World"
|
25 |
| - uart = UART(uart_id, bits_per_s, bits=8, parity=None, stop=1, tx=tx_pin, rx=rx_pin) |
| 47 | + uart = UART(uart_id, bits_per_s, bits=8, parity=None, stop=1, **pins) |
| 48 | + time.sleep_ms(initial_delay_ms) |
26 | 49 |
|
27 | 50 | start_us = time.ticks_us()
|
28 | 51 | uart.write(text)
|
|
33 | 56 | bits_per_char = 10
|
34 | 57 | expect_us = (len(text)) * bits_per_char * 1_000_000 // bits_per_s
|
35 | 58 | delta_us = abs(duration_us - expect_us)
|
36 |
| - print(bits_per_s, delta_us <= timing_margin_us or delta_us) |
| 59 | + margin_us = timing_margin_us + bit_margin * 1_000_000 // bits_per_s |
| 60 | + print(bits_per_s, delta_us <= margin_us or delta_us) |
0 commit comments