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

笔记---FreeRtos系统中外设中断优先级设置的要点

[复制链接]
power568 发布时间:2019-4-16 14:15
本帖最后由 power568 于 2019-4-16 16:07 编辑 4 z9 U( L& _7 B! B- I

+ l. m2 x! v" Z( T; C) s       最近在写一个串口通讯的程序,软件、硬件平台如下:# W( O0 [8 c5 D0 {0 E6 \4 {
硬件: STM32F429ZG0 J8 e9 v# q% I  r
系统: FreeRtos、HAL库, [# s6 A1 E* [5 {
工具:  Iar7.6
& l: [: `* e' W1 U* g
" s% P2 G$ T8 x* o       计划是利用串口DMA+IDLE空闲中断的方式接收数据,在中断内将接收到的数据通过队列的方式发送出去,在任务内部等待接收队列,串口中断配置及ISR如下:
  1. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);//// 中断配置,优先级大小不能乱配置
    2 f. S% ]% r  `" b: _+ m
  2. HAL_NVIC_EnableIRQ(USART1_IRQn);
    - k+ J/ O8 K4 T6 i. Z
  3. 3 T  ~! |" `; n( a* i
  4. void USART1_IRQHandler(void)% B/ A6 P7 y( X! Q
  5. {  G) k1 Z. E* B. q3 R/ ~' ~6 r
  6.         uint32_t                 flag=0;
    : J, p0 ]3 W, Z6 y
  7.         BaseType_t        pHigherPriorityTaskWoken = pdFALSE;
      D6 k; B1 A) q' b1 u" a$ I
  8.         # B/ C! Z* t% p" H+ C
  9.         flag = __HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE); 5 H0 W+ _; {/ n
  10.         if((flag != RESET))# W6 K$ g4 v! O, [, |
  11.         {' M* a# ?+ ?, g  B* L9 ]0 L
  12.                 __HAL_UART_CLEAR_IDLEFLAG(&huart1);# v# n8 m- L. X# A! v( w5 F
  13.                 huart1.Instance->SR;  
    $ Z; T4 s9 x1 t
  14.                 huart1.Instance->DR;! j/ t# M% Z- n! D

  15. 8 p  @! @# K+ o& m9 W) Z- B/ c$ P% S
  16.                 HAL_UART_DMAStop(&huart1);* }5 g! A' S. J1 D
  17.                 xQueueSendFromISR(UrtK125RcvQueue, DrvUsartRcvBuf, &pHigherPriorityTaskWoken);. T' E7 U/ Q6 x4 Q
  18.                 portYIELD_FROM_ISR(pHigherPriorityTaskWoken);7 R, m3 e) Y( E& i/ A' q, B
  19.         }
    ; _+ B6 v  d' r5 {
  20. }
复制代码

