vTaskDelay with long wait times
`FreeRtos::delay_us` will block for less than the given time for fractional milliseconds
c - ESP IDF: Task never leaves vTaskDelay() - Stack Overflow
delay() function doesn't work for less than 10ms
Hello,
I am working on a project in IDF with FreeRTOS. Among all the tasks, the project has one task that needs to run every day at midnight (00:00 UTC). To do this, I have an external RTC with which I calculate how many seconds are left until midnight (I have verified that the calculation is correct). From there, I convert it to milliseconds and calculate the number of ticks to wait using pdMS_TO_TICKS(), passing this tick number to vTaskDelay(). However, the task runs much earlier. Does anyone know why this might be? The Tick rate (Hz) is 1000.
On the other hand, I have verified that when I convert X milliseconds to ticks using pdMS_TO_TICKS() and then calculate how many milliseconds those ticks are with pdTICKS_TO_MS(), the milliseconds value does not match the initial one. Does anyone know why this might be? Here's an example:
43200 seconds to milliseconds: 43200000 43200 seconds to ticks: 250327 43200000 milliseconds to ticks: 250327 250327 ticks to milliseconds: 250327
Thank you very much in advance.
Hello,
I am currently working on a project that uses FreeRTOS to create a bunch of tasks to do things. The code was written be a former student and I have to implement some stuff and make it run a bit smoother but I can't really understand the FreeRTOS part especially when it comes to Delays and TaskDelays or someone also told me about yields.
As an example I have 2 Tasks:
xTaskCreatePinnedToCore(
Task1code, /* Function to implement the task */
"Pubblish", /* Name of the task */
8192, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
&Task1, /* Task handle. */
1);
delay(100);
xTaskCreatePinnedToCore(
Task2Code, /* Function to implement the task */
"Sensors", /* Name of the task */
4096, /* Stack size in words */
NULL, /* Task input parameter */
2, /* Priority of the task */
&Task2, /* Task handle. */
1);and the following functions :
void Task1code( void * parameter) {
for (;;) {
if (client.connected()) {
String temp = getData();
vTaskDelay(800);
if (!client.publish("topic", temp ())) {
log_e("Couldnt publish to 'Topic' ");
vTaskDelay(200);
}
}
delay(3000);
}
}
void Task2code( void * parameter) {
for (;;){
getTempValue(); // reads some DS81B0 temperaturesensors over 1-wire
delay(750);
}
}I am kind of unsure what vTaskDelay does in the first code as I cant really understand this page here https://www.freertos.org/a00127.html - Also, can I use normal delays in a extra task or is it bad ?
I would love if someone could explains me the vTaskDelay a bit more noob-friendly and maybe has some ideas on how to make that code run better ?
Edit:
Also I question would be is how to use vTaskDelayUntil()
Thanks !
vTaskDelay is basically the same as Arduino delay() But if I remember correctly you have to divide it by the ticks per millisecond See the ESP documentation you can search for vTaskDelay and you'll see a number of results
vTaskDelay just causes the task to pause at that point for the specified duration. You generally use it in loops like you're doing here, or between statements that need a defined delay between them, such as when driving pins high or low for a communication protocol.
The real question is, what is this delay() function you're calling? Where is that defined? It seems like it probably does the same thing, but there's no such function in the FreeRTOS API AFAIK.
The problem is no to pass control back to FreeRTOS but the handling of the watchdog in the eps-idf framework.
The watchdog is "fed" in the IDLE task and while the APP_MAIN task has a higher priority than IDLE it is never interrupted.
If you don't want to use vTaskDelay maybe you could make the priority of the IDLE and the MAIN task equal. Than both should be executed (but you must be careful if there any even more task that do not get enough attention).
I don't recommend doing this for anything but testing, but you can simply disable the task watchdog on the IDLE tasks.
menuconfig->Component config->Esp32-specific config -> deactivate Task watchdog
For production code you should follow best-practice and use the FreeRTOS functions such as vTaskDelay so you don't hog the CPU.