Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Timer for periodic status reporting in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Timer for periodic status reporting
Create Timer
↓
Start Timer
↓
Timer Period Elapses?
No→Wait
Yes↓
Timer Callback Executes
↓
Report Status
↓
Timer Auto-Restarts
↩Back to Timer Period Elapses
The timer is created and started, then waits for the period to elapse. When time is up, the callback runs to report status, and the timer restarts automatically.
Execution Sample
FreeRTOS
TimerHandle_t xTimer = xTimerCreate("StatusTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, vStatusReportCallback);
xTimerStart(xTimer, 0);

void vStatusReportCallback(TimerHandle_t xTimer) {
    printf("Status: All systems OK\n");
}
This code creates a FreeRTOS timer that calls a function every 1000 milliseconds to print a status message.
Execution Table
StepTimer StateTime Elapsed (ms)ConditionActionOutput
1Created0Timer started?Start timer
2Running01000 ms elapsed?No - wait
3Running10001000 ms elapsed?Yes - call callbackStatus: All systems OK
4Running1000Auto-reload enabled?Yes - timer restarts
5Running20001000 ms elapsed?Yes - call callbackStatus: All systems OK
6Running2000Auto-reload enabled?Yes - timer restarts
7Running30001000 ms elapsed?Yes - call callbackStatus: All systems OK
8Running3000Auto-reload enabled?Yes - timer restarts
9Running40001000 ms elapsed?Yes - call callbackStatus: All systems OK
10Running4000Auto-reload enabled?Yes - timer restarts
11StoppedN/ATimer stopped externally?Stop timer
💡 Timer stopped externally or system shutdown
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Timer StateCreatedRunningRunningRunningRunningStopped
Time Elapsed (ms)01000200030004000N/A
Callback CalledNoYesYesYesYesNo
Key Moments - 3 Insights
Why does the timer callback run multiple times automatically without restarting it manually?
Because the timer is created with auto-reload enabled (pdTRUE), it restarts itself after each period, as shown in execution_table rows 4, 6, 8, and 10.
What happens if the timer is stopped externally?
The timer state changes to 'Stopped' and no further callbacks occur, as shown in execution_table row 11 where the timer stops and no output is produced.
Why is the time elapsed reset after each callback?
The timer counts time per period, so after each callback, the elapsed time resets to zero for the next period, reflected by the increasing multiples of 1000 ms in the table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the first status report get printed?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Check the 'Output' column in execution_table row 3 for the first printed status.
According to variable_tracker, what is the Timer State after the second callback?
ACreated
BRunning
CStopped
DPaused
💡 Hint
Look at 'Timer State' row in variable_tracker after 'After 2' column.
If auto-reload was set to pdFALSE, how would the execution_table change after the first callback?
ATimer would stop and no further callbacks occur
BTimer would continue running and callbacks repeat
CTimer would restart immediately without delay
DTimer would print multiple status messages at once
💡 Hint
Refer to execution_table rows 4 and 6 where auto-reload causes restart; without it, timer stops.
Concept Snapshot
FreeRTOS timers run code periodically.
Create timer with xTimerCreate(name, period, auto_reload, id, callback).
Start timer with xTimerStart.
Callback runs when period elapses.
Auto-reload (pdTRUE) restarts timer automatically.
Use for periodic status reporting or tasks.
Full Transcript
This visual execution trace shows how a FreeRTOS timer is created, started, and runs a callback periodically every 1000 milliseconds. The timer is created with auto-reload enabled, so after each callback execution that prints a status message, the timer restarts automatically. The execution table tracks the timer state, elapsed time, conditions checked, actions taken, and output printed at each step. The variable tracker shows how the timer state and callback calls change over time. Key moments clarify why the timer repeats automatically and what happens when it stops. The quiz questions test understanding of when callbacks occur, timer state changes, and effects of auto-reload. The snapshot summarizes the key syntax and behavior for using FreeRTOS timers for periodic status reporting.

Practice

(1/5)
1. What is the main purpose of using a timer in FreeRTOS for periodic status reporting?
easy
A. To pause the entire system for a set time
B. To create a new task that runs forever
C. To run a function repeatedly at fixed time intervals
D. To allocate memory dynamically for tasks

Solution

  1. Step 1: Understand timer functionality in FreeRTOS

    Timers in FreeRTOS are designed to call a function repeatedly or once after a delay.
  2. Step 2: Relate to periodic status reporting

    Periodic status reporting requires running code repeatedly at fixed intervals, which timers enable.
  3. Final Answer:

    To run a function repeatedly at fixed time intervals -> Option C
  4. Quick Check:

    Timer = repeated function calls [OK]
Hint: Timers run code repeatedly at set intervals [OK]
Common Mistakes:
  • Confusing timers with tasks
  • Thinking timers pause the system
  • Assuming timers allocate memory
2. Which of the following is the correct way to create a FreeRTOS timer named statusTimer with a 1000 ms period and auto-reload enabled?
easy
A. xTimerCreate("statusTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback);
B. xTimerCreate("statusTimer", 1000, pdFALSE, NULL, TimerCallback);
C. xTimerCreate("statusTimer", pdMS_TO_TICKS(1000), pdFALSE, NULL, TimerCallback);
D. xTimerCreate("statusTimer", 1000, pdTRUE, NULL, TimerCallback);

Solution

  1. Step 1: Identify correct period conversion

    FreeRTOS timers use ticks, so pdMS_TO_TICKS(1000) converts 1000 ms to ticks correctly.
  2. Step 2: Check auto-reload parameter

    pdTRUE enables auto-reload for periodic timers, which is needed here.
  3. Final Answer:

    xTimerCreate("statusTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback); -> Option A
  4. Quick Check:

    Use pdMS_TO_TICKS and pdTRUE for periodic timers [OK]
Hint: Use pdMS_TO_TICKS for ms and pdTRUE for auto-reload [OK]
Common Mistakes:
  • Using raw milliseconds instead of ticks
  • Setting auto-reload to pdFALSE for periodic timer
  • Confusing pdTRUE and pdFALSE values
3. Given the code snippet below, what will be printed every second?
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;
}
medium
A. Status: OK printed every second repeatedly
B. Status: OK printed once after 1 second
C. No output because timer is not started
D. Compilation error due to missing parameters

Solution

  1. Step 1: Analyze timer creation and start

    The timer is created with pdTRUE for auto-reload and started immediately, so it runs repeatedly.
  2. Step 2: Understand callback behavior

    The callback prints "Status: OK" each time the timer expires, which is every 1000 ms (1 second).
  3. Final Answer:

    Status: OK printed every second repeatedly -> Option A
  4. Quick Check:

    Auto-reload timer prints every second [OK]
Hint: Auto-reload timers call callback repeatedly [OK]
Common Mistakes:
  • Assuming timer runs only once
  • Forgetting to start the timer
  • Confusing callback parameters
4. Identify the error in the following FreeRTOS timer code snippet:
TimerHandle_t statusTimer = xTimerCreate("statusTimer", 1000, pdTRUE, NULL, TimerCallback);
xTimerStart(statusTimer, 0);
medium
A. Timer callback function is missing
B. Timer name must be NULL, not a string
C. Auto-reload parameter must be pdFALSE for periodic timers
D. Timer period should be converted to ticks using pdMS_TO_TICKS

Solution

  1. Step 1: Check timer period parameter

    The period is given as 1000 (milliseconds) but FreeRTOS expects ticks, so pdMS_TO_TICKS is needed.
  2. Step 2: Verify other parameters

    The timer name as string is allowed, auto-reload pdTRUE is correct, and callback is assumed defined.
  3. Final Answer:

    Timer period should be converted to ticks using pdMS_TO_TICKS -> Option D
  4. Quick Check:

    Always convert ms to ticks with pdMS_TO_TICKS [OK]
Hint: Always convert ms to ticks with pdMS_TO_TICKS [OK]
Common Mistakes:
  • Passing raw milliseconds instead of ticks
  • Confusing auto-reload true/false
  • Assuming timer name must be NULL
5. You want to implement a FreeRTOS timer that reports system status every 2 seconds but only for 5 times, then stops automatically. Which approach correctly achieves this?
hard
A. Create a one-shot timer with 2-second period and restart it manually 5 times in callback
B. Create an auto-reload timer with 2-second period and count executions in callback, then stop timer after 5 times
C. Create an auto-reload timer with 10-second period and print status every 2 seconds inside callback
D. Create a one-shot timer with 10-second period and start it 5 times in a loop

Solution

  1. Step 1: Understand timer types and requirements

    Auto-reload timers run repeatedly; one-shot timers run once. We need repeated calls every 2 seconds.
  2. 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.
  3. Final Answer:

    Create an auto-reload timer with 2-second period and count executions in callback, then stop timer after 5 times -> Option B
  4. Quick Check:

    Auto-reload + count + stop after 5 [OK]
Hint: Use auto-reload timer and stop after count in callback [OK]
Common Mistakes:
  • Using one-shot timer without manual restarts
  • Incorrect timer period causing wrong intervals
  • Not stopping timer after desired count
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard