Skip to content
systemcoding.dev
Embedded

Reduce RP2040 Power With Dormant Mode and Clock Gating

Cut RP2040 current by gating unused clocks with the CLOCKS sleep-enable registers and entering dormant mode, then wake on a GPIO edge with pico-extras.

7 min read
Summarize in AI
Flat illustration of a microcontroller wired to an LED and a sensor, with the Raspberry Pi, Arm and C logos.

The RP2040 has no dedicated low-power mode register in the sense an STM32 does. It has three layers that you combine: clock gating, which stops the clocks to blocks you are not using; the WFI sleep, where the cores stop and the chip keeps its clocks running; and dormant mode, where the crystal oscillator itself is stopped and the whole chip waits for a GPIO event. Each layer saves more and costs more to leave, and the order to try them is the order of increasing effort. This post explains the clock tree features that matter, shows the SDK calls for gating and for dormant mode, and walks a button-wake example. It uses Pico SDK 2.0.0, the pico_sleep library from pico-extras, and arm-none-eabi-gcc 13.3 on a Raspberry Pi Pico. All register names come from the RP2040 Datasheet, in the Clocks and the XOSC chapters.

Clock gating: stop what you do not use

Every peripheral on the RP2040 gets its clock from the CLOCKS block, and that block has enable registers per condition. For each peripheral there is a bit in WAKE_EN0 and WAKE_EN1, which says whether its clock runs while a core is running, and a bit in SLEEP_EN0 and SLEEP_EN1, which says whether it runs while both cores are in WFI sleep. After reset all the bits are set, so everything is clocked in both states.

The SDK exposes the registers through clocks_hw. To keep only the clocks the application needs when the cores sleep, clear the others:

gating.cc
#include "pico/stdlib.h"
#include "hardware/clocks.h"
#include "hardware/structs/clocks.h"

static void keep_only_uart_and_timer_clocked_in_sleep(void)
{
    // Clear everything, then enable what must keep running during WFI sleep.
    clocks_hw->sleep_en0 = 0;
    clocks_hw->sleep_en1 = 0;

    // The timer and the UART0 peripheral clocks, so an alarm or a byte can wake the core.
    clocks_hw->sleep_en0 |= CLOCKS_SLEEP_EN0_CLK_SYS_SRAM0_BITS   // SRAM banks and ROM must stay
                         |  CLOCKS_SLEEP_EN0_CLK_SYS_SRAM1_BITS
                         |  CLOCKS_SLEEP_EN0_CLK_SYS_SRAM2_BITS
                         |  CLOCKS_SLEEP_EN0_CLK_SYS_SRAM3_BITS
                         |  CLOCKS_SLEEP_EN0_CLK_SYS_ROM_BITS
                         |  CLOCKS_SLEEP_EN0_CLK_SYS_BUSFABRIC_BITS
                         |  CLOCKS_SLEEP_EN0_CLK_SYS_BUSCTRL_BITS;
    clocks_hw->sleep_en1 |= CLOCKS_SLEEP_EN1_CLK_SYS_TIMER_BITS
                         |  CLOCKS_SLEEP_EN1_CLK_SYS_UART0_BITS
                         |  CLOCKS_SLEEP_EN1_CLK_PERI_UART0_BITS;
}

Clearing a bit that something depends on stops that thing. The memory banks in this example must be kept, or the cores cannot execute after the wake-up: the datasheet lists bus fabric, SRAM and ROM among the clocks the cores need. The safest way to build such a mask is to start from the application's actual behaviour: write the minimal set, test that the chip wakes, and add bits when it does not.

The SDK also has a per-clock stop. clock_stop(clk_usb) and clock_stop(clk_adc) stop the whole clock generator when you know the block is unused, and the datasheet's "Clock generators" section gives each one's enable bit:

stop_clocks.cc
clock_stop(clk_usb);    // if you never use USB
clock_stop(clk_adc);    // if you do not sample
clock_stop(clk_rtc);    // if you do not use the RTC

You must not stop clk_sys or clk_ref this way. Lowering the system clock frequency also helps, since dynamic power is proportional to frequency, and set_sys_clock_khz(48000, true) runs the system at 48 MHz instead of the 125 MHz that the SDK selects by default. That is a larger saving than most gating, and it needs no change to wake behaviour.

Sleep with WFI

With the gating in place, the cores sleep with __wfi() from the SDK, which executes the Arm instruction. The second core is halted if you never started it, and the first sleeps until an interrupt. The chip keeps its oscillators running, so the wake-up is instant.

wfi_loop.cc
#include "hardware/sync.h"

static volatile bool tick;

static int64_t alarm_cb(alarm_id_t id, void *user)
{
    tick = true;
    return 0;
}

int main(void)
{
    stdio_init_all();
    keep_only_uart_and_timer_clocked_in_sleep();

    for (;;) {
        add_alarm_in_ms(1000, alarm_cb, NULL, false);
        while (!tick) {
            __wfi();            // pending IRQ wakes it; race: see below
        }
        tick = false;
        printf("tick\n");
    }
}
Text
tick
tick
tick

The loop has the classic lost-wake-up window between the test and __wfi(), which the Cortex-M WFI post covers. For a one-second alarm the harm is a delayed tick, but for something that matters, use save_and_disable_interrupts() and restore_interrupts() around the test, so a pending interrupt still wakes wfi.

Dormant mode: stop the oscillator

Dormant mode is the lowest-power state of the RP2040 that keeps the chip powered. In it the crystal oscillator (XOSC) is stopped, and so every clock derived from it. Nothing is clocked. The chip waits for an edge or level on a GPIO, detected asynchronously by the GPIO block, which restarts the oscillator and lets the cores continue. RAM keeps its contents and execution continues at the instruction after the one that entered dormant mode.

The SDK itself does not include the sleep functions. They are in the pico_sleep library of pico-extras, which you add as a separate repository:

CMakeLists.txtcmake
cmake_minimum_required(VERSION 3.13)
include(pico_sdk_import.cmake)
include(pico_extras_import.cmake)

project(dormant_demo C CXX ASM)
pico_sdk_init()

add_executable(dormant_demo main.c)
target_link_libraries(dormant_demo pico_stdlib pico_sleep hardware_clocks hardware_rosc)
pico_add_extra_outputs(dormant_demo)

The program switches the system to run from the crystal alone, goes dormant until a button edge, and then restores the clocks:

dormant.cc
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/sleep.h"
#include "hardware/clocks.h"
#include "hardware/rosc.h"

#define WAKE_GPIO 15

int main(void)
{
    stdio_init_all();
    gpio_init(PICO_DEFAULT_LED_PIN);
    gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
    gpio_pull_up(WAKE_GPIO);

    for (;;) {
        printf("going dormant\n");
        stdio_flush();
        uart_default_tx_wait_blocking();

        sleep_run_from_xosc();                          // clk_sys from the crystal only
        sleep_goto_dormant_until_pin(WAKE_GPIO, true, false);  // edge, falling
        sleep_power_up();                               // restart PLLs and clocks

        printf("awake\n");
        gpio_put(PICO_DEFAULT_LED_PIN, 1);
        sleep_ms(500);
        gpio_put(PICO_DEFAULT_LED_PIN, 0);
    }
}
Text
going dormant
awake
going dormant
awake

sleep_run_from_xosc first reconfigures every clock generator to run from the crystal oscillator and turns off the PLLs and the ring oscillator, because the dormant entry stops XOSC and anything still sourced elsewhere would continue to draw current. sleep_goto_dormant_until_pin(pin, edge, high) sets the GPIO interrupt for the wake condition, with edge true for an edge and high false for the falling one, then writes the XOSC dormant key, which stops the oscillator. When the edge arrives, the oscillator restarts, and execution continues. sleep_power_up restores the PLLs and the clock tree to the configuration the SDK started with.

Two details trip people. The UART output must be flushed before going dormant, because the clock for the UART stops, and a half-sent byte is lost, hence the wait on the UART transmitter. And the wake pin must be held in a defined state: the internal pull-up here keeps the line high until the button pulls it low.

What you give up

Dormant mode costs the time for the crystal to restart, which the datasheet describes with the oscillator's start-up delay and a counter that waits for stability. The timer peripheral is stopped, so the system's microsecond timer does not advance while dormant. If your program relied on time_us_64() to measure elapsed time across the sleep, it is wrong afterwards, and you need an external RTC or the on-chip RTC clocked from a different source. The RP2040's on-chip RTC runs from the 48 MHz clock divided down, which also stops, so the RTC does not run in dormant mode unless you provide an external 1 Hz or 32 kHz source.

Also note the USB. A dormant chip is not a USB device, so the host sees a disconnect, and the example must expect a dropped serial port when run from a laptop.

Comparing the three steps

TechniqueWake sourceWake timeState keptGains
Lower clk_sysNone neededInstantAllDynamic power falls with frequency
WFI with sleep gatingAny enabled IRQInstantAllStops core and ungated clocks
DormantGPIO edge or level onlyOscillator restartRAM and registersStops all clocks, XOSC included
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.