Skip to content

Commit 00a0cd7

Browse files
committed
tests/ports/rp2: Tune rp2.DMA test so it runs in all configurations.
Changes in this commit: - Allow the DMA instance to be any instance, not just DMA(0); eg WLAN may be using DMA(0). - Make the DMA timing test run a little faster by preloading `dma.active`. - Run the DMA timing test 10 times and take the average time taken as the test result, to eliminate any big effects of caching. - Change the expected time to `range(30, 80)` to cover RP2040, RP2350, RISC-V variants, and both bytecode and native emitter. - Add a `sleep_ms(1)` after waiting for the IRQ to fire, so that any scheduled code gets a chance to run when the test is compiled with the native emitter. With these changes this test passes reliably on RPI_PICO, RPI_PICO_W, RPI_PICO2, RPI_PICO2_W, RPI_PICO2-RISCV and RPI_PICO2_W-RISCV, in both bytecode and native emitter mode, with and without WLAN enabled. Signed-off-by: Damien George <[email protected]>
1 parent ffd7e0e commit 00a0cd7

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

tests/ports/rp2/rp2_dma.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def tearDown(self):
2020

2121
def test_printing(self):
2222
dma = self.dma
23-
self.assertEqual(str(dma), "DMA(0)")
23+
self.assertEqual(str(dma), "DMA({})".format(dma.channel))
2424

2525
def test_pack_unpack_ctrl(self):
2626
dma = self.dma
@@ -34,7 +34,7 @@ def test_pack_unpack_ctrl(self):
3434
self.assertEqual(ctrl_dict["ahb_err"], 0)
3535
self.assertEqual(ctrl_dict["bswap"], 0)
3636
self.assertEqual(ctrl_dict["busy"], 0)
37-
self.assertEqual(ctrl_dict["chain_to"], 0)
37+
self.assertEqual(ctrl_dict["chain_to"], dma.channel)
3838
self.assertEqual(ctrl_dict["enable"], 1)
3939
self.assertEqual(ctrl_dict["high_pri"], 0)
4040
self.assertEqual(ctrl_dict["inc_read"], 1)
@@ -58,7 +58,7 @@ def test_register_access(self):
5858
self.assertEqual(dma.write, 0)
5959
self.assertEqual(dma.count, 0)
6060
self.assertEqual(dma.ctrl & 0x01F, 25)
61-
self.assertEqual(dma.channel, 0)
61+
self.assertIn(dma.channel, range(16))
6262
self.assertIsInstance(dma.registers, memoryview)
6363

6464
def test_close(self):
@@ -86,26 +86,32 @@ def test_simple_memory_copy(self):
8686
def test_time_taken_for_large_memory_copy(self):
8787
def run_and_time_dma(dma):
8888
ticks_us = time.ticks_us
89+
active = dma.active
8990
irq_state = machine.disable_irq()
9091
t0 = ticks_us()
91-
dma.active(True)
92-
while dma.active():
92+
active(True)
93+
while active():
9394
pass
9495
t1 = ticks_us()
9596
machine.enable_irq(irq_state)
9697
return time.ticks_diff(t1, t0)
9798

98-
dma = self.dma
99-
dest = bytearray(16 * 1024)
100-
dma.read = SRC
101-
dma.write = dest
102-
dma.count = len(dest) // 4
103-
dma.ctrl = dma.pack_ctrl()
104-
dt = run_and_time_dma(dma)
105-
expected_dt_range = range(40, 70) if is_rp2350 else range(70, 125)
106-
self.assertIn(dt, expected_dt_range)
107-
self.assertEqual(dest[:8], SRC[:8])
108-
self.assertEqual(dest[-8:], SRC[-8:])
99+
num_average = 10
100+
dt_sum = 0
101+
for _ in range(num_average):
102+
dma = self.dma
103+
dest = bytearray(16 * 1024)
104+
dma.read = SRC
105+
dma.write = dest
106+
dma.count = len(dest) // 4
107+
dma.ctrl = dma.pack_ctrl()
108+
dt_sum += run_and_time_dma(dma)
109+
self.assertEqual(dest[:8], SRC[:8])
110+
self.assertEqual(dest[-8:], SRC[-8:])
111+
self.tearDown()
112+
self.setUp()
113+
dt = dt_sum // num_average
114+
self.assertIn(dt, range(30, 80))
109115

110116
def test_config_trigger(self):
111117
# Test using .config(trigger=True) to start DMA immediately.
@@ -136,6 +142,7 @@ def callback(dma):
136142
)
137143
while dma.active():
138144
pass
145+
time.sleep_ms(1) # when running as native code, give the scheduler a chance to run
139146
self.assertEqual(irq_flags, 1)
140147
self.assertEqual(dest[:8], SRC[:8])
141148
self.assertEqual(dest[-8:], SRC[-8:])

0 commit comments

Comments
 (0)