当前位置:网站首页>FreeRTOS creation tasks - dynamic creation, static creation
FreeRTOS creation tasks - dynamic creation, static creation
2022-08-02 12:32:00 【Mountain,】
任务创建函数:xTaskCreate()
BaseType_t xTaskCreate( TaskFunction_t pxTaskCode, //函数指针
const char * const pcName, //函数名
const configSTACK_DEPTH_TYPE usStackDepth, //栈深度
void * const pvParameters, //参数
UBaseType_t uxPriority, //优先级
TaskHandle_t * const pxCreatedTask // 任务句柄)
Each task has its ownTCB结构体–tskTaskControlBlock
任务控制块,Used to save various information for this task.
The last item of a function parameter is oneTCB结构体,只不过用TaskHandle_t
表示.
typedef struct tskTaskControlBlock * TaskHandle_t;
The task can be manipulated using this task handle,For example, let the task go to sleep,Just pass the task handle of the task as a parameter to the corresponding function.
注意:FreeRTOSThe tasks in must be in the form of an infinite loop with no return value.
TaskHandle_t xHandleTask1;
void Task1(void * param)
{
while(1)
{
printf("A");
}
}
TaskHandle_t xHandleTask2;
void Task2(void * param)
{
while(1)
{
printf("a");
}
}
int main( void )
{
prvSetupHardware();
printf("Hello World!\r\n");
xTaskCreate(Task1,"Task1",100,NULL,1,&xHandleTask1);
xTaskCreate(Task2,"Task2",100,NULL,1,&xHandleTask2);
vTaskStartScheduler();
return 0;
}
输出结果:
FreeRTOSCreate task additions can be usedxTaskCreate();
Functions dynamically create tasks,也可以使用xTaskCreateStatic()
Functions create tasks statically.
The former creates tasks dynamically,Allocation sum of stack spaceTCBStructures, etc. are automatically created and allocated by the system,The user does not need to do other operations;The latter creates tasks statically,Task control blocks need to be assigned to tasks in advanceTCBand stack space,And some other configuration is required.
函数原型:
可以看到,静态创建任务函数xTaskCreateStatic()
的参数与xTaskCreate();
Before dynamically creating the parameters of the task5个是一样的,Both are task function pointers、任务名字、栈深度、参数、优先级,But the last two are different,一个是栈,一个是任务TCB,Both of these need to be created by the user in advance.
Creating a task function using static also requires a macro switch to be enabled.在tasks.cYou can see in the file to enable the macroconfigSUPPORT_STATIC_ALLOCATION
才能使用xTaskCreateStatic()
函数.
所以在FreeRTOSConfig.h
文件中定义configSUPPORT_STATIC_ALLOCATION
为1.
#define configSUPPORT_STATIC_ALLOCATION 1
Open the start task caller functionvTaskStartScheduler( )
,可以看到,如果我们定义了configSUPPORT_STATIC_ALLOCATION
,在使用xTaskCreateStatic()
函数前,You also need to provide a function:vApplicationGetIdleTaskMemory()
才行.So we also need to provide this function to use statically create task functions.
StackType_t xIdleTaskStack[100];
StaticTask_t xIdleTaskTcb;
void vApplicationGetIdleTaskMemory(StaticTask_t * * ppxIdleTaskTCBBuffer, StackType_t * * ppxIdleTaskStackBuffer, uint32_t * pulIdleTaskStackSize)
{
*ppxIdleTaskTCBBuffer = &xIdleTaskTcb;
*ppxIdleTaskStackBuffer = xIdleTaskStack;
*pulIdleTaskStackSize = 100;
}
经过上面两步:Enable macro switches、提供vApplicationGetIdleTaskMemory()
函数,We can use static to create task functions.
StackType_t xTask3Stack[100]; //栈
StaticTask_t xTask3Tcb; //TCB结构体
void Task3(void * param)
{
while(1)
{
printf("1");
}
}
int main( void )
{
prvSetupHardware();
printf("Hello World!\r\n");
xTaskCreate(Task1,"Task1",100,NULL,1,&xHandleTask1);
xTaskCreate(Task2,"Task2",100,NULL,1,&xHandleTask2);
xTaskCreateStatic(Task3,"Task3",100,NULL,1,xTask3Stack,&xTask3Tcb);
vTaskStartScheduler();
return 0;
}
输出结果:
可以看到,All three tasks run successfully.
边栏推荐
猜你喜欢
Seneor Exposure Basics
WebUI自动化测试框架搭建从0到1(完整源码)更新完毕
Taurus.MVC V3.0.3 Microservice Open Source Framework Released: Make the evolution of .NET architecture easier in large concurrency.
pig4cloud服务架构使用
手撸架构,MongDB 面试50问
To eliminate air bubbles to save the mushroom h5 small game source code
【第六届强网杯CTF-Wp】
MD5 detailed explanation (check file integrity)
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
NVIDIA NeMo Metrics 轻量性能采集系统
随机推荐
LeetCode_139_单词拆分
photo-sphere-viewer中文文档
js stopwatch countdown plugin
30 lines of code to realize serverless real-time health code recognition -- operation manual
数据湖(三):Hudi概念术语
SQL Server 2019安装错误0x80004005 服务没有及时响应启动或控制请求详细解决方法
SQL Server2019安装步骤及脱机安装Microsoft机器学习组件下一步不能继续的问题
技术分享| 融合调度系统中的电子围栏功能说明
如何关闭开启硬件加速[通俗易懂]
ssm access database data error
解决anaconda下载pytorch速度极慢的方法
NVIDIA NeMo Metrics 轻量性能采集系统
SQL Server 数据库之生成与执行 SQL 脚本
分布式限流利器,手撕&redisson实现
DTG-SSOD: The latest semi-supervised detection framework, Dense Teacher (with paper download)
测试开发之路,我在大厂做测试这四年的感悟
MyCat2 introduction and installation and basic use
力扣27-移除元素——简单题
MyCat2的介绍与安装以及基本使用
如何更好评估信用贷风险?看这场评分卡模型直播就可以了