0
0
FreeRTOSprogramming~5 mins

Task function signature in FreeRTOS

Choose your learning style9 modes available
Introduction

Task functions are the main code blocks that run in FreeRTOS tasks. They need a specific format so the system can run them properly.

When creating a new task to run code concurrently.
When defining what a task should do when it starts.
When passing a function to FreeRTOS to manage as a task.
When organizing code to run in separate threads on a microcontroller.
Syntax
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.

Examples
Basic task function with no return and a generic parameter pointer.
FreeRTOS
void vTaskCode(void *pvParameters) {
    // Task code here
}
Task function that receives a pointer to an integer parameter and uses it.
FreeRTOS
void SensorTask(void *pvParameters) {
    int sensorId = *(int *)pvParameters;
    // Use sensorId in task
}
Sample Program

This program creates a FreeRTOS task named "ExampleTask" that prints its name every second. The task function uses the parameter to know its name.

FreeRTOS
#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;
}
OutputSuccess
Important Notes

Task functions must never return; they usually run an infinite loop.

Always cast pvParameters to the correct pointer type before use.

Summary

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.