---
title: "Idle states"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/kernel/idle/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Idle states

Source: https://developer.repebble.com/pebbleos/docs/samples/kernel/idle/

This sample sleeps and reads how long the CPU spent in its shallowest [idle state](https://developer.repebble.com/pebbleos/docs/kernel/idle.md) meanwhile. It then blocks that state, as a driver does while its peripheral cannot take it, and sleeps again: the CPU keeps running.

Idle states need `CONFIG_CPU_IDLE`, which the sample enables in its `prj.conf`.

Run:

`pbl samples samples/kernel/idle`

Boards:

every board

## Sources

src/main.c

```
#include \<stdint.h\>#include \<pbl/kernel/delay.h\>#include \<pbl/kernel/idle.h\>#include \<pbl/logging/logging.h\>#define SLEEP\_MS 200/\* The shallowest idle state, which every SoC has. SoCs name theirs in pbl/soc. \*/#define STATE 0static uint64\_t prv\_idle\_us\_while\_sleeping(void) {uint64\_t before\_us = pbl\_idle\_residency\_us(STATE);pbl\_sleep\_ms(SLEEP\_MS);return pbl\_idle\_residency\_us(STATE) - before\_us;}int main(void) {uint64\_t idle\_us;/\* With nothing to run, the CPU idles until the sleep is over. \*/idle\_us = prv\_idle\_us\_while\_sleeping();PBL\_LOG\_INFO("main: idled for most of a %d ms sleep: %s", SLEEP\_MS,idle\_us \> SLEEP\_MS \* 1000 / 2 ? "yes" : "no");/\* A driver blocks a state its peripheral does not work in, while it is busy. Blocking the \* shallowest one keeps the CPU running. \*/pbl\_idle\_state\_block(STATE);idle\_us = prv\_idle\_us\_while\_sleeping();pbl\_idle\_state\_release(STATE);PBL\_LOG\_INFO("main: idled for any of a %d ms sleep with the state blocked: %s", SLEEP\_MS,idle\_us \> 0 ? "yes" : "no");PBL\_LOG\_INFO("Sample done");return 0;}
```

## Output

```
main: idled for most of a 200 ms sleep: yes
main: idled for any of a 200 ms sleep with the state blocked: no
Sample done
```

Generated from PebbleOS [7753e801d](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/kernel/idle.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/kernel/idle.md).
