Index
In FreeRTOS, software timers provide a way to perform periodic or delayed operations independently of task execution. They run in the context of the timer service task, meaning that timer callbacks do not directly compete with other tasks for CPU time, making them ideal for non-critical operations that don’t need to run in real time.
Key Concepts
- One-shot timer: Runs only once after a specified delay.
- Auto-reload timer: Runs periodically at specified intervals.
Benefits of Software Timers
- Offloads work from tasks that don’t need immediate attention.
- Simplifies periodic or delayed tasks that don’t require a dedicated task.
- Reduces overhead compared to tasks that would otherwise use
vTaskDelay()
orvTaskDelayUntil()
for time-based operations.
Functions for Software Timers
xTimerCreate()
: Creates a timer.xTimerStart()
: Starts the timer.xTimerStop()
: Stops the timer.xTimerChangePeriod()
: Changes the timer’s period.
Example: Using Software Timers in FreeRTOS
In this example, we create two software timers:
- A one-shot timer that toggles an LED once after a 2-second delay.
- An auto-reload timer that toggles an LED every 1 second.
Code Example:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "driver/gpio.h"
#define LED_PIN_ONE_SHOT 2 // GPIO for one-shot timer LED
#define LED_PIN_AUTO_RELOAD 4 // GPIO for auto-reload timer LED
// Timer handles
TimerHandle_t oneShotTimer;
TimerHandle_t autoReloadTimer;
// One-shot timer callback function
void oneShotTimerCallback(TimerHandle_t xTimer) {
static int ledState = 0;
ledState = !ledState; // Toggle LED state
gpio_set_level(LED_PIN_ONE_SHOT, ledState);
printf("One-shot Timer: LED toggled after 2 seconds\n");
}
// Auto-reload timer callback function
void autoReloadTimerCallback(TimerHandle_t xTimer) {
static int ledState = 0;
ledState = !ledState; // Toggle LED state
gpio_set_level(LED_PIN_AUTO_RELOAD, ledState);
printf("Auto-reload Timer: LED toggled every 1 second\n");
}
void app_main(void) {
// Configure LED pins as outputs
gpio_pad_select_gpio(LED_PIN_ONE_SHOT);
gpio_set_direction(LED_PIN_ONE_SHOT, GPIO_MODE_OUTPUT);
gpio_pad_select_gpio(LED_PIN_AUTO_RELOAD);
gpio_set_direction(LED_PIN_AUTO_RELOAD, GPIO_MODE_OUTPUT);
// Create a one-shot timer with a 2-second delay
oneShotTimer = xTimerCreate("One-Shot Timer", pdMS_TO_TICKS(2000), pdFALSE, (void *)0, oneShotTimerCallback);
if (oneShotTimer == NULL) {
printf("Failed to create one-shot timer\n");
} else {
// Start the one-shot timer
xTimerStart(oneShotTimer, 0);
}
// Create an auto-reload timer with a 1-second period
autoReloadTimer = xTimerCreate("Auto-Reload Timer", pdMS_TO_TICKS(1000), pdTRUE, (void *)0, autoReloadTimerCallback);
if (autoReloadTimer == NULL) {
printf("Failed to create auto-reload timer\n");
} else {
// Start the auto-reload timer
xTimerStart(autoReloadTimer, 0);
}
}
Explanation of the Code
- Setting up GPIO for LEDs:
LED_PIN_ONE_SHOT
andLED_PIN_AUTO_RELOAD
are configured as output pins for LEDs that will be toggled by the timers.
- Creating the One-Shot Timer:
xTimerCreate()
is used to create a one-shot timer with a delay of 2000 milliseconds (2 seconds).- The third parameter,
pdFALSE
, specifies that this is a one-shot timer. oneShotTimerCallback
is defined as the callback function that toggles the LED once after 2 seconds.
- Creating the Auto-Reload Timer:
xTimerCreate()
creates an auto-reload timer with a period of 1000 milliseconds (1 second).- The third parameter,
pdTRUE
, specifies that this is an auto-reload timer. autoReloadTimerCallback
toggles the LED every 1 second while the timer is active.
- Starting the Timers:
- Both
xTimerStart()
calls start the one-shot and auto-reload timers immediately.
- Both
Output Explanation
When running this code, you should see output like:
One-shot Timer: LED toggled after 2 seconds
Auto-reload Timer: LED toggled every 1 second
Auto-reload Timer: LED toggled every 1 second
...
- The one-shot timer toggles the
LED_PIN_ONE_SHOT
LED once after 2 seconds, and then stops. - The auto-reload timer toggles the
LED_PIN_AUTO_RELOAD
LED every second.
Summary
- One-shot timers are useful for delayed, single-time events, such as initial setup.
- Auto-reload timers are ideal for repetitive tasks, like periodic sensor readings.
FreeRTOS software timers simplify task timing management by allowing scheduled delays and periodic tasks without interrupting task flow or adding more tasks.