Software timers help run tasks after a delay or repeatedly without blocking the main program. They make managing time-based actions easy and efficient.
Why software timers are useful in FreeRTOS
TimerHandle_t xTimerCreate(
const char * const pcTimerName,
const TickType_t xTimerPeriodInTicks,
const UBaseType_t uxAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction
);xTimerCreate creates a new software timer.
pxCallbackFunction is the function called when the timer expires.
TimerHandle_t myTimer = xTimerCreate("MyTimer", pdMS_TO_TICKS(1000), pdFALSE, NULL, myCallback);
TimerHandle_t periodicTimer = xTimerCreate("Periodic", pdMS_TO_TICKS(500), pdTRUE, NULL, periodicCallback);
This program creates a one-shot timer that prints a message after 2 seconds. It simulates waiting by printing "Waiting..." every second.
#include "FreeRTOS.h" #include "timers.h" #include <stdio.h> void vTimerCallback(TimerHandle_t xTimer) { printf("Timer expired!\n"); } int main(void) { TimerHandle_t xTimer = xTimerCreate("OneShot", pdMS_TO_TICKS(2000), pdFALSE, NULL, vTimerCallback); if (xTimer != NULL) { xTimerStart(xTimer, 0); } // Simulate scheduler running and waiting for timer for (int i = 0; i < 3; i++) { vTaskDelay(pdMS_TO_TICKS(1000)); printf("Waiting... %d seconds\n", i + 1); } return 0; } // Dummy vTaskDelay for simulation void vTaskDelay(const TickType_t xTicksToDelay) { // Just a placeholder to simulate delay }
Software timers run in the timer service task, so they don't block your main tasks.
Always start the timer with xTimerStart() after creating it.
Use pdMS_TO_TICKS() to convert milliseconds to ticks for timer periods.
Software timers let you run code after a delay or repeatedly without blocking.
They are useful for time-based actions like timeouts and periodic checks.
Creating and starting timers is simple with FreeRTOS API functions.
Practice
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 AQuick Check:
Software timers = delayed non-blocking code [OK]
- Confusing timers with speeding up CPU
- Thinking timers replace tasks
- Assuming timers store data permanently
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 DQuick Check:
Start timer = xTimerStart() [OK]
- Confusing task delay with timer start
- Using queue functions for timers
- Deleting tasks instead of starting timers
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 CQuick Check:
Auto-reload timer = repeated callback [OK]
- Thinking callback runs only once
- Assuming callback runs immediately
- Believing callback never runs
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 AQuick Check:
Timer callback runs only if started [OK]
- Forgetting to start the timer
- Setting invalid timer periods silently
- Assuming creation auto-starts timer
Solution
Step 1: Identify periodic sensor check requirement
You want to check sensor every 500ms repeatedly without blocking.Step 2: Choose timer setup for repeated checks
Auto-reload timer with 500ms period runs callback repeatedly without blocking tasks.Step 3: Eliminate other options
One-shot requires manual restart, vTaskDelay blocks task, 0ms period is invalid.Final Answer:
Create a timer with 500ms period, auto-reload enabled, and put sensor check in callback. -> Option BQuick Check:
Periodic sensor check = auto-reload timer [OK]
- Using one-shot timers for repeated tasks
- Blocking tasks with vTaskDelay
- Setting invalid zero period timers
Ready to prove it?
Pass the test and this pattern moves unknown
