Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.2k
Alarm pool sleep changes#16454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
dpgeorge merged 5 commits intomicropython:masterfrompeterharperuk:alarm_pool_sleep_changesMay 12, 2025
Uh oh!
There was an error while loading.Please reload this page.
Merged
Alarm pool sleep changes#16454
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
ee2c78c
rp2: Use pico-sdk alarm pool instead of soft timer for sleep.
peterharperuk2a4f1c9
rp2/modmachine: Add debug code for mp_machine_lightsleep.
peterharperuk03da155
tests/ports/rp2: Update machine idle test to revert skip for RP2350.
peterharperuk977fd94
tests/ports/rp2: Add a test case for light sleeping from CPU1.
projectgus69993da
rp2/modmachine: Add mutual exclusion for machine.lightsleep().
projectgusFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
82 changes: 68 additions & 14 deletionsports/rp2/modmachine.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletionports/rp2/mpconfigport.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
12 changes: 1 addition & 11 deletionsports/rp2/mphalport.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletionstests/ports/rp2/rp2_lightsleep_thread.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Verify that a thread running on CPU1 can go to lightsleep | ||
# and wake up in the expected timeframe | ||
import _thread | ||
import time | ||
import unittest | ||
from machine import lightsleep | ||
N_SLEEPS = 5 | ||
SLEEP_MS = 250 | ||
IDEAL_RUNTIME = N_SLEEPS * SLEEP_MS | ||
MAX_RUNTIME = (N_SLEEPS + 1) * SLEEP_MS | ||
MAX_DELTA = 20 | ||
class LightSleepInThread(unittest.TestCase): | ||
def thread_entry(self, is_thread=True): | ||
for _ in range(N_SLEEPS): | ||
lightsleep(SLEEP_MS) | ||
if is_thread: | ||
self.thread_done = True | ||
def elapsed_ms(self): | ||
return time.ticks_diff(time.ticks_ms(), self.t0) | ||
def setUp(self): | ||
self.thread_done = False | ||
self.t0 = time.ticks_ms() | ||
def test_cpu0_busy(self): | ||
_thread.start_new_thread(self.thread_entry, ()) | ||
# CPU0 is busy-waiting not asleep itself | ||
while not self.thread_done: | ||
self.assertLessEqual(self.elapsed_ms(), MAX_RUNTIME) | ||
self.assertAlmostEqual(self.elapsed_ms(), IDEAL_RUNTIME, delta=MAX_DELTA) | ||
def test_cpu0_sleeping(self): | ||
_thread.start_new_thread(self.thread_entry, ()) | ||
time.sleep_ms(MAX_RUNTIME) | ||
self.assertTrue(self.thread_done) | ||
self.assertAlmostEqual(self.elapsed_ms(), MAX_RUNTIME, delta=MAX_DELTA) | ||
def test_cpu0_also_lightsleep(self): | ||
_thread.start_new_thread(self.thread_entry, ()) | ||
time.sleep_ms(50) # account for any delay in starting the thread | ||
self.thread_entry(False) # does the same lightsleep loop, doesn't set the done flag | ||
while not self.thread_done: | ||
time.sleep_ms(10) | ||
# | ||
# Only one thread can actually be in lightsleep at a time to avoid | ||
# races, but otherwise the behaviour when both threads call lightsleep() | ||
# is unspecified. | ||
# | ||
# Currently, the other thread will return immediately if one is already | ||
# in lightsleep. Therefore, runtime can be between IDEAL_RUNTIME and | ||
# IDEAL_RUNTIME * 2 depending on how many times the calls to lightsleep() race | ||
# each other. | ||
# | ||
# Note this test case is really only here to ensure that the rp2 hasn't | ||
# hung or failed to sleep at all - not to verify any correct behaviour | ||
# when there's a race to call lightsleep(). | ||
self.assertGreaterEqual(self.elapsed_ms(), IDEAL_RUNTIME - MAX_DELTA) | ||
self.assertLessEqual(self.elapsed_ms(), IDEAL_RUNTIME * 2 + MAX_DELTA) | ||
if __name__ == "__main__": | ||
unittest.main() |
6 changes: 0 additions & 6 deletionstests/ports/rp2/rp2_machine_idle.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.