FreeRTOS for STC32G12K128内核代码解读(三)队列(2)
FreeRTOS for STC32G12K128内核代码解读第三章队列(2)队列创建 -Gentleman
1.队列创建函数xQueueCreate()
queue = xQueueCreate( 100, sizeof( int ) );
跳进去发现一个宏
几个参数:
参数1:队列长度, 即有多少个项,实例中为 100个 int类型变量
参数2:项大小, 每项内存空间的大小, 直接通过sizeof 返回
参数3:队列类型
这里是创建基础的队列 不是其他的信号量
再跳一次
到函数内部了
定义了一个 Queue_t 队列(头)结构
我们观察一下这个结构
2.队列头 Queue_t
这个头用于记录队列起始地址 ,待写数据指针,联合体在下面展开,两个链表(待发送和接收),队列已存放长度,队列总长度 ,队列项大小,读写队列锁等信息
联合体展开
队列 和 信号量需要的结构,信号量先跳过
队列的结构里 存放 队列结束地址 与 最后一项指针
3.初始化队列
判断一下,是否溢出
xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize );
计算队列多少字节 分配空间用
pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */
给新队列分配内存空间
pucQueueStorage = ( uint8_t * ) pxNewQueue;
pucQueueStorage += sizeof( Queue_t ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
队列数据存储空间 在队列头后面
prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
初始化新队列
空队列(数据区域) 就吧pcHead指向 队列(头)的起始空间
非空就把pcHead 指向 队列数据存储区域
pxNewQueue->uxLength = uxQueueLength;
pxNewQueue->uxItemSize = uxItemSize;
长度和 项大小 赋值
跳进下个函数
pxQueue->u.xQueue.pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
pxQueue->pcWriteTo = pxQueue->pcHead;
pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - 1U ) * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
pxQueue->cRxLock = queueUNLOCKED;
pxQueue->cTxLock = queueUNLOCKED;
看这段代码
pcTail 指向了 队列结束
pcWriteTo 指向了第一项
pcReadFrom 指向 队列最后的项
读写锁 未锁
(可以对照队列结构图来看)
本帖最后由 gentleman 于 2024-3-17 17:34 编辑
上一期 FreeRTOS for STC32G12K128内核代码解读(三)队列(1) - FreeRTOS/实时操作系统/文件系统/嵌入式系统软件,TFT-GUI/uGFX - 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)
下一期 FreeRTOS for STC32G12K128内核代码解读(三)队列(3) - FreeRTOS/实时操作系统/文件系统/嵌入式系统软件,TFT-GUI/uGFX - 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)
占 {:4_218:}暂时还是不接触了,有点高深{:4_249:}
页:
[1]