一.动态创建任务
1.函数xTaskCreate()
BaseType_t xTaskCreate(
TaskFunction_t pxTaskCode, // 函数指针, 任务函数
const char * const pcName, // 任务的名字
const configSTACK_DEPTH_TYPE usStackDepth, // 栈大小,单位为word,10表示40字节
void * const pvParameters, // 调用任务函数时传入的参数
UBaseType_t uxPriority, // 优先级
TaskHandle_t * const pxCreatedTask ); // 任务句柄, 以后使用它来操作这个任务
2.实现
void Task1Function(void*param)
{
while(1)
{
printf("1");
}
}
void Task2Function(void*param)
{
while(1)
{
printf("2");
}
}
int main( void )
{
TaskHandle_t xHandleTask;
#ifdef DEBUG
debug();
#endif
prvSetupHardware();
printf("hello,world,nimade!\r\n");
xTaskCreate(Task1Function,"Task1",100,NULL,1,&xHandleTask);
xTaskCreate(Task2Function,"Task2",100,NULL,1,&xHandleTask);
/* Start the scheduler. */
vTaskStartScheduler();
/* Will only get here if there was not enough heap space to create the
idle task. */
return 0;
}
二.静态创建任务
TCB结构体得事先分配好栈,栈要事先分配好定义一个数组。
1.函数xTaskCreateStatic()
TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
const char * const pcName,
const uint32_t ulStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
StackType_t * const puxStackBuffer,//自己提供的栈
StaticTask_t * const pxTaskBuffer )//自己编写的结构体
配置这个函数要先配置configSUPPORT_STATIC_ALLOCATION
同时还要配置Idle线程
StackType_t xIdleTaskStack[100];//100*4字节
StaticTask_t xIdleTaskTCB;
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t * pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
*ppxIdleTaskStackBuffer = xIdleTaskStack;
*pulIdleTaskStackSize = 100;
}
2.实现
void Task4Function(void*param)
{
while(1)
{
printf("3");
}
}
StackType_t xTask4Stack[100];//100*4字节
StaticTask_t xTask4TCB;
StackType_t xIdleTaskStack[100];//100*4字节
StaticTask_t xIdleTaskTCB;
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t * pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
*ppxIdleTaskStackBuffer = xIdleTaskStack;
*pulIdleTaskStackSize = 100;
}
int main( void )
{
LED_Init();
TaskHandle_t xHandleTask1;
#ifdef DEBUG
debug();
#endif
prvSetupHardware();
printf("hello,world,nimade!\r\n");
xTaskCreateStatic(Task4Function,"Task4",100,NULL,1,xTask4Stack,&xTask4TCB);
/* Start the scheduler. */
vTaskStartScheduler();
/* Will only get here if there was not enough heap space to create the
idle task. */
return 0;
}
void Task1Function(void*param)
{
while(1)
{
printf("1");
}
}
void Task2Function(void*param)
{
while(1)
{
printf("2");
}
}
void Task3Function(void*param)
{
while(1)
{
printf("3");
}
}
StackType_t xTask3Stack[100];//100*4
StaticTask_t xTask3TCB;
StackType_t xIdleTaskStack[100];//100*4
StaticTask_t xIdleTaskTCB;
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t * pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
*ppxIdleTaskStackBuffer = xIdleTaskStack;
*pulIdleTaskStackSize = 100;
}
int main( void )
{
TaskHandle_t xHandleTask;
#ifdef DEBUG
debug();
#endif
prvSetupHardware();
printf("hello,world,nimade!\r\n");
xTaskCreate(Task1Function,"Task1",100,NULL,1,&xHandleTask);
xTaskCreate(Task2Function,"Task2",100,NULL,1,&xHandleTask);
xTaskCreateStatic(Task3Function,"Task3",100,NULL,1,xTask3Stack,&xTask3TCB);
/* Start the scheduler. */
vTaskStartScheduler();
/* Will only get here if there was not enough heap space to create the
idle task. */
return 0;
}
三.进一步实验
1.验证优先级
A.验证相同优先级
static int task1flagrum=0;
static int task2flagrum=0;
static int task3flagrum=0;
void Task1Function(void*param)
{
while(1)
{
task1flagrum=1;
task2flagrum=0;
task3flagrum=0;
printf("1");
}
}
void Task2Function(void*param)
{
while(1)
{
task1flagrum=0;
task2flagrum=1;
task3flagrum=0;
printf("2");
}
}
void Task3Function(void*param)
{
while(1)
{
task1flagrum=0;
task2flagrum=0;
task3flagrum=1;
printf("3");
}
}
StackType_t xTask3Stack[100];//100*4
StaticTask_t xTask3TCB;
StackType_t xIdleTaskStack[100];//100*4
StaticTask_t xIdleTaskTCB;
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t * pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
*ppxIdleTaskStackBuffer = xIdleTaskStack;
*pulIdleTaskStackSize = 100;
}
int main( void )
{
TaskHandle_t xHandleTask;
#ifdef DEBUG
debug();
#endif
prvSetupHardware();
printf("hello,world,nimade!\r\n");
xTaskCreate(Task1Function,"Task1",100,NULL,1,&xHandleTask);
xTaskCreate(Task2Function,"Task2",100,NULL,1,&xHandleTask);
xTaskCreateStatic(Task3Function,"Task3",100,NULL,1,xTask3Stack,&xTask3TCB);
/* Start the scheduler. */
vTaskStartScheduler();
/* Will only get here if there was not enough heap space to create the
idle task. */
return 0;
}
同优先级任务交错执行
B.修改优先级
xTaskCreate(Task1Function,"Task1",100,NULL,2,&xHandleTask);//提高优先级
xTaskCreate(Task2Function,"Task2",100,NULL,1,&xHandleTask);
xTaskCreateStatic(Task3Function,"Task3",100,NULL,1,xTask3Stack,&xTask3TCB);
优先级不同:高优先级任务先执行,高优先级任务不主动放弃任务,低优先级任务不可能执行。
c.结论
高优先级任务优先执行,同等优先级任务交错执行。
2.删除任务
TaskHandle_t xHandleTask1;
TaskHandle_t xHandleTask2;
static int task1flagrum=0;
static int task2flagrum=0;
static int task3flagrum=0;
void Task1Function(void*param)
{
while(1)
{
task1flagrum=1;
task2flagrum=0;
task3flagrum=0;
printf("1");
}
}
void Task2Function(void*param)
{
int i=0;
while(1)
{
task1flagrum=0;
task2flagrum=1;
task3flagrum=0;
printf("2");
if(i++==100)
{
vTaskDelete(xHandleTask1);//删除别人的任务
}
if(i==200)
{
vTaskDelete(xHandleTask2);//自删
}
}
}
void Task3Function(void*param)
{
while(1)
{
task1flagrum=0;
task2flagrum=0;
task3flagrum=1;
printf("3");
}
}
StackType_t xTask3Stack[100];//100*4
StaticTask_t xTask3TCB;
StackType_t xIdleTaskStack[100];//100*4
StaticTask_t xIdleTaskTCB;
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t * pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
*ppxIdleTaskStackBuffer = xIdleTaskStack;
*pulIdleTaskStackSize = 100;
}
int main( void )
{
#ifdef DEBUG
debug();
#endif
prvSetupHardware();
printf("hello,world,nimade!\r\n");
xTaskCreate(Task1Function,"Task1",100,NULL,1,&xHandleTask1);
xTaskCreate(Task2Function,"Task2",100,NULL,1,&xHandleTask2);
xTaskCreateStatic(Task3Function,"Task3",100,NULL,1,xTask3Stack,&xTask3TCB);
/* Start the scheduler. */
vTaskStartScheduler();
/* Will only get here if there was not enough heap space to create the
idle task. */
return 0;
}
3.使用同一个任务函数创建多个任务
TaskHandle_t xHandleTask1;
TaskHandle_t xHandleTask2;
static int task1flagrum=0;
static int task2flagrum=0;
static int task3flagrum=0;
void Task1Function(void*param)
{
while(1)
{
task1flagrum=1;
task2flagrum=0;
task3flagrum=0;
printf("1");
}
}
void Task2Function(void*param)
{
int i=0;
while(1)
{
task1flagrum=0;
task2flagrum=1;
task3flagrum=0;
printf("2");
if(i++==100)
{
vTaskDelete(xHandleTask1);//删除别人的任务
}
if(i==200)
{
vTaskDelete(xHandleTask2);//自删,存入NULL就可以了
}
}
}
void Task3Function(void*param)
{
while(1)
{
task1flagrum=0;
task2flagrum=0;
task3flagrum=1;
printf("3");
}
}
void TaskGerericFunction(void*param)
{
int val=(int)param;
while(1)
{
printf("%d ",val);
}
}
StackType_t xTask3Stack[100];//100*4
StaticTask_t xTask3TCB;
StackType_t xIdleTaskStack[100];//100*4
StaticTask_t xIdleTaskTCB;
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t * pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
*ppxIdleTaskStackBuffer = xIdleTaskStack;
*pulIdleTaskStackSize = 100;
}
int main( void )
{
#ifdef DEBUG
debug();
#endif
prvSetupHardware();
printf("hello,world,nimade!\r\n");
xTaskCreate(Task1Function,"Task1",100,NULL,1,&xHandleTask1);
xTaskCreate(Task2Function,"Task2",100,NULL,1,&xHandleTask2);
xTaskCreateStatic(Task3Function,"Task3",100,NULL,1,xTask3Stack,&xTask3TCB);
xTaskCreate(TaskGerericFunction,"Task4",100,(void*)4,1,NULL);
xTaskCreate(TaskGerericFunction,"Task5",100,(void*)5,1,NULL);
/* Start the scheduler. */
vTaskStartScheduler();
/* Will only get here if there was not enough heap space to create the
idle task. */
return 0;
}
4.栈大小
TaskHandle_t xHandleTask1;
TaskHandle_t xHandleTask2;
static int task1flagrum=0;
static int task2flagrum=0;
static int task3flagrum=0;
void Task1Function(void*param)
{
volatile char buf[500];//故意设计很大的数组,数据存放在栈中
int i;
while(1)
{
task1flagrum=1;
task2flagrum=0;
task3flagrum=0;
printf("1");
for(i=0;i<500;i++)
{
buf[i]=0;
}
}
}
void Task2Function(void*param)
{
int i=0;
while(1)
{
task1flagrum=0;
task2flagrum=1;
task3flagrum=0;
printf("2");
if(i++==100)
{
//vTaskDelete(xHandleTask1);//删除别人的任务
}
if(i==200)
{
//vTaskDelete(xHandleTask2);//自删,存入NULL就可以了
}
}
}
void Task3Function(void*param)
{
while(1)
{
task1flagrum=0;
task2flagrum=0;
task3flagrum=1;
printf("3");
}
}
void TaskGerericFunction(void*param)
{
int val=(int)param;
while(1)
{
printf("%d ",val);
}
}
StackType_t xTask3Stack[100];//100*4
StaticTask_t xTask3TCB;
StackType_t xIdleTaskStack[100];//100*4
StaticTask_t xIdleTaskTCB;
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t * pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
*ppxIdleTaskStackBuffer = xIdleTaskStack;
*pulIdleTaskStackSize = 100;
}
int main( void )
{
#ifdef DEBUG
debug();
#endif
prvSetupHardware();
printf("hello,world,nimade!\r\n");
xTaskCreate(Task1Function,"Task1",100,NULL,1,&xHandleTask1);
xTaskCreate(Task2Function,"Task2",100,NULL,1,&xHandleTask2);
xTaskCreateStatic(Task3Function,"Task3",100,NULL,1,xTask3Stack,&xTask3TCB);
xTaskCreate(TaskGerericFunction,"Task4",100,(void*)4,1,NULL);
xTaskCreate(TaskGerericFunction,"Task5",100,(void*)5,1,NULL);
/* Start the scheduler. */
vTaskStartScheduler();
/* Will only get here if there was not enough heap space to create the
idle task. */
return 0;
}