I read the guide, but it seems to me that I don’t quite understand how it works…
When the timer is enabled, a task is created similar to a normal task, and there is also a queue that associates the timer task and the timer, right?
if a task (for example task 1) and a timer task have the same priority and when task 1 is running, but the time has come for the timer to work, what will happen? task number 1 will stop, the timer callback will occur, and then return to task 1 or until the scheduler reaches the timer task in turn?
The Timer Service Task is not “special” to the scheduler, so it schedules just like any other task. If you have pre-emption and round-robin scheduling enabled, tasks of a given priority will rotate at every tick (unless the running task block sooner). So, if the timer task is at the same priority of other tasks, it will wait its turn at that priority to run.
Normally, you configure the Timer Task to be fairly high priority, above most normal tasks. (You may have tasks with very tight time-lines that you put above it), as any task whos running holds off the timer task affects all timers that are running.
So, if task 1 is running and for example: in the middle of its execution, the time of a timer with a higher priority has come, the task will not be interrupted, it will finish executing the one ms allocated to it and the next task will be a timer task, and only then other tasks with a lower priority?
“Timers” do not have priority. Tasks (and interrupts) have priority. If you make the Timer Task higher in priority than Task1, then when a timer expires, the Timer task will be switched to, run the timer and then it will block for the next event, and other tasks can run.
Note, timers only expire on ticks, as they are measured in units of ticks.
I meant, a timer task can’t interrupt another task while it’s running, like external interrupts?
I did an experiment:
I created three tasks and set the same priority, loaded them a little with work, doing something and going to sleep for 10ms.
I created two timers with an interval of 100ms for both, gave them the same priority as other tasks and noticed that the time interval was floating, and when I set the priority of the timer task higher than that of other tasks, the time interval became correct.
So, thank you for mentioning that you need to set the priority of the timer task higher than the priority of other tasks, if this task is not critical to be completed immediately.
The timer task CAN interrupt another task due to the scheduler being run by the tick interrupt. Interrupt break into program execution, and can, by triggering the scheduler, return to a different task (and the original task will be resumed at some later point in time)
Is there a difference in priority, I mean, if I have three tasks, two of which have priority 0, and the third 1, that is, if I set priority 2 or 3, and not 1, then this will have a different effect or is everything above > 0 considered the same?
Be aware that priority 0 is the idle task priority. Usually application task priorities start at 1.
In FreeRTOS the highest priority task runs. Tasks with equal priority get scheduled by time slices.
For further details I’d recommend the docs here Tasks and Co-routines [Getting Started] - FreeRTOS and also the book Free RTOS Book and Reference Manual
I will use tasks at priority level 0, and generally place any task that might not block for an extended period of time at that priority level.
One thing to note, “Timers” do not have priority, the Timer Task does, and that established an execution priority for all timer callbacks and most timer operations (since they happen in the timer task, sent via the TimerQueue)
I’m also using idle prio tasks for some background activities not relying on dedicated scheduling or timing. But that’s probably not a common use case…