概 述
基本原理
单消息代码描述
将数据发送到流缓冲区的代码:
xMessageBufferSend()
{
/* If a time out is specified and there isn't enough
space in the message buffer to send the data, then
enter the blocked state to wait for more space. */
if( time out != 0 )
{
while( there is insufficient space in the buffer &&
not 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 buffer
sbSEND_COMPLETED()
}
}
从流缓冲区读取数据的代码:
xMessageBufferReceive()
{
/* If a time out is specified and the buffer doesn't
contain any data that can be read, then enter the
blocked state to wait for the buffer to contain data. */
if( time out != 0 )
{
while( there is no data in the buffer &&
not timed out waiting )
{
Enter the blocked state to wait for data
}
}
if( there is data in the buffer )
{
read data from buffer
sbRECEIVE_COMPLETED()
}
}
如图箭头所示,其中发送和接收任务位于不同的MCU内核上:
接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。
发送任务将数据写入消息缓冲区。
sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。
多消息代码描述
但是,要考虑有两个或更多消息缓冲区的情况,ISR必须首先确定哪个消息缓冲区包含数据。
如果消息缓冲区的数量很少,则有几种方法可以实现:
如果硬件允许,则每个消息缓冲区可以使用不同的中断线,从而使中断服务程序和消息缓冲区之间保持一对一的映射。
中断服务例程可以简单地查询每个消息缓冲区以查看其是否包含数据。
使用sbSEND_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 buffer
other than the control message buffer then write the handle of the message
buffer that contains data to the control message buffer, then raise an
interrupt in the other core. If this function was called because data was
written 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 buffer
is 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 the
control 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 is
blocked on the xUpdatedMessageBuffer message buffer waiting for data to
arrive. */
xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,
&xHigherPriorityTaskWoken );
}
/* Normal FreeRTOS "yield from interrupt" semantics, where
xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to
pdTRUE if the interrupt unblocks a task that has a priority above that of
the currently executing task. */
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}
中断服务例程从控制消息缓冲区中读取包含数据的消息缓冲区的句柄,然后将该句柄传递给xMessageBufferSendCompletedFromISR()API函数以取消阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再存在空的。
END
→点关注,不迷路←
文章引用微信公众号"嵌入式微处理器",如有侵权,请联系管理员删除!