作者 | strongerHuang
微信公众号 | strongerHuang

概述
基本原理

单消息代码描述
xMessageBufferSend(){/* If a time out is specified and there isn't enoughspace in the message buffer to send the data, thenenter the blocked state to wait for more space. */if( time out != 0 ){while( there is insufficient space in the buffer &¬ timed out waiting ){Enter the blocked state to wait for space in the buffer}}if( there is enough space in the buffer ){write data to buffersbSEND_COMPLETED()}}
xMessageBufferReceive(){/* If a time out is specified and the buffer doesn'tcontain any data that can be read, then enter theblocked state to wait for the buffer to contain data. */if( time out != 0 ){while( there is no data in the buffer &¬ timed out waiting ){Enter the blocked state to wait for data}}if( there is data in the buffer ){read data from buffersbRECEIVE_COMPLETED()}}

多消息代码描述
-
如果硬件允许,则每个消息缓冲区可以使用不同的中断线,从而使中断服务程序和消息缓冲区之间保持一对一的映射。 -
中断服务例程可以简单地查询每个消息缓冲区以查看其是否包含数据。 -
可以通过传递元数据(消息是什么,消息的预期接收者是什么等等)以及实际数据的单个消息缓冲区来代替多个消息缓冲区。
/* Added to FreeRTOSConfig.h to override the default implementation. *//* Implemented in a C file. */void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer ){size_t BytesWritten./* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.If this function was called because data was written to any message bufferother than the control message buffer then write the handle of the messagebuffer that contains data to the control message buffer, then raise aninterrupt in the other core. If this function was called because data waswritten to the control message buffer then do nothing. */if( xUpdatedBuffer != xControlMessageBuffer ){BytesWritten = xMessageBufferSend( xControlMessageBuffer,&xUpdatedBuffer,sizeof( xUpdatedBuffer ),0 );/* If the bytes could not be written then the control message bufferis too small! */configASSERT( BytesWritten == sizeof( xUpdatedBuffer );/* Generate interrupt in the other core (pseudocode). */GenerateInterrupt();}}
void InterruptServiceRoutine( void ){MessageBufferHandle_t xUpdatedMessageBuffer;BaseType_t xHigherPriorityTaskWoken = pdFALSE;/* Receive the handle of the message buffer that contains data from thecontrol message buffer. Ensure to drain the buffer before returning. */while( xMessageBufferReceiveFromISR( xControlMessageBuffer,&xUpdatedMessageBuffer,sizeof( xUpdatedMessageBuffer ),&xHigherPriorityTaskWoken )== sizeof( xUpdatedMessageBuffer ) ){/* Call the API function that sends a notification to any task that isblocked on the xUpdatedMessageBuffer message buffer waiting for data toarrive. */xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,&xHigherPriorityTaskWoken );}/* Normal FreeRTOS "yield from interrupt" semantics, wherexHigherPriorityTaskWoken is initialised to pdFALSE and will then get set topdTRUE if the interrupt unblocks a task that has a priority above that ofthe currently executing task. */portYIELD_FROM_ISR( xHigherPriorityTaskWoken );}
