0
0
Embedded Cprogramming~20 mins

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

Choose your learning style9 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.