📖 Scenario: You are working on a simple embedded system using FreeRTOS. You want to create a timer that triggers a callback function every second to toggle an LED. Understanding the execution context of the timer callback is important to write safe and correct code.
🎯 Goal: Build a FreeRTOS program that creates a software timer with a callback function. The callback toggles a variable representing an LED state. You will learn how to set up the timer, configure its callback, and understand the context in which the callback runs.
📋 What You'll Learn
Create a FreeRTOS software timer named xTimer with a 1000 ms period
Define a callback function named vTimerCallback that toggles an ledState variable
Start the timer using xTimerStart
Print the ledState value inside the callback to observe changes
💡 Why This Matters
🌍 Real World
Timers are used in embedded systems to perform actions periodically, such as blinking LEDs, reading sensors, or sending data.
💼 Career
Understanding timer callbacks and their execution context is essential for embedded software developers working with real-time operating systems like FreeRTOS.
Progress0 / 4 steps
1
Create the LED state variable and timer handle
Create a global variable called ledState initialized to 0. Also declare a variable called xTimer of type TimerHandle_t.
FreeRTOS
Hint
Use int ledState = 0; to represent the LED off state. Declare xTimer as TimerHandle_t to hold the timer reference.
2
Create the timer with a callback function
Define a function called vTimerCallback that takes a TimerHandle_t xTimer parameter. Inside it, toggle ledState between 0 and 1. Then create the timer xTimer using xTimerCreate with name "LEDTimer", period 1000 ms, auto-reload enabled, id 0, and callback vTimerCallback.
FreeRTOS
Hint
The callback toggles ledState using ledState = !ledState;. Use pdMS_TO_TICKS(1000) to convert 1000 ms to ticks. Set auto-reload to pdTRUE.
3
Start the timer
Start the timer xTimer by calling xTimerStart with block time 0.
FreeRTOS
Hint
Use xTimerStart(xTimer, 0); to start the timer immediately without blocking.
4
Print the LED state inside the callback
Modify the vTimerCallback function to print the current ledState value using printf with the format "LED State: %d\n".
FreeRTOS
Hint
Use printf("LED State: %d\n", ledState); inside the callback to show the LED state changes.
Practice
(1/5)
1. In FreeRTOS, where does a timer callback function execute when a timer expires?
easy
A. Inside the main application task context
B. Inside an interrupt service routine (ISR)
C. Inside the timer service task context
D. Inside a hardware timer peripheral
Solution
Step 1: Understand timer callback execution
Timer callbacks in FreeRTOS run when the timer expires, but not in the main task or ISR context.
Step 2: Identify the correct context
The timer callback runs inside the timer service task context, which is a dedicated task for handling timers.
Final Answer:
Inside the timer service task context -> Option C
Quick Check:
Timer callback context = timer service task [OK]
Hint: Timer callbacks run in timer service task, not main or ISR [OK]
Common Mistakes:
Thinking timer callbacks run in ISR context
Assuming callbacks run in main application task
Confusing hardware timer peripheral with callback context
2. Which of the following is the correct way to define a FreeRTOS timer callback function?
easy
A. void TimerCallback(TimerHandle_t xTimer)
B. void TimerCallback(void)
C. int TimerCallback(int timerId)
D. void TimerCallback(void *param)
Solution
Step 1: Recall FreeRTOS timer callback signature
The callback function must accept a TimerHandle_t parameter representing the timer.
Step 2: Match the correct function signature
Only the signature with void TimerCallback(TimerHandle_t xTimer) matches FreeRTOS requirements.
Final Answer:
void TimerCallback(TimerHandle_t xTimer) -> Option A
The timer is created with 1 second period, one-shot, and started, but vTaskStartScheduler() is missing.
Step 2: Check scheduler requirement
FreeRTOS timers require the scheduler (and timer service task) to be running via vTaskStartScheduler(); without it, the callback never executes, producing no output.
Final Answer:
No output -> Option B
Quick Check:
Missing vTaskStartScheduler() = no callback execution [OK]
A. Using vTaskDelay inside timer callback blocks timer service task
B. Missing return statement in callback
C. Incorrect parameter type in callback
D. printf cannot be used in timer callbacks
Solution
Step 1: Understand timer callback context
Timer callbacks run inside the timer service task, which handles all timers sequentially.
Step 2: Analyze use of vTaskDelay in callback
Calling vTaskDelay blocks the timer service task, delaying other timers and causing timing issues.
Final Answer:
Using vTaskDelay inside timer callback blocks timer service task -> Option A
Quick Check:
Blocking calls in timer callback delay all timers [OK]
Hint: Never block or delay inside timer callbacks [OK]
Common Mistakes:
Thinking callbacks can safely block or delay
Assuming missing return causes error
Believing printf is disallowed in callbacks
5. You want to toggle an LED every 500ms using a FreeRTOS timer. Which approach correctly uses the timer callback without blocking the timer service task?