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

【安富莱STM32F407之uCOS-III教程】第10章 μCOS-III在高版本MDK...

[复制链接]
baiyongbin2009 发布时间:2014-12-23 19:27
本帖最后由 baiyongbin2009 于 2014-12-23 19:30 编辑
; b: N9 f  E/ s/ c
$ J0 D) q  B" w5 F特别说明:7 C# A$ b- n) F* w$ w
1.  本教程是安富莱电子原创。  V* b, L. U! g" \, I
2.  安富莱STM32F407开发板资料已经全部开源,开源地址:地址链接

# p0 m* m* N1 D# Y, I& U3.  当前共配套300多个实例,4套用户手册。
5 ?& O/ U( {8 t; B' f# B
1 o% U$ ?; J/ I! [
第10章  μCOS-III在高版本MDK开启FPU方案

8 Q) i* h) q/ s2 c
    由于官方提供的μCOS-III移植工程中对于浮点寄存器的入栈和出栈处理是错误的,所以网上就流传了各种修正版本。但是这些修正的代码只能在MDK4.7以下版本中可以正常的运行,MDK4.7及其以上的版本无法正常运行。本期教程为此而生。本期教程提供的方案只有任务使用了浮点寄存器(也就是做了浮点运算)才需要将其入栈,没有使用浮点寄存器的任务不需要进行入栈,认识到这点很重要。此方案在MDK4.54、4.73、5.10以及IAR6.3、6.7上面测试均通过。
    10.1 官方移植方案
    10.2 开启FPU解决方案
    10.3 开启FPU的优劣
    10.4 总结
8 H6 `$ |9 k' R9 C3 U( G, J8 I: K( T
10.1  官方移植方案
    官方提供的移植工程里面,只有IAR工程里面才有浮点寄存器的入栈和出栈处理函数,MDK工程里面是没有的。下面这个是os_cpu_c.c文件夹中的函数:
  1. /*
    5 p% Y) I4 X" W0 T
  2. *********************************************************************************************************
    % P, W: q6 o4 R9 y/ G+ Q$ F+ C! D
  3. *                                        INITIALIZE A TASK'S STACK* |% t- c) C0 i& c- H
  4. *4 I5 o& M0 m$ g* Y: U6 \" I
  5. * Description: This function is called by either OSTaskCreate() or OSTaskCreateExt() to initialize the3 \+ k7 Y, E) N7 I; G0 [/ Z
  6. *              stack frame of the task being created.  This function is highly processor specific.# }1 Y! ?* u" D9 L) K+ U
  7. *
    # B+ C9 g5 V+ T/ `/ _/ }0 h# r: e
  8. * Arguments  : task          is a pointer to the task code
    4 o5 i& N, ]6 ^2 S% V
  9. *
    ! Z% m/ ]+ c- X1 o1 E4 ]
  10. *              p_arg         is a pointer to a user supplied data area that will be passed to the task
    ' |1 H' }% i! t9 U
  11. *                            when the task first executes.
    " `. J5 F/ o- Q8 x) k, U3 N
  12. *
    - _% u- o1 t4 r/ ]
  13. *              ptos          is a pointer to the top of stack.  It is assumed that 'ptos' points to! a: Y. G5 H: @7 _. n5 R5 ^- W
  14. *                            a 'free' entry on the task stack.  If OS_STK_GROWTH is set to 1 then
    ' G: C8 L. p2 J1 N/ o/ Q* k  e
  15. *                            'ptos' will contain the HIGHEST valid address of the stack.  Similarly, if% \  D* T( k; G) \) _; c/ h5 c$ j
  16. *                            OS_STK_GROWTH is set to 0, the 'ptos' will contains the LOWEST valid address
    " [" r1 _1 l0 {
  17. *                            of the stack.* L: \1 C$ a. f- @
  18. *
    ; n( U3 T2 ~, \6 G! j% G+ U9 \% \- I
  19. *              opt           specifies options that can be used to alter the behavior of OSTaskStkInit().
    , v0 |& i2 J1 |1 g* v. F
  20. *                            (see uCOS_II.H for OS_TASK_OPT_xxx).( x. r  L7 i6 \$ N' |* @
  21. *
    ; G2 y( M' Q- v7 a/ _
  22. * Returns    : Always returns the location of the new top-of-stack once the processor registers have
    ) o- x! u) f! O1 v  |/ A$ j
  23. *              been placed on the stack in the proper order.
    ; r# }3 d/ E' G$ r) D
  24. *
    " o( q9 k  t( T; v' I7 O$ T  @
  25. * Note(s)    : (1) Interrupts are enabled when task starts executing.1 F4 w+ ~5 d" q
  26. *
    . T6 }4 G; f, C4 `3 o0 N
  27. *              (2) All tasks run in Thread mode, using process stack.6 A- p( ?4 }! S) W
  28. *5 [2 B% R( I7 M% D
  29. *              (3) There are two different stack frames depending on whether the Floating-Point(FP)
    - E0 m2 [- s8 U) n" A4 C
  30. *                  co-processor is enabled or not.: z$ j5 X& @( C  B7 S; i$ ~
  31. *
    " Y: b7 s3 s7 K$ s9 N2 V5 `7 v) t
  32. *                  (a) The stack frame shown in the diagram is used when the FP co-processor is not present and
    & x$ r- g; c+ N
  33. *                      OS_TASK_OPT_SAVE_FP is disabled. In this case, the FP registers and FP Status Control
    - E' |  h( q3 C+ m, x
  34. *                      register are not saved in the stack frame.* Q5 m# Y# F  B
  35. *
    4 f3 R. y( T5 v9 k4 U: @# Z- L
  36. *                  (b) If the FP co-processor is present but the OS_TASK_OPT_SAVE_FP is not set, then the stack8 t2 D  X* O# z" c7 q
  37. *                      frame is saved as shown in diagram (a). Moreover, if OS_TASK_OPT_SAVE_FP is set, then the7 j; ^/ D# H2 v1 a2 h! V: \
  38. *                      FP registers and FP Status Control register are saved in the stack frame.+ i" |1 h, y% \$ s! V+ r
  39. *$ K% e7 v' [" Z" o+ n- a3 _
  40. *                      (1) When enabling the FP co-processor, make sure to clear bits ASPEN and LSPEN in the
    ) \9 \8 t5 j, [) ?" J8 U
  41. *                          Floating-Point Context Control Register (FPCCR).
    . E0 ]' G( Y! g7 o; W7 U
  42. *
