📖 Scenario: You are developing a simple embedded application using FreeRTOS. You want to understand how to use one-shot and auto-reload timers to perform tasks after delays or periodically.
🎯 Goal: Build a FreeRTOS program that creates two timers: one one-shot timer and one auto-reload timer. The program will print messages when each timer expires, demonstrating the difference between the two timer types.
📋 What You'll Learn
Create a one-shot timer with a 2-second delay
Create an auto-reload timer with a 1-second period
Start both timers
Print messages when timers expire to show their behavior
💡 Why This Matters
🌍 Real World
Timers are used in embedded systems to perform actions after delays or periodically, such as blinking LEDs, reading sensors, or sending data.
💼 Career
Understanding FreeRTOS timers is essential for embedded software developers working on real-time applications and IoT devices.
Progress0 / 4 steps
1
Create the one-shot timer
Create a one-shot timer called oneShotTimer with a 2000 ms period and a callback function named vOneShotTimerCallback. Use xTimerCreate with pdFALSE for auto-reload parameter.
FreeRTOS
Hint
Use xTimerCreate with the third argument pdFALSE to make a one-shot timer.
2
Create the auto-reload timer
Create an auto-reload timer called autoReloadTimer with a 1000 ms period and a callback function named vAutoReloadTimerCallback. Use xTimerCreate with pdTRUE for auto-reload parameter.
FreeRTOS
Hint
Use xTimerCreate with the third argument pdTRUE to make an auto-reload timer.
3
Start both timers
Start both oneShotTimer and autoReloadTimer using xTimerStart with a block time of 0.
FreeRTOS
Hint
Use xTimerStart with block time 0 to start timers immediately.
4
Print messages in timer callbacks
Add printf statements inside vOneShotTimerCallback and vAutoReloadTimerCallback to print "One-shot timer expired" and "Auto-reload timer expired" respectively. Then call startTimers() and run the FreeRTOS scheduler with vTaskStartScheduler().
FreeRTOS
Hint
Use printf inside each callback to show when timers expire. Call startTimers() and then vTaskStartScheduler() in main().
Practice
(1/5)
1. What is the main difference between a one-shot timer and an auto-reload timer in FreeRTOS?
easy
A. One-shot timers run once after a delay; auto-reload timers run repeatedly at fixed intervals.
B. One-shot timers run repeatedly; auto-reload timers run only once.
C. One-shot timers are faster than auto-reload timers.
D. Auto-reload timers cannot be stopped once started.
Solution
Step 1: Understand timer behavior
One-shot timers execute their callback function only once after the specified delay.
Step 2: Compare with auto-reload timers
Auto-reload timers automatically restart after each expiration, running repeatedly at fixed intervals.
Final Answer:
One-shot timers run once after a delay; auto-reload timers run repeatedly at fixed intervals. -> Option A
Quick Check:
One-shot = single run, Auto-reload = repeated runs [OK]
Hint: One-shot = once, Auto-reload = repeat [OK]
Common Mistakes:
Confusing which timer runs repeatedly
Thinking one-shot timers repeat automatically
Assuming auto-reload timers cannot be stopped
2. Which of the following is the correct way to create an auto-reload timer in FreeRTOS using xTimerCreate?
easy
A. xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdFALSE, NULL, TimerCallback);
B. xTimerCreate("Timer", 1000, pdTRUE, NULL, TimerCallback);
C. xTimerCreate("Timer", 1000, pdFALSE, NULL, TimerCallback);
D. xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback);
Solution
Step 1: Identify auto-reload parameter
The third parameter of xTimerCreate is uxAutoReload, which should be pdTRUE for auto-reload timers.
Step 2: Check time parameter format
Use pdMS_TO_TICKS(1000) to convert milliseconds to ticks correctly.
Final Answer:
xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback); -> Option D
Quick Check:
Auto-reload needs pdTRUE and tick conversion [OK]
Hint: Auto-reload = pdTRUE, use pdMS_TO_TICKS for time [OK]