Loading...
Searching...
No Matches
powman.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _HARDWARE_POWMAN_H
8#define _HARDWARE_POWMAN_H
9
10#include "pico.h"
11#include "hardware/structs/powman.h"
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
24
25// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_HARDWARE_POWMAN, Enable/disable hardware_powman assertions, type=bool, default=0, group=hardware_powman
26#ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_POWMAN
27#define PARAM_ASSERTIONS_ENABLED_HARDWARE_POWMAN 0
28#endif
29
30// PICO_CONFIG: PICO_POWMAN_CALIBRATE_LPOSC_FROM_OTP, Use the OTP calibration value for the low power oscillator frequency, type=bool, default=1, group=hardware_powman
31#ifndef PICO_POWMAN_CALIBRATE_LPOSC_FROM_OTP
32#define PICO_POWMAN_CALIBRATE_LPOSC_FROM_OTP 1
33#endif
34
47
54
59void powman_timer_set_1khz_tick_source_lposc_with_hz(uint32_t lposc_freq_hz);
60
67
72void powman_timer_set_1khz_tick_source_xosc_with_hz(uint32_t xosc_freq_hz);
73
79
88void powman_timer_enable_gpio_1hz_sync(uint32_t gpio);
89
94
98uint64_t powman_timer_get_ms(void);
99
105void powman_timer_set_ms(uint64_t time_ms);
106
115void powman_timer_enable_alarm_at_ms(uint64_t alarm_time_ms);
116
124
133static inline void powman_set_bits(volatile uint32_t *reg, uint32_t bits) {
134 invalid_params_if(HARDWARE_POWMAN, bits >> 16);
135 hw_set_bits(reg, POWMAN_PASSWORD_BITS | bits);
136}
137
147static inline void powman_clear_bits(volatile uint32_t *reg, uint32_t bits) {
148 invalid_params_if(HARDWARE_POWMAN, bits >> 16);
149 hw_clear_bits(reg, POWMAN_PASSWORD_BITS | bits);
150}
151
155static inline bool powman_timer_is_running(void) {
156 return powman_hw->timer & POWMAN_TIMER_RUN_BITS;
157}
158
165static inline void powman_timer_stop(void) {
166 powman_clear_bits(&powman_hw->timer, POWMAN_TIMER_RUN_BITS);
167}
168
172static inline void powman_timer_pause(void) {
173 powman_clear_bits(&powman_hw->timer, POWMAN_TIMER_RUN_BITS);
175}
176
180static inline void powman_timer_start(void) {
181 powman_set_bits(&powman_hw->timer, POWMAN_TIMER_RUN_BITS);
182}
183
190static inline void powman_clear_alarm(void) {
191 powman_clear_bits(&powman_hw->timer, POWMAN_TIMER_ALARM_BITS);
192}
193
204typedef enum powman_power_domains powman_power_domain_t;
205
206typedef uint32_t powman_power_state;
207
208typedef fixed_bitset_type(POWMAN_POWER_DOMAIN_COUNT) pstate_bitset_t;
209#define pstate_bitset_none() fixed_bitset_with_fill(pstate_bitset_t, POWMAN_POWER_DOMAIN_COUNT, 0)
210#define pstate_bitset_all() fixed_bitset_with_fill(pstate_bitset_t, POWMAN_POWER_DOMAIN_COUNT, 1)
211
212static inline pstate_bitset_t *pstate_bitset_remove_all(pstate_bitset_t *domains) {
213 fixed_bitset_clear_all(&domains->bitset);
214 return domains;
215}
216
217static inline pstate_bitset_t *pstate_bitset_add_all(pstate_bitset_t *domains) {
218 fixed_bitset_set_all(&domains->bitset);
219 return domains;
220}
221
222static inline pstate_bitset_t *pstate_bitset_add(pstate_bitset_t *domains, powman_power_domain_t domain) {
223 fixed_bitset_set(&domains->bitset, domain);
224 return domains;
225}
226
227static inline pstate_bitset_t *pstate_bitset_remove(pstate_bitset_t *domains, powman_power_domain_t domain) {
228 fixed_bitset_clear(&domains->bitset, domain);
229 return domains;
230}
231
232static inline bool pstate_bitset_is_set(pstate_bitset_t *domains, powman_power_domain_t domain) {
233 return fixed_bitset_get(&domains->bitset, domain);
234}
235
236static inline bool pstate_bitset_none_set(pstate_bitset_t *domains) {
237 return fixed_bitset_is_empty(&domains->bitset);
238}
239
240static inline pstate_bitset_t *pstate_bitset_from_powman_power_state(pstate_bitset_t *domains, powman_power_state pstate) {
241 static_assert(sizeof(powman_power_state) <= sizeof(uint32_t), "");
242 fixed_bitset_write_word(&domains->bitset, 0, pstate);
243 return domains;
244}
245
246static inline powman_power_state pstate_bitset_to_powman_power_state(pstate_bitset_t *domains) {
247 return fixed_bitset_read_word(&domains->bitset, 0);
248}
249
253powman_power_state powman_get_power_state(void);
254
266int powman_set_power_state(powman_power_state state);
267
268#define POWMAN_POWER_STATE_NONE 0
269
275static inline powman_power_state powman_power_state_with_domain_on(powman_power_state orig, enum powman_power_domains domain) {
276 invalid_params_if(HARDWARE_POWMAN, domain >= POWMAN_POWER_DOMAIN_COUNT);
277 return orig | (1u << domain);
278}
279
285static inline powman_power_state powman_power_state_with_domain_off(powman_power_state orig, enum powman_power_domains domain) {
286 invalid_params_if(HARDWARE_POWMAN, domain >= POWMAN_POWER_DOMAIN_COUNT);
287 return orig &= ~(1u << domain);
288}
289
295static inline bool powman_power_state_is_domain_on(powman_power_state state, enum powman_power_domains domain) {
296 invalid_params_if(HARDWARE_POWMAN, domain >= POWMAN_POWER_DOMAIN_COUNT);
297 return state & (1u << domain);
298}
299
304void powman_enable_alarm_wakeup_at_ms(uint64_t alarm_time_ms);
305
313void powman_enable_gpio_wakeup(uint gpio_wakeup_num, uint32_t gpio, bool edge, bool high);
314
319
324void powman_disable_gpio_wakeup(uint gpio_wakeup_num);
325
330
337bool powman_configure_wakeup_state(powman_power_state sleep_state, powman_power_state wakeup_state);
338
349static inline void powman_set_debug_power_request_ignored(bool ignored) {
350 if (ignored)
351 powman_set_bits(&powman_hw->dbg_pwrcfg, 1);
352 else
353 powman_clear_bits(&powman_hw->dbg_pwrcfg, 1);
354}
355
356#ifdef __cplusplus
357}
358#endif
359
360#endif
static fixed_bitset_t * fixed_bitset_clear(fixed_bitset_t *bitset, uint bit_index)
Clear a single bit in the bitset.
Definition fixed_bitset.h:202
bool fixed_bitset_is_empty(fixed_bitset_t *bitset)
Determine if bitset is empty.
Definition fixed_bitset.c:17
static fixed_bitset_t * fixed_bitset_set(fixed_bitset_t *bitset, uint bit_index)
Set a single bit in the bitset.
Definition fixed_bitset.h:188
static bool fixed_bitset_get(const fixed_bitset_t *bitset, uint bit_index)
Get the value of a single bit in the bitset.
Definition fixed_bitset.h:231
static fixed_bitset_t * fixed_bitset_clear_all(fixed_bitset_t *bitset)
Clear all bits in the bitset.
Definition fixed_bitset.h:151
#define fixed_bitset_type(N)
Macro used to define a fixed-size bitset of a given size.
Definition fixed_bitset.h:48
static fixed_bitset_t * fixed_bitset_set_all(fixed_bitset_t *bitset)
Set all bits in the bitset.
Definition fixed_bitset.h:162
static uint32_t fixed_bitset_read_word(const fixed_bitset_t *bitset, uint word_num)
Read a word in the bitset.
Definition fixed_bitset.h:138
static fixed_bitset_t * fixed_bitset_write_word(fixed_bitset_t *bitset, uint word_num, uint32_t value)
Write a word in the bitset.
Definition fixed_bitset.h:124
static __force_inline void hw_set_bits(io_rw_32 *addr, uint32_t mask)
Atomically set the specified bits to 1 in a HW register.
Definition address_mapped.h:135
static __force_inline void hw_clear_bits(io_rw_32 *addr, uint32_t mask)
Atomically clear the specified bits to 0 in a HW register.
Definition address_mapped.h:145
static void powman_timer_start(void)
Start the powman timer.
Definition powman.h:180
void powman_timer_set_1khz_tick_source_lposc(void)
Use the ~32KHz low power oscillator as the powman timer source.
Definition powman.c:85
static void powman_set_debug_power_request_ignored(bool ignored)
Ignore wake up when the debugger is attached.
Definition powman.h:349
void powman_enable_alarm_wakeup_at_ms(uint64_t alarm_time_ms)
Wake up from an alarm at a given time.
Definition powman.c:283
static bool powman_power_state_is_domain_on(powman_power_state state, enum powman_power_domains domain)
Helper function to check if a domain is on in a given powman_power_state.
Definition powman.h:295
int powman_set_power_state(powman_power_state state)
Set the power state.
Definition powman.c:191
void powman_timer_enable_gpio_1hz_sync(uint32_t gpio)
Use a 1Hz external signal as the powman timer source for seconds only.
Definition powman.c:170
static void powman_clear_alarm(void)
Clears the powman alarm.
Definition powman.h:190
void powman_timer_disable_gpio_1hz_sync(void)
Stop using 1Hz external signal as the powman timer source for seconds.
Definition powman.c:175
static void powman_clear_bits(volatile uint32_t *reg, uint32_t bits)
hw_clear_bits helper function
Definition powman.h:147
void powman_disable_alarm_wakeup(void)
Disable waking up from alarm.
Definition powman.c:288
static bool powman_timer_is_running(void)
Determine if the powman timer is running.
Definition powman.h:155
static void powman_timer_stop(void)
Stop the powman timer.
Definition powman.h:165
uint64_t powman_timer_get_ms(void)
Returns current time in ms.
Definition powman.c:49
void powman_timer_set_1khz_tick_source_gpio(uint32_t gpio)
Use a 1KHz external tick as the powman timer source.
Definition powman.c:165
static void powman_timer_pause(void)
Pause the powman timer.
Definition powman.h:172
bool powman_configure_wakeup_state(powman_power_state sleep_state, powman_power_state wakeup_state)
Configure sleep state and wakeup state.
Definition powman.c:235
static void powman_set_bits(volatile uint32_t *reg, uint32_t bits)
hw_set_bits helper function
Definition powman.h:133
void powman_timer_set_1khz_tick_source_xosc_with_hz(uint32_t xosc_freq_hz)
Use the crystal oscillator (specifying frequency) as the powman timer source.
Definition powman.c:116
void powman_timer_set_1khz_tick_source_xosc(void)
Use the crystal oscillator as the powman timer source.
Definition powman.c:112
void powman_timer_set_ms(uint64_t time_ms)
Set current time in ms.
Definition powman.c:39
void powman_enable_gpio_wakeup(uint gpio_wakeup_num, uint32_t gpio, bool edge, bool high)
Wake up from a gpio.
Definition powman.c:293
powman_power_state powman_get_power_state(void)
Get the current power state.
Definition powman.c:179
void powman_timer_enable_alarm_at_ms(uint64_t alarm_time_ms)
Set an alarm at an absolute time in ms.
Definition powman.c:265
void powman_timer_set_1khz_tick_source_lposc_with_hz(uint32_t lposc_freq_hz)
Use the low power oscillator (specifying frequency) as the powman timer source.
Definition powman.c:89
void powman_disable_all_wakeups(void)
Disable all wakeup sources.
Definition powman.c:317
powman_power_domains
Power domains of powman.
Definition powman.h:197
static powman_power_state powman_power_state_with_domain_off(powman_power_state orig, enum powman_power_domains domain)
Helper function modify a powman_power_state to turn a domain off.
Definition powman.h:285
uint32_t powman_timer_get_lposc_calib_freq(void)
Get the calibrated frequency of the low power oscillator.
Definition powman.c:67
void powman_disable_gpio_wakeup(uint gpio_wakeup_num)
Disable wake up from a gpio.
Definition powman.c:312
static powman_power_state powman_power_state_with_domain_on(powman_power_state orig, enum powman_power_domains domain)
Helper function modify a powman_power_state to turn a domain on.
Definition powman.h:275
void powman_timer_disable_alarm(void)
Disable the alarm.
Definition powman.c:278
@ POWMAN_POWER_DOMAIN_SRAM_BANK1
bank1 includes the top 256K of sram plus sram 8 and 9 (scratch x and scratch y)
Definition powman.h:198
@ POWMAN_POWER_DOMAIN_SRAM_BANK0
bank0 is bottom 256K of sSRAM
Definition powman.h:199
@ POWMAN_POWER_DOMAIN_XIP_CACHE
XIP cache is 2x8K instances.
Definition powman.h:200
@ POWMAN_POWER_DOMAIN_SWITCHED_CORE
Switched core logic (processors, busfabric, peris etc).
Definition powman.h:201