Skip to content

Commit 5e3cf0b

Browse files
committed
tests: Generalise rp2 timer test into a cross-port test.
Now all ports with machine.Timer except nrf support both hard and soft callbacks, generalise tests/ports/rp2_machine_timer.py into tests/extmod/machine_timer.py. There is an existing machine_soft_timer.py which varies period= and covers the nrf port but skips esp32/esp8266 because they don't support software timers. In our new test, we try varying freq= instead of period=, and cover esp32/esp8266 (with a fixed choice of hardware timer) but skip nrf because it doesn't support hard= or freq=. Add a check that the heap is locked (and allocation fails) in hard callbacks and it is unlocked (and allocation succeeds) in soft callbacks, to ensure we're getting the right kind of callback, not falling back to the default. Signed-off-by: Chris Webb <chris@arachsys.com>
1 parent 2387641 commit 5e3cf0b

File tree

4 files changed

+67
-36
lines changed

4 files changed

+67
-36
lines changed

tests/extmod/machine_timer.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import sys
2+
3+
try:
4+
from machine import Timer
5+
from time import sleep_ms
6+
except:
7+
print("SKIP")
8+
raise SystemExit
9+
10+
if sys.platform in ("nrf"):
11+
# The nrf port doesn't support selection of hard and soft callbacks,
12+
# and only allows Timer(period=N), not Timer(freq=N).
13+
print("SKIP")
14+
raise SystemExit
15+
elif sys.platform in ("esp32", "esp8266"):
16+
# Test with a hardware timer as software timers aren't implemented
17+
# on the esp32 and esp8266 ports. Timer ID 0 is guaranteed to exist.
18+
timer_id = 0
19+
else:
20+
timer_id = -1
21+
22+
# Test both hard and soft IRQ handlers and both one-shot and periodic
23+
# timers. We adjust period in tests/extmod/machine_soft_timer.py, so try
24+
# adjusting freq here instead. The heap should be locked in hard callbacks
25+
# and unlocked in soft callbacks.
26+
27+
28+
def callback(t):
29+
print("callback", modes[mode], kinds[hard], freq, end=" ")
30+
try:
31+
allocate = bytearray(1)
32+
print("unlocked")
33+
except MemoryError:
34+
print("locked")
35+
36+
37+
modes = {Timer.ONE_SHOT: "one-shot", Timer.PERIODIC: "periodic"}
38+
kinds = {False: "soft", True: "hard"}
39+
40+
for mode in modes:
41+
for hard in kinds:
42+
for freq in 500, 250:
43+
timer = Timer(
44+
timer_id,
45+
mode=mode,
46+
freq=freq,
47+
hard=hard,
48+
callback=callback,
49+
)
50+
sleep_ms(9)
51+
timer.deinit()

tests/extmod/machine_timer.py.exp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
callback one-shot soft 500 unlocked
2+
callback one-shot soft 250 unlocked
3+
callback one-shot hard 500 locked
4+
callback one-shot hard 250 locked
5+
callback periodic soft 500 unlocked
6+
callback periodic soft 500 unlocked
7+
callback periodic soft 500 unlocked
8+
callback periodic soft 500 unlocked
9+
callback periodic soft 250 unlocked
10+
callback periodic soft 250 unlocked
11+
callback periodic hard 500 locked
12+
callback periodic hard 500 locked
13+
callback periodic hard 500 locked
14+
callback periodic hard 500 locked
15+
callback periodic hard 250 locked
16+
callback periodic hard 250 locked

tests/ports/rp2/rp2_machine_timer.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/ports/rp2/rp2_machine_timer.py.exp

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy