|
使用FreeRTOS消息缓冲区,实现简单的非对称多处理(AMP)核心到核心通信(STM32H7 双核处理器)。 一、概述 实现STM32H7双核之间通信是FreeRTOS官方提供的一个方案,是基于FreeRTOS消息缓冲区,该消息缓冲区是无锁循环缓冲区,可以将大小不同的数据包从单个发送方传递到单个接收方。 说明,该消息缓冲区仅提供数据的传输,不提供通信相关协议处理。 二、基本原理 实现双核之间通信基本原理:发送和接收任务位于非对称多处理器(AMP)配置中的多核微控制器(MCU)的不同内核上,这意味着每个内核都运行自己的FreeRTOS程序。 同时,一个内核在另一个内核中具有生成中断的能力,以及两个内核都有访问的内存区域(共享内存)。消息缓冲区以每个内核上运行在应用程序已知的地址置在共享内存中,如下图: 
理想情况下,还将有一个内存保护单元(MPU),以确保只能通过内核的消息缓冲区API来访问消息缓冲区,并最好将共享内存标记为不可被其他程序占用。 三、单消息代码描述 这里官方提供了实现该方案的基础代码(仅供参考)。 将数据发送到流缓冲区的代码: - xMessageBufferSend()3 I# v, [# |4 e- z( w: P
- {2 x+ X, @# Y% T+ F
- /* If a time out is specified and there isn't enough# {5 t6 ]. P! b+ r# u- O
- space in the message buffer to send the data, then$ c2 U6 K7 A" h
- enter the blocked state to wait for more space. */
0 H, O5 n( \, Z, u - if( time out != 0 )2 ^# T* D2 @" B4 ?' b% m4 c
- {$ X( ~: d# y6 r4 L! u$ k# X. a
- while( there is insufficient space in the buffer &&7 J1 x: [4 D6 X2 H! ?
- not timed out waiting )
# v8 V' O, M7 o9 y5 Q% {5 n - {+ g/ d0 G* d; q( P6 S* f, j. M
- Enter the blocked state to wait for space in the buffer
( {! h/ K) n n1 o0 F - }
. F+ i% q/ M3 p - }
. j! w1 Q; f7 b - 7 x3 A! k, L5 l; Y+ `" m4 ^
复制代码- if( there is enough space in the buffer )
6 C. P. S: q g! @0 R/ [ - {" B# J4 ^- K. d4 p/ @4 i3 j; A
- write data to buffer
3 s8 a5 b7 B' x" h6 Y& l" M0 Y' | - sbSEND_COMPLETED()4 o5 e4 }! i0 M* I
- }' f2 ?* D1 Q/ z( C
- }
复制代码
0 d T; v9 J' _从流缓冲区读取数据的代码: - xMessageBufferReceive()( a) b1 G- {. i
- {
/ J( z) p8 l' R0 W! G! \ O - /* If a time out is specified and the buffer doesn't+ ^% }' y, S, U4 _3 m
- contain any data that can be read, then enter the
, E5 i: }# l& g4 }0 R/ | - blocked state to wait for the buffer to contain data. */
' l0 A& ~5 `0 G6 t) w) p - if( time out != 0 )5 `6 i7 w! Z r, T# p0 y! A
- {
2 N2 V, S& z7 A) s" w+ W- H - while( there is no data in the buffer &&
% j! V; I& f! v% Z - not timed out waiting )* K5 L- N; f8 M8 H) G2 y! J
- {8 E8 u: \; y/ A0 u+ x; Q; {
- Enter the blocked state to wait for data L. h3 I! u9 r2 |! k8 F7 j
- }
' j9 U/ J( d$ V" b1 {4 T - }! Q1 O. v7 R$ y/ y' G" P
# e5 m* F/ w3 g& h3 E
复制代码- if( there is data in the buffer )
" I4 t4 K; t" B" {, X! m - {
/ I% ^! m; u+ s+ C4 [0 D0 Z: u! h0 { - read data from buffer
; {, W% _, h- i - sbRECEIVE_COMPLETED()
4 l" ^! Y8 Y2 q' t - }- S0 @3 ]" i1 w) {$ G$ t& A
- }
复制代码
( z% H; b/ N6 j4 R如果任务在xMessageBufferReceive()中进入阻塞状态以等待缓冲区包含数据,则将数据发送到缓冲区必须取消阻塞该任务,以便它可以完成其操作。 当xMessageBufferSend()调用sbSEND_COMPLETED()时,任务将不受阻碍。  5 f; B% U6 ~$ o% l, @
ISR通过将消息缓冲区的句柄作为参数传递给xMessageBufferSendCompletedFromISR()函数来解除对任务的阻塞。
+ o$ t( ?0 P/ R% U如图箭头所示,其中发送和接收任务位于不同的MCU内核上: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 4.中断服务例程调用xMessageBufferSendCompletedFromISR()来解除阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再为空。 1 A. J q. C# T& d
四、多消息代码描述 当只有一个消息缓冲区时,很容易将消息缓冲区的句柄传递到xMessageBufferSendCompletedFromISR()中。 - r% [& Z5 a9 i( U- `5 D% m
但是要考虑有两个或更多消息缓冲区的情况,ISR必须首先确定哪个消息缓冲区包含数据。如果消息缓冲区的数量很少,则有几种方法可以实现: 如果硬件允许,则每个消息缓冲区可以使用不同的中断线,从而使中断服务程序和消息缓冲区之间保持一对一的映射。 中断服务例程可以简单地查询每个消息缓冲区以查看其是否包含数据。 可以通过传递元数据(消息是什么,消息的预期接收者是什么等等)以及实际数据的单个消息缓冲区来代替多个消息缓冲区。 ( R6 G" l4 a! a- ^* v/ H' U
1 k w" l) l/ K4 D8 _ , ^. T( K5 B/ b: ~* P( T
但是,如果存在大量或未知的消息缓冲区,则这些技术效率不高。 % |; \( f5 j* _2 f* M
在这种情况下,可伸缩的解决方案是引入单独的控制消息缓冲区。如下面的代码所示,sbSEND_COMPLETED()使用控制消息缓冲区将包含数据的消息缓冲区的句柄传递到中断服务例程中。
9 |/ q0 O8 ~& E$ [- v- _( H2 ^/ n使用sbSEND_COMPLETED()的实现: - /* Added to FreeRTOSConfig.h to override the default implementation. */
# j: G- q8 ~0 K - #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )
7 L. [- u# ]7 K& o4 y! Q9 f* p; h) \
复制代码- /* Implemented in a C file. */2 p) K# C: \# l* ]6 I
- void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer )" m4 k) ~+ j w0 g6 I/ \, S* g
- {) w: i1 ~. I* N; S% q4 G. _9 }
- size_t BytesWritten.+ Z! s8 W6 {) n% }7 ?
复制代码- /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.
7 t* x/ W1 I2 [ - If this function was called because data was written to any message buffer
5 w6 I) v+ L& [5 T: `, q8 P - other than the control message buffer then write the handle of the message
! U# o% O4 _! F - buffer that contains data to the control message buffer, then raise an
, W1 D1 h# ]9 s7 C4 e2 s - interrupt in the other core. If this function was called because data was3 I6 I' p0 W$ l# ~# H
- written to the control message buffer then do nothing. */: S2 O% I) Q7 L$ ]+ m( i) p9 F
- if( xUpdatedBuffer != xControlMessageBuffer )
- w0 n" \: \! \' L3 r v* { - {2 z$ R/ l$ B$ y" J, }! _' g
- BytesWritten = xMessageBufferSend( xControlMessageBuffer,' `6 R/ W+ T, B5 @
- &xUpdatedBuffer,
4 h# \2 l" D6 Y2 Q- q: j; J% P - sizeof( xUpdatedBuffer ),$ a, {, Y" ?9 i9 ^# c. y
- 0 );
- L% C+ Z3 X/ [
复制代码- /* If the bytes could not be written then the control message buffer
8 @. E# ?! _5 h, g# X/ K6 ] K5 S - is too small! */5 Z3 e3 W4 {" o; C' q2 l. @3 w# m
- configASSERT( BytesWritten == sizeof( xUpdatedBuffer );! t( y. `2 z6 [. T E
复制代码- /* Generate interrupt in the other core (pseudocode). */* u6 |: H* h# ]! s! x: k6 Q
- GenerateInterrupt();2 M8 I' Y# A& I @
- }. `2 g0 h, `% {/ T2 O+ [! d5 M
- }
复制代码然后,ISR读取控制消息缓冲区以获得句柄,将句柄作为参数传递到xMessageBufferSendCompletedFromISR()中: - void InterruptServiceRoutine( void )6 E/ d( x- b+ s
- {% I& d* u$ t" C- q; z7 ^
- MessageBufferHandle_t xUpdatedMessageBuffer;
! q7 W" B3 I5 w - BaseType_t xHigherPriorityTaskWoken = pdFALSE;
$ x$ |* ~6 [ g% z" G
复制代码- /* Receive the handle of the message buffer that contains data from the
1 E0 W& e, v7 W1 |. M - control message buffer. Ensure to drain the buffer before returning. */
% {/ u; o7 k0 D. { - while( xMessageBufferReceiveFromISR( xControlMessageBuffer,
& {5 L, ]/ K/ V _2 @, ? K" J - &xUpdatedMessageBuffer," @( Z t" g2 l7 v0 L
- sizeof( xUpdatedMessageBuffer ),4 z. \' B; }; ]% U
- &xHigherPriorityTaskWoken )
5 k8 l% w8 j0 d6 s3 | - == sizeof( xUpdatedMessageBuffer ) )
( e& q/ z+ G4 [- K& M" N - {. t0 r; x+ |1 w( H0 D) T
- /* Call the API function that sends a notification to any task that is
" j: u; C* l1 [' e - blocked on the xUpdatedMessageBuffer message buffer waiting for data to
0 |- D9 s9 `/ u' @ - arrive. */" l+ V* l" ?4 j+ N! w/ y- z( e
- xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,- q t3 j3 D' \0 {2 |
- &xHigherPriorityTaskWoken );/ f( u# {: }2 q/ S! g2 k" A a
- }" o1 F* R2 V1 a4 ]& c$ r1 N1 A: g$ b
复制代码- /* Normal FreeRTOS "yield from interrupt" semantics, where
. L- ?) x. v6 M. R. |) I/ v5 N - xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to3 L( Y7 W; H+ C& F7 Q8 C' j% h
- pdTRUE if the interrupt unblocks a task that has a priority above that of
' Q7 P) |! c5 @) J - the currently executing task. */, c$ Z1 m- B p% ^7 N4 Z3 k+ D% n, X
- portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
( b' e9 k( u' ~ - }
复制代码 , c- {. d* I: s9 d8 [
: O+ G% X5 a; C! y! H' z. d A% ]
如图,使用控制消息缓冲区时的顺序: 1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。 2.发送任务将数据写入消息缓冲区。 3.sbSEND_COMPLETED()将现在包含数据的消息缓冲区的句柄发送到控制消息缓冲区。 4.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。 5.中断服务例程从控制消息缓冲区中读取包含数据的消息缓冲区的句柄,然后将该句柄传递给xMessageBufferSendCompletedFromISR()API函数以取消阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再存在空的。
( x" `& E: a+ M, F当然,以上仅提供基础原理和方法,具体实现需结合项目实际情况。更多相关内容,请参看官方信息。 2 d% o9 i* ?# t4 D
* v0 A; {$ R6 j: E0 w2 K; B D- }- x
|
好好学习