1.动态任务创建和删除
1.1任务创建
#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
BaseType_t xTaskCreate( TaskFunction_t pxTaskCode, /*Pointer to the task entry function. */
const char * const pcName, /* A descriptive name for the task.*/
const uint16_t usStackDepth, /*The size of the task stack specified as the number of variables the stack can hold - not the number of bytes.*/
void * const pvParameters, /*Pointer that will be used as the parameter for the task being created.*/
UBaseType_t uxPriority, /*The priority at which the task should run. */
TaskHandle_t * const pxCreatedTask /*Used to pass back a handle by which the created task can be referenced.*/
) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
#endif
1.2任务删除
/*xTaskToDelete The handle of the task to be deleted.
Passing NULL will cause the calling task to be deleted.*/
void vTaskDelete( TaskHandle_t xTaskToDelete ) PRIVILEGED_FUNCTION;
1.3实例
#include "task.h"
#define START_TASK_PRIO 1 /* 任务优先级 */
#define START_STK_SIZE 128 /* 任务堆栈大小 */
TaskHandle_t myTask_Handler; /* 任务句柄 */
/*任务函数声明*/
void start_task(void *pvParameters);
int main()
{
/* 创建任务 */
xTaskCreate((TaskFunction_t )start_task, /*任务函数*/
(const char* )"start_task", /*任务名称*/
(uint16_t )START_STK_SIZE, /*任务堆栈大小*/
(void* )NULL, /*传递给任务函数的参数*/
(UBaseType_t )START_TASK_PRIO, /*任务优先级*/
(TaskHandle_t* )&myTask_Handler); /*任务句柄*/
/* 开启任务调度 */
vTaskStartScheduler();
}
/*任务函数实现*/
void start_task(void *pvParameters)
{
/* do something */
/* 删除任务 */
vTaskDelete(myTask_Handler);
}
2.静态任务创建和删除
如果创建静态任务则需要使能 #define configSUPPORT_STATIC_ALLOCATION 1
但是在FreeRTOS中使能了静态任务创建则需要用户自定义是实现空闲任务和定时器任务任务控制块内存分配
任务堆栈大小可在源码中参考动态任务创建时这两个任务的堆栈大小。
那么就需要实现这两个任务的任务控制块vApplicationGetIdleTaskMemory 和 vApplicationGetTimerTaskMemory
2.1任务创建
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,/*Pointer to the task entry function.*/
const char * const pcName,/*A descriptive name for the task.*/
const uint32_t ulStackDepth,/*The size of the task stack specified as the number of variables the stack can hold - not the number of bytes. */
void * const pvParameters,/*Pointer that will be used as the parameter for the task being created.*/
UBaseType_t uxPriority,/*The priority at which the task will run.*/
StackType_t * const puxStackBuffer,/*Must point to a StackType_t array that has at least ulStackDepth indexes - the array will then be used as the task's stack, removing the need for the stack to be allocated dynamically.*/
StaticTask_t * const pxTaskBuffer /*Must point to a variable of type StaticTask_t, which will then be used to hold the task's data structures, removing the need for the memory to be allocated dynamically.*/
) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
#endif /* configSUPPORT_STATIC_ALLOCATION */
2.2任务删除
/* 如果要使用这个函数则需要设置宏 INCLUDE_vTaskDelete == 1,
当我们要退出任务则调用该函数。*/
void vTaskDelete( TaskHandle_t xTaskToDelete ) PRIVILEGED_FUNCTION;
2.3例程
/* 空闲任务任务堆栈*/
static StackType_t IdleTaskStack[configMINIMAL_STACK_SIZE];
/*静态空闲任务控制块*/
static StaticTask_t IdleTaskTCB;
/*定时器服务任务堆栈*/
static StackType_t TimerTaskStack[configTIMER_TASK_STACK_DEPTH];
/*静态定时器服务任务控制块*/
static StaticTask_t TimerTaskTCB;
/*任务优先级*/
#define START_TASK_PRIO 1
/*任务堆栈大小 */
#define START_STK_SIZE 128
/*任务堆栈*/
StackType_t StartTaskStack[START_STK_SIZE];
/*静态任务控制块*/
StaticTask_t StartTaskTCB;
/*静态任务句柄*/
TaskHandle_t StartTask_Handler;
/*静态任务函数*/
void start_task(void *pvParameters);
/*获取空闲任务地任务堆栈和任务控制块内存*/
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, /*任务控制块内存*/
StackType_t **ppxIdleTaskStackBuffer, /*任务堆栈内存*/
uint32_t *pulIdleTaskStackSize)/*任务堆栈大小*/
{
*ppxIdleTaskTCBBuffer=&IdleTaskTCB;
*ppxIdleTaskStackBuffer=IdleTaskStack;
*pulIdleTaskStackSize=configMINIMAL_STACK_SIZE;
}
/* 获取定时器服务任务的任务堆栈和任务控制块内存 */
void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, /*任务控制块内存*/
StackType_t **ppxTimerTaskStackBuffer, /*任务堆栈内存*/
uint32_t *pulTimerTaskStackSize)/*任务堆栈大小*/
{
*ppxTimerTaskTCBBuffer=&TimerTaskTCB;
*ppxTimerTaskStackBuffer=TimerTaskStack;
*pulTimerTaskStackSize=configTIMER_TASK_STACK_DEPTH;
}
int main()
{
/* 静态创建任务 */
StartTask_Handler = xTaskCreateStatic((TaskFunction_t )start_task, /* 任务函数 */
(const char* )"start_task", /*任务名称 */
(uint32_t )START_STK_SIZE, /*任务堆栈大小 */
(void* )NULL, /*传递给任务函数的参数 */
(UBaseType_t )START_TASK_PRIO, /*任务优先级*/
(StackType_t* )StartTaskStack, /*任务堆栈*/
(StaticTask_t* )&StartTaskTCB); /*任务控制块*/
/* 开启任务调度 */
vTaskStartScheduler();
}/
/* 开启任务调度 */
vTaskStartScheduler();
}
//静态任务函数
void start_task(void *pvParameters)
{
/* do something */
/* 删除开始任务 */
vTaskDelete(StartTask_Handler);
}