问题:
v F6 ?- S7 y1 a该问题由某客户提出,发生在 STM32F401CEY6 401CEY6 401CEY6 器件上。据其工程师讲述:在程序中,在进入 STOP 模式之前他已经将 STM32F401 的 I/O 口做完处理了,但是电流仍然比数据手册写的数值还要大很多,不知道是在哪里消耗了电流。希望能帮他验证一下。
+ w& t$ Z u( q( k+ O7 _9 N
& j3 n' a* q5 Q0 n+ m b8 y* o调研:% K$ U3 U( {- i; U$ X0 V- y+ Y
我们先来看一下 STM32F401 的数据手册中,对其在 STOP 模式下工作的电流的参数:
* s" x$ G Q: c# E' q! S# ]' `0 J6 {8 f# l/ @" c/ y
4 z3 E$ o8 R4 m* B( x6 p8 N
6 R. b& {$ L3 J按客户的要求,我们主要是验证在 STOP 模式下,调压器在低功耗模式下的电流,也就是表格中典型电流为 42uA 的那个值。, }6 s9 y# N& M3 S2 v5 M- d3 r
我们使用 STM32F401 的 Discovery 板进行验证,因为在 Discovery 板上只需要把 Idd 的那个 JP2 的跳线帽拿掉接上电流表就可以测试电流了,当然,我们必须要检查一下,这个电流供进来给 MCU 作为VDD 时,还可能连接什么电路会导致电流消耗的,检查了一遍,主要是要注意 BOOT0 以及 BOOT1脚。
& N) T: x& q2 k9 t1 P8 Z i" N& h+ V4 U& x
( {1 c" j5 `0 e/ q* i8 G/ o7 p
: O9 Y- n$ j) Q/ Y; u- M由于运行用户代码,所以 BOOT0 和 BOOT1 都将接到低电平上,所以电路上要确保 SB19 和 SB20 短路,但是我们可以看到,BOOT1 上面从 VDD 到 R30 到 R31 再到 GND,将会有一个电流产生,所以必须要把 R30 取下来。BOOT0 由于本身 R29 就没有接,所以倒是没有关系。
: {5 ^ y$ y( L7 c+ K( }- N好,那现在开始验证电流,我们使用 32F401CDISCOVERY 的固件库中的 PWR_STOP 的例程来验证。因为不使用 RTC,所以我们将程序中的 RTC 部分删掉:将 stm32f4xx_it.c 中的 voidRTC_WKUP_IRQHandler(void)函数中的内容删除,再将 main.c 中的 static void RTC_Config(void)函数删除,在 int main(void)主函数中也清除掉相关的内容,主函数如下:
0 B$ z* b6 x4 j7 {3 n1 A: l3 e; h; N/ J- int main(void)
* @/ A7 b) `6 e* @" h - {
4 M4 Y$ d! o/ G6 B - /*!< At this stage the microcontroller clock setting is already configured,
( ~. c# v" s) A* r! _ - this is done through SystemInit() function which is called from startup
, w8 g% C, j5 b# e* R. i - file (startup_stm32f401xx.s) before to branch to application main.6 n# h9 j1 F; V3 b
- To reconfigure the default setting of SystemInit() function, refer to
' N! o+ x" [) R! [3 l) N* m - system_stm32f4xx.c file; S" p7 @% }# p8 z6 ?# M& j
- */
8 p* B8 Z1 e f# B
. U: Y9 E6 H5 ^- /* Configure the SysTick to generate an interrupt each 250 ms */
/ @0 K' W N2 P3 f$ C - SysTick_Configuration();% ~+ |/ ]0 ]) y" ?0 ^$ F* r" b
- /* Initialize LEDs and User Button mounted on STM32F401C-DISCO board */
0 j% W9 x' ^ t2 u& B - STM_EVAL_LEDInit(LED3);
7 B7 p" T1 e6 G6 X5 z7 i - STM_EVAL_LEDInit(LED6);
' a6 i& S+ E/ [: ]9 G! S - /* Key button (EXTI_Line0) will be used to wakeup ths system from STOP mode */% p3 J3 W$ E, n# U0 @7 @
- STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
" `8 V0 d6 ?8 X$ N; t0 f3 h5 E - while (1)$ Z+ M- [6 Q+ A/ K3 Q* n
- {
7 w. M7 ]) [* p5 v - /* Insert 5 second delay */5 h; ^* c/ Y4 Q/ x" n& o
- Delay(20);
7 n4 f l* P# b( r, |* C; {: C8 P - /* Turn OFF LED3 */1 k5 f. |0 {: S0 }% f* _
- STM_EVAL_LEDOff(LED3);
3 S; a- f3 t* M: C$ I - /* Enter Stop Mode */5 t+ Q. @% c% J7 A+ G( E
- PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
: Y' [* \( k) J2 i9 j* q1 k - /* Configures system clock after wake-up from STOP: enable HSE, PLL and select* c7 ^" B7 p9 ?7 s3 Z
- PLL as system clock source (HSE and PLL are disabled in STOP mode) */
, W8 E Z% p: W6 M0 ]+ n - SYSCLKConfig_STOP();8 ?1 ?5 T' E, p
- }& @/ X6 _* X4 }! L
- }
复制代码 2 ?- M3 b5 u2 e8 p+ g& V
编译,下载测试电流,进入 STOP 模式后电流为 2.23mA 左右。0 C; Q' u( Z! Z* k- g
大家应该已经发现,此例程在进入 STOP 模式之前,并没有对 I/O 口进行处理,导致很多配置为输入浮空的 I/O 口在受到外界干扰的时候,状态不定,消耗了大量的电流。在实际应用中,在进入 STOP模式前,可以根据实际情况,将 I/O 口设置成模拟输入,输入+上下拉电阻,或者输出固定电平。所以,在这里,我们将我们没用到的 I/O 通通设置为模拟输入,只保留 PA0 作为按键,产生外部中断来将 MCU 从 STOP 模式中唤醒。配置 I/O 口后,为了方便从 STOP 模式唤醒后还能直观地看到 LED灯,我们需要在 stm32f4xx_it.c 的 void EXTI0_IRQHandler(void)函数中加入 LED6 的初始化函数STM_EVAL_LEDInit(LED6),在 main()主函数的 SYSCLKConfig_STOP()之后加上STM_EVAL_LEDInit(LED3)。
& F2 e( t- g8 R% l' H) ^( [% evoid EXTI0_IRQHandler(void)如下:' z$ ^% Q5 c9 v6 x$ y6 |6 r; h1 @
- void EXTI0_IRQHandler(void)( u$ X S$ R+ `! {' D( E n# w
- {+ e+ R( r) w" {" ]. _. K
- if(EXTI_GetITStatus(USER_BUTTON_EXTI_LINE) != RESET)
5 g* o: Q+ I, J2 p% \ - {9 N4 b- B( s5 `) M
- /* Clear the USER Button EXTI line pending bit */
5 T! ]# O9 p9 L: t* w - EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE);* ]4 j( F. G9 R/ |" x$ a
- /* Toggle LED6 */
$ X/ z( r1 j L1 N/ N( O& h' ? - STM_EVAL_LEDInit(LED6);
3 W+ Y! O4 h+ q0 [7 w - STM_EVAL_LEDToggle(LED6);
6 H3 K; C7 f4 E/ A2 M - }) u i8 ~/ U% B U. o7 ~' X
- }
复制代码
+ [# \! f) c% H" |& K2 b$ W5 TInt main(void)中的主循环 while(1)中的内容如下:# i9 a! e- x) Z- w* G& q& i
- while (1)# p& N' ?1 B1 u; t Q, w/ u/ X# m
- {
/ x% f; V; r9 ? - /* Insert 5 second delay */5 Z u; ^: J$ l2 ~7 W) N
- Delay(20);
/ B3 i( ?+ s3 M& k$ K - /* Turn OFF LED3 */
/ P& u* U" p6 M - STM_EVAL_LEDOff(LED3);6 m! y4 N, `. G1 E# |
- : O6 }1 m0 \( p
- /* Configure all I/O ports */! e. z! l; L, S' t% C! S
- GPIO_InitTypeDef GPIO_InitStructure;
" [# Z: C* ]! c$ g' c - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
' [4 \) X6 `+ v" F - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);# i0 o# k9 s' H. o7 n
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
+ i: ~, |$ p9 A# T8 x) ]+ @ - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
6 I7 G3 d' n* B6 J" P+ `' [ - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);4 s! w- ?# `9 j
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
# L1 s! {/ }) W- j7 c2 S1 i5 U - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
$ C( x# q$ ^. R2 Z1 [- o/ t7 O) T - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
1 M' N9 `- |$ Z q" v - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
9 `/ x" { n" L. }4 o - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
$ h w3 m" |/ i! S! `9 ` - GPIO_Init(GPIOB, &GPIO_InitStructure);! K0 g* P4 e9 q0 g9 [" R
- GPIO_Init(GPIOC, &GPIO_InitStructure);
/ v6 _* b$ d5 N$ e( @) A( ~; w - GPIO_Init(GPIOD, &GPIO_InitStructure);
/ ~8 m' j+ f" i( Y0 S4 |) d - GPIO_Init(GPIOE, &GPIO_InitStructure);3 @: C* L. a5 K& L! {( V/ D" ~# V
- GPIO_InitStructure.GPIO_Pin = (~GPIO_Pin_0);2 C/ T$ l; A E! g) c
- GPIO_Init(GPIOA, &GPIO_InitStructure);
8 l0 B5 ~6 ?( L* }% x# `0 T# Y7 u* i; L - /* Enter Stop Mode */
( e- ^) m6 a) [8 L& l/ ~, _ - PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
% i+ Y5 j8 S1 W0 _ k+ r( L - /* Configures system clock after wake-up from STOP: enable HSE, PLL and select, h% H/ @; ~5 X: i5 h3 D! i
- PLL as system clock source (HSE and PLL are disabled in STOP mode) */8 c: C z1 A( v# ]: h- g
- SYSCLKConfig_STOP();6 l1 f0 Z" x$ l% k m8 }
- STM_EVAL_LEDInit(LED3);
. L4 |4 C0 l% m: s6 p; m - }
复制代码 1 m2 I# m3 o, R6 z: Z
编译,下载测试电流,进入 STOP 模式后电流为 155uA 左右。这不对啊,距离我们看到的 42uA 还有很大的差距。客户就是在这里不知道为什么了。
" w* h/ t/ ~) V. Z2 c, g8 H3 l我们来继续查找原因,客户跟 32F401CDISCOVERY 板一样,使用了外部 8MHz 晶振,所以当使用HSE 的时候,PH0/PH1 被作为 OSC_IN 和 OSC_OUT。问题来了,当进入 STOP 模式时,HSE RC振荡器被禁止,PH0/PH1 这个时候就不再是 OSC_IN 和 OSC_OUT 了,变成普通的 I/O 口,而且是处理浮空输入状态。所以我们还得需要对这两个引脚的 I/O 配置进行处理。再来修改 main()主函数中的while(1)主循环,加入对 PH0/PH1 的处理,程序如下:7 b( K: M, q: @, @$ c& S1 G
- while (1)
2 s8 u; H6 D% _4 w2 R+ `, ` - {3 X! |' M, W! w. E" x
- /* Insert 5 second delay */ e% a5 j2 M# l7 j e) z! a
- Delay(20);
- B( f" @# o5 }6 k4 r - /* Turn OFF LED3 */' K; A8 U, r/ \: i7 ^6 h
- STM_EVAL_LEDOff(LED3);) a. K% x7 O! c- w' T6 Q* k% ?2 R
% L7 ]+ a. j% P* t7 a% J+ T- /* Configure all I/O ports */
/ D. V l+ W" Q) B8 G - GPIO_InitTypeDef GPIO_InitStructure;
8 N7 Q4 y8 @% s' p" n- l - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
' K' B# W# Z9 ]! n0 n, r0 L- \ - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
, j% ?# Y' |1 B - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
! z" e0 O4 x Z& J; ~5 A - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
: _$ C" Y `5 |, Z5 j$ v - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);' y" l: m& }$ J7 W6 L+ r7 V
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);3 B& X' W! m( ^% J6 F5 G }
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;: N! |9 A8 G; \1 d2 R
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
y: i$ e) F1 R) b& o' {; m - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;( m" D* T0 z% h, K0 W& A; U- G
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
" x, R/ |& |" D! j3 E: F* N - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
6 P! J4 e. b, D* ?5 ~2 y - GPIO_Init(GPIOB, &GPIO_InitStructure);! M1 W; i* a( y
- GPIO_Init(GPIOC, &GPIO_InitStructure);
4 i- X9 I4 `7 G; u0 F - GPIO_Init(GPIOD, &GPIO_InitStructure);! h0 i9 |9 Q U6 d5 O! w+ j
- GPIO_Init(GPIOE, &GPIO_InitStructure);
' V8 |0 q& {9 |. `+ g0 Q - GPIO_Init(GPIOH, &GPIO_InitStructure);. B- \7 M" z$ H v- d" [0 L
- GPIO_InitStructure.GPIO_Pin = (~GPIO_Pin_0);4 I% _0 j6 a4 A+ ]6 |
- GPIO_Init(GPIOA, &GPIO_InitStructure);- w/ F; }0 S' z
- /* Enter Stop Mode */' x2 W9 b' q' {: K$ \
- PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
5 K% |1 Z) o+ H9 b$ D) A - /* Configures system clock after wake-up from STOP: enable HSE, PLL and select9 X4 K& d0 H# l+ X
- PLL as system clock source (HSE and PLL are disabled in STOP mode) */9 G$ d6 w V, N& J3 ^: V$ Q4 P
- SYSCLKConfig_STOP();2 o+ A V$ Q. K
- STM_EVAL_LEDInit(LED3);
2 d3 o l' ^# [1 _( {: `" y - }
复制代码
( m/ |0 f1 g7 F; c编译,下载测试电流,进入 STOP 模式后电流为 38.5uA。已经比我们从表格中看到 42uA 还小了,任务完成。当然,数据手册中 42uA 是在 VDD=3.3V 的情况下数值,而我们的 Discovery 板的 VDD 为3V。
* o4 S) M. N* ?/ q Q( F
$ p7 k; {3 ?3 y我们还可以再测一下让 Flash 进入掉电模式下的电流,修改 while(1)程序如下:
$ B* _3 X j, F% N* V; |) n- while (1)$ |1 e0 B" L, W" W3 m5 r) r/ h
- {
3 P/ S9 E$ b$ s8 C& x& I3 q( [; s - /* Insert 5 second delay */
' z8 I+ D0 e* P x4 A4 A - Delay(20);
4 f9 Z6 x5 B, g) t - /* Turn OFF LED3 */ ; @+ c: I+ I( u7 f/ R% o+ C' j
- STM_EVAL_LEDOff(LED3);
+ \& q0 A- M) _5 s. | B6 Y- {
, r; F1 J$ n( O- /* Configure all I/O ports */. I) S+ ]- @' K. s w
- GPIO_InitTypeDef GPIO_InitStructure;
% f! n7 ]: r" D4 g2 p - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
* E) } u4 [& { - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
# B: c! Y- w: d( i) N5 h' o - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);- g, G4 L2 ~0 A- b- m
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);+ _$ I( \3 N# Y
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);! @- o& X' e; G. u0 e4 k) n. r
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);& B) R/ x: D3 S
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;/ P/ z. _' D( w3 F* H3 @7 A2 V: E
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;% q+ g, ~/ B1 Q$ }7 [* ?. N
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
5 N: U; x+ H5 T( h, g8 W - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
3 G) N" |: n& H" `) k - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;- {" I2 ~' [: y, t F" h9 A
- GPIO_Init(GPIOB, &GPIO_InitStructure);
2 y, ~7 _1 s' F9 s4 @ - GPIO_Init(GPIOC, &GPIO_InitStructure);7 _% }# C% k4 t% B& C
- GPIO_Init(GPIOD, &GPIO_InitStructure);
9 |& p8 k/ C, L - GPIO_Init(GPIOE, &GPIO_InitStructure);
: V9 C. F5 U1 @% m$ m) w - GPIO_Init(GPIOH, &GPIO_InitStructure);
% I* B. {9 m6 a* Z" y6 X' @ - GPIO_InitStructure.GPIO_Pin = (~GPIO_Pin_0);) H& j; g* Y9 x0 o7 l5 A
- GPIO_Init(GPIOA, &GPIO_InitStructure);1 Y& y* N6 O' s8 |0 W
- /* Configure flash power down mode */( ~2 h4 C, l6 h
- PWR_FlashPowerDownCmd(ENABLE);
) d/ t% ^( ?- ~' ?& x - /* Enter Stop Mode */
* ^$ W) O. \4 ^5 ^+ s6 q" c - PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);- C4 b8 t d- S+ {4 j& A
- /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
( C% b0 u/ a# ~' b; {$ U0 X6 L* K9 V - PLL as system clock source (HSE and PLL are disabled in STOP mode) */
+ C4 M& j* d- g2 c t, |6 f' N7 G - SYSCLKConfig_STOP();
Y0 y Y1 I8 k7 Y4 Y. n - STM_EVAL_LEDInit(LED3);
9 B! U- n" h# Y6 ]: E+ ]# T) u" G9 P - }
复制代码 2 w# b7 i# e$ s6 u+ J) x
编译,下载测试电流,进入 STOP 模式后电流为 10.2uA,与数据手册中的典型值基本一致。
& c1 @1 y, A9 l, Z8 p: [6 ]7 ~ [) B
结论:
4 k$ M- ^3 c% R0 n: g客户在硬件上使用了 HSE,而在进入 STOP 模式前,只对其他 I/O 进行了处理,漏掉了 HSE 对应的两个I/O 口,导致电流过大。
# ]& X, [0 N" E+ L5 `# `7 g
. y4 P4 v* X& E2 g$ }处理:
H2 o% s+ m$ @2 d J2 b! B( v对 PH0/PH1 进行处理,进入 STOP 模式前配置成模拟输入。
* |$ K. F$ A$ n+ I7 v; F# d0 ?0 L8 N: a% g) G& ]0 g2 f' f
建议:7 \0 _5 _- p8 \& \3 ]
在进入 STOP 模式之前,一定要根据实际电路情况,细致地对各个 I/O 口进行处理。
6 _ }2 g" Q. V, u
7 k+ t% ]* r" {- s
$ ?! a8 Z1 r+ o" `1 c+ S0 P7 B, E: b3 a+ z. E; x( s' u$ w
7 ?# e8 B6 }( D& Z4 M1 }. Y
|