使用FreeRTOS消息缓冲区,实现简单的非对称多处理(AMP)核心到核心通信(STM32H7 双核处理器)。 一、概述 实现STM32H7双核之间通信是FreeRTOS官方提供的一个方案,是基于FreeRTOS消息缓冲区,该消息缓冲区是无锁循环缓冲区,可以将大小不同的数据包从单个发送方传递到单个接收方。 说明,该消息缓冲区仅提供数据的传输,不提供通信相关协议处理。 二、基本原理 实现双核之间通信基本原理:发送和接收任务位于非对称多处理器(AMP)配置中的多核微控制器(MCU)的不同内核上,这意味着每个内核都运行自己的FreeRTOS程序。 同时,一个内核在另一个内核中具有生成中断的能力,以及两个内核都有访问的内存区域(共享内存)。消息缓冲区以每个内核上运行在应用程序已知的地址置在共享内存中,如下图: 理想情况下,还将有一个内存保护单元(MPU),以确保只能通过内核的消息缓冲区API来访问消息缓冲区,并最好将共享内存标记为不可被其他程序占用。 三、单消息代码描述 这里官方提供了实现该方案的基础代码(仅供参考)。 将数据发送到流缓冲区的代码: - xMessageBufferSend()
" E1 w2 d" M1 m9 h5 J% F$ N - {
0 p$ V4 p- K$ B- w' y3 J1 } - /* If a time out is specified and there isn't enough
+ |2 F; y0 ]: R J - space in the message buffer to send the data, then3 C# q! t5 _- o' r
- enter the blocked state to wait for more space. */3 Y+ L" j, z( l$ c' A
- if( time out != 0 )0 m. J& {1 p3 w: B
- {1 s4 N9 c8 j8 ^
- while( there is insufficient space in the buffer &&
7 |! k, A9 H7 g0 \6 s - not timed out waiting )! w, q ^8 q( y' U8 k/ ]( E5 z9 D% p. s/ O
- {* S' z; F/ _) S9 b- d( R/ t
- Enter the blocked state to wait for space in the buffer8 P+ {; m% l" Y" K' d
- }3 D; S; j' T) @3 w2 O' B
- }4 I" X! f" M3 u; u
- r; ?9 b4 C) x* e7 S
复制代码- if( there is enough space in the buffer )
. ~6 H* K9 A) E7 y: {& Y - {
/ ~. H7 n; u- \9 y/ j( O6 A - write data to buffer' B* I; j- p+ M. I+ A' c5 Z% I
- sbSEND_COMPLETED()
4 {' u" k3 F& }4 ^$ v! _ - } g2 O0 o. _6 X2 [" Y, K
- }
复制代码
9 V9 D; {& n$ C; b* I从流缓冲区读取数据的代码: - xMessageBufferReceive()/ g: `) z3 @( A2 \
- {) f2 t( p" h7 K
- /* If a time out is specified and the buffer doesn't" r( E5 Z/ y9 J
- contain any data that can be read, then enter the
- }1 d9 q S( S* Q2 z E$ Q) o& f - blocked state to wait for the buffer to contain data. */! j! N8 H' r5 ^7 p. y
- if( time out != 0 )
b0 P5 w3 Z' X9 n) `( _1 U - {
0 N ?3 n7 H( P9 L" ^4 J! S3 a - while( there is no data in the buffer &&+ ?7 k7 y) B# Y4 m7 U- _
- not timed out waiting )
4 o2 ~/ d% u( \ - {. G' f) c) Z3 c+ ?
- Enter the blocked state to wait for data
$ i$ _* _7 b* c# B8 N$ l( Z) ~8 C* O - }0 s9 V" x4 g0 j2 ^( L+ E
- }3 O$ z9 p; g4 D- }3 N8 b
- 3 C" T( V+ I% `$ s* p; h
复制代码- if( there is data in the buffer )2 K# ]7 F8 q* i6 A2 q; O/ k$ ^( K. b
- {' w9 `1 h) r- a" m
- read data from buffer
9 G$ g. k. f* _; M9 V - sbRECEIVE_COMPLETED()0 F$ z% X) V+ ~7 r& Y @3 g, e
- }
. U% b5 f' M1 O2 W! s - }
复制代码
. k1 o: r5 k+ Z7 ]' V如果任务在xMessageBufferReceive()中进入阻塞状态以等待缓冲区包含数据,则将数据发送到缓冲区必须取消阻塞该任务,以便它可以完成其操作。 当xMessageBufferSend()调用sbSEND_COMPLETED()时,任务将不受阻碍。
, c! ?/ ^) \& Z. W& [% FISR通过将消息缓冲区的句柄作为参数传递给xMessageBufferSendCompletedFromISR()函数来解除对任务的阻塞。 q) N6 S1 ]7 }& w/ S. R
如图箭头所示,其中发送和接收任务位于不同的MCU内核上: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 4.中断服务例程调用xMessageBufferSendCompletedFromISR()来解除阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再为空。
/ @% b3 g" ^& b. i+ q1 s8 A四、多消息代码描述 当只有一个消息缓冲区时,很容易将消息缓冲区的句柄传递到xMessageBufferSendCompletedFromISR()中。 " s" C/ H* W0 j
但是要考虑有两个或更多消息缓冲区的情况,ISR必须首先确定哪个消息缓冲区包含数据。如果消息缓冲区的数量很少,则有几种方法可以实现: 如果硬件允许,则每个消息缓冲区可以使用不同的中断线,从而使中断服务程序和消息缓冲区之间保持一对一的映射。 中断服务例程可以简单地查询每个消息缓冲区以查看其是否包含数据。 可以通过传递元数据(消息是什么,消息的预期接收者是什么等等)以及实际数据的单个消息缓冲区来代替多个消息缓冲区。
4 {. ?7 I( ]/ E' `& X# b6 J5 M6 D3 n, ^9 k- a. O
7 n$ g! ~/ K# W但是,如果存在大量或未知的消息缓冲区,则这些技术效率不高。
& B& f$ E8 C$ W+ \; K3 Q0 B在这种情况下,可伸缩的解决方案是引入单独的控制消息缓冲区。如下面的代码所示,sbSEND_COMPLETED()使用控制消息缓冲区将包含数据的消息缓冲区的句柄传递到中断服务例程中。
# p* N: Y* [& W( t. D1 F使用sbSEND_COMPLETED()的实现: - /* Added to FreeRTOSConfig.h to override the default implementation. */
/ }8 U% H( a3 s& {1 f7 U4 X0 G) q - #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )3 }# R6 {# E2 ]( [/ c- J* l Q3 n- Y
复制代码- /* Implemented in a C file. */
4 R- o3 e- t( J! a - void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer )( z& k9 Z2 w K v
- {( \* F$ X" D; ?) Z
- size_t BytesWritten.! J0 S9 J. v2 y+ K3 l. t6 w
复制代码- /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.
$ B# E8 m4 B; m( c/ c6 U - If this function was called because data was written to any message buffer1 X! s0 e5 y: ?" F; q2 ~
- other than the control message buffer then write the handle of the message
/ R7 H% X8 W \+ y; Z$ f% C - buffer that contains data to the control message buffer, then raise an
" e7 `; i8 s1 m1 C - interrupt in the other core. If this function was called because data was& [6 [8 M. Z" a( e0 Y
- written to the control message buffer then do nothing. */ h5 O- ^5 N9 n4 V! Z, N" f5 a# O
- if( xUpdatedBuffer != xControlMessageBuffer )! {/ a$ N( G1 Y
- {3 U0 b- @' ~2 m
- BytesWritten = xMessageBufferSend( xControlMessageBuffer,
! w$ H% M9 n4 j' l5 w' @! H - &xUpdatedBuffer,
9 _- ~2 O# o2 `5 U# k/ a* {* C6 V# u - sizeof( xUpdatedBuffer ),
8 w4 d8 ~7 l7 @* O9 ^% c. O - 0 );
: M8 @* {( B9 Z2 ~7 x1 c# _
复制代码- /* If the bytes could not be written then the control message buffer
$ ^/ e* ?8 O: K0 T$ B - is too small! */9 G: }- ?) b8 F& h) |
- configASSERT( BytesWritten == sizeof( xUpdatedBuffer );% P! m6 x( ?7 E
复制代码- /* Generate interrupt in the other core (pseudocode). */
0 e0 Y; h6 U, k4 N8 C. y/ i4 H" P8 [ - GenerateInterrupt();' c6 }% l1 Q( m- e7 s0 I
- }# y/ s4 \; y7 y
- }
复制代码然后,ISR读取控制消息缓冲区以获得句柄,将句柄作为参数传递到xMessageBufferSendCompletedFromISR()中: - void InterruptServiceRoutine( void )
' y. Y# ]0 l& c! N - {6 b, M6 B! I4 z$ L9 `
- MessageBufferHandle_t xUpdatedMessageBuffer;/ u3 u" K1 i4 T2 J% w/ o- a9 Z
- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
4 H5 L \9 D% F2 x1 F$ j+ A
复制代码- /* Receive the handle of the message buffer that contains data from the5 k u8 N9 a' f2 P/ R! _ y
- control message buffer. Ensure to drain the buffer before returning. */; u$ ^; t1 {$ q3 X' z
- while( xMessageBufferReceiveFromISR( xControlMessageBuffer,
7 b0 P0 M! p4 T0 z: K( i9 d% ^ - &xUpdatedMessageBuffer,& O5 p1 o! V6 Q
- sizeof( xUpdatedMessageBuffer ),% [' Q( g+ x* [$ u0 c n% ?
- &xHigherPriorityTaskWoken )
& R9 q" ^# n+ J$ d2 A! G, g. y - == sizeof( xUpdatedMessageBuffer ) ); m% K8 C* e6 ]
- {1 R4 @$ P V7 U4 a7 b: E; T
- /* Call the API function that sends a notification to any task that is+ b- p+ N3 k3 M" u/ J8 s+ I
- blocked on the xUpdatedMessageBuffer message buffer waiting for data to
( D {2 B, d9 F8 r4 K - arrive. */: R7 P! W7 \( b( K
- xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,: d# r. B/ @6 Z
- &xHigherPriorityTaskWoken ); |/ ~" l, O5 T) L% _
- }
. }5 Q" q$ C$ |" E
复制代码- /* Normal FreeRTOS "yield from interrupt" semantics, where
t; ? F! [, Z - xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to/ s1 o0 D1 p k( w% A7 O
- pdTRUE if the interrupt unblocks a task that has a priority above that of
5 q- \4 m6 |7 P- Y6 G - the currently executing task. */$ X' |0 B# b4 s4 R- F% [
- portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
* ]6 u, W3 J$ D - }
复制代码 ( I* z; f' h% ^* j* U
0 A$ a2 y% H/ u3 M c) s0 f2 \如图,使用控制消息缓冲区时的顺序: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()将现在包含数据的消息缓冲区的句柄发送到控制消息缓冲区。 4.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 5.中断服务例程从控制消息缓冲区中读取包含数据的消息缓冲区的句柄,然后将该句柄传递给xMessageBufferSendCompletedFromISR()API函数以取消阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再存在空的。
/ H+ A7 h; _% C3 |1 H当然,以上仅提供基础原理和方法,具体实现需结合项目实际情况。更多相关内容,请参看官方信息。
* p: r0 V3 o! m. N, O. J) i/ h& v, o$ x- H
|
好好学习