两天的调试与查阅资料终于明白了HAL库中的低功耗模式的切换和退出。主要还是自己作死选择了STM32G071,本文内容基于NUCLEO-G071RB,CUBEMX版本5.2.0,STM32Cube FW_G0 V1.2.0,MDK 5.27.1.0。 |! c) O- n v6 n. X
' o/ M# a, j, E* @3 K首先配置时钟、UART、RTC和中断。
8 R; t& J- o/ W9 ^, }* u; B, V, C7 g! B2 h k( U
) l; i6 R5 v" {2 \0 e- E6 U! Q0 I
: M' K- T" m" F0 }( f+ B2 c1 dRCC选择外部晶振(shutdown 模式只能使用外部晶振)其他默认 X2 Y- _, I& c4 M* g$ M: K% D8 _
7 _% R3 L+ f; R: U1 ~! t% `. h9 u7 y
; e: u& B( X1 v' D3 Z
SYS选择调试接口,和wakeup_pin2,STM32G0目前新线还是很多资源的
# }# H# z; O% I9 E4 O) R7 T. b: A. K, G; s
1 s# a- v1 g+ W$ X, R6 Q' R6 Q2 n" V/ p" Q1 ]; P$ _
设置RTC,填写相关初始化信息、开启ALARMA时间设定为开机后30秒,注意RTC_AlarmMask=RTC_AlarmMask_None;则为精确匹配,即闹钟不仅要求时分秒匹配还要匹配日期和星期,都匹配后触发闹钟中断)
3 f7 {) m! f; n7 O7 a7 x C9 q# u9 T1 r
; s# B) s8 C9 L5 h- `
1 D9 T) l% { U. Y$ x设置串口+ Z6 b! _% q0 j& F# z$ ~8 b( s7 _
J% E5 Y& ]% n! U& s0 v
( _/ J- l$ \0 u/ h0 z- y+ m3 B' t! p s
打开中断入口
9 Q* u s! @ i/ {6 I4 N, ]( C( H" j2 Y4 r o7 a/ n
; T |7 u T, o* y0 M
! z3 `. E m0 k' H' V
* R" M( v, \) q7 Y6 _. Y( E2 ]3 R2 b# d: G" z& j; F2 m
完成设置后点击生成代码创建工程
9 f, k$ A o! P* X5 H' B9 `; o$ o- d- D# _' \0 n2 \
1.首先喜闻乐见的printf函数/ f8 m b ]( ~
- /* USER CODE BEGIN Includes */6 S, F0 a q" a* R( y& F+ q. ~
- #include "stdio.h"
4 B+ i$ L. z' @" M/ C# `5 W1 @5 s$ ^' @2 o - /* USER CODE END Includes */
# W# P8 K8 q( a& V8 E7 v+ L3 Z - * v6 D6 ~: X5 X, B$ B7 F- d
- \) @1 Q0 B& W: l# U: I' o- / H2 J0 M3 G9 z8 _" P ?: d2 @% Y
- #ifdef __GNUC__% X2 z; \: ]% M% T
- /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
: S; [$ S0 I8 L - set to 'Yes') calls __io_putchar() */
% x- x3 V4 x5 s - #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
, ?7 P6 c+ {; A# P5 m! E. K - #else
A1 C+ D \) Z - #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)7 i6 S2 N+ z, v( c5 U( c0 r& Y
- #endif /* __GNUC__ */
! j* }8 z+ i) } - PUTCHAR_PROTOTYPE
/ t/ P" I) E9 H$ B. ~ - {. l- ~1 i7 |$ n ]8 e
- /* Place your implementation of fputc here */
1 J4 S: b7 a3 n5 d y2 Z. r5 @. H - /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission *// I/ }7 p8 r. `6 I0 }% N3 l- C0 z2 z
8 o+ |# o$ b4 }3 S5 z5 a" A- HAL_UART_Transmit(&hlpuart1, (uint8_t *)&ch, 1, 0xFFFF); u3 {+ a1 T k9 ]7 p
- ; I4 v2 `1 I+ `$ m) f7 v+ P% X
- return ch;
; J7 [4 U, Y! [3 c1 x - }
复制代码
2 A& @9 G% B/ Y( ~2.RTC时间获取函数) w# k. o8 [! J& D5 O. l
0 M1 F3 G$ i1 P! K* ^$ |: D5 {
- /* USER CODE BEGIN PTD */& n4 l, ~8 v _
- RTC_DateTypeDef sdatestructureget;
7 U* ?& t9 M( Y. } h# C: J# r - RTC_TimeTypeDef stimestructureget;8 e* s$ H ]! |0 L* s
- uint8_t aShowTime[16] = "hh:ms:ss";
6 C- G/ Y% O8 r1 N' \1 f - /* USER CODE END PTD */
7 J! x8 V1 [3 [/ h" P9 ]6 s, l - , g' R1 O, ?4 M% e
- /* USER CODE BEGIN PD */' s0 L$ b& j* ?! z, i1 c
- static void RTC_TimeShow(uint8_t *showtime);! Z9 k% N! [; ]
- /* USER CODE END PD */
) k4 R0 @1 w; Z+ `/ `& u+ s* B' [
" ^! m2 o% I9 L& g5 Z) `0 ]2 H0 w- % v. z& Z8 |' u7 v
- static void RTC_TimeShow(uint8_t *showtime)2 K7 z: b, Y% j+ _! d# x5 ~* M, D
- {
" \8 ?( M: Z7 k; C' D* S/ G# p
! A* o$ S9 r% `2 B5 E6 h3 a
/ j. @) H2 v" C3 K+ Y3 B `2 V- /* Get the RTC current Time */
' v. A% Y2 E- G2 C - HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
0 O, m8 h' t& x$ h - /* Get the RTC current Date */8 a" O3 M5 l: |6 F4 p& m
- HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
+ m' H a3 f/ \2 b( {# u8 z- O6 F, c - /* Display time Format : hh:mm:ss */$ g U, P# Z/ |+ ~. P' T
- sprintf((char *)showtime, "%02d:%02d:%02d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);3 m- q# K* c. J, D( e
- }
复制代码 # [1 ]; Y' L; G X
3.standby进入函数
/ t7 A1 R9 {/ q9 b. [* ?4 o7 Z- /* USER CODE BEGIN PD */; E' T& Z+ P9 v) u- o8 k7 o
- void Sys_Enter_Standby(void);
- L) T3 K% j4 \3 E/ w4 Q( T3 w - /* USER CODE END PD */
" v6 K! K( W0 X; a5 x! e
% y" Y0 F6 t1 d+ j: W% u3 Q. ?- void Sys_Enter_Standby(void)
! J; c3 d2 N: t+ T - {
! T, d, t: }7 o" V( U* r - /* The Following Wakeup sequence is highly recommended prior to Standby mode entry) O c8 b2 Q8 X$ \7 z! N
- - Enable wakeup
- f* B! ?7 U; k/ `, } @ g, Y - - Clear wake up pin flag depending in edge detection & pin level.
) o& O% ?4 k0 g. K2 ^9 l2 `, P, f- } - - Enter the Standby mode.
! z) p/ L3 i5 B8 _) G - */$ J* G: v% s5 H- i; s8 @* o* F
# m" H) P" ?+ _! s* x% o- /* Enable WakeUp Pin PWR_WAKEUP_PIN2 connected to PC.13 */0 \# @" |0 V8 y9 T8 [5 v
- HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN2_LOW);//上升沿触发% S5 u" P, _6 b( j: v+ J
- HAL_PWREx_EnableInternalWakeUpLine();
* X+ u4 I1 E$ m, Q& E) l- s( H - /* Clear all related wakeup flags*/0 n. l1 G/ g8 _+ C8 U' o1 d& v' X' P5 _
- __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF2);$ V& T- q. [$ N& ^- g- y, B
2 W3 y. g ^4 ?, O( r) \- /* Enter the Standby mode */6 K# B. L: E o5 ]
- //HAL_PWR_EnterSTANDBYMode();
2 F2 e& t' _9 E( w( F - printf("enter Shutdown Mode!");. d+ D4 o1 ]) o/ S1 A
- HAL_PWREx_EnterSHUTDOWNMode();
8 ?( c; L+ x' y" m - 8 _8 T! K; S3 N! C7 {5 N s# Z6 Z6 f
- /* This code will never be reached! */- R& L8 x8 u: w5 K/ y; c: ~9 E
- }
复制代码 9 J I; T0 F6 Q' @- J f- Y. a# Q* y
4.ALarmA中断回调函数 NVIC_SystemReset()用于standby触发AlarmA时间后与wake_pin触发系统自复位不同进入rtc中断后无法复位问题(我解决不了就软复位)9 U/ ~) U" E3 g9 r! i! b
- void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)" _! Q# Y+ c* U9 ~$ g' [& A* ?4 j9 Q
- {
" I! u) l6 N$ b+ O9 x - /* Prevent unused argument(s) compilation warning */
& G+ R% T% L3 w; b# P$ s: p - UNUSED(hrtc);
4 O/ b2 k& d5 H6 t
/ l! d" {0 a2 r u6 h: c- /* NOTE : This function should not be modified, when the callback is needed,
C( x/ A: M; h8 r5 c& W3 \. G4 s - the HAL_RTC_AlarmAEventCallback could be implemented in the user file
_; z, G! [6 ?- c4 w2 i' u) _ - */
1 e# V" w/ G: @. K
% ?" a r' Q, m9 P' m! [ H$ F- flag =1;
0 \) _% e0 m \- [. H! d( g+ {2 D( d - // NVIC_SystemReset();
# N! [. O# Q- l - y& S& p. ~7 _# ` m+ L
- }
复制代码
. m% M4 L2 g, U+ X+ l4 q) D 5.在main函数中5秒的时候进入低功耗模式
! ]# @/ c3 L# J& U- /* USER CODE BEGIN 2 */
" ~# c+ w: _( d+ n+ B - uint8_t sec=0;3 _2 X5 X$ ~' W1 \/ X4 h
- if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)8 ^' z- H* D1 {4 R+ L! K" M
- {+ [+ O2 V6 }$ H+ {% [% L& D1 h! i
- /* Clear Standby flag */3 l3 c! j1 r6 M% Z3 f" [
- __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);" {7 j, m- ^, h5 ?/ q: N9 N. v: `
- printf("wake up from standby!");
1 u. K, _6 Q6 U; r! D - /* Check and Clear the Wakeup flag */1 g7 X$ O) l/ }8 V
- if (__HAL_PWR_GET_FLAG(PWR_FLAG_WUF2) != RESET)
: H# Y( f$ J2 B. i) ~ ]! R; \ - {
4 ^& @+ Z3 U8 @3 m1 A1 A - __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF2);) Z! ]" g: f8 m
- }
^3 A( C) y& q& L; k - 3 W6 H4 l2 `2 X/ {5 c5 _
- /* Wait that user release the User push-button */4 S2 K% o" \2 h6 Q
- while(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_SET){}
0 l4 }) C1 w t& K
. P, J/ _5 S0 n2 G$ L1 ]0 s- }
/ |: u* p5 G! B1 t
5 e$ x- T; p, _% q- o% @- - _# ~" A! r9 R3 l1 S' Z& B5 Z3 F
- /* USER CODE END 2 */, L/ _, b! Z, _
- 2 H! C6 P1 U' b3 K& t( m& E
- /* Infinite loop */! s0 v" A0 G; g5 ?" Y, D; L
- /* USER CODE BEGIN WHILE */9 K6 @: T2 T+ q2 ^1 M8 k
- while (1)9 t Q" O9 W8 j* B9 N
- {; w C3 c7 c- ?4 a4 S7 [6 j" ^
- /* USER CODE END WHILE */
. `0 ?' \! ^ |0 j
9 m q. m5 ?2 P9 h- /* USER CODE BEGIN 3 */8 o8 L9 k6 f2 d$ a( C
- RTC_TimeShow(aShowTime);
1 Z. K6 L1 [2 @( e0 n) S) E - if(sec == 0)flag=0;
. r+ X& R/ U# E, r
$ _: |0 ]$ i( O- if(sec == 5)Sys_Enter_Standby();
; I3 W" d4 y$ B/ Y3 B8 \ - if(sec!=stimestructureget.Seconds)
5 U9 u7 D% |: o4 F - {
& B; x6 y! s! y* y s. ^2 e - sec=stimestructureget.Seconds;
# E) h ]! R; J: v: E# | - printf("%s",(char *)aShowTime);, |5 o6 \! i# b/ m8 G6 [ H
- if(flag) HAL_GPIO_TogglePin(LD4_GPIO_Port,LD4_Pin);5 }! q6 v* b1 @$ F8 h
- }( R' n. I9 c3 A; S+ U3 `/ q
- }4 @9 L N' ?9 t; b' p2 t. m3 l
- /* USER CODE END 3 */
1 U, O) L1 z% H$ P: h& |* `/ { - }
复制代码 * B& y; M1 A1 g( x" p; ]
至此STM32G071关于低功耗模式的进入与退出已经可以实际使用,大家可以参考,实际使用稍作调整。/ l, L2 L9 n) s4 j1 \) ^6 Y
# b2 w* \0 I0 i9 l1 C
附加 避免上电自动配置RTC时钟
1 @# s6 \, Z6 E4 x( }; F- /* USER CODE BEGIN Check_RTC_BKUP */* ~9 ?! d( L' A; e" N# ^
- if (__HAL_RTC_GET_FLAG(&hrtc, RTC_FLAG_INITS) == 0x00u){}
8 y! b& a g1 E+ `4 H2 V9 i9 [ - else{return;}
. L4 \; f9 J$ o/ Q7 E - /* USER CODE END Check_RTC_BKUP */
复制代码
& O' x0 Y7 g1 ?% Q- @9 `5 P( b以上代码请添加于MX_RTC_Init(void)中 \ ]6 L1 r3 k: }
# c# d1 `% N& J
. w" ?1 K' I3 q+ ?1 Z! Z5 ~% \* @& c* [# C
项目文件
5 k5 u1 \3 n' h0 Y! V) c
0 Y9 Q, f7 R, E4 O) Z+ W
8 z/ g5 [2 L3 X, J8 Q8 c7 G- ~2 {) y* b7 e
3 R# V |8 M1 a$ H5 ^, H
|