FreeRTOS--邮箱

示例源码基于FreeRTOS V9.0.0

邮箱

1. 概述

一种特殊的队列,队列深度只有1,写数据的时候是覆盖写,读数据的时候,读完不会把数据从队列删除;

写数据的时候永远是成功的,读数据的时候仅第一次队列无数据时会阻塞或失败,当队列有数据后,读都会是成功的;

2. 接口API

// 写队列
#define xQueueOverwrite( xQueue, pvItemToQueue ) xQueueGenericSend( ( xQueue ), ( pvItemToQueue ), 0, queueOVERWRITE )
#define xQueueOverwriteFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken ) xQueueGenericSendFromISR( ( xQueue ), ( pvItemToQueue ), ( pxHigherPriorityTaskWoken ), queueOVERWRITE )

// 读队列
#define xQueuePeek( xQueue, pvBuffer, xTicksToWait ) xQueueGenericReceive( ( xQueue ), ( pvBuffer ), ( xTicksToWait ), pdTRUE )
BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue, void * const pvBuffer );
  • 写队列时通过宏xQueueOverwrite和xQueueOverwriteFromISR调用,其中FromISR是中断服务程序接口,仅在中断服务内使用。写队列的实际接口同普通队列操作,区别在函数参数,xTicksToWait=0,因为写永远成功,不需要阻塞;xCopyPosition=queueOVERWRITE,覆盖写方式;

  • 读队列时通过宏xQueuePeek调用,实际接口同普通队列读接口,区别在函数参数,xJustPeek=pdTRUE,表示仅读,读后不删除。中断服务程序使用的接口是xQueuePeekFromISR;

  • 创建队列同普通队列,需要注意队列深度只有1;

普通队列的相关接口API见 队列

2.1 xQueuePeekFromISR源码

BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue,  void * const pvBuffer )
{
BaseType_t xReturn;
UBaseType_t uxSavedInterruptStatus;
int8_t *pcOriginalReadPosition;
Queue_t * const pxQueue = ( Queue_t * ) xQueue;

    configASSERT( pxQueue );
    configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
    configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */

    /* RTOS ports that support interrupt nesting have the concept of a maximum
    system call (or maximum API call) interrupt priority.  Interrupts that are
    above the maximum system call priority are kept permanently enabled, even
    when the RTOS kernel is in a critical section, but cannot make any calls to
    FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h
    then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
    failure if a FreeRTOS API function is called from an interrupt that has been
    assigned a priority above the configured maximum system call priority.
    Only FreeRTOS functions that end in FromISR can be called from interrupts
    that have been assigned a priority at or (logically) below the maximum
    system call	interrupt priority.  FreeRTOS maintains a separate interrupt
    safe API to ensure interrupt entry is as fast and as simple as possible.
    More information (albeit Cortex-M specific) is provided on the following
    link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
    portASSERT_IF_INTERRUPT_PRIORITY_INVALID();

    uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
    {
        /* Cannot block in an ISR, so check there is data available. */
        if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
        {
            traceQUEUE_PEEK_FROM_ISR( pxQueue );

            /* Remember the read position so it can be reset as nothing is
            actually being removed from the queue. */
            pcOriginalReadPosition = pxQueue->u.pcReadFrom;
            prvCopyDataFromQueue( pxQueue, pvBuffer );
            pxQueue->u.pcReadFrom = pcOriginalReadPosition;

            xReturn = pdPASS;
        }
        else
        {
            xReturn = pdFAIL;
            traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
        }
    }
    portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );

    return xReturn;
}

2.2 xQueuePeekFromISR分析

img

posted @ 2023-12-02 18:33  流翎  阅读(235)  评论(0编辑  收藏  举报