0
0
FreeRTOSprogramming~10 mins

Choosing priorities for real applications in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Choosing priorities for real applications
Identify tasks
Assign priorities based on importance
Check for resource conflicts
Adjust priorities to avoid starvation
Implement and test
Monitor and fine-tune
This flow shows how to pick task priorities step-by-step in a real FreeRTOS application.
Execution Sample
FreeRTOS
void TaskA(void *pvParameters) {
  for(;;) {
    // High priority task work
    vTaskDelay(100 / portTICK_PERIOD_MS);
  }
}

void TaskB(void *pvParameters) {
  for(;;) {
    // Low priority task work
    vTaskDelay(200 / portTICK_PERIOD_MS);
  }
}
Two tasks run with different priorities and delays to show priority effects.
Execution Table
StepRunning TaskPriorityActionResult
1TaskAHighStarts runningTaskA runs first
2TaskAHighCalls vTaskDelay(100 / portTICK_PERIOD_MS)TaskA blocks, scheduler runs TaskB
3TaskBLowStarts runningTaskB runs while TaskA is blocked
4TaskBLowCalls vTaskDelay(200 / portTICK_PERIOD_MS)TaskB blocks, no ready tasks
5Idle TaskLowestRunsCPU idle until TaskA delay ends
6TaskAHighDelay ends, becomes readyTaskA runs again
7TaskAHighRepeats cycleTaskA keeps priority advantage
💡 Simulation ends after one full cycle of TaskA and TaskB delays.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6Final
TaskA StateReadyBlocked (delay)BlockedBlockedRunningRunning
TaskB StateReadyRunningRunningBlocked (delay)BlockedBlocked
CPU Running TaskNoneTaskBTaskBIdleTaskATaskA
Key Moments - 3 Insights
Why does TaskA run before TaskB even though both are ready at the start?
Because TaskA has a higher priority (see execution_table step 1), FreeRTOS always runs the highest priority ready task.
What happens when TaskA calls vTaskDelay?
TaskA blocks and stops running (step 2), so the scheduler runs the next highest priority ready task, TaskB (step 3).
Why does the Idle Task run at step 5?
Both TaskA and TaskB are blocked on delay, so no user tasks are ready; the Idle Task runs to keep CPU busy.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which task is running at step 3?
ATaskB
BIdle Task
CTaskA
DNo task running
💡 Hint
Check the 'Running Task' column at step 3 in the execution_table.
At which step does TaskA become ready again after delay?
AStep 4
BStep 6
CStep 2
DStep 7
💡 Hint
Look for when TaskA state changes from blocked to ready in variable_tracker and execution_table.
If TaskB had higher priority than TaskA, what would happen at step 1?
ATaskA runs first
BIdle Task runs first
CTaskB runs first
DBoth run simultaneously
💡 Hint
Recall FreeRTOS runs the highest priority ready task (see concept_flow and execution_table step 1).
Concept Snapshot
In FreeRTOS, assign task priorities based on importance.
Higher priority tasks preempt lower ones.
Use delays or blocking to allow lower priority tasks to run.
Avoid priority inversion and starvation by adjusting priorities.
Test and monitor to fine-tune priorities for smooth operation.
Full Transcript
Choosing priorities in FreeRTOS means deciding which tasks are more important so they run first. We start by listing tasks, then assign priorities based on how critical they are. The scheduler always runs the highest priority ready task. If a high priority task blocks (like calling vTaskDelay), lower priority tasks get CPU time. The Idle Task runs only when no other tasks are ready. This example shows TaskA with high priority running first, then blocking, letting TaskB run. When both block, the Idle Task runs. Understanding this helps avoid problems like starvation and ensures smooth multitasking.