The xTaskCreate() function helps you start a new task (a small program) that runs alongside other tasks in FreeRTOS. It lets your microcontroller do many things at once.
0
0
xTaskCreate() function in FreeRTOS
Introduction
When you want your device to read a sensor and control a display at the same time.
When you need to handle button presses while also sending data over Wi-Fi.
When you want to separate different parts of your program to keep code organized and responsive.
Syntax
FreeRTOS
BaseType_t xTaskCreate(
TaskFunction_t pvTaskCode,
const char * const pcName,
configSTACK_DEPTH_TYPE usStackDepth,
void *pvParameters,
UBaseType_t uxPriority,
TaskHandle_t *pxCreatedTask
);pvTaskCode is the function where your task's code lives.
usStackDepth is how much memory the task needs for its work.
Examples
This creates a task named "Task1" with priority 1 and no parameters.
FreeRTOS
xTaskCreate(
vTaskFunction,
"Task1",
1000,
NULL,
1,
NULL
);This creates a task that uses sensorData and saves its handle for later control.
FreeRTOS
xTaskCreate(
vSensorTask,
"Sensor",
500,
(void *) &sensorData,
2,
&sensorTaskHandle
);Sample Program
This program creates a task that prints "LED ON" and "LED OFF" every half second, simulating blinking. The scheduler runs the task.
FreeRTOS
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void vBlinkTask(void *pvParameters) { for(;;) { printf("LED ON\n"); vTaskDelay(pdMS_TO_TICKS(500)); printf("LED OFF\n"); vTaskDelay(pdMS_TO_TICKS(500)); } } int main(void) { xTaskCreate( vBlinkTask, "Blink", 1000, NULL, 1, NULL ); vTaskStartScheduler(); for(;;); // Should never reach here return 0; }
OutputSuccess
Important Notes
Always check the return value of xTaskCreate() to ensure the task was created successfully.
Stack size depends on what your task does; too small causes crashes, too big wastes memory.
Task priority controls which task runs first when multiple are ready.
Summary
xTaskCreate() starts a new task in FreeRTOS.
You give it the task function, name, stack size, parameters, priority, and a place to store the task handle.
It helps your device do many things at once smoothly.