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

【安富莱STM32F407之uCOS-III教程】第9章 μCOS-III系统移植文...

[复制链接]
baiyongbin2009 发布时间:2014-12-22 16:52
本帖最后由 baiyongbin2009 于 2014-12-22 16:54 编辑
) L6 p( J, p8 C6 H8 H* K9 C
; h/ s6 {$ B' ^3 c8 T7 |特别说明:
0 z$ [2 L& s' n# [! `2 ]) A4 k. [1.  本教程是安富莱电子原创。5 I9 {& ^! P  J, |1 d  Z6 ~
2.  安富莱STM32F407开发板资料已经全部开源,开源地址:地址链接
& x6 p7 B. T  h; A( f
3.  当前共配套300多个实例,4套用户手册。
) Z5 ^$ v% e. V3 h& i' ?
第9章  μCOS-III系统移植文件详解
2 |' I. X. R1 J6 r# e/ g
    本期教程主要主要跟大家讲解μCOS-III系统的官方移植文件,理解这几个移植文件很重要,如果这几个文件理解的比较深入的话,对于后面学习源码大有裨益。这几个文件主要就是实现任务切换的功能,关于任务切换咱们在前面几期已经详细讲解了工作原理(没有看的同学一定要看第4章和第5章,要不理解学习本章教程依然吃力),本期教程就是结合前面讲解,进一步讲解μCOS-III中任务切换实现的原理。
    9.1 移植文件
    9.2 os_cpu.h文件讲解
    9.3 os_cpu_c.c文件讲解
    9.4 os_cpu_a.asm文件讲解
    9.5 总结
9.1  移植文件
    官方提供的uCOS-III,uC-LIB,uC-CPU文件中都有Ports文件夹,咱们主要关心的是uCOS-III中的Ports文件夹。Ports文件夹中含有如下三个版本的移植文档:
9.1.png
) n  D+ T( [3 l! H( n$ [4 A* @
    RealView就是用MDK编译器的移植文件,其余两个文件夹中的移植文件是用IAR和GUN的。咱们就以RealView中的移植文件为例跟大家详细的讲解下。RealView中的文件如下:
9.2.png
0 k- i# K( z7 i0 m
    os_cpu.h,os_cpu_c.c,os_cpu_a.asm这三个文件在移植过程中最重要,下面主要的就是把这三个文件中的内容详细讲解一下。
9.1  os_cpu.h文件讲解
    此头文件的内容比较少,下面就将里面的内容说明一下
9.2.1      宏定义
  1. #ifdef   OS_CPU_GLOBALS
    0 g; X6 s* Z3 d0 T7 q
  2. #define  OS_CPU_EXT5 ?! P% D- d" v2 G
  3. #else2 p) i6 r9 E4 g& S1 x$ r+ X
  4. #define  OS_CPU_EXT  extern   1 Y: O% g: x2 K+ u( p& M* n
  5. #endif4 b8 w+ o+ A/ w9 }: Y

  6. * r7 I5 [9 a; L8 G  @0 y
  7. /*
    $ ]8 m7 l# m2 z# k8 f. f$ D* K
  8. *********************************************************************************************************. `2 q7 A0 |  H& l6 O6 a
  9. *                                               MACROS
    ; E7 a5 U& w$ z7 h! W
  10. *********************************************************************************************************
    6 Z; g* K% k% `1 z1 r% R0 h
  11. */0 ], ?" }2 o+ V# R) F
  12. 0 }9 O& b% V8 i& M1 k* U
  13. #define  OS_TASK_SW()               OSCtxSw()
复制代码

4 L* f/ b' b# b9 \9 u) j. x
  l  由于工程中没有声明OS_CPU_GLOBALS,所以使用的都是#define  OS_CPU_EXT  extern源码的头文件中使用OS_CPU_EXT的地方很多,大家要记住这个的含义。
  l  #define OS_TASK_SW()     OSCtxSw()
    这个是任务级的任务切换函数,后面还有个中断级的任务切换。函数实体在os_cpu_a.asm文件中。此函数的主要功能就是实现任务的切换。
  1. /*& F, h8 p' w& G% ^
  2. *********************************************************************************************************/ |' P, s. ~8 I* Z
  3. *                              OS TICK INTERRUPT PRIORITY CONFIGURATION
    : e/ \6 m; H( D
  4. *! [+ o4 j$ H% ~
  5. * Note(s) : (1) For systems that don't need any high, real-time priority interrupts; the tick interrupt
    ; Z6 i1 n5 p' e, {6 T" y3 k" H
  6. *               should be configured as the highest priority interrupt but won't adversely affect system
    ) h3 e) h' A8 Q, }5 x' D
  7. *               operations.
    $ ?' V! F5 _% n8 W" z" k4 }0 b
  8. *
    + H1 u% a5 G# e* F- f
  9. *           (2) For systems that need one or more high, real-time interrupts; these should be configured+ g* D/ x' g4 y& _% q& u4 j$ h+ q
  10. *               higher than the tick interrupt which MAY delay execution of the tick interrupt.
    $ P: r" S- c2 w
  11. *
    - C6 e/ b6 s8 x: O  H: h4 m
  12. *               (a) If the higher priority interrupts do NOT continually consume CPU cycles but only
    * E1 y9 u! g4 Q1 m( {
  13. *                   occasionally delay tick interrupts, then the real-time interrupts can successfully& J2 j+ J9 }; X" k) g
  14. *                   handle their intermittent/periodic events with the system not losing tick interrupts
    3 Q$ N' F  {1 s- C" @
  15. *                   but only increasing the jitter.. y0 O/ F8 d) |
  16. *
    9 T6 q5 c( P+ w0 [& h5 l+ s* y
  17. *               (b) If the higher priority interrupts consume enough CPU cycles to continually delay the
    , S9 l: ^" v% F' e# H! A9 b
  18. *                   tick interrupt, then the CPU/system is most likely over-burdened & can't be expected
    ; T) k6 M% \% u2 G
  19. *                   to handle all its interrupts/tasks. The system time reference gets compromised as a/ x- ~. f( N: m" R2 `/ `
  20. *                   result of losing tick interrupts.: Y/ l3 B& l$ p1 f$ P
  21. *********************************************************************************************************! N2 C3 ^7 n+ l2 @
  22. */; x6 Z! }1 r* Y' T! L: O- [$ b; _
  23. ! H) x. X' D) }. t, E
  24. #define  OS_CPU_CFG_SYSTICK_PRIO           0u
复制代码
l OS_CPU_CFG_SYSTICK_PRIO 用于配制嘀嗒定时器的优先级。关于嘀嗒定时器优先级的配置还是很讲究的,也就是上面注释所写的。
  Ø 对于那些不需要高优先级中断的系统,嘀嗒定时器中断要配置成最高优先级的中断,但是不能影响系统操作。
  Ø 如果系统中多个高优先级的中断,而且优先级比嘀嗒定时器的优先级高,那么就可能会延迟嘀嗒定时器中断。
    u 如果高优先级的中断不会持续的占有CPU,只是偶尔的延迟嘀嗒定时器中断,那么实时中断可以间歇性或者周期性的处理系统事件而不丢失嘀嗒定时器中断,只是增加抖动(因为高优先级中断的执行会抢占嘀嗒定时器中断的执行或者高优先级中断执行的时候嘀嗒定时器中断会一直得不到执行从而造成嘀嗒定时器中断在执行时间上的抖动)。
    u 如果高优先级的任务长时间的占有CPU时间会造成系统超负荷运行,而不能执行嘀嗒定时器中断,任务也不能得到及时的执行。这种情况可以认为系统丢失了几次嘀嗒定时器的执行。

' `3 f, _, ^6 f- e; j
赞 收藏 评论11 发布时间:2014-12-22 16:52

举报

11个回答
baiyongbin2009 回答时间:2014-12-22 16:58:09
本帖最后由 baiyongbin2009 于 2014-12-22 17:00 编辑 + k* C. Z+ y+ ~
8 K1 Z4 X1 G# i: J$ M+ b) n
(续)9.2  os_cpu.h文件讲解5 z7 f8 Q& ]. w% s+ w
2 e+ j! L& G3 ^# M8 y- Z4 D& e
9.2.2      时间戳配置
  1. /*
    1 L8 j# ~2 r" L
  2. *********************************************************************************************************8 Y# v# L, C+ N& `0 `; a% v7 z
  3. *                                       TIMESTAMP CONFIGURATION" B$ X/ |- O! ^/ A% I# W$ V/ m
  4. *
    " w$ r. \5 u" C5 W. S
  5. * Note(s) : (1) OS_TS_GET() is generally defined as CPU_TS_Get32() to allow CPU timestamp timer to be of- |' @, `' {* o& H! |, A1 z3 z
  6. *               any data type size.
      z: ?. r! B+ }6 {
  7. *
    2 l# y1 m5 Z3 I; u! @# G9 A: }
  8. *           (2) For architectures that provide 32-bit or higher precision free running counters
    7 R. L' P5 A( ?! [
  9. *               (i.e. cycle count registers):
    2 Q& _; b2 c5 o6 Q$ A+ R
  10. *
    & i9 Y  ~0 T; w! W0 H8 y, m
  11. *               (a) OS_TS_GET() may be defined as CPU_TS_TmrRd() to improve performance when retrieving. I+ o8 E4 Y$ h& F
  12. *                   the timestamp.: g. B0 q' Q2 @( h. a
  13. *, _! ~+ F' M9 g( `3 Z
  14. *               (b) CPU_TS_TmrRd() MUST be configured to be greater or equal to 32-bits to avoid
    2 X( C- B9 g  \. Z
  15. *                   truncation of TS.
    - ^# n; ^9 W  o/ e# n  ]
  16. *********************************************************************************************************6 R9 d5 ]; m  s4 U0 d4 t4 C
  17. */
    : Q* D& N' S  M' k7 P& Q  X

  18. 6 p5 N4 L7 V7 N/ z. Q2 r
  19. #if      OS_CFG_TS_EN == 1u
    & J% Q" b0 r* ~4 R
  20. #define  OS_TS_GET()               (CPU_TS)CPU_TS_TmrRd() /* See Note #2a.                     */& y9 L/ g5 ^, O; o! x. z+ B, d. F
  21. #else% ]  V4 @& k) a" ?4 F+ w
  22. #define  OS_TS_GET()               (CPU_TS)0u8 A& \' K# F+ y+ _9 S! c) \
  23. #endif6 [: F! P3 D, X: T0 [$ d+ l
  24. ' `: C4 y% j/ Q
  25. #if (CPU_CFG_TS_32_EN    == DEF_ENABLED) && \
    9 m% Z* v7 ?* A7 {7 o" @6 r
  26.     (CPU_CFG_TS_TMR_SIZE  < CPU_WORD_SIZE_32)
    8 v) `% O5 C( G' T9 u/ E
  27.                                                    /* CPU_CFG_TS_TMR_SIZE MUST be >= 32-bit (see Note #2b).  */, c0 J3 L5 y8 N. B
  28. #error  "cpu_cfg.h, CPU_CFG_TS_TMR_SIZE MUST be >= CPU_WORD_SIZE_32"( D. N$ h' t6 `
  29. #endif
复制代码
; ?. e! H- S% G# G2 z# B& F
l OS_CFG_TS_EN在函数os_cfg.h里面进行了宏定义:
#defineOS_CFG_TS_EN                    1u   //用于使能或者禁止时间戳,这里1表示使能        
//时间戳,0表示禁止时间戳
       下面重点说一下函数CPU_TS_TmrRd(),相对来说此函数比较的重要。这个函数在μCOS-III中主要用于任务利用率的测量,和信号量、邮箱、事件标志组等执行时间的测量。这个函数不属于μCOS-III源码部分,确切的说应该是属于μC/CPU。此函数的内容需要用户根据自己使用的处理器进行实现,以提供更高精度的时间基准,至少要高于系统的时钟节拍精度,这样才能获得更准确的函数执行时间和任务利用率的测量。使用这个函数前需要先做初始化(在文件bsp.c里面):
  1. /*, u; T3 u8 R4 J5 j' Z3 u
  2. *********************************************************************************************************
    ! _7 N+ K/ x  t6 N! y# i. g
  3. *                                          CPU_TS_TmrInit()! |' m" Y% `! A& o4 y) G& k
  4. *# C# @. ?' D/ a3 K
  5. * Description : Initialize & start CPU timestamp timer.
    6 L+ v: [2 ?3 V8 K# P$ t# ^
  6. *$ l; Y" ~' S" M3 Z* b
  7. * Argument(s) : none.4 I, O/ S; z& N% U" @
  8. *7 X# [8 L5 @, r2 s- g
  9. * Return(s)   : none.. P; E* e& J. P6 F
  10. *1 B1 y5 U* B7 q. \
  11. * Caller(s)   : CPU_TS_Init().8 [0 s+ R$ o6 I  X
  12. *) t% {/ f# ]3 b9 a4 C- x, B
  13. *               This function is an INTERNAL CPU module function & MUST be implemented by application/
    " ~: ?2 H/ z9 d. V
  14. *               BSP function(s) [see Note #1] but MUST NOT be called by application function(s).
    2 {1 u! J. o% a. c' k. B5 k$ B- Y
  15. *4 V) g7 U; f5 @
  16. * Note(s)     : (1) CPU_TS_TmrInit() is an application/BSP function that MUST be defined by the developer/ B! t9 }+ \, N6 |( Y6 j8 E
  17. *                   if either of the following CPU features is enabled :
    4 O0 y, `% k/ Z4 I% T! U4 t6 k( M% S5 B
  18. *( a3 S9 C. T( u* F, Y2 Q9 U
  19. *                   (a) CPU timestamps- D0 o8 {+ G" F* h
  20. *                   (b) CPU interrupts disabled time measurements( b3 N  k+ x: S$ j8 n9 [6 B
  21. *3 G- }% g" v4 g  V% I
  22. *                   See 'cpu_cfg.h  CPU TIMESTAMP CONFIGURATION  Note #1'1 |9 K/ L3 v5 S4 [
  23. *                     & 'cpu_cfg.h  CPU INTERRUPTS DISABLED TIME MEASUREMENT CONFIGURATION  Note #1a'.7 o; Y' q' }' |: `/ N/ H) s
  24. *$ q. B: F/ |- K7 d4 K  f
  25. *               (2) (a) Timer count values MUST be returned via word-size-configurable 'CPU_TS_TMR'
    ' T8 y* K- F: _( V1 f
  26. *                       data type.
    7 C8 ?) Z! b5 O8 v5 J% z& Y
  27. *
    3 a  X  H# x+ g
  28. *                       (1) If timer has more bits, truncate timer values' higher-order bits greater& S6 u. y2 b- o# \
  29. *                           than the configured 'CPU_TS_TMR' timestamp timer data type word size.
    . y; F! a3 h$ C. u' X3 x- I. b
  30. *
    & a4 @& C2 I2 A) A+ ?% Z7 n+ D
  31. *                       (2) Since the timer MUST NOT have less bits than the configured 'CPU_TS_TMR'
    3 e' p( ^8 `0 L. I( Q: u5 G
  32. *                           timestamp timer data type word size; 'CPU_CFG_TS_TMR_SIZE' MUST be
      t1 p# U( @2 v, @5 ]
  33. *                           configured so that ALL bits in 'CPU_TS_TMR' data type are significant.
    3 v% {( d( l. e
  34. *
    # q- b/ i' |; v, e
  35. *                           In other words, if timer size is not a binary-multiple of 8-bit octets4 Z9 s/ E2 i0 r5 r% _
  36. *                           (e.g. 20-bits or even 24-bits), then the next lower, binary-multiple' \6 D( v- z; k; \# M/ J4 ^7 x
  37. *                           octet word size SHOULD be configured (e.g. to 16-bits).  However, the/ v/ f% l% i  G
  38. *                           minimum supported word size for CPU timestamp timers is 8-bits.
    5 x+ q. J' G: H9 t0 p) x, t3 P
  39. *5 d* W" Y$ o+ ~/ P  T
  40. *                       See also 'cpu_cfg.h   CPU TIMESTAMP CONFIGURATION  Note #2'
    2 p3 Z# y/ l0 y/ j) B+ t0 Q2 J# a
  41. *                              & 'cpu_core.h  CPU TIMESTAMP DATA TYPES     Note #1'.
    + F9 {" ~; U" N6 w
  42. *
    ( {1 G4 ?! H& O7 s
  43. *                   (b) Timer SHOULD be an 'up'  counter whose values increase with each time count.% G1 P( [* m' i6 m& p
  44. *# n- S0 ]/ [) M2 |+ G
  45. *                   (c) When applicable, timer period SHOULD be less than the typical measured time# i9 N8 c6 _  {9 E1 u
  46. *                       but MUST be less than the maximum measured time; otherwise, timer resolution
    8 ?! F" E$ v2 X4 F, l
  47. *                       inadequate to measure desired times." d/ T  v4 G2 c2 H
  48. *5 ?1 i2 U. x9 r
  49. *                   See also 'CPU_TS_TmrRd()  Note #2'.9 X% n1 H9 R  E3 B  p' F5 b/ \
  50. *********************************************************************************************************; x1 L* Q6 r# F0 e) G) B7 A
  51. */
    ( T9 |8 K6 E- v4 C9 O9 N2 Z
  52. / h- d, y: ~/ U
  53. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
    1 R7 u) g9 z" o# I7 a/ L3 ?
  54. void  CPU_TS_TmrInit (void)
    9 d) }8 m* d' Z" N7 b8 M3 y; Y
  55. {3 V# X+ h. L3 j% ^" E# q* k
  56.     CPU_INT32U  fclk_freq;8 c$ v# _) k  B' \/ f* y3 X

  57. : H$ c1 A* R7 q
  58.   F; Y$ K! ~- r/ k
  59.     fclk_freq = BSP_CPU_ClkFreq();
    8 Q: j) }+ J4 n/ p3 _

  60. & o. ~8 V0 H7 q4 g) F
  61.     BSP_REG_DEM_CR     |= (CPU_INT32U)BSP_BIT_DEM_CR_TRCENA;   /* Enable Cortex-M4's DWT CYCCNT reg.  */
    ! ?- o" i/ \  T: b7 A
  62.     BSP_REG_DWT_CYCCNT  = (CPU_INT32U)0u;
      n- e. W# w: D: l, H# J
  63.     BSP_REG_DWT_CR     |= (CPU_INT32U)BSP_BIT_DWT_CR_CYCCNTENA;% u; i3 z# C: a  @0 e( T
  64. ' h' M. K+ ^1 ^7 w
  65.     CPU_TS_TmrFreqSet((CPU_TS_TMR_FREQ)fclk_freq);
    ' U. `" [7 Q: j$ U" _4 V% y
  66. }; r( a- r1 ]  Z* G# R
  67. #endif
复制代码
  Ø 这个函数不需要用户调用,用户可以通过调用函数CPU_Init();(在文件cpu_core.c里面)来调用这个函数。
  Ø 这里时基的实现是使用Cortex-M3/M4中自带的时钟周期计数器,关于时钟周期计数器的知识,大家需要查阅相关的技术手册进行了解,这里只需要知道每计数一次就是一个时钟周期,比如主频是168MHz的话,那么时钟周期就是1/168000000秒。除了μCOS-III是使用的时钟周期计数器作为时基以外,embOS也是使用的这个计数器作为时基。FreeRTOS是用的定时器做的时基。
  Ø 关于此函数的其它信息,需要大家看一下上面的英文说明,看看了解一下即可。
0 c7 G" `4 L1 r+ B' o) |
* e1 o. i' B) \( a( y- `

  @0 {0 c9 M1 u4 r% U
! S% Y4 z5 O2 y0 Q+ d/ e
baiyongbin2009 回答时间:2014-12-22 16:59:18

* n& Z3 t& G4 g# s2 j- _4 r
(续)9.2.2  时间戳配置

% Y! _+ V- F3 c& U( [
说完了时间戳的初始化,下面说一下时钟周期计数器时间的读取,相关的函数还是位于bsp.c文件里面。
  1. /*
    , Y1 L! A9 C. U: n! t' b
  2. *********************************************************************************************************5 h0 J8 {7 c0 i1 L
  3. * CPU_TS_TmrRd()6 ]; |0 e* y- j& b) G
  4. *6 y) v- ~! p! q# P
  5. * Description : Get current CPU timestamp timer count value.6 L! _" S! |/ a7 X
  6. *9 S0 G2 W+ ^$ X$ ~" E
  7. * Argument(s) : none.
    . r# o6 X( Z& d3 F
  8. *! u& r+ l! \( J9 }2 ?9 @6 O
  9. * Return(s) : Timestamp timer count (see Notes #2a & #2b).  s4 d) w' Q; Y% ~  x; k. ~1 V
  10. *4 W* a0 r1 o0 S
  11. * Caller(s) : CPU_TS_Init(),9 B" @; S  s3 R" |
  12. * CPU_TS_Get32(),/ ?% n1 O9 ]$ j; J8 k
  13. * CPU_TS_Get64(),6 J: w) G) p2 o
  14. * CPU_IntDisMeasStart(),7 O. i0 _( a' R( d1 Q8 \
  15. * CPU_IntDisMeasStop().
    3 \" W1 A9 |; Y- G, ]$ t: @* U1 [
  16. */ F# B; e- W0 w7 f
  17. * This function is an INTERNAL CPU module function & MUST be implemented by application/, v- v" ^* P$ R. n/ O  W( m# r" \
  18. * BSP function(s) [see Note #1] but SHOULD NOT be called by application function(s).
    , Q$ O, _) ^7 T/ C8 ]4 W
  19. *, h. }3 w) F7 o: c
  20. * Note(s) : (1) CPU_TS_TmrRd() is an application/BSP function that MUST be defined by the developer! b- A+ V0 d8 P3 V2 r+ m
  21. * if either of the following CPU features is enabled :1 f& J2 W1 m) n0 g/ g
  22. *# d- F5 Z- f$ Q
  23. * (a) CPU timestamps" ~5 u7 s3 a) y6 n  _9 R1 k8 X
  24. * (b) CPU interrupts disabled time measurements
    - k0 ^" |; W+ i4 `0 R2 t7 ]
  25. *7 R. o9 C4 h% {8 G0 H
  26. * See 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #1'( X+ n7 O( `8 b/ ~: l1 a# P8 @# v
  27. * & 'cpu_cfg.h CPU INTERRUPTS DISABLED TIME MEASUREMENT CONFIGURATION Note #1a'.
    & r3 }  |6 l+ i6 Y
  28. *% V: \5 t4 z. t3 H. N/ v1 ]; y$ g
  29. * (2) (a) Timer count values MUST be returned via word-size-configurable 'CPU_TS_TMR'- s( M; `: n2 v7 d
  30. * data type.# n+ V& X( d- X$ L( ?% N
  31. *
    - C4 [# [8 _9 Z" {: Y
  32. * (1) If timer has more bits, truncate timer values' higher-order bits greater. x# |( d* j1 V3 [5 t9 Q: C4 V3 A
  33. * than the configured 'CPU_TS_TMR' timestamp timer data type word size.( l! Z1 p9 ^+ w2 k* t! \
  34. *' c, H# L- n' j" j+ R
  35. * (2) Since the timer MUST NOT have less bits than the configured 'CPU_TS_TMR'$ S# F7 a6 p$ [6 w$ }: T
  36. * timestamp timer data type word size; 'CPU_CFG_TS_TMR_SIZE' MUST be
    , _7 D$ b# J0 d. K3 J
  37. * configured so that ALL bits in 'CPU_TS_TMR' data type are significant.
    ) U& g) \6 _6 ]3 H# B$ C9 e- K
  38. *
    3 o  H+ X+ E6 Z% I5 G2 Z
  39. * In other words, if timer size is not a binary-multiple of 8-bit octets
    " E. S+ x- G7 J9 P9 K3 H- T
  40. * (e.g. 20-bits or even 24-bits), then the next lower, binary-multiple
    " P, m- O8 _; s& @3 @5 L; T/ E
  41. * octet word size SHOULD be configured (e.g. to 16-bits). However, the( i; W1 E3 d; {3 G
  42. * minimum supported word size for CPU timestamp timers is 8-bits.
    / T7 j/ b7 ?7 Q. ^6 C; A9 M; z8 z
  43. *) S! z. \1 R. y  K0 f  O
  44. * See also 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #2'6 _; z. a6 `& m4 R/ l2 \
  45. * & 'cpu_core.h CPU TIMESTAMP DATA TYPES Note #1'.
    / \. R; n. w( f5 L, b: l
  46. *4 w1 s5 _8 H7 ?5 S
  47. * (b) Timer SHOULD be an 'up' counter whose values increase with each time count.
    , E' H/ |% B5 @* H; P3 A
  48. *% u- z' \1 t5 a& p. x
  49. * (1) If timer is a 'down' counter whose values decrease with each time count,
    ) B8 {0 P: u$ e/ }. V# G4 R" U
  50. * then the returned timer value MUST be ones-complemented.
    ) B. V1 \  x- o
  51. *
    : h6 c. ^8 d  H" ^
  52. * (c) (1) When applicable, the amount of time measured by CPU timestamps is
    / k5 u. L; b+ K, `4 E
  53. * calculated by either of the following equations :1 a: b4 k- x5 P; B* }2 E) x. c
  54. *
    7 r  s: Q5 K. R$ U6 d$ m
  55. * (A) Time measured = Number timer counts * Timer period
    ( r& n5 o5 H: w1 B
  56. *# g, C$ B8 @' U; I6 |& N' w
  57. * where& ]+ p6 X2 M/ u4 V+ V0 \( W) ~
  58. *5 q! r7 k, F' ^
  59. * Number timer counts Number of timer counts measured
    6 a' u; |2 N, P) ?4 _$ P- E
  60. * Timer period Timer's period in some units of
    . D5 _' W7 D+ Z+ T
  61. * (fractional) seconds" N* r3 v/ D. T& E4 e3 a
  62. * Time measured Amount of time measured, in same
    $ o7 [; \/ j& }+ Y$ l3 I2 d6 i
  63. * units of (fractional) seconds, f2 a2 {, l9 c& S0 i6 C
  64. * as the Timer period
    4 ~$ h7 N) |& y
  65. *. {2 X9 R* B& Y# ?  u" A
  66. * Number timer counts
    , N; x; M2 f* o) [3 E  ?1 U
  67. * (B) Time measured = ---------------------
    0 {) ?8 u6 g- i; E& Z7 L
  68. * Timer frequency6 N2 ?8 x, W8 p
  69. *& A: _3 U' |' W0 Z' }5 R
  70. * where) o( _' W  I* y* P! E  m$ v; _# i
  71. *
    4 b& p5 i, J5 Q
  72. * Number timer counts Number of timer counts measured
    , V. U6 P$ G( u) a! \; L( g
  73. * Timer frequency Timer's frequency in some units
    2 s/ B; U. w; j& t
  74. * of counts per second( l. w& I3 g2 Y8 c" V
  75. * Time measured Amount of time measured, in seconds
    ! Q5 I7 _1 q5 r+ k5 z$ v/ i! O9 ]6 p
  76. *" ~, P+ L1 e' s" k( [
  77. * (2) Timer period SHOULD be less than the typical measured time but MUST be less
    - U: F( d' z! w/ [
  78. * than the maximum measured time; otherwise, timer resolution inadequate to  s0 ^/ P9 l3 m4 v5 @
  79. * measure desired times.
    * M) G5 b4 ~: z( y+ o0 B: x! K
  80. *********************************************************************************************************' U% o4 r' B6 A6 [
  81. */% f  Y( t3 g# v

  82.   ?: H! B$ k7 N0 `4 e
  83. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)- g* I7 w+ ]- C  w6 `0 M$ z8 e1 U, F
  84. CPU_TS_TMR CPU_TS_TmrRd (void)
    ( V9 @  f) F' G7 F
  85. {
    ! f5 s1 K( u0 U
  86. CPU_TS_TMR ts_tmr_cnts;) H6 `+ d- ^: J1 H6 F9 r9 q/ R

  87. 6 Z* W# ]* q3 v/ R- W% y0 ~, \  U
  88. , [2 W2 g. B# N8 t/ d: H( l; Q
  89. ts_tmr_cnts = (CPU_TS_TMR)BSP_REG_DWT_CYCCNT;
    ( j" Y' Z" w) m4 e+ E5 k" n

  90.   C$ L- u5 t' G7 y
  91. return (ts_tmr_cnts);' L, p* S: j! [
  92. }
    / P  R: Q" }- h5 i0 X9 V" e9 O1 B
  93. #endif: b7 T4 X) L  ?! L7 E
  94. 2 G9 W) F8 [, i# h1 |3 R
  95. /*. z- @6 ^8 k! k8 i4 ]
  96. *********************************************************************************************************+ j  C; ]4 u: i
  97. * CPU_TSxx_to_uSec()0 \1 {; U* }% H$ ]9 u# i
  98. *
    7 E! c5 |; l" i; \, |
  99. * Description : Convert a 32-/64-bit CPU timestamp from timer counts to microseconds.$ s+ X3 L. I: o+ z- V' p0 o
  100. *
    8 J4 _% i8 T' P; i. n/ L
  101. * Argument(s) : ts_cnts CPU timestamp (in timestamp timer counts [see Note #2aA]).
    0 h1 X8 c2 ]2 {: j: m- V6 T+ w4 s0 j1 l
  102. *8 v  Q1 q; \/ t3 z/ N8 y# [
  103. * Return(s) : Converted CPU timestamp (in microseconds [see Note #2aD]).6 n2 ]& `* ?/ G+ {6 F* G
  104. *' T. w$ ~# a/ j/ \% C
  105. * Caller(s) : Application.
    # A, y1 ]4 q) C
  106. *
    : w! K4 }' E2 a# k) |
  107. * This function is an (optional) CPU module application programming interface (API)& ?6 ~* E% {% l- ~% Z
  108. * function which MAY be implemented by application/BSP function(s) [see Note #1] &
    ' @) J! L7 U7 ]6 I
  109. * MAY be called by application function(s).
    " K% j. C$ S: n6 N, \
  110. *0 N# m$ ~: V6 d8 `! u; G: k
  111. * Note(s) : (1) CPU_TS32_to_uSec()/CPU_TS64_to_uSec() are application/BSP functions that MAY be
    / y, f# L! N0 {, E+ F1 h& d
  112. * optionally defined by the developer when either of the following CPU features is+ v5 Z7 G0 `! |7 N. T
  113. * enabled :/ d8 ~3 y/ ~, V- W
  114. *$ c# j( s0 S; ^9 r1 \& l9 e, k
  115. * (a) CPU timestamps; t8 {7 Q  I& `
  116. * (b) CPU interrupts disabled time measurements
    7 h" c" y5 i- {* j2 g) d' V
  117. *9 t7 N5 b1 z; D+ r! _9 S9 E
  118. * See 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #1'# I% S. b: }, J- W/ z9 m
  119. * & 'cpu_cfg.h CPU INTERRUPTS DISABLED TIME MEASUREMENT CONFIGURATION Note #1a'.
      T, z# e: F% H1 P5 O
  120. *; q- D7 O% f! g( w: {1 Y+ @
  121. * (2) (a) The amount of time measured by CPU timestamps is calculated by either of
    " D, N' ~/ ~$ F. S6 k  a, _$ Y
  122. * the following equations :! Z7 L. Y% e  z: N- h
  123. *
    # F. n# S& K/ J2 F, y) f. s: J
  124. * 10^6 microseconds3 K4 s) n& ]( p5 @- o4 R  P& m
  125. * (1) Time measured = Number timer counts * ------------------- * Timer period( a. k4 _$ z3 N
  126. * 1 second' k9 [' z/ I/ Q* @- F* C( K
  127. *3 b8 F! y% D& Q/ c2 n
  128. * Number timer counts 10^6 microseconds
    : y6 U# w' R2 l" k* _2 Y
  129. * (2) Time measured = --------------------- * -------------------0 _3 M1 g4 d& N  r4 T3 H* \+ U) E  w6 I
  130. * Timer frequency 1 second. ]4 ^3 t/ x7 v( p4 M5 Y0 P
  131. *( U( y& U' v( l' L. _0 w
  132. * where
    + ?6 g2 ~3 h7 i: U/ m- Y
  133. *8 D$ r+ w* [( i& B0 E' E$ p4 r) v6 F
  134. * (A) Number timer counts Number of timer counts measured
    , Y2 {. |7 K3 E5 v4 z
  135. * (B) Timer frequency Timer's frequency in some units9 e! m5 c/ h9 |! O9 f/ `
  136. * of counts per second
    - r9 i2 c) i. ^  M$ T) b" H$ }! ~
  137. * (C) Timer period Timer's period in some units of
    ' f9 @( D2 i  v% q8 y1 q: V
  138. * (fractional) seconds& S0 M" H# A  l( F% x- A
  139. * (D) Time measured Amount of time measured,
    & ?3 x; ~$ j6 {* ~
  140. * in microseconds8 B, q; ^- s9 a  C  D+ H% d
  141. *5 f4 C% A. @+ N: l
  142. * (b) Timer period SHOULD be less than the typical measured time but MUST be less
    # T- I9 s7 @& ]2 E. c. G) t
  143. * than the maximum measured time; otherwise, timer resolution inadequate to; f. D. |" O8 W
  144. * measure desired times.+ o7 Q- ~" l4 H3 a  m1 v
  145. *
    ' M0 {& f1 e3 K' C  Z  l/ s6 Y( I
  146. * (c) Specific implementations may convert any number of CPU_TS32 or CPU_TS64 bits8 y+ q! P% g3 k) N& @' z' k0 t
  147. * -- up to 32 or 64, respectively -- into microseconds.
    : {! P- g6 w# [; T. {
  148. *********************************************************************************************************
      s4 d" l! w0 q5 h' J
  149. */
    1 g4 m5 E' p, m0 n! G
  150. ! ^5 m) o  }) x$ b  M
  151. #if (CPU_CFG_TS_32_EN == DEF_ENABLED)
    . q/ A$ A! ]. Q( W) Z6 h) M8 l  F/ {8 z
  152. CPU_INT64U CPU_TS32_to_uSec (CPU_TS32 ts_cnts)
    4 `. T, _' y& M6 k
  153. {
    2 E; D6 A  w3 [* x
  154. CPU_INT64U ts_us;" Q' @. L1 X  s* T1 u* l
  155. CPU_INT64U fclk_freq;7 `3 s/ T/ G# n
  156. 1 i% s% ~/ I+ V! V+ S" x5 Y) c
  157. 7 E% v4 L7 l* d5 Z5 m
  158. fclk_freq = BSP_CPU_ClkFreq();
    / i+ S, G! y- [& r
  159. ts_us = ts_cnts / (fclk_freq / DEF_TIME_NBR_uS_PER_SEC);
    6 U! m. l4 E. f- [8 T

  160. 2 I1 r  J! {+ T8 S
  161. return (ts_us);
    6 w- |) D) i# }+ G
  162. }1 J2 F  x5 j. W& N. N. U
  163. #endif
    ) }8 H! Q, ?% Y

  164. 9 G! p9 I5 }; K/ U# ?5 g
  165. * T. m, f; j- Q
  166. #if (CPU_CFG_TS_64_EN == DEF_ENABLED)5 O6 w$ w5 c+ f( p/ l9 e3 c7 x
  167. CPU_INT64U CPU_TS64_to_uSec (CPU_TS64 ts_cnts)/ _. q4 H6 {  z; ^
  168. {
    1 `( L1 Q. G% I0 v7 g
  169. CPU_INT64U ts_us;
    * y5 D/ z% z# k6 d5 A
  170. CPU_INT64U fclk_freq;4 W8 `4 Q2 x# [. h  ^# R8 l% E. K( l
  171. ; T: I5 i* ]/ a

  172. 4 H8 D$ C; W$ V; @  i! v
  173. fclk_freq = BSP_CPU_ClkFreq();
    " n) ~$ R, a4 n6 P/ ]: F
  174. ts_us = ts_cnts / (fclk_freq / DEF_TIME_NBR_uS_PER_SEC);' z8 Z& R" p1 k. U

  175. $ [8 S  \0 \5 q) m% S8 H; i$ I
  176. return (ts_us);
    1 }) G3 P9 N( p2 K& _) B3 S( [0 Z
  177. }$ p1 J, `# I: ^( C) c
  178. #endif
复制代码
    从上面的函数可以看出时钟周期计数器时间的获取还是很容易的,直接读取相应寄存器就能获得当前的计数。下面的两个函数CPU_TS32_to_uSec和CPU_TS32_to_uSec用于将时钟周期的数值转换成us为单位。函数还是很容易看懂,上面的英文说明反倒搞得挺复杂,大家读读了解些即可。

) E2 d( D6 g1 v% d
baiyongbin2009 回答时间:2014-12-22 17:01:20
9.2.3      函数声明
  1. /*
    : b0 {5 K/ @0 O
  2. *********************************************************************************************************
    . j  [* v2 N) X6 L- `
  3. *                                          GLOBAL VARIABLES
    " j- v& p: ]$ d4 T* F
  4. *********************************************************************************************************
    " E! y/ |3 j8 `+ O4 ?
  5. */
    * S7 w- I9 s, d! A% C  X& ^) j& D  P
  6. 3 g) P' R3 a. k4 L$ s1 @
  7. OS_CPU_EXT  CPU_STK  *OS_CPU_ExceptStkBase;" u$ p: B( Q$ S  F( u

  8. , x1 _  d1 M. V
  9. /*% k( Z8 i4 ], k+ o
  10. *********************************************************************************************************
    7 f: Q. B# f+ D3 @( x
  11. *                                         FUNCTION PROTOTYPES
    & p! _$ @; O( \8 r
  12. *********************************************************************************************************
    4 r) C% w3 R: `9 ~" E
  13. */" \. V. ]  y6 H9 G' I$ U% x

  14.   |3 ~7 x, E" v/ T0 F) L. i
  15. void  OSCtxSw              (void);; T( F8 X! Q6 s% G4 W, b2 w/ \
  16. void  OSIntCtxSw           (void);( N# `0 h3 a, `% W& Q1 B7 \
  17. void  OSStartHighRdy       (void);) b( I4 \3 T3 Z! s+ C" v
  18. ! W: h8 ^2 k0 W
  19. void  OS_CPU_PendSVHandler (void);6 q) ^/ a/ P$ Y9 B! s% q- w
  20. ) X% U  L% I  Y

  21. & s: X" u1 k% R; Q' m
  22. void  OS_CPU_SysTickHandler(void);
    . p( O' l; ^/ i7 z. B/ s* z
  23. void  OS_CPU_SysTickInit   (CPU_INT32U  cnts);
复制代码

; J2 H: @( L8 o: n7 e
l *OS_CPU_ExceptStkBase  异常堆栈指针,后面讲源码的时候在跟大家讲。
l  void  OSCtxSw              (void);
    void OSIntCtxSw           (void);
    void OSStartHighRdy       (void);
    void OS_CPU_PendSVHandler (void);
这四个函数在文件os_cpu_a.asm里面,主要用于实现任务切换。
l  void  OS_CPU_SysTickHandler(void);
    void  OS_CPU_SysTickInit   (CPU_INT32U cnts);
这两个函数的实体在文件os_cpu_c.c文件里面。

, `" k( B- W+ e5 ]9 G
baiyongbin2009 回答时间:2014-12-22 17:05:35
本帖最后由 baiyongbin2009 于 2014-12-22 17:11 编辑
' w0 o3 f+ ~9 P4 j* R6 A) T" }- r  f+ I0 }; w9 P
9.3  os_cpu_c.c文件讲解
    这文件里面主要是一些钩子函数,任务堆栈初始化和嘀嗒定时器的初始化。下面就详细的把这三个部分说明一下。
9.3.1      钩子函数
    钩子函数的主要功能就是扩展函数的功能,用户可以根据自己的需要往里面添加相关的测试函数,这个文件里面的钩子函数一共有个,下面就分别的讲解下。
  1. /*
    " ]& B5 K" ?6 X: |2 Z8 V
  2. *********************************************************************************************************- b( K6 u7 M) j( J. |9 \8 [1 t
  3. *                                           IDLE TASK HOOK' _0 a# J( ]! i% p! P" z  M) o* X; ]
  4. *
    1 b5 A. F! ~# _" X5 g
  5. * Description: This function is called by the idle task.  This hook has been added to allow you to do6 ]/ v5 g' b6 W& A& _7 K# s
  6. *              such things as STOP the CPU to conserve power.' j' {! j: l% g) T1 i* j, `$ h: _
  7. *
    ' U' ]% h1 K) F9 |0 M$ w6 Z5 z
  8. * Arguments  : None.
    5 K1 Z; i* {$ ?% P
  9. *
    ( W; H" `1 H# I: l5 e6 `' P
  10. * Note(s)    : None.
    9 s. E' b# t8 T! r0 L
  11. *********************************************************************************************************
    ' n* A. s1 N6 e- o
  12. */
    . N7 V( v& r; u6 I% W

  13. - c" u" f1 ^7 L! v
  14. void  OSIdleTaskHook (void)( b/ N+ N; L' R' a+ H
  15. {1 `- n7 \8 i+ Z0 F! l0 }
  16. #if OS_CFG_APP_HOOKS_EN > 0u
    4 ?. y  \  E0 S2 F1 R
  17.     if (OS_AppIdleTaskHookPtr != (OS_APP_HOOK_VOID)0) {  `& D2 n, V, s5 n. h+ n
  18.         (*OS_AppIdleTaskHookPtr)();" L+ @- T& g7 Z* S! w- o& X% S
  19.     }
    . t/ F$ d3 e; R$ @- y5 w9 ?/ v
  20. #endif
    7 t& c* I" F2 N4 s9 a
  21. }
复制代码
l OSIdleTaskHook是空闲任务的钩子函数
    此函数被空闲任务调用,用户可以通过函数指针变量*OS_AppIdleTaskHookPtr指向用户设计的函数来实现用户所想实现的任务。这个函数主要用于实现低功耗。
  1. /*
    & U- D6 ]$ h( J' M2 |6 h- {
  2. *********************************************************************************************************  |: _9 c7 ~6 j. C- Y: Y3 U0 M8 Q
  3. *                                       OS INITIALIZATION HOOK+ J3 R" @" W3 ?
  4. *6 @5 L- o- w6 x
  5. * Description: This function is called by OSInit() at the beginning of OSInit().: r5 d$ j- h9 ?6 t
  6. *
      ?7 W+ p, h4 i1 }: H. X% h
  7. * Arguments  : None./ U5 [+ s  K  j" |2 N, L3 q' k
  8. *
    3 N& b6 p6 D. }. A9 h
  9. * Note(s)    : None.; s7 ]5 b: T+ \, h4 @, \" L- Q
  10. *********************************************************************************************************
    ' S7 @  P7 Z1 Y8 |! x3 \7 _1 J' b
  11. */
    ) A+ o6 C9 h* }$ j6 v1 Q

  12. / V8 f+ g! R* S8 C: ]6 x/ |
  13. void  OSInitHook (void)/ }& ?8 ?4 h" F; w5 j2 W) O7 q
  14. {% ?2 R! ?; p$ w/ Q
  15.                                                 /* 8-byte align the ISR stack.     */   2 t3 D. x$ a  W3 ^5 O. j+ r
  16.     OS_CPU_ExceptStkBase = (CPU_STK *)(OSCfg_ISRStkBasePtr + OSCfg_ISRStkSize);
    6 L5 \* A6 u. y2 k9 _
  17.     OS_CPU_ExceptStkBase = (CPU_STK *)((CPU_STK)(OS_CPU_ExceptStkBase) & 0xFFFFFFF8);' k' @* I) O5 L6 C* V
  18. }
复制代码
l OSInitHook是OS初始化的钩子函数
    这个函数主要用于获取异常堆栈的基地址。这里这特别注意:这个异常堆栈是用于MSP(主堆栈空间),这样可以很方便的查询主堆栈的使用情况。针对Cortex-M4/M3内核的MCU,在使用RTOS的情况下,主堆栈主要用在中断服务程序中。
  1. /*
    ' G+ |8 v3 m9 l( d8 l4 `
  2. *********************************************************************************************************( V$ L# I* W2 p$ A4 D8 v5 a
  3. *                                         STATISTIC TASK HOOK
    2 O  F1 c, y- N1 f0 s
  4. *
    8 v% p9 d: d  e6 @
  5. * Description: This function is called every second by uC/OS-III's statistics task.  This allows your# }4 h& x  A8 v7 B
  6. *              application to add functionality to the statistics task.; p  y9 }4 t$ @! S  @9 W8 i; F6 s
  7. *( O4 R$ f7 T: J  C& C) d6 C2 h
  8. * Arguments  : None.7 v6 A: p' Y/ |/ L+ U
  9. *! v; b5 x+ L% e% w2 c/ {% d5 P8 _
  10. * Note(s)    : None.# b9 h: C3 E, D! g" G% `7 c! `
  11. *********************************************************************************************************1 @1 f0 S  W( @! y) C
  12. */
    % B$ H3 a: G' X7 w* T; M. o
  13. . |& Y8 N% j) R1 E3 y8 k; l' K
  14. void  OSStatTaskHook (void)
    5 U/ l3 V% @/ W& U
  15. {
    8 _+ @: M, f; Q+ ]. C7 O
  16. #if OS_CFG_APP_HOOKS_EN > 0u
    0 r  {3 @& }* J' X
  17.     if (OS_AppStatTaskHookPtr != (OS_APP_HOOK_VOID)0) {; ]1 n* O) U6 L3 s
  18.         (*OS_AppStatTaskHookPtr)();
    & H! I- [0 u3 Q$ K  B. O3 s/ M
  19.     }
    1 g; r; t9 k0 f: g  ]" m2 g
  20. #endif
    ; f) ]/ s: K1 a" f& X
  21. }
复制代码
l OSStatTaskHook是统计任务的钩子函数
    使用方法和前面的空闲任务钩子函数一样。
  1. /*
    % ^) y/ x* P" F. e* U
  2. *********************************************************************************************************, S  w8 f$ }$ W- Y
  3. *                                          TASK CREATION HOOK
    / o' r6 P6 I1 t  r
  4. ** w) i9 a+ ^- c: r% _% h+ h% ~  }
  5. * Description: This function is called when a task is created.
    ; I9 ~- e/ ?- J, f1 K. U# u) M
  6. *
    5 T8 j/ p; L) V4 ?2 j: H: L
  7. * Arguments  : p_tcb        Pointer to the task control block of the task being created.3 s( h: Z6 Q; E. t
  8. *
    : m7 @( c% p7 Y9 b/ I
  9. * Note(s)    : None.
    . I8 e3 ?0 @6 {; w1 T4 M! ^; r! `
  10. *********************************************************************************************************
    9 }3 {3 ^" n' P8 H0 k" g# M: V
  11. */  p) e% c  ~/ A" y3 D4 V

  12. , V. \6 \/ P5 o* x: P( |+ h+ j
  13. void  OSTaskCreateHook (OS_TCB  *p_tcb)
    ( ~6 t0 Y. j: h
  14. {
    0 h$ R5 }" f: W, v( M: e
  15. #if OS_CFG_APP_HOOKS_EN > 0u$ o/ l0 G  |8 Q$ Y: o2 }% @
  16.     if (OS_AppTaskCreateHookPtr != (OS_APP_HOOK_TCB)0) {/ R. G* \. B1 s+ A" W
  17.         (*OS_AppTaskCreateHookPtr)(p_tcb);$ B2 n" P6 o1 F% s# z
  18.     }9 _" n& V2 y5 O
  19. #else
    + E, P4 G& q# L. U# o
  20.     (void)p_tcb;                                            /* Prevent compiler warning                               */9 R& i( x4 `3 F. W& Y9 ~+ j* m
  21. #endif
    5 z4 |  U$ U) X" u4 _; N1 l
  22. }
复制代码
l OSTaskCreateHook是创建任务时的钩子函数
    使用方法和前面的空闲任务钩子函数基本一样,只是多了个函数参数。
  1. /*6 A. A  ^7 g8 G0 b. A1 v
  2. *********************************************************************************************************; E9 @+ h; N0 K. ]' \
  3. *                                           TASK DELETION HOOK
    - ?! v/ K3 q3 m% L6 ]4 p) A
  4. *
    7 P+ K8 `& V+ L9 T
  5. * Description: This function is called when a task is deleted.
    4 c2 ^' B9 i1 R# d! p
  6. *
    / i3 v: n8 d2 n! W2 p
  7. * Arguments  : p_tcb        Pointer to the task control block of the task being deleted.) h, h; T9 Y( n0 d4 ~! i
  8. *
    ' n- d5 x+ t, x$ u* ]6 ~" D
  9. * Note(s)    : None.
    ' a& v6 H3 E$ u1 I3 }1 g
  10. *********************************************************************************************************
    & W1 w  R3 k' c8 g4 m
  11. */
    : y2 V- w, Q+ [- @% `) @% H6 `; a, A
  12. ! z  Y- ^& {8 y3 f: d
  13. void  OSTaskDelHook (OS_TCB  *p_tcb)
    % C4 h7 \; N! S5 T9 q
  14. {# w+ U. m$ i- a. W+ V$ I$ v
  15. #if OS_CFG_APP_HOOKS_EN > 0u
    1 h/ c: L$ i6 T* @
  16.     if (OS_AppTaskDelHookPtr != (OS_APP_HOOK_TCB)0) {
    ' A! ]; T, v9 ~' X& ~
  17.         (*OS_AppTaskDelHookPtr)(p_tcb);' `/ n1 d0 M2 r% b2 b. _+ p
  18.     }7 V3 _, j: g- c
  19. #else: P1 [* U; j2 B% k2 C
  20.     (void)p_tcb;                                            /* Prevent compiler warning                               */, v8 r8 e) p) n3 s, S' I
  21. #endif
    & _3 \1 `! Z2 T+ y6 O4 \
  22. }
复制代码
l OSTaskDelHook 是任务删除的钩子函数
    使用方法和前面的创建任务钩子函数一样。

$ k( l2 x4 C1 p+ _
baiyongbin2009 回答时间:2014-12-22 17:10:44
(续)9.3.1  钩子函数* q5 ^3 i' r+ i9 R
  1. /*
    # v: T9 `& D0 ?, g
  2. *********************************************************************************************************
    " Y6 y2 J6 Q# z. W0 D
  3. *                                            TASK RETURN HOOK
    3 [& ?6 l- m8 m9 {& v
  4. *
    " ^3 T1 v) v2 J6 }7 A
  5. * Description: This function is called if a task accidentally returns.  In other words, a task should
    6 O$ O  c: n4 m# l! R7 C
  6. *              either be an infinite loop or delete itself when done.
    1 E& m  c3 m; ^5 J5 L' d6 p6 o
  7. *
    9 u* f2 k. O- e/ R/ m- o' I# x
  8. * Arguments  : p_tcb        Pointer to the task control block of the task that is returning.
    3 k) P8 u) p, O( a9 G! K- w
  9. *; Z+ v6 W$ |! R+ [) R$ ^
  10. * Note(s)    : None.1 i  o: j4 q, a% D  Z  W, ]9 Z) J% r
  11. *********************************************************************************************************+ s% c( O+ c! |1 t  h; D
  12. */9 l* y, J9 Y4 a6 N, H  f' w+ n, G
  13. " q- @& i$ Y+ `4 B  Q; r
  14. void  OSTaskReturnHook (OS_TCB  *p_tcb)
    ! B  R# C% s9 I- B9 b" l5 ^
  15. {: m. _% v" N6 H* t3 [
  16. #if OS_CFG_APP_HOOKS_EN > 0u: I3 m8 N1 L- p* b& A. G$ b
  17.     if (OS_AppTaskReturnHookPtr != (OS_APP_HOOK_TCB)0) {5 n4 B9 C! T8 J: n
  18.         (*OS_AppTaskReturnHookPtr)(p_tcb);3 u" U: {) h+ V! h/ `
  19.     }
    . {- H8 [/ d6 e" s
  20. #else
    0 G8 y5 f, `) K  o+ B
  21.     (void)p_tcb;                                            /* Prevent compiler warning                               */
    . F$ {& W+ U* w# M3 G+ {4 K
  22. #endif! B( B( h" k4 ?) s( P
  23. }
复制代码
l OSTaskReturnHook是任务返回的钩子函数
    这个函数的使用方面和前面的创建任务钩子函数是一样的。在RTOS中,任务必须得是超级循环的形式,如果任务只执行一次或者任务中有返回函数,系统就会调用异常返回函数将这个任务挂起或者删除(用户可以自行配置)。在异常返回函数中调用钩子函数。
  1. /*
    8 h8 b0 G9 u* y; O& g' A% z
  2. *********************************************************************************************************
    $ {2 M6 H# Q5 ?* `+ ]6 Q  f
  3. *                                           TASK SWITCH HOOK' q1 ^; z+ f3 A* ~* O
  4. *+ a; J# W. E9 P! Z  p( K  A0 i
  5. * Description: This function is called when a task switch is performed.  This allows you to perform other
    # O( V1 z' u. I1 O6 s7 }
  6. *              operations during a context switch./ G; x- W8 r0 r  I% F. u6 `3 t
  7. *: _# Z* ]( P9 L1 F/ c! Y
  8. * Arguments  : None.9 Y; w4 a4 L; f3 Y4 I
  9. *
    / `- O) e) ~/ y  v# e
  10. * Note(s)    : 1) Interrupts are disabled during this call.4 a5 h. A( G7 u7 O1 W5 \5 |6 F4 Q  @/ R
  11. *              2) It is assumed that the global pointer 'OSTCBHighRdyPtr' points to the TCB of the task
    $ _4 W4 f, ^8 K* m# s5 M& h; q
  12. *                 that will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCurPtr' points$ q7 q9 s$ F- z- E+ A
  13. *                 to the task being switched out (i.e. the preempted task).
    ) l& G3 }4 {% v8 |+ A
  14. *********************************************************************************************************
    2 C6 m5 p) R  i5 z
  15. */" [, _! m1 z9 b$ |4 N! e4 x
  16. & h( e  r0 V5 F
  17. void  OSTaskSwHook (void)
    * w$ b1 V$ u3 p9 h. ?
  18. {
    4 i! W3 }( `* @2 M8 _3 [. B
  19. #if OS_CFG_TASK_PROFILE_EN > 0u- e- h# j" s9 L! W
  20.     CPU_TS  ts;
    2 k7 f3 p' L- T  j5 l3 i0 ~
  21. #endif
    ) L( u/ j" u1 Q; `4 R& r
  22. #ifdef  CPU_CFG_INT_DIS_MEAS_EN
    ; N2 J0 s" p+ H5 s* O8 o4 }8 z, v
  23.     CPU_TS  int_dis_time;/ G5 b9 A+ `$ `- ~: q6 U
  24. #endif0 w) l- r$ M: L- h

  25. . y' H' E4 O- X3 ^. x- x% `
  26. $ {- X# g: {! h: V
  27. , d; {+ z3 I  n' Z2 ^1 i! ?8 r
  28. #if OS_CFG_APP_HOOKS_EN > 0u; ]* i0 z0 z3 [3 Z' T
  29.     if (OS_AppTaskSwHookPtr != (OS_APP_HOOK_VOID)0) {; C6 p- r" m7 k% |( @$ C
  30.         (*OS_AppTaskSwHookPtr)();
    1 G# ~# ]$ h+ A/ t, v/ c
  31.     }/ Y! h( P! }/ P/ x
  32. #endif7 F  n2 `4 [6 z5 ^( S+ k& C" z

  33. % S8 P: B$ u+ M( m: }$ n5 H
  34. #if OS_CFG_TASK_PROFILE_EN > 0u; m% A+ ~: y* {3 {" L
  35.     ts = OS_TS_GET();& C" O; U# G( [9 C
  36.     if (OSTCBCurPtr != OSTCBHighRdyPtr) {0 }* z* K- h5 |% H8 T& S+ W
  37.         OSTCBCurPtr->CyclesDelta  = ts - OSTCBCurPtr->CyclesStart;+ e; `  S5 A7 @* i0 @$ `
  38.         OSTCBCurPtr->CyclesTotal += (OS_CYCLES)OSTCBCurPtr->CyclesDelta;
    : p6 }8 Z3 }7 {# r0 `
  39.     }
    . c3 ?  {: C0 t6 o2 V
  40. 3 W' g3 O  I! N4 j( A& p, [& V' [5 k
  41.     OSTCBHighRdyPtr->CyclesStart = ts;
    & l2 r: o( f" ^* Q  R' v7 a0 Z( _
  42. #endif
    / F& X5 e0 D$ w: x  q7 _1 j
  43. " @% U' i# ]% z9 B) l8 C8 Z
  44. #ifdef  CPU_CFG_INT_DIS_MEAS_EN; g. f; O- ]7 v; q- X& T) _
  45.     int_dis_time = CPU_IntDisMeasMaxCurReset();  /* Keep track of per-task interrupt disable time  */
    9 z5 u- J$ _+ K; @. i2 j
  46.     if (OSTCBCurPtr->IntDisTimeMax < int_dis_time) {9 L+ \( x$ N7 s
  47.         OSTCBCurPtr->IntDisTimeMax = int_dis_time;+ p$ G% u! _% ?6 K" D0 X' F( Q$ s
  48.     }7 ~! a/ I& _( ?- z4 ?% {
  49. #endif
    % @' a- V; B7 f; e1 y' _! X" {: S1 j
  50. % P* G, O+ f" j3 P2 O0 R8 R' p
  51. #if OS_CFG_SCHED_LOCK_TIME_MEAS_EN > 0u9 p! |4 a) K1 a- `4 G& S% ]
  52.                                                /* Keep track of per-task scheduler lock time    */
    ' N0 u$ y( C- Z
  53.     if (OSTCBCurPtr->SchedLockTimeMax < OSSchedLockTimeMaxCur) {
    0 F( t. |, i. q1 `' [" a+ D3 c2 j
  54.         OSTCBCurPtr->SchedLockTimeMax = OSSchedLockTimeMaxCur;6 _7 F0 H' _$ ?9 k# s5 ~- I
  55.     }, o2 ~% ?- u5 G( n8 B, V: }
  56.     OSSchedLockTimeMaxCur = (CPU_TS)0;         /* Reset the per-task value                     */& G! h. F# ?$ z
  57. #endif) G( V% L( b- P/ q, b
  58. }
复制代码
l OSTaskSwHook 是任务切换的钩子函数
    这个函数里面也有用到函数指针,使用方法和前面的函数是一样的。函数里面还有一些执行时间的测量,这些内容会在后面源码讲解的时候再跟大家分析。
  1. /*' `' j3 r9 y4 Y6 s+ ~# C, T& k. j
  2. *********************************************************************************************************- T  ], f& c6 x" W6 t1 D
  3. *                                              TICK HOOK
    # n* O- Z' T6 a4 V4 v% n
  4. *
    7 H, a  O. s) B
  5. * Description: This function is called every tick.
    % w. Z2 P1 W% M) `
  6. *" q9 R+ {3 I) W) D) Q  ^5 q" V
  7. * Arguments  : None.
    0 f/ r  ?" Y: z+ J* X. s
  8. *
    9 y: Y: M1 f0 B& i
  9. * Note(s)    : 1) This function is assumed to be called from the Tick ISR.
    9 P8 v0 K! n' C$ V$ {5 d  W
  10. *********************************************************************************************************
    & O6 l! w' W& Y+ Y
  11. */+ x) D3 r+ Y3 o7 F

  12. 3 r  P8 b, G0 M% y% t
  13. void  OSTimeTickHook (void)
    ) u( p$ u& z( y
  14. {
    * {) j7 b! d' r1 @" @1 m, d
  15. #if OS_CFG_APP_HOOKS_EN > 0u0 R. ?1 \- X5 u: ]" e
  16.     if (OS_AppTimeTickHookPtr != (OS_APP_HOOK_VOID)0) {. D2 k, y9 X# ~: [$ M. P
  17.         (*OS_AppTimeTickHookPtr)();& E/ }: i6 A1 G( B0 ]; f' f, V# v
  18.     }
    ; F9 K; x% H3 d. x0 Y  k2 d7 u; J
  19. #endif
    " x) O7 F+ ]% j9 ^7 P9 d9 |8 e
  20. }
复制代码
l OSTimeTickHook 是嘀嗒定时器钩子函数
    这个函数的用法和前面的函数一样,被嘀嗒定时器中断服务程序所调用。

! N. s8 V% S  J' H4 e; O; |0 P
baiyongbin2009 回答时间:2014-12-22 17:12:27
9.3.2      嘀嗒定时器配置
    关于嘀嗒定时器在V5开发板用户手册第10章:SysTick实验里面有详细的讲解。这里就不再详细的赘述了,这里特别注意咱们已经在前面的os_cpu.h文件里面配置了嘀嗒定时器的优先级为0,也就是最高优先级。
  1. /*% G# B3 m5 G/ b0 u# ]  @
  2. ********************************************************************************************************** N* S4 K0 G  ?! u( |+ x
  3. *                                          SYS TICK HANDLER
    $ _: v9 U+ m# r- R
  4. *+ }% S/ O, q9 |5 p$ Z, u
  5. * Description: Handle the system tick (SysTick) interrupt, which is used to generate the uC/OS-II tick
    2 V' l: L- T3 E0 @! r2 Z+ ^+ h1 ]
  6. *              interrupt.
      v9 p! ~4 O( N3 ~
  7. *
    ) e. w  v! T$ a3 {. Q5 `: J, O
  8. * Arguments  : None.$ x% X+ {  |1 W+ O$ \, Z
  9. *+ D6 |! I$ z0 A7 I  t2 v
  10. * Note(s)    : 1) This function MUST be placed on entry 15 of the Cortex-M3 vector table.
    2 O* [3 Y  E% v5 y1 j2 A; t
  11. *********************************************************************************************************
    ; ^+ Z6 r5 ]5 g& r/ n4 z
  12. */8 t# A. [4 v( _( a" E

  13. ; A" B7 G( {0 m. ?
  14. void  OS_CPU_SysTickHandler (void)2 T' ^* o  v+ J6 P' r( o: L; ^3 p
  15. {
    $ s, m  b# o: o. O; j
  16.     CPU_SR_ALLOC();
    - f  W4 E0 S- ?& y

  17. - O5 a! z9 R- }, l4 O* `
  18. ; m5 a% Y7 y* E& G. V. B
  19.     CPU_CRITICAL_ENTER();
      H0 z+ `3 T* H( v
  20.     OSIntNestingCtr++;                                      /* Tell uC/OS-III that we are starting an ISR             */
    9 b, i& |- O, R4 n2 k, E+ I
  21.     CPU_CRITICAL_EXIT();* i+ A, P7 x* b6 [' _

  22. 4 O! ]% j( c, A( B0 z- ?7 k
  23.     OSTimeTick();                                           /* Call uC/OS-III's OSTimeTick()                          */3 G+ r4 r: k2 \3 @1 A% q

  24. / U0 |4 t% n$ x' m& [) R
  25.     OSIntExit();                                            /* Tell uC/OS-III that we are leaving the ISR             */) Y% S9 O# |0 ?8 E
  26. }
    5 n4 a7 W" ]0 h

  27. & z8 c8 k7 S2 d% e" @
  28. /*
    4 r( R" \, K6 ]+ w2 Z
  29. *********************************************************************************************************$ c7 x3 {1 l& J4 q9 \1 F) a- Q5 q
  30. *                                         INITIALIZE SYS TICK+ T* t3 p5 n# ]- j1 ]
  31. *
    # t3 }3 N1 q  d
  32. * Description: Initialize the SysTick.- h1 r' Z/ G: u' e3 @& k
  33. *. }. Z2 t) \+ s& B1 t+ {
  34. * Arguments  : cnts         Number of SysTick counts between two OS tick interrupts.
    9 w4 u( H. [9 P2 Z  L
  35. *7 _! g" p# L, h) \& _. ^& _
  36. * Note(s)    : 1) This function MUST be called after OSStart() & after processor initialization.
    * L+ j. o" h: m1 B- N
  37. *********************************************************************************************************, u$ p. w9 \& W* B9 }
  38. */7 M: ]2 U& q+ J, K2 ?) Z( }. o. b
  39. " w" U7 Y$ _, \) {! P
  40. void  OS_CPU_SysTickInit (CPU_INT32U  cnts)
    * ^: Z8 g* c6 B. x- a
  41. {
    3 I  |1 I% C8 Q) r
  42.     CPU_INT32U  prio;
    $ F2 C0 E* f1 `8 v6 t
  43. & l  z* J/ O8 ]: Z9 C+ d1 E# t
  44. * c1 {$ S; o" W' n  C
  45.     CPU_REG_NVIC_ST_RELOAD = cnts - 1u;
    3 F* @  N. r+ m. r) O

  46. 7 d+ e( _0 l4 O" ]9 {0 @
  47.                                                             /* Set SysTick handler prio.                              */; L( i1 v. D5 ~  \" w1 I2 N
  48.     prio                   = CPU_REG_NVIC_SHPRI3;
    9 D+ G+ @: U& r( _0 k
  49.     prio                  &= DEF_BIT_FIELD(24, 0);1 K6 u3 z+ z2 j
  50.     prio                  |= DEF_BIT_MASK(OS_CPU_CFG_SYSTICK_PRIO, 24);
    9 V- l! i9 F7 F& K
  51. " n# V& y9 v4 s( W
  52.     CPU_REG_NVIC_SHPRI3    = prio;* P5 Y! k+ L) G
  53. 4 c( |' S4 v, a( |6 C3 p
  54.                                                             /* Enable timer.                                          */
    + L, @5 X0 C  |2 b% t: M
  55.     CPU_REG_NVIC_ST_CTRL  |= CPU_REG_NVIC_ST_CTRL_CLKSOURCE |
    1 _5 W0 x! \: B8 A6 H- l
  56.                              CPU_REG_NVIC_ST_CTRL_ENABLE;
    ! r, p. q% N2 N- ~
  57.                                                             /* Enable timer interrupt.                                */
    2 V0 f0 Z2 x( A3 l" Q' P# t
  58.     CPU_REG_NVIC_ST_CTRL  |= CPU_REG_NVIC_ST_CTRL_TICKINT;
    ) I6 W( Y4 x& [
  59. }
复制代码
4 I1 m- G5 j  _3 g* b# y- C. d" A
baiyongbin2009 回答时间:2014-12-22 17:13:46
9.3.3      任务堆栈初始化
    每个任务创建时都会调用这个函数,主要用于初始化任务的堆栈,以便于第一次切换到各个任务时能正确的切换(特别注意这句话,用心体会)。
  1. /*
    $ J9 A* Y, d0 \9 F' @7 S8 U1 [4 B" I
  2. **********************************************************************************************************
    ! v# `; A2 S- {
  3. *                                       INITIALIZE A TASK'S STACK
    * O- f* l9 q0 x/ e) }
  4. *
    $ K, X7 B$ @7 t8 P/ D, A7 I6 N
  5. * Description: This function is called by OS_Task_Create() or OSTaskCreateExt() to initialize the stack
    9 Y8 v/ W0 j) m/ c  m8 d5 K
  6. *              frame of the task being created. This function is highly processor specific.
    ( O- b% y& c" L0 `+ B/ A1 F+ f
  7. *" Z# p2 z, z1 D. A7 Y6 s
  8. * Arguments  : p_task       Pointer to the task entry point address.
    + I1 z4 Q) Z: W' b  \. Y1 R. s
  9. *
    + u2 X5 w2 _  W% u$ ?! R% u
  10. *              p_arg        Pointer to a user supplied data area that will be passed to the task
    . o% V5 M, s+ z0 j0 ~
  11. *                               when the task first executes.) w& D) A& T# p* e
  12. ** f# G: S) p  z+ Q, C: x" G
  13. *              p_stk_base   Pointer to the base address of the stack." ?6 S, g% y1 W; q1 k& a" `& y
  14. *0 y7 ^9 r- |6 \  u+ G6 _
  15. *              stk_size     Size of the stack, in number of CPU_STK elements.( L/ C. z& V( X& T# f
  16. *
    - R; B5 i* O$ Y7 Q
  17. *              opt          Options used to alter the behavior of OS_Task_StkInit().5 o+ |" }7 G+ P
  18. *                            (see OS.H for OS_TASK_OPT_xxx).
    4 r( w9 Y0 }) p  D! G
  19. *+ j% M, G0 ]6 R
  20. * Returns    : Always returns the location of the new top-of-stack' once the processor registers have  y4 r3 k2 U3 D+ d9 x
  21. *              been placed on the stack in the proper order.- e# X% ~: B8 X4 {
  22. *
    ; Z6 Y, j" q# A
  23. * Note(s)    : 1) Interrupts are enabled when task starts executing.( C7 ?% V3 N! v; \0 b: r
  24. *
    , ^; E, w/ `" ]( J6 [1 K
  25. *              2) All tasks run in Thread mode, using process stack.9 D8 k) X. X/ ^
  26. **********************************************************************************************************
    % M7 @- r; A0 Y1 z1 E! [
  27. */$ }* p" U, T8 y- P
  28. + g7 b% a4 C. }
  29. CPU_STK  *OSTaskStkInit (OS_TASK_PTR    p_task,1 n- j" d! K* H* O# r7 N* d; x
  30.                          void          *p_arg,
    . v/ T) f+ }5 A
  31.                          CPU_STK       *p_stk_base,3 s+ M+ V3 m  i' `
  32.                          CPU_STK       *p_stk_limit,
    ( k1 K0 W3 g3 J( U
  33.                          CPU_STK_SIZE   stk_size,
    9 D9 k4 S' U. c& V( n5 k
  34.                          OS_OPT         opt)1 i" s& o; t7 k
  35. {0 U/ U- x- @7 W8 E+ E% K
  36.     CPU_STK  *p_stk;
    + A; t' l8 K$ G/ Z" A. n. e
  37. * A; W2 O7 G' b! k3 k# R3 k
  38. ' U9 P5 k2 a( @2 r# M1 d) v8 w
  39.     (void)opt;                             /* Prevent compiler warning                               */
    1 M4 G/ Z5 @/ x. b; \/ I# P. \
  40. : C- u4 V0 p4 `# q
  41.     p_stk = &p_stk_base[stk_size];         /* Load stack pointer                                     */
    5 O! ]4 a7 |  V% i% l5 I
  42.                                            /* Align the stack to 8-bytes.                            */% ?) c, S3 {% x5 ]& }
  43.     p_stk = (CPU_STK *)((CPU_STK)(p_stk) & 0xFFFFFFF8);0 |: H( b+ D* p& V# P: p
  44.                                            /* Registers stacked as if auto-saved on exception        */
    0 s0 F! h7 D5 r& ]* P# S
  45.     *--p_stk = (CPU_STK)0x01000000u;       /* xPSR                                                   */! I9 j: @: E: H/ Q* o0 o6 _- w8 x* a
  46.     *--p_stk = (CPU_STK)p_task;            /* Entry Point                                            */
    , ]) i5 v, q8 z  |: |! Y
  47.     *--p_stk = (CPU_STK)OS_TaskReturn;     /* R14 (LR)                                               */% m- T( \. u+ h' ]' K
  48.     *--p_stk = (CPU_STK)0x12121212u;       /* R12                                                    */, d; z0 C& x1 O& N$ b; W
  49.     *--p_stk = (CPU_STK)0x03030303u;       /* R3                                                     */
    6 ~8 X6 X. v* j' ]# o+ S( a
  50.     *--p_stk = (CPU_STK)0x02020202u;       /* R2                                                     */" v8 f( {. Y8 C4 G; f
  51.     *--p_stk = (CPU_STK)p_stk_limit;       /* R1                                                     */3 c# o/ B! {" x) b; f5 z6 y
  52.     *--p_stk = (CPU_STK)p_arg;               /* R0 : argument                                          */
    0 W' \' U4 |( d# F6 _1 C+ `! i3 S
  53.                                              /* Remaining registers saved on process stack             */
    3 Q& [6 {: `3 [, X! ]8 d
  54.     *--p_stk = (CPU_STK)0x11111111u;         /* R11                                                    */+ ~# R( r% f8 J4 f7 t# ~
  55.     *--p_stk = (CPU_STK)0x10101010u;         /* R10                                                    */
    ' n& z% Q0 _! _0 v  Z9 r
  56.     *--p_stk = (CPU_STK)0x09090909u;         /* R9                                                     */# G% P2 q! |8 Q& O6 V6 Y
  57.     *--p_stk = (CPU_STK)0x08080808u;         /* R8                                                     */
    : ]3 ^, Z2 M- @8 a
  58.     *--p_stk = (CPU_STK)0x07070707u;         /* R7                                                     */3 @2 V/ _9 R9 C6 s
  59.     *--p_stk = (CPU_STK)0x06060606u;         /* R6                                                     */5 o" F. B! j& s8 B& R+ |: D* C
  60.     *--p_stk = (CPU_STK)0x05050505u;         /* R5                                                     */
    9 z( P* k. Z, f& f2 Q
  61.     *--p_stk = (CPU_STK)0x04040404u;         /* R4                                                     */
      J0 \7 r4 i3 k' ^

  62. ; v  g: S6 |/ r, y8 v: L- w
  63.     return (p_stk);
    . O# s0 ~: b. H* c7 m2 q
  64. }
复制代码
    看似简单的初始化,信息量还是很大的。
  l 任务堆栈空间的8字节对齐。
  l 任务堆栈的初始化顺序,先是自动入栈的8个寄存器xPSR、PC、LR、R14、R12、R3、R2、R1、R0,然后是手动入栈的8个寄存器R4-R11。
  l 由于M4内核的堆栈生长方向是向下生长的满栈,所以初始化的时候先获得任务堆栈的末地址,然后依次递减存储这些寄存器。
  l 特别注意这句初始化:*--p_stk = (CPU_STK)OS_TaskReturn; 它的作用就是防止用户写的任务不是超级循环,而出现任务返回的情况,如果任务返回的话就会返回到OS_TaskReturn这个函数里面。
    假如任务1的堆栈控件是uint32_t  stack[1024],那么堆栈初始化后寄存器在堆栈中的排列如下:
9.3.png

, m8 i" A5 ^2 L. i0 O
baiyongbin2009 回答时间:2014-12-22 17:15:35
本帖最后由 baiyongbin2009 于 2014-12-22 17:17 编辑
/ R! e2 o0 c6 a# a& o8 v9 }6 s  w# s  T; s5 L
9.4  os_cpu_a.asm文件讲解
    这个文件中函数的主要作用是实现任务切换,如果大家第5章的教程学懂了,这里面的函数要简单很多,μCOS-III中没有使用到SVC,只是使用了PendSV。所以大家只需懂得PendSV的使用即可。
9.4.1      开启多任务3 G* S: [6 y- N, h8 |
NVIC_INT_CTRL   EQU     0xE000ED04                              ; Interruptcontrol state register.
NVIC_SYSPRI14   EQU     0xE000ED22                              ; System priorityregister (priority 14).
NVIC_PENDSV_PRI EQU          0xFF                              ; PendSV priority value (lowest).
NVIC_PENDSVSET  EQU     0x10000000                              ; Value totrigger PendSV exception.

7 a# D1 m& _$ _/ f5 C7 g8 N6 ^+ _
;********************************************************************************************************
;                                        START MULTITASKING
;                                     void OSStartHighRdy(void)
;
; Note(s) : 1) This function triggers a PendSV exception (essentially,causes a context switch) to cause
;              the first task tostart.
;
;           2) OSStartHighRdy()MUST:
;              a) Setup PendSVexception priority to lowest;
;              b) Set initial PSP to0, to tell context switcher this is first run;
;              c) Set the main stackto OS_CPU_ExceptStkBase
;              d) Trigger PendSVexception;
;              e) Enable interrupts(tasks will run with interrupts enabled).
;********************************************************************************************************

- r, g4 \6 R) m6 Y% a# B. ~
OSStartHighRdy
    LDR     R0, =NVIC_SYSPRI14                          ; Set the PendSVexception priority
    LDR     R1, =NVIC_PENDSV_PRI
    STRB    R1, [R0]
  O% ?- L! g$ x8 ?
    MOVS    R0, #0                                      ; Set the PSP to 0 forinitial context switch call
    MSR     PSP, R0
  n0 o. [/ z0 K
    LDR     R0, =OS_CPU_ExceptStkBase                   ; Initialize the MSP to theOS_CPU_ExceptStkBase
    LDR     R1, [R0]
    MSR     MSP, R1   
( j+ C* Z: A7 e) k% V
    LDR     R0, =NVIC_INT_CTRL                          ; Trigger the PendSVexception (causes context switch)
    LDR     R1, =NVIC_PENDSVSET
    STR     R1, [R0]

( o) |! t; q7 @; B( }4 Q+ g; Z! ?" y
    CPSIE   I                                           ;Enable interrupts at processor level
% \1 K6 _0 L" U/ {* k5 A) a6 x7 A
OSStartHang
    B       OSStartHang                                 ; Should never get here

) L0 z3 a) @8 C% S6 F. [
下面说一下上面的汇编代码都实现了什么操作。
l LDR     R0, =NVIC_SYSPRI14                             
    LDR     R1, =NVIC_PENDSV_PRI
    STRB    R1, [R0]
    这三段代码的主要功能设置PendSV,这里将其设置位最低优先级,为什么要设置位最低优先级?在前面的教程中已经说得很清楚了,这里就不再赘述了。
5 T6 p4 B$ e( b" ?- M7 M
l MOVS    R0, #0                                       
    MSR     PSP, R0
    设置PSP = 0,是告诉具体的任务切换程序(OS_CPU_PendSVHandler()),这是第一次任务切换。做过切换后PSP就不会为0了,后面讲OS_CPU_PendSVHandler()时会看到。
9 Q/ o! B1 y4 j" z% J
l LDR     R0, =OS_CPU_ExceptStkBase                 
    LDR     R1, [R0]
    MSR     MSP,R1   
    设置MSP = OS_CPU_ExceptStkBase,开启多任务后MSP主要用于中断服务程序。
, A; n9 C1 ?4 E  V0 O
l LDR     R0, =NVIC_INT_CTRL                                
    LDR     R1,=NVIC_PENDSVSET
    STR     R1, [R0]
    这里是使能PendSV中断。

3 n% J; ]2 j0 y  d4 V6 m
l CPSIE   I                                                
    OSStartHang
    B      OSStartHang  
    这里主要是使能全局中断,使能后如果没有其它高优先级的中断需要执行,就会立即响应PendSV中断。所以说如果程序运行到了B    OSStartHang,说明多任务启动失败了。

1 z4 z: H$ H, v: Q; Y( B
baiyongbin2009 回答时间:2014-12-22 17:18:05
9.4.2      任务切换使能
    主要有两个任务切换的函数,一个是任务级的任务切换,另一个是中断级的任务切换。这两个函数都只是使能PendSV中断,任务切换的实际工作是在PendSV中完成的。
  1. ;********************************************************************************************************
    . V3 Y& F4 h* ?6 M2 p/ `2 g% `
  2. ;                       PERFORM A CONTEXT SWITCH (From task level) - OSCtxSw()
    # V6 p1 p5 y- V+ ~# n
  3. ;
    - Z. W) e! v  c
  4. ; Note(s) : 1) OSCtxSw() is called when OS wants to perform a task context switch.  This function4 M, J: I+ d: C( ~; I; z
  5. ;              triggers the PendSV exception which is where the real work is done.9 I, R7 [9 j1 J2 W/ S; ?
  6. ;********************************************************************************************************
    ! y0 c6 C2 r  `4 ~+ G  e4 \1 ^

  7. 6 {0 U/ G5 e# V# Z
  8. OSCtxSw
    / l* U: b: |" P( l
  9.     LDR     R0, =NVIC_INT_CTRL                           ; Trigger the PendSV exception (causes context switch)
    , H1 S% Z; C- d6 R
  10.     LDR     R1, =NVIC_PENDSVSET( t$ }3 U( U4 Q3 o* V. k8 d
  11.     STR     R1, [R0]
    # E2 k' J7 a1 k4 F- D+ O1 ~
  12.     BX      LR
    1 H& N, [  [9 ~- b8 H

  13. . g. @# x5 o9 {/ Y" n
  14. ;********************************************************************************************************2 h! [$ f; d" O) W$ \. _
  15. ;                   PERFORM A CONTEXT SWITCH (From interrupt level) - OSIntCtxSw()+ R& W/ |2 ~& s, [9 {! o) r
  16. ;( c& K: B9 V- {. u
  17. ; Note(s) : 1) OSIntCtxSw() is called by OSIntExit() when it determines a context switch is needed as* |/ k9 j& {$ j' ]8 }( m0 B9 b
  18. ;              the result of an interrupt.  This function simply triggers a PendSV exception which will
    0 c4 }/ V/ j; N/ U
  19. ;              be handled when there are no more interrupts active and interrupts are enabled.
    + M5 K) d# w4 }! Z2 B  W
  20. ;********************************************************************************************************! |, f( y, m% M" ~7 Y

  21. 8 z/ X5 ]$ Z  I: ]' N
  22. OSIntCtxSw: L+ B- Z  c5 h$ n# I- S1 k/ x
  23.     LDR     R0, =NVIC_INT_CTRL                          ; Trigger the PendSV exception (causes context switch)
    / w* y8 i; W# P$ c: H
  24.     LDR     R1, =NVIC_PENDSVSET
    / Z+ x; R& L2 ]
  25.     STR     R1, [R0]
    ' R0 p' N& L5 D) }3 O0 W* `8 f
  26.     BX      LR
复制代码
+ B6 {; }5 X6 I; o' M$ _) p& v6 p

, M" U: q2 j2 M& z
baiyongbin2009 回答时间:2014-12-22 17:19:38
9.4.3      PendSV中断
    关于PendSV中断,我们在第4章和第5章已经非常详细的讲解了,这里主要是给大家分析一下任务切换的处理过程,这个中断函数的前面有英文的注释说的非常好,大家有必要看一下。
  1. ;********************************************************************************************************
    9 L6 ~, M6 s9 v; k; |
  2. ;                                       HANDLE PendSV EXCEPTION
    " Z( R6 X$ \& G8 r: }
  3. ;                                   void OS_CPU_PendSVHandler(void)' a8 |9 A+ O1 c3 {! _
  4. ;$ E, z& d$ _- Y$ r" t2 m2 o
  5. ; Note(s) : 1) PendSV is used to cause a context switch.  This is a recommended method for performing2 [4 r& B$ @; x  U$ ]6 K% Y
  6. ;              context switches with Cortex-M3.  This is because the Cortex-M3 auto-saves half of the" f! F- p+ N3 _2 Q  q! V+ J
  7. ;              processor context on any exception, and restores same on return from exception.  So only! K3 l( I; M/ W( X; R) t/ i6 n
  8. ;              saving of R4-R11 is required and fixing up the stack pointers.  Using the PendSV exception
    % o- o$ s( V: P; o# i
  9. ;              this way means that context saving and restoring is identical whether it is initiated from0 F8 Z* d! R5 H" y
  10. ;              a thread or occurs due to an interrupt or exception.& z: q& G/ z8 E! }  Z
  11. ;
    ; @8 L: c5 K9 d+ q0 ~
  12. ;           2) Pseudo-code is:, n+ }0 b$ F- O6 T4 G' d* t
  13. ;              a) Get the process SP, if 0 then skip (goto d) the saving part (first context switch);
    # i2 ]0 h8 m" W2 h) A0 A
  14. ;              b) Save remaining regs r4-r11 on process stack;; R: ~+ Z8 G4 e/ @9 C
  15. ;              c) Save the process SP in its TCB, OSTCBCurPtr->OSTCBStkPtr = SP;4 L9 X4 n( X, {1 q* U9 `0 ?
  16. ;              d) Call OSTaskSwHook();2 @; ^; U$ p3 X9 N% N) I* r9 z1 w
  17. ;              e) Get current high priority, OSPrioCur = OSPrioHighRdy;
    6 `# A' ?# [* G0 ]
  18. ;              f) Get current ready thread TCB, OSTCBCurPtr = OSTCBHighRdyPtr;
    9 y5 ]% f3 U# K3 a
  19. ;              g) Get new process SP from TCB, SP = OSTCBHighRdyPtr->OSTCBStkPtr;
    : ?4 f: s$ @* _$ L. z$ g/ T
  20. ;              h) Restore R4-R11 from new process stack;- s0 \3 ?, K, C' ?, w! E6 y
  21. ;              i) Perform exception return which will restore remaining context.
    1 ?* x; y/ F! s
  22. ;
    # ^% }8 _4 g+ T, G& u
  23. ;           3) On entry into PendSV handler:
    $ K! C5 r, \% U% o9 i
  24. ;              a) The following have been saved on the process stack (by processor):
    * ^$ q' U7 A0 o4 P9 e) k' G
  25. ;                 xPSR, PC, LR, R12, R0-R3( G! |8 g6 t. Y' F- Y7 n2 o) u
  26. ;              b) Processor mode is switched to Handler mode (from Thread mode). O  m/ l- z: |" }- p1 _
  27. ;              c) Stack is Main stack (switched from Process stack)2 Q9 j7 ^, W) e' O; F1 p
  28. ;              d) OSTCBCurPtr      points to the OS_TCB of the task to suspend2 n  C- H' L$ h3 U5 c
  29. ;                 OSTCBHighRdyPtr  points to the OS_TCB of the task to resume
    ( f2 ^4 P% A/ A! t0 p6 Q1 U9 v
  30. ;
    & B( p+ A3 L4 I2 n' ~/ g  f
  31. ;           4) Since PendSV is set to lowest priority in the system (by OSStartHighRdy() above), we% s1 I9 Q4 Z2 u4 i2 g: N! E
  32. ;              know that it will only be run when no other exception or interrupt is active, and; ?' t1 ~8 B" n' @  |# M% H
  33. ;              therefore safe to assume that context being switched out was using the process stack (PSP).7 ~$ w1 U) }. c( {# Z8 C$ S
  34. ;********************************************************************************************************
    $ @' k% j* X0 J9 M& S3 }5 `
  35. / b* J6 }1 S' r
  36. OS_CPU_PendSVHandler! G0 }) z7 ^9 y; P
  37.     CPSID   I                                           ; Prevent interruption during context switch$ S# X3 P% y; A. j$ @! @4 A
  38.     MRS     R0, PSP                                     ; PSP is process stack pointer
    + U$ ?( C/ B" v% S1 L# J
  39.     CBZ     R0, OS_CPU_PendSVHandler_nosave             ; Skip register save the first time
    : w' U$ v' p; y2 z6 B
  40. 0 T  t, E5 c. B( d6 T: q2 d
  41.     SUBS    R0, R0, #0x20                               ; Save remaining regs r4-11 on process stack
    % N% e0 h" m) ?' h3 W2 u- ~, h. p3 m
  42.     STM     R0, {R4-R11}6 ?0 o5 ?, f* J' j
  43. / D$ [+ P7 I) k) B9 j' W
  44.     LDR     R1, =OSTCBCurPtr                            ; OSTCBCurPtr->OSTCBStkPtr = SP;
    : f* E9 i, T4 j$ _( n) A8 A
  45.     LDR     R1, [R1]
    2 i8 Y  c+ c5 W
  46.     STR     R0, [R1]                                     ; R0 is SP of process being switched out3 R. L' N$ a, P6 @
  47. ! }; @% K2 Z3 w  n$ n- t/ t( ]
  48.                                                      ; At this point, entire context of process has been saved- q+ e/ H' F1 Y' C; n& I
  49. OS_CPU_PendSVHandler_nosave
    + A: F- G; n- ]5 p4 b
  50.     PUSH    {R14}                                               ; Save LR exc_return value
    # t; B8 ?, J" B* w) i
  51.     LDR     R0, =OSTaskSwHook                                   ; OSTaskSwHook();
    " {( w9 j+ }6 d( {
  52.     BLX     R0
    : b* t% T/ q1 b, f8 Z
  53.     POP     {R14}
    # n4 N2 a  |9 M: b  i: \2 c  j; D
  54. ( f+ `9 T- J$ w
  55.     LDR     R0, =OSPrioCur                                      ; OSPrioCur   = OSPrioHighRdy;
    3 m$ Z2 ~$ e1 ?6 p) z$ g& B
  56.     LDR     R1, =OSPrioHighRdy) z1 j" |! i8 q0 H3 q
  57.     LDRB    R2, [R1]% D* ?5 C* g8 V6 i6 }& m5 \4 W
  58.     STRB    R2, [R0]+ a1 H0 l: t4 a4 n) R
  59. 1 ^: s/ m( G+ J# e
  60.     LDR     R0, =OSTCBCurPtr                                    ; OSTCBCurPtr = OSTCBHighRdyPtr;' k, S8 l# t9 E7 D
  61.     LDR     R1, =OSTCBHighRdyPtr
    & \7 N+ x- g# g: ~( ]
  62.     LDR     R2, [R1]  D" R5 L, G, N2 ]& }! q
  63.     STR     R2, [R0]
    3 A' r* ]# _# |

  64. & h# n) I" ^" w+ y) F
  65.     LDR     R0, [R2]                                            ; R0 is new process SP; SP = OSTCBHighRdyPtr->StkPtr;* p' P$ ]  g$ w$ @; }% ~4 g, s6 o
  66.     LDM     R0, {R4-R11}                                        ; Restore r4-11 from new process stack
    $ [) C6 s7 r+ X
  67.     ADDS    R0, R0, #0x20
      k7 g& d: O/ s# T( d- H) z8 G4 }
  68.     MSR     PSP, R0                                             ; Load PSP with new process SP* p) w$ k7 B( g. @, w5 {
  69.     ORR     LR, LR, #0x04                                       ; Ensure exception return uses process stack
    # A! }7 n# b) P+ X
  70.     CPSIE   I# D' J0 p- d' o. p- a" g6 k
  71.     BX      LR                                                  ; Exception return will restore remaining context  f8 f7 I% M& e$ V
  72. & f. Y$ z( [2 S9 M6 ?2 j: l4 V& R
  73.     END
复制代码
    这里PendSV中断的主要作用就是保存当前任务的现场,恢复下一个任务的寄存器,从而切换到下一个任务中执行程序,并保证退出PendSV后使用的是PSP (任务堆栈指针)。下面先把这些汇编代码解释一下:
  1. OS_CPU_PendSVHandler                       ;xPSR, PC, LR, R12, R0-R3已自动保存入栈
    " [- a) |8 t: x: B* o( g9 R0 ^3 c. I" g
  2. CPSID   I                                  ;任务切换期间需要关中断
    / o* _3 X# c: G
  3. MRS     R0, PSP                            ;R0 = PSP: Q% J) j, y) ^8 e% u$ i3 H
  4. CBZ     R0, OS_CPU_PendSVHandler_nosave    ;如果PSP==0跳转到OS_CPU_PendSVHandler_nosave去执行 在多任务: r( |- h3 ^. R# B; n5 k  h
  5. ;的初始化时PSP被初始化为0,表示任务是第一次运行,不需要压栈
    ) q4 P4 f1 e& Z% g! ]$ t) G

  6. 9 J: D4 _6 M. h6 S2 C5 ]; b
  7. /********************************将当前的任务寄存器入栈*********************************************/
    * x( g! k$ r: M: ~
  8. SUBS    R0, R0, #0x20        ;R0 -= 0x20 保存R4-R11到任务堆栈 共32个字节            & ?  ~$ k) a1 n( N* q
  9. STM     R0, {R4-R11}         ;压栈R4-R11
    $ v; z6 l7 s- \# ?
  10. LDR     R1, =OSTCBCur        ;获取OSTCBCur->OSTCBStkPtr,. e8 o% C9 n7 _7 _
  11. ;OSTCBStkPtr是OSTCBCur 中的第一个变量                           
    % }6 U! i3 o+ i) _, B  T( o
  12. LDR     R1, [R1]             ;R1 = *R1 (R1 = OSTCBCur)+ n: p( _1 u+ M2 \
  13. STR     R0, [R1]             ;*R1 = R0 (*OSTCBCur = SP)
    , {2 i& N3 Y6 ^
  14.                              ;将当前任务的堆栈保存到自己的任务控制块. |! t& @- A; r. T9 q- E( h! ]5 i
  15. ;OSTCBCur->OSTCBStkPtr = PSP
    2 O, f! x1 c' i+ ?4 h3 d; g
  16. ;程序运行此位置,已经保存了当前任务的寄存器。+ y2 `$ @7 Y% i# }1 e
  17.                             - N. P  Y: R1 h+ }7 W; `2 L) L* v
  18. OS_CPU_PendSVHandler_nosave. s( {+ Q: M+ ?. d  S7 }2 A, |
  19. PUSH    {R14}                ;R14 入栈1 ^7 d4 g, n0 Q/ L3 h5 i
  20. LDR     R0, =OSTaskSwHook    ;OSTaskSwHook(); R0 = &OSTaskSwHook
    9 F# q3 i4 \$ K
  21. BLX     R0                   ;调用OSTaskSwHook()
    ! U/ o6 v( V) o  s/ i/ \4 G
  22. POP     {R14}                ;恢复R14
    5 k. T2 P* k1 N

  23. / s/ {+ c1 I2 C) n1 d& h8 P* G- i
  24. /********************************加载要执行任务的寄存器*********************************************/+ b5 h! C- w+ t! L4 [/ y
  25. LDR     R0, =OSPrioCur      ;R0 = &OSPrioCur! h$ @- U" J2 v  H. m
  26. LDR     R1, =OSPrioHighRdy  ;R1 = &OSPrioHighRdy$ \- E$ {9 u1 X% Z
  27. LDRB    R2, [R1]            ;R2 = *R13 C, e- ~; B; b  }
  28. STRB    R2, [R0]            ;*R0 = R20 w$ }# E0 ?" Z& z
  29.                             ;上面这四句函数的功能就是OSPrioCur = OSPrioHighRdy
    4 ~4 r1 E+ d( R0 d; O* n
  30.   ]% i* {0 k+ x; C: b
  31. LDR     R0, =OSTCBCur       ;R0 = &OSTCBCur7 u2 R- F: p' F& B1 e: v
  32. LDR     R1, =OSTCBHighRdy   ;R1 = &OSTCBHighRdy
    ; w, D' i! ~/ U6 I$ a
  33. LDR     R2, [R1]            ;R2 = *R1
    ) t- U# N. z" _+ h' P4 \
  34. STR     R2, [R0]            ;*R0 = R2
    5 E- k( e/ a1 |4 j' r3 r
  35. ;上面这四句函数的功能就是OSTCBCur = OSTCBHighRdy" q: y$ |, q8 r& u3 o
  36. 4 O8 _! {! \/ R3 Q# [3 n3 {1 c0 \
  37. LDR     R0, [R2]            ;R0 = *R2 也就是R0 = OSTCBHighRdy又因为SP = OSTCBHighRdy->OSTCBStkPtr
    , u. [$ m" o3 o3 v+ g1 p! _9 T
  38.                             ;这条指令的含义就是R0=PSP。
    2 ^3 k- `/ D+ A9 s. t
  39. LDM     R0, {R4-R11}        ;从任务堆栈恢复R4-R11+ I4 Z" L3 w, u8 J8 U
  40. ADDS    R0, R0, #0x20       ;调整R0 += 0x20- Q; {' {8 J5 P0 Y8 h) D
  41. MSR     PSP, R0             ;PSP = R0
    4 ^# V3 y/ a) b( v  ^- e2 @3 P4 C
  42. ORR     LR, LR, #0x04     ; 确保LR位2为1,返回后使用进程堆栈PSP
    5 x: Z5 c1 g1 q( n- {, P# x
  43. CPSIE   I                 ; 开中断
    7 t2 o6 ]' P. ~( M1 Z. ~- e( F
  44. BX      LR                ; 中断返回,剩下的8个寄存器会自动的出栈。
    1 W4 @) L+ Z# Q) P2 y7 [4 E' t6 a; ^
  45. END
