📖 Scenario: You are building a simple embedded system using FreeRTOS. You want to perform certain actions repeatedly after fixed time intervals without blocking the main tasks.
🎯 Goal: Learn how to set up and use software timers in FreeRTOS to perform actions periodically without stopping other tasks.
📋 What You'll Learn
Create a FreeRTOS software timer with a specific period
Set up a callback function that runs when the timer expires
Start the software timer
Print a message each time the timer callback runs
💡 Why This Matters
🌍 Real World
Software timers help embedded systems perform tasks like blinking LEDs, reading sensors, or sending data at regular intervals without stopping other important work.
💼 Career
Understanding software timers is essential for embedded developers working with FreeRTOS to build responsive and efficient real-time applications.
Progress0 / 4 steps
1
Create a software timer variable
Create a variable called xTimer of type TimerHandle_t and initialize it to NULL.
FreeRTOS
Hint
Use the type TimerHandle_t and set xTimer to NULL initially.
2
Define the timer callback function
Write a function called vTimerCallback that takes a TimerHandle_t xTimer parameter and prints the message "Timer callback executed".
FreeRTOS
Hint
The callback function must match the signature void vTimerCallback(TimerHandle_t xTimer) and use printf to print the message.
3
Create and start the software timer
Create the software timer xTimer using xTimerCreate with the name "MyTimer", a period of 1000 / portTICK_PERIOD_MS (1 second), auto-reload set to pdTRUE, no timer ID (NULL), and the callback function vTimerCallback. Then start the timer using xTimerStart.
FreeRTOS
Hint
Use xTimerCreate with the exact parameters and then call xTimerStart with block time 0.
4
Observe the timer callback output
Print the message "Timer started, waiting for callbacks..." to indicate the timer is running.
FreeRTOS
Hint
Use printf to show the message exactly as given.
Practice
(1/5)
1. Why are software timers useful in FreeRTOS?
easy
A. They allow running code after a delay without blocking other tasks.
B. They increase the CPU speed automatically.
C. They replace the need for tasks entirely.
D. They store data permanently in memory.
Solution
Step 1: Understand the purpose of software timers
Software timers let you schedule code to run later or repeatedly without stopping other tasks.
Step 2: Compare options to this purpose
Only They allow running code after a delay without blocking other tasks. describes this behavior correctly; others describe unrelated features.
Final Answer:
They allow running code after a delay without blocking other tasks. -> Option A
Quick Check:
Software timers = delayed non-blocking code [OK]
Hint: Think: timers run code later without stopping tasks [OK]
Common Mistakes:
Confusing timers with speeding up CPU
Thinking timers replace tasks
Assuming timers store data permanently
2. Which FreeRTOS API function is used to start a software timer?
easy
A. vTaskDelay()
B. vTaskDelete()
C. xQueueSend()
D. xTimerStart()
Solution
Step 1: Identify the function to start timers
The FreeRTOS API uses xTimerStart() to begin a software timer.
Step 2: Eliminate unrelated functions
vTaskDelay() delays tasks, xQueueSend() sends data to queues, vTaskDelete() deletes tasks, so they are incorrect.
Final Answer:
xTimerStart() -> Option D
Quick Check:
Start timer = xTimerStart() [OK]
Hint: Start timers with xTimerStart() function [OK]
Common Mistakes:
Confusing task delay with timer start
Using queue functions for timers
Deleting tasks instead of starting timers
3. What will happen if a software timer is set with a 1000ms period and auto-reload enabled?
medium
A. The timer callback never runs.
B. The timer callback runs once after 1000ms.
C. The timer callback runs repeatedly every 1000ms.
D. The timer callback runs immediately and stops.
Solution
Step 1: Understand auto-reload behavior
When auto-reload is enabled, the timer restarts automatically after each period.
Step 2: Apply to 1000ms period
The callback runs every 1000ms repeatedly, not just once or immediately.
Final Answer:
The timer callback runs repeatedly every 1000ms. -> Option C
Quick Check:
Auto-reload timer = repeated callback [OK]
Hint: Auto-reload means repeat callback every period [OK]
Common Mistakes:
Thinking callback runs only once
Assuming callback runs immediately
Believing callback never runs
4. You created a software timer but its callback never runs. What is a likely cause?
medium
A. The timer was not started with xTimerStart().
B. The timer period was set to zero.
C. The callback function has no parameters.
D. The timer was created with xTimerCreate() correctly.
Solution
Step 1: Check timer start requirement
After creating a timer, you must start it with xTimerStart() for callbacks to run.
Step 2: Evaluate other options
Zero period is invalid but usually causes errors; callback parameters are fixed by API; correct creation alone doesn't start timer.
Final Answer:
The timer was not started with xTimerStart(). -> Option A
Quick Check:
Timer callback runs only if started [OK]
Hint: Always start timers with xTimerStart() after creation [OK]
Common Mistakes:
Forgetting to start the timer
Setting invalid timer periods silently
Assuming creation auto-starts timer
5. You want to check a sensor every 500ms without blocking other tasks. How should you use a FreeRTOS software timer?
hard
A. Create a timer with 0ms period to check sensor continuously.
B. Create a timer with 500ms period, auto-reload enabled, and put sensor check in callback.
C. Use vTaskDelay(500) inside a task to check sensor periodically.
D. Create a one-shot timer with 500ms period and restart it manually each time.