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

【经验分享】STM32 低功耗相关函数和类型

[复制链接]
STMCU小助手 发布时间:2022-3-30 11:00
01. 概述
/ ^4 y& n% C5 Z* ~5 u8 r9 r3 d4 E很多单片机都有低功耗模式,STM32F4 也不例外。在系统或电源复位以后,微控制器处于运行状态。运行状态下的 HCLK 为 CPU 提供时钟,内核执行程序代码。当 CPU 不需继续运行时,可以利用多个低功耗模式来节省功耗,例如等待某个外部事件时。用户需要根据最低电源消耗,最快速启动时间和可用的唤醒源等条件,选定一个最佳的低功耗模式。# b7 E. u  ^4 X7 O: `* n
# U0 d2 }: ~1 J
在stm32f4xx_pwr.c 和stm32f4xx_pwr.h 文件中。1 E  n* n7 Y, @/ Z
+ @) M1 K$ C5 W, ?
02. 相关类型
, R& l" o5 P. mPWR_PVD_detection_level
1 S5 ^, t4 C7 p6 u/ k* q. I) D6 [% n
% ~# |: e( u: U! f6 ^
  1. /** @defgroup PWR_PVD_detection_level - X6 R$ Z' z7 M
  2.   * @{3 {0 E- r. a' Y5 I+ K  d( R9 a
  3.   */ % u, ]1 ?7 E% m) X7 h6 h
  4. #define PWR_PVDLevel_0                  PWR_CR_PLS_LEV0% `- @4 i& {/ C% l/ f
  5. #define PWR_PVDLevel_1                  PWR_CR_PLS_LEV1
    + p& |" I6 @4 \9 G" c- N& a# t8 z
  6. #define PWR_PVDLevel_2                  PWR_CR_PLS_LEV23 F! E8 F0 O7 P& Q7 f) p  |: N
  7. #define PWR_PVDLevel_3                  PWR_CR_PLS_LEV37 K' Y3 c6 n( e: p* F% W, l3 I
  8. #define PWR_PVDLevel_4                  PWR_CR_PLS_LEV4" ?/ n  m+ i0 a
  9. #define PWR_PVDLevel_5                  PWR_CR_PLS_LEV5
    1 j1 Z) c0 n7 U7 H  f
  10. #define PWR_PVDLevel_6                  PWR_CR_PLS_LEV6
    ! B* J2 Z* l0 y: w* O
  11. #define PWR_PVDLevel_7                  PWR_CR_PLS_LEV7
    & w& ]/ P+ r% s1 E
  12. 0 S) N* u) D  S2 p0 P
  13. #define IS_PWR_PVD_LEVEL(LEVEL) (((LEVEL) == PWR_PVDLevel_0) || ((LEVEL) == PWR_PVDLevel_1)|| \
    / c) S% Y7 S) N: i" E( T
  14.                                  ((LEVEL) == PWR_PVDLevel_2) || ((LEVEL) == PWR_PVDLevel_3)|| \2 L7 j; x' A8 a" W3 d- Q) s
  15.                                  ((LEVEL) == PWR_PVDLevel_4) || ((LEVEL) == PWR_PVDLevel_5)|| \9 l! m: Z* K) E) G: a; B! W, T0 u; G
  16.                                  ((LEVEL) == PWR_PVDLevel_6) || ((LEVEL) == PWR_PVDLevel_7))
复制代码

  Q9 ?+ K  N- y, I2 OPWR_Regulator_state_in_STOP_mode
