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?
9 Replies
shardul17
shardul177mo ago
I am not sure about whether esp cores are SMP or AMP. If cores are asymmetric multiprocessing then one core will act as primary core. In that case it will need some time for system level functioning. In above example, The CPU is not getting the time for housekeeping and only one task is monopolizing the CPU. It is based on assumption that cores are AMP. Please correct if i am wrong.
Marvee Amasi
Marvee Amasi7mo ago
@shardul17 I think they are SMP configured
attachment 0
shic
shic7mo ago
Why is the expected behaviour of LED to be constantly on?
shardul17
shardul177mo ago
Exactly... I also didn't get that...
shic
shic7mo ago
Whenever using shared resources you should use mutexes to avoid collision, and more importantly there must be a way(Like notifications) to synchronize the task operation to make the behaviour predictable.
Priyanka Singh
Priyanka Singh7mo ago
Are you expecting one Led to be on with each thread? The implementation seems different in terms of your expectations
Marvee Amasi
Marvee Amasi7mo ago
@shic , that is what I want. I want both LEDs to be constantly on , except I am doing it the wrong way from my code. I am open to corrections
shic
shic7mo ago
I think there's a misunderstanding in what you're looking for and what you have asked. If you want the two LED to be constantly on, don't turn them off. If you want them blinking concurrently in separate cores - only blink one LED per task.
Marvee Amasi
Marvee Amasi7mo ago
Yh I shouldn't turn them off. I actually programmed it to blink .
Want results from more Discord servers?
Add your server