复制代码
    为了帮助大家更好的理解这个过程,这里举一个简单的例子:
  l 第一步:开始多任务后,系统就会从咱们上面说的OSStartHighRdy进入到PendSV中断,进入中断前使用的是MSP,也就是说自动入栈的8个寄存器被压入到MSP所指向的堆栈空间。
  l 第二步:进入中断后开始MSP(因为中断程序中只能使用MSP,而不能使用PSP)。由于是第一次进入执行PendSV,剩下的8个需要手动入栈的寄存器不需要执行入栈操作(为什么不需要入栈操作? 因为第一次执行PendSV前的函数只是μCOS-III的初始化,而不是需要执行的任务,所以现场不需要保存)。
  l 第三步:获得当前需要执行的最高优先级任务A后,程序就会从任务A的堆栈空间恢复需要手动入栈的8个寄存器R4-R11,在退出PendSV后,剩下的8个寄存器内容会自动得到恢复。由于前面初始化任务堆栈的时候,已经指定了任务的地址,所以这时就能正确的切换到任务A中。
  l 第四步:当任务A被挂起或者其它的原因,系统再次进入到PendSV中断,进入中断前自动入栈的8个寄存器已经被保存到任务A的堆栈空间,进入中断后再将剩余8个需要手动入栈的8个寄存器推入任务A的堆栈空间,并保存PSP到任务A的TCB控制块,剩下执行的操作就和第三步相同了,获取另一个需要执行的高优先级任务B。初学的同学一定要不这个过程分析透,并牢记。这个过程很重要,要不任务怎么切换的都不知道。

6 c$ c$ B$ O# D! h
baiyongbin2009 回答时间:2014-12-22 17:20:39
9.5  总结
    希望初学的同学将文章所讲的三个文件自行分析一下,特别是PendSV中断函数的内容。本期教程的内容理解透了,对于后面学习源码大有裨益。
/ C5 T9 E& J" x1 x7 n1 S! S
参考资料:
1.       www.micrium.com

$ L& S# Y, P* h8 v7 j- s" m* n  x

所属标签

相似技术帖

官网相关资源

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