|
使用FreeRTOS消息缓冲区,实现简单的非对称多处理(AMP)核心到核心通信(STM32H7 双核处理器)。 一、概述 实现STM32H7双核之间通信是FreeRTOS官方提供的一个方案,是基于FreeRTOS消息缓冲区,该消息缓冲区是无锁循环缓冲区,可以将大小不同的数据包从单个发送方传递到单个接收方。 说明,该消息缓冲区仅提供数据的传输,不提供通信相关协议处理。 二、基本原理 实现双核之间通信基本原理:发送和接收任务位于非对称多处理器(AMP)配置中的多核微控制器(MCU)的不同内核上,这意味着每个内核都运行自己的FreeRTOS程序。 同时,一个内核在另一个内核中具有生成中断的能力,以及两个内核都有访问的内存区域(共享内存)。消息缓冲区以每个内核上运行在应用程序已知的地址置在共享内存中,如下图: 
理想情况下,还将有一个内存保护单元(MPU),以确保只能通过内核的消息缓冲区API来访问消息缓冲区,并最好将共享内存标记为不可被其他程序占用。 三、单消息代码描述 这里官方提供了实现该方案的基础代码(仅供参考)。 将数据发送到流缓冲区的代码: - xMessageBufferSend() \$ c% p& A: p I* X5 P
- {! T# }4 V3 V# K1 V: @* [9 o; k
- /* If a time out is specified and there isn't enough
# U: g' p, ~7 A' W$ [7 b - space in the message buffer to send the data, then; |' L" F! ]& r6 K! `
- enter the blocked state to wait for more space. */
& K; B5 ~ h' j - if( time out != 0 )( \ Q6 {+ n7 a+ L6 c
- {
/ X# g+ u4 C/ h) S - while( there is insufficient space in the buffer &&
+ `; L/ n# e) `0 U7 C& e - not timed out waiting )0 u# R( [9 d4 h" [; l1 k
- {
: J# c7 i, _' i. j" l3 J - Enter the blocked state to wait for space in the buffer
5 j8 G: w5 m/ m& k2 ?' M6 l- W( q' L0 t - }8 [9 Q& ^& K( b/ p5 Q4 |, I1 s$ W! b
- }* ~& X; g8 c! M: W% L. S
- " L2 ]9 R3 J$ O) Z, D
复制代码- if( there is enough space in the buffer ), r: x, V# C1 ]1 B
- {
, t r0 s; k1 f/ ] - write data to buffer
+ C* o1 l& q* T) F! `9 K D% c6 G - sbSEND_COMPLETED()
0 P, D. S2 s8 _3 w) n - }
+ F- | Z) Z+ U0 k$ ` r - }
复制代码
. C& P+ ?4 h7 _/ W8 [- [从流缓冲区读取数据的代码: - xMessageBufferReceive()
$ ~+ c% i+ w# k/ k1 z. e - {
9 V$ f2 g7 G0 z l) q - /* If a time out is specified and the buffer doesn't& e h, [( y& @
- contain any data that can be read, then enter the
' h/ R' d) I$ }5 U6 n! G* g - blocked state to wait for the buffer to contain data. */
% L/ X" t8 P. d1 b! c - if( time out != 0 )
7 r `# v$ b( M9 c - {
! _, B, {& W; ^' j- Y - while( there is no data in the buffer &&7 T7 Q. g: d! O! y
- not timed out waiting )
% N, f. o( A1 C( S2 q0 R - {2 m/ N7 j! e/ _, n8 z, y/ h* q
- Enter the blocked state to wait for data/ g. y- y9 V+ I* x' Z; x5 X
- }
0 n [% z1 ?( T2 L: ^! j4 r; m2 e - }
4 n" }( [1 t4 T0 ?$ h
& V5 ]! y/ l' `) }, ~
复制代码- if( there is data in the buffer )) b3 |+ b' i3 n8 T, Y9 x1 P
- { x2 m/ V! D3 Q- g3 V# q
- read data from buffer
3 W. P( b- I# |8 a- ]+ O" F5 }6 k - sbRECEIVE_COMPLETED()4 ^6 ~- O- S* F* f& f
- }
, m8 F9 j# Z- d* Y2 f - }
复制代码 " f# A" b3 E `0 \" i
如果任务在xMessageBufferReceive()中进入阻塞状态以等待缓冲区包含数据,则将数据发送到缓冲区必须取消阻塞该任务,以便它可以完成其操作。 当xMessageBufferSend()调用sbSEND_COMPLETED()时,任务将不受阻碍。 
- X' f* g; g& T3 l0 i% c: F3 qISR通过将消息缓冲区的句柄作为参数传递给xMessageBufferSendCompletedFromISR()函数来解除对任务的阻塞。 3 O9 C) Y* G" |
如图箭头所示,其中发送和接收任务位于不同的MCU内核上: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 4.中断服务例程调用xMessageBufferSendCompletedFromISR()来解除阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再为空。 , w2 I! z8 H$ Y0 b0 Q1 N4 b$ H, _( \
四、多消息代码描述 当只有一个消息缓冲区时,很容易将消息缓冲区的句柄传递到xMessageBufferSendCompletedFromISR()中。
5 S y/ ]% d' ^$ K5 n. \$ X" a( f但是要考虑有两个或更多消息缓冲区的情况,ISR必须首先确定哪个消息缓冲区包含数据。如果消息缓冲区的数量很少,则有几种方法可以实现: 如果硬件允许,则每个消息缓冲区可以使用不同的中断线,从而使中断服务程序和消息缓冲区之间保持一对一的映射。 中断服务例程可以简单地查询每个消息缓冲区以查看其是否包含数据。 可以通过传递元数据(消息是什么,消息的预期接收者是什么等等)以及实际数据的单个消息缓冲区来代替多个消息缓冲区。
" ]7 H4 \- {3 J% q) l6 F7 }3 Q9 C- ~0 R. N) I5 k! B; ?
& n7 F) [& F& ?但是,如果存在大量或未知的消息缓冲区,则这些技术效率不高。 ; z, n4 v% J- G7 j! c$ U6 ~# |- p% M
在这种情况下,可伸缩的解决方案是引入单独的控制消息缓冲区。如下面的代码所示,sbSEND_COMPLETED()使用控制消息缓冲区将包含数据的消息缓冲区的句柄传递到中断服务例程中。
! H# z& f4 O% i' r- O, Q使用sbSEND_COMPLETED()的实现: - /* Added to FreeRTOSConfig.h to override the default implementation. */
) a) T# h: h8 d4 S - #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )
! B2 G, |" b" N" S; z% K9 U
复制代码- /* Implemented in a C file. */
( R9 q% n" ^$ ~+ D5 K - void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer )
$ A# w' U+ l- o- q; l) P6 e - {' G: U" f! i7 g
- size_t BytesWritten.
* \# T* L. A. b: g- ]
复制代码- /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.
2 t9 z# b5 u2 ^" t" c9 }7 Q3 c - If this function was called because data was written to any message buffer6 ~( W( @4 L Y; T4 X
- other than the control message buffer then write the handle of the message
3 q2 Z" ~$ W1 U! c) ?( ~" u - buffer that contains data to the control message buffer, then raise an5 ~4 f( I# _; V9 q' `/ C9 M
- interrupt in the other core. If this function was called because data was
: U7 E% O: L8 S' |8 g& y1 r - written to the control message buffer then do nothing. */4 C- P; q/ l3 C9 E5 J
- if( xUpdatedBuffer != xControlMessageBuffer )
4 T, a4 J l- m5 H - {& G% ]# H1 Z( j3 R U* a" p
- BytesWritten = xMessageBufferSend( xControlMessageBuffer,4 F( |# P1 M% t
- &xUpdatedBuffer,- H3 l: @* a! F8 f
- sizeof( xUpdatedBuffer ),
( u( }: D% g( Y( u - 0 );8 S4 m( ^* x. A) [% [# c n, X
复制代码- /* If the bytes could not be written then the control message buffer5 `% G) ]. D; Z) I( W
- is too small! */
) o3 C. }9 [& m) K - configASSERT( BytesWritten == sizeof( xUpdatedBuffer );1 f- @# M$ F) P# F
复制代码- /* Generate interrupt in the other core (pseudocode). */% D' v* w0 U* Q9 l3 N7 i& D+ e
- GenerateInterrupt();
- n9 r; E3 |% h3 k9 }8 S - }. @5 h5 y1 s9 F0 \
- }
复制代码然后,ISR读取控制消息缓冲区以获得句柄,将句柄作为参数传递到xMessageBufferSendCompletedFromISR()中: - void InterruptServiceRoutine( void )& G$ M' u1 r, D6 b
- {( k& @) M5 @' P! A4 Y" y5 o0 q: v
- MessageBufferHandle_t xUpdatedMessageBuffer;
; B( q/ Q/ L' O9 ~& {5 r7 A7 M8 \ - BaseType_t xHigherPriorityTaskWoken = pdFALSE;
3 v1 ^* e/ P2 J4 R$ s; _1 V: y
复制代码- /* Receive the handle of the message buffer that contains data from the
: |4 B. M( G5 [8 @ - control message buffer. Ensure to drain the buffer before returning. */& P2 ~1 M* O% r4 L! K
- while( xMessageBufferReceiveFromISR( xControlMessageBuffer,
7 M( ~8 ]& s) c, `% Z - &xUpdatedMessageBuffer,
; _, S( h" V: f# O/ T - sizeof( xUpdatedMessageBuffer ),
: W) r- w( Z% x# Y8 o - &xHigherPriorityTaskWoken )
0 S) H& f' v2 @0 y& K - == sizeof( xUpdatedMessageBuffer ) )
: y/ u2 X3 ~% t" \" U - {4 I H; `. y G* P2 O) T
- /* Call the API function that sends a notification to any task that is( S k' Q8 h d& a0 _, p
- blocked on the xUpdatedMessageBuffer message buffer waiting for data to1 ?5 n0 e1 a3 ?7 Q, l) B1 o9 p
- arrive. */7 u1 }4 E# H. C
- xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,( a1 T7 F& Y+ f: g. H" v y8 L
- &xHigherPriorityTaskWoken );* [7 r6 v9 k6 v. ?2 Y) `
- }/ [( O& u! o$ U$ ]8 y8 g
复制代码- /* Normal FreeRTOS "yield from interrupt" semantics, where
b& _; H5 ?8 M& e5 @5 L9 ]1 X - xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to9 q; q' x9 u( @( R0 p/ v
- pdTRUE if the interrupt unblocks a task that has a priority above that of
0 t- H- O0 T7 G3 a) ` - the currently executing task. */
* T6 e# d, u9 _4 w4 c1 V - portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
" }: h P% e% z8 i" j c - }
复制代码 7 J/ D( ]3 }- k" s, r! b& {
3 S- r+ W* w+ |9 T

如图,使用控制消息缓冲区时的顺序: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()将现在包含数据的消息缓冲区的句柄发送到控制消息缓冲区。 4.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 5.中断服务例程从控制消息缓冲区中读取包含数据的消息缓冲区的句柄,然后将该句柄传递给xMessageBufferSendCompletedFromISR()API函数以取消阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再存在空的。 & G9 f$ |+ |+ ~! {7 p
当然,以上仅提供基础原理和方法,具体实现需结合项目实际情况。更多相关内容,请参看官方信息。
; J: r. I! m, E% C5 |) c$ l [! ?
7 T, _5 o$ ]5 Z |
好好学习