← index #7035PR #9438
Related · high · value 3.656
QUERY · ISSUE

rp2: Can't wake from lightsleep with Pin IRQ

openby peterhinchopened 2021-03-15updated 2026-03-24
port-rp2

On a pin change this code sample toggles the LED but never outputs "got here".

from machine import Pin, lightsleep
p25 = Pin(25, Pin.OUT)
p0 = Pin(0, Pin.IN, Pin.PULL_UP)
def foo(_):
    p25(not p25())  # Toggle the LED

p0.irq(foo, trigger=Pin.IRQ_FALLING)
while True:
    lightsleep()
    print('Got here')

The docs indicate that to fix this, the IRQ should be instantiated with wake=machine.SLEEP. Unfortunately this constant does not exist.

CANDIDATE · PULL REQUEST

ports/rp2: Fix lightsleep to work with interrupts and cyw43.

mergedby felixdoerreopened 2022-09-27updated 2023-02-14
port-rp2

This PR prevents micropython from "hanging" when using lightsleep while the wifi chip is active. A bit more rationale for the changes:
Whenever the wifi chip wants to interrupt the microcontroller to notify it for a new package, it sets the CYW43_PIN_WL_HOST_WAKE pin to high, triggering an IRQ. However, as polling the chip cannot happen in an interrupt handler, it subsequently notifies the pendsv-service to do a poll as soon as the interrupt handler ended. In order to prevent a new interrupt from happening immediately afterwards, even before the poll has run, the irq handler disables interrupts from the pin:
https://github.com/micropython/micropython/blob/bdbc44474f92db19a40b5f710a140a0bf70fb0ec/ports/rp2/mpnetworkport.c#L58

The first problem occurs, when a wifi package arrives while the main loop is in cyw43-code. In order to prevent concurrent access of the hardware, the network code blocks pendsv from running again while entering lwip code: https://github.com/micropython/micropython/blob/99c25897783122003550d58f8d31b4a8fecc8611/ports/rp2/mpnetworkport.c#L95
The same holds for direct cyw43 code (like changing the cyw43-gpios, i.e. the LED on the pico W). While the pendsv is disabled, interrupts can still occur to schedule a poll (and disable further interrupts), but it will not run. This can happen while the microcontroller is anywhere in rp2040 code.

In order to preserve power while waiting for cyw43 responses, cyw43_configport.h defines CYW43_DO_IOCTL_WAIT and CYW43_SDPCM_SEND_COMMON_WAIT to __WFI(). While this might work in most cases, there are 2 edge cases where it fails:

  • When an interrupt has already been received by the cyw43 stack, for example due to an incoming ethernet packet (I observed this deadlock on a SWP stacktrace).
  • When the interrupt from the cyw43 response comes before the microcontroller entered the __WFI() instruction.

When that happens, wfi will just block forever as no further interrupts are received. The only way to safely use wfi to wake up from an interrupt is inside a critical section, as this delays interrupts until the wfi is entered, possibly resuming immediately until interrupts are reenabled and the interrupt handler is run. Additionally this critical section needs to check whether the interrupt has already been disabled and pendsv was triggered, as in such a case, wfi can never be woken up, and needs to be skipped, because there is already a package from the network chip waiting. Note that this turns cyw43_yield into a nop (and thereby the cyw43-loops into busy waits) from the second time onwards, as after the first call, a pendsv request will definitely be pending. More logic could be added, to explicitly enable the interrupt in this case.

Regarding lightsleep: This code has a similar problem. When an interrupt occurs during lightsleep, the IRQ and pendsv handler and thereby poll are run immediately, with the clocks still disabled, causing the spi transfers to fail. If we don't want to add complex logic inside the IRQ handler we need to protect the whole lightsleep procedure form interrupts with a critical section, exiting out early if an interrupt is pending for whatever reason. Only then we can start to shut down clocks and only enable interrupts when the system is ready again. Other interrupt handlers might also be happy, that they are only run when the system is fully operational.

I've tested the changes on a pico W which I send into lightsleep within an endless loop and pinging from extern. With these changes the pico doesn't lock up anymore but wakes up, processes the packages correctly, and goes back to lightsleep.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied