Loading...
Searching...
No Matches
low_power.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _PICO_LOW_POWER_H_
8#define _PICO_LOW_POWER_H_
9
10#include "pico.h"
11#include "hardware/timer.h"
12#include "pico/time.h"
13#include "pico/aon_timer.h"
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
71
72// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_PICO_LOW_POWER, Enable/disable assertions in the pico_low_power module, type=bool, default=0, group=pico_low_power
73#ifndef PARAM_ASSERTIONS_ENABLED_PICO_LOW_POWER
74#define PARAM_ASSERTIONS_ENABLED_PICO_LOW_POWER 0
75#endif
76
77// PICO_CONFIG: PICO_LOW_POWER_MIN_AON_SLEEP_TIME_MS, Minimum supported time to spend in sleep using the AON timer in milliseconds, type=int, default=2000 on RP2040, 10 otherwise, group=pico_low_power
78#ifndef PICO_LOW_POWER_MIN_AON_SLEEP_TIME_MS
79#if PICO_RP2040
80#define PICO_LOW_POWER_MIN_AON_SLEEP_TIME_MS 2000
81#else
82#define PICO_LOW_POWER_MIN_AON_SLEEP_TIME_MS 10
83#endif
84#endif
85
86// PICO_CONFIG: PICO_LOW_POWER_MIN_DORMANT_TIME_MS, Minimum supported time to spend dormant in milliseconds, type=int, default=2000 on RP2040, 10 otherwise, group=pico_low_power
87#ifndef PICO_LOW_POWER_MIN_DORMANT_TIME_MS
88#if PICO_RP2040
89#define PICO_LOW_POWER_MIN_DORMANT_TIME_MS 2000
90#else
91#define PICO_LOW_POWER_MIN_DORMANT_TIME_MS 10
92#endif
93#endif
94
95// PICO_CONFIG: PICO_LOW_POWER_MIN_PSTATE_TIME_MS, Minimum supported time to spend in Pstate in milliseconds, type=int, default=10, group=pico_low_power
96#ifndef PICO_LOW_POWER_MIN_PSTATE_TIME_MS
97#define PICO_LOW_POWER_MIN_PSTATE_TIME_MS 10
98#endif
99
100#include "hardware/clocks.h"
101#if HAS_RP2040_RTC
102#include "hardware/rtc.h"
103#endif
104#if HAS_POWMAN_TIMER
105#include "hardware/powman.h"
106#endif
107
108typedef enum {
109 DORMANT_CLOCK_SOURCE_XOSC,
110 DORMANT_CLOCK_SOURCE_ROSC,
111#if PICO_RP2040
112 DORMANT_CLOCK_SOURCE_RTC,
113#else
114 DORMANT_CLOCK_SOURCE_LPOSC,
115#endif
116 NUM_DORMANT_CLOCK_SOURCES
117} dormant_clock_source_t;
118
119#if PICO_RP2040
120#define DORMANT_CLOCK_SOURCE_DEFAULT DORMANT_CLOCK_SOURCE_RTC
121#else
122#define DORMANT_CLOCK_SOURCE_DEFAULT DORMANT_CLOCK_SOURCE_LPOSC
123#endif
124
125#if PICO_RP2040
126#define DORMANT_CLOCK_HZ_DEFAULT RTC_CLOCK_FREQ_HZ
127#else
128#define DORMANT_CLOCK_HZ_DEFAULT 0 // ignored
129#endif
130
131#if HAS_POWMAN_TIMER
132typedef void (*low_power_pstate_resume_func)(pstate_bitset_t *pstate);
133#endif
134
145void low_power_set_pins_low_leakage_exclude_mask(uint32_t exclude_mask);
146
154#if NUM_BANK0_GPIOS <= 32
155static inline void low_power_set_pins_low_leakage_exclude_mask64(uint64_t exclude_mask) {
156 low_power_set_pins_low_leakage_exclude_mask((uint32_t)exclude_mask);
157}
158#else
159void low_power_set_pins_low_leakage_exclude_mask64(uint64_t exclude_mask);
160#endif
161
170int low_power_sleep_until_irq(const clock_dest_bitset_t *keep_enabled);
171
184int low_power_sleep_until_timer(timer_hw_t *timer, absolute_time_t until, const clock_dest_bitset_t *keep_enabled, bool exclusive);
185
196static inline int low_power_sleep_until_default_timer(absolute_time_t until, const clock_dest_bitset_t *keep_enabled, bool exclusive) {
197 return low_power_sleep_until_timer(PICO_DEFAULT_TIMER_INSTANCE(), until, keep_enabled, exclusive);
198}
199
211int low_power_sleep_until_aon_timer(absolute_time_t until, const clock_dest_bitset_t *keep_enabled, bool exclusive);
212
213
227int low_power_sleep_until_gpio_pin_state(uint gpio_pin, bool edge, bool high, const clock_dest_bitset_t *keep_enabled, bool exclusive);
228
238#if PICO_RP2040
239int low_power_set_external_clock_source(uint src_hz, uint gpio_pin);
240#else
241static inline int low_power_set_external_clock_source(__unused uint src_hz, __unused uint gpio_pin) {
242 return PICO_OK;
243}
244#endif
245
272int low_power_dormant_until_aon_timer(absolute_time_t until, dormant_clock_source_t dormant_clock_source, const clock_dest_bitset_t *keep_enabled);
273
300int low_power_dormant_until_gpio_pin_state(uint gpio_pin, bool edge, bool high, dormant_clock_source_t dormant_clock_source, const clock_dest_bitset_t *keep_enabled);
301
302#if HAS_POWMAN_TIMER
320int low_power_pstate_until_aon_timer(absolute_time_t until, pstate_bitset_t *pstate, low_power_pstate_resume_func resume_func);
321
339int low_power_pstate_until_gpio_pin_state(uint gpio_pin, bool edge, bool high, pstate_bitset_t *pstate, low_power_pstate_resume_func resume_func);
340
347pstate_bitset_t *low_power_persistent_pstate_get(pstate_bitset_t *pstate);
348#endif
349
350
351// Convenience functions to avoid needing the correct absolute_time_t
352
364static inline bool low_power_start_aon_timer_at_time_ms(uint64_t ms) {
365 struct timespec ts;
366 ms_to_timespec(ms, &ts);
367 return aon_timer_start(&ts);
368}
369
379static inline bool low_power_start_aon_timer(void) {
380 if (aon_timer_is_running()) return true;
382}
383
394static inline int low_power_sleep_for_us(timer_hw_t *timer, uint64_t us, const clock_dest_bitset_t *keep_enabled, bool exclusive) {
395 return low_power_sleep_until_timer(timer, make_timeout_time_us(us), keep_enabled, exclusive);
396}
397
408static inline int low_power_sleep_for_ms(uint32_t ms, const clock_dest_bitset_t *keep_enabled, bool exclusive) {
409 return low_power_sleep_until_default_timer(make_timeout_time_ms(ms), keep_enabled, exclusive);
410}
411
423static inline int low_power_dormant_for_ms(uint32_t ms, dormant_clock_source_t dormant_clock_source, const clock_dest_bitset_t *keep_enabled) {
425 return low_power_dormant_until_aon_timer(aon_timer_make_timeout_time_ms(ms), dormant_clock_source, keep_enabled);
426}
427
428#if HAS_POWMAN_TIMER
439static inline int low_power_pstate_for_ms(uint32_t ms, pstate_bitset_t *pstate, low_power_pstate_resume_func resume_func) {
442}
443#endif
444
445#ifdef __cplusplus
446}
447#endif
448
449#endif
#define PICO_DEFAULT_TIMER_INSTANCE()
Returns the default timer instance on the platform based on the setting of PICO_DEFAULT_TIMER.
Definition timer.h:182
bool aon_timer_is_running(void)
Check if the AON timer is running.
Definition aon_timer.c:252
bool aon_timer_start(const struct timespec *ts)
Start the AON timer running using the specified timespec as the current time.
Definition aon_timer.c:195
static absolute_time_t aon_timer_make_timeout_time_ms(uint32_t ms)
Convenience method to get the timestamp a number of milliseconds from the current time of the AON tim...
Definition aon_timer.h:197
@ PICO_OK
No error; the operation succeeded.
Definition error.h:23
int low_power_pstate_until_aon_timer(absolute_time_t until, pstate_bitset_t *pstate, low_power_pstate_resume_func resume_func)
Go to Pstate until time using AON timer.
Definition low_power.c:750
static int low_power_sleep_until_default_timer(absolute_time_t until, const clock_dest_bitset_t *keep_enabled, bool exclusive)
Sleep until time using default timer.
Definition low_power.h:196
static int low_power_set_external_clock_source(__unused uint src_hz, __unused uint gpio_pin)
Set the external clock source for the AON timer.
Definition low_power.h:241
void low_power_set_pins_low_leakage_exclude_mask(uint32_t exclude_mask)
Set all pins to a low leakage state.
Definition low_power.c:227
static int low_power_pstate_for_ms(uint32_t ms, pstate_bitset_t *pstate, low_power_pstate_resume_func resume_func)
Go to Pstate for a number of milliseconds.
Definition low_power.h:439
int low_power_dormant_until_gpio_pin_state(uint gpio_pin, bool edge, bool high, dormant_clock_source_t dormant_clock_source, const clock_dest_bitset_t *keep_enabled)
Go dormant until GPIO pin state changes.
Definition low_power.c:632
pstate_bitset_t * low_power_persistent_pstate_get(pstate_bitset_t *pstate)
Get Pstate which keeps persistent data powered on.
Definition low_power.c:683
static bool low_power_start_aon_timer(void)
Start the AON timer at the current system time.
Definition low_power.h:379
int low_power_sleep_until_gpio_pin_state(uint gpio_pin, bool edge, bool high, const clock_dest_bitset_t *keep_enabled, bool exclusive)
Sleep until GPIO pin state changes.
Definition low_power.c:376
static int low_power_sleep_for_us(timer_hw_t *timer, uint64_t us, const clock_dest_bitset_t *keep_enabled, bool exclusive)
Sleep for a number of microseconds.
Definition low_power.h:394
static bool low_power_start_aon_timer_at_time_ms(uint64_t ms)
Start the AON timer at a specific time in milliseconds.
Definition low_power.h:364
static int low_power_dormant_for_ms(uint32_t ms, dormant_clock_source_t dormant_clock_source, const clock_dest_bitset_t *keep_enabled)
Go dormant for a number of milliseconds.
Definition low_power.h:423
int low_power_pstate_until_gpio_pin_state(uint gpio_pin, bool edge, bool high, pstate_bitset_t *pstate, low_power_pstate_resume_func resume_func)
Go to Pstate until GPIO pin state changes.
Definition low_power.c:769
int low_power_sleep_until_aon_timer(absolute_time_t until, const clock_dest_bitset_t *keep_enabled, bool exclusive)
Sleep until time using AON timer.
Definition low_power.c:322
int low_power_dormant_until_aon_timer(absolute_time_t until, dormant_clock_source_t dormant_clock_source, const clock_dest_bitset_t *keep_enabled)
Go dormant until time using AON timer.
Definition low_power.c:562
int low_power_sleep_until_timer(timer_hw_t *timer, absolute_time_t until, const clock_dest_bitset_t *keep_enabled, bool exclusive)
Sleep until time using timer.
Definition low_power.c:269
static int low_power_sleep_for_ms(uint32_t ms, const clock_dest_bitset_t *keep_enabled, bool exclusive)
Sleep for a number of milliseconds.
Definition low_power.h:408
int low_power_sleep_until_irq(const clock_dest_bitset_t *keep_enabled)
Sleep until an interrupt occurs.
Definition low_power.c:248
static void low_power_set_pins_low_leakage_exclude_mask64(uint64_t exclude_mask)
Set all pins to a low leakage state (64-bit mask version).
Definition low_power.h:155
static absolute_time_t make_timeout_time_us(uint64_t us)
Convenience method to get the timestamp a number of microseconds from the current time.
Definition time.h:148
static absolute_time_t make_timeout_time_ms(uint32_t ms)
Convenience method to get the timestamp a number of milliseconds from the current time.
Definition time.h:158
uint64_t absolute_time_t
An opaque 64 bit timestamp in microseconds.
Definition types.h:43
Definition timer.h:26