Bird
Raised Fist0
Embedded Cprogramming~20 mins

Idle mode behavior in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Idle Mode Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this idle mode simulation code?

Consider this embedded C code snippet simulating entering idle mode and waking up on an interrupt. What will be printed?

Embedded C
#include <stdio.h>
#include <stdbool.h>

volatile bool idle = false;

void enter_idle_mode() {
    idle = true;
    printf("Entering idle mode...\n");
}

void wake_up() {
    if (idle) {
        idle = false;
        printf("Woke up from idle mode!\n");
    }
}

int main() {
    enter_idle_mode();
    wake_up();
    wake_up();
    return 0;
}
A
Entering idle mode...
Woke up from idle mode!
B
Entering idle mode...
Woke up from idle mode!
Woke up from idle mode!
C
Woke up from idle mode!
Entering idle mode...
D
Entering idle mode...
Attempts:
2 left
💡 Hint

Think about the state of the idle variable after waking up once.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes idle mode behavior in embedded systems?

Choose the statement that correctly explains what happens when an embedded system enters idle mode.

AThe CPU stops executing instructions and all peripherals are powered off.
BThe CPU halts execution but peripherals and interrupts remain active to wake the CPU.
CThe CPU continues running at full speed but ignores interrupts until idle mode ends.
DThe system resets all registers and restarts the program from the beginning.
Attempts:
2 left
💡 Hint

Think about how the CPU can wake up from idle mode.

🔧 Debug
advanced
2:30remaining
Why does this idle mode code fail to wake up on interrupt?

Examine the code below. It is supposed to enter idle mode and wake up on an interrupt, but it never wakes up. What is the cause?

Embedded C
volatile bool idle = false;

void enter_idle_mode() {
    idle = true;
    // CPU enters idle mode here
}

void interrupt_handler() {
    idle = false;
}

int main() {
    enter_idle_mode();
    while (idle) {
        // wait for interrupt
    }
    printf("Woke up!\n");
    return 0;
}
AThe idle flag is not declared volatile, so the compiler optimizes the loop incorrectly.
BThe interrupt handler does not clear the idle flag, so the loop never ends.
CThe CPU never actually enters idle mode because there is no instruction to halt it.
DThe main function does not enable interrupts before entering idle mode.
Attempts:
2 left
💡 Hint

Think about what is missing to actually stop the CPU.

📝 Syntax
advanced
1:30remaining
What error does this idle mode code produce?

Identify the error in this embedded C code snippet related to idle mode.

Embedded C
void enter_idle_mode() {
    idle = true;
    __WFI();
}
ASyntaxError: missing semicolon after 'idle = true'
BTypeError: __WFI is not a function
CRuntimeError: idle variable not declared
DNo error, code runs correctly
Attempts:
2 left
💡 Hint

Check the line endings carefully.

🚀 Application
expert
3:00remaining
How many times will the CPU wake up in this idle mode loop?

Given the code below, how many times will the CPU print "Woke up!" before the program ends?

Embedded C
#include <stdio.h>
#include <stdbool.h>

volatile bool idle = false;
int wake_count = 0;

void enter_idle_mode() {
    idle = true;
    // Simulate CPU halt
}

void interrupt_handler() {
    if (wake_count < 3) {
        idle = false;
        wake_count++;
    }
}

int main() {
    for (int i = 0; i < 5; i++) {
        enter_idle_mode();
        interrupt_handler();
        if (!idle) {
            printf("Woke up!\n");
        }
    }
    return 0;
}
A0
B5
C2
D3
Attempts:
2 left
💡 Hint

Look at how wake_count limits waking up.

Practice

(1/5)
1. What is the main purpose of using idle mode in embedded C programming?
easy
A. To reset the CPU to its initial state
B. To increase the CPU clock speed for faster processing
C. To save power by pausing the CPU when it is not busy
D. To disable all interrupts permanently

Solution

  1. Step 1: Understand idle mode function

    Idle mode pauses the CPU to save power when it is not performing tasks.
  2. Step 2: Compare options with idle mode purpose

    Only To save power by pausing the CPU when it is not busy correctly states that idle mode saves power by pausing the CPU.
  3. Final Answer:

    To save power by pausing the CPU when it is not busy -> Option C
  4. Quick Check:

    Idle mode = power saving by pausing CPU [OK]
