Force a context switch to a predetermined task

are there any reasons for freeRTOS not allowing (i.e. having an API) to switch to another predetermined task?

I assume it has to do with RTOS logic and principles, but no idea why exactly. Is it so the kernel ensures that all tasks are executed and/or executed in the optimal way? Or just to avoid breaking the priority logic of RTOS? Or other reasons?

Many thanks

The fundamental rule for Scheduling is the Highest Priority Ready task is ALWAYS selected to run. Trying to make some task that is NOT at the highest priority of the ready list run could be a confusion to the logic.

I suspect that a perceived need for this indicates not understanding how to use priorities, or not having assigned those priorities right. If you want a task that isn’t the highest priority to run next, maybe it should have had a higher priority, or maybe the task that would run it is stead has too high of a priority, or a task has mixed priority operations.

A task that has a few things that need to be done quickly at high priority, then more steps that can be done at a lower priority, and will NEVER need to go back to the higher priority operation until ALL the low priority stuff is done, would be one of the few reasons to have a task that dynamically changes its priority, dropping when it gets to the low priority operations, and then raising again when it finishes them and gets ready to wait for the next High Priority request.


A little information here FreeRTOS single-core AMP and SMP RTOS task scheduling - FreeRTOS - more in the book.

1 Like

Of course, Richards is, as usual, absolutely right and to the point. Having said that and just for the fun of it and not implying that is typically done. You could make the other task you would like to switch to the highest priority task e.g. with vTaskPrioritySet()[1].

[1] This page describes the RTOS vTaskPrioritySet() FreeRTOS API function which is part of the RTOS task control API. FreeRTOS is a professional grade, small footprint, open source RTOS for microcontrollers.

Thank you all as always! :slight_smile: