|
使用FreeRTOS消息缓冲区,实现简单的非对称多处理(AMP)核心到核心通信(STM32H7 双核处理器)。 一、概述 实现STM32H7双核之间通信是FreeRTOS官方提供的一个方案,是基于FreeRTOS消息缓冲区,该消息缓冲区是无锁循环缓冲区,可以将大小不同的数据包从单个发送方传递到单个接收方。 说明,该消息缓冲区仅提供数据的传输,不提供通信相关协议处理。 二、基本原理 实现双核之间通信基本原理:发送和接收任务位于非对称多处理器(AMP)配置中的多核微控制器(MCU)的不同内核上,这意味着每个内核都运行自己的FreeRTOS程序。 同时,一个内核在另一个内核中具有生成中断的能力,以及两个内核都有访问的内存区域(共享内存)。消息缓冲区以每个内核上运行在应用程序已知的地址置在共享内存中,如下图: 
理想情况下,还将有一个内存保护单元(MPU),以确保只能通过内核的消息缓冲区API来访问消息缓冲区,并最好将共享内存标记为不可被其他程序占用。 三、单消息代码描述 这里官方提供了实现该方案的基础代码(仅供参考)。 将数据发送到流缓冲区的代码: - xMessageBufferSend(): B- Z% X7 i" H: R* q9 J
- {* J9 D2 h7 K0 n$ J% B6 U2 F
- /* If a time out is specified and there isn't enough6 G# R: H* r$ }; {
- space in the message buffer to send the data, then
8 b; o& `: a1 [) f- @ - enter the blocked state to wait for more space. */% H( Q) o. p" A3 l
- if( time out != 0 )1 E# X6 V+ o) E
- {
# Q+ h6 O6 {( G! W6 ?/ c( d - while( there is insufficient space in the buffer &&, \- x; h& e* L% c; {. E2 ~
- not timed out waiting )
" L9 J- M* ]7 L' o: Q - {8 d$ C7 V0 T4 R% ^9 t) @' W; T7 {
- Enter the blocked state to wait for space in the buffer( o5 b. n/ u, Q, t; L' y! a
- } j& {7 a: Q0 ?0 R4 U
- }
- U% |' b9 h: \* T/ h& X - ( x4 L4 t. T+ ]& v! c& N4 X
复制代码- if( there is enough space in the buffer )3 T i( c% f7 I! |2 a
- {
" W& v6 L# r ^6 G0 B$ Q! H9 z- d - write data to buffer F+ ~" a1 w9 j, Z1 x' S+ X
- sbSEND_COMPLETED()3 U0 o: l, A8 t
- }
0 M& L$ K$ X6 ^* j - }
复制代码
# D9 Y+ c1 l9 S8 Y1 S从流缓冲区读取数据的代码: - xMessageBufferReceive()
) x: w- J" O- y3 a- s) T - {
* N* ^0 I3 J; y% `# l4 O - /* If a time out is specified and the buffer doesn't* e y% u {/ r, v
- contain any data that can be read, then enter the) N! Z. {" @0 ^( }
- blocked state to wait for the buffer to contain data. */
, S) {6 }- `" E( w; O; v# k6 b# P - if( time out != 0 )9 N' j* M& o9 c- o/ G* C
- {6 G1 Z; d/ p* ~( k
- while( there is no data in the buffer &&
$ [; n/ H7 U5 L" h) B# f, M - not timed out waiting )
v5 a- y' q3 _+ b2 x1 {! f - {
5 ~( [; N$ ^- W0 m/ n - Enter the blocked state to wait for data( M% n% {% L( p* N* y6 h
- }" m# e8 c- t+ ~$ n2 b3 ]# K
- }! |5 n! S4 }8 K9 S. v
- 3 E) l- G$ X3 k% f' ~: v, e
复制代码- if( there is data in the buffer )
5 i& k O" I# i [+ K, y0 B* P - {. i# ~3 G) V L' D. l% V
- read data from buffer) Q, H/ p( y* t/ f; ~
- sbRECEIVE_COMPLETED()# d- _- T7 o ^5 M0 T: R+ U
- }
0 ~0 U' V9 \8 f8 r, m! j0 B: i - }
复制代码
$ E- I- N& P' [6 e' R* \, ?如果任务在xMessageBufferReceive()中进入阻塞状态以等待缓冲区包含数据,则将数据发送到缓冲区必须取消阻塞该任务,以便它可以完成其操作。 当xMessageBufferSend()调用sbSEND_COMPLETED()时,任务将不受阻碍。 
; u3 ?4 w5 D+ @ ?! qISR通过将消息缓冲区的句柄作为参数传递给xMessageBufferSendCompletedFromISR()函数来解除对任务的阻塞。
5 g8 _- [: v' w3 i5 a如图箭头所示,其中发送和接收任务位于不同的MCU内核上: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 4.中断服务例程调用xMessageBufferSendCompletedFromISR()来解除阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再为空。 4 t: s; z& c1 O& A" [
四、多消息代码描述 当只有一个消息缓冲区时,很容易将消息缓冲区的句柄传递到xMessageBufferSendCompletedFromISR()中。 # s7 A9 b5 B8 y) y' c! c7 B; q! E
但是要考虑有两个或更多消息缓冲区的情况,ISR必须首先确定哪个消息缓冲区包含数据。如果消息缓冲区的数量很少,则有几种方法可以实现: 如果硬件允许,则每个消息缓冲区可以使用不同的中断线,从而使中断服务程序和消息缓冲区之间保持一对一的映射。 中断服务例程可以简单地查询每个消息缓冲区以查看其是否包含数据。 可以通过传递元数据(消息是什么,消息的预期接收者是什么等等)以及实际数据的单个消息缓冲区来代替多个消息缓冲区。
% W. Y8 H3 {3 e+ W
6 y4 d1 u' {- U* U. B' n! \
; K; E/ m4 E" W) T& |; i$ F但是,如果存在大量或未知的消息缓冲区,则这些技术效率不高。
7 f+ ^) c p9 x0 {$ l0 D6 y6 k0 u在这种情况下,可伸缩的解决方案是引入单独的控制消息缓冲区。如下面的代码所示,sbSEND_COMPLETED()使用控制消息缓冲区将包含数据的消息缓冲区的句柄传递到中断服务例程中。 * e/ l, z9 j3 T: P
使用sbSEND_COMPLETED()的实现: - /* Added to FreeRTOSConfig.h to override the default implementation. */6 [; g/ C4 ^0 o: x& K/ [
- #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )
) H0 I8 T8 s- R! k1 B0 o$ j
复制代码- /* Implemented in a C file. */
1 l3 F/ N: I9 }# y' R4 E - void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer )
6 {9 Q6 I! \2 X9 p - {
* N5 s; Q$ D5 c4 A7 t- k - size_t BytesWritten.
& F: G' L$ e$ l8 }! ~ J& ~
复制代码- /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.
7 n7 F% ]& _2 r, ?! W - If this function was called because data was written to any message buffer9 Y, l8 C9 L4 M" F5 e
- other than the control message buffer then write the handle of the message
6 K3 o* A2 |! n& q! @8 F - buffer that contains data to the control message buffer, then raise an
6 C5 L$ N2 I) m/ q# s _ F - interrupt in the other core. If this function was called because data was
1 Y3 a3 r9 u3 d& J" ` - written to the control message buffer then do nothing. */
* D$ n# M. E) t g2 o* m8 }* N - if( xUpdatedBuffer != xControlMessageBuffer )5 q* V# J: _, V- b
- {! y' \ @ V. r [1 V' _
- BytesWritten = xMessageBufferSend( xControlMessageBuffer,7 Z1 b- R( g* m
- &xUpdatedBuffer,
% C; c J! o' a - sizeof( xUpdatedBuffer ),
' i. Z% V& L% Z4 q0 t - 0 );5 D' }0 k& q8 |# p1 W
复制代码- /* If the bytes could not be written then the control message buffer3 w3 d" c9 t' c4 T
- is too small! */' l2 B2 B" Y. g% d% T
- configASSERT( BytesWritten == sizeof( xUpdatedBuffer );
% b+ y' E* W) ^7 v
复制代码- /* Generate interrupt in the other core (pseudocode). */
/ Z2 E3 L% [! [. R* F+ Z9 G! x - GenerateInterrupt();8 c2 T/ _0 B4 `4 I
- }
2 y, p" @% j7 o/ M8 t - }
复制代码然后,ISR读取控制消息缓冲区以获得句柄,将句柄作为参数传递到xMessageBufferSendCompletedFromISR()中: - void InterruptServiceRoutine( void )/ O* N/ r# {+ L7 c
- {/ g) v9 ~) V/ T9 o
- MessageBufferHandle_t xUpdatedMessageBuffer;( n1 ?9 }! O- G3 }6 R) F
- BaseType_t xHigherPriorityTaskWoken = pdFALSE;1 U) j# C( `1 A; M
复制代码- /* Receive the handle of the message buffer that contains data from the0 t8 P: [% A5 r3 P6 w
- control message buffer. Ensure to drain the buffer before returning. */
8 w% D* w: p9 v: u6 ?$ n - while( xMessageBufferReceiveFromISR( xControlMessageBuffer,9 A; g( x1 s: r3 A! c& _) r
- &xUpdatedMessageBuffer,
5 V2 l; E" }. c - sizeof( xUpdatedMessageBuffer ),
9 d! } x. E- U9 W/ ^ - &xHigherPriorityTaskWoken )
" O4 Z0 T" f3 Y. v# F& l - == sizeof( xUpdatedMessageBuffer ) )) R1 Y: l/ ~' { \" Y
- {
! R* U: N o8 ]$ v) | - /* Call the API function that sends a notification to any task that is' s$ s$ Z/ a. X6 f. R0 }
- blocked on the xUpdatedMessageBuffer message buffer waiting for data to0 }2 K! A2 w" ?
- arrive. */& p1 U/ I- u; k0 l
- xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,
7 j3 u7 }* O& @! g$ H8 y - &xHigherPriorityTaskWoken );3 n, _3 h$ R* |
- }
4 `$ Q' U0 L% l8 ~1 u8 G E
复制代码- /* Normal FreeRTOS "yield from interrupt" semantics, where! L8 |/ ]/ \/ _, H7 ~! E, I! l
- xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to3 U; s* {% F4 x0 F7 ~- x' q7 N# [
- pdTRUE if the interrupt unblocks a task that has a priority above that of
' t0 ^$ D+ o+ P, [5 f) Q - the currently executing task. */
]" i% C b2 m' u% D6 [! e - portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
. @% ^2 s1 k% O% j: L - }
复制代码
' Q' t: I9 j. b& w4 I) C9 t1 k/ L5 s1 ` z" }$ b6 ~1 d; i9 K

如图,使用控制消息缓冲区时的顺序: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()将现在包含数据的消息缓冲区的句柄发送到控制消息缓冲区。 4.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 5.中断服务例程从控制消息缓冲区中读取包含数据的消息缓冲区的句柄,然后将该句柄传递给xMessageBufferSendCompletedFromISR()API函数以取消阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再存在空的。
' g4 F/ ?) n1 H6 Y/ T4 W' O9 N当然,以上仅提供基础原理和方法,具体实现需结合项目实际情况。更多相关内容,请参看官方信息。
& B3 m3 ?" S) ^9 n4 h' {$ p7 L# y
|
好好学习