FreeRTOS 学习笔记3——Queue

FreeRTOS Queue Note

TOC

常用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;
}




posted @ 2021-01-01 16:23  JerryZheng2020  阅读(1055)  评论(0编辑  收藏  举报