STM32F103ZE FreeRTOS任务创建与删除

  • Post author:
  • Post category:其他


一、前期准备

单片机:STM32F103ZET6

开发环境:MDK5.14

库函数:标准库V3.5

FreeRTOS:V9.0.0(网上下载,官网上目前是V10.0.1)

二、实验效果

总共6个任务,串口1、4个RGB小灯以不同频率闪烁、一个删除任务,并通过串口2打印调试信息。删除任务每秒进行一次自加。当加到3的时候删除第一个小灯的任务,以此类推直到第四个小灯任务删除,之后再自加3次删除自身。这时候只有串口1任务在工作。

这里写图片描述

这里写图片描述

三、程序讲解

任务创建函数:此为动态内存分配,由FreeRTOS的heap4.c分配

BaseType_t xTaskCreate(

TaskFunction_t pxTaskCode, /任务函数

const char * const pcName, //任务名称

const uint16_t usStackDepth, //任务堆栈大小

void *const pvParameters, //传递给任务函数的参数

UBaseType_t uxPriority, //任务优先级

TaskHandle_t * const pxCreateTask //任务句柄,删除任务也是删除任务句柄

);

任务删除函数:

vTaskDelete(

TaskHandle_t xTaskToDelete //任务句柄

);

四、测试代码

完整工程请加QQ:1002521871,验证:呵呵 。只附上与FreeRTOS相关的代码,其余串口,LED初始化请参考

STM32F103ZE FreeRTOS移植、测试


rtos_app.h

#ifndef	__RTOS_APP_H__
#define	__RTOS_APP_H__
#include "conf.h"
#include "FreeRTOS.h"
#include "task.h"

#define		START_TASK_PRIO			1
#define		START_STK_SIZE			128
extern		TaskHandle_t 			STARTTask_Handler;
extern 		void START_task(void *pvParameters);

#define		LED0_TASK_PRIO			3
#define		LED0_STK_SIZE			50


#define		LED1_TASK_PRIO			4
#define		LED1_STK_SIZE			50


#define		LED2_TASK_PRIO			5
#define		LED2_STK_SIZE			50


#define		LED3_TASK_PRIO			6
#define		LED3_STK_SIZE			50


#define		UART1_TASK_PRIO			7
#define		UART1_STK_SIZE			50


#define		UART2_TASK_PRIO			7
#define		UART2_STK_SIZE			50

#define		Mess_TASK_PRIO			2
#define		Mess_STK_SIZE			50

#define		TEST_TASK_RPIO			7
#define		TEST_STK_SIZE			128

#endif

rtos_app.c

#include "rtos_app.h"

TaskHandle_t	STARTTask_Handler;
TaskHandle_t	LED0Task_Handler;
TaskHandle_t	LED1Task_Handler;
TaskHandle_t	LED2Task_Handler;
TaskHandle_t	LED3Task_Handler;
TaskHandle_t	UART1Task_Handler;
TaskHandle_t	UART2Task_Handler;
TaskHandle_t	MessTask_Handler;
TaskHandle_t	TESTTask_Handler;

void LED0_task(void *pvParameters)
{
	while (1)
	{
		RGB_LED1 = ON;
		vTaskDelay(200 / portTICK_RATE_MS);
		RGB_LED1 = OFF;
		vTaskDelay(800 / portTICK_RATE_MS);
	}
}

void LED1_task(void *pvParameters)
{
	while (1)
	{
		RGB_LED2 = ON;
		vTaskDelay(300 / portTICK_RATE_MS);
		RGB_LED2 = OFF;
		vTaskDelay(700 / portTICK_RATE_MS);
	}
}

void LED2_task(void *pvParameters)
{
	while (1)
	{
		RGB_LED3 = ON;
		vTaskDelay(400 / portTICK_RATE_MS);
		RGB_LED3 = OFF;
		vTaskDelay(600 / portTICK_RATE_MS);
	}
}

void LED3_task(void *pvParameters)
{
	while (1)
	{
		RGB_LED4 = ON;
		vTaskDelay(500 / portTICK_RATE_MS);
		RGB_LED4 = OFF;
		vTaskDelay(500 / portTICK_RATE_MS);
	}
}

void UART1_task(void *pvParameters)
{
	int i = 0;
	while (1)
	{
		taskENTER_CRITICAL();
		USART_PRINTF_FLAG = 1;
		printf("UART1-%05d: FreeRTOS Test!\r\n", i);
		i ++;
		taskEXIT_CRITICAL();
		vTaskDelay(1000 / portTICK_RATE_MS);
	}
}

void UART2_task(void *pvParameters)
{
	int i = 0;
	while (1)
	{
		taskENTER_CRITICAL();
		USART_PRINTF_FLAG = 2;
		printf("UART2-%05d: FreeRTOS Test!\r\n", i);
		i ++;
		taskEXIT_CRITICAL();
		vTaskDelay(1000 / portTICK_RATE_MS);	
	}
}

