Bird
Raised Fist0
Embedded Cprogramming~20 mins

Wake-up sources configuration in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Wake-up Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Wake-up source bitmask configuration output
What is the output of the following code snippet configuring wake-up sources using bitmasks?
Embedded C
/* Define wake-up sources as bitmasks */
#define WAKEUP_TIMER   (1 << 0)
#define WAKEUP_GPIO    (1 << 1)
#define WAKEUP_RTC     (1 << 2)

unsigned int wakeup_sources = 0;

void enable_wakeup_sources() {
    wakeup_sources |= WAKEUP_TIMER;
    wakeup_sources |= WAKEUP_GPIO;
}

int main() {
    enable_wakeup_sources();
    printf("0x%X", wakeup_sources);
    return 0;
}
A0x4
B0x1
C0x2
D0x3
Attempts:
2 left
💡 Hint
Remember that bitwise OR combines bits set in both masks.
❓ Predict Output
intermediate
2:00remaining
Wake-up source configuration with conditional flags
What is the output of this code that conditionally enables wake-up sources?
Embedded C
#include <stdio.h>
#define WAKEUP_TIMER (1 << 0)
#define WAKEUP_GPIO  (1 << 1)
#define WAKEUP_RTC   (1 << 2)

unsigned int configure_wakeup(int enable_timer, int enable_gpio, int enable_rtc) {
    unsigned int sources = 0;
    if (enable_timer) sources |= WAKEUP_TIMER;
    if (enable_gpio) sources |= WAKEUP_GPIO;
    if (enable_rtc) sources |= WAKEUP_RTC;
    return sources;
}

int main() {
    unsigned int result = configure_wakeup(1, 0, 1);
    printf("0x%X", result);
    return 0;
}
A0x5
B0x3
C0x6
D0x7
Attempts:
2 left
💡 Hint
Check which bits correspond to enabled sources.
🔧 Debug
advanced
2:00remaining
Identify the error in wake-up source configuration
What error does this code produce when compiling?
Embedded C
#define WAKEUP_TIMER (1 << 0)
#define WAKEUP_GPIO (1 << 1)

unsigned int wakeup_sources = 0;

void enable_sources() {
    wakeup_sources = wakeup_sources | WAKEUP_TIMER & WAKEUP_GPIO;
}

int main() {
    enable_sources();
    return 0;
}
ANo error, compiles successfully
BSyntax error: missing semicolon
CLogical error: no bits are set (remains unchanged)
DType error: incompatible types in bitwise operation
Attempts:
2 left
💡 Hint
Check operator precedence between | and &.
📝 Syntax
advanced
2:00remaining
Find the syntax error in wake-up source configuration
Which option contains the correct syntax to enable both WAKEUP_TIMER and WAKEUP_GPIO bits?
Awakeup_sources |= (WAKEUP_TIMER & WAKEUP_GPIO);
Bwakeup_sources |= WAKEUP_TIMER | WAKEUP_GPIO;
Cwakeup_sources =+ WAKEUP_TIMER | WAKEUP_GPIO;
Dwakeup_sources = wakeup_sources | WAKEUP_TIMER & WAKEUP_GPIO;
Attempts:
2 left
💡 Hint
Check operator precedence and assignment operators.
🚀 Application
expert
3:00remaining
Calculate number of enabled wake-up sources
Given the wakeup_sources variable holds enabled wake-up bits, what is the number of enabled sources after this code runs?
Embedded C
#define WAKEUP_TIMER (1 << 0)
#define WAKEUP_GPIO  (1 << 1)
#define WAKEUP_RTC   (1 << 2)
#define WAKEUP_USB   (1 << 3)

unsigned int wakeup_sources = 0;

void enable_sources() {
    wakeup_sources |= WAKEUP_TIMER;
    wakeup_sources |= WAKEUP_GPIO;
    wakeup_sources |= WAKEUP_RTC;
    wakeup_sources &= ~WAKEUP_GPIO;
}

int count_enabled_sources(unsigned int sources) {
    int count = 0;
    while (sources) {
        count += sources & 1;
        sources >>= 1;
    }
    return count;
}

int main() {
    enable_sources();
    int enabled = count_enabled_sources(wakeup_sources);
    printf("%d", enabled);
    return 0;
}
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
Check which bits remain set after clearing WAKEUP_GPIO.

Practice

(1/5)
1. Which of the following is a common wake-up source in embedded C devices?
easy
A. GPIO pin interrupt
B. Floating point calculation
C. String concatenation
D. File system read

Solution

  1. Step 1: Understand wake-up sources

    Wake-up sources are hardware events that can wake a device from sleep mode, such as GPIO interrupts or timers.
  2. Step 2: Identify valid wake-up sources

    GPIO pin interrupts are commonly used as wake-up sources because they detect external signals.
  3. Final Answer:

    GPIO pin interrupt -> Option A
  4. Quick Check:

    Wake-up source = GPIO pin interrupt [OK]
Hint: Wake-up sources are hardware events like GPIO or timers [OK]
Common Mistakes:
  • Confusing software operations with hardware wake-up sources
  • Thinking file operations can wake embedded devices
  • Assuming calculations can trigger wake-up
