What a task is
A FreeRTOS task is a C function with an endless loop. This one, sense, reads a sensor, then waits. It must never return. The kernel, the core of the RTOS, lets several such tasks take turns on one CPU.
xTaskCreate gives sense its own stack of 256 words (1,024 bytes on this 32-bit chip) and priority 2. A second task, logger, gets priority 1. A bigger number means more important. New tasks start Ready: able to run, waiting for the CPU.
Who gets the CPU
vTaskStartScheduler() hands the CPU to the kernel. It adds an idle task at priority 0, then runs the highest-priority Ready task: sense, priority 2. This chip has one core, so exactly one task is Running at a time.
sense reads its sensor in 0.5 ms, then calls vTaskDelay(5). It moves to Blocked until 5 ticks pass; our tick is 1 ms. A Blocked task uses no CPU time and cannot be picked to run.
With sense blocked, the highest-priority Ready task is logger, priority 1, so it runs. It needs 6 ms of CPU to format and write a log line. logger never mentions sense: the kernel alone decides who runs.
At tick 5 sense's delay ends and it becomes Ready. 2 beats 1, so the kernel stops logger at once, mid-line, saves its registers on logger's own stack and runs sense. That is preemption.
sense blocks again after 0.5 ms, until tick 10. logger resumes exactly where it stopped, from its saved registers. It finishes at 7 ms and calls vTaskDelay(20), so it is Blocked until tick 27.
From 7 ms none of our tasks is Ready, so the idle task runs. It can put the CPU into a low-power mode. At tick 10 sense wakes and preempts idle, exactly as it preempted logger.
The rule
Delete vTaskDelay(5) and sense is always Ready. Priority 2 always wins, so logger and idle never run again: they starve. With priorities, high-priority tasks must block to wait for time or an event.
What happens
;; step 1/9 · task
task a C function that loops forever
void sense(void *p) { for (;;) { ... } }
kernel the core of the RTOS: it decides which task runs
rule a task function must never return
// STM32F4DISCOVERY, FreeRTOS. FreeRTOSConfig.h: configTICK_RATE_HZ 1000 (1 tick = 1 ms)
#include "FreeRTOS.h"
#include "task.h"
void readSensor(void); // about 0.5 ms of work (our example)
void writeLog(void); // about 6 ms of work (our example)
void sense(void *p) {
for (;;) {
readSensor();
vTaskDelay(5);
}
}
void logger(void *p) {
for (;;) {
writeLog();
vTaskDelay(20);
}
}
int main(void) {
// clock and pin setup omitted
xTaskCreate(sense, "sense", 256, NULL, 2, NULL);
xTaskCreate(logger, "logger", 256, NULL, 1, NULL);
vTaskStartScheduler(); // never returns while there is enough heap
for (;;) {}
}
The card shows only the sense task and its creation. The whole program also defines logger the same way with priority 1, and main() creates both tasks and calls vTaskStartScheduler(). readSensor() and writeLog() stand for real work; 0.5 ms and 6 ms are example times we chose. vTaskDelay counts ticks, not milliseconds. We set configTICK_RATE_HZ to 1000, so one tick is 1 ms and vTaskDelay(5) waits 5 ms. Portable code writes vTaskDelay(pdMS_TO_TICKS(5)). The delay counts from the tick when it is called, so the period drifts; vTaskDelayUntil keeps an exact period. A real context switch takes a few microseconds, and the tick interrupt itself uses a little CPU time. The timeline ignores both.