复制代码
*                   +------------+      +------------+
*                    |            |       |            |
*                   +------------+      +------------+
*                    |    xPSR   |       |    xPSR   |
*                    +------------+       +------------+      
*                    |Return Addr|       |Return Addr |
*                   +------------+      +------------+
*                    |  LR(R14)  |       |   LR(R14) |
*                   +------------+      +------------+
*                    |    R12    |       |     R12   |
*                   +------------+      +------------+
*                    |    R3     |       |     R3    |
*                    +------------+       +------------+
*                    |    R2     |       |     R0    |
*                   +------------+      +------------+
*                    |    R1     |       |     R1    |
*                   +------------+       +------------+
*                    |    R0     |       |     R0    |
*                   +------------+      +------------+
*                    |    R11    |       |     R11   |
*                   +------------+      +------------+
*                    |    R10    |       |     R10   |
*                   +------------+      +------------+
*                    |    R9     |       |     R9    |
*                   +------------+      +------------+
*                    |    R8     |       |     R8    |
*                   +------------+      +------------+
*                    |    R7     |       |     R7    |
*                   +------------+      +------------+
*                    |    R6     |       |     R6    |
*                    +------------+       +------------+
*                    |    R5     |       |     R5    |
*                   +------------+      +------------+
*                    |    R4     |       |     R4    |
*                   +------------+       +------------+
*                         (a)             |  FPSCR    |
*                                        +------------+
*                                        |     S31    |
*                                        +------------+
*                                                .
*                                               .
*                                               .
*                                        +------------+
*                                        |     S1     |
                                          +------------+
*                                        |     S0     |
*                                        +------------+
*                                             (b)

* q1 t/ u, Z/ l: o2 G! U3 ]2 e( Q6 m' d# G' K
7 ]" P: X. m8 ]( f, }; f9 @. O' ^

5 }* D7 ~# Q( d0 A8 u8 h: w8 T$ T. g
赞 收藏 评论6 发布时间:2014-12-23 19:27

举报

