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

【经验分享】FreeRTOS临界区

[复制链接]
STMCU小助手 发布时间:2022-3-26 15:00
01. 概述# y  w; m- d) `" ?; g" K
临界段代码也叫做临界区,是指那些必须完整运行,不能被打断的代码段,比如有的外设的初始化需要严格的时序,初始化过程中不能被打断。FreeRTOS在进入临界端代码的时候需要关闭中断,当处理完临界段代码以后再打开中断。FreeRTOS系统本身就有很多的临界段代码。这些代码都加了临界段代码保护,我们在写自己的用户程序的时候有些地方也需要添加临界段代码保护。, p* x, C: d; q$ d5 V4 }7 B
; z. S( ~; |0 U1 n
FreeRTOS与临界段代码保护有关的函数有4个:
0 l7 c! k! P- i0 L3 n3 u( \0 c- a' N# K
  1. /**
    ; T5 t& X& y% c. C" L; J9 }
  2. * task. h
    8 x2 q" V/ P! L. p. @, N
  3. *
    * \- w! S6 C: M) _. {4 A5 r+ M
  4. * Macro to mark the start of a critical code region.  Preemptive context; K$ I9 J7 o- r
  5. * switches cannot occur when in a critical region.' p4 U& N" l3 y- B5 A- r
  6. *6 O. M9 m. u! K1 I* Z
  7. * NOTE: This may alter the stack (depending on the portable implementation)8 a! j( b  ]7 Q+ f7 c8 c
  8. * so must be used with care!
    % }7 z: _: ^6 Y. i4 L
  9. *# y: J0 S" j5 R: R. e% C! M
  10. * \defgroup taskENTER_CRITICAL taskENTER_CRITICAL7 e, z5 V' X6 j# j- n+ m7 n- S4 a
  11. * \ingroup SchedulerControl
    # E% m5 C0 n% ~' v" i
  12. */0 h. a  v% b& m
  13. #define taskENTER_CRITICAL()               portENTER_CRITICAL() //任务级  w$ k$ g/ J( C" q4 {2 W' H
  14. #define taskENTER_CRITICAL_FROM_ISR()      portSET_INTERRUPT_MASK_FROM_ISR() //中断级
    6 ]8 S0 {9 F  i: v5 D- [

  15. # c' l, a7 B2 \9 z
  16. /**
    5 j# o! l9 D4 W( L& t- P; G, s' m
  17. * task. h
    3 C  e1 y) q: n/ T/ F" B8 q* S6 e
  18. *
    * a0 R' i! G6 e9 V+ Z4 T
  19. * Macro to mark the end of a critical code region.  Preemptive context6 I2 T: f* K: [3 b) E
  20. * switches cannot occur when in a critical region.6 A- }9 w9 C; M5 Y
  21. *
    ( J3 A5 [% x  \/ ]
  22. * NOTE: This may alter the stack (depending on the portable implementation), Y! y) x  [# \# Y3 v
  23. * so must be used with care!3 Y& z+ T5 y; m- W5 ]6 G8 A8 ]9 l
  24. *2 l( y% S, d* n7 ~; r) e
  25. * \defgroup taskEXIT_CRITICAL taskEXIT_CRITICAL
    ' a" @: i6 I  q: f& o! y1 D
  26. * \ingroup SchedulerControl
    ( v* K; B6 ?9 i) W2 v4 S/ u' }% j) e7 N
  27. */
    - C' k$ S4 A! E, V" F. P
  28. #define taskEXIT_CRITICAL()                portEXIT_CRITICAL() //任务级
    * f% s8 E6 t: e5 U6 M
  29. #define taskEXIT_CRITICAL_FROM_ISR( x )    portCLEAR_INTERRUPT_MASK_FROM_ISR( x ) //中断级. t( R( Z$ R/ ?
复制代码

1 E+ y+ p- C& G$ u- m02. 任务级临界区代码保护
( [0 s; S5 B$ W( S- ytaskENTER_CRITICAL()和taskEXIT_CRITICAL()是任务级的临界区代码保护,一个是进入临界区,一个是退出临界区,这两个函数一定要成对的使用。9 Y3 \" E* n+ c. d; G

5 b  o  t! E- E6 n& Z. [: I1 K
  1. /**
    0 K8 a% T% O' {' _7 w
  2. * task. h! v! a* l; a# ^; a: ^
  3. *
    , A5 C7 p: Z6 E. K$ J( K
  4. * Macro to mark the start of a critical code region.  Preemptive context  ^7 ?- D! x4 Y6 q. k7 ^( T
  5. * switches cannot occur when in a critical region.
    9 w0 p' {- L0 j) Z+ ~5 K
  6. *
    % i1 `( l" d5 l( [& W5 X
  7. * NOTE: This may alter the stack (depending on the portable implementation)
    : Z1 i5 B1 t$ p  g; ^, N& f
  8. * so must be used with care!4 P; G5 _+ l8 m+ `! T0 U  S
  9. *
    : U& f2 d0 j2 ~$ v& @! `$ p
  10. * \defgroup taskENTER_CRITICAL taskENTER_CRITICAL
    & m. `5 U9 S. ?! ]4 w* R& ]
  11. * \ingroup SchedulerControl
    % [' o3 A" W; }. J9 `8 q
  12. */
    ' q6 ^, p+ W! h, F7 X
  13. #define taskENTER_CRITICAL()               portENTER_CRITICAL() //任务级
    & G  n; X% ~# N6 E! |  E" K
  14. 4 w2 S! w  A7 Y2 G$ J1 t! d# O% X
  15. 4 @0 A0 @  ]& b' k3 W! ~* Y+ I3 a) n2 W
  16. /**" |! M8 n) f/ @: z3 w# r/ ^+ V
  17. * task. h, [) e6 e3 {. ]% Q3 ^- y& ^/ Y' g
  18. *# D' C  d5 k6 C9 |5 O1 D
  19. * Macro to mark the end of a critical code region.  Preemptive context
    0 D3 Z7 W1 N$ q/ L/ p
  20. * switches cannot occur when in a critical region.3 ^; i+ ~; ?1 a, r" I0 \. P( }3 f8 _9 D, W
  21. *
    & |' }( X. k+ b7 ]9 J- u
  22. * NOTE: This may alter the stack (depending on the portable implementation)
    / P+ S1 U# s+ {+ X
  23. * so must be used with care!- j) t0 q  u! Q+ O& s* o" q5 i0 B
  24. *2 j( A9 d3 C3 I/ a, `7 z# u4 D9 b
  25. * \defgroup taskEXIT_CRITICAL taskEXIT_CRITICAL
    $ N" {, x: @  x5 {$ U. R6 \
  26. * \ingroup SchedulerControl
    ! {2 n( _! X/ f$ A; k3 m5 P6 W
  27. */; B% K. Q8 H3 b( I  ~" z
  28. #define taskEXIT_CRITICAL()                portEXIT_CRITICAL() //任务级
    0 }* ?8 i* F6 G/ t8 c6 a  [0 i
复制代码

! O  F0 S( c, q1 i' _2 UportENTER_CRITICAL()和portEXIT_CRITICAL()也是宏定义,在portmacro.h中有定义。
' j- S/ g7 H( U% Q. Y4 c
  1.     #define portENTER_CRITICAL()                      vPortEnterCritical()2 b$ ?, \/ p9 p' x& U$ S6 @1 a
  2.     #define portEXIT_CRITICAL()                       vPortExitCritical()
复制代码
7 _% B7 n$ D$ H  o( _, Y
vPortEnterCritical()和vPortExitCritical()函数实现如下:
  1. void vPortEnterCritical( void )
    $ T  J5 u' R# y
  2. {( y. M) B9 }2 j; X9 r# B% Z
  3.     portDISABLE_INTERRUPTS();
    9 M- ]- k0 `: b  R2 k3 V  ]  e
  4.     uxCriticalNesting++;
    4 Z$ r/ F, C' b8 j) }/ ~. j
  5. 7 N6 c; [0 |. i# x
  6.     /* This is not the interrupt safe version of the enter critical function so9 r/ l: _6 M8 F8 {- s; w$ L
  7.      * assert() if it is being called from an interrupt context.  Only API
    ) ?0 S  m+ U# c$ V
  8.      * functions that end in "FromISR" can be used in an interrupt.  Only assert if
    : b9 S0 j/ m, o  d5 X4 x
  9.      * the critical nesting count is 1 to protect against recursive calls if the# O. {' w5 o% ~7 l9 V/ v, e
  10.      * assert function also uses a critical section. */
    . D. o+ Z8 l, d; L
  11.     if( uxCriticalNesting == 1 )! x7 X  x0 T1 N# O, j
  12.     {
    % m( o* O. }. q6 f
  13.         configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );6 _" H9 c$ f" e$ u) u0 A( f# O! q
  14.     }9 ]: }9 @$ h  K' }& J5 o: X
  15. }
    , ?1 f- ?$ `, ]! k) }# N

  16. 3 o. V2 U" T* S  m
  17. void vPortExitCritical( void )3 x' q9 n  T; `6 ^% @
  18. {
    2 b* \6 k2 H2 J! Z" t5 y7 S+ R
  19.     configASSERT( uxCriticalNesting );
    3 a4 y- _( |  t3 u: u+ X
  20.     uxCriticalNesting--;& |/ Q7 ^* K7 C& a, U

  21. 1 B4 S# P- C# G2 U
  22.     if( uxCriticalNesting == 0 )
    . _8 f; [% L+ a& ~( E
  23.     {
    6 |6 H) B  i) D9 N8 J, M  v0 @
  24.         portENABLE_INTERRUPTS();
    2 d7 e  K5 _4 u, A
  25.     }
    " n3 P1 e# d" Q
  26. }
复制代码
3 P& t5 g1 ^/ ^: _) N
03.中断级临界区代码保护
$ W( q1 D5 F' mtaskENTER_CRITICAL_FROM_ISR()和taskEXIT_CRITICAL_FROM_ISR()是中断级别临界区代码保护,是用在中断服务程序中的,而且这个中断的优先级一定要低于预先设置的值。0 t% T- y7 k& @- Z* n& @
+ Q5 M' w& C- d9 [3 S- L
  1. /**- @3 v, g6 l/ m/ x/ J. t. O
  2. * task. h
    # |- m: I2 N% A6 p( ~- h+ ~1 G
  3. *! F6 ]# T8 Q$ ^0 L" |" |
  4. * Macro to mark the start of a critical code region.  Preemptive context# w' E/ _: V8 W
  5. * switches cannot occur when in a critical region.
    $ D5 W5 ?; ~( ~% I! E! Q
  6. *
    8 z2 D* h. u0 t+ O0 U+ w
  7. * NOTE: This may alter the stack (depending on the portable implementation)
    $ x0 E, C2 R0 n1 C1 k
  8. * so must be used with care!- U/ p9 s' G2 F9 Z1 v, `
  9. *
    3 H  c* }, q! a, t+ O
  10. * \defgroup taskENTER_CRITICAL taskENTER_CRITICAL
    ( V( U7 t# @+ q
  11. * \ingroup SchedulerControl" y+ Q  _7 ~. W4 P3 q; |- N0 t
  12. */
    8 ?8 j& X  _" x  R. ]& l
  13. #define taskENTER_CRITICAL_FROM_ISR()      portSET_INTERRUPT_MASK_FROM_ISR() //中断级* C% c$ _4 t9 f3 X
  14. , Y" r- N  A  o5 x( j9 o
  15. /**; ]' P5 ?& a; G" l
  16. * task. h) x5 T" e8 I! `. N
  17. *
    : G; m% {3 D* w4 [) n% u4 Z; w7 ]4 Q
  18. * Macro to mark the end of a critical code region.  Preemptive context
    ! Q8 r8 h: G# z6 X" l+ n
  19. * switches cannot occur when in a critical region.
    . \5 D8 P, [9 }2 j
  20. *
    , n/ R4 d9 E0 Y0 [# s/ ^, ~
  21. * NOTE: This may alter the stack (depending on the portable implementation)$ W7 J* u) B7 r  P
  22. * so must be used with care!
    3 ]0 \: x0 E7 W) Z0 U+ A, ?
  23. *
    ) A  S6 x% c# [
  24. * \defgroup taskEXIT_CRITICAL taskEXIT_CRITICAL
    ; X* X+ ]6 Z# Y. c: C' `: O
  25. * \ingroup SchedulerControl4 t7 V" G- q; e2 o+ Q2 f, r
  26. */
    3 L' R0 _5 a) R) T
  27. #define taskEXIT_CRITICAL_FROM_ISR( x )    portCLEAR_INTERRUPT_MASK_FROM_ISR( x ) //中断级( @/ \4 ]2 z+ Q- {7 B
复制代码
2 s% i' F, F8 I* m- G( Z: m2 K
在portmacro.h文件中有如下定义:
  1.     #define portSET_INTERRUPT_MASK_FROM_ISR()         ulPortRaiseBASEPRI()
    " u5 b0 s/ g/ o. E
  2.     #define portCLEAR_INTERRUPT_MASK_FROM_ISR( x )    vPortSetBASEPRI( x )
复制代码

# ]* Z5 {% G* t1 ^) w7 ]ulPortRaiseBASEPRI()和vPortSetBASEPRI()相关实现
, v9 x1 W* P1 ?! k) l: h: _8 q, p, T% _/ w0 t6 T0 A, y
  1.     static portFORCE_INLINE void vPortSetBASEPRI( uint32_t ulBASEPRI )
    + @' ]/ _$ i6 d' u2 i
  2.     {, u) Y4 H; g) z( x% o/ W
  3.         __asm
    ( ~8 c7 V- C6 K! H0 c! z4 C
  4.         {
    & k6 @: u; v" P; t
  5.             /* Barrier instructions are not used as this function is only used to  W! h& m6 t& M3 k
  6.              * lower the BASEPRI value. */" z, R  E1 L* C- f* A2 J* {
  7. /* *INDENT-OFF* */6 q' s* r1 G0 ^+ R! u
  8.         msr basepri, ulBASEPRI* _1 M5 ~8 H2 t. |/ y
  9. /* *INDENT-ON* */
    1 n& I5 I* k" t, u3 M
  10.         }
    5 c+ r4 }3 {- {' _7 ]5 \# p2 w+ d  v
  11.     }# c- |4 N# S, I0 L& a

  12. % K! K5 L( C* M1 V! N
  13.     static portFORCE_INLINE uint32_t ulPortRaiseBASEPRI( void )
    $ R+ j% i1 J8 _; ~0 ]( z
  14.     {
    ! E5 S% a* R$ ]7 C. F3 A% g* L9 x0 V; g
  15.         uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
    + R- _% B: }: u0 M* p

  16. ) d: o+ U  \& u- @- Z
  17.         __asm
    5 I1 `* U6 D; c8 Q! o7 J5 o8 c! O- R
  18.         {
    * k& R, ^  @% h! O# n& W
  19.             /* Set BASEPRI to the max syscall priority to effect a critical
    " {. L- C% O* D, c5 h2 `: Q
  20.              * section. */4 H* ^! k8 F6 p7 M
  21. /* *INDENT-OFF* */
    , u! N: @% B+ w* C" ]
  22.             mrs ulReturn, basepri
    9 f# \  S7 ?7 }6 |1 W( x; @5 }
  23.             msr basepri, ulNewBASEPRI0 _, U$ F) P3 `5 ^
  24.             dsb
    ) o/ e7 [$ |8 _5 p: s
  25.             isb  A( H1 J! \+ F0 M( y# e+ k2 w
  26. /* *INDENT-ON* */( s$ i% W& @( T
  27.         }
    " {" N0 o8 Q- p  x; y# L
  28.   K3 x4 J/ O" t: j5 Z1 b
  29.         return ulReturn;
    ! v7 z* _5 S. |% ]( q" p* [% X
  30.     }
复制代码
中断级临界区代码应用示例3 N% e* Z" b( ^6 o% g4 l& x4 s6 |
  L8 e: g2 p+ K& @/ E/ j
(`]T2%D}G5A4[}`JCH680VY.png ) C* i3 q4 Q' w* u* H. M* T' w, `8 r
5 R/ B0 ]. ^" R- @$ R* ]

  y- U! ?/ H  G" m! u8 r( _
赞 收藏 评论0 发布时间:2022-3-26 15:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

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