0
0
Embedded Cprogramming~5 mins

Wake-up sources configuration in Embedded C

Choose your learning style9 modes available
Introduction

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.

When you want a device to wake up on a button press.
When a sensor detects movement and should wake the system.
When a timer expires and the device needs to perform a task.
When an external signal or interrupt should wake the device.
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
This example sets a GPIO pin as a wake-up source.
Embedded C
void configure_wakeup_sources(void) {
    // Enable GPIO as wake-up source
    WAKEUP_GPIO_ENABLE = 1;
    WAKEUP_GPIO_PIN = 5; // Pin number
}
This example configures a timer to wake the device after 1000 ms.
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;
}
OutputSuccess
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.