当前位置:网站首页>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.
边栏推荐
猜你喜欢
不错的射击类js小游戏源码
Lexicon 27 - Remove Elements - Simple Questions
ssm access database data error
Manual architecture, Mysql interview 126 questions
js炫酷仪表盘插件
Speed up your programs with bitwise operations
js半圆环加载进度动画js特效
Likou 977-Squaring of ordered arrays - brute force method & double pointer method
#Summer Challenge#[FFH] OpenHarmony Device Development Foundation (3) Compilation Dependencies
Drools(8): WorkBench uses
随机推荐
一款强大的js弹出alert插件
技术分享| 融合调度系统中的电子围栏功能说明
Thymeleaf
php字符串的截取方式
js秒表倒计时插件
Likou 209 - String with the Minimum Length - Sliding Window Method
Drools(8):WorkBench使用
测试开发之路,我在大厂做测试这四年的感悟
How to better assess credit risk?Just watch this scorecard model live
Solve the problem of Chinese garbled characters in exporting excel file names
SQL Server 2014安装教程(保姆级图解教程)
用位运算为你的程序加速
FreeRTOS创建任务--动态创建、静态创建
第11章 文件
LeetCode_377_组合总和Ⅳ
Pod Scheduling Strategy: Affinity, Stain and Stain Tolerance
内存存储结构
np.nan, np.isnan, None, pd.isnull, pd.isna finishing and summary
Likou 704 - binary search
图神经网络(GNN)的简介「建议收藏」