FreeRTOS(2)——动态任务创建和删除

一、动态创建任务函数

BaseType_t xTaskCreat
(
   TaskFuntion_t pxTaskCode, //指向任务函数的指针
const char*const pcName, //任务名字
   const configSTACK_DEPTH_TYPE usStackDepth, //任务堆栈大小
   void*const pvParameters, //传递给任务函数的参数
   UBaseType_t uxPriority, //任务优先级
   TaskHandle_t*const pxCreateTask //任务句柄


)
返回值描述
pdPASS任务创建成功
errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY任务创建失败

2、实现动态创建任务流程

(1)将宏configSUPPORT_DYNAMIC_ALLOCATION配置为1

(2)定义函数入口参数

(2)定义函数入口参数

函数创建的任务会立刻进入就绪态,由任务调度器调度运行

二、代码实现

#include "FreeRTOS_Demo.h"
#include "FreeRTOS.h"
#include "task.h"

#define START_TASK_PRIO 1 //优先级
#define START_TASK_STACK_SIZE 128 //堆栈大小
TaskHandle_t start_task_handler; //任务句柄

void start_task(void * pvParameters);

#define TASK1_PRIO 2 //优先级
#define TASK1_STACK_SIZE 128 //堆栈大小
TaskHandle_t task1_handler; //任务句柄
void task1(void * pvParameters);

#define TASK2_PRIO 3 //优先级
#define TASK2_STACK_SIZE 128 //堆栈大小
TaskHandle_t task2_handler; //任务句柄
void task2(void * pvParameters);

#define TASK3_PRIO 4 //优先级
#define TASK3_STACK_SIZE 128 //堆栈大小
TaskHandle_t task3_handler; //任务句柄
void task3(void * pvParameters);

uint8_t key_unkow = 0;

void freertos_demo(void)
{
xTaskCreate((TaskFunction_t)   start_task, //指向任务函数的指针
(char*) "start_task", //任务名字
(configSTACK_DEPTH_TYPE) START_TASK_STACK_SIZE, //任务堆栈大小
(void* ) NULL, //传递给任务函数的参数
(UBaseType_t ) START_TASK_PRIO, //任务优先级
(TaskHandle_t*) &start_task_handler //任务句柄
);


vTaskStartScheduler();//开启任务调度


}

void start_task(void * pvParameters)
{
xTaskCreate((TaskFunction_t)   task1, //指向任务函数的指针
(char*) "task1", //任务名字
(configSTACK_DEPTH_TYPE) TASK1_STACK_SIZE, //任务堆栈大小
(void* ) NULL, //传递给任务函数的参数
(UBaseType_t ) TASK1_PRIO, //任务优先级
(TaskHandle_t*) &task1_handler //任务句柄
);

xTaskCreate((TaskFunction_t)   task2, //指向任务函数的指针
(char*) "task2", //任务名字
(configSTACK_DEPTH_TYPE) TASK2_STACK_SIZE, //任务堆栈大小
(void* ) NULL, //传递给任务函数的参数
(UBaseType_t ) TASK2_PRIO, //任务优先级
(TaskHandle_t*) &task2_handler //任务句柄
);

xTaskCreate((TaskFunction_t)   task3, //指向任务函数的指针
(char*) "task3", //任务名字
(configSTACK_DEPTH_TYPE) TASK3_STACK_SIZE, //任务堆栈大小
(void* ) NULL, //传递给任务函数的参数
(UBaseType_t ) TASK3_PRIO, //任务优先级
(TaskHandle_t*) &task3_handler //任务句柄
);

vTaskDelete(NULL);

}

//任务1,蓝灯每500ms翻转一次
void task1(void * pvParameters)
{
while(1)
{
LED1_TOGGLE();
vTaskDelay(500);
}

}

//任务2,绿灯每250ms翻转一次
void task2(void * pvParameters)
{
while(1)
{
LED0_TOGGLE();
vTaskDelay(250);
}


}


//任务3,按下按键删除任务1,不让蓝灯灯翻转
void task3(void * pvParameters)
{
uint8_t key = 0;

while(1)
{
key = Key_GetNum();
if(key == 2 && key_unkow == 0)
{
if(task1_handler != NULL)
{
vTaskDelete(task1_handler);
key_unkow = 1;

}


}

vTaskDelay(10);
}


}

三、任务控制块结构体

typedef struct tskTaskControlBlock
  {
volatile StackType_t *pxTopOfStack; //任务栈栈顶,必须为TCB

#if ( portUSING_MPU_WRAPPERS == 1 )
xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
#endif

ListItem_t xStateListItem; //任务状态列表项
ListItem_t xEventListItem; //任务事件列表项
UBaseType_t uxPriority; //任务优先级,数值越大,优先级越大
StackType_t *pxStack; //任务栈起始地址
char pcTaskName[ configMAX_TASK_NAME_LEN ];//任务名字

#if ( portSTACK_GROWTH > 0 )
StackType_t *pxEndOfStack; /*< Points to the end of the stack on architectures where the stack grows up from low memory. */
#endif

#if ( portCRITICAL_NESTING_IN_TCB == 1 )
UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
#endif

#if ( configUSE_TRACE_FACILITY == 1 )
UBaseType_t uxTCBNumber; /*< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
#endif

#if ( configUSE_MUTEXES == 1 )
UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
UBaseType_t uxMutexesHeld;
#endif

#if ( configUSE_APPLICATION_TASK_TAG == 1 )
TaskHookFunction_t pxTaskTag;
#endif

#if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
#endif

#if( configGENERATE_RUN_TIME_STATS == 1 )
uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
#endif

#if ( configUSE_NEWLIB_REENTRANT == 1 )
/* Allocate a Newlib reent structure that is specific to this task.
Note Newlib support has been included by popular demand, but is not
used by the FreeRTOS maintainers themselves. FreeRTOS is not
responsible for resulting newlib operation. User must be familiar with
newlib and must provide system-wide implementations of the necessary
stubs. Be warned that (at the time of writing) the current newlib design
implements a system-wide malloc() that must be provided with locks. */
struct _reent xNewLib_reent;
#endif

#if( configUSE_TASK_NOTIFICATIONS == 1 )
volatile uint32_t ulNotifiedValue;
volatile uint8_t ucNotifyState;
#endif

/* See the comments above the definition of
tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
#if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
#endif

#if( INCLUDE_xTaskAbortDelay == 1 )
uint8_t ucDelayAborted;
#endif

} tskTCB;
文末附加内容
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