FreeRTOS学习(1)---FreeRTOS队列(一)

介绍队列的基本知识,共分三部分,本文是第一部分。

基于 FreeRTOS 的应用程序由一组独立的任务构成——每个任务都是具有独立权限的小程序。这些独立的任务之间很可能会通过相互通信以提供有用的系统功能。FreeRTOS 中所有的通信与同步机制都是基于队列实现的。

队列结构体:

这个结构体在queue.c中。

<code> typedef struct QueueDefinition/<code>
<code>{/<code>
<code>int8_t *pcHead; /*< Points to the beginning of the queue storage area. *//<code>
<code>int8_t *pcTail; /*< Points to the byte at the end of the queue storage area.  Once more byte is allocated than necessary to store the queue items, this is used as a marker. *//<code>
<code>int8_t *pcWriteTo; /*< Points to the free next place in the storage area. *//<code>


<code>union /* Use of a union is an exception to the coding standard to ensure two mutually exclusive structure members don't appear simultaneously (wasting RAM). *//<code>
<code>{/<code>
<code>int8_t *pcReadFrom; /*< Points to the last place that a queued item was read from when the structure is used as a queue. *//<code>
<code>UBaseType_t uxRecursiveCallCount;/*< Maintains a count of the number of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. *//<code>
<code>} u;/<code>
<code> /<code>
<code>List_t xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. *//<code>
<code>List_t xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. *//<code>
<code> /<code>
<code>volatile UBaseType_t uxMessagesWaiting;/*< The number of items currently in the queue. *//<code>
<code>UBaseType_t uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. *//<code>
<code>UBaseType_t uxItemSize; /*< The size of each items that the queue will hold. *//<code>
<code> /<code>
<code>volatile int8_t cRxLock; /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. *//<code>
<code>volatile int8_t cTxLock; /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. *//<code>
<code> /<code>
<code>#if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )/<code>
<code>uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. *//<code>
<code>#endif/<code>
<code> /<code>
<code>#if ( configUSE_QUEUE_SETS == 1 )/<code>
<code>struct QueueDefinition *pxQueueSetContainer;/<code>
<code>#endif/<code>
<code> /<code>
<code>#if ( configUSE_TRACE_FACILITY == 1 )/<code>
<code>UBaseType_t uxQueueNumber;/<code>
<code>uint8_t ucQueueType;/<code>
<code>#endif/<code>
<code> /<code>
<code>} xQUEUE;/<code>

创建队列:

QueueHandle_t xQueueCreate (UBaseType_t uxQueueLength, UBaseType_t uxItemSize);

参数描述 usQueueLength:队列项数目 uxItemSize:每个队列项大小,单位是字节

队列项通过拷贝入队而不是通过引用入队,因此需要队列项的大小。每个队列项的大小必须相同。返回值:成功创建队列返回队列句柄,否自返回0。

向队列发送消息:

创建好队列,就可以向队列发送消息了。Freertos提供了8个发送消息的API函数,分别是:


xQueueSendToBack()用于将数据发送到队列尾;而 xQueueSendToFront()用于将数据发送到队列首。 xQueueSend()完全等同xQueueSendToBack()。

但切记不要在中断服务例程中调用 xQueueSendToFront() 或xQueueSendToBack()。系统提供中断安全版本的 xQueueSendToFrontFromISR()与

xQueueSendToBackFromISR()用于在中断服务中实现相同的功能。

函数原型:

portBASE_TYPE xQueueSendToFront( xQueueHandle xQueue, const void * pvItemToQueue,

portTickType xTicksToWait );

portBASE_TYPE xQueueSendToBack( xQueueHandle xQueue, const void * pvItemToQueue,

portTickType xTicksToWait );

xQueue 目标队列的句柄。这个句柄即是调用 xQueueCreate()创建该队

列时的返回值。

pvItemToQueue 发送数据的指针。其指向将要复制到目标队列中的数据单元。

由于在创建队列时设置了队列中数据单元的长度,所以会从该指

针指向的空间复制对应长度的数据到队列的存储区域。

xTicksToWait 阻塞超时时间。如果在发送时队列已满,这个时间即是任务处于

阻塞态等待队列空间有效的最长等待时间。


FreeRTOS学习(1)---FreeRTOS队列(一)


FreeRTOS学习(1)---FreeRTOS队列(一)


FreeRTOS学习(1)---FreeRTOS队列(一)



分享到:


相關文章: