Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

rp2,docs: Stop machine.idle() blocking indefinitely, update docs.#15345

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

Conversation

projectgus
Copy link
Contributor

@projectgusprojectgus commentedJun 25, 2024
edited
Loading

Summary

Updates rp2 port to always resume from idle within 1ms max.

When rp2 port went tickless the behaviour of machine.idle() changed as there is no longer a tick interrupt to wake it up every millisecond. On a quiet system without interrupts it can now block indefinitely. No other port does this.

A lot of existing code (i.e. micropython-lib lps22h, lcd160cr sensor drivers, lora sync_modem driver, usb-device-hid) callsmachine.idle() inside a tight loop that is polling some condition. This reduces the power usage compared to constantly looping, but can be faster than calling a sleep function. However there's not always an interrupt before the condition they are polling for, so it's difficult to restructure this code ifmachine.idle() doesn't have any upper limit on execution time.

Also changes the machine module documentation to explain the new behaviour and recommended usage. I believe all ports will pause for at most 1ms now, and suggest this is good behaviour to stick with.

This fix depends on#15398 for correct behaviour, and that PR should be merged before this.

Testing

Tested on rp2 PICO and PICO_W:

  • Added a unit test to verify themachine.idle() wakeup time. This test fails on master (30ms average wakeup time on PICO-W, never completes on PICO). The test also fails in this branch without the linked PR (average wakeup time of 0.02ms). Passes after this branch is rebased with the WFE fix.
  • Ranwhile True: machine.idle() and verified power consumption was approximately the same as tunningtime.sleep(10). This is also depends on the linked PR with the WFE fix.

Trade-offs and Alternatives

  • Keeping the current behaviour of "machine.idle() blocks indefinitely on some ports" rules out a lot of use cases, and the power consumption is only very marginally less. Lightsleep is always going to be a better fit for long idle periods, and saves much more power.
  • Could add an optionalmachine.idle(sleep_ms=1) argument but it's hard to come up with a use case to justify the extra complexity. This isn't a sleep function so an interrupt can resume execution at any time, and looping with 1ms periods has basically the same power consumption as looping with 1000ms idle periods.

This work was funded through GitHub Sponsors.

@github-actionsGitHub Actions
Copy link

Code size report:

   bare-arm:    +0 +0.000% minimal x86:    +0 +0.000%    unix x64:    +0 +0.000% standard      stm32:    +0 +0.000% PYBV10     mimxrt:    +0 +0.000% TEENSY40        rp2:    +8 +0.001% RPI_PICO_W       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@dpgeorge

This comment was marked as outdated.

@projectgus

This comment was marked as outdated.

@projectgusprojectgusforce-pushed thebugfix/rp2_machine_idle branch from5edcfaa to4bc14eeCompareJuly 5, 2024 06:48
@projectgusprojectgusforce-pushed thebugfix/rp2_machine_idle branch 3 times, most recently from3079deb to42d0b28CompareJuly 5, 2024 07:13
@codecovCodecov
Copy link

codecovbot commentedJul 5, 2024
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.43%. Comparing base(e1ecc23) to head(ba98533).

Additional details and impacted files
@@           Coverage Diff           @@##           master   #15345   +/-   ##=======================================  Coverage   98.43%   98.43%           =======================================  Files         161      161             Lines       21275    21275           =======================================  Hits        20942    20942             Misses        333      333

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report?Share it here.

@projectgus
Copy link
ContributorAuthor

This PR is ready now but depends on#15398 for correct behaviour.

@projectgusprojectgusforce-pushed thebugfix/rp2_machine_idle branch from42d0b28 to657fd48CompareJuly 5, 2024 07:20
@projectgusprojectgus marked this pull request as ready for reviewJuly 5, 2024 07:20
@projectgusprojectgus changed the titleWIP: rp2,docs: Stop machine.idle() blocking indefinitely, update docs.rp2,docs: Stop machine.idle() blocking indefinitely, update docs.Jul 5, 2024
A lot of existing code (i.e. micropython-lib lps22h, lcd160cr sensordrivers, lora sync_modem driver, usb-device-hid) calls machine.idle()inside a tight loop that is polling some condition. This reduces the powerusage compared to constantly looping, but can be faster than calling asleep function. However on a tickless port there's not always an interruptbefore the condition they are polling for, so it's difficult to restructurethis code if machine.idle() doesn't have any upper limit on execution time.This commit specifies an upper limit of 1ms before machine.idle() resumesexecution. This is already the case for all ports except rp2.This work was funded through GitHub Sponsors.Signed-off-by: Angus Gratton <angus@redyak.com.au>
Updates rp2 port to always resume from idle within 1ms max.When rp2 port went tickless the behaviour of machine.idle() changed asthere is no longer a tick interrupt to wake it up every millisecond. On aquiet system it would now block indefinitely. No other port does this.See parent commit for justification of why this change is useful.Also adds a test case that fails without this change.This work was funded through GitHub Sponsors.Signed-off-by: Angus Gratton <angus@redyak.com.au>
@projectgusprojectgusforce-pushed thebugfix/rp2_machine_idle branch from657fd48 toba98533CompareJuly 23, 2024 06:50
@projectgus
Copy link
ContributorAuthor

@dpgeorge Rebased to pick up the WFE fix, and split into two commits. I ended up making the new test rp2-only for now as there are several ports it won't pass on.

@dpgeorgedpgeorge merged commitba98533 intomicropython:masterJul 23, 2024
29 checks passed
@dpgeorge
Copy link
Member

Thanks! I tested it and the test works well (on rp2, and also stm32 just for my interest).

@projectgusprojectgus deleted the bugfix/rp2_machine_idle branchNovember 1, 2024 05:12
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@projectgus@dpgeorge

[8]ページ先頭

©2009-2025 Movatter.jp