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

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

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

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

一、概述

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

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

二、基本原理

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

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

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

三、单消息代码描述

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

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

  1. xMessageBufferSend()
    # X% z* z5 \2 N' r# Z1 ~- N7 U9 ]
  2. {
    ' _9 C6 T% @: R6 f+ e4 ]# S" f
  3.     /* If a time out is specified and there isn't enough
    5 R3 x# u; z% Y% b4 P. ]
  4.     space in the message buffer to send the data, then
    ) y/ m7 k! u0 u: N3 |9 [  ?7 f
  5.     enter the blocked state to wait for more space. */' t& L! u) {- \, J0 [  f9 \
  6.     if( time out != 0 )) S# A; r3 a# C4 Y4 O+ {, _
  7.     {
    # j' L7 W, m- M( w
  8.         while( there is insufficient space in the buffer &&) t& t, ?2 D, h* ?" w, |& b$ I
  9.                not timed out waiting )/ P1 W3 k0 Z- ^4 g7 G
  10.         {
      N3 X! `" ]/ ?1 ~& i
  11.             Enter the blocked state to wait for space in the buffer
    + G  E/ d  e: T6 D, e* s3 S
  12.         }
    " {7 f4 Q0 P' e7 c
  13.     }1 u+ ^) g2 r) d' f' @1 Z5 [

  14. 8 S& `4 b1 i$ x6 k5 n8 q$ t, }
复制代码
  1. if( there is enough space in the buffer )
    ! N$ |! Z- |+ V) e2 O
  2. {
    , z/ ?- l5 E. \8 t; r; P
  3. write data to buffer
    4 P3 ?( m( o: `/ O
  4. sbSEND_COMPLETED(): ]4 e3 f& `  p2 e: N! ]
  5. }
    ) `; x9 j' R* l& K. R; Q7 f; A
  6. }
复制代码
& B) i3 A6 z+ M* U( H

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

  1. xMessageBufferReceive()
    + k# Z; e. `+ ~; i2 l! s3 |6 v
  2. {
    4 ]+ a/ B( E+ y8 z/ W; h! H
  3.     /* If a time out is specified and the buffer doesn't' A8 n7 \$ k& U# H& [
  4.     contain any data that can be read, then enter the- q+ u9 w' Q; n7 n$ i( h4 H' {
  5.     blocked state to wait for the buffer to contain data. */
    ; z. w! a" `1 G( r
  6.     if( time out != 0 )8 s  G/ _: C. d8 j- ~4 M3 I
  7.     {: v1 R; I% A) W. X
  8.         while( there is no data in the buffer &&' U  V  V/ ]; u; e8 A* u) b/ w
  9.                not timed out waiting )
    - a7 q! S5 e! y
  10.         {
    ! J6 X; T2 J7 i( F3 O
  11.             Enter the blocked state to wait for data
    , W( m7 E; J$ V
  12.         }
    & x* F; p# l$ \: _+ z
  13.     }
    $ F# q* |7 @" o! a) C2 g& N% X

  14.   w! B$ }1 y  ?! n
复制代码
  1. if( there is data in the buffer )
    - f; I; d. u, f: h0 f6 D
  2. {
    ; ?" {& `" E. O* Q
  3. read data from buffer
    * h) A& R& Y' ~8 |  N5 g2 D
  4. sbRECEIVE_COMPLETED()9 v( y6 Y# r% ^
  5. }% K2 O/ A' @7 h
  6. }
复制代码
  X; W# }* M8 l. ]$ ]% b! Z( R

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

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

. z8 P- p+ i, j: a( t

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

% v* A7 Z3 z0 g2 r" ]

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

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

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

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

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

2 Z+ B; [. J: p/ h/ z

四、多消息代码描述

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

; O( W. \0 J/ [) r+ _  ?  b

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

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

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

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

    ( w- v& m: d  P
    + ]; T* c1 a, n' e$ g

7 k/ J. b, k  M

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


' a/ e! E) K5 S% r5 I' f9 p

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

* t1 O! g5 ?6 d5 g/ s4 }  j

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

  1. /* Added to FreeRTOSConfig.h to override the default implementation. */
    0 ?+ w. Y1 L/ O$ ]6 k: Z
  2. #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )
    0 x) m; p+ e' I# [  T5 @
