Implementing Multi-Task Using ESP32’s FreeRTOS


Introduction

Using ESP-IDF (Espressif IoT Development Framework) to write multi-task programs mainly uses the multi-task management function provided by FreeRTOS. ESP32's multi-task management is implemented based on FreeRTOS, so you can use the API provided by FreeRTOS to create and manage multiple 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.

ESP32 DevKit Development Module

Choose an ESP32 module such as ESP32 DevKitC V4.

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.

Using xTaskCreate

Use the functions provided by FreeRTOS to create a new task. Its parameters include task function pointer, task name, task stack size, task parameters, task priority and task handle.

Coding

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

// Define log tags for tasks
static const char *TAG1 = "Task1";
static const char *TAG2 = "Task2";

// Get current time
const char* get_time_string() {
    static char time_str[64];
    time_t now;
    struct tm timeinfo;
    time(&now);
    localtime_r(&now, &timeinfo);
    strftime(time_str, sizeof(time_str), "%H:%M:%S", &timeinfo);
    return time_str;
}

void task1(void *parameter)
{
    while (1)
    {
        ESP_LOGI(TAG1, "[%s] Task 1 is running", get_time_string());
        vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for 1000 ms
    }
}

void task2(void *parameter)
{
    while (1)
    {
        ESP_LOGI(TAG2, "[%s] Task 2 is running", get_time_string());
        vTaskDelay(2000 / portTICK_PERIOD_MS); // Delay for 2000 ms
    }
}

void app_main()
{
    // Create the task-1
    xTaskCreate(&task1, "Task 1", 2048, NULL, 1, NULL);
    
    // Create the task-2
    xTaskCreate(&task2, "Task 2", 2048, NULL, 1, NULL);
}

Compile, Flash & Monitor

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

Results

Executed on ESP32 using esp_log, the following log output is displayed in the serial monitor as shown below...
I (1314) Task1: [00:00:01] Task 1 is running
I (2314) Task2: [00:00:02] Task 2 is running
I (2314) Task1: [00:00:02] Task 1 is running
I (3314) Task1: [00:00:03] Task 1 is running
I (4314) Task2: [00:00:04] Task 2 is running
I (4314) Task1: [00:00:04] Task 1 is running
I (5314) Task1: [00:00:05] Task 1 is running
I (6314) Task2: [00:00:06] Task 2 is running
I (6314) Task1: [00:00:06] Task 1 is running
I (7314) Task1: [00:00:07] Task 1 is running
I (8314) Task2: [00:00:08] Task 2 is running
I (8314) Task1: [00:00:08] Task 1 is running
I (9314) Task1: [00:00:09] Task 1 is running
In the FreeRTOS system, task1 and task2 run asynchronously, which means that their execution does not occur synchronously. This is because FreeRTOS uses round-robin scheduling (Round-Robin Scheduling) or other scheduling strategies to manage the running of tasks, and the output of the above log can indicate asynchronous running.

Conclusion

Through the above steps and methods, you can use VSCode to develop ESP32 applications and implement multitasking with FreeRTOS. If there are any errors, please check the configuration of ESP-IDF, VSCode, etc. to make sure that the paths and environment variables are set correctly.