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隊列(一)



分享到:


相關文章: