Task priority assignment helps FreeRTOS decide which task should run first. It makes sure important tasks get CPU time before less important ones.
0
0
Task priority assignment in FreeRTOS
Introduction
When you have multiple tasks and want some to run before others.
When a task needs to respond quickly, like reading a sensor.
When you want background tasks to run only when the CPU is free.
When you want to avoid delays in critical operations.
When managing tasks that depend on each other's timing.
Syntax
FreeRTOS
xTaskCreate( TaskFunction_t pvTaskCode, const char * const pcName, configSTACK_DEPTH_TYPE usStackDepth, void *pvParameters, UBaseType_t uxPriority, TaskHandle_t *pxCreatedTask );
uxPriority is the task priority number. Higher means more important.
Priorities usually start at 0 (lowest) and go up to configMAX_PRIORITIES - 1.
Examples
This creates a task named "Task1" with priority 2.
FreeRTOS
xTaskCreate( vTask1, "Task1", 1000, NULL, 2, NULL );
Creates a sensor reading task with higher priority 5 to run quickly.
FreeRTOS
xTaskCreate( vSensorReadTask, "Sensor", 500, NULL, 5, NULL );
Creates a background task with low priority 1 to run when CPU is free.
FreeRTOS
xTaskCreate( vBackgroundTask, "Background", 800, NULL, 1, NULL );
Sample Program
This program creates two tasks: one with high priority (3) and one with low priority (1). The high priority task will run before the low priority task.
FreeRTOS
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void vTaskHighPriority(void *pvParameters) { for (;;) { printf("High priority task running\n"); vTaskDelay(pdMS_TO_TICKS(1000)); } } void vTaskLowPriority(void *pvParameters) { for (;;) { printf("Low priority task running\n"); vTaskDelay(pdMS_TO_TICKS(1000)); } } int main(void) { xTaskCreate(vTaskHighPriority, "HighTask", 1000, NULL, 3, NULL); xTaskCreate(vTaskLowPriority, "LowTask", 1000, NULL, 1, NULL); vTaskStartScheduler(); for (;;) {} return 0; }
OutputSuccess
Important Notes
Higher priority tasks can interrupt lower priority tasks.
Use priorities carefully to avoid starving low priority tasks.
FreeRTOS priorities are integers; 0 is the lowest priority.
Summary
Task priority controls which task runs first in FreeRTOS.
Assign higher numbers to more important tasks.
Proper priority assignment helps your program run smoothly and responsively.