FREERTOS的队列使用实例

 参考正点原子的freertos的视频教程,在STM32F407上面的实验。

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "string.h"
#include "key.h"
#include "queue.h"
#include "timer.h"

//开始任务
#define StackTskSize 128
#define TskPri       1
void Start_Task( void *pvParameters);
TaskHandle_t StartTsk_Handle;

//控制led亮灭任务
#define StackTsk1Size 50
#define Tsk1Pri            2
void Start_Task1(void *pvParameters);
TaskHandle_t StartTsk1_Handle;

//按键处理任务
#define KeyProcessTskSize 80
#define KeyProcessTskPri    3
void KeyProcess_Task(void *pvParameters);
TaskHandle_t KeyProcess_Handle;

//定义队列相关
QueueHandle_t Key_Queue;
#define Key_Queue_Size (1)

QueueHandle_t Message_Queue;
#define Message_Queue_Size (4)

int main(void)
{ 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
	delay_init(168);		//初始化延时函数
	uart_init(115200);     	//初始化串口
	LED_Init();		        //初始化LED端口
	KEY_Init();
	TIM3_Int_Init(5000-1,16800-1);
	//开始任务的任务编号为0
	xTaskCreate(	(TaskFunction_t) Start_Task,  //任务函数
							  (char *        ) "Start_Task", //任务名
						  	(uint16_t      ) StackTskSize, //任务堆栈大小
							  (void *        ) NULL,         //任务参数
						  	(UBaseType_t   ) TskPri,   //任务优先级
							  (TaskHandle_t *) &StartTsk_Handle ); 
								
	vTaskStartScheduler();   //开启任务调度  //先后创建了空闲任务和定时器服务任务
}
 
void Start_Task( void *pvParameters)
{
	taskENTER_CRITICAL();
	
	//创建队列
	Key_Queue=xQueueCreate(Key_Queue_Size, sizeof(u8));
	
	Message_Queue=xQueueCreate(Message_Queue_Size,USART_REC_LEN);

	if(Key_Queue==NULL)
	{
	printf("Key_Queue create fail!!!\r\n");
	}
	
	 //创建task1
	 xTaskCreate(	(TaskFunction_t) Start_Task1,  //任务函数
							  (char *        ) "Start_Task1", //任务名
						  	(uint16_t      ) StackTsk1Size, //任务堆栈大小
							  (void *        ) NULL,         //任务参数
						  	(UBaseType_t   ) Tsk1Pri,   //任务优先级
							  (TaskHandle_t *) &StartTsk1_Handle ); 
  //查询任务
		xTaskCreate(	(TaskFunction_t)KeyProcess_Task,  //任务函数
							  (char *        ) "KeyProcess_Task", //任务名
						  	(uint16_t      ) KeyProcessTskSize, //任务堆栈大小
							  (void *        ) NULL,         //任务参数
						  	(UBaseType_t   ) KeyProcessTskPri,   //任务优先级
							  (TaskHandle_t *) &KeyProcess_Handle ); 			
    vTaskDelete(StartTsk_Handle);		//NULL就是删除当前任务。	
		taskEXIT_CRITICAL();						
}

void Start_Task1(void *pvParameters)
{ 
	u8 key;
	BaseType_t err;
  while(1)
	{	
	  key=KEY_Scan(0);
		if((Key_Queue!=NULL)&&(key))  //队列和按键都是有效的
		{
		//err=xQueueSend(Key_Queue,&key,10);     //10时阻塞时间10ms 普通队列发送
			
			err=xQueueOverwrite(Key_Queue,&key);   //覆写的方式发送
			
			if(err!= pdTRUE)
			{
			printf("Queue send failed!!\r\n");
			}
		}
	 LED0=~LED0;
	 vTaskDelay(10);
	}
	
}

