Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Tasks in FreeRTOS

In FreeRTOS, a task is a basic unit of execution. Each task is essentially a function that runs independently, allowing for multitasking within an embedded system. Tasks enable you to perform multiple operations simultaneously, which is particularly useful in applications that require real-time responsiveness or need to manage multiple processes.

Key Concepts of Tasks

  1. Task Creation: You can create a task using the xTaskCreate() function, which allows you to define the task function, stack size, priority, and other parameters.
  2. Task Deletion: A task can be deleted using the vTaskDelete() function. This frees up the resources associated with the task.
  3. Task States: Tasks can be in one of several states:
    • Running: The task is currently executing.
    • Ready: The task is ready to run and waiting for CPU time.
    • Blocked: The task is waiting for an event, such as a semaphore or queue.
    • Suspended: The task is not eligible for execution until it is resumed.
    • Deleted: The task has been removed and is no longer valid.
  4. Priority Levels: Each task in FreeRTOS can have a priority level (0 being the lowest). Higher priority tasks can preempt lower priority ones, ensuring that critical tasks are executed promptly.

Example: Task Creation, Deletion, and Priority Levels

In this example, we’ll create two tasks that blink an LED at different rates and demonstrate how to delete a task.

Step 1: Set Up the Environment

Ensure you have ESP-IDF installed and your environment set up:

. $HOME/esp/esp-idf/export.sh

Step 2: Create a New Project

Create a new folder for your project and initialize it.

Step 3: Write the Code

Here’s a simple FreeRTOS application with two tasks:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

#define LED_PIN_1 GPIO_NUM_2 // First LED pin
#define LED_PIN_2 GPIO_NUM_4 // Second LED pin

// Task to blink LED 1
void blink_task_1(void *pvParameter) {
    gpio_pad_select_gpio(LED_PIN_1);
    gpio_set_direction(LED_PIN_1, GPIO_MODE_OUTPUT);

    while (1) {
        gpio_set_level(LED_PIN_1, 1); // Turn LED 1 on
        vTaskDelay(500 / portTICK_PERIOD_MS); // Delay for 500 ms
        gpio_set_level(LED_PIN_1, 0); // Turn LED 1 off
        vTaskDelay(500 / portTICK_PERIOD_MS); // Delay for 500 ms
    }
}

// Task to blink LED 2
void blink_task_2(void *pvParameter) {
    gpio_pad_select_gpio(LED_PIN_2);
    gpio_set_direction(LED_PIN_2, GPIO_MODE_OUTPUT);

    while (1) {
        gpio_set_level(LED_PIN_2, 1); // Turn LED 2 on
        vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for 1 second
        gpio_set_level(LED_PIN_2, 0); // Turn LED 2 off
        vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for 1 second
    }
}

// Main application entry point
void app_main(void) {
    // Create the first task (higher priority)
    xTaskCreate(blink_task_1, "Blink Task 1", 2048, NULL, 2, NULL);
    
    // Create the second task (lower priority)
    xTaskCreate(blink_task_2, "Blink Task 2", 2048, NULL, 1, NULL);

    // Optionally, delete a task (e.g., after 10 seconds)
    vTaskDelay(10000 / portTICK_PERIOD_MS);
    vTaskDelete(NULL); // This will delete the task that calls it (in this case, app_main)
}

Explanation of the Code

  1. LED Pin Definitions: Two LED pins are defined (GPIO 2 and GPIO 4).
  2. Task Functions:
    • blink_task_1: This task blinks the first LED (GPIO 2) on and off every 500 ms.
    • blink_task_2: This task blinks the second LED (GPIO 4) on and off every 1000 ms.
  3. Task Creation:
    • In app_main(), two tasks are created using xTaskCreate(). The first task has a higher priority (2) than the second task (1). This means that blink_task_1 will preempt blink_task_2 if both are ready to run.
  4. Task Deletion: After a delay of 10 seconds, vTaskDelete(NULL) is called. This will delete the task that called it, which in this case is app_main. As a result, the main task exits, but the LED tasks will continue to run until they are explicitly deleted (if you want to add that functionality later).

Step 4: Build and Flash the Project

  1. Build the project:
idf.py build

2. Flash the project to the ESP32:

idf.py -p /dev/ttyUSB0 flash

After flashing, the first LED (GPIO 2) will blink every half second, while the second LED (GPIO 4) will blink every second. The application will run for 10 seconds before the main task (app_main) exits, but both blinking tasks will continue executing since they were created before the deletion.

Summary

In this example, we demonstrated how to create and delete tasks in FreeRTOS, manage their states, and set priority levels. Understanding tasks is crucial for effective multitasking in embedded systems, as it allows you to manage multiple operations concurrently and respond to events in real-time. FreeRTOS provides a robust framework for building responsive applications on ESP32, enhancing the capabilities of your projects.

    Leave a Reply

    Your email address will not be published.

    Need Help?