void Mess_task(void *pvParameters)
{
	while (1)
	{
		//taskENTER_CRITICAL();
		if (cmdDealFlag == 1)
		{			
			DealWithUARTMess(RecData);
			cmdDealFlag = 0;
		}
		//taskEXIT_CRITICAL();
	}
}

void TEST_task(void *pvParameters)
{
	uint8_t times = 0;
	while(1)
	{
		
		if (times == 3)
		{
			vTaskDelete(LED0Task_Handler);
			taskENTER_CRITICAL();
			USART_PRINTF_FLAG = 2;
			printf("Task_LED0 has already deleted!\r\n");
			taskEXIT_CRITICAL();
		}
		else if (times == 6)
		{
			vTaskDelete(LED1Task_Handler);
			taskENTER_CRITICAL();
			USART_PRINTF_FLAG = 2;
			printf("Task_LED1 has already deleted!\r\n");
			taskEXIT_CRITICAL();
		}
		else if (times == 9)
		{
			vTaskDelete(LED2Task_Handler);
			taskENTER_CRITICAL();
			USART_PRINTF_FLAG = 2;
			printf("Task_LED2 has already deleted!\r\n");
			taskEXIT_CRITICAL();
		}
		else if (times ==  12)
		{
			vTaskDelete(LED3Task_Handler);
			taskENTER_CRITICAL();
			USART_PRINTF_FLAG = 2;
			printf("Task_LED3 has already deleted!\r\n");
			taskEXIT_CRITICAL();
		}
		else if (times == 15)
		{
			taskENTER_CRITICAL();
			USART_PRINTF_FLAG = 2;
			printf("Test task has already deleted!\r\n");
			taskEXIT_CRITICAL();
			vTaskDelete(TESTTask_Handler);
			taskEXIT_CRITICAL();
		}
		taskENTER_CRITICAL();
		USART_PRINTF_FLAG = 2;
		printf("Times = %d\r\n", times);
		taskEXIT_CRITICAL();
		times ++;
		vTaskDelay(1000 / portTICK_RATE_MS);	
	}
	//vTaskDelete(NULL);
}

void START_task(void *pvParameters)
{
	taskENTER_CRITICAL();
	xTaskCreate(
		(TaskFunction_t) TEST_task,
		(const char *) "TEST_task",
		(uint16_t) TEST_STK_SIZE,
		(void *) NULL,
		(UBaseType_t) TEST_TASK_RPIO,
		(TaskHandle_t *) &TESTTask_Handler
	);
		
	xTaskCreate(
		(TaskFunction_t) LED0_task,
		(const char *) "LED0_task",
		(uint16_t) LED0_STK_SIZE,
		(void *) NULL,
		(UBaseType_t) LED0_TASK_PRIO,
		(TaskHandle_t *) &LED0Task_Handler
	);
		
	xTaskCreate(
		(TaskFunction_t) LED1_task,
		(const char *) "LED1_task",
		(uint16_t) LED1_STK_SIZE,
		(void *) NULL,
		(UBaseType_t) LED1_TASK_PRIO,
		(TaskHandle_t *) &LED1Task_Handler
	);
		
	xTaskCreate(
		(TaskFunction_t) LED2_task,
		(const char *) "LED2_task",
		(uint16_t) LED2_STK_SIZE,
		(void *) NULL,
		(UBaseType_t) LED2_TASK_PRIO,
		(TaskHandle_t *) &LED2Task_Handler
	);
		
	xTaskCreate(
		(TaskFunction_t) LED3_task,
		(const char *) "LED3_task",
		(uint16_t) LED3_STK_SIZE,
		(void *) NULL,
		(UBaseType_t) LED3_TASK_PRIO,
		(TaskHandle_t *) &LED3Task_Handler
	);
		
	xTaskCreate(
		(TaskFunction_t) UART1_task,
		(const char *) "UART1_task",
		(uint16_t) UART1_STK_SIZE,
		(void *) NULL,
		(UBaseType_t) UART1_TASK_PRIO,
		(TaskHandle_t *) &UART1Task_Handler
	);
//		
//	xTaskCreate(
//		(TaskFunction_t) UART2_task,
//		(const char *) "UART2_task",
//		(uint16_t) UART2_STK_SIZE,
//		(void *) NULL,
//		(UBaseType_t) UART2_TASK_PRIO,
//		(TaskHandle_t *) &UART2Task_Handler
//	);
		
	xTaskCreate(
		(TaskFunction_t) Mess_task,
		(const char *) "Mess_task",
		(uint16_t) Mess_STK_SIZE,
		(void *) NULL,
		(UBaseType_t) Mess_TASK_PRIO,
		(TaskHandle_t *) &MessTask_Handler
	);
	
	vTaskDelete(STARTTask_Handler);
	taskEXIT_CRITICAL();
}

由于作者能力有限,有不妥之处欢迎指正,邮箱alex_hua@foxmail.com



版权声明:本文为huazhen1234原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。