Priority numbering helps FreeRTOS decide which task should run first. It keeps your program organized and responsive.
0
0
Priority numbering in FreeRTOS
Introduction
When you want certain tasks to run before others, like handling urgent sensor data.
When you have background tasks that can wait, like logging information.
When you want to avoid tasks blocking each other by setting clear priorities.
When you need smooth multitasking in embedded systems.
When you want to control how your system reacts to different events.
Syntax
FreeRTOS
UBaseType_t uxPriority = 0; // Lowest priority uxPriority = configMAX_PRIORITIES - 1; // Highest priority
Priority numbers start at 0, which is the lowest priority.
The highest priority is one less than configMAX_PRIORITIES.
Examples
Defines the lowest and highest priority values based on FreeRTOS settings.
FreeRTOS
const UBaseType_t lowPriority = 0; const UBaseType_t highPriority = configMAX_PRIORITIES - 1;
Creates a task with priority 2, which is higher than 0 but lower than the max.
FreeRTOS
xTaskCreate(taskFunction, "Task1", 1000, NULL, 2, NULL);
Creates a task with the highest priority allowed.
FreeRTOS
xTaskCreate(taskFunction, "Task2", 1000, NULL, configMAX_PRIORITIES - 1, NULL);
Sample Program
This program creates two tasks: one with low priority and one with the highest priority. The high priority task runs more often because FreeRTOS always runs the highest priority task ready to run.
FreeRTOS
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void vTaskLowPriority(void *pvParameters) { for (;;) { printf("Low priority task running\n"); vTaskDelay(pdMS_TO_TICKS(1000)); } } void vTaskHighPriority(void *pvParameters) { for (;;) { printf("High priority task running\n"); vTaskDelay(pdMS_TO_TICKS(500)); } } int main(void) { xTaskCreate(vTaskLowPriority, "LowTask", 1000, NULL, 1, NULL); xTaskCreate(vTaskHighPriority, "HighTask", 1000, NULL, configMAX_PRIORITIES - 1, NULL); vTaskStartScheduler(); for (;;) {} return 0; }
OutputSuccess
Important Notes
Tasks with higher priority always run before lower priority tasks.
If two tasks have the same priority, FreeRTOS switches between them to share CPU time.
Setting priorities carefully helps your system stay fast and responsive.
Summary
Priority numbers start at 0 (lowest) and go up to configMAX_PRIORITIES - 1 (highest).
Higher priority tasks run before lower priority ones.
Use priority numbering to control task order and system responsiveness.