你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】基于FreeRTOS消息缓冲区,实现STM32H7双核之间通信的原理

[复制链接]
STMCU小助手 发布时间:2021-12-30 14:49

使用FreeRTOS消息缓冲区,实现简单的非对称多处理(AMP)核心到核心通信(STM32H7 双核处理器)。

一、概述

实现STM32H7双核之间通信是FreeRTOS官方提供的一个方案,是基于FreeRTOS消息缓冲区,该消息缓冲区是无锁循环缓冲区,可以将大小不同的数据包从单个发送方传递到单个接收方。

说明,该消息缓冲区仅提供数据的传输,不提供通信相关协议处理。

二、基本原理

实现双核之间通信基本原理:发送和接收任务位于非对称多处理器(AMP)配置中的多核微控制器(MCU)的不同内核上,这意味着每个内核都运行自己的FreeRTOS程序。

同时,一个内核在另一个内核中具有生成中断的能力,以及两个内核都有访问的内存区域(共享内存)。消息缓冲区以每个内核上运行在应用程序已知的地址置在共享内存中,如下图:

理想情况下,还将有一个内存保护单元(MPU),以确保只能通过内核的消息缓冲区API来访问消息缓冲区,并最好将共享内存标记为不可被其他程序占用。

三、单消息代码描述

这里官方提供了实现该方案的基础代码(仅供参考)。

