Introduction to RTOS Part 8 - Software Timer | Digi-Key Electronics
Understanding Timers in Microcontroller Programming
The Importance of Timers
- Timers are essential for managing function execution delays and periodic tasks in programming, particularly in microcontrollers.
- Common applications include blinking LEDs, refreshing LCDs, polling sensors, or sending pulses to servo motors.
Managing Periodic Tasks
- For tasks like polling a temperature sensor every 220 milliseconds, creating separate tasks can be inefficient due to overhead.
- Using
xTaskGetTickCountallows existing tasks to check elapsed time without the need for additional task creation.
Precision and Limitations
- The default tick timer is set to one millisecond; higher precision requires hardware timers which are limited and less portable.
- Software timers in FreeRTOS can call functions when they expire but are also bound by the tick timer's precision.
Timer Service Task Functionality
- FreeRTOS creates a background task known as the Timer Service Task that manages timers and executes callback functions upon expiration.
- Callback functions run at the same priority level as the Timer Service Task and should execute quickly without blocking.
Best Practices for Timer Callbacks
- Avoid using delay functions or blocking operations within timer callbacks; instead, utilize FreeRTOS APIs for command management.
- Commands sent to the Timer Service Task via API functions allow control over multiple timers efficiently.
Configuring Timers in FreeRTOS on ESP32
Initial Configuration Steps
- To enable timer support on ESP32 with Arduino, modify
FreeRTOSConfig.hby settingconfigUSE_TIMERSto 1 if not already configured.
Understanding Default Settings
- Default settings include a Timer Service Task priority of 1, queue length of 10, and stack depth of 2 kilobytes (in words).
Available API Functions
- Various API functions exist for creating, deleting, starting, stopping, and resetting timers.
- Use "from ISR" functions when interacting with the Timer Service Task from interrupt service routines to avoid blocking issues.
Using Software Timers in FreeRTOS
Introduction to Software Timers
- The discussion begins with the use of software timers in FreeRTOS, specifically mentioning the need to include
timers.hfor those using vanilla FreeRTOS. The Arduino ESP32 package includes this by default.
- A one-shot timer is introduced, which calls a specified function after a set time period but only executes once. A callback function is created that accepts a timer handle as a parameter.
Timer Configuration and Setup
- The setup phase involves configuring the serial port and printing a welcome message. A divider is added for clarity in execution flow.
- The
xTimerCreatefunction is called to create the timer, requiring five parameters: name, length (in ticks), auto-reload setting, timer ID pointer, and callback function name.
- It’s emphasized that timers cannot be less than one tick; thus, creating timers shorter than one millisecond isn't possible with this configuration.
Timer Functionality
- The importance of checking for null pointers after attempting to create a timer is highlighted. If successful, it proceeds to start the timer using
xTimerStart.
- When starting the timer, it’s crucial to specify a queue wait time; here,
portMAX_DELAYindicates waiting indefinitely if needed.
One-Shot vs Auto-Relaod Timers
- After demonstrating the one-shot timer's functionality, an auto-reload timer is introduced. This requires creating another global variable for its handle.
- Changes are made to configure the auto-reload timer: updating its handle variable and setting its period to 1000 milliseconds while enabling auto-reload.
Practical Application and Challenge
- In the callback function for both timers, unique identification of each calling timer is implemented by retrieving their IDs during creation.
- Upon running both timers on an ESP32 device, it's observed that the auto-reload timer expires first and continues executing every second while the one-shot stops after its initial execution.
Conclusion and Next Steps
- A challenge is presented involving an LCD backlight dimming feature where users must implement character input handling via serial terminal commands alongside LED control using software timers.