pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
    • Playground
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Threads
      • Mutexes
      • Semaphores
      • Message queues
      • Poll groups
      • Timers
      • Work queues
      • Delayed work
      • Heaps
      • Time
      • Idle states
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • CRC
      • SHA-256
      • AES
      • Files
      • Settings
      • Translations
      • Input events
      • Loader
      • Log levels
      • Profiler
      • Shell commands
      • Task watchdog
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • GPU
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Rust
      • Kernel benchmark
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Subsystems
    • Drivers
    • Services
    • Libraries
    • Third-party
    • Application SDK
  • Hardware
    • Boards
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

Idle states

This sample sleeps and reads how long the CPU spent in its shallowest idle state 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 0

static 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. This page is maintained in the pebbleos repository: docs/samples/kernel/idle.md.

Overview

  • Sources
  • Output