RP2350 / Pico 2 machine.lightsleep not working correctly
Port, board and/or hardware
Pico 2 / RP2350
MicroPython version
MicroPython v1.24.0-preview.201.g269a0e0e1 on 2024-08-09; Raspberry Pi Pico2 with RP2350
Reproduction
- Install latest Firmware from https://micropython.org/download/RPI_PICO2/
- Implement
machine.lightsleep(5000)in your code - Run code.
Expected behaviour
Expected Pico 2 to enter lightsleep mode for 5 seconds, then continue to next bit of code.
Observed behaviour
lightsleep is maybe entered for a cycle or so, but code carries on and never stops executing for any time value I enter for lightsleep.
Additional Information
Complete code example:
# Lightsleep MicroPython example.
from machine import Pin
import time
led = Pin(25, Pin.OUT)
while True:
led.toggle()
time.sleep_ms(5000)
led.toggle()
machine.lightsleep(5000)
Code of Conduct
Yes, I agree
ports/rp2/modmachine.c: Fix lightsleep watchdog interactions.
Summary
Aside: I am a little hesitant to propose these changes. If the maintainers decide to reject this pull request I will understand.
Lightsleep on rp2 is gradually getting more complex and these changes would contribute to that complexity.
Fixes RP2040 issue #17228 and RP2350 issue #17229.
RP2040 issue is when watchdog timer expires while lightsleep is running the RP2040 will lock up during power-on startup.
This occurs because the power-on state machine is configured to not re-initialize the ROSC and lightsleep stopped the ROSC.
RP2350 issue is the watchdog timer does not decrement while lightsleep is running.
Once lightsleep returns the timer will start decrementing again. This occurs because lightsleep clears the bit
CLOCKS_SLEEP_EN1_CLK_SYS_WATCHDOG_BITS while sleeping.
Testing
I ran the reproduction methods from the two issues.
I also ran tests/port/rp2/rp2_lightsleep.py.
Trade-offs and Alternatives
General alternative. Document that lightsleep and watchdog don't play well together and leave the code as is.
Alternatives for RP2040 issue #17228:
- Raise issue for pico-sdk. Get them to change function _watchdog_enable() so the ROSC and XOSC do get reset on a watchdog timeout. The existing behavior in _watchdog_enable() has been there from day one. There might not be a good reason for it. Disadvantage: a possible long wait for a new pico-sdk release.
- Modify lightsleep to not stop the ROSC. I measure 4.96 milli-amps when lightsleep disables ROSC and 5.09 milli-amps when it does not. This is a small power increase.
Alternatives for the RP2350 issue #17229:
- I don't have any :-).