如何向任何 PlatformIO 项目添加 FreeRTOS 任务(线程)
大多数 PlatformIO 默认配置已经启用了 FreeRTOS - 它们只是不使用它。
为了启动新的 FreeRTOS “线程”(在 FreeRTOS 术语中称为任务),首先添加这些包含:
freertos_includes.cpp
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>现在添加任务函数和句柄:
freertos_task_function.cpp
TaskHandle_t myTaskHandle;
void MyTask( void * parameter )
{
for(;;)
{
// TODO 任务代码放在这里
}
// 如果你退出循环,这是为了清理资源
vTaskDelete( NULL );
}然后使用此代码启动任务一次,例如在你的 main 函数中:
freertos_xTaskCreate_example.cpp
// 启动 MyTask 线程
xTaskCreate(
MyTask, // 任务函数
"MyTask", // 名称
10000, // 栈大小
NULL, // 参数
1, // 优先级
&myTaskHandle);另请参阅我们关于如何使用 xTaskCreateStatic() 为任务使用静态分配而非动态分配的栈内存的新文章:FreeRTOS 任务静态栈内存 (xTaskCreateStatic) 示例
Check out similar posts by category:
C/C++, Electronics, Embedded, FreeRTOS, PlatformIO
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow