1. 程式人生 > 實用技巧 >FreeRTOS 學習筆記3——Queue

FreeRTOS 學習筆記3——Queue

FreeRTOS Queue Note

FreeRTOS Queue Note
常用API
xQueueCreate, 佇列建立
xQueueSendToFront and xQueueSendToBack()
xQueueReceive,佇列接收
uxQueueMessagesWaiting, 查詢佇列中有多少個佇列元素
接收多個queue
xQueueCreateSet
xQueueAddToSet
examples
MailBox
API
examplesTOC

常用API

xQueueCreate, 佇列建立

QueueHandle_t xQueueCreate( UBaseType_t uxQueueLength, UBaseType_t uxItemSize );

xQueueSendToFront and xQueueSendToBack()

xQueueSendToFront, 傳送資料至佇列頭部
xQueueSendToBack,傳送資料至佇列尾部
xQueueSendxQueueSendToBack完全一致!

BaseType_t xQueueSendToFront( QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait );
BaseType_t xQueueSendToBack( QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait );

xQueueReceive,佇列接收

BaseType_t xQueueReceive( QueueHandle_t xQueue,
void * const pvBuffer,
TickType_t xTicksToWait );

uxQueueMessagesWaiting, 查詢佇列中有多少個佇列元素

UBaseType_t uxQueueMessagesWaiting( QueueHandle_t xQueue );

接收多個queue

xQueueCreateSet

/**
 * @param uxEventQueueLength Queue sets store events that occur on
 * the queues and semaphores contained in the set.  uxEventQueueLength specifies
 * the maximum number of events that can be queued at once.  To be absolutely
 * certain that events are not lost uxEventQueueLength should be set to the
 * total sum of the length of the queues added to the set, where binary
 * semaphores and mutexes have a length of 1, and counting semaphores have a
 * length set by their maximum count value.  Examples:
 *  + If a queue set is to hold a queue of length 5, another queue of length 12,
 *    and a binary semaphore, then uxEventQueueLength should be set to
 *    (5 + 12 + 1), or 18.
 *  + If a queue set is to hold three binary semaphores then uxEventQueueLength
 *    should be set to (1 + 1 + 1 ), or 3.
 *  + If a queue set is to hold a counting semaphore that has a maximum count of
 *    5, and a counting semaphore that has a maximum count of 3, then
 *    uxEventQueueLength should be set to (5 + 3), or 8.
*/
//若長度,設定成了多少個佇列,則會報錯:(prvNotifyQueueSetContainer)- assert failed!
//一定要設定成佇列元素的總個數
QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength );

xQueueAddToSet

BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore,
QueueSetHandle_t xQueueSet );
````

### xQueueSelectFromSet

```c
QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet,
const TickType_t xTicksToWait );

examples

QueueHandle_t xQueueThatContainsData;
/* Declare a variable of type QueueSetHandle_t. This is the queue set to which the
two queues are added. */
static QueueSetHandle_t xQueueSet = NULL;

static QueueHandle_t xQueue1 = NULL, xQueue2 = NULL;

xQueue1 = xQueueCreate( 1, sizeof( char * ) );
xQueue2 = xQueueCreate( 1, sizeof( char * ) );
xQueueSet = xQueueCreateSet( 1 * 2 );
xQueueAddToSet( xQueue1, xQueueSet );
xQueueAddToSet( xQueue2, xQueueSet );

const char * const pcMessage = "Message from vSenderTask1\r\n";
xQueueSend( xQueue1, &pcMessage, 0 );
xQueueSend( xQueue2, &pcMessage, 0 );

QueueHandle_t xQueueThatContainsData;
xQueueThatContainsData = ( QueueHandle_t ) xQueueSelectFromSet( xQueueSet, portMAX_DELAY );

// 若需要判斷,是哪一個佇列有資料,可以這樣:
// if (xQueueThatContainsData == xQueue1) {
//         xQueueReceive( xQueue1, &pcReceivedString, 0 );
// } else if (xQueueThatContainsData == xQueue2) {
//      xQueueReceive( xQueue2, &pcReceivedString, 0 );
// }

char *pcReceivedString;
xQueueReceive( xQueueThatContainsData, &pcReceivedString, 0 );

MailBox

A mailbox is used to hold data that can be read by any task, or any interrupt service
routine. The data does not pass through the mailbox, but instead remains in the
mailbox until it is overwritten. The sender overwrites the value in the mailbox. The
receiver reads the value from the mailbox, but does not remove the value from the
mailbox.

API

xQueueOverwrite() should only be used with queues that have a length of one.
xQueuePeek() is used to receive (read) an item from a queue without the item being removed
from the queue.

BaseType_t xQueueOverwrite( QueueHandle_t xQueue, const void * pvItemToQueue );
BaseType_t xQueuePeek( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait );

examples

/* A mailbox is a queue, so its handle is stored in a variable of type
QueueHandle_t. */
typedef struct xExampleStructure
{
    TickType_t xTimeStamp;
    uint32_t ulValue;
} Example_t;


QueueHandle_t xMailbox;
xMailbox = xQueueCreate( 1, sizeof( Example_t ) );

// mailbox send data
Example_t xData;
xQueueOverwrite( xMailbox, &xData );

// mailbox receive data
Example_t *pxData;
xQueuePeek( xMailbox, pxData, portMAX_DELAY );

/* Return pdTRUE if the value read from the mailbox has been updated since this
function was last called. Otherwise return pdFALSE. */
if( pxData->xTimeStamp > xPreviousTimeStamp )
{
    xDataUpdated = pdTRUE;
}
else
{
    xDataUpdated = pdFALSE;
}


來自為知筆記(Wiz)