Reputation: 1
If higher priority task is waiting for an event and lower priority task is running. Before tick interrupt higher priority task gets ready then how context switch will happen before tick interrupt? Till tick interrupt higher priority task will be waiting for tick interrupt to context switch?
I need to know that in freertos context switch happen only at tick interrupt? If it is not then how higher priority task preempt the lower priority task before tick interrupt, if it get ready before tick interrupt?
Upvotes: 0
Views: 1208
Reputation: 3282
To clarify your question and provide a more complete anwser, let's name things first. You mentioned you have two tasks - let's call them HighPriorityTask
and LowPriorityTask
. In such scenario, LowPriorityTask
will only ever run if HighPriorityTask
is blocked, which happens when either of the following is done:
HighPriorityTask
calls vTaskDelay
function or similar that explicitly stops its execution for given amount of time or forever e.g. with vTaskSuspend
,HighPriorityTask
calls xQueueReceive
or similar with non-zero blocking time and there's no elements in the queue,HighPriorityTask
calls xQueueSend
function or similar with non-zero blocking time and there's no free space left in the queue.If neither of the above or similar cases happen that cause HighPriorityTask
to be blocked, then HighPriorityTask
will continue to run forever. That includes task preemption (which I assume you refer to as "tick interrupt") which will not cause LowPriorityTask
to run because both tasks are not the same priority and only tasks with same priority are given their time slices with preemption. This even includes calling taskYIELD from HighPriorityTask
which explicitly causes FreeRTOS to perform a context switch, but in this case all it's going to do is to go back to executing HighPriorityTask
code, as this is still the highest non-blocked task currently executing.
Therefore to summarize - if you have two different priority tasks in a "running" state, then the lower priority task will never run (task preemption won't assign any time to lower priority task) unless higher priority task explicitly blocks.
Upvotes: 1
Reputation: 3246
If configUSE_PREEMPTION is set to 1 in FreeRTOSConfig.h then FreeRTOS will always run the highest priority task that is able to run even if a task becomes available to run in between tick interrupts. This happens because sending an event that unblocks a task requires you to call a FreeRTOS API function, and the context switch happens in the API function. I would recommend reading the free book to learn more about the scheduling policy.
(repost of response already provided here https://forums.freertos.org/t/freertos-context-switch/8333)
Upvotes: 1