FreeRTOS helps small devices run many tasks smoothly by managing how they share the processor.
0
0
FreeRTOS architecture overview
Introduction
When you want to run multiple tasks on a small microcontroller.
When you need tasks to communicate or share data safely.
When you want to handle time-based events or delays easily.
When you want your device to respond quickly to external events.
When you want to organize your program into smaller, manageable parts.
Syntax
FreeRTOS
FreeRTOS has these main parts: - Tasks: Small programs that run independently. - Scheduler: Decides which task runs and when. - Queues: Help tasks send messages to each other. - Semaphores and Mutexes: Help tasks share resources safely. - Timers: Run code after a delay or repeatedly. You create tasks and start the scheduler to run them.
The scheduler uses priorities to decide which task runs first.
Tasks can be in states like running, ready, blocked, or suspended.
Examples
This example shows how to create a simple task and start the scheduler.
FreeRTOS
void vTaskCode( void * pvParameters ) {
for( ;; ) {
// Task code here
}
}
xTaskCreate( vTaskCode, "Task1", 1000, NULL, 1, NULL );
vTaskStartScheduler();This example shows how tasks can send and receive data using a queue.
FreeRTOS
QueueHandle_t xQueue = xQueueCreate( 10, sizeof( int ) ); // Send data to queue int value = 100; xQueueSend( xQueue, &value, 0 ); // Receive data from queue int receivedValue; xQueueReceive( xQueue, &receivedValue, portMAX_DELAY );
Sample Program
This program creates two tasks that print messages at different intervals. The scheduler runs them so they share the processor.
FreeRTOS
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void vTask1( void *pvParameters ) { for( ;; ) { printf("Task 1 is running\n"); vTaskDelay( pdMS_TO_TICKS(1000) ); } } void vTask2( void *pvParameters ) { for( ;; ) { printf("Task 2 is running\n"); vTaskDelay( pdMS_TO_TICKS(2000) ); } } int main( void ) { xTaskCreate( vTask1, "Task1", 1000, NULL, 1, NULL ); xTaskCreate( vTask2, "Task2", 1000, NULL, 1, NULL ); vTaskStartScheduler(); for( ;; ); return 0; }
OutputSuccess
Important Notes
FreeRTOS runs on many small devices like microcontrollers.
Tasks should not block forever unless intended, or the scheduler may stop running other tasks.
Use priorities wisely to avoid one task blocking others.
Summary
FreeRTOS lets small devices run many tasks by managing time and resources.
It uses tasks, queues, semaphores, and a scheduler to organize work.
Understanding its architecture helps you write better multitasking programs.