PebbleOS v4.47.0-512-g5285d4236
Loading...
Searching...
No Matches
Idle states

Low-power states the CPU idles in, provided by the SoC. More...

Data Structures

struct  pbl_idle_state
 Idle state. More...
 
struct  pbl_idle_hook_times
 Times of an idle hook, see pbl_idle_hook_set_times(). More...
 
struct  pbl_idle_hook
 Idle hook, see PBL_IDLE_HOOK_DEFINE. More...
 
struct  pbl_idle_latency_request
 Request on the time the CPU takes to wake up, see pbl_idle_latency_request_add(). More...
 

Macros

#define PBL_IDLE_STATES_MAX   4
 Maximum number of idle states.
 
#define PBL_IDLE_HOOK_DEFINE(_name, _enter, _exit, _arg)
 Define a hook, for a driver to act before the CPU idles and after.
 
#define PBL_IDLE_HOOK_LATE_DEFINE(_name, _enter, _exit, _arg)
 Define a hook the SoC calls last before the CPU idles and first after.
 
#define PBL_IDLE_HOOK(_name)   (&pbl_idle_hook_##_name)
 Get a hook defined with PBL_IDLE_HOOK_DEFINE in the same file.
 

Typedefs

typedef uint8_t pbl_idle_state_id_t
 Index of an idle state in pbl_soc_idle_states.
 

Functions

static void pbl_cpu_idle (void)
 Wait for an interrupt.
 
static void pbl_cpu_idle_deep (void)
 Wait for an interrupt in the CPU's deep sleep.
 
void pbl_idle_hook_set_times (const struct pbl_idle_hook *hook, uint32_t min_us, uint32_t exit_us)
 Set the times of a hook, rounded up to ticks.
 
void pbl_idle_hooks_late_enter (pbl_idle_state_id_t state)
 Call the enter function of every late hook.
 
void pbl_idle_hooks_late_exit (pbl_idle_state_id_t state)
 Call the exit function of every late hook.
 
static void pbl_idle_state_block (pbl_idle_state_id_t state)
 Keep the CPU out of an idle state and of every deeper one.
 
static void pbl_idle_state_release (pbl_idle_state_id_t state)
 Release a block taken with pbl_idle_state_block().
 
static void pbl_idle_latency_request_add (struct pbl_idle_latency_request *req, uint32_t us)
 Keep the CPU out of the idle states, and off the hooks, that take longer to wake up from.
 
static void pbl_idle_latency_request_remove (struct pbl_idle_latency_request *req)
 Remove a request added with pbl_idle_latency_request_add().
 
void pbl_idle_set_enabled (bool enable)
 Allow the CPU to idle or keep it running, e.g.
 
uint64_t pbl_idle_residency_us (pbl_idle_state_id_t state)
 Get the time spent in an idle state since boot.
 

Variables

const struct pbl_idle_state pbl_soc_idle_states []
 Idle states, shallowest first.
 
const size_t pbl_soc_num_idle_states
 Number of entries in pbl_soc_idle_states.
 

Detailed Description

Low-power states the CPU idles in, provided by the SoC.

When nothing is runnable, the kernel picks the deepest state that no driver blocks and is worth entering for the time until the next timeout, programs the system clock event device to wake the CPU up early enough, enters the state, and accounts for the time spent in it from the system clock source. The SoC only puts the CPU to sleep.

Idle states need CONFIG_CPU_IDLE. Without it the kernel only calls pbl_cpu_idle(), a tick at a time, pbl_idle_state_block() and pbl_idle_state_release() do nothing, and nothing else here is available.

{.name = "wfi", .min_ticks = 1, .exit_ticks = 0, .enter = pbl_cpu_idle},
};
const struct pbl_idle_state pbl_soc_idle_states[]
Idle states, shallowest first.
const size_t pbl_soc_num_idle_states
Number of entries in pbl_soc_idle_states.
static void pbl_cpu_idle(void)
Wait for an interrupt.
Definition idle.h:65
#define PBL_ARRAY_SIZE(array)
Get the number of elements of an array.
Definition size.h:40
Idle state.
Definition idle.h:45
const char * name
Name, for statistics.
Definition idle.h:47

Data Structure Documentation

◆ pbl_idle_hook_times

struct pbl_idle_hook_times

Times of an idle hook, see pbl_idle_hook_set_times().

Data Fields
pbl_tick_t exit_ticks Time the exit function takes, in ticks; the CPU is woken up this much earlier.
pbl_tick_t min_ticks Shortest time until the next timeout worth calling the hook for, in ticks; 0 until set, and the hook is not called meanwhile.

◆ pbl_idle_latency_request

struct pbl_idle_latency_request

Request on the time the CPU takes to wake up, see pbl_idle_latency_request_add().

Macro Definition Documentation

