ESP32+FreeRTOS: Task Suspension and Task Resume with vTaskSuspend and vTaskResume

Опубликовано: 02 Июль 2024
на канале: Abdul Rehman 2050
305
7

In this video, we will learn how to use the vTaskSuspend and vTaskResume functions in FreeRTOS to suspend and resume tasks on an ESP32 microcontroller. We will also discuss the use of the task priority and the state of the task.
keywords::
#ESP32
#FreeRTOS
vTaskSuspend
#vTaskResume
task suspension
task resume
task priority
task state

In this video we updated the previous ESP32 + FreeRTOS based LED blinking example for Task Suspend and Task Resume. We created two tasks in this, one is the same like the previous tutorial the LED blinking task and the second is the Control task which is responsible for reading a push button press detection. The button press will suspend the LED task if it is not already suspended. But if the task is already previously suspended it will resume that Task.

Previous video link:    • FreeRTOS ESP32 Tutorial | esp32 FreeR...  

Current video code:


// Define pin numbers
#define LED_PIN 2
#define BUTTON_PIN 13

// Task handles
TaskHandle_t ledTaskHandle = NULL;
TaskHandle_t controlTaskHandle = NULL;




// LED task
void ledTask(void *pvParameters) {
int core = xPortGetCoreID();
for (;;) {
digitalWrite(LED_PIN, HIGH);
Serial.printf("LED ON - Running on core %d\n", core);
vTaskDelay(1000 / portTICK_PERIOD_MS);

digitalWrite(LED_PIN, LOW);
Serial.printf("LED OFF - Running on core %d\n", core);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}


// Control task
void controlTask(void *pvParameters) {
bool isSuspended = false;
for (;;) {
if (digitalRead(BUTTON_PIN) == LOW) {
if (isSuspended) {
vTaskResume(ledTaskHandle);
Serial.println("LED Task Resumed");
isSuspended = false;
} else {
vTaskSuspend(ledTaskHandle);
Serial.println("LED Task Suspended");
isSuspended = true;
}

}

}
}




void setup() {
// Initialize serial communication
Serial.begin(115200);

// Initialize the onboard LED pin
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);

// Initialize the button pin
pinMode(BUTTON_PIN, INPUT_PULLUP);

// Create the LED task
xTaskCreatePinnedToCore(
ledTask, // Task function
"LED Task", // Task name
2048, // Stack size
NULL, // Task parameters
1, // Task priority
&ledTaskHandle, // Task handle
0 // Core number to run the task on (0 or 1)
);

// Create the control task
xTaskCreatePinnedToCore(
controlTask, // Task function
"Control Task", // Task name
2048, // Stack size
NULL, // Task parameters
1, // Task priority
&controlTaskHandle,// Task handle
1 // Core number to run the task on (0 or 1)
);
}

void loop() {
// Empty loop since tasks are running in FreeRTOS
}

keywords: freertos,esp32,freertos esp32,esp32 tutorial,esp32 projects,freertos tutorial,esp32 freertos,freertos esp32 robot,esp32 and freertos,freertos with esp32,esp32 project,freertos arduino and esp32, freertos with esp32 and arduino,use freertos with esp32 and arduino ide,freertos arduino,rtos esp32,esp32 arduino,frertos,esp arduino and freertos