1 任务创建和删除
函数 | 描述 |
xTaskCreate() | 动态创建一个任务 |
xTaskCreateStatic() | 静态创建一个任务 |
xTaskCreateRestricted() | 创建一个使用MPU进行限制的任务,相关内存使用动态内存分配 |
vTaskDelete() | 删除一个任务 |
1.1 静态创建的时候会出现两个未定义错误vApplicationGetTimerTaskMemory和vApplicationGetIdleTaskMemory
我们需要自行定义这两个函数
/* 空闲中断所需任务 */
static StackType_t IdleTaskStack[configMINIMAL_STACK_SIZE];
static StaticTask_t IdleTaskTCB;
void vApplicationGetIdleTaskMemory(
StaticTask_t **ppxIdleTaskTCBBuffer,
StackType_t **ppxIdleTaskStackBuffer,
uint32_t *pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &IdleTaskTCB;
*ppxIdleTaskStackBuffer = IdleTaskStack;
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
/* 定时器所需任务*/
static StackType_t TimerTaskStack[configTIMER_TASK_STACK_DEPTH];
static StaticTask_t TimerTaskTCB;
void vApplicationGetTimerTaskMemory(
StaticTask_t **ppxTimerTaskTCBBuffer,
StackType_t **ppxTimerTaskStackBuffer,
uint32_t *pulTimerTaskStackSize)
{
*ppxTimerTaskTCBBuffer = &TimerTaskTCB;
*ppxTimerTaskStackBuffer = TimerTaskStack;
*pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}
代码
#include "FreeRTOS_Static.h"
#include "main.h"
void task1( void * pvParameters )
{
while(1)
{
LED_BLUE_TOGGLE;
vTaskDelay(200);
}
}
void task2( void * pvParameters )
{
while(1)
{
LED_RED_TOGGLE;
vTaskDelay(500);
}
}
#define START_TASK_STACK_SIZE 128
#define START_TASK_PRIO 1
StackType_t StartTaskStackBuffer[START_TASK_STACK_SIZE];
StaticTask_t StartTaskTCB;
TaskHandle_t Start_Task_Handler;
void Start_Task( void * pvParameters );
#define START_TASK1_STACK_SIZE 128
#define START_TASK1_PRIO 2
StackType_t StartTask1StackBuffer[START_TASK1_STACK_SIZE];
StaticTask_t StartTask1TCB;
TaskHandle_t Start_Task1_Handler;
void Start_Task1( void * pvParameters );
#define START_TASK2_STACK_SIZE 128
#define START_TASK2_PRIO 3
StackType_t StartTask2StackBuffer[START_TASK2_STACK_SIZE];
StaticTask_t StartTask2TCB;
TaskHandle_t Start_Task2_Handler;
void Start_Task2( void * pvParameters );
StaticTask_t IdleTaskTCB;
StackType_t IdleStackBuffer[configMINIMAL_STACK_SIZE];
void vApplicationGetIdleTaskMemory(
StaticTask_t **ppxIdleTaskTCBBuffer,
StackType_t **ppxIdleTaskStackBuffer,
uint32_t *pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &IdleTaskTCB;
*ppxIdleTaskStackBuffer = IdleStackBuffer;
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
StaticTask_t TimerTaskTCB;
StackType_t TimerTaskStackBuffer[configTIMER_TASK_STACK_DEPTH];
void vApplicationGetTimerTaskMemory(
StaticTask_t **ppxTimerTaskTCBBuffer,
StackType_t **ppxTimerTaskStackBuffer,
uint32_t *pulTimerTaskStackSize )
{
*ppxTimerTaskTCBBuffer = &TimerTaskTCB;
*ppxTimerTaskStackBuffer = TimerTaskStackBuffer;
*pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}
void FreeRTOS_Static_Demo()
{
Start_Task_Handler = xTaskCreateStatic(
(TaskFunction_t) Start_Task,
(const char *) "start_task0",
(const uint32_t) START_TASK_STACK_SIZE,
(void * )NULL,
(UBaseType_t) START_TASK_PRIO,
(StackType_t *)StartTaskStackBuffer,
(StaticTask_t *)&StartTaskTCB );
vTaskStartScheduler();
}
void Start_Task(void * pvParameters)
{
Start_Task1_Handler = xTaskCreateStatic(
(TaskFunction_t) Start_Task1,
(const char *) "start_task1",
(const uint32_t) START_TASK1_STACK_SIZE,
(void * )NULL,
(UBaseType_t) START_TASK1_PRIO,
(StackType_t *)StartTask1StackBuffer,
(StaticTask_t *)&StartTask1TCB );
Start_Task2_Handler = xTaskCreateStatic(
(TaskFunction_t) Start_Task2,
(const char *) "start_task2",
(const uint32_t) START_TASK2_STACK_SIZE,
(void * )NULL,
(UBaseType_t) START_TASK2_PRIO,
(StackType_t *)StartTask2StackBuffer,
(StaticTask_t *)&StartTask2TCB );
vTaskDelete(Start_Task_Handler);
}
void Start_Task1(void * pvParameters)
{
while(1)
{
LED_BLUE_TOGGLE;
vTaskDelay(200);
}
}
void Start_Task2(void * pvParameters)
{
while(1)
{
LED_RED_TOGGLE;
vTaskDelay(400);
}
}
1.2 动态任务创建
#include "FreeRTOS_Demo.h"
#include "main.h"
/*
创建任务
1 任务优先级
2 任务堆大小
3 任务句柄
4 任务函数
*/
#define START_TASK_PRIO 1 /* 任务优先级 */
#define START_TASK_SIZE 128 /* 任务堆大小 */
TaskHandle_t StartTask_Handler; /* 任务句柄 */
void start_task(void *pvParameters); /* 任务函数 */
/* 任务1 */
#define TASK1_PRIO 2
#define TASK1_SIZE 128
TaskHandle_t StartTask1_Handler;
void start_task1(void *pvParameters);
/* 任务2 */
#define TASK2_PRIO 2
#define TASK2_SIZE 128
TaskHandle_t StartTask2_Handler;
void start_task2(void *pvParameters);
/**
FreeRTOS例程入口函数
*/
void FreeRTOS_Demo(void)
{
xTaskCreate( (TaskFunction_t)start_task, /* 任务函数 */
(const char * )"start_task", /* 任务名称 */
(const uint16_t) START_TASK_SIZE, /* 任务堆大小 */
(void * ) NULL, /* 传给任务函数的参数 */
(UBaseType_t )START_TASK_PRIO, /* 任务优先级 */
(TaskHandle_t * )&StartTask_Handler ); /* 任务句柄 */
vTaskStartScheduler();
}
void start_task(void *pvParameters)
{
taskENTER_CRITICAL(); /* 进入临界区 */
/* 创建任务1 */
xTaskCreate( (TaskFunction_t)start_task1, /* 任务函数 */
(const char * )"start_task1", /* 任务名称 */
(const uint16_t) TASK1_SIZE, /* 任务堆大小 */
(void * ) NULL, /* 传给任务函数的参数 */
(UBaseType_t )TASK1_PRIO, /* 任务优先级 */
(TaskHandle_t * )&StartTask1_Handler ); /* 任务句柄 */
/* 创建任务2 */
xTaskCreate( (TaskFunction_t)start_task2, /* 任务函数 */
(const char * )"start_task2", /* 任务名称 */
(const uint16_t) TASK2_SIZE, /* 任务堆大小 */
(void * ) NULL, /* 传给任务函数的参数 */
(UBaseType_t )TASK2_PRIO, /* 任务优先级 */
(TaskHandle_t * )&StartTask2_Handler ); /* 任务句柄 */
vTaskDelete(StartTask_Handler); /* 删除开始任务 */
taskEXIT_CRITICAL(); /* 退出临界区 */
}
void start_task1(void *pvParameters)
{
while(1)
{
LED_BLUE_TOGGLE;
vTaskDelay(200);
}
}
void start_task2(void *pvParameters)
{
while(1)
{
LED_RED_TOGGLE;
vTaskDelay(400);
}
}