Wake-up sources configuration lets a device know what events can turn it on from sleep mode. This saves power by only waking up when needed.
Wake-up sources configuration in Embedded C
Introduction
Syntax
Embedded C
void configure_wakeup_sources(void) {
// Enable wake-up source
WAKEUP_SOURCE_ENABLE = 1; // Example register or flag
// Configure specific source
WAKEUP_SOURCE_TYPE = WAKEUP_SOURCE_TIMER; // or WAKEUP_SOURCE_GPIO
}Registers or flags depend on your microcontroller model.
Always check datasheet for exact names and values.
Examples
Embedded C
void configure_wakeup_sources(void) {
// Enable GPIO as wake-up source
WAKEUP_GPIO_ENABLE = 1;
WAKEUP_GPIO_PIN = 5; // Pin number
}Embedded C
void configure_wakeup_sources(void) {
// Enable timer as wake-up source
WAKEUP_TIMER_ENABLE = 1;
WAKEUP_TIMER_VALUE = 1000; // Time in ms
}Sample Program
This program simulates setting GPIO pin 3 and a timer as wake-up sources. It prints the configuration to show what is set.
Embedded C
#include <stdio.h> // Simulated registers int WAKEUP_GPIO_ENABLE = 0; int WAKEUP_GPIO_PIN = -1; int WAKEUP_TIMER_ENABLE = 0; int WAKEUP_TIMER_VALUE = 0; void configure_wakeup_sources(void) { // Enable GPIO pin 3 as wake-up source WAKEUP_GPIO_ENABLE = 1; WAKEUP_GPIO_PIN = 3; // Enable timer wake-up after 500 ms WAKEUP_TIMER_ENABLE = 1; WAKEUP_TIMER_VALUE = 500; } int main() { configure_wakeup_sources(); printf("GPIO Wake-up Enabled: %d\n", WAKEUP_GPIO_ENABLE); printf("GPIO Pin: %d\n", WAKEUP_GPIO_PIN); printf("Timer Wake-up Enabled: %d\n", WAKEUP_TIMER_ENABLE); printf("Timer Value: %d ms\n", WAKEUP_TIMER_VALUE); return 0; }
Important Notes
Wake-up sources vary by hardware; always consult your device's manual.
Incorrect configuration can cause the device to never wake or wake unexpectedly.
Test wake-up behavior carefully in your application.
Summary
Wake-up sources tell a device what events can turn it on from sleep.
Common sources include GPIO pins and timers.
Configuration depends on your hardware and must be done carefully.
Practice
1. Which of the following is a common wake-up source in embedded C devices?
easy
Solution
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.Step 2: Identify valid wake-up sources
GPIO pin interrupts are commonly used as wake-up sources because they detect external signals.Final Answer:
GPIO pin interrupt -> Option AQuick 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
Solution
Step 1: Recognize common embedded C macros
Macros often use uppercase and underscores, like GPIO_WAKEUP_ENABLE(pin).Step 2: Match syntax style
GPIO_WAKEUP_ENABLE(5); matches typical embedded C macro style for enabling wake-up on GPIO pin 5.Final Answer:
GPIO_WAKEUP_ENABLE(5); -> Option BQuick 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
Solution
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.Step 2: Check function calls and conditions
Functions like is_wakeup_source and constants GPIO_3, TIMER are undefined, leading to compilation failure.Final Answer:
Compilation error -> Option DQuick 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
Solution
Step 1: Review GPIO wake-up configuration
The code enables GPIO pin 2 twice, which can cause conflicts or redundant configuration.Step 2: Check timer configuration and function structure
Timer value 500 ms is valid; no return needed for void function.Final Answer:
Duplicate GPIO wake-up enable causes conflict -> Option AQuick 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
Solution
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.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.Final Answer:
WAKEUP_ENABLE_GPIO(4); WAKEUP_ENABLE_TIMER(2000); -> Option CQuick 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
10 questions ~5 mineasy hard
