Task scheduling, how does it work in this case?

thomasx
Posts: 8
Joined: Sun Mar 17, 2024 10:07 pm

Task scheduling, how does it work in this case?

Postby thomasx » Fri May 17, 2024 1:55 pm

Hi

I have inherited some code using Task scheduling that I am not very familiar with and here's a section of code I can't quite wrap my head around.
The code is running on an ESP32-WROOM, DevKitC V4, and I am using the Arduino IDE.

There's a lot of code in several files, so I can't include it all here. But here is an excerpt that I hope is sufficient to help me understand how the scheduling works.

In essence I am trying to understand how hwLoop works.
It seems it is only started once, enters an endless loop, and then somehow is allowed to continue running every now and then by itself, or what is going on? When is it called, how often is it called, does it start more than once, is it interrupted in its running and comes back to where it left off? What is the point of using task scheduling in the case of hwloop?

Please help me understand about this.
#include <TaskScheduler.h>

TaskHandle_t hwLoopTh;
Scheduler hwScheduler;

volatile int16_t rpm=0;
void IRAM_ATTR interruptRpm() {
rpm++;
if(rpm>2000) rpm = 0;
}

setup() {
xTaskCreateUniversal(hwLoop, "HW", 16384, NULL, 1, &hwLoopTh, 0);
}

loop()
{
delay(1000);
}

void hwLoop(void *param) {

bool on = true;
hw_timer_t *timer = NULL;
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &interruptRpm, false);
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);

for (;;) {
// some code for calculations etc
if (on) {
hwScheduler.execute();
}
delay(1000);
}
}
Last edited by thomasx on Fri May 17, 2024 8:12 pm, edited 1 time in total.

ESP_Sprite
Posts: 9775
Joined: Thu Nov 26, 2015 4:08 am

Re: Task scheduling, how does it work in this case?

Postby ESP_Sprite » Fri May 17, 2024 2:15 pm

Can't tell from this code. xTaskCreateUniversal and the Scheduler object aren't part of ESP-IDF, and those seem to play a crucial role here.

andyol
Posts: 1
Joined: Fri May 17, 2024 3:01 pm

Re: Task scheduling, how does it work in this case?

Postby andyol » Fri May 17, 2024 3:23 pm

The scheduling of tasks is part of the FreeRTOS that the ESP-IDF is based on, but with some differences.

Check out the learning material on FreeRTOS on their website, and how it is implemented by ESP32 here:

https://docs.espressif.com/projects/esp ... ertos.html

In short, with ESP version, the scheduler doesn't need to be started, this is already implemented in the OS.
Once a task has been created the scheduler will start scheduling it.

The function you're asking about enters once and never exits, it keeps running for ever and the scheduler will swap it in and out between other tasks that I assume are also in the code somewhere. If this is the one and only task, then the code could just as well have been inside the main loop.

It's a fundamental in task scheduling that tasks have endless loops and never exits. The scheduler will run them one by one based on priorities.

ESP_Sprite
Posts: 9775
Joined: Thu Nov 26, 2015 4:08 am

Re: Task scheduling, how does it work in this case?

Postby ESP_Sprite » Sat May 18, 2024 1:17 am

andyol wrote:
Fri May 17, 2024 3:23 pm
The scheduling of tasks is part of the FreeRTOS that the ESP-IDF is based on, but with some differences.
Yes, but that is not what his code is doing here.

MicroController
Posts: 1760
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Task scheduling, how does it work in this case?

Postby MicroController » Sat May 18, 2024 9:13 am

When is it called,
Being run in a newly created task, it is called by the OS's scheduler as soon as possible considering all other tasks and their priorities.
how often is it called, does it start more than once,
Unlike loop() it is called exactly once, when the task created via xTaskCreate... starts.
is it interrupted in its running and comes back to where it left off?
Yes, that's exactly what FreeRTOS, or multi-tasking in general, does. At the latest, control is handed back to FreeRTOS by the delay(1000) in hwLoop so that during the wait time other tasks can run.
What is the point of using task scheduling in the case of hwloop?
The Scheduler object is unrelated to the OS tasks, in that it supports a form of 'cooperative' multi-tasking. To do that, its execute() function needs to be called repeatedly, which the hwLoop() does. Using this Scheduler while you also have FreeRTOS tasks and timers at your disposal may or may not make sense.

thomasx
Posts: 8
Joined: Sun Mar 17, 2024 10:07 pm

Re: Task scheduling, how does it work in this case?

Postby thomasx » Sat May 18, 2024 9:04 pm

Thanks for the info MicroController and Andyol!

Who is online

Users browsing this forum: No registered users and 102 guests