Introduction to RTOS Part 8 - Software Timer | Digi-Key Electronics

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 xTaskGetTickCount allows 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.h by setting configUSE_TIMERS to 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.h for 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 xTimerCreate function 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_DELAY indicates 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.
Video description

In embedded systems, timers offer the ability to delay a call to a function, periodically call a function, or control some peripheral without the need for CPU intervention. In FreeRTOS, a software timer is controlled by a timer service task and can be used to execute a function some time in the future based on the RTOS tick timer. The solution to the challenge in the video can be found here: https://www.digikey.com/en/maker/projects/introduction-to-rtos-solution-to-part-8-software-timers/0f64cf758da440a29476165a5b2e577e Code for this video series (including demonstrations, challenges, and solutions) can be found here: https://github.com/ShawnHymel/introduction-to-rtos FreeRTOS gives us a software timer library that we can use to configure several timers. These timers are tied to the tick timer, so we cannot get a resolution greater than the tick timer’s resolution (1 ms by default in the ESP32 Arduino package). When we include the timer library, it will automatically spawn a timer service (or “daemon”) task that runs at priority 1 (default in ESP32 Arduino package). This service task is in charge of managing all of the software timers that are set. Timers can count up or down and can be set to “one-shot” or “auto-reload.” Whenever a counter reaches its maximum value (as set in code), it will execute the callback function associated with that timer. A “one-shot” timer will execute the callback function only once, unless the timer is told to start counting again. An “auto-reload” timer will execute the callback function periodically at a rate specified by the timer’s max value. In the video, we demonstrate the use of software timers and explain how they function within FreeRTOS. We also issue a challenge to create an auto-dimming LED feature whenever an interface is not being used. Product Links: https://www.digikey.com/en/products/detail/adafruit-industries-llc/3405/7244967 Related Videos: Introduction to RTOS Part 1 - What is a Real-Time Operating System (RTOS)? - https://youtu.be/F321087yYy4​ Introduction to RTOS Part 2 - Getting Started with FreeRTOS - https://youtu.be/JIr7Xm_riRs​ Introduction to RTOS Part 3 - Task Scheduling - https://youtu.be/95yUbClyf3E​ Introduction to RTOS Part 4 - Memory Management - https://youtu.be/Qske3yZRW5I​ Introduction to RTOS Part 5 - Queue - https://youtu.be/pHJ3lxOoWeI​ Introduction to RTOS Part 6 - Mutex - https://youtu.be/I55auRpbiTs​ Introduction to RTOS Part 7 - https://youtu.be/5JcMtbA9QEE​ Introduction to RTOS Part 8 - https://youtu.be/b1f1Iex0Tso Introduction to RTOS Part 9 - https://youtu.be/qsflCf6ahXU Introduction to RTOS Part 10 - https://youtu.be/hRsWi4HIENc Introduction to RTOS Part 11 - https://youtu.be/C2xKhxROmhA Introduction to RTOS Part 12 - https://youtu.be/LPSHUcH5aQc Related Project Links: https://www.digikey.com/en/maker/projects/introduction-to-rtos-solution-to-part-8-software-timers/0f64cf758da440a29476165a5b2e577e Related Articles: https://www.digikey.com/en/maker/videos/shawn-hymel/getting-started-with-stm32-and-nucleo-part-3-how-to-run-multiple-threads-with-cmsis-rtos-interface Learn more: Maker.io - https://www.digikey.com/en/maker Digi-Key’s Blog – TheCircuit https://www.digikey.com/en/blog Connect with Digi-Key on Facebook https://www.facebook.com/digikey.electronics/ And follow us on Twitter https://twitter.com/digikey