当前位置:网站首页>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.
边栏推荐
猜你喜欢

How to better assess credit risk?Just watch this scorecard model live

An example of type3 voltage loop compensator taking Boost as an example

excel 批量翻译-excel 批量函数公司翻译大全免费

1.3 Rapid Spanning Tree Protocol RSTP

photo-sphere-viewer中文文档

WebUI自动化测试框架搭建从0到1(完整源码)更新完毕

liunx基础命令讲解

MD5 detailed explanation (check file integrity)

看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!

WPF——自定义日历
随机推荐
ABAP-OOAVL模板程序
自己如何做小程序呢?
liunx基础命令讲解
如何更好评估信用贷风险?看这场评分卡模型直播就可以了
openresty 性能优化
Speed up your programs with bitwise operations
商业流程服务BPass你真的了解吗?
linux basic command explanation
技术分享| 融合调度系统中的电子围栏功能说明
LeetCode_377_组合总和Ⅳ
np.nan, np.isnan, None, pd.isnull, pd.isna 整理与小结
图神经网络(GNN)的简介「建议收藏」
力扣977-有序数组的平方——暴力法&双指针法
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
zabbix自动化监控脚本
服务器间传输文件
kvm部署
FreeRTOS--栈实验
FreeRTOS--stack experiment
The 7 most commonly used data analysis thinking, solve 95% of the analysis problems