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

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

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

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

一、概述

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

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

二、基本原理

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

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

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

三、单消息代码描述

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

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

  1. xMessageBufferSend()
    " E1 w2 d" M1 m9 h5 J% F$ N
  2. {
    0 p$ V4 p- K$ B- w' y3 J1 }
  3.     /* If a time out is specified and there isn't enough
    + |2 F; y0 ]: R  J
  4.     space in the message buffer to send the data, then3 C# q! t5 _- o' r
  5.     enter the blocked state to wait for more space. */3 Y+ L" j, z( l$ c' A
  6.     if( time out != 0 )0 m. J& {1 p3 w: B
  7.     {1 s4 N9 c8 j8 ^
  8.         while( there is insufficient space in the buffer &&
    7 |! k, A9 H7 g0 \6 s
  9.                not timed out waiting )! w, q  ^8 q( y' U8 k/ ]( E5 z9 D% p. s/ O
  10.         {* S' z; F/ _) S9 b- d( R/ t
  11.             Enter the blocked state to wait for space in the buffer8 P+ {; m% l" Y" K' d
  12.         }3 D; S; j' T) @3 w2 O' B
  13.     }4 I" X! f" M3 u; u
  14.   r; ?9 b4 C) x* e7 S
复制代码
  1. if( there is enough space in the buffer )
    . ~6 H* K9 A) E7 y: {& Y
  2. {
    / ~. H7 n; u- \9 y/ j( O6 A
  3. write data to buffer' B* I; j- p+ M. I+ A' c5 Z% I
  4. sbSEND_COMPLETED()
    4 {' u" k3 F& }4 ^$ v! _
  5. }  g2 O0 o. _6 X2 [" Y, K
  6. }
复制代码

9 V9 D; {& n$ C; b* I

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

  1. xMessageBufferReceive()/ g: `) z3 @( A2 \
  2. {) f2 t( p" h7 K
  3.     /* If a time out is specified and the buffer doesn't" r( E5 Z/ y9 J
  4.     contain any data that can be read, then enter the
    - }1 d9 q  S( S* Q2 z  E$ Q) o& f
  5.     blocked state to wait for the buffer to contain data. */! j! N8 H' r5 ^7 p. y
  6.     if( time out != 0 )
      b0 P5 w3 Z' X9 n) `( _1 U
  7.     {
    0 N  ?3 n7 H( P9 L" ^4 J! S3 a
  8.         while( there is no data in the buffer &&+ ?7 k7 y) B# Y4 m7 U- _
  9.                not timed out waiting )
    4 o2 ~/ d% u( \
  10.         {. G' f) c) Z3 c+ ?
  11.             Enter the blocked state to wait for data
    $ i$ _* _7 b* c# B8 N$ l( Z) ~8 C* O
  12.         }0 s9 V" x4 g0 j2 ^( L+ E
  13.     }3 O$ z9 p; g4 D- }3 N8 b
  14. 3 C" T( V+ I% `$ s* p; h
复制代码
  1. if( there is data in the buffer )2 K# ]7 F8 q* i6 A2 q; O/ k$ ^( K. b
  2. {' w9 `1 h) r- a" m
  3. read data from buffer
    9 G$ g. k. f* _; M9 V
  4. sbRECEIVE_COMPLETED()0 F$ z% X) V+ ~7 r& Y  @3 g, e
  5. }
    . U% b5 f' M1 O2 W! s
  6. }
复制代码

. k1 o: r5 k+ Z7 ]' V

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

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


, c! ?/ ^) \& Z. W& [% F

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

  q) N6 S1 ]7 }& w/ S. R

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

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

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

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

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


/ @% b3 g" ^& b. i+ q1 s8 A

四、多消息代码描述

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

" s" C/ H* W0 j

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

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

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

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


    4 {. ?7 I( ]/ E' `& X
    # b6 J5 M6 D3 n, ^9 k- a. O

7 n$ g! ~/ K# W

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


& B& f$ E8 C$ W+ \; K3 Q0 B

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


# p* N: Y* [& W( t. D1 F

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

  1. /* Added to FreeRTOSConfig.h to override the default implementation. */
    / }8 U% H( a3 s& {1 f7 U4 X0 G) q
  2. #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )3 }# R6 {# E2 ]( [/ c- J* l  Q3 n- Y
复制代码
  1. /* Implemented in a C file. */
    4 R- o3 e- t( J! a
  2. void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer )( z& k9 Z2 w  K  v
  3. {( \* F$ X" D; ?) Z
  4. size_t BytesWritten.! J0 S9 J. v2 y+ K3 l. t6 w
复制代码
  1. /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.
    $ B# E8 m4 B; m( c/ c6 U
  2. If this function was called because data was written to any message buffer1 X! s0 e5 y: ?" F; q2 ~
  3. other than the control message buffer then write the handle of the message
    / R7 H% X8 W  \+ y; Z$ f% C
  4. buffer that contains data to the control message buffer, then raise an
    " e7 `; i8 s1 m1 C
  5. interrupt in the other core. If this function was called because data was& [6 [8 M. Z" a( e0 Y
  6. written to the control message buffer then do nothing. */  h5 O- ^5 N9 n4 V! Z, N" f5 a# O
  7. if( xUpdatedBuffer != xControlMessageBuffer )! {/ a$ N( G1 Y
  8. {3 U0 b- @' ~2 m
  9.     BytesWritten = xMessageBufferSend( xControlMessageBuffer,
    ! w$ H% M9 n4 j' l5 w' @! H
  10.                                                          &xUpdatedBuffer,
    9 _- ~2 O# o2 `5 U# k/ a* {* C6 V# u
  11.                                                          sizeof( xUpdatedBuffer ),
    8 w4 d8 ~7 l7 @* O9 ^% c. O
  12.                                                           0 );
    : M8 @* {( B9 Z2 ~7 x1 c# _
复制代码
  1. /* If the bytes could not be written then the control message buffer
    $ ^/ e* ?8 O: K0 T$ B
  2. is too small! */9 G: }- ?) b8 F& h) |
  3. configASSERT( BytesWritten == sizeof( xUpdatedBuffer );% P! m6 x( ?7 E
复制代码
  1. /* Generate interrupt in the other core (pseudocode). */
    0 e0 Y; h6 U, k4 N8 C. y/ i4 H" P8 [
  2. GenerateInterrupt();' c6 }% l1 Q( m- e7 s0 I
  3. }# y/ s4 \; y7 y
  4. }
复制代码

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

  1. void InterruptServiceRoutine( void )
    ' y. Y# ]0 l& c! N
  2. {6 b, M6 B! I4 z$ L9 `
  3. MessageBufferHandle_t xUpdatedMessageBuffer;/ u3 u" K1 i4 T2 J% w/ o- a9 Z
  4. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    4 H5 L  \9 D% F2 x1 F$ j+ A
复制代码
  1. /* Receive the handle of the message buffer that contains data from the5 k  u8 N9 a' f2 P/ R! _  y
  2. control message buffer. Ensure to drain the buffer before returning. */; u$ ^; t1 {$ q3 X' z
  3. while( xMessageBufferReceiveFromISR( xControlMessageBuffer,
    7 b0 P0 M! p4 T0 z: K( i9 d% ^
  4. &xUpdatedMessageBuffer,& O5 p1 o! V6 Q
  5. sizeof( xUpdatedMessageBuffer ),% [' Q( g+ x* [$ u0 c  n% ?
  6. &xHigherPriorityTaskWoken )
    & R9 q" ^# n+ J$ d2 A! G, g. y
  7. == sizeof( xUpdatedMessageBuffer ) ); m% K8 C* e6 ]
  8. {1 R4 @$ P  V7 U4 a7 b: E; T
  9. /* Call the API function that sends a notification to any task that is+ b- p+ N3 k3 M" u/ J8 s+ I
  10. blocked on the xUpdatedMessageBuffer message buffer waiting for data to
    ( D  {2 B, d9 F8 r4 K
  11. arrive. */: R7 P! W7 \( b( K
  12. xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,: d# r. B/ @6 Z
  13. &xHigherPriorityTaskWoken );  |/ ~" l, O5 T) L% _
  14. }
    . }5 Q" q$ C$ |" E
复制代码
  1. /* Normal FreeRTOS "yield from interrupt" semantics, where
      t; ?  F! [, Z
  2. xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to/ s1 o0 D1 p  k( w% A7 O
  3. pdTRUE if the interrupt unblocks a task that has a priority above that of
    5 q- \4 m6 |7 P- Y6 G
  4. the currently executing task. */$ X' |0 B# b4 s4 R- F% [
  5. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
    * ]6 u, W3 J$ D
  6. }
复制代码
( I* z; f' h% ^* j* U

0 A$ a2 y% H/ u3 M  c) s0 f2 \

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

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

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

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

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

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


/ H+ A7 h; _% C3 |1 H

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


* p: r0 V3 o! m. N, O. J
) i/ h& v, o$ x- H
收藏 评论1 发布时间:2021-12-30 14:49

举报

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

好好学习

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版