0
0
Embedded Cprogramming~20 mins

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

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