summaryrefslogtreecommitdiff
path: root/tests/extmod/machine_hard_timer.py
diff options
context:
space:
mode:
authorChris Webb <chris@arachsys.com>2025-08-25 15:25:21 +0100
committerDamien George <damien.p.george@gmail.com>2025-09-25 23:59:24 +1000
commitec6cdf3718ce49026940c5e2f65fb07f3fc23309 (patch)
treebacc3c416b6af9b9dd3dee997105e5415f28f3ea /tests/extmod/machine_hard_timer.py
parentccc954256f890b6626558349b513a6d840da24d9 (diff)
tests: Test hardware timers as well as software timers.
On platforms where hardware timers are available, test these in each combination of hard/soft and one-shot/periodic in the same way as for software timers. Where a platform supports both software (id = -1) and hardware (id >= 0) timers, the behaviour of both is now checked. For now, esp8266 is the only platform that supports hardware timers and both hard and soft callbacks. Signed-off-by: Chris Webb <chris@arachsys.com>
Diffstat (limited to 'tests/extmod/machine_hard_timer.py')
-rw-r--r--tests/extmod/machine_hard_timer.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/extmod/machine_hard_timer.py b/tests/extmod/machine_hard_timer.py
new file mode 100644
index 000000000..8fe42ea85
--- /dev/null
+++ b/tests/extmod/machine_hard_timer.py
@@ -0,0 +1,45 @@
+import sys
+
+try:
+ from machine import Timer
+ from time import sleep_ms
+except:
+ print("SKIP")
+ raise SystemExit
+
+if sys.platform == "esp8266":
+ timer = Timer(0)
+else:
+ # Hardware timers are not implemented.
+ print("SKIP")
+ raise SystemExit
+
+# Test both hard and soft IRQ handlers and both one-shot and periodic
+# timers. We adjust period in tests/extmod/machine_soft_timer.py, so try
+# adjusting freq here instead. The heap should be locked in hard callbacks
+# and unlocked in soft callbacks.
+
+
+def callback(t):
+ print("callback", mode[1], kind[1], freq, end=" ")
+ try:
+ allocate = bytearray(1)
+ print("unlocked")
+ except MemoryError:
+ print("locked")
+
+
+modes = [(Timer.ONE_SHOT, "one-shot"), (Timer.PERIODIC, "periodic")]
+kinds = [(False, "soft"), (True, "hard")]
+
+for mode in modes:
+ for kind in kinds:
+ for freq in 50, 25:
+ timer.init(
+ mode=mode[0],
+ freq=freq,
+ hard=kind[0],
+ callback=callback,
+ )
+ sleep_ms(90)
+ timer.deinit()