Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restore rp2040 sleep clocks after explicit light sleep #10018

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dhalbert
Copy link
Collaborator

@dhalbert dhalbert commented Jan 30, 2025

On RP2040, light_sleep_until_alarms() would turn off some clocks during the light sleep by clearing some bits in clocks_hw->sleep_en0 and clocks_hw->sleep_en1. Those clocks are turned off when a wfi() or wfe() happens. But light_sleep_until_alarms() failed to restore the original values of those registers after a light sleep. So the next time a wfi() or wfe() was used, even not during an explicit light sleep, those clocks would get turned off. This caused the clocks to go off during time.sleep() (which uses wfi()), for instance.

To fix, I saved and restored the register values.

Tested with a simplified version of the program in #8561 (I just put an LED on A1).

import alarm
import board
import pwmio
import time

piezo = pwmio.PWMOut(board.A1, duty_cycle=0, frequency=440, variable_frequency=False)
piezo.duty_cycle = 65535 // 2  # On 50%
time.sleep(1)
piezo.duty_cycle = 0 # off

t = 5
print("Sleeping for", 5, "seconds...")
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + t)
alarm.light_sleep_until_alarms(time_alarm)
while True:
    print("Beeping...")
    piezo.duty_cycle = 65535 // 2  # On 50%
    time.sleep(0.25)
    piezo.duty_cycle = 0 # off
    time.sleep(0.25)

@dhalbert dhalbert requested review from tannewt and jepler January 30, 2025 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rp2040 light_sleep did not restore clocks
1 participant