I have inherited some code using Task scheduling running on an ESP32-WROOM, DevKitC V4, that I am not very familiar with and here's a section of code I can't quite wrap my head around. Would someone be kind and enlighten me how this works?
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 hwlopp 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?
Please help me understand this one.
#include <TaskScheduler.h>
TaskHandle_t hwLoopTh;
Scheduler hwScheduler;
int16_t rpm=0;
void IRAM_ATTR interruptRpm() {
rpm++;
if(rpm>2000) rpm = 0;
}
setup() {
xTaskCreateUniversal(hwLoop, "HW", 16384, NULL, 1, &hwLoopTh, 0);
}
loop()
{
}
void hwLoop(void *param) {
hw_timer_t *timer = NULL;
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &interruptRpm, false); //false was true, but generated a warning about no EDGE interrupts, LEVEL used instead
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
for (;;) {
// some code for calculations etc
if (on) {
hwScheduler.execute();
}
delay(1000);
}
}
From the detail "IRAM" I conclude you are talking about an ESP32 microcontroller.
Such things are very hardware-specific. So it is really important to give the information what exact type of microcontroller you are using
ESP32
ESP32-S2
ESP32-C3
ESP32-S3
all ESP's use FreeRTOS in the background for the "tasking"
you should mention your exact type of your microcontroller in the title of your thread to attract that people that know about tasking in ESP32s
Not sure where you got this code but it likely does not make sense as such
In a nutshell the code creates a task which sets up a hardware timer running at 1Mhz which increments the rpm variable (that is not used elsewhere and is not declared volatile) every second until it reaches 2000 then it's reset and an infinite loop with some placeholder code (commented out) for calculations, followed by a conditional statement that executes tasks scheduled by hwScheduler if on (which is defined nowhere) is true...
if this is all your inheritance, I suggest you toss it away and start form scratch whatever you need to do
I am sure that anyone with knowledge in task scheduling will understand my questions in the OP, and will know how to address a civil and helpful post, and how to ask for more info if needed.
And for the record - I never said the code didn't work. I asked HOW it works, implying that it does, and with specific reference to hwLoop.
Sad to see how some kind of posters always have hidden profiles.
probably referring to me. I've a hidden profile because I like my privacy (only way to disable the presence feature) and got tired of unwanted PM too easily.
I have sufficient FreeRTOS expertise to tell you that what you posted is not well-written. As @J-M-L noted, unless you post code that actually compiles, nobody can tell you much more.
@J-M-L's synopsis of what the code does is accurate, given the limited amount of information that you provided.
So you are well acquainted with task scheduling and understands the ins and outs of it, and xTaskCreateUniversal, but can't, given the OP, explain when or how often hwLoop is run, because the code presented in the OP won't compile?
As should be pretty clear from the OP, I didn't write the code, I didn't say it is well written, I didn't say it will compile. On the contrary I was clear that it is an excerpt and why it is an excerpt.
You may very well think the code is not well written, but with all due respect that's not at all what I asked about.
well given the code won't compile, the hwLoop will never run...
otherwise it's called only once and it never ends because there is a builtin infinite loop in that function introduced by
for (;;)
if the code were to compile (meaning there is another part of the code we don't see) the time allocated to that task depends on what else is happening.
You are explicitly pinning the task to core 0 which typically runs the system tasks (management of resources, memory allocation, and other low-level functions) and Wi-Fi & BT stacks — while core 1 is used for running the Arduino loop and other user-defined tasks
in the grand scheme of things, the ancillary tasks leave enough time for this infinite loop to run and given you have a delay(1000) at the end of the hwLoop, you could say it runs once per second give or take (assuming that when on is true, hwScheduler.execute() does not trigger blocking code)
Can't you understand it's hard to provide more than this?
can you clarify exactly what your question is? Do you understand what is a task in RTOS and how the OS scheduler works?
The info given in the original post is certainly enough both to answer the questions asked about task scheduling and the hwLoop, as well as to understand the knowledge level of the poster when it comes to task scheduling. Close to none, and that's perfectly alright, we've all been there. Some just seem to have forgotten
First and foremost, just like you said already in the OP, @Thomasx, hwLoop starts once, runs all the time and never stops. Once the task is created it starts running. There's nothing strange or out of the ordinary going on here.
What is "going on", is the OS task handling, where a scheduler swaps between different tasks.
Obviously, there are other tasks as well, not shown in the "code excerpt" in the OP, and the scheduler switches between these, and that includes the main loop()!
So all tasks are being run "simultaneously", though one by one. So it gives the impression of a multitasking system. Just like the main loop is running "continuously all the time", so is hwLoop. The scheduler switches between them, regardless of where in the code they are.
These are the basic fundamentals of a task scheduling OS like the FreeRTOS, that the ESP is based on.
Well, there's no such thing as a stupid question. Plenty of dito answers though, sadly.
Disclamer: The ESP32 having dual cores has not been considered in the above. Simplifications made to accommodate novice learners.