当前位置:网站首页>FreeRTOS(九)——队列
FreeRTOS(九)——队列
2022-06-29 09:10:00 【水似冰】
在编写项目应用时,常常会遇到一个任务和另一个任务进行“沟通交流”的情况,在没有操作系统时,全局变量可以解决这个问题,但是如果在使用操作系统的应用中用全部变量来传递信息就会涉及到“资源管理”的问题,而且全局变量不易维护,往往逻辑复杂的程序中,无法追踪全局变量被谁使用或被谁更改。 FreeRTOS对此提供一个叫做“队列”的机制。
本文分为如下几部分:
- 队列简介
- 队列结构体
- 队列创建
- 向队列发送消息
- 队列上锁和解锁
- 从队列读取信息
队列简介
数据存储
队列采用先进先出(FIFO)的存储缓冲机制。数据发送到队列中会导致数据拷贝,也就是将要发送的数据拷贝到队列中,这意味着在队列中存储的是数据原始值,也就是值传递。虽然这样会浪费一点时间,但是一旦将消息发送到队列中原始的数据缓冲区就可以删除或覆写,FreeRTOS中队列传递消息虽然使用的数据拷贝,但也可以使用引用来传递消息。
多任务访问
任何任务都可以向队列中发送消息,或从队列中提取消息。
出队阻塞
当任务尝试从一个队列中读取消息的时候可以指定一个阻塞时间,这个阻塞时间就是当任务从队列中读取消息无效的时候任务阻塞的时间。
比如说队列Q是空的,这是A任务来读取,此时A又三种选择:
- 不等待
- 等待一段时间
- 死等
选哪一个由阻塞时间决定
入队阻塞
入队是指向队列中发送消息,将消息加入到队列中,和出队阻塞一样,当一个任务向队列中发送消息的话也可以设置阻塞时间。
队列操作过程




