队列创建
宏定义:configSUPPORT_STATIC_ALLOCATION
//静态创建 QueueHandle_t xQueueCreateStatic( UBaseType_t uxQueueLength, UBaseType_t uxItemSize, uint8_t *pucQueueStorageBuffer, StaticQueue_t *pxQueueBuffer ); /* The queue is to be created to hold a maximum of 10 uint64_t variables. */ #define QUEUE_LENGTH 10 #define ITEM_SIZE sizeof( uint64_t ) /* The variable used to hold the queue's data structure. */ static StaticQueue_t xStaticQueue; /* The array to use as the queue's storage area. This must be at least (uxQueueLength * uxItemSize) bytes. 至少队列最大长度,用于保存队列数据*/ uint8_t ucQueueStorageArea[ QUEUE_LENGTH * ITEM_SIZE ]; QueueHandle_t xQueue; /* Create a queue capable of containing 10 uint64_t values. */ xQueue = xQueueCreateStatic( QUEUE_LENGTH, ITEM_SIZE, ucQueueStorageArea, &xStaticQueue );
//动态创建:FreeRTOS进行RAM分配队列存储空间 /* Define the data type that will be queued. */ typedef struct A_Message { char ucMessageID; char ucData[ 20 ]; } AMessage; /* Define the queue parameters. */ #define QUEUE_LENGTH 5 #define QUEUE_ITEM_SIZE sizeof( AMessage ) xQueue = xQueueCreate( QUEUE_LENGTH, QUEUE_ITEM_SIZE ); //创建队列,不需要提供队列数据存储地方
vQueueDelete( xQueue ); //队列删除
队列接收
//队列数据接收 BaseType_t xQueueReceive( QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait ); /* Create a task, passing in the queue handle as the task parameter. */ xTaskCreate( vAnotherTask, “Task”, STACK_SIZE, ( void * ) xQueue, /* The queue handle is used as the task parameter. */ TASK_PRIORITY, NULL ); void vAnotherTask( void *pvParameters ) { QueueHandle_t xQueue; AMessage xMessage; /* The queue handle is passed into this task as the task parameter. Cast the void * parameter back to a queue handle. */ xQueue = ( QueueHandle_t ) pvParameters; for( ;; ) { /* Wait for the maximum period for data to become available on the queue. The period will be indefinite if INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. */ if( xQueueReceive( xQueue, &xMessage, portMAX_DELAY ) != pdPASS ) { /* Nothing was received from the queue – even after blocking to wait for data to arrive. */ } else { /* xMessage now contains the received data. */ } } } BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, void *pvBuffer, BaseType_t *pxHigherPriorityTaskWoken ); /*Note 1. xQueueReceiveFromISR()调用能使任务从block状态解除 2. xQueueReceiveFromISR()只能指明是否进行一个上下文切换请求 3. 不能在任务调度之前调用,即任务调度之前就能响应的中断不能调用该函数接口 */BaseType_t xQueueReset( QueueHandle_t xQueue );
//重置队列 队列中所有数据都将被丢弃
队列发送:
BaseType_t xQueueSend( QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait ); BaseType_t xQueueSendToFront( QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait ); BaseType_t xQueueSendToBack( QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait ); //xQueueSend() and xQueueSendToBack() perform the same operation so are equivalent. BaseType_t xQueueSendFromISR( QueueHandle_t xQueue, const void *pvItemToQueue, BaseType_t *pxHigherPriorityTaskWoken ); BaseType_t xQueueSendToBackFromISR( QueueHandle_t xQueue, const void *pvItemToQueue, BaseType_t *pxHigherPriorityTaskWoken ); BaseType_t xQueueSendToFrontFromISR( QueueHandle_t xQueue, const void *pvItemToQueue, BaseType_t *pxHigherPriorityTaskWoken );
队列查询:
//队列空查询 BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t pxQueue ); //队列full查询 BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t pxQueue ); //查询队列中有多少项 UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue ); UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue ); //队列overwrite操作 主要用于队列长度为1 的队列,表示一种状态 BaseType_t xQueueOverwrite( QueueHandle_t xQueue, const void *pvItemToQueue ); BaseType_t xQueueOverwriteFromISR( QueueHandle_t xQueue, const void *pvItemToQueue, BaseType_t *pxHigherPriorityTaskWoken ); /*pxHigherPriorityTaskWoken:如果overwrite操作导致一个block任务为unblock,且优先级比当前task高,将pxHigherPriorityTaskWoken设置为TRUE*/ //读取数据,但不将该数据从队列中移除 返回一个项目的数据 BaseType_t xQueuePeek( QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait );
BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue, void *pvBuffer );
队列集:
//队列集创建: xActivatedMember = xQueueSelectFromSet( xQueueSet, pdMS_TO_TICKS( 200 ) ); //从队列集中移除某项: if( xQueueRemoveFromSet( xQueue, xQueueSet ) != pdPASS )
//从队列集中读取数据QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet, const TickType_t xTicksToWait ); QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet );
标签:const,BaseType,队列,void,QueueHandle,xQueue From: https://www.cnblogs.com/zypprocess/p/18296656