Task functions are the main code blocks that run in FreeRTOS tasks. They need a specific format so the system can run them properly.
Task function signature in FreeRTOS
void TaskName(void *pvParameters)
The function must return void because tasks do not return values.
The parameter void *pvParameters allows passing any data to the task.
void vTaskCode(void *pvParameters) {
// Task code here
}void SensorTask(void *pvParameters) {
int sensorId = *(int *)pvParameters;
// Use sensorId in task
}This program creates a FreeRTOS task named "ExampleTask" that prints its name every second. The task function uses the parameter to know its name.
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void vTaskExample(void *pvParameters) { const char *taskName = (const char *)pvParameters; for (;;) { printf("Running task: %s\n", taskName); vTaskDelay(pdMS_TO_TICKS(1000)); } } int main(void) { const char *name = "Task1"; xTaskCreate(vTaskExample, "ExampleTask", 1000, (void *)name, 1, NULL); vTaskStartScheduler(); for(;;); // Should never reach here return 0; }
Task functions must never return; they usually run an infinite loop.
Always cast pvParameters to the correct pointer type before use.
Task functions have the signature void TaskName(void *pvParameters).
They run code inside an infinite loop and do not return.
The pvParameters pointer lets you pass any data to the task.
Practice
What is the correct signature for a FreeRTOS task function?
Solution
Step 1: Recall FreeRTOS task function signature
FreeRTOS tasks must have the signature with return type void and a single void pointer parameter.Step 2: Match the signature with options
Only void TaskName(void *pvParameters) matchesvoid TaskName(void *pvParameters)exactly.Final Answer:
void TaskName(void *pvParameters) -> Option BQuick Check:
Task signature = void TaskName(void *pvParameters) [OK]
- Using int return type instead of void
- Omitting the void* parameter
- Using incorrect parameter types
Which of the following is a syntactically correct FreeRTOS task function declaration?
void MyTask(_____)
Solution
Step 1: Understand parameter type for FreeRTOS tasks
Task functions must accept a single parameter of typevoid *.Step 2: Check each option
Only void *pvParameters usesvoid *pvParameters, which is correct syntax.Final Answer:
void *pvParameters -> Option AQuick Check:
Parameter type = void *pvParameters [OK]
- Using int or char pointer instead of void pointer
- Using void without pointer
- Missing parameter entirely
What will happen if you run this FreeRTOS task function?
void ExampleTask(void *pvParameters) {
for(;;) {
// Task code here
}
}Solution
Step 1: Analyze the task function loop
The task contains afor(;;)infinite loop, so it runs repeatedly.Step 2: Understand FreeRTOS task behavior
Tasks should not return; they run inside infinite loops to keep running.Final Answer:
The task will run repeatedly in an infinite loop. -> Option DQuick Check:
Infinite loop in task = runs repeatedly [OK]
- Thinking task runs once and exits
- Expecting return value from task
- Confusing infinite loop with error
Identify the error in this FreeRTOS task function:
void FaultyTask(void *pvParameters) {
int count = 0;
while(count < 5) {
count++;
}
return;
}Solution
Step 1: Check task function return behavior
FreeRTOS tasks must not return; they should run inside infinite loops.Step 2: Analyze the code flow
This task loops 5 times then returns, which is incorrect for FreeRTOS tasks.Final Answer:
The function should not return; it must loop forever. -> Option AQuick Check:
Tasks must not return, use infinite loop [OK]
- Allowing task to return
- Changing parameter type incorrectly
- Misunderstanding loop conditions
You want to pass an integer value to a FreeRTOS task. Which is the correct way to define and use the task function?
void NumberTask(_____)
{
int received = *(int *)pvParameters;
for(;;) {
// Use received value
}
}Solution
Step 1: Recall task function parameter type
Task functions must accept avoid *parameter to allow any data type.Step 2: Understand data passing
The integer is passed via pointer cast fromvoid *, so parameter must bevoid *pvParameters.Final Answer:
void *pvParameters -> Option CQuick Check:
Use void* parameter to pass any data type [OK]
- Using int instead of void* parameter
- Using void without pointer
- Not casting void* to correct type inside task
Ready to prove it?
Pass the test and this pattern moves unknown
