A conventional processor can only execute a single task at a time - but by rapidly switching between tasks a multitasking operating system can make it appear as if each task is executing concurrently. This is depicted by the diagram below which shows the execution pattern of three tasks with respect to time. The task names are color coded and written down the left hand. Time moves from left to right, with the colored lines showing which task is executing at any particular time. The upper diagram demonstrates the perceived concurrent execution pattern, and the lower the actual multitasking execution pattern.
The diagram below demonstrates how two tasks (1: Suspend waiting for a key press, 2: Suspend waiting for 2ms since the start of the previous cycle) would be scheduled by a real time operating system. The RTOS has itself created a task - the idle task - which will execute only when there are no other tasks able to do so. The RTOS idle task is always in a state where it is able to execute.
Multitasking is performed on FreeRTOS by creating Tasks or Multiple Tasks. Tasks in FreeRTOS are individual programs which has capability to run independently and synchronously with other tasks. In General, each task is a program of its own with infinite while loop.
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
Create a new task and add it to the list of tasks that are ready to run.
xTaskCreate(
vTaskBlink, // Function to implement the task
"Task 1", // Friendly name of the task
2048, // Stack size in bytes, how much memory you want to keep it for this task
NULL, // Task input parameter, NULL for no passing
5, // Priority of the task, 1 lowest, 5 highest
NULL // Used to pass back a handle
);
Create a new task with a specified affinity and add it to the list of tasks that are ready to run.
xTaskCreatePinnedToCore(
vTaskCore, // Function to implement the task
"coreTask", // Friendly name of the task
1000, // Stack size in bytes, how much memory you want to keep it for this task
NULL, // Task input parameter, NULL for no passing
2, // Priority of the task, 1 lowest, 5 highest
NULL, // Used to pass back a handle
coreID // Core where the task should run
);
Remove a task from the RTOS real time kernel's management. The task being deleted will be removed from all ready, blocked, suspended and event lists.
- Variables of non stdint types are prefixed
x
. Examples includeBaseType_t
andTickType_t
. - API functions are prefixed with their return type, as per the convention defined for variables with the addition of the prefix
v
for void. - API function names start with the name of the file in which they are defined. For example
vTaskDelete
is defined intasks.c
, and has a void return type.