|
1 | 1 | # Test machine.PWM, frequency and duty cycle (using machine.time_pulse_us). |
2 | 2 | # |
3 | 3 | # IMPORTANT: This test requires hardware connections: the PWM-output and pulse-input |
4 | | -# pins must be wired together (see the variable `pwm_pulse_pins`). |
5 | | - |
6 | | -import sys |
7 | | -import time |
| 4 | +# pins must be wired together (see the variable `pwm_loopback_pins`). |
8 | 5 |
|
9 | 6 | try: |
10 | 7 | from machine import time_pulse_us, Pin, PWM |
11 | 8 | except ImportError: |
12 | 9 | print("SKIP") |
13 | 10 | raise SystemExit |
14 | 11 |
|
15 | | -import unittest |
| 12 | +import machine, sys, time, unittest |
| 13 | +from target_wiring import pwm_loopback_pins |
16 | 14 |
|
17 | 15 | pwm_freq_limit = 1000000 |
18 | 16 | freq_margin_per_thousand = 0 |
19 | 17 | duty_margin_per_thousand = 0 |
20 | 18 | timing_margin_us = 5 |
21 | 19 |
|
22 | | -# Configure pins based on the target. |
23 | | -if "alif" in sys.platform: |
24 | | - pwm_pulse_pins = (("P0_4", "P0_5"),) |
25 | | -elif "esp32" in sys.platform: |
26 | | - pwm_pulse_pins = ((4, 5),) |
| 20 | +# Slow MCUs cannot capture short pulses using `time_pulse_us` so limit the maximum PWM |
| 21 | +# frequency tested on such targets. |
| 22 | +if hasattr(machine, "freq"): |
| 23 | + f = machine.freq() |
| 24 | + if isinstance(f, tuple): |
| 25 | + f = f[0] |
| 26 | + if f <= 48_000_000: |
| 27 | + pwm_freq_limit = 2_000 |
| 28 | + elif f <= 64_000_000: |
| 29 | + pwm_freq_limit = 5_000 |
| 30 | + |
| 31 | +# Tune test parameters based on the target. |
| 32 | +if "esp32" in sys.platform: |
27 | 33 | freq_margin_per_thousand = 2 |
28 | 34 | duty_margin_per_thousand = 1 |
29 | 35 | timing_margin_us = 20 |
30 | 36 | elif "esp8266" in sys.platform: |
31 | | - pwm_pulse_pins = ((4, 5),) |
32 | 37 | pwm_freq_limit = 1_000 |
33 | 38 | duty_margin_per_thousand = 3 |
34 | 39 | timing_margin_us = 50 |
35 | | -elif "mimxrt" in sys.platform: |
36 | | - if "Teensy" in sys.implementation._machine: |
37 | | - # Teensy 4.x |
38 | | - pwm_pulse_pins = ( |
39 | | - ("D0", "D1"), # FLEXPWM X and UART 1 |
40 | | - ("D2", "D3"), # FLEXPWM A/B |
41 | | - ("D11", "D12"), # QTMR and MOSI/MISO of SPI 0 |
42 | | - ) |
43 | | - else: |
44 | | - pwm_pulse_pins = (("D0", "D1"),) |
45 | | -elif "pyboard" in sys.platform: |
46 | | - pwm_pulse_pins = ((Pin.cpu.A0, Pin.cpu.A1),) |
47 | | -elif "rp2" in sys.platform: |
48 | | - pwm_pulse_pins = (("GPIO0", "GPIO1"),) |
49 | | -elif "samd" in sys.platform: |
50 | | - pwm_pulse_pins = (("D0", "D1"),) |
51 | | - if "SAMD21" in sys.implementation._machine: |
52 | | - # MCU is too slow to capture short pulses. |
53 | | - pwm_freq_limit = 2_000 |
54 | | -else: |
55 | | - print("Please add support for this test on this platform.") |
56 | | - raise SystemExit |
57 | 40 |
|
58 | 41 |
|
59 | 42 | # Test a specific frequency and duty cycle. |
@@ -160,7 +143,7 @@ def test_freq_10000(self): |
160 | 143 |
|
161 | 144 |
|
162 | 145 | # Generate test classes, one for each set of pins to test. |
163 | | -for pwm, pulse in pwm_pulse_pins: |
| 146 | +for pwm, pulse in pwm_loopback_pins: |
164 | 147 | cls_name = "Test_{}_{}".format(pwm, pulse) |
165 | 148 | globals()[cls_name] = type( |
166 | 149 | cls_name, (TestBase, unittest.TestCase), {"pwm_pin": pwm, "pulse_pin": pulse} |
|
0 commit comments