将数据发送到流缓冲区的代码:

  1. xMessageBufferSend()
    ! u2 h  D4 Y) p5 D) U) K
  2. {
    3 G* h, p! }- j  W# |6 L- ?& g
  3.     /* If a time out is specified and there isn't enough
    . q$ M! y1 u4 N0 D
  4.     space in the message buffer to send the data, then
    + W: E! V* t# [6 a$ G
  5.     enter the blocked state to wait for more space. */
    ) m$ R8 B  P/ Z- J; R& U- n
  6.     if( time out != 0 )
    ' Q" c( k  g+ U2 S# L4 @
  7.     {# G& S+ X! j' W/ `& o
  8.         while( there is insufficient space in the buffer &&# z' v( Y" _9 z+ W0 P
  9.                not timed out waiting )3 C! x, L/ [4 P+ w3 M
  10.         {$ U/ b$ ]- i8 L# h2 c( H) L
  11.             Enter the blocked state to wait for space in the buffer: O9 P' X) y: ~1 H, I" [2 q+ j
  12.         }5 o  [( C& O7 |' N
  13.     }+ J. q$ B, H- P) D8 K, N1 ?0 T6 {% n

  14. 7 ]+ o$ u$ W1 m" M0 h8 u$ `5 e. R, B; y
复制代码
  1. if( there is enough space in the buffer )
    1 _; k# G% F6 _9 p# Y
  2. {7 g  e) s( M4 X" E6 L. c
  3. write data to buffer+ S5 Q4 ~$ x+ ^& U) a% L
  4. sbSEND_COMPLETED()8 v( l; Y+ R8 T& g$ _7 {
  5. }! B$ `( a6 f/ Y- ~! ^
  6. }
复制代码

7 x! @! ~' M- L/ P3 e! O

从流缓冲区读取数据的代码:​​​​​​​

  1. xMessageBufferReceive()
    & r/ B4 `. H0 e) n# ^3 h
  2. {6 X' H" C/ j" F5 c, C* S9 O4 a+ E
  3.     /* If a time out is specified and the buffer doesn't
    2 U/ U! Y9 G8 Z8 B
  4.     contain any data that can be read, then enter the1 ~  a2 R5 O$ v4 F, f8 H* T
  5.     blocked state to wait for the buffer to contain data. */
    , X% B) t8 B4 a* p1 i
  6.     if( time out != 0 )
    6 i3 ~, ?* M" G- q% v# K
  7.     {* |- r! G6 U! E
  8.         while( there is no data in the buffer &&
    4 B3 D  z) `" I6 c" X! }
  9.                not timed out waiting ). I# O& u$ P0 B0 i
  10.         {: e/ j6 l; h" C* k
  11.             Enter the blocked state to wait for data
    4 l. J: B" {. Q; I1 o; j2 l8 i
  12.         }
    7 ~( a! C4 S) X4 A
  13.     }
    % s& U/ U. y+ B

  14. . |7 N' w. u/ t6 ^/ k
复制代码
  1. if( there is data in the buffer )
    0 C5 l! V/ ^" E6 O; ]
  2. {
    6 V& G$ J# Z3 G
  3. read data from buffer
    " S) X8 C8 k& [
  4. sbRECEIVE_COMPLETED(). \( L! V9 E) R
  5. }
    * K' E1 Z) C6 N
  6. }
复制代码
& L8 o, ~% L! s

如果任务在xMessageBufferReceive()中进入阻塞状态以等待缓冲区包含数据,则将数据发送到缓冲区必须取消阻塞该任务,以便它可以完成其操作。

当xMessageBufferSend()调用sbSEND_COMPLETED()时,任务将不受阻碍。


; u+ v6 F* G2 n+ C; v% Q. G! B- D

ISR通过将消息缓冲区的句柄作为参数传递给xMessageBufferSendCompletedFromISR()函数来解除对任务的阻塞。

, r3 J! I5 l( g3 k! r, ?0 @

如图箭头所示,其中发送和接收任务位于不同的MCU内核上:

1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。

2.发送任务将数据写入消息缓冲区。

3.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。

4.中断服务例程调用xMessageBufferSendCompletedFromISR()来解除阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再为空。


4 L8 w$ z+ q% g" R& Q1 a3 t# a

四、多消息代码描述

当只有一个消息缓冲区时,很容易将消息缓冲区的句柄传递到xMessageBufferSendCompletedFromISR()中。


1 H( [/ A& w2 W. Q  O

但是要考虑有两个或更多消息缓冲区的情况,ISR必须首先确定哪个消息缓冲区包含数据。如果消息缓冲区的数量很少,则有几种方法可以实现:

  • 如果硬件允许,则每个消息缓冲区可以使用不同的中断线,从而使中断服务程序和消息缓冲区之间保持一对一的映射。

  • 中断服务例程可以简单地查询每个消息缓冲区以查看其是否包含数据。

  • 可以通过传递元数据(消息是什么,消息的预期接收者是什么等等)以及实际数据的单个消息缓冲区来代替多个消息缓冲区。


    5 b8 ~: f# N! Z/ a5 u. j' J( a0 `

    % J7 ]* E: @1 h& e8 ^0 ^6 W
2 v* }0 `9 d0 t. ~4 a

但是,如果存在大量或未知的消息缓冲区,则这些技术效率不高。


: B0 ~+ s: H& v; J

在这种情况下,可伸缩的解决方案是引入单独的控制消息缓冲区。如下面的代码所示,sbSEND_COMPLETED()使用控制消息缓冲区将包含数据的消息缓冲区的句柄传递到中断服务例程中。

1 U% I6 ?7 n5 x1 v0 b' t2 L! P

使用sbSEND_COMPLETED()的实现:​​​​​​​

  1. /* Added to FreeRTOSConfig.h to override the default implementation. */
    & N7 [1 d! }# M& v2 p6 W( C  D
  2. #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )
    ; n5 f' C8 L# w/ l
复制代码
  1. /* Implemented in a C file. */
    * M/ Z' K2 t& \# ?1 T
  2. void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer )
    ' g1 X5 u! N' W/ Z+ a3 E
  3. {* i8 F$ p/ R+ r' d* _
  4. size_t BytesWritten.* m& Q. J0 _" j4 ]  t/ S
复制代码
  1. /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.4 x3 |8 m  T- v3 }
  2. If this function was called because data was written to any message buffer
      h2 {. K9 [/ u- Y( X
  3. other than the control message buffer then write the handle of the message
    : t: Q! Q7 {! \1 \: {2 h
  4. buffer that contains data to the control message buffer, then raise an
    " Z! j) z7 t" [( u
  5. interrupt in the other core. If this function was called because data was! w1 i( W  P) ]4 s
  6. written to the control message buffer then do nothing. */
    ; P6 c& E' V2 C6 o) K, e+ |  b
  7. if( xUpdatedBuffer != xControlMessageBuffer )
    ) M1 y- l2 Y7 \7 W5 D: }+ D
  8. {
    $ u9 m* _3 E! V  j
  9.     BytesWritten = xMessageBufferSend( xControlMessageBuffer,9 A8 o; Y" c' z0 U$ d6 E, y. C2 M
  10.                                                          &xUpdatedBuffer,
    ; {  g# t) D6 V
  11.                                                          sizeof( xUpdatedBuffer ),! x' _- P. E8 e) V) d1 P' P
  12.                                                           0 );9 Y% ]. G) I1 [- s7 O$ P' J
复制代码
  1. /* If the bytes could not be written then the control message buffer
    7 P" ?, s. P$ B7 |7 F
  2. is too small! */
    8 P# m4 s% a! O3 u- ?
  3. configASSERT( BytesWritten == sizeof( xUpdatedBuffer );2 Q( R7 d& J4 N! R+ j
复制代码
  1. /* Generate interrupt in the other core (pseudocode). */8 T+ u$ U' ?) _& `: ?
  2. GenerateInterrupt();
    * N+ Z) R8 Y. v
  3. }
    . h2 D: l' M: V
  4. }
