FreeRTOS questions

Gilbert
Posts: 43
Joined: Wed Sep 16, 2020 2:58 pm

FreeRTOS questions

Postby Gilbert » Thu Mar 12, 2026 1:51 pm

I'm not familiar at all with FreeRTOS, please excuse if I'm asking silly questions. Arduino IDE 2.3.8.
I'm using FreeRTOS Xtimer to read sensor data once per second. When I look around on the Web, I see that I should include "FreeRTOS.h" in my code. The example I'm following (Random Nerd Tuto's) doesn't include any FreeRTOS header files. Code without including FreeRTOS header compiles without errors. So, what about the need (or not) to include FreeRTOS header files?
Question #2: I read that the xTime callback does not run on ISR level (unfortunately many callbacks from other Arduino libs do...) which tells me that I don't have to adhere to ISR 'rules' (i.e. get out of there as quickly as you can) and I can make my callback task as long and complicated as needed, as long as I'm not using any blocking code. Right?

Just to satisfy my curiosity: if that task would take longer than the timer period and would still be running when the next timer callback occurs (I know, this is extremely unlikely unless my task executes a never-ending loop...) what happens? Will the new callback interrupt the one which is still running or will it wait untill the running callback task returns? Speaking of which... how does the callback become active? Is there a hidden mechanism which gives control to FreeRTOS from time to time?

lbernstone
Posts: 1132
Joined: Mon Jul 22, 2019 3:20 pm

Re: FreeRTOS questions

Postby lbernstone » Thu Mar 12, 2026 4:00 pm

1) Arduino.h imports FreeRTOS.h, so you don't need to. It doesn't hurt anything, and makes your dependencies more clear if you have it in your code.
2) xTimer manages a list of your tasks, and should serialize them.
Note that there are also hardware timers, and you can use them with hw_timer API. These are much more accurate, will be interrupt based, and smash each other if they overlap.
Arduino provides the Ticker library to simplify setting up timers.
If you have scheduled routines that have drop dead times, it is best to set up a queue/mutex with an appropriate TicksToWait to ensure they don't just stack up and consume your memory.

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

Re: FreeRTOS questions

Postby MicroController » Thu Mar 12, 2026 11:27 pm

Question #2: I read that the xTime callback does not run on ISR level (unfortunately many callbacks from other Arduino libs do...) which tells me that I don't have to adhere to ISR 'rules' .... Right?
Yes, all FreeRTOS timer callbacks happen inside a dedicated "timer" task, so ISR restrictions don't apply to the callback code. Since that timer task is a shared resource across the system, you still don't want excessively long-running callbacks; while one callback is running, no other timer events/callbacks are processed, i.e. one 'misbehaving' timer callback can delay all other timers - even ones which you may not even know existed because they're used internally by some 3rd-party library.

Gilbert
Posts: 43
Joined: Wed Sep 16, 2020 2:58 pm

Re: FreeRTOS questions

Postby Gilbert » Sat Mar 14, 2026 2:28 pm

Thanks for the clear explanations.

Who is online

Users browsing this forum: Qwantbot, Semrush [Bot] and 2 guests