2. Which syntax correctly enables a GPIO pin as a wake-up source in embedded C?
easy
A. enable_wakeup(GPIO_PIN_5);
B. GPIO_WAKEUP_ENABLE(5);
C. wakeup_enable(GPIO5);
D. set_wakeup_pin(5);

Solution

  1. Step 1: Recognize common embedded C macros

    Macros often use uppercase and underscores, like GPIO_WAKEUP_ENABLE(pin).
  2. Step 2: Match syntax style

    GPIO_WAKEUP_ENABLE(5); matches typical embedded C macro style for enabling wake-up on GPIO pin 5.
  3. Final Answer:

    GPIO_WAKEUP_ENABLE(5); -> Option B
  4. Quick Check:

    Macro style = GPIO_WAKEUP_ENABLE(5) [OK]
Hint: Look for uppercase macro style for hardware config [OK]
Common Mistakes:
  • Using lowercase function names not defined in embedded C
  • Confusing parameter formats
  • Missing parentheses or semicolons
3. What will be the output of the following code snippet?
void configure_wakeup() {
    WAKEUP_ENABLE_TIMER(1000); // 1000 ms
    WAKEUP_ENABLE_GPIO(3);
}

int main() {
    configure_wakeup();
    if (is_wakeup_source(GPIO_3)) {
        printf("Wakeup by GPIO 3\n");
    } else if (is_wakeup_source(TIMER)) {
        printf("Wakeup by Timer\n");
    } else {
        printf("No wakeup source\n");
    }
    return 0;
}
medium
A. Wakeup by GPIO 3
B. Wakeup by Timer
C. No wakeup source
D. Compilation error

Solution

  1. Step 1: Analyze function and macros usage

    The code uses WAKEUP_ENABLE_TIMER and WAKEUP_ENABLE_GPIO macros but does not define or include them, causing compilation errors.
  2. Step 2: Check function calls and conditions

    Functions like is_wakeup_source and constants GPIO_3, TIMER are undefined, leading to compilation failure.
  3. Final Answer:

    Compilation error -> Option D
  4. Quick Check:

    Undefined macros/functions cause compilation error [OK]
Hint: Undefined macros or functions cause compile errors [OK]
Common Mistakes:
  • Assuming macros are predefined without headers
  • Ignoring missing function definitions
  • Confusing runtime output with compile errors
4. Identify the error in this wake-up source configuration code:
void setup_wakeup() {
    WAKEUP_ENABLE_GPIO(2);
    WAKEUP_ENABLE_GPIO(2);
    WAKEUP_ENABLE_TIMER(500);
}
medium
A. Duplicate GPIO wake-up enable causes conflict
B. Timer value 500 is invalid
C. Missing return statement
D. No error, code is correct

Solution

  1. Step 1: Review GPIO wake-up configuration

    The code enables GPIO pin 2 twice, which can cause conflicts or redundant configuration.
  2. Step 2: Check timer configuration and function structure

    Timer value 500 ms is valid; no return needed for void function.
  3. Final Answer:

    Duplicate GPIO wake-up enable causes conflict -> Option A
  4. Quick Check:

    Duplicate GPIO enable = conflict [OK]
Hint: Avoid enabling same GPIO wake-up twice [OK]
Common Mistakes:
  • Thinking timer value is invalid
  • Expecting return in void function
  • Ignoring duplicate hardware config issues
5. You want to configure a device to wake up either by GPIO pin 4 or after 2 seconds using a timer. Which code snippet correctly sets both wake-up sources?
hard
A. WAKEUP_CONFIG(GPIO4 | TIMER_2S);
B. WAKEUP_ENABLE_TIMER(2); WAKEUP_ENABLE_GPIO(4);
C. WAKEUP_ENABLE_GPIO(4); WAKEUP_ENABLE_TIMER(2000);
D. WAKEUP_ENABLE_GPIO(4, 2000);

Solution

  1. Step 1: Understand separate wake-up source configuration

    GPIO and timer wake-up sources are usually enabled with separate calls specifying pin and time in milliseconds.
  2. Step 2: Evaluate options for correctness

    WAKEUP_ENABLE_GPIO(4); WAKEUP_ENABLE_TIMER(2000); correctly enables GPIO pin 4 and timer with 2000 ms (2 seconds). WAKEUP_ENABLE_TIMER(2); WAKEUP_ENABLE_GPIO(4); uses 2 (seconds) instead of milliseconds. WAKEUP_ENABLE_GPIO(4, 2000); incorrectly combines parameters. WAKEUP_CONFIG(GPIO4 | TIMER_2S); uses an undefined macro.
  3. Final Answer:

    WAKEUP_ENABLE_GPIO(4); WAKEUP_ENABLE_TIMER(2000); -> Option C
  4. Quick Check:

    Separate calls with correct units = WAKEUP_ENABLE_GPIO(4); WAKEUP_ENABLE_TIMER(2000); [OK]
Hint: Enable GPIO and timer separately with correct units [OK]
Common Mistakes:
  • Using seconds instead of milliseconds for timer
  • Combining GPIO and timer in one call
  • Using undefined macros
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