Hint: Idle mode means CPU pauses to save power [OK]
Common Mistakes:
  • Thinking idle mode speeds up CPU
  • Confusing idle mode with reset
  • Believing interrupts are disabled permanently
2. Which instruction is used in embedded C to enter idle mode?
easy
A. WFI
B. NOP
C. SVC
D. MOV

Solution

  1. Step 1: Recall instruction for idle mode

    The WFI (Wait For Interrupt) instruction is used to enter idle mode.
  2. Step 2: Verify other instructions

    NOP does nothing, SVC triggers supervisor call, MOV moves data; none enter idle mode.
  3. Final Answer:

    WFI -> Option A
  4. Quick Check:

    Idle mode instruction = WFI [OK]
Hint: WFI means wait for interrupt, used for idle mode [OK]
Common Mistakes:
  • Choosing NOP thinking it pauses CPU
  • Confusing SVC with idle mode instruction
  • Using MOV as idle mode command
3. What will happen when the CPU executes the following code snippet?
__asm__("WFI");
// CPU is idle here until interrupt occurs
medium
A. CPU continues running instructions without pause
B. CPU resets immediately
C. CPU disables all interrupts
D. CPU enters idle mode and waits for an interrupt

Solution

  1. Step 1: Understand WFI instruction effect

    WFI causes CPU to enter idle mode and wait for an interrupt to wake it.
  2. Step 2: Analyze other options

    CPU does not continue running instructions without pause, reset immediately, or disable all interrupts on WFI.
  3. Final Answer:

    CPU enters idle mode and waits for an interrupt -> Option D
  4. Quick Check:

    WFI = idle mode wait for interrupt [OK]
Hint: WFI pauses CPU until interrupt wakes it [OK]
Common Mistakes:
  • Assuming CPU runs instructions after WFI immediately
  • Thinking WFI resets CPU
  • Believing WFI disables interrupts
4. Identify the error in this code snippet that tries to enter idle mode:
void enter_idle() {
__asm__("WFI");
__disable_irq();
}
medium
A. No error, code is correct
B. Interrupts are disabled after WFI, so CPU may never wake
C. Function should return a value
D. WFI instruction is incorrect syntax

Solution

  1. Step 1: Understand WFI and interrupt disabling order

    WFI waits for interrupt to wake CPU, but interrupts are disabled after WFI.
  2. Step 2: Analyze impact of disabling interrupts after WFI

    If interrupts are disabled after WFI, CPU may not wake properly because interrupts are blocked.
  3. Final Answer:

    Interrupts are disabled after WFI, so CPU may never wake -> Option B
  4. Quick Check:

    Disabling interrupts after WFI blocks wake-up [OK]
Hint: Disable interrupts before WFI, not after [OK]
Common Mistakes:
  • Disabling interrupts after WFI instead of before
  • Thinking WFI syntax is wrong
  • Expecting function to return value unnecessarily
5. You want to save power by putting the CPU into idle mode but only when no tasks are pending. Which approach correctly implements this behavior in embedded C?
if (no_tasks_pending()) {
__disable_irq();
if (no_tasks_pending()) {
__asm__("WFI");
}
__enable_irq();
}
hard
A. This code safely enters idle mode only if no tasks are pending
B. This code disables interrupts but never enables them again
C. WFI should be called before disabling interrupts
D. no_tasks_pending() should be called only once before WFI

Solution

  1. Step 1: Analyze double check of no_tasks_pending()

    Disabling interrupts then checking again ensures no tasks arrived during interrupt disable.
  2. Step 2: Confirm interrupts are re-enabled after WFI

    Interrupts are enabled after WFI, so CPU can wake properly.
  3. Final Answer:

    This code safely enters idle mode only if no tasks are pending -> Option A
  4. Quick Check:

    Double check + disable/enable IRQ = safe idle entry [OK]
Hint: Disable IRQ, double-check, then WFI, finally enable IRQ [OK]
Common Mistakes:
  • Not re-enabling interrupts after WFI
  • Calling WFI before disabling interrupts
  • Checking no_tasks_pending only once
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard