freeRTOS学习day2任务创建(静态创建)

静态创建相比较更加麻烦

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "task.h"

#define START_TASK_PRIO 1  //任务优先级
#define START_STK_SIZE  120  //任务堆栈
StackType_t StartTaskStack[START_STK_SIZE];
StaticTask_t StartTaskTCB;
TaskHandle_t Start_Handler;
void start_task(void *pvParameters);

#define Task1_TASK_PRIO 2  //任务优先级
#define Task1_STK_SIZE  120  //任务堆栈
StackType_t Task1Stack[START_STK_SIZE];
StaticTask_t Task1TCB;
TaskHandle_t Task1_Handler;
void task1_task(void *pvParameters);

#define Task2_TASK_PRIO 3  //任务优先级
#define Task2_STK_SIZE  120  //任务堆栈
StackType_t Task2Stack[START_STK_SIZE];
StaticTask_t Task2TCB;
TaskHandle_t Task2_Handler;
void task2_task(void *pvParameters);

#define Task3_TASK_PRIO 4  //任务优先级
#define Task3_STK_SIZE  120  //任务堆栈
StackType_t Task3Stack[START_STK_SIZE];
StaticTask_t Task3TCB;
TaskHandle_t Task3_Handler;
void task3_task(void *pvParameters);


void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );



//*******************下面注释掉的代码是通过 位带 操作实现IO口控制**************************************
//空闲任务
static StackType_t IdleTaskStack[configMINIMAL_STACK_SIZE];
static StaticTask_t IdleTaskTCB;
//定时器任务
static StackType_t TimerTaskStack[configTIMER_TASK_STACK_DEPTH];
static StaticTask_t TimerTaskTCB;
//空闲任务所需内存
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
	*ppxIdleTaskTCBBuffer=&IdleTaskTCB;
	*ppxIdleTaskStackBuffer=IdleTaskStack;
	*pulIdleTaskStackSize=configMINIMAL_STACK_SIZE;
}


//定时器任务所需内存
void vApplicationGetTimerTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
	*ppxIdleTaskTCBBuffer=&TimerTaskTCB;
	*ppxIdleTaskStackBuffer=TimerTaskStack;
	*pulIdleTaskStackSize=configTIMER_TASK_STACK_DEPTH;
}
int main(void)
{ 
  uart_init(115200);
	delay_init(168);		  //初始化延时函数
	LED_Init();		        //初始化LED端口
  while(1)
	{
		xTaskCreateStatic(	(TaskFunction_t) start_task,
									      ( char * ) "start_task",
									      ( uint32_t) START_STK_SIZE,
									      (void *)  NULL,
									      (UBaseType_t) START_TASK_PRIO,
									      (StackType_t *) StartTaskStack,
									      (StaticTask_t *) &StartTaskTCB);
			
	   vTaskStartScheduler();//   开启任务调度器  注意一定要开启
	 }
}


//创建开始任务
void start_task(void *pvParameters)
{
	//创建Task1
	xTaskCreateStatic(	(TaskFunction_t) task1_task,
									      ( char * ) "task1_task",
									      ( uint32_t) Task1_STK_SIZE,
									      (void *)  NULL,
									      (UBaseType_t) Task1_TASK_PRIO,
									      (StackType_t *) Task1Stack,
									      (StaticTask_t *) &Task1TCB);
 //创建Task2
 xTaskCreateStatic(	(TaskFunction_t) task2_task,
									      ( char * ) "task2_task",
									      ( uint32_t) Task2_STK_SIZE,
									      (void *)  NULL,
									      (UBaseType_t) Task2_TASK_PRIO,
									      (StackType_t *) Task2Stack,
									      (StaticTask_t *) &Task2TCB);
 //创建Task3
 xTaskCreateStatic(	(TaskFunction_t) task3_task,
									      ( char * ) "task3_task",
									      ( uint32_t) Task3_STK_SIZE,
									      (void *)  NULL,
									      (UBaseType_t) Task3_TASK_PRIO,
									      (StackType_t *) Task3Stack,
									      (StaticTask_t *) &Task3TCB);
 vTaskDelete(Start_Handler);							 

}

void task1_task(void *pvParameters)
{
	while(1)
	{
		printf("task 1 is ok\r\n");
		vTaskDelay(500);
	}
	
}
void task2_task(void *pvParameters)
{
	while(1)
	{
		printf("task 2 is ok\r\n");
		vTaskDelay(800);
	}
	
}
void task3_task(void *pvParameters)
{
	while(1)
	{
		printf("task 3 is ok\r\n");
		vTaskDelay(1000);
	}
}






你可能感兴趣的:(学习)