ESP32 Tutorial – Using FreeRTOS In IDF


Introduction

ESP32 is a powerful microcontroller, and the SDK uses ESP-IDF (Espressif IoT Development Framework) to effectively develop embedded applications. FreeRTOS is a real-time operating system (RTOS) that is often used in embedded systems to achieve multitasking.

FreeRTOS

FreeRTOS is a lightweight real-time operating system designed for microcontrollers with the following features:

1. Multi-task scheduling: supports multiple tasks running in parallel.
2. Time management: Provide delay and timing functions.
3. Resource management: including mutex (Mutex), semaphore (Semaphore), queue (Queue), etc., used for communication and synchronization between tasks.
4. Low power consumption: Supports low power consumption management, suitable for battery-powered equipment.

ESP32 IDF’s FreeRTOS

FreeRTOS is a built-in part of ESP-IDF for ESP32. Developers can use the API provided by ESP-IDF to create and manage FreeRTOS tasks.

Install VSCode & ESP-IDF VSCode Extension

Make sure you have installed and configured the ESP-IDF development environment. You can also refer to the article the ESP32 Tutorial – ESP-IDF With VSCode.

Create A New ESP32 Project

We can use VSCode's IDF plug-in to create a new ESP32 project. Please refer to ESP32 Tutorial – How To Create An ESP32 Project With VSCode.

Coding main.c

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#include "esp_system.h"
#include "esp_log.h"

// Timer handles
TimerHandle_t timer1Handle = NULL;
TimerHandle_t timer2Handle = NULL;

// Timer1 callback
void timer1Callback(TimerHandle_t xTimer) {
    ESP_LOGI("Timer1", "Timer 1 callback called");
}

// Timer2 callback
void timer2Callback(TimerHandle_t xTimer) {
    ESP_LOGI("Timer2", "Timer 2 callback called");
}

void app_main() {
    // Create Timer 1 with a period of 1000 ms (1 second)
    timer1Handle = xTimerCreate("Timer1", pdMS_TO_TICKS(1000), pdTRUE, (void *)0, timer1Callback);
    if (timer1Handle != NULL) {
        xTimerStart(timer1Handle, 0); // Start the timer
    }

    // Create Timer 2 with a period of 2000 ms (2 seconds)
    timer2Handle = xTimerCreate("Timer2", pdMS_TO_TICKS(2000), pdTRUE, (void *)0, timer2Callback);
    if (timer2Handle != NULL) {
        xTimerStart(timer2Handle, 0); // Start the timer
    }

    // Main program does not need further actions, can delete itself
    vTaskDelete(NULL);
}
This code creates two software timers, Timer1 and Timer2. These timers fire once every second, call Timer1Callback and Timer2Callback respectively and print messages to the serial port. This is a basic software timer application example that can help developers understand and use the software timer function of FreeRTOS.

Compile, Flash & Monitor

Find the "ESP32-IDF: Build, Flash and Monitor" ICON in VSCode to execute and burn.

Results

We can view the printing results of ESP_LOGI on the terminal, as follows...
I (2301) Timer1: Timer 1 callback called
I (3301) Timer2: Timer 2 callback called
I (3301) Timer1: Timer 1 callback called
I (4301) Timer1: Timer 1 callback called
I (5301) Timer2: Timer 2 callback called
I (5301) Timer1: Timer 1 callback called
I (6301) Timer1: Timer 1 callback called
I (7301) Timer2: Timer 2 callback called
I (7301) Timer1: Timer 1 callback called
I (8301) Timer1: Timer 1 callback called
I (9301) Timer2: Timer 2 callback called
I (9301) Timer1: Timer 1 callback called

Conclusion

This example shows how to implement timing operations using FreeRTOS's software timers, which is an efficient way to manage timing events in embedded systems.