Marvee Amasi
Marvee Amasi
DIIDevHeads IoT Integration Server
Created by Marvee Amasi on 4/29/2024 in #middleware-and-os
How can I run independent tasks on separate cores using ESP-IDF on ESP32?
Hey @Middleware & OS coding gurus 👩‍💻, There is this thing I want to fully understand. I'm working with ESP-IDF on an ESP32 and trying to run two independent tasks simultaneously on separate cores. Look how I did it using
C
C
👇👇:
xTaskCreatePinnedToCore(sensorsTask, "Sensor", 5000, 0, 0, &sensorTaskHandler, 0);

xTaskCreatePinnedToCore(storageTask, "Storage", 5000, 0, 0, &storageTaskHandler, 1);

void storageTask() {
for (;;) {
gpio_set_level(4, 1);
// Turn on LED on pin 4
gpio_set_level(2, 0);
// Turn off LED on pin 2
}
}

void sensorsTask() {
for (;;) {
gpio_set_level(2, 1);
// Turn on LED on pin 2
gpio_set_level(4, 0);
// Turn off LED on pin 4
}
}
xTaskCreatePinnedToCore(sensorsTask, "Sensor", 5000, 0, 0, &sensorTaskHandler, 0);

xTaskCreatePinnedToCore(storageTask, "Storage", 5000, 0, 0, &storageTaskHandler, 1);

void storageTask() {
for (;;) {
gpio_set_level(4, 1);
// Turn on LED on pin 4
gpio_set_level(2, 0);
// Turn off LED on pin 2
}
}

void sensorsTask() {
for (;;) {
gpio_set_level(2, 1);
// Turn on LED on pin 2
gpio_set_level(4, 0);
// Turn off LED on pin 4
}
}
The expected behavior is for both LEDs to be constantly lit. However, they are flickering, I think the tasks are running one core at a time. Adding
vTaskDelay(1)
vTaskDelay(1)
in both tasks resolves the issue. But my question is: - Why are the LEDs flickering even though the tasks are assigned to separate cores and should be running concurrently? - Why is the delay necessary if both cores are supposedly operating independently? I understand that the delay allows for context switching between tasks, but shouldn't that be handled by the FreeRTOS scheduler with two cores available?
10 replies