6个回答
baiyongbin2009 回答时间:2014-12-23 19:32:50
本帖最后由 baiyongbin2009 于 2014-12-23 19:36 编辑
0 f+ c2 W/ g. z3 q
0 a% R- J& e  m5 ^(续)10.1  官方移植方案
  1. *- E) J5 r5 P$ x, Y8 c- E
  2. *             (4) The SP must be 8-byte aligned in conforming to the Procedure Call Standard for the ARM architecture - E6 W2 _/ z) q1 _5 K
  3. *' v6 H( v; F5 s
  4. *                    (a) Section 2.1 of the  ABI for the ARM Architecture Advisory Note. SP must be 8-byte aligned 2 X% H8 X/ H% X$ L
  5. *                        on entry to AAPCS-Conforming functions states :
    ; s7 A. o6 m$ t) M& d: N6 l6 M* {  d
  6. *                    
      x5 O  a1 G6 d3 G8 ]# d, C
  7. *                        The Procedure Call Standard for the ARM Architecture [AAPCS] requires primitive
      B8 a( D5 A' w' u/ \# \" B7 m
  8. *                        data types to be naturally aligned according to their sizes (for size = 1, 2, 4, 8 bytes).
    0 z6 r+ X/ o: o- {% z
  9. *                        Doing otherwise creates more problems than it solves. 0 e% ]2 C: l8 i) I) K( b
  10. *2 j; O% h- I. _, e+ X. I* Z
  11. *                        In return for preserving the natural alignment of data, conforming code is permitted " M, G" _0 Y8 u( c
  12. *                        to rely on that alignment. To support aligning data allocated on the stack, the stack
    7 f4 r" n; t2 P, G: F9 p. f
  13. *                        pointer (SP) is required to be 8-byte aligned on entry to a conforming function. In
    7 A! d+ B/ n2 l8 M2 X! U0 O$ f% c
  14. *                        practice this requirement is met if:! c- u" l+ P9 ]2 w
  15. *
    & ^' C3 ^+ K* j1 k! O2 b  C
  16. *                           (1) At each call site, the current size of the calling function抯 stack frame is a multiple of 8 bytes.3 v0 V3 }, ~7 m3 t) u& V
  17. *                               This places an obligation on compilers and assembly language programmers.7 m. G& U1 e. ^  u: d3 R, R4 Y. Z
  18. *9 b: w8 m* L- j  C$ ?; h. [: W
  19. *                           (2) SP is a multiple of 8 when control first enters a program.
    . s7 |9 b4 p5 A0 {" g1 G6 V  P' k
  20. *                               This places an obligation on authors of low level OS, RTOS, and runtime library 3 [) G" p! y' D3 }! a' _
  21. *                               code to align SP at all points at which control first enters - f$ w' {1 O3 T
  22. *                               a body of (AAPCS-conforming) code.
    , Q% A9 q7 o; v
  23. *              / h: {1 y8 y) A" h
  24. *                       In turn, this requires the value of SP to be aligned to 0 modulo 8:
    7 q- f& q+ {6 J) {$ g, x1 ^
  25. *
    5 p4 P6 ^8 d# n8 |
  26. *                           (3) By exception handlers, before calling AAPCS-conforming code.
    $ M* i5 H' Z  z
  27. *3 d, \0 F1 X( I5 Q$ |8 C/ {
  28. *                           (4) By OS/RTOS/run-time system code, before giving control to an application.
    # l+ o" a7 R. G2 t( b% r
  29. *
    8 w. ~7 z1 g) x
  30. *                 (b) Section 2.3.1 corrective steps from the the SP must be 8-byte aligned on entry
    ; b* e  T9 g, d0 g6 h) ~
  31. *                     to AAPCS-conforming functions advisory note also states.
    8 X1 ?, o2 ?, X
  32. * # H8 n; S+ k9 p. h/ f3 v" Y
  33. *                     " This requirement extends to operating systems and run-time code for all architecture versions
    & p$ J5 U+ J$ {$ X7 `6 y
  34. *                       prior to ARMV7 and to the A, R and M architecture profiles thereafter. Special considerations
    9 P1 c6 Y8 d8 h2 ^1 Z; V
  35. *                       associated with ARMV7M are discussed in ?.3.3"
    : P/ C' E9 u- y$ t( {: [' S
  36. * 4 A$ a+ s5 B/ C( T$ l  [
  37. *                     (1) Even if the SP 8-byte aligment is not a requirement for the ARMv7M profile, the stack is aligned& q8 T5 v( q8 |; Z* ~/ C
  38. *                         to 8-byte boundaries to support legacy execution enviroments.
    9 R" Y$ y9 b: v& b; {
  39. *! N/ `0 p: S" ]+ u8 E; u
  40. *                 (c) Section 5.2.1.2 from the Procedure Call Standard for the ARM 0 _2 O9 P4 g3 R0 W( r
  41. *                     architecture states :  "The stack must also conform to the following " ]: X: H$ H0 Y3 \
  42. *                     constraint at a public interface:5 X# R5 S, f  F: I" u. d
  43. *
    1 }; Q, r1 o  n1 m4 i
  44. *                     (1) SP mod 8 = 0. The stack must be double-word aligned"1 w2 r# L# m: ], M: Z
  45. *7 L5 z4 ]$ W" w& _3 i# Q  q5 z& T
  46. *                 (d) From the ARM Technical Support Knowledge Base. 8 Byte stack aligment.
    5 J0 x. s9 O8 @( \
  47. *
    ! u" k4 U. H, E) {1 V1 I
  48. *                     "8 byte stack alignment is a requirement of the ARM Architecture Procedure
    " j5 Z4 C) k$ ^
  49. *                      Call Standard [AAPCS]. This specifies that functions must maintain an 8 byte
    & D# V) c. m# a: |( m2 |8 B; p
  50. *                      aligned stack address (e.g. 0x00, 0x08, 0x10, 0x18, 0x20) on all external 5 l2 ]$ C# m. \1 |
  51. *                      interfaces. In practice this requirement is met if:
    ' s5 P) p$ J) U8 @1 d6 o
  52. *6 f! X7 v! \# H$ o
  53. *                      (1) At each external interface, the current stack pointer
    % d& m, I1 W9 t* O) P, b" C
  54. *                          is a multiple of 8 bytes.* a7 D8 D- E/ c* K& p
  55. *
    % w$ f; E7 u, S
  56. *                      (2) Your OS maintains 8 byte stack alignment on its external interfaces
    * ]. e9 r; A) d$ S
  57. *                          e.g. on task switches"
    6 R2 W7 _4 m6 p7 V# E1 u
  58. *
    5 w4 q( p2 a6 }  l/ }7 A
  59. **********************************************************************************************************7 n8 D) O; X9 }1 m1 ~
复制代码
" R, ^$ q6 k, h# P
# [- W. [8 }7 {

