A timer helps run a task repeatedly at set times. This is useful to check or report status regularly without stopping other work.
Timer for periodic status reporting in FreeRTOS
TimerHandle_t xTimerCreate(
const char * const pcTimerName,
const TickType_t xTimerPeriodInTicks,
const UBaseType_t uxAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction
);
BaseType_t xTimerStart(TimerHandle_t xTimer, TickType_t xTicksToWait);xTimerCreate makes a timer. You give it a name, time period, if it repeats, an ID, and a function to run when time is up.
xTimerStart starts the timer counting. The timer calls your function when the time ends.
vTimerCallback every second repeatedly.TimerHandle_t myTimer = xTimerCreate("StatusTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, vTimerCallback); // Creates a timer that runs every 1000 ms (1 second) and repeats.
vTimerCallback only once after 5 seconds.TimerHandle_t oneShotTimer = xTimerCreate("OneShot", pdMS_TO_TICKS(5000), pdFALSE, NULL, vTimerCallback); // Creates a timer that runs once after 5 seconds.
This program creates a timer that prints a status message every 2 seconds. The message includes a count of how many times it has run.
The timer runs repeatedly because pdTRUE is set for auto-reload.
The scheduler starts to run tasks and timers.
#include "FreeRTOS.h" #include "timers.h" #include "task.h" #include <stdio.h> // Timer callback function void vStatusTimerCallback(TimerHandle_t xTimer) { static int count = 0; count++; printf("Status report #%d: System running.\n", count); } int main(void) { // Create a timer that triggers every 2 seconds TimerHandle_t xStatusTimer = xTimerCreate( "StatusTimer", pdMS_TO_TICKS(2000), // 2000 ms = 2 seconds pdTRUE, // Auto-reload (periodic) NULL, vStatusTimerCallback ); if (xStatusTimer == NULL) { printf("Failed to create timer.\n"); return 1; } // Start the timer if (xTimerStart(xStatusTimer, 0) != pdPASS) { printf("Failed to start timer.\n"); return 1; } // Start the scheduler vTaskStartScheduler(); // Should never reach here for(;;); return 0; }
Timer callback runs in the timer service task, so keep it short and fast.
Use pdMS_TO_TICKS() to convert milliseconds to ticks for timer periods.
Make sure FreeRTOS scheduler is running for timers to work.
Timers let you run code repeatedly or once after a delay.
Use xTimerCreate to make a timer and xTimerStart to start it.
Timer callbacks should be quick and simple to avoid blocking other tasks.
Practice
Solution
Step 1: Understand timer functionality in FreeRTOS
Timers in FreeRTOS are designed to call a function repeatedly or once after a delay.Step 2: Relate to periodic status reporting
Periodic status reporting requires running code repeatedly at fixed intervals, which timers enable.Final Answer:
To run a function repeatedly at fixed time intervals -> Option CQuick Check:
Timer = repeated function calls [OK]
- Confusing timers with tasks
- Thinking timers pause the system
- Assuming timers allocate memory
statusTimer with a 1000 ms period and auto-reload enabled?Solution
Step 1: Identify correct period conversion
FreeRTOS timers use ticks, so pdMS_TO_TICKS(1000) converts 1000 ms to ticks correctly.Step 2: Check auto-reload parameter
pdTRUE enables auto-reload for periodic timers, which is needed here.Final Answer:
xTimerCreate("statusTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback); -> Option AQuick Check:
Use pdMS_TO_TICKS and pdTRUE for periodic timers [OK]
- Using raw milliseconds instead of ticks
- Setting auto-reload to pdFALSE for periodic timer
- Confusing pdTRUE and pdFALSE values
void TimerCallback(TimerHandle_t xTimer) {
printf("Status: OK\n");
}
int main() {
TimerHandle_t statusTimer = xTimerCreate("statusTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback);
xTimerStart(statusTimer, 0);
vTaskStartScheduler();
return 0;
}Solution
Step 1: Analyze timer creation and start
The timer is created with pdTRUE for auto-reload and started immediately, so it runs repeatedly.Step 2: Understand callback behavior
The callback prints "Status: OK" each time the timer expires, which is every 1000 ms (1 second).Final Answer:
Status: OK printed every second repeatedly -> Option AQuick Check:
Auto-reload timer prints every second [OK]
- Assuming timer runs only once
- Forgetting to start the timer
- Confusing callback parameters
TimerHandle_t statusTimer = xTimerCreate("statusTimer", 1000, pdTRUE, NULL, TimerCallback);
xTimerStart(statusTimer, 0);Solution
Step 1: Check timer period parameter
The period is given as 1000 (milliseconds) but FreeRTOS expects ticks, so pdMS_TO_TICKS is needed.Step 2: Verify other parameters
The timer name as string is allowed, auto-reload pdTRUE is correct, and callback is assumed defined.Final Answer:
Timer period should be converted to ticks using pdMS_TO_TICKS -> Option DQuick Check:
Always convert ms to ticks with pdMS_TO_TICKS [OK]
- Passing raw milliseconds instead of ticks
- Confusing auto-reload true/false
- Assuming timer name must be NULL
Solution
Step 1: Understand timer types and requirements
Auto-reload timers run repeatedly; one-shot timers run once. We need repeated calls every 2 seconds.Step 2: Implement counting and stopping logic
Use an auto-reload timer with 2-second period, count callback calls, and stop timer after 5 executions.Final Answer:
Create an auto-reload timer with 2-second period and count executions in callback, then stop timer after 5 times -> Option BQuick Check:
Auto-reload + count + stop after 5 [OK]
- Using one-shot timer without manual restarts
- Incorrect timer period causing wrong intervals
- Not stopping timer after desired count
Ready to prove it?
Pass the test and this pattern moves unknown
