0
0
Embedded Cprogramming~5 mins

Idle mode behavior in Embedded C

Choose your learning style9 modes available
Introduction

Idle mode helps save power by stopping the CPU when it has nothing to do.

When your device waits for a button press without doing other tasks.
When a sensor is inactive and the system can rest.
When you want to reduce battery use during pauses in activity.
When the system waits for a timer or interrupt to wake it up.
Syntax
Embedded C
void enter_idle_mode(void) {
    // Set CPU to idle mode
    // Wait for interrupt to wake up
    __asm__("WFI");
}

WFI stands for 'Wait For Interrupt'. It stops the CPU until an interrupt happens.

This is often used in microcontrollers to save power.

Examples
Simple idle mode using the WFI instruction to pause CPU until an interrupt.
Embedded C
void enter_idle_mode(void) {
    __asm__("WFI");
}
Idle mode with extra steps to save more power by turning off unused parts.
Embedded C
void enter_idle_mode(void) {
    // Prepare peripherals before idle
    disable_unused_peripherals();
    __asm__("WFI");
}
Sample Program

This program shows entering idle mode and waking up after an interrupt (simulated here).

Embedded C
#include <stdio.h>

void enter_idle_mode(void) {
    // Simulate idle mode with WFI instruction
    printf("Entering idle mode...\n");
    __asm__("WFI");
    printf("Woke up from idle mode.\n");
}

int main(void) {
    printf("System running.\n");
    enter_idle_mode();
    printf("System resumed work.\n");
    return 0;
}
OutputSuccess
Important Notes

Idle mode stops the CPU but keeps peripherals running.

Interrupts like timers or buttons can wake the CPU from idle.

Using idle mode helps extend battery life in embedded devices.

Summary

Idle mode pauses the CPU to save power when idle.

Use the WFI instruction to enter idle mode.

Interrupts wake the CPU to resume work.