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

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

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

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

一、概述

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

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

二、基本原理

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

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

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

三、单消息代码描述

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

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

  1. xMessageBufferSend()) x5 ~7 C* a# h0 _' L. ^! z
  2. {
    7 A$ a+ `* b9 {+ k/ i
  3.     /* If a time out is specified and there isn't enough
    + R0 W) P0 h+ w8 ~; W
  4.     space in the message buffer to send the data, then
    1 |, D) A, k- B5 E
  5.     enter the blocked state to wait for more space. */7 z, w* m  }6 }9 |) x# M+ ]' y
  6.     if( time out != 0 )/ `7 R. B/ \( [1 X
  7.     {
    9 g" B8 H; f4 e4 C
  8.         while( there is insufficient space in the buffer &&
    3 o7 U/ P. X( q9 M. j1 Z
  9.                not timed out waiting )
    + z- W2 i2 k3 v' J) S
  10.         {' v" O( W: @! D9 @2 o' d- c6 Q
  11.             Enter the blocked state to wait for space in the buffer2 U. h+ Y* }5 @
  12.         }
    6 i( b% Q9 h2 n9 A
  13.     }
    ( h& n, I" z0 N
  14. 8 [, e' c1 o% a8 g' y
复制代码
  1. if( there is enough space in the buffer )& U. v$ \& c5 M6 @, }6 u. N
  2. {
    - |) d! {, w# Z$ ?7 h1 s
  3. write data to buffer
    : ~# J. C+ W# m2 X
  4. sbSEND_COMPLETED()
    2 W" C! s2 R# E5 y* h" t9 e+ I5 p
  5. }" }) c" ?, p! C: D4 G& _
  6. }
复制代码
+ e2 q2 s7 P0 f( q9 ^5 e

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

  1. xMessageBufferReceive()6 Z5 c+ E2 B# ]" [, t9 ?
  2. {
    ; B' Z1 ~% l3 x/ H. u
  3.     /* If a time out is specified and the buffer doesn't  q. _/ [- _5 [- q0 f& E
  4.     contain any data that can be read, then enter the
    7 R5 F8 T7 q- {4 I: N. {
  5.     blocked state to wait for the buffer to contain data. */6 n0 E0 J8 }% [4 |+ h" o/ f
  6.     if( time out != 0 )- l' I8 _$ }* [/ G! l5 V, X
  7.     {& J% T: Y2 c* a% R5 O4 d" n% s
  8.         while( there is no data in the buffer &&
    ! m5 F* {8 J, \. n
  9.                not timed out waiting )
    6 X5 H2 `& ~; n
  10.         {9 j0 H6 r  w9 I. F
  11.             Enter the blocked state to wait for data
    7 x( Y9 D& x. i7 t9 n' m  {
  12.         }
    ) X7 U3 o! F$ P6 V
  13.     }% W* `: l( V% ~6 d2 ?5 ^
  14. * B# C2 l1 A, i6 [8 s
复制代码
  1. if( there is data in the buffer )1 L* H: N) W$ @
  2. {
    0 A6 w5 P* q% s: ]0 k9 N; D
  3. read data from buffer
    7 p6 @4 _, c4 |5 C$ y
  4. sbRECEIVE_COMPLETED()6 ?7 D7 n9 x) Z2 F. N
  5. }
    / b2 g/ ]0 g: J( T7 S8 w$ X
  6. }
复制代码
& w, o. Y6 {; D$ h5 Y) t

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

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


: z3 s4 I' K6 M  q8 y: A( [

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

0 p2 U+ z5 C5 ]" I( @7 I, a

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

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

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

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

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


( y, D) q# Z# {; p: j0 Z4 X5 k

四、多消息代码描述

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

. b1 v6 s4 W: g  r$ @

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

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

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

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

    8 x9 v9 ?- s9 l6 U! i6 [/ t( n- D' j
    + X9 g; I# k: C' Z* {

. z, Q2 j; g8 c, r

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

2 N% r; c. }/ G$ L4 H

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


; i2 Q; t) m5 [* V1 N" X  p

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

  1. /* Added to FreeRTOSConfig.h to override the default implementation. */$ p; k5 r" ^+ S- [' n7 V' L
  2. #define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreToCoreInterrupt( pxStreamBuffer )
    , f3 i" \$ A9 v2 R' S! W
复制代码
  1. /* Implemented in a C file. */4 [7 a( }2 J: r* {
  2. void vGenerateCoreToCoreInterrupt( MessageBufferHandle_t xUpdatedBuffer )
    # ]  M( A' G2 C; w) u" S) }
  3. {
    ) U$ _, w- x# F8 t! K$ l: B
  4. size_t BytesWritten.* x' y/ N+ w3 L! N
复制代码
  1. /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.
    : i0 V& g, H$ B
  2. If this function was called because data was written to any message buffer
    , m: o$ o5 a- P. O. z2 A, U
  3. other than the control message buffer then write the handle of the message
    7 O/ Z  R* L, i0 ]( @7 u3 \
  4. buffer that contains data to the control message buffer, then raise an) m* S0 i. i0 ?" N2 N. B
  5. interrupt in the other core. If this function was called because data was3 U3 r1 C" {3 L: u( E7 }: i
  6. written to the control message buffer then do nothing. */
    7 j1 [- S5 }- R# H) t) H$ ~4 M
  7. if( xUpdatedBuffer != xControlMessageBuffer )
    8 B: `0 k3 P! Z7 f7 Y5 _. y. q
  8. {+ a: Q, U" [5 S) X* {: k. L3 ~+ k
  9.     BytesWritten = xMessageBufferSend( xControlMessageBuffer,
    ) C$ y" F# H) w2 Y) t$ L8 l/ u
  10.                                                          &xUpdatedBuffer,
    7 V/ V. h- Q- U. n( r, M
  11.                                                          sizeof( xUpdatedBuffer ),6 U/ ?3 N0 V, @. d/ t
  12.                                                           0 );
    $ s: d$ U* B% j6 T& `6 R
复制代码
  1. /* If the bytes could not be written then the control message buffer
    . s$ u* f  ^9 Q0 @' W) P6 {9 }
  2. is too small! */
    9 n( |+ c) T+ s' F: G
  3. configASSERT( BytesWritten == sizeof( xUpdatedBuffer );
    6 P3 A$ w+ ]7 i
复制代码
  1. /* Generate interrupt in the other core (pseudocode). */
    : }0 ?" O' z+ ]9 U: m4 F
  2. GenerateInterrupt();
    ( C: Q, M5 L& w5 R
  3. }
    % L* a; i) i% `5 O) {, D' v$ x# h
  4. }
复制代码

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

  1. void InterruptServiceRoutine( void )% X7 e/ d* f& P9 l+ J
  2. {
    7 V& a, p" e& Q6 v1 z' X
  3. MessageBufferHandle_t xUpdatedMessageBuffer;
    : e8 |+ K7 t' e* @7 `
  4. BaseType_t xHigherPriorityTaskWoken = pdFALSE;# {. {9 T: q# K+ Q, t  o. o
复制代码
  1. /* Receive the handle of the message buffer that contains data from the
    , `) l  x2 y  W; h' r4 V5 V* V
  2. control message buffer. Ensure to drain the buffer before returning. */0 ]% \: V6 L4 }8 u  J( J
  3. while( xMessageBufferReceiveFromISR( xControlMessageBuffer,$ a, r- g4 ?$ a
  4. &xUpdatedMessageBuffer,
    0 l6 A( C( [& @3 o4 S+ E2 r$ K
  5. sizeof( xUpdatedMessageBuffer ),1 p* v* O- B1 }
  6. &xHigherPriorityTaskWoken )& Z7 X4 ~0 o: L5 u
  7. == sizeof( xUpdatedMessageBuffer ) )
    8 e: S5 q1 p" J7 y. I; f
  8. {- N, y* [2 C. h, M
  9. /* Call the API function that sends a notification to any task that is3 |! x' R# M* X6 K: P, ]* B, ~4 _
  10. blocked on the xUpdatedMessageBuffer message buffer waiting for data to
    0 k4 r  M" f6 W$ ]2 z
  11. arrive. */, T, r' R) I# R/ r' W
  12. xMessageBufferSendCompletedFromISR( xUpdatedMessageBuffer,
    3 E! [; a! K* D" D( i7 O" a9 Q( m# M
  13. &xHigherPriorityTaskWoken );- ~! p5 E- v* r2 P6 v, o% u
  14. }+ v: e( {! Q$ }1 k& H
复制代码
  1. /* Normal FreeRTOS "yield from interrupt" semantics, where. D# `3 e6 o! ]& W& x. z( D# a
  2. xHigherPriorityTaskWoken is initialised to pdFALSE and will then get set to
    8 W4 F+ k9 \7 F; j
  3. pdTRUE if the interrupt unblocks a task that has a priority above that of3 O: S( ]$ ]- K4 D; R+ g# k  K
  4. the currently executing task. */0 y( `( D% C9 y* T
  5. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
    . S7 a3 Q  _2 l, i- W5 ~+ I& \
  6. }
复制代码

/ R1 B, B, t# Y: f. Z6 x% b# v  N) T8 ^

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

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

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

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

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

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

. F) Y, F, Z* s$ h

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


) A. [2 S8 S/ S

% O0 C! Z) O5 ^8 B
收藏 评论1 发布时间:2021-12-30 14:49

举报

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

好好学习

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