void KeyProcess_Task(void *pvParameters)
{
	BaseType_t err;
	u8 key;
  while(1)
	{
		if(Key_Queue!=NULL)
    {
	  	 err=xQueueReceive(Key_Queue,&key,portMAX_DELAY);   //队列普通接收
		
		   if(err==pdTRUE) //成功获取到消息
		   {
		   printf("key=%d\r\n",key);
		   }
		}
		else
		{
	  vTaskDelay(10);
		}	
	}
	
}
#include "sys.h"
#include "usart.h"	
#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"
#include "string.h"

//如果使用ucos,则包括下面的头文件即可.
#if SYSTEM_SUPPORT_OS
#include "FreeRTOS.h"					//FreeRTOS使用	  
#endif

//加入以下代码,支持printf函数,而不需要选择use MicroLIB	  
#if 1
#pragma import(__use_no_semihosting)             
//标准库需要的支持函数                 
struct __FILE 
{ 
	int handle; 
}; 

FILE __stdout;       
//定义_sys_exit()以避免使用半主机模式    
void _sys_exit(int x) 
{ 
	x = x; 
} 
//重定义fputc函数 
int fputc(int ch, FILE *f)
{ 	
	while((USART1->SR&0X40)==0);//循环发送,直到发送完毕   
	USART1->DR = (u8) ch;      
	return ch;
}
#endif
 
#if EN_USART1_RX   //如果使能了接收
//串口1中断服务程序
//注意,读取USARTx->SR能避免莫名其妙的错误   	
u8 USART_RX_BUF[USART_REC_LEN];     //接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15,	接收完成标志
//bit14,	接收到0x0d
//bit13~0,	接收到的有效字节数目
u16 USART_RX_STA=0;       //接收状态标记	

//初始化IO 串口1 
//bound:波特率
void uart_init(u32 bound){
	//GPIO端口设置
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟
 
	//串口1对应引脚复用映射
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9复用为USART1
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1
	
	//USART1端口配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9与GPIOA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	//速度50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
	GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9,PA10

   //USART1 初始化设置
	USART_InitStructure.USART_BaudRate = bound;//波特率设置
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
	USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//收发模式
	USART_Init(USART1, &USART_InitStructure); //初始化串口1
	
	USART_Cmd(USART1, ENABLE);  //使能串口1 
	
	//USART_ClearFlag(USART1, USART_FLAG_TC);
	
#if EN_USART1_RX	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启相关中断

	//Usart1 NVIC 配置
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=6;//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;		//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器、

#endif
	
}

extern QueueHandle_t Message_Queue;   //消息队列句柄

void USART1_IRQHandler(void)                	//串口1中断服务程序
{
	u8 Res;
	BaseType_t ERR;
	BaseType_t PriorityTaskWoken=pdFALSE;
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾)
	{
		Res =USART_ReceiveData(USART1);//(USART1->DR);	//读取接收到的数据
		
		if((USART_RX_STA&0x8000)==0)//接收未完成
		{
			if(USART_RX_STA&0x4000)//接收到了0x0d
			{
				if(Res!=0x0a)
				{
					USART_RX_STA=0;//接收错误,重新开始
				}
				else 
				{
				USART_RX_STA|=0x8000;	//接收完成了 
				//向队列发送数据
				if(Message_Queue!=NULL)
				{
				ERR=xQueueSendFromISR(Message_Queue,USART_RX_BUF, &PriorityTaskWoken);		
				if(ERR==pdTRUE)
			  {
			  USART_RX_STA=0;	
        memset(USART_RX_BUF,0,USART_REC_LEN);		
				portYIELD_FROM_ISR(PriorityTaskWoken);
			  }
		  	else
			  {
		  	printf("Send Failed!!\r\n");
		  	}

				}
			}	
			}
			else //还没收到0X0D
			{	
				if(Res==0x0d)USART_RX_STA|=0x4000;
				else
				{
					USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
					USART_RX_STA++;
					if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收	  
				}		 
			}
		}   		 
	} 
} 
#endif	

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章