14_队列集

队列集

队列集简介

image-20240328151420716

队列集相关API函数介绍

image-20240328151448059

创建队列集

image-20240328151547760

往队列集中添加队列

image-20240328151625970

从队列集中移除队列

image-20240328151750746

获取队列集中有有效消息的队列

image-20240328151841947

队列集操作实验

image-20240328151856559

代码

freertos_demo.c

/**
 ****************************************************************************************************
 * @file        freertos.c
 * @author      正点原子团队(ALIENTEK)
 * @version     V1.4
 * @date        2022-01-04
 * @brief       FreeRTOS 移植实验
 * @license     Copyright (c) 2020-2032, 广州市星翼电子科技有限公司
 ****************************************************************************************************
 * @attention
 *
 * 实验平台:正点原子 探索者F407开发板
 * 在线视频:www.yuanzige.com
 * 技术论坛:www.openedv.com
 * 公司网址:www.alientek.com
 * 购买地址:openedv.taobao.com
 *
 ****************************************************************************************************
 */

#include "freertos_demo.h"
#include "./SYSTEM/usart/usart.h"
#include "./BSP/LED/led.h"
#include "./BSP/LCD/lcd.h"
#include "./BSP/KEY/key.h"
#include "./SYSTEM/delay/delay.h"
#include "./MALLOC/malloc.h"
/*FreeRTOS*********************************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/******************************************************************************************************/
/*FreeRTOS配置*/

/* START_TASK 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define START_TASK_STACK_SIZE 	128
#define START_TASK_PRIO			1
TaskHandle_t start_task_handler;

#define TASK1_STACK_SIZE 		128
#define TASK1_PRIO				2
TaskHandle_t task1_handler;

#define TASK2_STACK_SIZE 		128
#define TASK2_PRIO				3
TaskHandle_t task2_handler;

QueueSetHandle_t queue_set_handle;
QueueHandle_t queue_handle;
QueueHandle_t semphr_handle;

void start_task( void * pvParameters );

void task1( void * pvParameters )
{
	uint8_t key = 0;
	BaseType_t err;
	while(1)
	{
		key = key_scan(0);
		if(key == KEY0_PRES)
		{
			err = xQueueSend(queue_handle, &key, portMAX_DELAY);
			if(err == pdTRUE)
			{
				printf("队列写入成功!!!\r\n");
			}
		}else if(key == KEY1_PRES)
		{
			err = xSemaphoreGive(semphr_handle);
			if(err == pdPASS)
			{
				printf("信号量释放成功!!!\r\n");
			}
		}
	}
}

void task2( void * pvParameters )
{
	QueueSetMemberHandle_t member_handle;
	uint8_t key;
	BaseType_t err;
	while(1)
	{
		member_handle = xQueueSelectFromSet(queue_set_handle, portMAX_DELAY);
		if(member_handle == queue_handle)
		{
			err = xQueueReceive(member_handle, &key, portMAX_DELAY);
			if(err == pdTRUE)
			{
				printf("queue_handle的值为:%d\r\n", key);
			}
		}else if(member_handle == semphr_handle)
		{
			err = xSemaphoreTake(member_handle, portMAX_DELAY);
			if(err == pdTRUE)
			{
				printf("semphr_handle获取信号量成功!!!\r\n");
			}
		}
	}
}

/******************************************************************************************************/

/**
 * @brief       FreeRTOS例程入口函数
 * @param       无
 * @retval      无
 */
void freertos_demo(void)
{
    xTaskCreate(start_task, "start_task", START_TASK_STACK_SIZE, NULL, START_TASK_PRIO, &start_task_handler);
	vTaskStartScheduler();
}

void start_task( void * pvParameters )
{
	taskENTER_CRITICAL(); //进入临界区
	queue_set_handle = xQueueCreateSet(2); //创建队列集,可以存放2个队列
	if(queue_set_handle != NULL)
	{
		printf("队列集创建成功!!!\r\n");
	}else
	{
		printf("队列集创建失败!!!\r\n");
	}
	queue_handle = xQueueCreate(1, sizeof(uint8_t)); //创建队列
	if(queue_handle != NULL)
	{
		printf("队列创建成功!!!\r\n");
	}else
	{
		printf("队列创建失败!!!\r\n");
	}
	semphr_handle = xSemaphoreCreateBinary(); //创建二值信号量
	if(semphr_handle != NULL)
	{
		printf("二值信号量创建成功!!!\r\n");
	}else
	{
		printf("二值信号量创建失败!!!\r\n");
	}
	xQueueAddToSet(queue_handle, queue_set_handle); //添加队列到队列集
	xQueueAddToSet(semphr_handle, queue_set_handle); ////添加二值信号量到队列集
	xTaskCreate(task1, "task1", TASK1_STACK_SIZE, NULL, TASK1_PRIO, &task1_handler);
	xTaskCreate(task2, "task2", TASK2_STACK_SIZE, NULL, TASK2_PRIO, &task2_handler);
    vTaskDelete(NULL);
	taskEXIT_CRITICAL(); //退出临界区
}

队列集使用流程

image-20240328151914790

posted @ 2024-03-28 20:26  爱吃冰激凌的黄某某  阅读(4)  评论(0编辑  收藏  举报