3 l# H. f: D6 u5 Z8 h& d
) H1 M! W' H/ k" S  //// 为什么代码提交后,代码对齐格式变了呢???
7 {5 J' J7 \, d' e& _0 ?$ q8 ?; F: l6 H- J& ], T4 c( V
       调试发现进入中断后,程序卡在了函数xQueueSendFromISR(UrtK125RcvQueue, DrvUsartRcvBuf, &pHigherPriorityTaskWoken)内部configASSERT( ucCurrentPriority >= ucMaxSysCallPriority )处,具体为:
8 @1 }- P/ A8 u7 f
  1. #if( configASSERT_DEFINED == 1 )/ v9 s1 E, l2 }, p

  2. 0 X% f  r+ v9 o8 X% w3 U* s: k
  3.         void vPortValidateInterruptPriority( void )9 e5 S, [) x6 D0 C* D& P
  4.         {% D( G- ]# \, y4 ~4 W( F- w# }
  5.         uint32_t ulCurrentInterrupt;
    8 T- G) v1 s/ t7 e7 P5 j2 F
  6.         uint8_t ucCurrentPriority;1 }5 T# i& F5 ?- h& i  ^3 y
  7. , ^: b! y$ m  @2 {
  8.                 /* Obtain the number of the currently executing interrupt. */) a( ?# A6 U% O
  9.                 __asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) );3 v, ^& {2 i0 a
  10.                 //// ulCurrentInterrupt 调试显示为53, Z# t' K6 R4 a' n% i  M% b8 w

  11. 5 W6 S4 u; w* u. q4 k
  12.                 /* Is the interrupt number a user defined interrupt? */
    ; w8 c: B  E! S/ [1 P! x1 s
  13.                 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
    4 Q% \4 ?3 i9 p% e/ S
  14.                 {* u$ T+ v5 p8 E7 S% Z; X$ v: q
  15.                         /* Look up the interrupt's priority. */! n2 T. _6 t$ A, o3 o1 G% j5 L
  16.                         ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];9 T/ ^8 {  c8 K
  17.                         //// 当串口优先级设置小于5时,ucCurrentPriority的值小于0x50,导致下面的宏断0 E2 n9 \2 U0 a# N/ M6 Y8 F
  18.                         //// 言configASSERT条件值"==0",进而卡住, F0 t: _$ D, F+ I) I& e+ b
  19.                         //// 当串口优先级设置大于等于5时,ucCurrentPriority的值大于等于0x50,下面的( ^( T# l, N  R/ m; O3 \& ^/ [
  20.                         //// 宏configASSERT条件值"==1",所以不会卡住" S8 R$ I8 |8 }. \  h
  21. / y% X- X6 r6 j& G% N1 Y1 P; |) G# B
  22.                         /* The following assertion will fail if a service routine (ISR) for6 M/ Q2 s  Z) ~, p' c" m
  23.                         an interrupt that has been assigned a priority above! ?# }* n  U0 k$ {' [8 S0 B, h
  24.                         configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
    7 u  |; d4 Y& C! U
  25.                         function.  ISR safe FreeRTOS API functions must *only* be called- q& M6 I: S  U: z/ w
  26.                         from interrupts that have been assigned a priority at or below8 A3 D/ O  M$ A2 h! [
  27.                         configMAX_SYSCALL_INTERRUPT_PRIORITY./ M/ S; w8 z& Z
  28.                         //// 如果一个调用FreeRtos安全ISR API接口的中断ISR优先级分配大于1 d0 I: x3 E; B0 E/ Y& Q5 ?$ H
  29.                         //// configMAX_SYSCALL_INTERRUPT_PRIORITY(即中断优先级值小于
    # I$ Y/ i1 F/ c0 i' j2 l
  30.                         //// configMAX_SYSCALL_INTERRUPT_PRIORITY,因为FreeRtos中值越小优先级越高),
    5 @1 T# g# }# i8 S, H
  31.                         //// 下面的断言就会失败。FreeRtos安全ISR API接口必须只能在优先级分配大于/ D5 _8 l4 y; N6 Q2 C6 T
  32.                         //// configMAX_SYSCALL_INTERRUPT_PRIORITY的中断ISR中调用
    ) Z- j) x4 e- B* D. R. R
  33.                         % V1 ^6 Q* t( u8 w7 r1 J( {
  34.                         Numerically low interrupt priority numbers represent logically high* v: W9 j5 x. P  F6 j
  35.                         interrupt priorities, therefore the priority of the interrupt must
    6 v' P9 _, i. |  t7 A
  36.                         be set to a value equal to or numerically *higher* than0 W8 a; @+ _+ I) b6 K+ u" g! ~
  37.                         configMAX_SYSCALL_INTERRUPT_PRIORITY.
    ; W: ^. [% R& u
  38.                         //// 数值小的中断优先级逻辑上具有高的中断有限权,因此设置时,中断的优先级
    ( [2 k  P( O1 U) q
  39.                         //// 值必须必configMAX_SYSCALL_INTERRUPT_PRIORITY大: k% @2 t# ~7 _, H3 r
  40. * G6 L" s9 t/ ^& t4 C
  41.                         Interrupts that        use the FreeRTOS API must not be left at their9 Q! d0 A0 O9 H# d& M4 {. [( s
  42.                         default priority of        zero as that is the highest possible priority,! `) `* @5 ?# W
  43.                         which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,; B2 `4 i- v# Q
  44.                         and        therefore also guaranteed to be invalid.
    $ p1 i% b+ g. \! j
  45.                         //// 使用FreeRTOS API的中断的优先级禁止使用其0的认值,因为此时中断优先级最3 U- Q3 c: U: g; D& G
  46.                         //// 高,0值的优先级必然大于configMAX_SYSCALL_INTERRUPT_PRIORITY,因此也必# s' T8 @( ?3 ~4 _1 I7 h
  47.                         //// 然无效' |; f% K$ e) d
  48. + u4 a) s) S* P" Z
  49.                         FreeRTOS maintains separate thread and ISR API functions to ensure- _+ F* _  ^+ M! y5 y6 x  V
  50.                         interrupt entry is as fast and simple as possible.- B5 l! s- Y! {
  51.                         //// FreeRTOS为了确保中断入口尽可能的快和简单,因此分离了线程ISR API接口
    , @1 H3 m, T$ q# s
  52. " ~( h8 Y" m- B( L: F
  53.                         The following links provide detailed information:. M' k- Q3 |2 N' r
  54.                         http://www.freertos.org/RTOS-Cortex-M3-M4.html
    $ e, S: w( c; G. {* O% H" P
  55.                         http://www.freertos.org/FAQHelp.html */
    9 T* D/ b4 T8 y% E2 i( w
  56.                         configASSERT( ucCurrentPriority >= ucMaxSysCallPriority ); //// 程序卡住位置
    8 K2 c1 Z* h/ x: i9 m
  57.                         //// ucMaxSysCallPriority值为0x50
    7 M" h+ `% Q- C# P& e$ |( w; T
  58.                 }* ]2 W# b/ ]6 W& c

  59. 4 Y& |- z: Z3 J+ D+ U( W$ F, X
  60.                 /* Priority grouping:  The interrupt controller (NVIC) allows the bits
    ; _( f, _+ m1 P+ T5 s  w6 X- E' c
  61.                 that define each interrupt's priority to be split between bits that
    1 R( I2 b6 N* J! ^
  62.                 define the interrupt's pre-emption priority bits and bits that define
    1 B9 e1 M) U6 T4 x5 B
  63.                 the interrupt's sub-priority.  For simplicity all bits must be defined
    1 v2 n) H/ G9 d
  64.                 to be pre-emption priority bits.  The following assertion will fail if
    8 v: i& S5 m0 a& B- S- N. g$ K
  65.                 this is not the case (if some bits represent a sub-priority).
    # p3 O- O& C' b% n' G! B2 p

  66. - d4 q# k% r+ s
  67.                 If the application only uses CMSIS libraries for interrupt/ y. G) D3 V, |" o! i$ P; z
  68.                 configuration then the correct setting can be achieved on all Cortex-M. H, U3 C, ^$ i1 m7 u' p, N2 }  I
  69.                 devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the5 X) S/ }  W+ |
  70.                 scheduler.  Note however that some vendor specific peripheral libraries; @# _. G) k0 k( V
  71.                 assume a non-zero priority group setting, in which cases using a value
    3 j6 i6 L4 D4 n
  72.                 of zero will result in unpredicable behaviour. */
    7 k1 m* E2 b  E- H7 C; p. v
  73.                 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
    ) ^7 O( p" {5 }' i
  74.         }# W/ ]# \% ]7 P$ A# f' v+ h0 ^

  75. ; i* c) Q" Z$ J$ r1 l- \- [
  76. #endif /* configASSERT_DEFINED */
复制代码
% H! e5 x3 \+ z2 c5 B, J
       相关宏定义如下:/ H5 s# w7 g  O7 L, s
  1. #define portFIRST_USER_INTERRUPT_NUMBER                ( 16 )
复制代码
  1. & O1 U* F/ M  X) c
  2. /* The lowest interrupt priority that can be used in a call to a "set priority"
    # c2 H5 l* h) z1 f) k- O
  3. function. */
      M. B. ]# a  Z2 F8 c- j/ g. e3 w
  4. #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY   158 _) n* `: O  f# X" U0 F
  5. 3 |; n" l# Z: i: @& n7 }
  6. /* The highest interrupt priority that can be used by any interrupt service; m0 }9 a. U% L: i& i
  7. routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL4 I# X, g1 [7 D' Z& v9 }5 u
  8. INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER) V0 J& G$ r5 u+ A
  9. PRIORITY THAN THIS! (higher priorities are lower numeric values. */# x4 r. L8 x9 }/ @
  10. #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5
    6 Q5 b4 y2 j' N3 }
  11. 4 L7 u2 i% t3 n! w# [) ~+ L
  12. /* Interrupt priorities used by the kernel port layer itself.  These are generic% Z+ W: s% I# N- ^. O
  13. to all Cortex-M ports, and do not rely on any particular library functions. */* Z; c# ~9 c" }, F
  14. #define configKERNEL_INTERRUPT_PRIORITY                 ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )5 A; v1 c; A' u! q! J
  15. /* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
    & L) A3 P7 k+ ]! |. a
  16. See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */3 a, x$ t" A1 k) M' L
  17. #define configMAX_SYSCALL_INTERRUPT_PRIORITY         ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
    ; D; y6 R9 h7 d* H( t$ a

  18. ! t1 `  Y' ?) x) K5 K, {' z' ^3 [
  19. /* Normal assert() semantics without relying on the provision of an assert.h3 P( z  \9 c5 u, I- P2 Y3 q
  20. header file. */
    $ q/ N4 t6 ~! p! y/ [
  21. /* USER CODE BEGIN 1 */   
    4 ~; s% P! Y' Q
  22. #define configASSERT( x ) if ((x) == 0) {taskDISABLE_INTERRUPTS(); for( ;; );} ' ~5 I# r  i" L$ M
  23. /* USER CODE END 1 */
复制代码
  1. #define configPRIO_BITS         4
复制代码
8 g# ^! E! |# D& n/ k
- j0 D. `, A% q! W- Q/ x5 i3 Y
简单总结如下:2 w) d0 u5 {8 ~/ w/ T8 M9 ?" L# T
       使用了FreeRtos操作系统后,外设中断优先级值不能设置小于configMAX_SYSCALL_INTERRUPT_PRIORITY,否则会导致了宏configASSERT的条件成立,进而卡住了。
9 T6 E" ^3 z$ V+ e- {: }2 E$ E5 a
4 O8 c* r9 B$ _; v9 [
- c  K: M- y: d+ ~1 y' k! ]# v5 c% q) H. |8 ~" O+ f
- S! m# c3 h" z9 h0 O% ]% ]

, [* p' ?8 Q0 Q- R7 ~% a% Q! G
赞 收藏 2 评论1 发布时间:2019-4-16 14:15

举报

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