◆ PBL_IDLE_HOOK

#define PBL_IDLE_HOOK (   _name)    (&pbl_idle_hook_##_name)

Get a hook defined with PBL_IDLE_HOOK_DEFINE in the same file.

Parameters
_nameName of the hook.

◆ PBL_IDLE_HOOK_DEFINE

#define PBL_IDLE_HOOK_DEFINE (   _name,
  _enter,
  _exit,
  _arg 
)
Value:
static struct pbl_idle_hook_times pbl_idle_hook_times_##_name; \
static const struct pbl_idle_hook pbl_idle_hook_##_name PBL_ITERABLE_SECTION( \
const struct pbl_idle_hook, idle_hooks, #_name) = { \
.enter = (_enter), \
.exit = (_exit), \
.arg = (_arg), \
.times = &pbl_idle_hook_times_##_name, \
}
Times of an idle hook, see pbl_idle_hook_set_times().
Definition idle.h:90
#define PBL_ITERABLE_SECTION(type, tag, key)
Add the object to an iterable section, a table gathered at link time from any file.
Definition section.h:124
Idle hook, see PBL_IDLE_HOOK_DEFINE.
Definition idle.h:101
void(* enter)(pbl_idle_state_id_t state, void *arg)
Called before the CPU enters a state, may be NULL.
Definition idle.h:103
struct pbl_idle_hook_times * times
Times, NULL for a late hook.
Definition idle.h:109
void(* exit)(pbl_idle_state_id_t state, void *arg)
Called after the CPU left a state, may be NULL.
Definition idle.h:105

Define a hook, for a driver to act before the CPU idles and after.

The kernel calls the hooks around the idle states once their times are set with pbl_idle_hook_set_times(), when the time until the next timeout is at least the hook's minimum, and wakes the CPU up earlier by the time its exit function takes. It tells the state in state, an index in pbl_soc_idle_states, for a hook that only acts around some. Hooks run with every interrupt masked: they must be short and cannot block. On the way out they run in the opposite order.

static void prv_idle_enter(pbl_idle_state_id_t state, void *arg) {
prv_power_down(arg);
}
PBL_IDLE_HOOK_DEFINE(foo, prv_idle_enter, prv_idle_exit, &s_foo);
static int prv_init(void) {
// Powering back up takes 2 ms, which pays off from 5 ms on
return 0;
}
void pbl_idle_hook_set_times(const struct pbl_idle_hook *hook, uint32_t min_us, uint32_t exit_us)
Set the times of a hook, rounded up to ticks.
#define PBL_IDLE_HOOK_DEFINE(_name, _enter, _exit, _arg)
Define a hook, for a driver to act before the CPU idles and after.
Definition idle.h:154
#define PBL_IDLE_HOOK(_name)
Get a hook defined with PBL_IDLE_HOOK_DEFINE in the same file.
Definition idle.h:173
uint8_t pbl_idle_state_id_t
Index of an idle state in pbl_soc_idle_states.
Definition idle.h:42

Hooks are not called without CONFIG_CPU_IDLE.

Parameters
_nameIdentifier unique among hooks.
_enterFunction called before entering a state, or NULL.
_exitFunction called after leaving a state, or NULL.
_argArgument of the functions.

◆ PBL_IDLE_HOOK_LATE_DEFINE

#define PBL_IDLE_HOOK_LATE_DEFINE (   _name,
  _enter,
  _exit,
  _arg 
)
Value:
static const struct pbl_idle_hook pbl_idle_hook_late_##_name PBL_ITERABLE_SECTION( \
const struct pbl_idle_hook, idle_late, \
#_name) = {.enter = (_enter), .exit = (_exit), .arg = (_arg)}

Define a hook the SoC calls last before the CPU idles and first after.

For what must happen once the SoC took down what code needs to run from flash, where a state does. The SoC calls late hooks from the innermost part of such states. The functions, and all they call, must be PBL_SECTION_RAM or inline. Otherwise as PBL_IDLE_HOOK_DEFINE, without times: they belong to the state.

◆ PBL_IDLE_STATES_MAX

#define PBL_IDLE_STATES_MAX   4

Maximum number of idle states.

Typedef Documentation

◆ pbl_idle_state_id_t

typedef uint8_t pbl_idle_state_id_t

Index of an idle state in pbl_soc_idle_states.

Function Documentation

◆ pbl_cpu_idle()

static void pbl_cpu_idle ( void  )
static

Wait for an interrupt.

The shallowest idle state; deeper ones wrap it. Returns once an interrupt is pending, even a masked one. Always inlined, so it can run while the flash is powered down.

◆ pbl_cpu_idle_deep()

static void pbl_cpu_idle_deep ( void  )
static

Wait for an interrupt in the CPU's deep sleep.

