Understanding FreeRTOS Task Scheduling and Inter‑Task Communication
This article explains how FreeRTOS treats tasks as the smallest scheduling unit, details the three phases of task scheduling, describes task states and transitions, and reviews the built‑in communication mechanisms such as semaphores, queues, notifications, and event groups.
FreeRTOS Tasks Overview
In RTOS, the smallest schedulable unit is called a task, represented by a Task Control Block (TCB). FreeRTOS implements this model, providing lightweight task management.
Task Creation and Scheduler Startup
Tasks can be created dynamically with xTaskCreate or statically with xTaskCreateStatic. The creation sequence is:
Allocate stack memory.
Allocate a TCB.
Initialize the allocated memory.
Call prvInitialiseNewTask to set up the task context.
Insert the task into the ready list via prvAddNewTaskToReadyList.
After at least one task is created, vTaskStartScheduler is called. It performs:
Creation of the idle task (and, if enabled, the timer service task).
Disabling interrupts.
Marking the scheduler as running.
Calling xPortStartScheduler, which performs hardware‑specific initialization such as configuring the tick timer, enabling the FPU, and setting up the PendSV interrupt on Cortex‑M cores.
Task States and Transitions
A task can be in one of four states: Running, Ready, Blocked, or Suspended. Typical transitions are:
Running → Blocked : call vTaskDelay, ulTaskNotifyTake, or xSemaphoreTake.
Running → Suspended : call vTaskSuspend on itself.
Running → Ready : pre‑empted by a higher‑priority task or by time‑slice expiration.
Blocked → Ready : the awaited resource becomes available or a timeout expires.
Suspended → Ready : vTaskResume is invoked.
Ready → Running : the scheduler selects the task for execution.
Scheduling Algorithms
FreeRTOS supports two arbitration mechanisms:
Priority‑preemptive scheduling : vTaskSwitchContext calls taskSELECT_HIGHEST_PRIORITY_TASK() to choose the highest‑priority ready task.
Round‑robin time slicing : When configUSE_TIME_SLICING is enabled, the tick ISR calls xTaskIncrementTick. If a task exhausts its time slice, portYIELD forces a context switch to the next ready task of equal priority.
Task Deletion
Any task that is in Ready, Blocked, or Suspended state may call vTaskDelete to terminate itself. The idle task reclaims the task’s stack and TCB memory.
Inter‑Task Communication Primitives
FreeRTOS provides several mechanisms for data exchange and synchronization:
Message queues : store variable‑length messages; created with xQueueCreate and accessed via xQueueSend / xQueueReceive.
Semaphores : binary, counting, mutex, and recursive mutex types; used for resource protection and task synchronization.
Task notifications : a 32‑bit value associated with a single task; faster and more memory‑efficient than binary semaphores (up to ~45 % faster according to the vendor).
Event groups : allow a task to wait for a combination of bits, useful for synchronizing multiple events or tasks.
Typical Usage Example
The following pseudo‑code demonstrates creating two tasks, using a binary semaphore for synchronization, and sending a notification from an ISR:
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
SemaphoreHandle_t xBinarySem;
void vProducerTask(void *pvParameters)
{
for(;;)
{
// Produce data …
xSemaphoreGive(xBinarySem); // Wake consumer
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void vConsumerTask(void *pvParameters)
{
for(;;)
{
if (xSemaphoreTake(xBinarySem, portMAX_DELAY) == pdTRUE)
{
// Process data
}
}
}
/* ISR that notifies a task */
void vTimerISR(void)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(xConsumerTaskHandle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}References
FreeRTOS official documentation: https://www.freertos.org/features.html
FreeRTOS source code repository: https://www.freertos.org/a00104.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
OPPO Kernel Craftsman
Sharing Linux kernel-related cutting-edge technology, technical articles, technical news, and curated tutorials
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
