|
使用FreeRTOS消息缓冲区,实现简单的非对称多处理(AMP)核心到核心通信(STM32H7 双核处理器)。 一、概述 实现STM32H7双核之间通信是FreeRTOS官方提供的一个方案,是基于FreeRTOS消息缓冲区,该消息缓冲区是无锁循环缓冲区,可以将大小不同的数据包从单个发送方传递到单个接收方。 说明,该消息缓冲区仅提供数据的传输,不提供通信相关协议处理。 二、基本原理 实现双核之间通信基本原理:发送和接收任务位于非对称多处理器(AMP)配置中的多核微控制器(MCU)的不同内核上,这意味着每个内核都运行自己的FreeRTOS程序。 同时,一个内核在另一个内核中具有生成中断的能力,以及两个内核都有访问的内存区域(共享内存)。消息缓冲区以每个内核上运行在应用程序已知的地址置在共享内存中,如下图: 
理想情况下,还将有一个内存保护单元(MPU),以确保只能通过内核的消息缓冲区API来访问消息缓冲区,并最好将共享内存标记为不可被其他程序占用。 三、单消息代码描述 这里官方提供了实现该方案的基础代码(仅供参考)。 将数据发送到流缓冲区的代码: - xMessageBufferSend()
6 E* Z. t/ g3 o; m) I: x - {
3 I6 t4 f& u1 i& _1 a) A O - /* If a time out is specified and there isn't enough: h" l$ {2 v$ i b0 q
- space in the message buffer to send the data, then
& c1 _ ^7 Z8 r" S" |& J - enter the blocked state to wait for more space. */
/ F1 C3 C8 u2 V - if( time out != 0 )
9 P4 w0 J K6 j( O& y - {# E) F9 T9 Z' X0 {' }1 Y" O5 W
- while( there is insufficient space in the buffer &&/ O7 t) U) A% T/ g! K
- not timed out waiting )
: z" q& D4 [/ T+ X8 ?6 L - {
4 z8 `% z5 A& a" e$ x" e: R6 v+ | - Enter the blocked state to wait for space in the buffer$ f# N9 H* @* A% {: |; ]' A" J
- }$ m4 g! p0 V( t0 L0 F
- }1 L+ o$ ]* S% b" `
- I/ ?2 C$ Q6 |8 H
复制代码- if( there is enough space in the buffer )
! p; Y# j7 z. Q+ k: G, M - {
+ h, I! c' [0 S. |: ` - write data to buffer
* g/ [, w' \& R0 } - sbSEND_COMPLETED()9 z: C1 `" e9 D% k; T8 {* v
- }
2 v" q3 A' Y8 F: O - }
复制代码
. N) ], F' a) l1 ]) F, S从流缓冲区读取数据的代码: - xMessageBufferReceive()
. @* B- K( ]. X) m- c3 @+ t - {2 r7 n3 s; y/ U W. e) t
- /* If a time out is specified and the buffer doesn't4 Q- b7 C! m" {: v
- contain any data that can be read, then enter the# r$ ^2 r: y9 w8 j# c% D0 p
- blocked state to wait for the buffer to contain data. */
) L/ S0 b# }: a5 ~: o - if( time out != 0 )
1 k$ F: E" {5 p' n. S - {3 j7 k- M" X1 Q
- while( there is no data in the buffer &&
& [' t& w! c" [- q8 u) f - not timed out waiting )# z2 F" D2 v1 {
- {
) }* a5 i3 Y+ f% m7 I - Enter the blocked state to wait for data
; R% R* ?" L2 O2 V; ~7 G3 W - }
3 E* p) o) G* h - }" g1 g: V* B, ?. L& \
4 m$ I c% D7 c2 s/ _0 u
复制代码- if( there is data in the buffer )& P2 h+ N/ _! r v, i( h
- {
! k5 M6 k8 A6 x& s' Q, {2 ? - read data from buffer5 a7 E& z/ i- J+ P$ c
- sbRECEIVE_COMPLETED()
3 g- t" C& \* S - }! Y4 G9 \: Z/ m9 Z6 S
- }
复制代码 ) D1 X# C- d3 d+ q
如果任务在xMessageBufferReceive()中进入阻塞状态以等待缓冲区包含数据,则将数据发送到缓冲区必须取消阻塞该任务,以便它可以完成其操作。 当xMessageBufferSend()调用sbSEND_COMPLETED()时,任务将不受阻碍。 
, I9 S5 X4 o4 Z+ d5 p2 p: U; U) fISR通过将消息缓冲区的句柄作为参数传递给xMessageBufferSendCompletedFromISR()函数来解除对任务的阻塞。
5 C9 _$ j) o: h2 V3 o如图箭头所示,其中发送和接收任务位于不同的MCU内核上: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 4.中断服务例程调用xMessageBufferSendCompletedFromISR()来解除阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再为空。 8 y5 B! C; \9 F) R9 n# L; [
四、多消息代码描述 当只有一个消息缓冲区时,很容易将消息缓冲区的句柄传递到xMessageBufferSendCompletedFromISR()中。
; b' f( t: Z8 H- B4 E$ t但是要考虑有两个或更多消息缓冲区的情况,ISR必须首先确定哪个消息缓冲区包含数据。如果消息缓冲区的数量很少,则有几种方法可以实现: 如果硬件允许,则每个消息缓冲区可以使用不同的中断线,从而使中断服务程序和消息缓冲区之间保持一对一的映射。 中断服务例程可以简单地查询每个消息缓冲区以查看其是否包含数据。 可以通过传递元数据(消息是什么,消息的预期接收者是什么等等)以及实际数据的单个消息缓冲区来代替多个消息缓冲区。
6 R% k# S3 J6 a0 |9 a9 F6 w# \7 C' p& L( I8 L# P, V1 t. D9 i
5 z& f, N, U" N% z0 m1 ]: I m" Z但是,如果存在大量或未知的消息缓冲区,则这些技术效率不高。
J) p% q z8 _" O1 k, l. o在这种情况下,可伸缩的解决方案是引入单独的控制消息缓冲区。如下面的代码所示,sbSEND_COMPLETED()使用控制消息缓冲区将包含数据的消息缓冲区的句柄传递到中断服务例程中。
( l6 r e. Q" y" x* d3 \使用sbSEND_COMPLETED()的实现: - /* Added to FreeRTOSConfig.h to override the default implementation. */
, o6 o( r# u1 z' r; z" b - #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )
: l2 e- \1 K5 ^+ v. T5 b
复制代码- /* Implemented in a C file. */
4 w' w: [. ]3 y3 _; _4 o - void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer ): Q" x: W, k8 V3 C, G, U' e
- { P' W/ j1 A% c* G0 \5 H! W
- size_t BytesWritten.
& Q! c) v3 B5 ^7 a: U% L
复制代码- /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.
! Z- p z' N7 P- j/ m- J4 z+ p - If this function was called because data was written to any message buffer8 m1 s* V& q3 ?+ a8 d' K
- other than the control message buffer then write the handle of the message' n( _7 A4 }. `: {; }
- buffer that contains data to the control message buffer, then raise an
& r" _3 O& g- d1 N5 A - interrupt in the other core. If this function was called because data was
* P: E+ o `- ^, }8 J8 K - written to the control message buffer then do nothing. */
$ f; T" \( ^4 o: [: _4 o - if( xUpdatedBuffer != xControlMessageBuffer )
' L7 d( S1 ]6 _6 \2 p& r C - {
% @% \* m1 c) d g9 h, B - BytesWritten = xMessageBufferSend( xControlMessageBuffer,
2 G- G3 f8 l; r I8 u* ^; Q8 U: k2 @ - &xUpdatedBuffer,
9 j$ j8 a: b. ~" {5 ^/ t7 z$ W5 v - sizeof( xUpdatedBuffer ),
: O* H8 z$ M$ D/ r' I - 0 );9 `7 | y# N, Q0 n
复制代码- /* If the bytes could not be written then the control message buffer
0 v+ @* E) O9 _+ {/ C - is too small! */ s' N3 Q0 q$ B W7 [
- configASSERT( BytesWritten == sizeof( xUpdatedBuffer );& d/ P7 L/ S; L( U: C% r
复制代码- /* Generate interrupt in the other core (pseudocode). */4 S6 _3 n! v$ ?6 C8 i; a
- GenerateInterrupt();
5 Z# c1 g) a) g# X& Y C7 ]/ D - }8 v ]3 R) d6 v, @9 C9 G
- }
复制代码然后,ISR读取控制消息缓冲区以获得句柄,将句柄作为参数传递到xMessageBufferSendCompletedFromISR()中: - void InterruptServiceRoutine( void )) {5 g" `* q. I/ Z. H
- {
+ s' Y6 o& U: t/ z1 `( j1 R - MessageBufferHandle_t xUpdatedMessageBuffer;
4 d- Y& |; r3 I - BaseType_t xHigherPriorityTaskWoken = pdFALSE;
: J3 e: z# J- |) ?
复制代码- /* Receive the handle of the message buffer that contains data from the
' b8 P+ B! F3 a: t5 Z9 j - control message buffer. Ensure to drain the buffer before returning. */
. \' C% f$ e# ]3 m' ` b- E @/ j - while( xMessageBufferReceiveFromISR( xControlMessageBuffer,
3 Y0 k Q) @8 C9 x - &xUpdatedMessageBuffer,
0 e6 T4 p' i5 ?4 ~" |& H - sizeof( xUpdatedMessageBuffer ),
: e; z& n' a# y- ? - &xHigherPriorityTaskWoken ). D; {( i9 l* @# S7 ^4 x
- == sizeof( xUpdatedMessageBuffer ) ) D+ _( c& W5 D' |6 \; M- S5 T
- { Q" z. ?3 u- d$ S
- /* Call the API function that sends a notification to any task that is
3 y0 {/ b# m! A2 ~ - blocked on the xUpdatedMessageBuffer message buffer waiting for data to
0 n2 q! S/ U6 z3 ~ l7 [4 K - arrive. */
8 W t5 o% j; t' F5 H* E - xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,0 s8 y( {& i1 t3 v3 C' p7 S
- &xHigherPriorityTaskWoken );/ ]' V. O- D" O1 V S9 c
- }# l; G0 Y! {: B
复制代码- /* Normal FreeRTOS "yield from interrupt" semantics, where F( d0 g$ N/ _; R
- xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to5 i, D! {0 g" T# W' ]& e, B
- pdTRUE if the interrupt unblocks a task that has a priority above that of3 Q6 h( d% G7 [* F% r9 X w: I
- the currently executing task. */4 l+ a1 J, w% n, P/ E6 ]
- portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
p- Z5 C4 ]" S# b; C3 W( E8 @ - }
复制代码
* Q4 Z& t# V- p$ D; h
0 J0 n/ y! o6 |+ z ?
如图,使用控制消息缓冲区时的顺序: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()将现在包含数据的消息缓冲区的句柄发送到控制消息缓冲区。 4.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 5.中断服务例程从控制消息缓冲区中读取包含数据的消息缓冲区的句柄,然后将该句柄传递给xMessageBufferSendCompletedFromISR()API函数以取消阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再存在空的。 9 }6 m( M, y u `) |) z
当然,以上仅提供基础原理和方法,具体实现需结合项目实际情况。更多相关内容,请参看官方信息。
$ ` J0 j) W# [4 ^- T: s
E7 M+ I" G' G* z/ h) P |
好好学习