队列结构体
结构体定义在queue.c中:
typedef struct QueueDefinition
{
int8_t *pcHead;
int8_t *pcWriteTo;
{
int8_t *pcReadFrom;
UBaseType_t uxRecursiveCallCount;
} u;
List_t xTasksWaitingToReceive;
UBaseType_t uxLength;
volatile int8_t cRxLock;
#if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
uint8_t ucStaticallyAllocated;
#endif
#if ( configUSE_QUEUE_SETS == 1 )
struct QueueDefinition *pxQueueSetContainer;
#endif
#if ( configUSE_TRACE_FACILITY == 1 )
UBaseType_t uxQueueNumber;
uint8_t ucQueueType;
#endif
} xQUEUE;
/* The old xQUEUE name is maintained above then typedefed to the new Queue_t name below to enable the use of older kernel aware debuggers. */
typedef xQUEUE Queue_t;
队列创建
创建函数
使用队列之前必须先创建,创建的方法有两种,一种是静态的,另一种是动态的:
xQueueCreateStatic()xQueueCreate()
这两个函数本质上都是宏,真正完成队列创建的函数是xQueueGenericCreate()和xQueueGenericCreateStatic()。
函数xQueueCreateStatic()
该函数使用静态方法创建队列,队列所需的内存由用户自行分配。
QueueHandle_t xQueueCreateStatic(
UBaseType_t uxQueueLength,
UBaseType_t uxItemSiz,
uint8_t* pucQueueStorageBuffer,
StaticQueue_t* pxQueueBuffer
)
| 参数 | 作用 |
|---|---|
| uxQueueLength | 要创建队列的长度 |
| uxItemSiz | 队列中每个消息的长度,单位为字节 |
| pucQueueStorageBuffer | 指向队列消息的存储区(自行分配),必须指向一个uint8_t类型的数组,这个存储区长度要大于等于(uxQueueLength *uxItemSiz )字节 |
| pxQueueBuffer | 指向一个StaticQueue_t的变量,用来保存队列结构体 |
| 返回值 | |
|---|---|
| 其他值 | 队列创建成功的句柄 |
| NULL | 队列创建失败 |
函数xQueueCreate()
QueueHandle_t xQueueCreate(UBaseType_t uxQueueLength,
UBaseType_t uxItemSiz,)
| 参数 | 作用 |
|---|---|
| uxQueueLength | 要创建队列的长度 |
| uxItemSiz | 队列中每个消息的长度,单位为字节 |
| 返回值 | |
|---|---|
| 其他值 | 队列创建成功的句柄 |
| NULL | 队列创建失败 |
函数xQueueGenericCreateStatic()
QueueHandle_t xQueueGenericCreateStatic(
const UBaseType_t uxQueueLength,
const UBaseType_t uxItemSize,
uint8_t *pucQueueStorage,
StaticQueue_t *pxStaticQueue,
const uint8_t ucQueueType
)
| 参数 | 作用 |
|---|---|
| uxQueueLength | 要创建队列的长度 |
| uxItemSiz | 队列中每个消息的长度,单位为字节 |
| pucQueueStorage | 指向队列消息的存储区(自行分配),必须指向一个uint8_t类型的数组,这个存储区长度要大于等于(uxQueueLength *uxItemSiz )字节 |
| pxStaticQueue | 指向一个StaticQueue_t的变量,用来保存队列结构体 |
| ucQueueType | 队列类型 |
| 返回值 | |
|---|---|
| 其他值 | 队列创建成功的句柄 |
| NULL | 队列创建失败 |
函数xQueueGenericCreate()
QueueHandle_t xQueueGenericCreate(
const UBaseType_t uxQueueLength,
const UBaseType_t uxItemSize,
const uint8_t ucQueueType
)
| 参数 | 作用 |
|---|---|
| uxQueueLength | 要创建队列的长度 |
| uxItemSiz | 队列中每个消息的长度,单位为字节 |
| ucQueueType | 队列类型,默认为普通的消息队列 |
| 队列类型 | |
|---|---|
| queueQUEUE_TYPE_BASE | 普通消息队列 |
| queueQUEUE_TYPE_SET | 队列集 |
| queueQUEUE_TYPE_MUTEX | 互斥信号量 |
| queueQUEUE_TYPE_COUNTUNG_SEMAPHORE | 计数型信号量 |
| queueQUEUE_TYPE_BINARY_SEMAPHORE | 二值信号量 |
| queueQUEUE_TYPE_RECURSIVE_MUTEX | 递归互斥信号量 |
| 返回值 | |
|---|---|
| 其他值 | 队列创建成功的句柄 |
| NULL | 队列创建失败 |
向队列发送消息
队列上锁和解锁
从队列读取信息
边栏推荐
- Mysql5.7 installation tutorial in centos7 under Linux
- Data governance: Metadata Management (Part 2)
- Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages
- Visual assist plug-in settings for UE4 vs
- 通用分页框架
- Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear
- Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.
- 数据可视化:数据可视化的意义
- 2020-09-25 boost库的noncopyable,用于单例模式
- Simplicity studio does not recognize the new JLINK V9 solution
猜你喜欢

How to traverse objects in the vector container

基于keil5自动配置stm32f103标准库的官网freertos移植

基于PyQt5和Qt Designer的简易加法计算器的制作

Student addition / deletion gaih

转载 :判断对象是否具有属性的5种方法

Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer

Data governance: data standard management (Part III)

Five heart charity matchmaker team

Segmentation of Head and Neck Tumours Using Modified U-net

Have you done the network security "physical examination" this year?
随机推荐
Custom MVC framework implementation
Set up lamp environment under cenos7
通识篇:原型设计的认知,设计及最佳实践
CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
Application of decorator mode, packaging ServletRequest and adding addparameter method
阿里云服务器安装配置redis,无法远程访问
Mysql5.7 installation tutorial in centos7 under Linux
1424. 对角线遍历 II
Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail
Slider validation code
Hystrix熔断器:服务熔断与服务降级
367. effective complete square dichotomy
How to set Google Chrome as the default browser
linux环境下安装配置redis,并设置开机自启动
After installing anaconda, you need to enter a password to start jupyterlab
Self cultivation (XXI) servlet life cycle, service method source code analysis, thread safety issues
Cloud management platform: 9 open source cloud management platforms (CMP)
The former security director of Uber faced fraud allegations and had concealed data leakage incidents
Gd32f4xx Ethernet Chip (ENC28J60) Drive transplantation
Mac mysql数据库基本操作