Skip to content

Commit a0880ec

Browse files
committed
tests,rp2: Add a test case for light sleeping from CPU1.
This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent b9cfba5 commit a0880ec

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Verify that a thread running on CPU1 can go to lightsleep
2+
# and wake up in the expected timeframe
3+
import _thread
4+
import time
5+
import unittest
6+
from machine import lightsleep, Pin
7+
8+
try:
9+
led = Pin(Pin.board.LED, Pin.OUT)
10+
except AttributeError:
11+
led = None
12+
13+
N_SLEEPS = 5
14+
SLEEP_MS = 250
15+
16+
IDEAL_RUNTIME = N_SLEEPS * SLEEP_MS
17+
MAX_RUNTIME = (N_SLEEPS + 1) * SLEEP_MS
18+
19+
20+
class LightSleepInThread(unittest.TestCase):
21+
def thread_entry(self, is_thread=True):
22+
led.toggle()
23+
for _ in range(N_SLEEPS):
24+
lightsleep(SLEEP_MS)
25+
led.toggle()
26+
if is_thread:
27+
self.thread_done = True
28+
29+
def elapsed_ms(self):
30+
return time.ticks_diff(time.ticks_ms(), self.t0)
31+
32+
def setUp(self):
33+
self.thread_done = False
34+
self.t0 = time.ticks_ms()
35+
36+
def test_cpu0_busy(self):
37+
_thread.start_new_thread(self.thread_entry, ())
38+
# CPU0 is busy-waiting not asleep itself
39+
while not self.thread_done:
40+
self.assertLessEqual(self.elapsed_ms(), MAX_RUNTIME)
41+
self.assertAlmostEqual(self.elapsed_ms(), IDEAL_RUNTIME, delta=IDEAL_RUNTIME / 2)
42+
43+
def test_cpu0_sleeping(self):
44+
_thread.start_new_thread(self.thread_entry, ())
45+
time.sleep_ms(MAX_RUNTIME)
46+
self.assertTrue(self.thread_done)
47+
self.assertAlmostEqual(self.elapsed_ms(), IDEAL_RUNTIME, delta=IDEAL_RUNTIME / 2)
48+
49+
def test_cpu0_also_lightsleep(self):
50+
_thread.start_new_thread(self.thread_entry, ())
51+
time.sleep(0.050) # account for any delay in starting the thread
52+
self.thread_entry(False) # does the same lightsleep loop, doesn't set the done flag
53+
self.assertTrue(self.thread_done)
54+
# only one thread can actually be in lightsleep at a time to avoid races, so the total
55+
# runtime is doubled by doing it on both CPUs
56+
self.assertAlmostEqual(self.elapsed_ms(), IDEAL_RUNTIME * 2, delta=IDEAL_RUNTIME)
57+
58+
59+
if __name__ == "__main__":
60+
unittest.main()

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