What deep sleep stops is up to the SoC. Always inlined, like pbl_cpu_idle().

◆ pbl_idle_hook_set_times()

void pbl_idle_hook_set_times ( const struct pbl_idle_hook *  hook,
uint32_t  min_us,
uint32_t  exit_us 
)

Set the times of a hook, rounded up to ticks.

A driver sets them once it knows them, e.g. from the part it found; the hook is not called before.

Parameters
hookHook, see PBL_IDLE_HOOK.
min_usShortest time until the next timeout worth calling the hook for, in microseconds.
exit_usTime the exit function takes, in microseconds.

◆ pbl_idle_hooks_late_enter()

void pbl_idle_hooks_late_enter ( pbl_idle_state_id_t  state)

Call the enter function of every late hook.

Runs from RAM.

Parameters
stateIndex in pbl_soc_idle_states.

◆ pbl_idle_hooks_late_exit()

void pbl_idle_hooks_late_exit ( pbl_idle_state_id_t  state)

Call the exit function of every late hook.

Runs from RAM.

Parameters
stateIndex in pbl_soc_idle_states.

◆ pbl_idle_latency_request_add()

static void pbl_idle_latency_request_add ( struct pbl_idle_latency_request *  req,
uint32_t  us 
)
inlinestatic

Keep the CPU out of the idle states, and off the hooks, that take longer to wake up from.

For code that must run within some time of an interrupt, whatever the state the CPU idles in, as opposed to a peripheral that does not work in a state, see pbl_idle_state_block(). The shortest request applies. Can be called from ISRs. Does nothing without CONFIG_CPU_IDLE.

static struct pbl_idle_latency_request s_request;
pbl_idle_latency_request_add(&s_request, 500); // the radio is served within 500 us
prv_radio_event();
Request on the time the CPU takes to wake up, see pbl_idle_latency_request_add().
Definition idle.h:240
static void pbl_idle_latency_request_remove(struct pbl_idle_latency_request *req)
Remove a request added with pbl_idle_latency_request_add().
Definition idle.h:282
static void pbl_idle_latency_request_add(struct pbl_idle_latency_request *req, uint32_t us)
Keep the CPU out of the idle states, and off the hooks, that take longer to wake up from.
Definition idle.h:268
Parameters
reqRequest, not added yet. Must stay valid until removed.
usLongest time to wake up tolerated, in microseconds.

◆ pbl_idle_latency_request_remove()

static void pbl_idle_latency_request_remove ( struct pbl_idle_latency_request *  req)
inlinestatic

Remove a request added with pbl_idle_latency_request_add().

Parameters
reqRequest.

◆ pbl_idle_residency_us()

uint64_t pbl_idle_residency_us ( pbl_idle_state_id_t  state)

Get the time spent in an idle state since boot.

Parameters
stateIndex in pbl_soc_idle_states.
Returns
Microseconds.

◆ pbl_idle_set_enabled()

void pbl_idle_set_enabled ( bool  enable)

Allow the CPU to idle or keep it running, e.g.

while measuring.

Idle is allowed at boot.

Parameters
enableFalse to keep the CPU out of every idle state.

◆ pbl_idle_state_block()

static void pbl_idle_state_block ( pbl_idle_state_id_t  state)
inlinestatic

Keep the CPU out of an idle state and of every deeper one.

For a peripheral that does not work in the state, while it is busy. SoCs name their states, so that the call reads like the datasheet:

pbl_idle_state_block(SOC_FOO_IDLE_STOP); // the UART does not receive in stop mode
prv_receive();
pbl_idle_state_release(SOC_FOO_IDLE_STOP);
static void pbl_idle_state_block(pbl_idle_state_id_t state)
Keep the CPU out of an idle state and of every deeper one.
Definition idle.h:221
static void pbl_idle_state_release(pbl_idle_state_id_t state)
Release a block taken with pbl_idle_state_block().
Definition idle.h:234

Counted: balance each call with pbl_idle_state_release(). Can be called from ISRs. Does nothing without CONFIG_CPU_IDLE.

Parameters
stateIndex in pbl_soc_idle_states; 0 keeps the CPU running.

◆ pbl_idle_state_release()

static void pbl_idle_state_release ( pbl_idle_state_id_t  state)
inlinestatic

Release a block taken with pbl_idle_state_block().

Parameters
stateState passed to pbl_idle_state_block().

Variable Documentation

◆ pbl_soc_idle_states

const struct pbl_idle_state pbl_soc_idle_states[]
extern

Idle states, shallowest first.

Defined by SoCs that select CONFIG_SOC_HAS_IDLE_STATES; for the others the kernel defines a single state that calls pbl_cpu_idle().

◆ pbl_soc_num_idle_states

const size_t pbl_soc_num_idle_states
extern

Number of entries in pbl_soc_idle_states.