
II. Overview of FreeRTOS
2.1 Development History
2.2 Core Features
- Task Scheduling Flexibility
: Provides multiple scheduling strategies, including preemptive and round-robin, allowing for fine-tuning of task priority according to application needs, ensuring real-time response for critical tasks and reasonable CPU time allocation for non-critical tasks. - Rich Synchronization and Communication Mechanisms
: Such as semaphores, mutexes, message queues, event groups, etc., facilitating coordination of data sharing, resource mutual exclusion access, and asynchronous event handling between tasks, effectively avoiding data conflicts and deadlocks. - Low Resource Consumption
: The kernel code is streamlined, with low memory requirements, allowing it to run stably and efficiently in resource-constrained microcontroller environments, minimizing the impact of system overhead on application functionality. - Ease of Portability
: A highly abstracted hardware interface layer allows for rapid portability to different architecture chips with minimal adaptation code, covering mainstream series such as ARM, AVR, and PIC, greatly expanding the application range.
III. Analysis of FreeRTOS Architecture
3.1 Kernel Layer
- Task Scheduler
: As the core of the kernel, it is responsible for selecting ready tasks for execution based on the established scheduling strategy, maintaining task state transitions (ready, running, blocked, suspended, etc.), and real-time updating the state information in the Task Control Block (TCB) to ensure efficient system flow. - Task Management Module
: Covers operation functions for task creation, deletion, suspension, and resumption, initializing the TCB based on input parameters when creating tasks, allocating stack space, and incorporating tasks into the scheduling system; when deleting tasks, it releases relevant resources to ensure effective memory utilization. - Interrupt Management Subsystem
: Quickly responds and saves context when hardware interrupts occur, determining whether to nest interrupts based on interrupt priority, and accurately restoring the context after handling, coordinating the execution order of interrupt service routines (ISR) and tasks to prevent interrupts from interfering with normal task flow.
3.2 Middleware Layer (Optional)
Some versions include middleware components such as TCP/IP protocol stacks, file systems, USB drivers, etc. Taking the TCP/IP protocol stack as an example, it provides socket interfaces for applications with network connection needs, implementing network data transmission and connection management, adhering to standard protocol specifications, and reducing the difficulty of embedded network programming, enabling devices to easily connect to the Internet of Things.
Please open in WeChat client
3.3 Hardware Abstraction Layer (HAL)
IV. FreeRTOS Task Management Mechanism
4.1 Task States and Transitions
- Ready State
: The task is ready and waiting for the scheduler to allocate CPU time for execution, positioned in the task ready list and sorted by priority. - Running State
: Currently holds CPU usage rights, executing task code, with only one task in the running state at any given time, achieved through context switching by the scheduler to create the illusion of multitasking. - Blocked State
: The task is paused due to waiting for an event (such as a semaphore, message), timing delay, or resource unavailability, entering the blocked list, and will re-enter the ready state when conditions are met. - Suspended State
: The task is forcibly paused and does not participate in scheduling, commonly used for debugging or temporarily stopping non-critical tasks, which can be restored through external commands, differing from the blocked state in that the wake-up conditions are different.
Task state transitions are triggered by system calls, such as when a task initiates a wait for a semaphore operation, transitioning from the ready state to the blocked state; when the semaphore is released and the wait conditions are met, it transitions back to the ready state waiting for scheduling.
4.2 Task Creation and Deletion
When creating a task, developers need to define the task function (containing the task logic code), set the task priority, allocate stack size, and other parameters. The system call <span>xTaskCreate</span>
function allocates task stack space in the kernel memory pool, initializes the TCB, and adds the task to the ready list, completing the task startup preparation. To delete a task, the <span>vTaskDelete</span>
function is invoked to release the memory occupied by the task stack and TCB, cleaning up task-related resources to ensure no memory leaks in the system.
4.3 Task Priority and Scheduling Strategies
The lower the priority value, the higher the priority. The priority is specified when creating the task. Scheduling strategies are divided into:
- Preemptive Scheduling
: When a high-priority task is ready, it immediately preempts the currently running low-priority task, ensuring that real-time critical tasks are executed first, suitable for applications sensitive to response time, such as industrial alarm monitoring. - Round-Robin Scheduling
: Tasks of equal priority take turns obtaining fixed time slices of CPU usage, preventing low-priority tasks from starving for long periods, commonly used in scenarios where the multi-task load is relatively balanced, such as multi-sensor data collection in smart home environmental monitoring nodes.
