本帖最后由 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如下:- HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);//// 中断配置,优先级大小不能乱配置
2 f. S% ]% r `" b: _+ m - HAL_NVIC_EnableIRQ(USART1_IRQn);
- k+ J/ O8 K4 T6 i. Z - 3 T ~! |" `; n( a* i
- void USART1_IRQHandler(void)% B/ A6 P7 y( X! Q
- { G) k1 Z. E* B. q3 R/ ~' ~6 r
- uint32_t flag=0;
: J, p0 ]3 W, Z6 y - BaseType_t pHigherPriorityTaskWoken = pdFALSE;
D6 k; B1 A) q' b1 u" a$ I - # B/ C! Z* t% p" H+ C
- flag = __HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE); 5 H0 W+ _; {/ n
- if((flag != RESET))# W6 K$ g4 v! O, [, |
- {' M* a# ?+ ?, g B* L9 ]0 L
- __HAL_UART_CLEAR_IDLEFLAG(&huart1);# v# n8 m- L. X# A! v( w5 F
- huart1.Instance->SR;
$ Z; T4 s9 x1 t - huart1.Instance->DR;! j/ t# M% Z- n! D
8 p @! @# K+ o& m9 W) Z- B/ c$ P% S- HAL_UART_DMAStop(&huart1);* }5 g! A' S. J1 D
- xQueueSendFromISR(UrtK125RcvQueue, DrvUsartRcvBuf, &pHigherPriorityTaskWoken);. T' E7 U/ Q6 x4 Q
- portYIELD_FROM_ISR(pHigherPriorityTaskWoken);7 R, m3 e) Y( E& i/ A' q, B
- }
; _+ B6 v d' r5 { - }
复制代码
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- #if( configASSERT_DEFINED == 1 )/ v9 s1 E, l2 }, p
0 X% f r+ v9 o8 X% w3 U* s: k- void vPortValidateInterruptPriority( void )9 e5 S, [) x6 D0 C* D& P
- {% D( G- ]# \, y4 ~4 W( F- w# }
- uint32_t ulCurrentInterrupt;
8 T- G) v1 s/ t7 e7 P5 j2 F - uint8_t ucCurrentPriority;1 }5 T# i& F5 ?- h& i ^3 y
- , ^: b! y$ m @2 {
- /* Obtain the number of the currently executing interrupt. */) a( ?# A6 U% O
- __asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) );3 v, ^& {2 i0 a
- //// ulCurrentInterrupt 调试显示为53, Z# t' K6 R4 a' n% i M% b8 w
5 W6 S4 u; w* u. q4 k- /* Is the interrupt number a user defined interrupt? */
; w8 c: B E! S/ [1 P! x1 s - if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
4 Q% \4 ?3 i9 p% e/ S - {* u$ T+ v5 p8 E7 S% Z; X$ v: q
- /* Look up the interrupt's priority. */! n2 T. _6 t$ A, o3 o1 G% j5 L
- ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];9 T/ ^8 { c8 K
- //// 当串口优先级设置小于5时,ucCurrentPriority的值小于0x50,导致下面的宏断0 E2 n9 \2 U0 a# N/ M6 Y8 F
- //// 言configASSERT条件值"==0",进而卡住, F0 t: _$ D, F+ I) I& e+ b
- //// 当串口优先级设置大于等于5时,ucCurrentPriority的值大于等于0x50,下面的( ^( T# l, N R/ m; O3 \& ^/ [
- //// 宏configASSERT条件值"==1",所以不会卡住" S8 R$ I8 |8 }. \ h
- / y% X- X6 r6 j& G% N1 Y1 P; |) G# B
- /* The following assertion will fail if a service routine (ISR) for6 M/ Q2 s Z) ~, p' c" m
- an interrupt that has been assigned a priority above! ?# }* n U0 k$ {' [8 S0 B, h
- configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
7 u |; d4 Y& C! U - function. ISR safe FreeRTOS API functions must *only* be called- q& M6 I: S U: z/ w
- from interrupts that have been assigned a priority at or below8 A3 D/ O M$ A2 h! [
- configMAX_SYSCALL_INTERRUPT_PRIORITY./ M/ S; w8 z& Z
- //// 如果一个调用FreeRtos安全ISR API接口的中断ISR优先级分配大于1 d0 I: x3 E; B0 E/ Y& Q5 ?$ H
- //// configMAX_SYSCALL_INTERRUPT_PRIORITY(即中断优先级值小于
# I$ Y/ i1 F/ c0 i' j2 l - //// configMAX_SYSCALL_INTERRUPT_PRIORITY,因为FreeRtos中值越小优先级越高),
5 @1 T# g# }# i8 S, H - //// 下面的断言就会失败。FreeRtos安全ISR API接口必须只能在优先级分配大于/ D5 _8 l4 y; N6 Q2 C6 T
- //// configMAX_SYSCALL_INTERRUPT_PRIORITY的中断ISR中调用
) Z- j) x4 e- B* D. R. R - % V1 ^6 Q* t( u8 w7 r1 J( {
- Numerically low interrupt priority numbers represent logically high* v: W9 j5 x. P F6 j
- interrupt priorities, therefore the priority of the interrupt must
6 v' P9 _, i. | t7 A - be set to a value equal to or numerically *higher* than0 W8 a; @+ _+ I) b6 K+ u" g! ~
- configMAX_SYSCALL_INTERRUPT_PRIORITY.
; W: ^. [% R& u - //// 数值小的中断优先级逻辑上具有高的中断有限权,因此设置时,中断的优先级
( [2 k P( O1 U) q - //// 值必须必configMAX_SYSCALL_INTERRUPT_PRIORITY大: k% @2 t# ~7 _, H3 r
- * G6 L" s9 t/ ^& t4 C
- Interrupts that use the FreeRTOS API must not be left at their9 Q! d0 A0 O9 H# d& M4 {. [( s
- default priority of zero as that is the highest possible priority,! `) `* @5 ?# W
- which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,; B2 `4 i- v# Q
- and therefore also guaranteed to be invalid.
$ p1 i% b+ g. \! j - //// 使用FreeRTOS API的中断的优先级禁止使用其0的认值,因为此时中断优先级最3 U- Q3 c: U: g; D& G
- //// 高,0值的优先级必然大于configMAX_SYSCALL_INTERRUPT_PRIORITY,因此也必# s' T8 @( ?3 ~4 _1 I7 h
- //// 然无效' |; f% K$ e) d
- + u4 a) s) S* P" Z
- FreeRTOS maintains separate thread and ISR API functions to ensure- _+ F* _ ^+ M! y5 y6 x V
- interrupt entry is as fast and simple as possible.- B5 l! s- Y! {
- //// FreeRTOS为了确保中断入口尽可能的快和简单,因此分离了线程ISR API接口
, @1 H3 m, T$ q# s - " ~( h8 Y" m- B( L: F
- The following links provide detailed information:. M' k- Q3 |2 N' r
- http://www.freertos.org/RTOS-Cortex-M3-M4.html
$ e, S: w( c; G. {* O% H" P - http://www.freertos.org/FAQHelp.html */
9 T* D/ b4 T8 y% E2 i( w - configASSERT( ucCurrentPriority >= ucMaxSysCallPriority ); //// 程序卡住位置
8 K2 c1 Z* h/ x: i9 m - //// ucMaxSysCallPriority值为0x50
7 M" h+ `% Q- C# P& e$ |( w; T - }* ]2 W# b/ ]6 W& c
4 Y& |- z: Z3 J+ D+ U( W$ F, X- /* Priority grouping: The interrupt controller (NVIC) allows the bits
; _( f, _+ m1 P+ T5 s w6 X- E' c - that define each interrupt's priority to be split between bits that
1 R( I2 b6 N* J! ^ - define the interrupt's pre-emption priority bits and bits that define
1 B9 e1 M) U6 T4 x5 B - the interrupt's sub-priority. For simplicity all bits must be defined
1 v2 n) H/ G9 d - to be pre-emption priority bits. The following assertion will fail if
8 v: i& S5 m0 a& B- S- N. g$ K - this is not the case (if some bits represent a sub-priority).
# p3 O- O& C' b% n' G! B2 p
- d4 q# k% r+ s- If the application only uses CMSIS libraries for interrupt/ y. G) D3 V, |" o! i$ P; z
- configuration then the correct setting can be achieved on all Cortex-M. H, U3 C, ^$ i1 m7 u' p, N2 } I
- devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the5 X) S/ } W+ |
- scheduler. Note however that some vendor specific peripheral libraries; @# _. G) k0 k( V
- assume a non-zero priority group setting, in which cases using a value
3 j6 i6 L4 D4 n - of zero will result in unpredicable behaviour. */
7 k1 m* E2 b E- H7 C; p. v - configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
) ^7 O( p" {5 }' i - }# W/ ]# \% ]7 P$ A# f' v+ h0 ^
; i* c) Q" Z$ J$ r1 l- \- [- #endif /* configASSERT_DEFINED */
复制代码 % H! e5 x3 \+ z2 c5 B, J
相关宏定义如下:/ H5 s# w7 g O7 L, s
- #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
复制代码- & O1 U* F/ M X) c
- /* The lowest interrupt priority that can be used in a call to a "set priority"
# c2 H5 l* h) z1 f) k- O - function. */
M. B. ]# a Z2 F8 c- j/ g. e3 w - #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 158 _) n* `: O f# X" U0 F
- 3 |; n" l# Z: i: @& n7 }
- /* The highest interrupt priority that can be used by any interrupt service; m0 }9 a. U% L: i& i
- routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL4 I# X, g1 [7 D' Z& v9 }5 u
- INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER) V0 J& G$ r5 u+ A
- PRIORITY THAN THIS! (higher priorities are lower numeric values. */# x4 r. L8 x9 }/ @
- #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5
6 Q5 b4 y2 j' N3 } - 4 L7 u2 i% t3 n! w# [) ~+ L
- /* Interrupt priorities used by the kernel port layer itself. These are generic% Z+ W: s% I# N- ^. O
- to all Cortex-M ports, and do not rely on any particular library functions. */* Z; c# ~9 c" }, F
- #define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )5 A; v1 c; A' u! q! J
- /* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
& L) A3 P7 k+ ]! |. a - See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */3 a, x$ t" A1 k) M' L
- #define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
; D; y6 R9 h7 d* H( t$ a
! t1 ` Y' ?) x) K5 K, {' z' ^3 [- /* Normal assert() semantics without relying on the provision of an assert.h3 P( z \9 c5 u, I- P2 Y3 q
- header file. */
$ q/ N4 t6 ~! p! y/ [ - /* USER CODE BEGIN 1 */
4 ~; s% P! Y' Q - #define configASSERT( x ) if ((x) == 0) {taskDISABLE_INTERRUPTS(); for( ;; );} ' ~5 I# r i" L$ M
- /* USER CODE END 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 |