How to add FreeRTOS task ("thread") to any PlatformIO project
Most PlatformIO default configurations already have FreeRTOS enabled - they just don’t use it.
In order to start a new FreeRTOS “thread” (called task in FreeRTOS-speak), first add these includes:
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
Now add the task function and handle:
TaskHandle_t myTaskHandle;
void MyTask( void * parameter )
{
for(;;)
{
// TODO Task code goes here
}
// if you ever exit the loop, this is here to clean up the resources
vTaskDelete( NULL );
}
then start the task using this code once, for example in your main
function:
// Start MyTask thread
xTaskCreate(
MyTask, // Task function
"MyTask", // Name
10000, // Stack size
NULL, // Parameter
1, // Priority
&myTaskHandle);
Also see our new post on how to use xTaskCreateStatic() to use statically allocated instead of dynamically allocated stack memory for the task: FreeRTOS task with static stack memory (xTaskCreateStatic) example