/**
* FreeRTOS Software Timer Demo
*
* Demonstrate basic timer usage.
*
* Date: April 16, 2025
* Author: Mike
* License: 0BSD
*/
// You'll likely need this on vanilla FreeRTOS
//#include <timers.h>
// Use only core 1 for demo purposes
#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif
// Globals
static TimerHandle_t one_shot_timer = NULL;
static TimerHandle_t auto_reload_timer = NULL;
//*****************************************************************************
// Callbacks
void myTimerCallback(TimerHandle_t xTimer){
if ((uint32_t)pvTimerGetTimerID(xTimer) == 0) {
Serial.println("One-shot timer expired");
}
if ((uint32_t)pvTimerGetTimerID(xTimer) == 1) {
Serial.println("Auto-reload timer expired");
}
}
//*****************************************************************************
// Main (runs as its own task with priority 1 on core 1)
void setup() {
// Serial configuration.
Serial.begin(115200);
vTaskDelay(1000 / portTICK_PERIOD_MS);
Serial.println();
Serial.println("---FreeRTOS Timer Demo---");
// Create timers.
one_shot_timer = xTimerCreate(
"One-shot timer", // Name of timer
2000 / portTICK_PERIOD_MS, // Period/length of timer (in ticks)
pdFALSE, // Auto-reload
(void *)0, // Timer ID (a pointer to something)
myTimerCallback); // Callback function
auto_reload_timer = xTimerCreate(
"Auto-reload timer", // Name of timer
1000 / portTICK_PERIOD_MS, // Period of timer (in ticks)
pdTRUE, // Auto-reload
(void *)1, // Timer ID
myTimerCallback); // Callback function
// Since the timer uses heap memory to create necessary structures, it's possible that xTimer create will fail
if (one_shot_timer == NULL || auto_reload_timer == NULL){
Serial.println("Could not create one of the timers");
}
else{
vTaskDelay(1000 / portTICK_PERIOD_MS);
Serial.println("Starting timers...");
// Start timers (max block time if the command queue is full)
xTimerStart(one_shot_timer, portMAX_DELAY);
xTimerStart(auto_reload_timer, portMAX_DELAY);
}
// Delete the setup and loop task.
vTaskDelete(NULL);
}
void loop() {
// Execution should never get here.
}