|
有关STM32中HAL_Delay和中断一起使用的问题 昨天笔者使用STM32的外部中断EXTI(HAL库。笔者也是第一次用HAL库)配置一个简单的按键控制,因为需要使用HAL_DELAY进行一个短暂的延时,发现使用过程会造成卡死的现象。我们来分析一下原因: 首先,我在调试的过程中是可以进入主函数的。但是当我调试进入到中断回调函数这块,发现到HAL_Delay这里是无法往下进行的。所以问题就出在这里。
0 H; l& G1 y" A+ @4 @ 1-HAL_Delay我们先来看看HAL_Delay代码,先根据这里的代码进行分析 : - 1/**/ }8 K) z/ P& y- j
- 2 * @brief This function provides minimum delay (in milliseconds) based1 i. @2 o* I! R0 D5 D" d; b' d
- 3 * on variable incremented.
* d6 L" o3 {: E. c) v& W0 ^# t3 M - 4 * @note In the default implementation , SysTick timer is the source of time base.. U6 U7 D0 b- q$ N
- 5 * It is used to generate interrupts at regular time intervals where uwTick
- n9 y8 L. ~ x% W - 6 * is incremented.
3 ` D: T& j8 Z3 v8 ? - 7 * @note This function is declared as __weak to be overwritten in case of other- A# u Z; e5 B5 b0 C& i4 W* B V
- 8 * implementations in user file.1 K% h2 M5 S: k8 H& H2 M
- 9 * @param Delay specifies the delay time length, in milliseconds.! P; ]' Y+ C; [/ |/ J9 r
- 10 * @retval None
9 h1 z7 h, l* ?! g( h& i: q# a8 v - 11 */
) Z' c. U7 s+ S - 12__weak void HAL_Delay(uint32_t Delay)
% e; e$ b& f# p& E/ d$ u - 13{4 G1 s* e ^" q
- 14 uint32_t tickstart = HAL_GetTick();% g4 v. z, ~; w6 g& E! Y
- 15 uint32_t wait = Delay;
) H9 O. U! r; \+ i& n - 16
2 }1 g* ?# Y0 o6 l% D - 17 /* Add a freq to guarantee minimum wait */$ r0 f$ m! u& V i- k; ]2 b
- 18 if (wait < HAL_MAX_DELAY)- n9 w& [* u( c
- 19 {; ^2 G9 p1 W8 J# h( B% T+ _
- 20 wait += (uint32_t)(uwTickFreq);6 p/ U- m9 v- h0 ^2 N4 Y
- 21 }; s/ F, Y$ B9 G( R. d+ p# p
- 22 L& j& x8 }( t4 f- p: Y7 c
- 23 while ((HAL_GetTick() - tickstart) < wait)! d/ X3 r) D/ [, Y* \
- 24 {! I" K# _' j: J& S/ c4 c
- 25 }5 R0 Q5 w V& J( A- A- E* K' n3 |
- 26}
复制代码 ( t0 N" J3 B# W7 [" n: R
上面的注释和代码说HAL_Delay计时器的来源是SysTick定时器,并且在固定的时间内产生中断。对于所有的32位单片机来说,有中断肯定是有优先级的。所以这里有基本上有两个原因:一是优先级的问题,我设置的优先级高于HAL_Delay的优先级造成一直在HAL_Delay中卡死,还有一种就是main函数进不去也是一种原因这种原因就是另外一说了。但是我的是可以进入main函数的,所以,只能是中断优先级的问题。所以我查看了systick优先级。
Y; U. K3 t# |7 x& L3 t
' a, \6 ^( t" ]" V* E+ d- Q
7 P3 g0 ^ \% Q7 R' R! E. g9 O5 U$ x5 ~- [5 d4 o2 \
- 1/* Tip: To avoid modifying this file each time you need to use different HSE,, ?& G& H$ C( S, W+ o# E0 o
- 2 === you can define the HSE value in your toolchain compiler preprocessor. */
8 G9 S! `9 N$ B0 ?5 i - 3
: m6 T$ C* A- {# }) J' x3 m - 4/* ########################### System Configuration ######################### */; v. L) E$ m3 s" C
- 5/**- m6 W) u) d9 \9 T- G
- 6 * @brief This is the HAL system configuration section
, s/ v. V! s4 c! e9 r, S3 O- k0 G* w9 L - 7 */
5 _) _' f9 R- u1 C& k$ } - 8#define VDD_VALUE 3300U /*!< Value of VDD in mv */* l9 R: V9 r- N+ [0 j9 R5 v
- 9#define TICK_INT_PRIORITY 0x0FU /*!< tick interrupt priority */
7 F7 J5 I/ Z' ?: E! C - 10#define USE_RTOS 0U: L1 m: g9 m( `/ g! E+ e" F& V
- 11#define PREFETCH_ENABLE 1U
复制代码
7 t( ^7 \4 O" D% X6 Q2 u
, g7 R: Q5 z9 _7 m+ X1 P8 a5 h( ?6 ~从这句#define TICK_INT_PRIORITY 0x0FU /*!< tick interrupt priority */看到,它设置的优先级是最低的。所以是优先级的问题。既然问题找到了现在开始怎样去解决这个问题。 . V3 x7 ~( V& `/ d6 D
2-问题解决方案1-修改优先级我们可以在代码中或者STM32CubeMX中重新设置systick的优先级。 ' g+ N7 q! q! N- w
7 c( D: v9 X Z3 G6 U( V4 k$ E5 Q/ k" [
6 j: k( Y. p% w" U6 d: D, S
v% v0 B' B7 E; e
" [# l- k$ M# f, B5 S& M0 \5 ~2-自己重写延时函数根据____weak void HAL_Delay(uint32_t Delay)这个我们是可以自己重写演示函数的。__weak 的意思就是自己可以实现定义一个同名的函数。 当然如果不想写网上找一个就行。 但是我个人是建议自己重写一个延时函数,这样容易避免后续由HAL_delay引发的其他问题。 7 N5 i/ T( u/ p8 G5 [4 [
转载自: 南山府嵌入式 如有侵权请联系删除 0 U" ?9 g v3 Z& s
+ q. j% K4 Q- f3 f
9 q) ]$ p i! N0 Z9 P3 E7 W
|