|
使用FreeRTOS消息缓冲区,实现简单的非对称多处理(AMP)核心到核心通信(STM32H7 双核处理器)。 一、概述 实现STM32H7双核之间通信是FreeRTOS官方提供的一个方案,是基于FreeRTOS消息缓冲区,该消息缓冲区是无锁循环缓冲区,可以将大小不同的数据包从单个发送方传递到单个接收方。 说明,该消息缓冲区仅提供数据的传输,不提供通信相关协议处理。 二、基本原理 实现双核之间通信基本原理:发送和接收任务位于非对称多处理器(AMP)配置中的多核微控制器(MCU)的不同内核上,这意味着每个内核都运行自己的FreeRTOS程序。 同时,一个内核在另一个内核中具有生成中断的能力,以及两个内核都有访问的内存区域(共享内存)。消息缓冲区以每个内核上运行在应用程序已知的地址置在共享内存中,如下图: 
理想情况下,还将有一个内存保护单元(MPU),以确保只能通过内核的消息缓冲区API来访问消息缓冲区,并最好将共享内存标记为不可被其他程序占用。 三、单消息代码描述 这里官方提供了实现该方案的基础代码(仅供参考)。 将数据发送到流缓冲区的代码: - xMessageBufferSend()
# X% z* z5 \2 N' r# Z1 ~- N7 U9 ] - {
' _9 C6 T% @: R6 f+ e4 ]# S" f - /* If a time out is specified and there isn't enough
5 R3 x# u; z% Y% b4 P. ] - space in the message buffer to send the data, then
) y/ m7 k! u0 u: N3 |9 [ ?7 f - enter the blocked state to wait for more space. */' t& L! u) {- \, J0 [ f9 \
- if( time out != 0 )) S# A; r3 a# C4 Y4 O+ {, _
- {
# j' L7 W, m- M( w - while( there is insufficient space in the buffer &&) t& t, ?2 D, h* ?" w, |& b$ I
- not timed out waiting )/ P1 W3 k0 Z- ^4 g7 G
- {
N3 X! `" ]/ ?1 ~& i - Enter the blocked state to wait for space in the buffer
+ G E/ d e: T6 D, e* s3 S - }
" {7 f4 Q0 P' e7 c - }1 u+ ^) g2 r) d' f' @1 Z5 [
8 S& `4 b1 i$ x6 k5 n8 q$ t, }
复制代码- if( there is enough space in the buffer )
! N$ |! Z- |+ V) e2 O - {
, z/ ?- l5 E. \8 t; r; P - write data to buffer
4 P3 ?( m( o: `/ O - sbSEND_COMPLETED(): ]4 e3 f& ` p2 e: N! ]
- }
) `; x9 j' R* l& K. R; Q7 f; A - }
复制代码 & B) i3 A6 z+ M* U( H
从流缓冲区读取数据的代码: - xMessageBufferReceive()
+ k# Z; e. `+ ~; i2 l! s3 |6 v - {
4 ]+ a/ B( E+ y8 z/ W; h! H - /* If a time out is specified and the buffer doesn't' A8 n7 \$ k& U# H& [
- contain any data that can be read, then enter the- q+ u9 w' Q; n7 n$ i( h4 H' {
- blocked state to wait for the buffer to contain data. */
; z. w! a" `1 G( r - if( time out != 0 )8 s G/ _: C. d8 j- ~4 M3 I
- {: v1 R; I% A) W. X
- while( there is no data in the buffer &&' U V V/ ]; u; e8 A* u) b/ w
- not timed out waiting )
- a7 q! S5 e! y - {
! J6 X; T2 J7 i( F3 O - Enter the blocked state to wait for data
, W( m7 E; J$ V - }
& x* F; p# l$ \: _+ z - }
$ F# q* |7 @" o! a) C2 g& N% X
w! B$ }1 y ?! n
复制代码- if( there is data in the buffer )
- f; I; d. u, f: h0 f6 D - {
; ?" {& `" E. O* Q - read data from buffer
* h) A& R& Y' ~8 | N5 g2 D - sbRECEIVE_COMPLETED()9 v( y6 Y# r% ^
- }% K2 O/ A' @7 h
- }
复制代码 X; W# }* M8 l. ]$ ]% b! Z( R
如果任务在xMessageBufferReceive()中进入阻塞状态以等待缓冲区包含数据,则将数据发送到缓冲区必须取消阻塞该任务,以便它可以完成其操作。 当xMessageBufferSend()调用sbSEND_COMPLETED()时,任务将不受阻碍。  . z8 P- p+ i, j: a( t
ISR通过将消息缓冲区的句柄作为参数传递给xMessageBufferSendCompletedFromISR()函数来解除对任务的阻塞。 % v* A7 Z3 z0 g2 r" ]
如图箭头所示,其中发送和接收任务位于不同的MCU内核上: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 4.中断服务例程调用xMessageBufferSendCompletedFromISR()来解除阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再为空。 2 Z+ B; [. J: p/ h/ z
四、多消息代码描述 当只有一个消息缓冲区时,很容易将消息缓冲区的句柄传递到xMessageBufferSendCompletedFromISR()中。 ; O( W. \0 J/ [) r+ _ ? b
但是要考虑有两个或更多消息缓冲区的情况,ISR必须首先确定哪个消息缓冲区包含数据。如果消息缓冲区的数量很少,则有几种方法可以实现: 如果硬件允许,则每个消息缓冲区可以使用不同的中断线,从而使中断服务程序和消息缓冲区之间保持一对一的映射。 中断服务例程可以简单地查询每个消息缓冲区以查看其是否包含数据。 可以通过传递元数据(消息是什么,消息的预期接收者是什么等等)以及实际数据的单个消息缓冲区来代替多个消息缓冲区。 ( w- v& m: d P
+ ]; T* c1 a, n' e$ g
7 k/ J. b, k M但是,如果存在大量或未知的消息缓冲区,则这些技术效率不高。
' a/ e! E) K5 S% r5 I' f9 p在这种情况下,可伸缩的解决方案是引入单独的控制消息缓冲区。如下面的代码所示,sbSEND_COMPLETED()使用控制消息缓冲区将包含数据的消息缓冲区的句柄传递到中断服务例程中。 * t1 O! g5 ?6 d5 g/ s4 } j
使用sbSEND_COMPLETED()的实现: - /* Added to FreeRTOSConfig.h to override the default implementation. */
0 ?+ w. Y1 L/ O$ ]6 k: Z - #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )
0 x) m; p+ e' I# [ T5 @
复制代码- /* Implemented in a C file. */
9 J1 \0 q2 I7 Q; B W - void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer )
8 j$ N+ k, I+ E4 z2 d+ p8 t - {8 ?3 Y6 B: t5 D$ Q
- size_t BytesWritten.
# R' T. E8 y! c7 _$ a' m! k' ~
复制代码- /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.! u; v7 }7 ~7 E8 _! R
- If this function was called because data was written to any message buffer5 B6 A8 X7 ^" T, Q
- other than the control message buffer then write the handle of the message/ J z. }# W6 M2 E* e
- buffer that contains data to the control message buffer, then raise an
2 m, G. ~& I. ?8 L8 V5 h) R - interrupt in the other core. If this function was called because data was
7 W+ X- g( O; y" A$ s+ C8 U- F - written to the control message buffer then do nothing. */; p$ G X1 D! C4 l5 W
- if( xUpdatedBuffer != xControlMessageBuffer )7 f$ ^' C* ~3 D$ ~
- {& t& O& f- v. |
- BytesWritten = xMessageBufferSend( xControlMessageBuffer,8 J; H, B p" S" w+ N, c$ F M0 @
- &xUpdatedBuffer,* |' d$ U) ~, U1 a2 k$ f4 x# |1 \* g c
- sizeof( xUpdatedBuffer ),
' z3 ~! |$ w% H, x z9 ] - 0 );, y7 S. e: P3 ?+ z$ v3 g% r
复制代码- /* If the bytes could not be written then the control message buffer
9 J/ e8 }- O; R/ V# o! Y8 P4 ^ - is too small! */
) T3 d2 b9 f3 i; r9 m - configASSERT( BytesWritten == sizeof( xUpdatedBuffer );
, S. L6 j* h5 ?, D( I4 k; j: n
复制代码- /* Generate interrupt in the other core (pseudocode). */
+ \- |3 [, ^6 e3 q" y3 S' t - GenerateInterrupt();( x/ Q% D; c8 z4 c: V: \ A t
- }
: ^" A8 g4 ?. c: J' T5 ?1 V - }
复制代码然后,ISR读取控制消息缓冲区以获得句柄,将句柄作为参数传递到xMessageBufferSendCompletedFromISR()中: - void InterruptServiceRoutine( void )
! D: o! F' c2 |1 S - {5 D; D9 m" y _8 k7 t' \
- MessageBufferHandle_t xUpdatedMessageBuffer;' P% N& |* `$ s9 d
- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
$ e: Z3 r3 _: {& {
复制代码- /* Receive the handle of the message buffer that contains data from the
5 p& ]6 N B* g- o6 y P0 { - control message buffer. Ensure to drain the buffer before returning. */
: x7 _# x1 x7 d2 b: N. p - while( xMessageBufferReceiveFromISR( xControlMessageBuffer,$ L% Z7 ^+ w7 F
- &xUpdatedMessageBuffer,4 t1 G% O4 E* X2 _0 R$ D6 \0 E
- sizeof( xUpdatedMessageBuffer )," i3 S7 s2 D5 O- g6 d o9 u
- &xHigherPriorityTaskWoken )
. o `# q3 r, w9 I - == sizeof( xUpdatedMessageBuffer ) )
) U( E# \: O" v7 Q$ T: U9 S - {1 L+ F- m# x/ M) f
- /* Call the API function that sends a notification to any task that is
% e% u3 M& l; n! E# v - blocked on the xUpdatedMessageBuffer message buffer waiting for data to7 m5 l. K- ~8 n& G
- arrive. */
1 j1 u( Y7 v9 \' b* v7 N8 [7 t - xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,/ R$ v1 \2 Y5 |# B: k* i( W' c7 x. q
- &xHigherPriorityTaskWoken );4 M( e5 ?3 w% r8 V4 A( l3 e
- }: @7 P; }. O. K1 b: A4 c! J
复制代码- /* Normal FreeRTOS "yield from interrupt" semantics, where
" U* r' A' N2 N* W- o5 p) X2 H0 A( m - xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to
! ^$ n3 u5 r1 `0 w- R - pdTRUE if the interrupt unblocks a task that has a priority above that of& B/ C5 W3 M( S$ B
- the currently executing task. */- w& T) W2 }) d# Y6 F3 g/ [
- portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
5 X+ | R" J$ d* Z( Q - }
复制代码 3 [' T5 {- B* W
' b' b) s# H. t- Z

如图,使用控制消息缓冲区时的顺序: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()将现在包含数据的消息缓冲区的句柄发送到控制消息缓冲区。 4.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 5.中断服务例程从控制消息缓冲区中读取包含数据的消息缓冲区的句柄,然后将该句柄传递给xMessageBufferSendCompletedFromISR()API函数以取消阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再存在空的。
9 J" M( @* Z8 Z2 l当然,以上仅提供基础原理和方法,具体实现需结合项目实际情况。更多相关内容,请参看官方信息。
' q8 G9 E% c# ]4 y
3 V d, K& u: o4 P' z |
好好学习