0
0
FreeRTOSprogramming~10 mins

vTaskPrioritySet() dynamic priority in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - vTaskPrioritySet() dynamic priority
Start with Task at Priority P
Call vTaskPrioritySet(Task, NewPriority)
Check if NewPriority is valid
Update Task Priority
Scheduler adjusts task order
Task runs with new priority
This flow shows how calling vTaskPrioritySet changes a task's priority dynamically, then the scheduler updates task order accordingly.
Execution Sample
FreeRTOS
vTaskPrioritySet(xTaskHandle, 3);
// Task priority changes from 1 to 3
// Scheduler reorders tasks based on new priority
This code changes a task's priority from 1 to 3 dynamically at runtime.
Execution Table
StepActionCurrent PriorityNew PriorityScheduler ReactionResult
1Task created1-Task added to ready list at priority 1Task priority = 1
2Call vTaskPrioritySet(xTaskHandle, 3)13Scheduler reorders tasksTask priority updated to 3
3Scheduler runs3-Task moved to higher priority queueTask runs sooner
4Call vTaskPrioritySet(xTaskHandle, 0)30Scheduler reorders tasksTask priority updated to 0 (lowest)
5Scheduler runs0-Task moved to lowest priority queueTask runs later
6Call vTaskPrioritySet(xTaskHandle, 6)06Scheduler reorders tasksTask priority updated to 6 (highest)
7Scheduler runs6-Task moved to highest priority queueTask runs immediately
8Call vTaskPrioritySet(xTaskHandle, 10)610Invalid priority (above max)Priority unchanged, error handled
9End---Execution stops
💡 Execution stops after invalid priority attempt or normal completion
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
Task Priority130666
Key Moments - 3 Insights
Why doesn't the task priority change when an invalid priority is set?
Because vTaskPrioritySet checks if the new priority is within valid limits before updating. See execution_table row 8 where priority 10 is invalid, so priority stays at 6.
How does changing priority affect when the task runs?
Scheduler moves the task to the queue matching its new priority. Higher priority tasks run sooner. See rows 3, 5, and 7 for scheduler reactions.
Can a task priority be set lower than zero?
No, priorities below zero are invalid and ignored, similar to above max priority. This prevents errors and keeps scheduler stable.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the task priority after vTaskPrioritySet is called?
A3
B6
C0
D1
💡 Hint
Check the 'New Priority' and 'Result' columns at step 4 in execution_table.
At which step does the scheduler move the task to the highest priority queue?
AStep 7
BStep 5
CStep 3
DStep 2
💡 Hint
Look for 'Task moved to highest priority queue' in the Scheduler Reaction column.
If you try to set a task priority above the max allowed, what happens according to the execution_table?
APriority is set to max allowed
BPriority remains unchanged and error is handled
CPriority is set to zero
DTask is deleted
💡 Hint
See step 8 in execution_table for handling invalid priority.
Concept Snapshot
vTaskPrioritySet(TaskHandle, NewPriority)
- Changes task priority dynamically
- Scheduler reorders tasks immediately
- NewPriority must be valid (0 to configMAX_PRIORITIES-1)
- Higher priority tasks run before lower ones
- Invalid priorities are ignored
Full Transcript
This visual trace shows how vTaskPrioritySet changes a FreeRTOS task's priority at runtime. Starting with a task at priority 1, calling vTaskPrioritySet with a new priority updates the task's priority if valid. The scheduler then reorders tasks so higher priority tasks run sooner. If an invalid priority is given, the change is ignored to keep system stability. The variable tracker shows the task priority changing step-by-step. Key moments clarify why invalid priorities don't change the task and how priority affects scheduling. The quiz tests understanding of priority changes and scheduler reactions.