当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
Likou 58 - Left Rotation String
SQL Server 2014安装教程(保姆级图解教程)
sql concat()函数
Import and export data of SQL Server database
Likou 977-Squaring of ordered arrays - brute force method & double pointer method
pytorch模型转tensorflow模型
DTG-SSOD:最新半监督检测框架,Dense Teacher(附论文下载)
SQL Server 数据库之生成与执行 SQL 脚本
前男友买辣椒水威胁要抢女儿,女方能否申请人身安全保护令?
js真3d柱状图插件
【The 6th Strong Net Cup CTF-Wp】
【第六届强网杯CTF-Wp】
#Summer Challenge#[FFH] OpenHarmony Device Development Foundation (3) Compilation Dependencies
MD5 detailed explanation (check file integrity)
力扣209-长度最小的字符串——滑动窗口法
手撸架构,网络 面试36问
How to better assess credit risk?Just watch this scorecard model live
消除气泡解救蘑菇h5小游戏源码
力扣27-移除元素——简单题
Chapter 11 Documents