2

I was working with ESP-IDF and I want to run two task at the same time of different cores of ESP32, So I used xTaskCreatePinnedToCore(sensorsTask, "Sensor" , 5000, 0, 0, &sensorTaskHandler,0); xTaskCreatePinnedToCore(storageTask, "Storage", 5000, 0, 0, &storageTaskHandler,1); and the corrosponding tasks are:

`void storageTask(){
for(;;){
    gpio_set_level(4, 1);
    gpio_set_level(2, 0);
}
}


void sensorsTask(){
for(;;){
    gpio_set_level(2, 1);
    gpio_set_level(4, 0);
}
}`

if both tasks are running really parallel then both LEDs should be ON all the time, but in real Both LEDs were flickering, which mean that both task are running one by one. i-e "SensorTask" runs on core 0 first **THEN **"storageTask" runs on core 1. Now if we add vTaskDelay(1) in both tasks it works fine, but the question is if both tasks are running simultaneously and independent of each other then why LEDs were flickering, and what is the need of adding delay. keeping the thing in mind that this delay actually help to switch b/w tasks, but in this case we have two cores and two task so no switching is really required.

I am trying to run two tasks on two cores of ESP-32 at the same time but its feels that they are running one by one as in single core microcontrollers

  • What's the difference between "blinking" and "flickering"? Which exact MCU are you using? I see no real reason to believe the two tasks are running in some kind of coordinated way. What is the expected result? – pmacfarlane May 28 '23 at 22:54
  • I am usinf ESP WROOM-32, as you can see there no code that make LED's Blink, but still they flicker showing that tasks are switching and not running simultaneously as in single core MCU. BUT we have two cores that are independent of each other and both cores can run their respective tasks all the time and dose not require any switching. – Muhammad Hammad Umer May 29 '23 at 06:39

2 Answers2

2

You have created 2 tasks with priority 0 which is the lowest priority in the system. ESP IDF implicitly creates a several other tasks (many with higher priorities) and your tasks have to share CPU cores with those.

Firstly there are 2 idle tasks (one per core) with priority 0. Since the priorities of your tasks and the idle tasks are equal, they share core time equally (50/50). So the scheduler rotates them on each core every 10 ms by default, which is quite likely the source of your flickering.

Then there are other, higher priority tasks which ESP IDF creates. If you enabled WiFi then its driver runs in a high priority (23 by default) task. The High Resolution Timer service creates its own high priority (22) task. If you've enabled TCP/IP networking, this has runs in a high priority task; etc. Granted, those tasks don't take up much core time if you don't use their services. But they take up some.

So, in short you don't get exclusive use of a CPU core. You can cut out the idle tasks by raising your priority (by the way, the Task Watchdog Timer will complain and reset the system soon if you don't let the idle task run at all; unless you disable it). You can also cut out all other tasks by raising your priority to the maximum, but in any real embedded system those other tasks do something useful ;)

Tarmo
  • 3,728
  • 1
  • 8
  • 25
0

Might be to do with the fact that ESP32 is SMP.

https://www.freertos.org/single-core-amp-smp-rtos-scheduling.html

Not 100% sure but the Freertos task scheduler needs to run somewhere, it will only run on 1 core, subsequently stopping both tasks as it figures out what to do. So it will have to pause one of the task on a core. Without the delay then the scheduler may struggle.

This is a bit of guess. Might point you in the right direction.