FreeRtos静态创建和删除
在第一篇博文中已经讲了FreeRtos的相关配置,以及使用了动态创建进行了多任务的演示,本次实验进行演示的是静态的创建方法,通过本次的学习,可以明显的看出动态创建的优势和方便,静态创建时还需要我们自己去构建相关函数进行配置,所以一般我们都是使用动态创建的,静态知道怎么配置就行了。
1、系统配置
使用静态方法创建任务的时候需要将宏 configSUPPORT_STATIC_ALLOCATION 设置为 1,将这个宏配置为1 后进行编译会提示有两个函数未定义,这两个函数就是需要我们自己去重写的。
宏再FreeRTOS.h头文件里面,将其配置为1设置成静态创建
#define configSUPPORT_STATIC_ALLOCATION 1//使用静态创建的时候打开为1
根据报错提示我们去查找这两个函数进行重新配置 在task.h 和timers.h头文件中
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t * pulIdleTaskStackSize );
void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
StackType_t ** ppxTimerTaskStackBuffer,
uint32_t * pulTimerTaskStackSize );
2.两个函数构建
通过这两个函数来给空闲任务和定时器服务任务的任务堆栈和任务控制块分配内存
static StaticTask_t TldeTaskTCP;
static StackType_t IdleTaskkStack[configMINIMAL_STACK_SIZE];
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t *pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &TldeTaskTCP;
*ppxIdleTaskStackBuffer = IdleTaskkStack;
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
static StaticTask_t TimerTaskTCP;
static StackType_t TimerTaskkStack[configTIMER_TASK_STACK_DEPTH];
void vApplicationGetTimerTaskMemory ( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t *pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &TimerTaskTCP;
*ppxIdleTaskStackBuffer = TimerTaskkStack;
*pulIdleTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}
3.主函数任务构建
通过task1 和task2两个任务进行多任务演示
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
//开始任务
#define START_TASK_SIZE 120
#define START_TASK_PRIO 1
TaskHandle_t StartTask_Handle;
StackType_t StartTaskStack[START_TASK_SIZE];
StaticTask_t StartTaskTCB;
void start_task( void * pvParameters );
//任务1
#define Task1_TASK_SIZE 120
#define Task1_TASK_PRIO 2
TaskHandle_t Task1Task_Handle;
StackType_t Task1TaskStack[Task1_TASK_SIZE];
StaticTask_t Task1TaskTCB;
void task1_task( void * pvParameters );
//任务2
#define Task2_TASK_SIZE 120
#define Task2_TASK_PRIO 3
TaskHandle_t Task2Task_Handle;
StackType_t Task2TaskStack[Task2_TASK_SIZE];
StaticTask_t Task2TaskTCB;
void task2_task( void * pvParameters );
static StaticTask_t TldeTaskTCP;
static StackType_t IdleTaskkStack[configMINIMAL_STACK_SIZE];
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t *pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &TldeTaskTCP;
*ppxIdleTaskStackBuffer = IdleTaskkStack;
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
static StaticTask_t TimerTaskTCP;
static StackType_t TimerTaskkStack[configTIMER_TASK_STACK_DEPTH];
void vApplicationGetTimerTaskMemory ( StaticTask_t ** ppxIdleTaskTCBBuffer,
StackType_t ** ppxIdleTaskStackBuffer,
uint32_t *pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &TimerTaskTCP;
*ppxIdleTaskStackBuffer = TimerTaskkStack;
*pulIdleTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
delay_init(); //延时函数初始化
uart_init(115200); //初始化串口
LED_Init(); //初始化LED
printf("static create test\r\n");
StartTask_Handle = xTaskCreateStatic( (TaskFunction_t)start_task,
(char * ) "start_task", /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
(uint32_t) START_TASK_SIZE,
(void *) NULL,
(UBaseType_t) START_TASK_PRIO,
(StackType_t *)StartTaskStack,
(StaticTask_t *) &StartTaskTCB);
vTaskStartScheduler(); //开启任务调度
}
void start_task( void * pvParameters )
{
Task1Task_Handle = xTaskCreateStatic( (TaskFunction_t)task1_task,
(char * ) "task1_task", /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
(uint32_t) Task1_TASK_SIZE,
(void *) NULL,
(UBaseType_t) Task1_TASK_PRIO,
(StackType_t *)Task1TaskStack,
(StaticTask_t *) &Task1TaskTCB);
Task2Task_Handle = xTaskCreateStatic( (TaskFunction_t)task2_task,
(char * ) "task2_task", /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
(uint32_t) Task2_TASK_SIZE,
(void *) NULL,
(UBaseType_t) Task2_TASK_PRIO,
(StackType_t *)Task2TaskStack,
(StaticTask_t *) &Task2TaskTCB);
vTaskDelete(StartTask_Handle);
}
//任务1功能hanshu
void task1_task( void * pvParameters )
{
while(1)
{
printf("Task1 is running\r\n");
vTaskDelay(1000);
}
}
void task2_task( void * pvParameters )
{
while(1)
{
printf("Task2 is running\r\n");
vTaskDelay(1000);
}
}
4.实验效果