Skip to content

Commit 8f24c86

Browse files
committed
tests/extmod_hardware/machine_pwm.py: Convert test to use target_wiring.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 47871a4 commit 8f24c86

12 files changed

Lines changed: 63 additions & 34 deletions

File tree

tests/extmod_hardware/machine_pwm.py

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,42 @@
11
# Test machine.PWM, frequency and duty cycle (using machine.time_pulse_us).
22
#
33
# 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`).
85

96
try:
107
from machine import time_pulse_us, Pin, PWM
118
except ImportError:
129
print("SKIP")
1310
raise SystemExit
1411

15-
import unittest
12+
import machine, sys, time, unittest
13+
from target_wiring import pwm_loopback_pins
1614

1715
pwm_freq_limit = 1000000
1816
freq_margin_per_thousand = 0
1917
duty_margin_per_thousand = 0
2018
timing_margin_us = 5
2119

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:
2733
freq_margin_per_thousand = 2
2834
duty_margin_per_thousand = 1
2935
timing_margin_us = 20
3036
elif "esp8266" in sys.platform:
31-
pwm_pulse_pins = ((4, 5),)
3237
pwm_freq_limit = 1_000
3338
duty_margin_per_thousand = 3
3439
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
5740

5841

5942
# Test a specific frequency and duty cycle.
@@ -160,7 +143,7 @@ def test_freq_10000(self):
160143

161144

162145
# 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:
164147
cls_name = "Test_{}_{}".format(pwm, pulse)
165148
globals()[cls_name] = type(
166149
cls_name, (TestBase, unittest.TestCase), {"pwm_pin": pwm, "pulse_pin": pulse}

tests/run-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@
291291
"extmod/machine_uart_tx.py",
292292
"extmod_hardware/machine_can_timings.py",
293293
"extmod_hardware/machine_encoder.py",
294+
"extmod_hardware/machine_pwm.py",
294295
"extmod_hardware/machine_uart_irq_break.py",
295296
"extmod_hardware/machine_uart_irq_rx.py",
296297
"extmod_hardware/machine_uart_irq_rxidle.py",

tests/target_wiring/NUCLEO_WB55.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
uart_loopback_kwargs = {}
99

1010
spi_standalone_args_list = [(1,), (2,)]
11+
12+
pwm_loopback_pins = [("D1", "D0")]

tests/target_wiring/PYBx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212
# CAN args assume no connection for single device tests
1313
can_args = (1,)
1414
can_kwargs = {}
15+
16+
pwm_loopback_pins = [("X1", "X2")]

tests/target_wiring/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ SPI instances will be created using:
4848
for spi_args in spi_standalone_args_list:
4949
machine.SPI(*spi_args)
5050

51+
### PWM tests
52+
53+
PWM tests require a PWM output to be connected in loopback mode to another GPIO pin
54+
that will use `machine.time_pulse_us()` to time the PWM output signal. The variables
55+
are:
56+
57+
pwm_loopback_pins: list[tuple[Any, Any]]
58+
59+
The PWM and input Pin instances will be created using:
60+
61+
for pwm_pin, pulse_pin in pwm_loopback_pins:
62+
machine.PWM(pwm_pin)
63+
machine.Pin(pulse_pin, Pin.IN)
64+
5165
### Encoder tests
5266

5367
Encoder tests require one encoder to be connected in loopback mode to two other GPIO

tests/target_wiring/alif.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
uart_loopback_kwargs = {}
88

99
spi_standalone_args_list = [(0,)]
10+
11+
pwm_loopback_pins = [("P0_4", "P0_5")]

tests/target_wiring/esp32.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
else:
1515
spi_standalone_args_list = [(1,), (2,)]
1616

17+
pwm_loopback_pins = [(4, 5)]
18+
1719
encoder_loopback_id = 0
1820
encoder_loopback_out_pins = (4, 12)
1921
encoder_loopback_in_pins = (5, 13)

tests/target_wiring/esp8266.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
uart_loopback_kwargs = {}
88

99
spi_standalone_args_list = [(1,)]
10+
11+
pwm_loopback_pins = [(4, 5)]

tests/target_wiring/mimxrt.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
# Connect:
44
# - UART1 TX and RX, usually D0 and D1
55

6+
import sys
7+
68
uart_loopback_args = (1,)
79
uart_loopback_kwargs = {}
810

911
spi_standalone_args_list = [()]
1012

13+
if "Teensy" in sys.implementation._machine:
14+
# Teensy 4.x
15+
pwm_loopback_pins = [
16+
("D0", "D1"), # FLEXPWM X and UART 1
17+
("D2", "D3"), # FLEXPWM A/B
18+
("D11", "D12"), # QTMR and MOSI/MISO of SPI 0
19+
]
20+
else:
21+
pwm_loopback_pins = [("D0", "D1")]
22+
1123
encoder_loopback_id = 0
1224
encoder_loopback_out_pins = ("D0", "D2")
1325
encoder_loopback_in_pins = ("D1", "D3")

tests/target_wiring/rp2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
uart_loopback_kwargs = {"tx": "GPIO0", "rx": "GPIO1"}
88

99
spi_standalone_args_list = [(0,), (1,)]
10+
11+
pwm_loopback_pins = [("GPIO0", "GPIO1")]

0 commit comments

Comments
 (0)