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.
xTaskCreate() function in 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.
xTaskCreate(
vTaskFunction,
"Task1",
1000,
NULL,
1,
NULL
);xTaskCreate(
vSensorTask,
"Sensor",
500,
(void *) &sensorData,
2,
&sensorTaskHandle
);This program creates a task that prints "LED ON" and "LED OFF" every half second, simulating blinking. The scheduler runs the task.
#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; }
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.
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.
Practice
xTaskCreate() function in FreeRTOS?Solution
Step 1: Understand the role of
This function is designed to create and start a new task in FreeRTOS, allowing multitasking.xTaskCreate()Step 2: Compare with other options
Deleting tasks, pausing the scheduler, or allocating memory are handled by different functions, notxTaskCreate().Final Answer:
To start a new task that runs concurrently with others -> Option AQuick Check:
Task creation = start new task [OK]
- Confusing task creation with task deletion
- Thinking it pauses the scheduler
- Assuming it allocates general memory
xTaskCreate() in FreeRTOS?Solution
Step 1: Recall the correct parameter list
The correct syntax requires the task function, a string name in quotes, stack size, parameters, priority, and a pointer to the task handle.Step 2: Check each option carefully
xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority, &TaskHandle); matches the correct syntax exactly, including quotes around the name and the address-of operator for the task handle.Final Answer:
xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority, &TaskHandle); -> Option DQuick Check:
Correct syntax includes &TaskHandle [OK]
- Forgetting quotes around the task name
- Passing task handle without & operator
- Omitting the task handle parameter
void vTaskCode(void * pvParameters) {
for(;;) {
// Task code here
}
}
int main() {
TaskHandle_t xHandle = NULL;
BaseType_t result = xTaskCreate(vTaskCode, "Task1", 1000, NULL, 2, &xHandle);
if(result == pdPASS) {
// Task created successfully
}
return 0;
}What is the value of
result if the task is created successfully?Solution
Step 1: Understand
This function returnsxTaskCreate()return valuespdPASSif the task was created successfully, otherwisepdFAIL.Step 2: Analyze the code logic
The code checks ifresult == pdPASSto confirm success, soresultmust bepdPASSon success.Final Answer:
pdPASS -> Option AQuick Check:
Success return = pdPASS [OK]
- Confusing pdPASS with pdFAIL
- Assuming NULL or 1 as success
- Not checking the return value
xTaskCreate() usage:TaskHandle_t xHandle; BaseType_t result = xTaskCreate(vTaskCode, "Task2", 500, NULL, 1, xHandle);
Solution
Step 1: Check the task handle parameter
The last parameter must be a pointer to the task handle variable, so it requires the address operator &.Step 2: Verify other parameters
The task name is correctly a string in quotes, stack size 500 is valid, and priority 1 is allowed.Final Answer:
Missing & before xHandle in last parameter -> Option CQuick Check:
Task handle needs & operator [OK]
- Passing task handle without &
- Removing quotes from task name
- Incorrect assumptions about stack size or priority
xTaskCreate(). Which code snippet correctly creates both tasks and stores their handles?Solution
Step 1: Verify task handle usage
Each task handle must be passed by address using &. TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2); correctly does this for both handles.Step 2: Check task names and priorities
Task names are strings in quotes. Priorities differ (3 and 5) as required. TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2); matches this correctly.Step 3: Confirm no parameter mix-ups
TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 5, handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 3, handle2); misses & operator. TaskHandle_t handle1, handle2; xTaskCreate(Task1, Task1, 1000, NULL, 3, &handle1); xTaskCreate(Task2, Task2, 1000, NULL, 5, &handle2); uses function names instead of strings for task names. TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle2); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle1); swaps handles incorrectly.Final Answer:
TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2); -> Option BQuick Check:
Correct handles and priorities = TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2); [OK]
- Passing handles without &
- Using function names instead of string names
- Mixing up handles between tasks
Ready to prove it?
Pass the test and this pattern moves unknown
