lightsleep() not working properly Pi Pico WH v1.24.0
Port, board and/or hardware
Raspberry Pi Pico WH
MicroPython version
MicroPython v1.24.0 on 2024-10-25; Raspberry Pi Pico W with RP2040
Reproduction
Attempt following code, both with lightsleep() having no ms argument and one with:
import machine
from machine import Pin, deepsleep, lightsleep
def wake_up(pin):
# Handle wake-up logic here
led.toggle()
print("Button pressed, waking up!")
# Configure buttons
button = Pin(11, Pin.IN, Pin.PULL_UP) # Example pin
led = Pin(20, Pin.OUT)
led.on()
# Set up interrupt
button.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=wake_up)
# Go to sleep until a button is pressed
lightsleep()
Expected behaviour
Expect LED to react to button press when asleep and execute wake_up function. LED is on by default. LED should toggle/turn off when button is pressed down, both when machine is asleep and not asleep.
Observed behaviour
USB power tester indicates that the board sleeps only when NO MILLISECONDS ARGUMENT IS SPECIFIED.
When milliseconds ARE SPECIFIED, board does not appear to sleep as power consumption does not drop according to the USB tester. The board continues to work at full power, and the LED reacts to the button.
When NO MILLISECONDS ARE SPECIFIED, board appears to go to sleep (reducing power from >10ma to 0-1ma. But button does not do anything to LED.
Additional Information
I have tried adding the wake argument to button.irq, but Micropython says that extra keyword arguments are present.
I understand sleeping may have issues on the Pico W models. I have also tried turning off WiFi/setting the pin associated with Wifi to "low". That does not seem to have any impact whatsoever, since my code never explicitly turns Wifi on
https://forums.raspberrypi.com/viewtopic.php?p=2266590#p2266590
Code of Conduct
Yes, I agree
PICO machine.lightsleep resets registers SLEEP_EN0 and SLEEP_EN1
Port, board and/or hardware
rp2
MicroPython version
MicroPython v1.24.1 on 2024-11-29; Raspberry Pi Pico W with RP2040
Reproduction
I reproduced the problem with thonny running on a RPI 4 (not important).
I using code from my github project project RP2-PowerControl:
- Download power_ctrl_2040.py and power_ctrl_abstract.py to the pico W.
- Run the following code:
from machine import lightsleep
from power_ctrl_2040 import PowerCtrl
pwr = PowerCtrl()
print(pwr) # displays default values for WAKE_EN0, WAKE_EN1, SLEEP_EN0, and SLEEP_EN1
pwr.disable_while_sleeping(
pwr.EN1_CLK_SYS_UART1,
pwr.EN1_CLK_PERI_UART1,
pwr.EN0_CLK_SYS_SRAM3,
pwr.EN0_CLK_SYS_SRAM2
)
print(pwr) # note SLEEP_EN0 and SLEEP_EN1 changed.
lightsleep(100)
print(pwr) # back to default values
Output I see is:
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: FFFFFFFF sleep_en1: 00007FFF
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: 3FFFFFFF sleep_en1: 00007CFF
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: FFFFFFFF sleep_en1: 00007FFF
The third time pwr is printed should be the same as the second time.
Expected behaviour
The problem does not exist on earlier versions but unfortunately can't be reproduced because lightsleep breaks USB.
I coded the fix off the latest micropython. The version information for my build is:
MicroPython c73204128-dirty on 2024-12-29; Raspberry Pi Pico W with RP2040
Correct output is:
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: FFFFFFFF sleep_en1: 00007FFF
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: 3FFFFFFF sleep_en1: 00007CFF
PowerCtrl for RP2040
wake_en0: FFFFFFFF wake_en1: 00007FFF
sleep_en0: 3FFFFFFF sleep_en1: 00007CFF
Observed behaviour
The code in my project RP2-PowerControl is intended to reduce power consumed when the RP2040 (or RP2350) are sleeping for any reason.
Specifically, both CPU cores need to be asleep and no DMA active.
This includes calls to time.sleep_ms() for instance.
But also includes all cases where either __WFI() or __WFE() are called.
Because of this defect calling machine.lightsleep() resets SLEEP_EN0 and SLEEP_EN1 to their power on values.
The expected power savings will go away.
Additional Information
The problem was introduced in commit d1423ef.
Prior to that commit machine.lightsleep save the current value of SLEEP_EN0 and SLEEP_EN1 on entry and restore them on exit.
As mentioned, I have the fix coded and will submit a pull request shortly.
Code of Conduct
Yes, I agree