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
- /**
; T5 t& X& y% c. C" L; J9 } - * task. h
8 x2 q" V/ P! L. p. @, N - *
* \- w! S6 C: M) _. {4 A5 r+ M - * Macro to mark the start of a critical code region. Preemptive context; K$ I9 J7 o- r
- * switches cannot occur when in a critical region.' p4 U& N" l3 y- B5 A- r
- *6 O. M9 m. u! K1 I* Z
- * NOTE: This may alter the stack (depending on the portable implementation)8 a! j( b ]7 Q+ f7 c8 c
- * so must be used with care!
% }7 z: _: ^6 Y. i4 L - *# y: J0 S" j5 R: R. e% C! M
- * \defgroup taskENTER_CRITICAL taskENTER_CRITICAL7 e, z5 V' X6 j# j- n+ m7 n- S4 a
- * \ingroup SchedulerControl
# E% m5 C0 n% ~' v" i - */0 h. a v% b& m
- #define taskENTER_CRITICAL() portENTER_CRITICAL() //任务级 w$ k$ g/ J( C" q4 {2 W' H
- #define taskENTER_CRITICAL_FROM_ISR() portSET_INTERRUPT_MASK_FROM_ISR() //中断级
6 ]8 S0 {9 F i: v5 D- [
# c' l, a7 B2 \9 z- /**
5 j# o! l9 D4 W( L& t- P; G, s' m - * task. h
3 C e1 y) q: n/ T/ F" B8 q* S6 e - *
* a0 R' i! G6 e9 V+ Z4 T - * Macro to mark the end of a critical code region. Preemptive context6 I2 T: f* K: [3 b) E
- * switches cannot occur when in a critical region.6 A- }9 w9 C; M5 Y
- *
( J3 A5 [% x \/ ] - * NOTE: This may alter the stack (depending on the portable implementation), Y! y) x [# \# Y3 v
- * so must be used with care!3 Y& z+ T5 y; m- W5 ]6 G8 A8 ]9 l
- *2 l( y% S, d* n7 ~; r) e
- * \defgroup taskEXIT_CRITICAL taskEXIT_CRITICAL
' a" @: i6 I q: f& o! y1 D - * \ingroup SchedulerControl
( v* K; B6 ?9 i) W2 v4 S/ u' }% j) e7 N - */
- C' k$ S4 A! E, V" F. P - #define taskEXIT_CRITICAL() portEXIT_CRITICAL() //任务级
* f% s8 E6 t: e5 U6 M - #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- /**
0 K8 a% T% O' {' _7 w - * task. h! v! a* l; a# ^; a: ^
- *
, A5 C7 p: Z6 E. K$ J( K - * Macro to mark the start of a critical code region. Preemptive context ^7 ?- D! x4 Y6 q. k7 ^( T
- * switches cannot occur when in a critical region.
9 w0 p' {- L0 j) Z+ ~5 K - *
% i1 `( l" d5 l( [& W5 X - * NOTE: This may alter the stack (depending on the portable implementation)
: Z1 i5 B1 t$ p g; ^, N& f - * so must be used with care!4 P; G5 _+ l8 m+ `! T0 U S
- *
: U& f2 d0 j2 ~$ v& @! `$ p - * \defgroup taskENTER_CRITICAL taskENTER_CRITICAL
& m. `5 U9 S. ?! ]4 w* R& ] - * \ingroup SchedulerControl
% [' o3 A" W; }. J9 `8 q - */
' q6 ^, p+ W! h, F7 X - #define taskENTER_CRITICAL() portENTER_CRITICAL() //任务级
& G n; X% ~# N6 E! | E" K - 4 w2 S! w A7 Y2 G$ J1 t! d# O% X
- 4 @0 A0 @ ]& b' k3 W! ~* Y+ I3 a) n2 W
- /**" |! M8 n) f/ @: z3 w# r/ ^+ V
- * task. h, [) e6 e3 {. ]% Q3 ^- y& ^/ Y' g
- *# D' C d5 k6 C9 |5 O1 D
- * Macro to mark the end of a critical code region. Preemptive context
0 D3 Z7 W1 N$ q/ L/ p - * switches cannot occur when in a critical region.3 ^; i+ ~; ?1 a, r" I0 \. P( }3 f8 _9 D, W
- *
& |' }( X. k+ b7 ]9 J- u - * NOTE: This may alter the stack (depending on the portable implementation)
/ P+ S1 U# s+ {+ X - * so must be used with care!- j) t0 q u! Q+ O& s* o" q5 i0 B
- *2 j( A9 d3 C3 I/ a, `7 z# u4 D9 b
- * \defgroup taskEXIT_CRITICAL taskEXIT_CRITICAL
$ N" {, x: @ x5 {$ U. R6 \ - * \ingroup SchedulerControl
! {2 n( _! X/ f$ A; k3 m5 P6 W - */; B% K. Q8 H3 b( I ~" z
- #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- #define portENTER_CRITICAL() vPortEnterCritical()2 b$ ?, \/ p9 p' x& U$ S6 @1 a
- #define portEXIT_CRITICAL() vPortExitCritical()
复制代码 7 _% B7 n$ D$ H o( _, Y
vPortEnterCritical()和vPortExitCritical()函数实现如下:- void vPortEnterCritical( void )
$ T J5 u' R# y - {( y. M) B9 }2 j; X9 r# B% Z
- portDISABLE_INTERRUPTS();
9 M- ]- k0 `: b R2 k3 V ] e - uxCriticalNesting++;
4 Z$ r/ F, C' b8 j) }/ ~. j - 7 N6 c; [0 |. i# x
- /* This is not the interrupt safe version of the enter critical function so9 r/ l: _6 M8 F8 {- s; w$ L
- * assert() if it is being called from an interrupt context. Only API
) ?0 S m+ U# c$ V - * functions that end in "FromISR" can be used in an interrupt. Only assert if
: b9 S0 j/ m, o d5 X4 x - * the critical nesting count is 1 to protect against recursive calls if the# O. {' w5 o% ~7 l9 V/ v, e
- * assert function also uses a critical section. */
. D. o+ Z8 l, d; L - if( uxCriticalNesting == 1 )! x7 X x0 T1 N# O, j
- {
% m( o* O. }. q6 f - configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );6 _" H9 c$ f" e$ u) u0 A( f# O! q
- }9 ]: }9 @$ h K' }& J5 o: X
- }
, ?1 f- ?$ `, ]! k) }# N
3 o. V2 U" T* S m- void vPortExitCritical( void )3 x' q9 n T; `6 ^% @
- {
2 b* \6 k2 H2 J! Z" t5 y7 S+ R - configASSERT( uxCriticalNesting );
3 a4 y- _( | t3 u: u+ X - uxCriticalNesting--;& |/ Q7 ^* K7 C& a, U
1 B4 S# P- C# G2 U- if( uxCriticalNesting == 0 )
. _8 f; [% L+ a& ~( E - {
6 |6 H) B i) D9 N8 J, M v0 @ - portENABLE_INTERRUPTS();
2 d7 e K5 _4 u, A - }
" n3 P1 e# d" Q - }
复制代码 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
- /**- @3 v, g6 l/ m/ x/ J. t. O
- * task. h
# |- m: I2 N% A6 p( ~- h+ ~1 G - *! F6 ]# T8 Q$ ^0 L" |" |
- * Macro to mark the start of a critical code region. Preemptive context# w' E/ _: V8 W
- * switches cannot occur when in a critical region.
$ D5 W5 ?; ~( ~% I! E! Q - *
8 z2 D* h. u0 t+ O0 U+ w - * NOTE: This may alter the stack (depending on the portable implementation)
$ x0 E, C2 R0 n1 C1 k - * so must be used with care!- U/ p9 s' G2 F9 Z1 v, `
- *
3 H c* }, q! a, t+ O - * \defgroup taskENTER_CRITICAL taskENTER_CRITICAL
( V( U7 t# @+ q - * \ingroup SchedulerControl" y+ Q _7 ~. W4 P3 q; |- N0 t
- */
8 ?8 j& X _" x R. ]& l - #define taskENTER_CRITICAL_FROM_ISR() portSET_INTERRUPT_MASK_FROM_ISR() //中断级* C% c$ _4 t9 f3 X
- , Y" r- N A o5 x( j9 o
- /**; ]' P5 ?& a; G" l
- * task. h) x5 T" e8 I! `. N
- *
: G; m% {3 D* w4 [) n% u4 Z; w7 ]4 Q - * Macro to mark the end of a critical code region. Preemptive context
! Q8 r8 h: G# z6 X" l+ n - * switches cannot occur when in a critical region.
. \5 D8 P, [9 }2 j - *
, n/ R4 d9 E0 Y0 [# s/ ^, ~ - * NOTE: This may alter the stack (depending on the portable implementation)$ W7 J* u) B7 r P
- * so must be used with care!
3 ]0 \: x0 E7 W) Z0 U+ A, ? - *
) A S6 x% c# [ - * \defgroup taskEXIT_CRITICAL taskEXIT_CRITICAL
; X* X+ ]6 Z# Y. c: C' `: O - * \ingroup SchedulerControl4 t7 V" G- q; e2 o+ Q2 f, r
- */
3 L' R0 _5 a) R) T - #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文件中有如下定义:- #define portSET_INTERRUPT_MASK_FROM_ISR() ulPortRaiseBASEPRI()
" u5 b0 s/ g/ o. E - #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
- static portFORCE_INLINE void vPortSetBASEPRI( uint32_t ulBASEPRI )
+ @' ]/ _$ i6 d' u2 i - {, u) Y4 H; g) z( x% o/ W
- __asm
( ~8 c7 V- C6 K! H0 c! z4 C - {
& k6 @: u; v" P; t - /* Barrier instructions are not used as this function is only used to W! h& m6 t& M3 k
- * lower the BASEPRI value. */" z, R E1 L* C- f* A2 J* {
- /* *INDENT-OFF* */6 q' s* r1 G0 ^+ R! u
- msr basepri, ulBASEPRI* _1 M5 ~8 H2 t. |/ y
- /* *INDENT-ON* */
1 n& I5 I* k" t, u3 M - }
5 c+ r4 }3 {- {' _7 ]5 \# p2 w+ d v - }# c- |4 N# S, I0 L& a
% K! K5 L( C* M1 V! N- static portFORCE_INLINE uint32_t ulPortRaiseBASEPRI( void )
$ R+ j% i1 J8 _; ~0 ]( z - {
! E5 S% a* R$ ]7 C. F3 A% g* L9 x0 V; g - uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
+ R- _% B: }: u0 M* p
) d: o+ U \& u- @- Z- __asm
5 I1 `* U6 D; c8 Q! o7 J5 o8 c! O- R - {
* k& R, ^ @% h! O# n& W - /* Set BASEPRI to the max syscall priority to effect a critical
" {. L- C% O* D, c5 h2 `: Q - * section. */4 H* ^! k8 F6 p7 M
- /* *INDENT-OFF* */
, u! N: @% B+ w* C" ] - mrs ulReturn, basepri
9 f# \ S7 ?7 }6 |1 W( x; @5 } - msr basepri, ulNewBASEPRI0 _, U$ F) P3 `5 ^
- dsb
) o/ e7 [$ |8 _5 p: s - isb A( H1 J! \+ F0 M( y# e+ k2 w
- /* *INDENT-ON* */( s$ i% W& @( T
- }
" {" N0 o8 Q- p x; y# L - K3 x4 J/ O" t: j5 Z1 b
- return ulReturn;
! v7 z* _5 S. |% ]( q" p* [% X - }
复制代码 中断级临界区代码应用示例3 N% e* Z" b( ^6 o% g4 l& x4 s6 |
L8 e: g2 p+ K& @/ E/ j
) C* i3 q4 Q' w* u* H. M* T' w, `8 r
5 R/ B0 ]. ^" R- @$ R* ]
y- U! ?/ H G" m! u8 r( _ |