! F, y- |$ @6 @! H7 p$ a% A3 o; ^6 t
baiyongbin2009 回答时间:2014-12-23 19:38:02
(续)10.1  官方移植方案
" M) r7 J& n, j: S% s+ ?+ [
  1. */
    : W+ P1 t$ `7 g) C
  2. : s/ R! D+ b0 ~0 ^5 E5 G9 `+ T
  3. OS_STK *OSTaskStkInit (void (*task)(void *p_arg), void *p_arg, OS_STK *ptos, INT16U opt)
    6 {7 j3 Q$ {$ T/ m, b  R. G
  4. {
    8 N7 r( \& X; T6 k- m6 x
  5. OS_STK *p_stk;( n! J' d( U8 }7 Y8 S7 u) m$ C

  6. 4 B: L# T6 y& g; V! Z

  7. ! t5 Q0 |' }" Q( I. ?8 h
  8. p_stk = ptos + 1u; /* Load stack pointer */
    9 f7 l1 H7 l; d1 T5 G
  9. /* Align the stack to 8-bytes. */
    - F, \& G% d5 M' y- \/ M) c
  10. p_stk = (OS_STK *)((OS_STK)(p_stk) & 0xFFFFFFF8u);
    4 o# r9 e+ K: e7 g4 Q5 P
  11. /* Registers stacked as if auto-saved on exception */
    0 z9 R8 ^  l" y$ T( h* L
  12. *(--p_stk) = (OS_STK)0x01000000uL; /* xPSR */+ q% {$ N( p. _" j4 u
  13. *(--p_stk) = (OS_STK)task; /* Entry Point */
    3 d" I: U6 ^' ~( p
  14. *(--p_stk) = (OS_STK)OS_TaskReturn; /* R14 (LR) */' v6 u# R) O( @: [! C/ }! C1 Q3 ^
  15. *(--p_stk) = (OS_STK)0x12121212uL; /* R12 */3 ]: N4 ^% z, F( S$ `% ?
  16. *(--p_stk) = (OS_STK)0x03030303uL; /* R3 */( J, j- N. @9 z( W3 N
  17. *(--p_stk) = (OS_STK)0x02020202uL; /* R2 */
    ; n  i/ R9 n* w# V" y/ B2 J% b
  18. *(--p_stk) = (OS_STK)0x01010101uL; /* R1 */( ?5 ?% f2 f6 [
  19. *(--p_stk) = (OS_STK)p_arg; /* R0 : argument */
    7 [8 g& R! Y$ y2 L" x# }

  20. - R# a( e& a7 W1 [4 t+ X$ [% a
  21. /* Remaining registers saved on process stack */" M$ }7 o( c4 {- q; t0 ]
  22. *(--p_stk) = (OS_STK)0x11111111uL; /* R11 */
    : d. |  @$ X2 j& f
  23. *(--p_stk) = (OS_STK)0x10101010uL; /* R10 */
    ! U; Q( d9 \/ P) K2 J0 n
  24. *(--p_stk) = (OS_STK)0x09090909uL; /* R9 */! a" |7 ?2 [. I; s/ h+ X7 d
  25. *(--p_stk) = (OS_STK)0x08080808uL; /* R8 */+ e- ?, f! D& `) }" u0 r7 ]: X6 Z
  26. *(--p_stk) = (OS_STK)0x07070707uL; /* R7 */" o1 g* ]. r1 e/ q/ G( M+ \" a
  27. *(--p_stk) = (OS_STK)0x06060606uL; /* R6 */
    4 c  \. O) x* c: O; n" m; e
  28. *(--p_stk) = (OS_STK)0x05050505uL; /* R5 */
    6 a2 p+ u* L" |; C7 t8 g, j
  29. *(--p_stk) = (OS_STK)0x04040404uL; /* R4 */# n3 p) a7 Q6 L6 K* L  g3 w- Z
  30. 3 ^; S( a" @& y+ ?  |  W
  31. #if (OS_CPU_ARM_FP_EN > 0u)7 ^# b. X) Z8 @3 p) ~* \
  32. if ((opt & OS_TASK_OPT_SAVE_FP) != (INT16U)0) {
    ; i- |+ |8 |2 l" }
  33. *--p_stk = (OS_STK)0x02000000u; /* FPSCR */
    , }' ~. F! V5 |6 d) S
  34. /* Initialize S0-S31 floating point registers */
    * F3 @' v8 V) r) z9 _& e4 o( L5 D
  35. *--p_stk = (OS_STK)0x41F80000u; /* S31 *// A' I$ I, S1 \/ N# f$ a
  36. *--p_stk = (OS_STK)0x41F00000u; /* S30 */
    8 c/ i4 f4 I# C: K+ ?8 U# B5 D
  37. *--p_stk = (OS_STK)0x41E80000u; /* S29 */
    5 {- ~& ^( S8 M2 h, M
  38. *--p_stk = (OS_STK)0x41E00000u; /* S28 */
    9 x' o; O- B+ H1 ^% M6 u
  39. *--p_stk = (OS_STK)0x41D80000u; /* S27 */
    2 l0 v7 ~: ?$ M0 {9 q2 L: I
  40. *--p_stk = (OS_STK)0x41D00000u; /* S26 */; h3 O4 t* m) U) a7 L! V2 q4 L  E. \
  41. *--p_stk = (OS_STK)0x41C80000u; /* S25 */+ G6 @9 L$ I( }9 y8 ^2 b
  42. *--p_stk = (OS_STK)0x41C00000u; /* S24 */! r  n8 g* `$ b( G8 y
  43. *--p_stk = (OS_STK)0x41B80000u; /* S23 */
    - q( e* Q2 A' j* Q- ^8 o
  44. *--p_stk = (OS_STK)0x41B00000u; /* S22 */
    9 q6 k8 @3 w: t0 _8 |% o" T# I
  45. *--p_stk = (OS_STK)0x41A80000u; /* S21 *// }& F% R3 h# c
  46. *--p_stk = (OS_STK)0x41A00000u; /* S20 */8 m$ H: U: y3 A
  47. *--p_stk = (OS_STK)0x41980000u; /* S19 */
    + K# |. K9 A! t3 h3 F) O
  48. *--p_stk = (OS_STK)0x41900000u; /* S18 */
    ; N+ c3 H' R, v/ o9 V  L
  49. *--p_stk = (OS_STK)0x41880000u; /* S17 */
    1 [6 b4 W: j2 e) ?
  50. *--p_stk = (OS_STK)0x41800000u; /* S16 */
    + f2 x7 @7 Q3 f9 l
  51. *--p_stk = (OS_STK)0x41700000u; /* S15 */
    ; n' L" e2 W% j5 K
  52. *--p_stk = (OS_STK)0x41600000u; /* S14 */0 z* Z5 O! u* p8 H
  53. *--p_stk = (OS_STK)0x41500000u; /* S13 */1 O1 [% y/ u0 s2 T" Y$ e' d! n: ^) ?
  54. *--p_stk = (OS_STK)0x41400000u; /* S12 */
    # Z- S4 H/ ~* ^, D6 k7 U
  55. *--p_stk = (OS_STK)0x41300000u; /* S11 */
    ( ?# N- M% [! W* y- M
  56. *--p_stk = (OS_STK)0x41200000u; /* S10 */
    2 ?' L* S. X! A9 `
  57. *--p_stk = (OS_STK)0x41100000u; /* S9 */$ p3 J. n. ^1 p) A
  58. *--p_stk = (OS_STK)0x41000000u; /* S8 */
    9 b! [, o( ]- d9 F  G5 A+ K( d) m
  59. *--p_stk = (OS_STK)0x40E00000u; /* S7 */
    6 x. ^- R" i- b% L$ @, E
  60. *--p_stk = (OS_STK)0x40C00000u; /* S6 */7 e: `. `+ R% i% x: j* d
  61. *--p_stk = (OS_STK)0x40A00000u; /* S5 */
    5 N' Y5 P2 x! O3 s$ A  h( |
  62. *--p_stk = (OS_STK)0x40800000u; /* S4 */
    - m: T7 P( V7 k; h
  63. *--p_stk = (OS_STK)0x40400000u; /* S3 */
    * m! v2 ]6 z7 B  S# C% v5 v# z
  64. *--p_stk = (OS_STK)0x40000000u; /* S2 */% x/ V( g$ U: t: `) k$ P+ n
  65. *--p_stk = (OS_STK)0x3F800000u; /* S1 */. Z! M3 a4 L( N. z  R) E
  66. *--p_stk = (OS_STK)0x00000000u; /* S0 */
    ' E. B3 b+ b- [' T- L
  67. }- q, k; r2 Y* d8 {" q
  68. #endif 0 i" f8 v. V9 @
  69. " b9 m$ \! p9 r, m
  70. return (p_stk);+ M% r4 w; T2 j. p9 w# U
  71. }
复制代码
    官方提供的这个堆栈初始化是错误的的,为什么是错误的?因为这个不符合浮点寄存器的入栈和出栈顺序。还有一部分代码在os_cpu_a.asm文件中,内容如下:
  1. #ifdef __ARMVFP__
    1 E% G$ ]1 c# ?8 g( R0 k! P
  2.     PUBLIC  OS_CPU_FP_Reg_Push: A- U# t4 @$ A8 L) |9 z- o3 x
  3.     PUBLIC  OS_CPU_FP_Reg_Pop
    6 c+ {0 t6 E9 l3 O. A7 M. D- k( a
  4. #endif   
    - S; z' C7 T1 E/ w# S8 y

  5. 1 y/ v& a) a5 f) i7 \
  6. ;********************************************************************************************************
    ( n2 n7 }+ i# V/ @0 J) l
  7. ;                                               EQUATES
      u7 G# ^3 G, s
  8. ;********************************************************************************************************1 g- y- }$ U8 L6 Y* T2 Y

  9. 7 l4 v* I! H6 l0 \4 ?
  10. NVIC_INT_CTRL   EQU     0xE000ED04                              ; Interrupt control state register.% E& N! V* B" R' [* u# T6 C
  11. NVIC_SYSPRI14   EQU     0xE000ED22                              ; System priority register (priority 14).
    ; ]  D4 a( Q) J3 H
  12. NVIC_PENDSV_PRI EQU           0xFF                              ; PendSV priority value (lowest).
      }# M2 X7 Q' R/ z8 `
  13. NVIC_PENDSVSET  EQU     0x10000000                              ; Value to trigger PendSV exception.
    8 x' y; z/ X. F7 H8 C& x% y
  14. # Q+ d. s! W5 R

  15. 8 h/ L2 O) t% j) l  k/ X
  16. ;********************************************************************************************************
    + y) q& v: a: y/ g( X
  17. ;                                     CODE GENERATION DIRECTIVES
    / f) F  U9 X2 T; p" c2 C1 ~" n" D
  18. ;********************************************************************************************************
    ( ~; B: L; U  n* ^

  19. , R( C% [1 Z# H9 r0 y9 A/ R# S/ V
  20.     RSEG CODE:CODE:NOROOT(2)- k) a& P$ V$ i! v! G+ z+ t
  21.     THUMB
    3 {2 f* x6 k0 F/ U) R7 F
  22. / p0 h. k4 ^" b; G$ p* h
  23. #ifdef __ARMVFP__4 f8 |% L+ X* V, b4 [$ Z- s4 a2 D  G
  24. ;********************************************************************************************************+ E5 e7 y4 z* [
  25. ;                                   FLOATING POINT REGISTERS PUSH# \) S2 D, I+ ^6 k
  26. ;                             void  OS_CPU_FP_Reg_Push (OS_STK  *stkPtr)
    & K! J: ]; R' ^
  27. ;
    $ d- ^/ G* n3 Y8 H2 |1 K* G1 w$ }
  28. ; Note(s) : 1) This function saves S0-S31, and FPSCR registers of the Floating Point Unit.
    . y: k3 [6 c$ c) T. R9 ^
  29. ;) ]3 l/ ?" g& f1 j' E- j; A
  30. ;           2) Pseudo-code is:
    ' s0 z4 u5 r+ f6 h
  31. ;              a) Get FPSCR register value;0 ]' X) y& I; o3 a6 k5 C' e
  32. ;              b) Push value on process stack;
    , w' U+ c8 S8 R4 C7 K3 x, g
  33. ;              c) Push remaining regs S0-S31 on process stack;- K* t0 ?5 `, ~1 u
  34. ;              d) Update OSTCBCur->OSTCBStkPtr;% R5 b8 s& l! m( m
  35. ;********************************************************************************************************
    ; c- [# J% r) s! g! E0 b: X
  36. ( }6 @# k% B$ W9 i/ Z# \* l
  37. OS_CPU_FP_Reg_Push/ d, @/ B, p( s, R5 F, G  u$ d
  38.     MRS     R1, PSP                                             ; PSP is process stack pointer( M  L, Y- |+ d* A
  39.     CBZ     R1, OS_CPU_FP_nosave                                ; Skip FP register save the first time/ p- f" a7 D  g/ k; a5 ^
  40.    
    & U2 O5 [' b) ?$ K
  41.     VMRS    R1, FPSCR9 i2 V- t" z  S- v
  42.     STR R1, [R0, #-4]!
    - B9 M# E& x( O6 N. \6 L
  43.     VSTMDB  R0!, {S0-S31}
    . p. |8 g0 ^3 K, \" a% S/ W" d
  44.     LDR     R1, =OSTCBCur2 F# W: B7 @% J, x# B2 U0 A4 q
  45.     LDR     R2, [R1]/ g/ o/ p  E* _  Q! U
  46.     STR     R0, [R2]+ C. H: C& t. g) h5 T/ [7 h9 p
  47. OS_CPU_FP_nosave   7 `  z8 ^! Y. L7 I' l/ y: v
  48.     BX      LR6 N6 @" H8 q5 Z
  49. : g/ v7 g  U- n" s$ Y' ]
  50. ;********************************************************************************************************8 g/ ?5 l6 e8 K7 J& f3 P5 d: z
  51. ;                                   FLOATING POINT REGISTERS POP& ~4 R5 v" W$ t1 b0 a/ h( O' L
  52. ;                             void  OS_CPU_FP_Reg_Pop (OS_STK  *stkPtr)8 h0 B1 `, o% `% L* s
  53. ;6 f3 a1 \# g' X+ {
  54. ; Note(s) : 1) This function restores S0-S31, and FPSCR registers of the Floating Point Unit.
    ' a( B3 t  q$ C+ j3 ]! I
  55. ;
    ( h/ b8 l: s3 F3 v
  56. ;           2) Pseudo-code is:! s3 B4 M8 h, T% J1 S2 G7 n
  57. ;              a) Restore regs S0-S31 of new process stack;
    5 r6 L9 O- X: N8 j6 y% V6 Q# z
  58. ;              b) Restore FPSCR reg value4 J( V1 @- [3 A( T. [% a
  59. ;              c) Update OSTCBHighRdy->OSTCBStkPtr pointer of new proces stack;" z5 Z9 L+ z* h% c" d, U, N
  60. ;********************************************************************************************************1 v( Y, ~/ J3 C" Q
  61. $ k2 h. v/ @6 O( B/ t
  62. OS_CPU_FP_Reg_Pop
    2 x5 a: l) A* t9 s- A
  63.     VLDMIA  R0!, {S0-S31}  x$ d  `' Y5 A
  64.     LDMIA   R0!, {R1}& g* L9 T; N! i$ @) E5 ~
  65.     VMSR    FPSCR, R1   
    + p2 a7 u1 W0 F- i0 Z( _3 V$ d
  66.     LDR     R1, =OSTCBHighRdy
    & l. @% ~9 n9 f, D3 l5 c% s" N
  67.     LDR     R2, [R1]
    - D7 f1 P, [8 S: G* J* ~3 [% i
  68.     STR     R0, [R2]
    0 O' ~: u5 j) N$ e
  69.     BX      LR
    $ ^6 m4 y# }3 S
  70. #endif
复制代码
    如果不理解为什么这个浮点寄存的入栈和出栈是错误的,需要认真学习一下第5章:任务切换设计。第5章对于这个问题有深入的讲解。
8 X0 B; k+ `' k, y
baiyongbin2009 回答时间:2014-12-23 19:42:38
本帖最后由 baiyongbin2009 于 2014-12-23 19:48 编辑
1 {: Z5 x# s! u3 U  X- A2 e: s  T
10.2  开启FPU解决方案
    为了解决FPU的问题,有两个函数需要修改:一个是CPU_STK  *OSTaskStkInit(),另一个是PendSV中断。
10.2.1      修改函数CPU_STK  *OSTaskStkInit()
    函数所在的位置如下:
10.1.png
2 p. W3 [$ p2 v# K
    下面是修改后的内容:
  1. CPU_STK  *OSTaskStkInit (OS_TASK_PTR    p_task,
      X' h0 x# D. k
  2.                          void          *p_arg,
    5 }/ r5 A/ B' f2 c" C( ?
  3.                          CPU_STK       *p_stk_base,
    0 {/ @/ p' h9 Q6 F2 H7 K
  4.                          CPU_STK       *p_stk_limit,
    . i9 f* H: }3 K0 R  m4 e
  5.                          CPU_STK_SIZE   stk_size,# ^& C$ }( [1 f0 X8 R- N+ [7 `
  6.                          OS_OPT         opt)
    ; \# I7 a2 [! P  M
  7. {' g$ o+ F8 H3 N' _0 w$ x4 u& u5 p
  8.     CPU_STK  *p_stk;
    5 j/ c4 ?7 b1 n) Q% C

  9. ) M# m. [! Q0 d, r# P  T( k

  10. 5 L3 K: |0 I! X# |: ?3 \8 b
  11.     (void)opt;                                              /* Prevent compiler warning                               */
    / f% p& q4 O. z

  12. ! }- ]+ E* R# T6 R
  13.     p_stk = &p_stk_base[stk_size];                          /* Load stack pointer                                     */! w* l: Y8 H) g2 {& a
  14.                                                             /* Align the stack to 8-bytes.                            */
    5 R, n0 A! O$ w5 g1 S4 v
  15.     p_stk = (CPU_STK *)((CPU_STK)(p_stk) & 0xFFFFFFF8);
    0 X4 n, y- [8 D' U
  16.                                                             /* Registers stacked as if auto-saved on exception        */
    2 `' T& Y4 ]4 t' o2 t+ T

  17. & N1 x* c6 c% t
  18. *--p_stk = (CPU_STK)0x01000000u;                        /* xPSR                                                 */
    2 g! `; `/ G6 i* y
  19.     *--p_stk = (CPU_STK)p_task;                             /* Entry Point                                            */
    8 `5 V) n2 G$ }" W
  20.     *--p_stk = (CPU_STK)OS_TaskReturn;                      /* R14 (LR)                                               */" s' Q' I: W4 _- B1 X; P! P
  21.     *--p_stk = (CPU_STK)0x12121212u;                        /* R12                                                    */" m, s# v& P* {
  22.     *--p_stk = (CPU_STK)0x03030303u;                        /* R3                                                     */
    6 Q4 I" _3 X( f/ q7 A6 O; G9 M2 X5 [
  23.     *--p_stk = (CPU_STK)0x02020202u;                        /* R2                                                     */
    7 i# A8 g5 R% \1 ?4 ^  R
  24.     *--p_stk = (CPU_STK)p_stk_limit;                        /* R1                                                     */
    # c, Z6 D) A. m
  25.     *--p_stk = (CPU_STK)p_arg;                              /* R0 : argument                                          */
    0 s5 j( @* K% y7 v1 I% m8 e4 C" C
  26.                                                             /* Remaining registers saved on process stack             */
    + ]! t& P! D/ ?( g: @# t) A# j
  27.     *--p_stk = (CPU_STK)0x11111111u;                        /* R11                                                    */
    1 U) }: z+ X0 r( t. i
  28.     *--p_stk = (CPU_STK)0x10101010u;                        /* R10                                                    */! Z$ s4 s* J, @2 v' `; r% a
  29.     *--p_stk = (CPU_STK)0x09090909u;                        /* R9                                                     */: U, M$ m: H8 I% o# M3 w
  30.     *--p_stk = (CPU_STK)0x08080808u;                        /* R8                                                     */2 ]% F( z. `  s! E6 B
  31.     *--p_stk = (CPU_STK)0x07070707u;                        /* R7                                                     */
    ; f7 l( ?# {' k0 x
  32.     *--p_stk = (CPU_STK)0x06060606u;                        /* R6                                                     */
    ) k( i# w- v  R% q& \
  33.     *--p_stk = (CPU_STK)0x05050505u;                        /* R5                                                     */
    " h- S4 ?. a7 s' L6 [0 N9 o
  34.     *--p_stk = (CPU_STK)0x04040404u;                        /* R4                                                     */
    9 W$ y2 K# C0 W( p( m
  35. 5 G2 |$ q, s; U) C9 |/ ]
  36.     *--p_stk = (CPU_STK)0xFFFFFFFDUL;                                                                                                                            (1)1 Y) p: G4 W6 P* h
  37.      
    9 o9 m  _3 l+ V( d  p4 x& m
  38.       return (p_stk);
    6 k6 i) Z; ?/ f1 n
  39. }
复制代码
1.     这句话最重要,这里是将EXC_RETURN也进行了入栈处理。关于EXC_RETURN在前面4.2.5 特殊功能寄存器讲解。这里要补充一点,对于M4内核,EXC_RETURN的bit4也是有意义的。
    当bit4 = 1时,8个寄存器自动入栈,还有8个寄存器需要手动入栈
    当bit4 = 0时,18个浮点寄存器+8个寄存器自动入栈,还有16个浮点寄存器+8个寄存器需要手动入栈。
这些寄存器在第4章和第5章有详细的讲解,这里就不再赘述了。
7 t$ j* s- d3 I1 A2 m
10.2.png

0 U$ R5 A  P$ _2 N
: N+ X4 @% o1 A- x% l7 F: y& A
' ?' c6 [/ }/ q4 G% J; F  N
10.2.2      修改函数OS_CPU_PendSVHandler
    函数所在的位置如下:
10.3.png

" K. `* i# v2 ?0 @7 b
    PendSV中断需要修改的地方如下:
  1. OS_CPU_PendSVHandler
      _" I" y  g2 a9 l+ n" ^4 s
  2.     CPSID   I                                                   ; Prevent interruption during context switch8 q) o: _, y/ m. G# _1 F
  3.     MRS     R0, PSP                                             ; PSP is process stack pointer! s8 {7 p& _, v1 y/ W8 }  a
  4.     CBZ     R0, OS_CPU_PendSVHandler_nosave                     ; Skip register save the first time
    2 E" D" l0 ?" }* {
  5. * @1 ]  ?( _8 T
  6.     TST      LR, #0x10                                                                                                                                                (1). U! w8 U& w- F. z! A
  7. IT       EQ
    ( p( P5 \: `$ r
  8. VSTMDBEQ R0!, {S16-S31}
    , r7 H" W  K5 k1 V! c) K) }
  9.    
    # G( z. g+ ^0 l  M
  10. MOV      R3, LR                                                                                                                                                          (2)) X1 z- S9 |4 F' C1 Q: ?, I& K
  11. STMDB    R0!,{R3-R11}             : o; n  N: H. `/ Q3 H: s- l
  12. 2 q3 Q" `9 n% o9 Z8 @
  13.     LDR     R1, =OSTCBCurPtr                                    ; OSTCBCurPtr->OSTCBStkPtr = SP;/ j/ o  C- W! W9 w/ a; n$ v. |3 C
  14.     LDR     R1, [R1]
    + S6 o; F9 Y/ P$ }. @  g5 m
  15.     STR     R0, [R1]                                            ; R0 is SP of process being switched out: \+ s$ A3 p' X

  16. 6 l2 ^+ i7 r  j: |/ l4 d; [* K
  17.                                                                 ; At this point, entire context of process has been saved* q* J& b9 _; U6 q
  18. OS_CPU_PendSVHandler_nosave
    # E& q, ?6 R/ f7 t3 m+ Y# g
  19.     PUSH    {R14}                                               ; Save LR exc_return value
    2 n) I9 ]  j1 A; |/ P4 E
  20.     LDR     R0, =OSTaskSwHook                                   ; OSTaskSwHook();
    $ G8 ?, I9 w$ y4 r' s+ `
  21.     BLX     R0* b% L# p3 k9 Q  ~. @& a9 X
  22.     POP     {R14}
    # P" b& R  H7 y" t6 K; ]
  23. # p" f8 j* Y& j) _
  24.     LDR     R0, =OSPrioCur                                      ; OSPrioCur   = OSPrioHighRdy;
    1 b8 ^: M5 k6 U9 s+ H/ d5 v
  25.     LDR     R1, =OSPrioHighRdy/ S5 v( f" i) f; ?2 O
  26.     LDRB    R2, [R1]
    ; W- K) H6 x4 K" U$ a/ a
  27.     STRB    R2, [R0]
    : F3 d" W% m$ d
  28. ) H2 Q: |! U: q3 @* g& B
  29.     LDR     R0, =OSTCBCurPtr                                    ; OSTCBCurPtr = OSTCBHighRdyPtr;; ~; g4 u% |! x8 i9 f
  30.     LDR     R1, =OSTCBHighRdyPtr4 q$ |) f0 V4 _4 i5 u9 O. }
  31.     LDR     R2, [R1]2 Q# l1 \, N0 n; [
  32.     STR     R2, [R0]
    # b; W* |' P, b, w- l
  33.     LDR     R0, [R2]                                            ; R0 is new process SP; SP = OSTCBHighRdyPtr->StkPtr;
    5 Y/ ~  p) ?% \' O0 X2 D
  34. 0 F" C, `% i# i
  35. 2 x2 I7 b" o, f0 [
  36.     LDMIA    R0!,{R3-R11}                                                                                                                                                  (3)$ f6 R: d& y) X/ M! V# e( X
  37. MOV      LR, R3
    9 e( ~5 v1 s7 J' i: ~( `
  38.          
    . r$ e* ]0 i$ u2 _( I$ B" v5 j
  39. TST      LR, #0x10                                                                                                                                                                (4)& C4 w% t) [3 Y4 {: n  P6 U
  40. IT       EQ- {6 u& H% U8 d$ _* H9 w( i. k
  41. VLDMIAEQ R0!, {S16-S31}           
    $ R' y9 n) t- L4 }
  42.    
    0 I3 e! E( j$ ~6 H% \7 w
  43.     MSR     PSP, R0                                             ; Load PSP with new process SP) K  ?& M2 E% ]; H: C4 h7 @
  44.    6 v3 Z2 Y/ L4 p2 L: Z
  45.     CPSIE   I
    / {. s' @% U/ q0 H+ h4 s5 Q$ d# L( q5 I
  46.     BX      LR                                                  ; Exception return will restore remaining context7 _+ H" R3 K- o  C- N

  47. & V# [. b+ Z$ e7 \: p$ g
  48.     END
复制代码
1.     通过检测EXC_RETURN(LR)的bit4来看这个任务是否使用了浮点寄存器,如果使用了需要将剩余的16个浮点寄存器入栈。
TST   LR,  #0x10:
    TST指令通常与EQ,NE条件码配合使用。当所有测试位均为0时,EQ有效。而只要有一个测试位不为0,则NE有效。这里LR和0x10的值按位作逻辑“与”操作。
IT  EQ :
    这里IT指令就是IF-THEN的缩写。IF‐THEN(IT)指令围起一个块,里面最多有4条指令,它里面的指令可以条件执行。 IT已经带了一个“T”,因此还可以最多再带3个“T”或者“E”。并且对T和E的顺序没有要求。其中T对应条件成立时执行的语句,E对应条件不成立时执行的语句。在If‐Then块中的指令必须加上条件后缀,且T对应的指令必须使用和IT指令中相同的条件,E对应的指令必须使用和IT指令中相反的条件。
    IT的使用形式总结如下:
IT<cond> ;围起1条指令的IF-THEN块
IT<x><cond> ;围起2条指令的IF-THEN块
IT<x><y>  <cond> ;围起3条指令的IF-THEN块
IT<x><y><z><cond> ;围起4条指令的IF-THEN块
    其中<x>,<y>, <z>的取值可以是“T”或者“E”。而<cond>则是在下表中列出的条件(AL除外)。
10.4.png

4 |" x% z5 V' U- K7 G
' l' V0 o  ~) L: p* n" q
' [9 I9 J2 W$ O8 D) k5 V3 T
baiyongbin2009 回答时间:2014-12-23 19:45:29
(续)10.2.2  修改函数OS_CPU_PendSVHandler
    这么说还不够形象,下面举一个简单的例子,用IT指令优化C伪代码:
if(R0==R1)
{
R3= R4 + R5;
R3= R3 / 2;
}
else
{
R3= R6 + R7;
R3= R3 / 2;
}
    可以写作:
CMP  R0, R1     ; 比较R0和R1
ITTEEEQ          ; 如果R0== R1, Then-Then-Else-Else
ADDEQR3,R4, R5  ; 相等时加法
ASREQR3,R3, #1   ; 相等时算术右移
ADDNER3,R6, R7  ; 不等时加法
ASRNER3,R3, #1   ; 不等时算术右移
有了上面这些基础知识后再看下面的指令。
VSTMDBEQ R0!, {S16-S31}:
    结合上面的IT EQ,这里的意思就是 if  LR & 0x10 == 0 then  VSTMDB R0!, {S16-S31}。也就是咱们前面所说的如果EXC_RETURN(LR)的bit4= 0就表示使用浮点寄存器了,这里需要入栈。
2.     这里比较好理解,只不过也将EXC_RETURN进行了入栈。
3.     参考上面的第二条,只不过这里是出栈。
4.     参考上面的第一条,只不过这里是出栈。
10.2.3      开启FPU
    修改了上面的两个地方后别忘了开启FPU:
10.5.png
                              
    通过上面这三部就完成了相关的修改。
% P8 W3 h9 }, s# u* Z) `# a
baiyongbin2009 回答时间:2014-12-23 19:46:53
10.3  开启FPU的优劣
    开启FPU的好处就是加快浮点运算的执行速度,缺点就是增加任务堆栈的大小,因为34个浮点寄存器也需要入栈。同时也增加了任务的切换时间。下面是在μCOS-III的GUI任务和启动任务中使用了浮点运算的对比(App Task GUI和App Task Start)。
    第一个截图是开启了FPU:
10.6.png
    第二个截图是没有开启FPU:
10.7.png

+ N* A2 k# y5 I2 C* j. Q0 t
    特别对比下使用了浮点运行的两个任务。
10.4  总结       
6 w8 x' R% k* |+ q" `8 J9 y$ Q

5 w% V* O; _6 k! ?7 m  J    本期教程提供的方案在MDK4.54、4.73、5.10以及IAR6.3、6.7上面测试均通过,用户只需按照上面讲的三个地方做修改即可。* n0 |7 U1 d. {) b( x) K: B
& a  r& O/ @) x3 G: h- D1 {

" c) @; {+ Z6 u4 r" f9 |$ O- l& i
pjzmj2012 回答时间:2020-3-3 14:48:23
有ucosIII 3。06.02在S32 Design Studio下的教程吗

所属标签

相似技术帖

官网相关资源

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