! _2 s$ X, e5 K6 e3 S0 d
% O1 b! w: c+ u2 {; d( R
  1. /** @defgroup PWR_Regulator_state_in_STOP_mode
    : Q" r: F: E4 n
  2.   * @{* H( e& B4 Y9 A5 R" _+ P
  3.   */
    ! j8 [( b" w, b2 G2 \! m9 b9 ?
  4. #define PWR_MainRegulator_ON                        ((uint32_t)0x00000000)
    . d! C$ _+ V' N
  5. #define PWR_LowPowerRegulator_ON                    PWR_CR_LPDS
    8 {8 y" s: `- R, r, W

  6. ( s7 r9 l  f0 B
  7. /* --- PWR_Legacy ---*/
    7 E  n3 \, s5 ~7 [' e  i
  8. #define PWR_Regulator_ON                            PWR_MainRegulator_ON
    % [8 C7 w: y1 v1 ]7 j* W
  9. #define PWR_Regulator_LowPower                      PWR_LowPowerRegulator_ON& g  s5 Z) }: M; |

  10. . m3 H1 G3 F, }: L# M, y( {; ^
  11. #define IS_PWR_REGULATOR(REGULATOR) (((REGULATOR) == PWR_MainRegulator_ON) || \2 c, b% M; p/ y
  12.                                      ((REGULATOR) == PWR_LowPowerRegulator_ON))
复制代码
! D' v% S1 J2 v- H7 g! o* b4 q" \
PWR_Regulator_state_in_UnderDrive_mode& o/ {7 L! ]' R# q# b  f( @

/ B, H! L8 `) t/ U( Z
  1. /** @defgroup PWR_Regulator_state_in_UnderDrive_mode . G% Q3 M, ~" b( R8 V. }
  2.   * @{
    # u* n. M1 w. h
  3.   */* ~6 z$ n9 e' w" h7 U
  4. #define PWR_MainRegulator_UnderDrive_ON               PWR_CR_MRUDS
    0 Q4 P8 Y* T0 b' L8 O
  5. #define PWR_LowPowerRegulator_UnderDrive_ON           ((uint32_t)(PWR_CR_LPDS | PWR_CR_LPUDS))9 m9 i' x0 z0 s+ y. Z
  6. 0 U+ @1 [7 x& Q' \2 X. H
  7. #define IS_PWR_REGULATOR_UNDERDRIVE(REGULATOR) (((REGULATOR) == PWR_MainRegulator_UnderDrive_ON) || \+ h3 V/ M9 u7 N, P% K
  8.                                                 ((REGULATOR) == PWR_LowPowerRegulator_UnderDrive_ON))
复制代码
* f/ U9 `7 X3 E
PWR_STOP_mode_entry
4 C7 C9 @: n  j9 \
* S" X+ ]7 d7 m0 r# H( j* |- T
  1. /** @defgroup PWR_STOP_mode_entry
    ( ^1 M: O9 U# _8 O7 Q- O
  2.   * @{
    9 Q1 D$ V3 t$ Q
  3.   */
    . w$ z/ L5 ^( X# g! u
  4. #define PWR_STOPEntry_WFI               ((uint8_t)0x01)' q2 Y% ^  u$ O4 B* c
  5. #define PWR_STOPEntry_WFE               ((uint8_t)0x02)
    4 ]2 @. o; X$ R4 u' Q. i
  6. #define IS_PWR_STOP_ENTRY(ENTRY) (((ENTRY) == PWR_STOPEntry_WFI) || ((ENTRY) == PWR_STOPEntry_WFE))
复制代码
0 S: ?2 s2 h: B+ t6 Q# f8 Y- l
PWR_Regulator_Voltage_Scale
) H/ a/ {4 d2 m1 N) x- Z, O. H
6 P$ d. q3 W. ]2 U9 J6 j/ `3 u
  1. /** @defgroup PWR_Regulator_Voltage_Scale
    : w) N1 M$ o; ?3 B
  2.   * @{
    7 Y4 W0 J  \! H. v
  3.   */
    ! {- {# `& @0 T5 c0 p- i
  4. #define PWR_Regulator_Voltage_Scale1    ((uint32_t)0x0000C000)
    ' b2 q5 a2 C: N+ C+ |, V, N9 C+ E% {
  5. #define PWR_Regulator_Voltage_Scale2    ((uint32_t)0x00008000)
    $ [% X: w" i# d; f* S$ Z
  6. #define PWR_Regulator_Voltage_Scale3    ((uint32_t)0x00004000)
    2 l$ k* \  g9 `4 i
  7. #define IS_PWR_REGULATOR_VOLTAGE(VOLTAGE) (((VOLTAGE) == PWR_Regulator_Voltage_Scale1) || \3 v2 U8 _% t2 Q; t' m7 R; `% F
  8.                                            ((VOLTAGE) == PWR_Regulator_Voltage_Scale2) || \/ q/ P" z2 o9 I8 m9 I( _
  9.                                            ((VOLTAGE) == PWR_Regulator_Voltage_Scale3))
复制代码
  `2 L- I9 U- w0 F
PWR_Flag
1 o5 _0 Y, r$ b+ t( m: w  X
' K% M( Z+ j7 S5 o. M4 z
  1. /** @defgroup PWR_Flag
    - u& R* G$ @+ n4 X) v$ V: X
  2.   * @{( g( {) M$ Z$ {6 E
  3.   */
    7 M, P+ _) |* T
  4. #define PWR_FLAG_WU                     PWR_CSR_WUF7 v4 D- x, z1 Q4 d; A: {4 F1 f
  5. #define PWR_FLAG_SB                     PWR_CSR_SBF$ Q- C6 V/ k5 R( a% q8 J
  6. #define PWR_FLAG_PVDO                   PWR_CSR_PVDO) U! _6 o2 r! F( R. A- v% s  C
  7. #define PWR_FLAG_BRR                    PWR_CSR_BRR/ a. y( ~9 P9 A( t6 [# P& H
  8. #define PWR_FLAG_VOSRDY                 PWR_CSR_VOSRDY+ S8 u; i3 Y8 ^( r. V) T& E
  9. #define PWR_FLAG_ODRDY                  PWR_CSR_ODRDY
    ! G: U1 S2 |4 G  f  |: d
  10. #define PWR_FLAG_ODSWRDY                PWR_CSR_ODSWRDY1 ?; Z( E( {' }* e* G+ ~) |
  11. #define PWR_FLAG_UDRDY                  PWR_CSR_UDSWRDY4 s4 D1 F3 w0 N
  12. : a2 V5 T; L  z
  13. /* --- FLAG Legacy ---*/
    & b5 T# i( d  m9 R
  14. #define PWR_FLAG_REGRDY                  PWR_FLAG_VOSRDY               " D+ e7 r; p+ f5 K

  15. : Z8 v) b. ~. @0 B
  16. #define IS_PWR_GET_FLAG(FLAG) (((FLAG) == PWR_FLAG_WU) || ((FLAG) == PWR_FLAG_SB) || \
    5 I1 o6 a3 q; `- j4 |: Z# ^
  17.                                ((FLAG) == PWR_FLAG_PVDO) || ((FLAG) == PWR_FLAG_BRR) || \% O. h2 P9 W9 d
  18.                                ((FLAG) == PWR_FLAG_VOSRDY) || ((FLAG) == PWR_FLAG_ODRDY) || \  p  Y& B( C9 B' M# {7 T1 l
  19.                                ((FLAG) == PWR_FLAG_ODSWRDY) || ((FLAG) == PWR_FLAG_UDRDY))$ G4 P. R: _) s

  20. ' o4 r; z2 b6 q" ?

  21. ! e: @# {2 e) |
  22. #define IS_PWR_CLEAR_FLAG(FLAG) (((FLAG) == PWR_FLAG_WU) || ((FLAG) == PWR_FLAG_SB) || \7 W( E4 U& |! \; V' \1 D3 \
  23.                                  ((FLAG) == PWR_FLAG_UDRDY))
复制代码

2 P- e7 [/ g, J. f03. 相关函数
6 v, G- X' f0 F* O+ v
8 l1 ^% W! _: ^! L9 D
  1. /* Function used to set the PWR configuration to the default reset state ******/
    / Q4 R, ^0 O$ V4 J
  2. void PWR_DeInit(void);
    7 o7 Q$ o0 F9 R
  3.   J" f. o9 }- W
  4. /* Backup Domain Access function **********************************************/
    * i4 `! w2 E+ L$ Z; C
  5. void PWR_BackupAccessCmd(FunctionalState NewState);
    9 R5 t4 @5 i' p8 p5 e* w& ?! R2 t$ Y

  6. : f" p- J7 T7 |9 J' D# D. U6 U
  7. /* PVD configuration functions ************************************************/
    ( }# t# P( D  N
  8. void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel);
    5 f; ^6 d8 Z+ G( S! d# C; N5 T
  9. void PWR_PVDCmd(FunctionalState NewState);) \  [5 @& |: o9 O6 l
  10. ) k% Z. H9 G9 ?) W, W/ A
  11. void PWR_WakeUpPinCmd(FunctionalState NewState);3 q8 l- z2 ~  I7 J/ V6 r

  12. ( n  Y+ W9 I0 c
  13. /* Main and Backup Regulators configuration functions *************************/ $ z+ S! X- [8 L& \$ O+ O
  14. void PWR_BackupRegulatorCmd(FunctionalState NewState);
    + R" y5 Q/ T  j; e) ]! J
  15. void PWR_MainRegulatorModeConfig(uint32_t PWR_Regulator_Voltage);
    5 w0 o9 t0 c4 L, F( Z/ ~, a$ p* d
  16. void PWR_OverDriveCmd(FunctionalState NewState);
    ; L5 P; ?9 p/ w
  17. void PWR_OverDriveSWCmd(FunctionalState NewState);) O7 S" v: A- y* S/ p) N; J
  18. void PWR_UnderDriveCmd(FunctionalState NewState);) o" p9 E! @, B$ T; v4 W
  19. 6 N# q( F7 f8 B- c% |% |8 e7 B0 o
  20. /* FLASH Power Down configuration functions ***********************************/ ' ~! D. c& L' c( D6 }: }/ h, W
  21. void PWR_FlashPowerDownCmd(FunctionalState NewState);
    6 b  m- Z/ m- g2 D: T  i

  22. 3 C. x/ g8 `# w) C1 p  `1 ^
  23. /* Low Power modes configuration functions ************************************/ " E" e) f# U6 U. ~& P
  24. void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry);
    . b! n' S  q5 e7 e& R8 g
  25. void PWR_EnterUnderDriveSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry);
    8 c$ o1 [/ q" F
  26. void PWR_EnterSTANDBYMode(void);
    8 i0 z( W" b  C- Q

  27. 5 a, ~6 H' O) k# L
  28. /* Flags management functions *************************************************/ & @5 N0 j/ m, K6 @4 U* N
  29. FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG);
    0 f) w* y* W. c7 x3 J$ C& p! U
  30. void PWR_ClearFlag(uint32_t PWR_FLAG);1 i, [& d. B. B& k

  31. " S- F- `0 P3 s! P: l
复制代码

! T; ]+ v' }) R+ }8 t3 Q04. 结构体封装0 G( Q/ M2 O; ^
  1. /**
    5 V) l2 K7 a5 j3 q$ \
  2.   * @brief Power Control
    , L( e+ E0 O1 k; N4 o
  3.   */3 O$ b6 M) n2 [- Q4 t) ~
  4. 9 I9 \3 e6 N: A  o8 G$ P5 @
  5. typedef struct- w: A+ ^* @4 F) m' J
  6. {! v+ R8 l0 E  ?, S6 N' n* [) P
  7.   __IO uint32_t CR;   /*!< PWR power control register,        Address offset: 0x00 */
    ' v( K! B$ Z6 a( {
  8.   __IO uint32_t CSR;  /*!< PWR power control/status register, Address offset: 0x04 */* E+ u- K6 C# d$ h' J% |/ U+ q
  9. } PWR_TypeDef;
复制代码
1 Z* U# d. q3 y  x5 g) T
赞 收藏 评论0 发布时间:2022-3-30 11:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

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