复制代码

然后,ISR读取控制消息缓冲区以获得句柄,将句柄作为参数传递到xMessageBufferSendCompletedFromISR()中:​​​​​​​

  1. void InterruptServiceRoutine( void )* v/ I$ ^/ ~: S5 F- I2 ?
  2. {. w$ S1 ]2 T# F, T( s2 U  m( O
  3. MessageBufferHandle_t xUpdatedMessageBuffer;
    ) Z2 |) R! z8 ~$ S  q
  4. BaseType_t xHigherPriorityTaskWoken = pdFALSE;8 q8 R5 n# [5 i$ N% E# H* @- a
复制代码
  1. /* Receive the handle of the message buffer that contains data from the% c) `3 w6 L" ?7 x" w
  2. control message buffer. Ensure to drain the buffer before returning. */2 h4 W7 C! r( I1 O, U3 F
  3. while( xMessageBufferReceiveFromISR( xControlMessageBuffer,. `; N1 f3 }% |, l
  4. &xUpdatedMessageBuffer,
    3 B; X$ i8 f* {# J
  5. sizeof( xUpdatedMessageBuffer ),
    + O! R) c; ~0 g/ `
  6. &xHigherPriorityTaskWoken )
    , G8 F: h8 A/ O# M6 n
  7. == sizeof( xUpdatedMessageBuffer ) )0 R( I7 G' j" w% V: S2 n
  8. {
    ; x( W( m/ [) c  Z0 q9 q* ]. a! s/ ?
  9. /* Call the API function that sends a notification to any task that is1 C/ e: H. {' Y" M& W  U. x
  10. blocked on the xUpdatedMessageBuffer message buffer waiting for data to# D8 D( ?+ b! B) M' P2 B2 \$ }
  11. arrive. */0 N; N6 e" `2 S. O# _
  12. xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,' y% ~  ]' R8 d7 Z+ o+ B& i, n+ ~
  13. &xHigherPriorityTaskWoken );3 R& c5 V4 j6 _8 i# ~; i3 P; S
  14. }
      m% S4 \8 s2 n% C3 d6 v. k
复制代码
  1. /* Normal FreeRTOS "yield from interrupt" semantics, where
    8 ~, F8 W& ~  m0 M
  2. xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to
    7 A; f8 n, ~' G  k+ r: m
  3. pdTRUE if the interrupt unblocks a task that has a priority above that of8 [/ ?( i" B) ^1 }, y
  4. the currently executing task. */
      @; Q2 M2 T- J: r
  5. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );! d  E9 I+ T, C2 T8 E8 ~2 V
  6. }
复制代码
& G0 h, B' B0 T
% r' K# y" B& F' R6 x: c9 E- F# ~

如图,使用控制消息缓冲区时的顺序:

1.接收任务尝试从空的消息缓冲区中读取数据,并进入阻止状态以等待数据到达。

2.发送任务将数据写入消息缓冲区。

3.sbSEND_COMPLETED()将现在包含数据的消息缓冲区的句柄发送到控制消息缓冲区。

4.sbSEND_COMPLETED()在正在执行接收任务的内核中触发一个中断。

5.中断服务例程从控制消息缓冲区中读取包含数据的消息缓冲区的句柄,然后将该句柄传递给xMessageBufferSendCompletedFromISR()API函数以取消阻止接收任务,该任务现在可以从缓冲区读取,因为缓冲区不再存在空的。


- P% d9 ~* V$ K* v

当然,以上仅提供基础原理和方法,具体实现需结合项目实际情况。更多相关内容,请参看官方信息。

- ~  @; a5 I( ?* e+ O9 r' v

( e  g0 v& I4 T- g$ Q( |. {
赞 收藏 评论1 发布时间:2021-12-30 14:49

举报

1个回答
STMWoodData 回答时间:2021-12-30 15:32:35

好好学习

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版