复制代码
  1. /* Implemented in a C file. */
    9 J1 \0 q2 I7 Q; B  W
  2. void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer )
    8 j$ N+ k, I+ E4 z2 d+ p8 t
  3. {8 ?3 Y6 B: t5 D$ Q
  4. size_t BytesWritten.
    # R' T. E8 y! c7 _$ a' m! k' ~
复制代码
  1. /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.! u; v7 }7 ~7 E8 _! R
  2. If this function was called because data was written to any message buffer5 B6 A8 X7 ^" T, Q
  3. other than the control message buffer then write the handle of the message/ J  z. }# W6 M2 E* e
  4. buffer that contains data to the control message buffer, then raise an
    2 m, G. ~& I. ?8 L8 V5 h) R
  5. interrupt in the other core. If this function was called because data was
    7 W+ X- g( O; y" A$ s+ C8 U- F
  6. written to the control message buffer then do nothing. */; p$ G  X1 D! C4 l5 W
  7. if( xUpdatedBuffer != xControlMessageBuffer )7 f$ ^' C* ~3 D$ ~
  8. {& t& O& f- v. |
  9.     BytesWritten = xMessageBufferSend( xControlMessageBuffer,8 J; H, B  p" S" w+ N, c$ F  M0 @
  10.                                                          &xUpdatedBuffer,* |' d$ U) ~, U1 a2 k$ f4 x# |1 \* g  c
  11.                                                          sizeof( xUpdatedBuffer ),
    ' z3 ~! |$ w% H, x  z9 ]
  12.                                                           0 );, y7 S. e: P3 ?+ z$ v3 g% r
复制代码
  1. /* If the bytes could not be written then the control message buffer
    9 J/ e8 }- O; R/ V# o! Y8 P4 ^
  2. is too small! */
    ) T3 d2 b9 f3 i; r9 m
  3. configASSERT( BytesWritten == sizeof( xUpdatedBuffer );
    , S. L6 j* h5 ?, D( I4 k; j: n
复制代码
  1. /* Generate interrupt in the other core (pseudocode). */
    + \- |3 [, ^6 e3 q" y3 S' t
  2. GenerateInterrupt();( x/ Q% D; c8 z4 c: V: \  A  t
  3. }
    : ^" A8 g4 ?. c: J' T5 ?1 V
  4. }
复制代码

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

  1. void InterruptServiceRoutine( void )
    ! D: o! F' c2 |1 S
  2. {5 D; D9 m" y  _8 k7 t' \
  3. MessageBufferHandle_t xUpdatedMessageBuffer;' P% N& |* `$ s9 d
  4. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    $ e: Z3 r3 _: {& {
复制代码
  1. /* Receive the handle of the message buffer that contains data from the
    5 p& ]6 N  B* g- o6 y  P0 {
  2. control message buffer. Ensure to drain the buffer before returning. */
    : x7 _# x1 x7 d2 b: N. p
  3. while( xMessageBufferReceiveFromISR( xControlMessageBuffer,$ L% Z7 ^+ w7 F
  4. &xUpdatedMessageBuffer,4 t1 G% O4 E* X2 _0 R$ D6 \0 E
  5. sizeof( xUpdatedMessageBuffer )," i3 S7 s2 D5 O- g6 d  o9 u
  6. &xHigherPriorityTaskWoken )
    . o  `# q3 r, w9 I
  7. == sizeof( xUpdatedMessageBuffer ) )
    ) U( E# \: O" v7 Q$ T: U9 S
  8. {1 L+ F- m# x/ M) f
  9. /* Call the API function that sends a notification to any task that is
    % e% u3 M& l; n! E# v
  10. blocked on the xUpdatedMessageBuffer message buffer waiting for data to7 m5 l. K- ~8 n& G
  11. arrive. */
    1 j1 u( Y7 v9 \' b* v7 N8 [7 t
  12. xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,/ R$ v1 \2 Y5 |# B: k* i( W' c7 x. q
  13. &xHigherPriorityTaskWoken );4 M( e5 ?3 w% r8 V4 A( l3 e
  14. }: @7 P; }. O. K1 b: A4 c! J
复制代码
  1. /* Normal FreeRTOS "yield from interrupt" semantics, where
    " U* r' A' N2 N* W- o5 p) X2 H0 A( m
  2. xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to
    ! ^$ n3 u5 r1 `0 w- R
  3. pdTRUE if the interrupt unblocks a task that has a priority above that of& B/ C5 W3 M( S$ B
  4. the currently executing task. */- w& T) W2 }) d# Y6 F3 g/ [
  5. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
    5 X+ |  R" J$ d* Z( Q
  6. }
复制代码
3 [' T5 {- B* W
' b' b) s# H. t- Z

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

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

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

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

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

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


9 J" M( @* Z8 Z2 l

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


' q8 G9 E% c# ]4 y

3 V  d, K& u: o4 P' z
收藏 评论1 发布时间:2021-12-30 14:49

举报

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

好好学习

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版