1 /**
2   ******************************************************************************
3   * @file    stm32g4xx_hal_pwr.h
4   * @author  MCD Application Team
5   * @brief   Header file of PWR HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2019 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 
19 /* Define to prevent recursive inclusion -------------------------------------*/
20 #ifndef STM32G4xx_HAL_PWR_H
21 #define STM32G4xx_HAL_PWR_H
22 
23 #ifdef __cplusplus
24  extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32g4xx_hal_def.h"
29 
30 /** @addtogroup STM32G4xx_HAL_Driver
31   * @{
32   */
33 
34 /** @addtogroup PWR
35   * @{
36   */
37 
38 /* Exported types ------------------------------------------------------------*/
39 
40 /** @defgroup PWR_Exported_Types PWR Exported Types
41   * @{
42   */
43 
44 /**
45   * @brief  PWR PVD configuration structure definition
46   */
47 typedef struct
48 {
49   uint32_t PVDLevel;   /*!< PVDLevel: Specifies the PVD detection level.
50                             This parameter can be a value of @ref PWR_PVD_detection_level. */
51 
52   uint32_t Mode;      /*!< Mode: Specifies the operating mode for the selected pins.
53                            This parameter can be a value of @ref PWR_PVD_Mode. */
54 }PWR_PVDTypeDef;
55 
56 
57 /**
58   * @}
59   */
60 
61 /* Exported constants --------------------------------------------------------*/
62 
63 /** @defgroup PWR_Exported_Constants PWR Exported Constants
64   * @{
65   */
66 
67 
68 /** @defgroup PWR_PVD_detection_level Programmable Voltage Detection levels
69   * @{
70   */
71 #define PWR_PVDLEVEL_0                  PWR_CR2_PLS_LEV0  /*!< PVD threshold around 2.0 V */
72 #define PWR_PVDLEVEL_1                  PWR_CR2_PLS_LEV1  /*!< PVD threshold around 2.2 V */
73 #define PWR_PVDLEVEL_2                  PWR_CR2_PLS_LEV2  /*!< PVD threshold around 2.4 V */
74 #define PWR_PVDLEVEL_3                  PWR_CR2_PLS_LEV3  /*!< PVD threshold around 2.5 V */
75 #define PWR_PVDLEVEL_4                  PWR_CR2_PLS_LEV4  /*!< PVD threshold around 2.6 V */
76 #define PWR_PVDLEVEL_5                  PWR_CR2_PLS_LEV5  /*!< PVD threshold around 2.8 V */
77 #define PWR_PVDLEVEL_6                  PWR_CR2_PLS_LEV6  /*!< PVD threshold around 2.9 V */
78 #define PWR_PVDLEVEL_7                  PWR_CR2_PLS_LEV7  /*!< External input analog voltage (compared internally to VREFINT) */
79 /**
80   * @}
81   */
82 
83 /** @defgroup PWR_PVD_Mode  PWR PVD interrupt and event mode
84   * @{
85   */
86 #define PWR_PVD_MODE_NORMAL                 ((uint32_t)0x00000000)   /*!< Basic mode is used */
87 #define PWR_PVD_MODE_IT_RISING              ((uint32_t)0x00010001)   /*!< External Interrupt Mode with Rising edge trigger detection */
88 #define PWR_PVD_MODE_IT_FALLING             ((uint32_t)0x00010002)   /*!< External Interrupt Mode with Falling edge trigger detection */
89 #define PWR_PVD_MODE_IT_RISING_FALLING      ((uint32_t)0x00010003)   /*!< External Interrupt Mode with Rising/Falling edge trigger detection */
90 #define PWR_PVD_MODE_EVENT_RISING           ((uint32_t)0x00020001)   /*!< Event Mode with Rising edge trigger detection */
91 #define PWR_PVD_MODE_EVENT_FALLING          ((uint32_t)0x00020002)   /*!< Event Mode with Falling edge trigger detection */
92 #define PWR_PVD_MODE_EVENT_RISING_FALLING   ((uint32_t)0x00020003)   /*!< Event Mode with Rising/Falling edge trigger detection */
93 /**
94   * @}
95   */
96 
97 
98 
99 
100 /** @defgroup PWR_Regulator_state_in_SLEEP_STOP_mode  PWR regulator mode
101   * @{
102   */
103 #define PWR_MAINREGULATOR_ON            ((uint32_t)0x00000000) /*!< Regulator in main mode      */
104 #define PWR_LOWPOWERREGULATOR_ON        PWR_CR1_LPR            /*!< Regulator in low-power mode */
105 /**
106   * @}
107   */
108 
109 /** @defgroup PWR_SLEEP_mode_entry  PWR SLEEP mode entry
110   * @{
111   */
112 #define PWR_SLEEPENTRY_WFI              ((uint8_t)0x01)        /*!< Wait For Interruption instruction to enter Sleep mode */
113 #define PWR_SLEEPENTRY_WFE              ((uint8_t)0x02)        /*!< Wait For Event instruction to enter Sleep mode        */
114 /**
115   * @}
116   */
117 
118 /** @defgroup PWR_STOP_mode_entry  PWR STOP mode entry
119   * @{
120   */
121 #define PWR_STOPENTRY_WFI               ((uint8_t)0x01)       /*!< Wait For Interruption instruction to enter Stop mode */
122 #define PWR_STOPENTRY_WFE               ((uint8_t)0x02)       /*!< Wait For Event instruction to enter Stop mode        */
123 /**
124   * @}
125   */
126 
127 
128 /** @defgroup PWR_PVD_EXTI_LINE  PWR PVD external interrupt line
129   * @{
130   */
131 #define PWR_EXTI_LINE_PVD  ((uint32_t)0x00010000)   /*!< External interrupt line 16 Connected to the PVD EXTI Line */
132 /**
133   * @}
134   */
135 
136 /** @defgroup PWR_PVD_EVENT_LINE  PWR PVD event line
137   * @{
138   */
139 #define PWR_EVENT_LINE_PVD  ((uint32_t)0x00010000)  /*!< Event line 16 Connected to the PVD Event Line */
140 /**
141   * @}
142   */
143 
144 /**
145   * @}
146   */
147 
148 /* Exported macros -----------------------------------------------------------*/
149 /** @defgroup PWR_Exported_Macros  PWR Exported Macros
150   * @{
151   */
152 
153 /** @brief  Check whether or not a specific PWR flag is set.
154   * @param  __FLAG__: specifies the flag to check.
155   *           This parameter can be one of the following values:
156   *            @arg @ref PWR_FLAG_WUF1 Wake Up Flag 1. Indicates that a wakeup event
157   *                  was received from the WKUP pin 1.
158   *            @arg @ref PWR_FLAG_WUF2 Wake Up Flag 2. Indicates that a wakeup event
159   *                  was received from the WKUP pin 2.
160   *            @arg @ref PWR_FLAG_WUF3 Wake Up Flag 3. Indicates that a wakeup event
161   *                  was received from the WKUP pin 3.
162   *            @arg @ref PWR_FLAG_WUF4 Wake Up Flag 4. Indicates that a wakeup event
163   *                  was received from the WKUP pin 4.
164   *            @arg @ref PWR_FLAG_WUF5 Wake Up Flag 5. Indicates that a wakeup event
165   *                  was received from the WKUP pin 5.
166   *            @arg @ref PWR_FLAG_SB StandBy Flag. Indicates that the system
167   *                  entered StandBy mode.
168   *            @arg @ref PWR_FLAG_WUFI Wake-Up Flag Internal. Set when a wakeup is detected on
169   *                 the internal wakeup line.
170   *            @arg @ref PWR_FLAG_REGLPS Low Power Regulator Started. Indicates whether or not the
171   *                 low-power regulator is ready.
172   *            @arg @ref PWR_FLAG_REGLPF Low Power Regulator Flag. Indicates whether the
173   *                 regulator is ready in main mode or is in low-power mode.
174   *            @arg @ref PWR_FLAG_VOSF Voltage Scaling Flag. Indicates whether the regulator is ready
175   *                 in the selected voltage range or is still changing to the required voltage level.
176   *            @arg @ref PWR_FLAG_PVDO Power Voltage Detector Output. Indicates whether VDD voltage is
177   *                  below or above the selected PVD threshold.
178 @if PWR_CR2_PVME1
179   *            @arg @ref PWR_FLAG_PVMO1 Peripheral Voltage Monitoring Output 1. Indicates whether VDDUSB voltage is
180   *                  is below or above PVM1 threshold (applicable when USB feature is supported).
181 @endif
182 @if PWR_CR2_PVME2
183   *            @arg @ref PWR_FLAG_PVMO2 Peripheral Voltage Monitoring Output 2. Indicates whether VDDIO2 voltage is
184   *                  is below or above PVM2 threshold (applicable when VDDIO2 is present on device).
185 @endif
186   *            @arg @ref PWR_FLAG_PVMO3 Peripheral Voltage Monitoring Output 3. Indicates whether VDDA voltage is
187   *                  is below or above PVM3 threshold.
188   *            @arg @ref PWR_FLAG_PVMO4 Peripheral Voltage Monitoring Output 4. Indicates whether VDDA voltage is
189   *                  is below or above PVM4 threshold.
190   *
191   * @retval The new state of __FLAG__ (TRUE or FALSE).
192   */
193 #define __HAL_PWR_GET_FLAG(__FLAG__)  ( ((((uint8_t)(__FLAG__)) >> 5U) == 1)  ?\
194                                       (PWR->SR1 & (1U << ((__FLAG__) & 31U))) :\
195                                       (PWR->SR2 & (1U << ((__FLAG__) & 31U))) )
196 
197 /** @brief  Clear a specific PWR flag.
198   * @param  __FLAG__: specifies the flag to clear.
199   *          This parameter can be one of the following values:
200   *            @arg @ref PWR_FLAG_WUF1 Wake Up Flag 1. Indicates that a wakeup event
201   *                  was received from the WKUP pin 1.
202   *            @arg @ref PWR_FLAG_WUF2 Wake Up Flag 2. Indicates that a wakeup event
203   *                  was received from the WKUP pin 2.
204   *            @arg @ref PWR_FLAG_WUF3 Wake Up Flag 3. Indicates that a wakeup event
205   *                  was received from the WKUP pin 3.
206   *            @arg @ref PWR_FLAG_WUF4 Wake Up Flag 4. Indicates that a wakeup event
207   *                  was received from the WKUP pin 4.
208   *            @arg @ref PWR_FLAG_WUF5 Wake Up Flag 5. Indicates that a wakeup event
209   *                  was received from the WKUP pin 5.
210   *            @arg @ref PWR_FLAG_WU Encompasses all five Wake Up Flags.
211   *            @arg @ref PWR_FLAG_SB Standby Flag. Indicates that the system
212   *                  entered Standby mode.
213   * @retval None
214   */
215 #define __HAL_PWR_CLEAR_FLAG(__FLAG__)   ( (((uint8_t)(__FLAG__)) == PWR_FLAG_WU) ?\
216                                          (PWR->SCR  = (__FLAG__)) :\
217                                          (PWR->SCR = (1U << ((__FLAG__) & 31U))) )
218 /**
219   * @brief Enable the PVD Extended Interrupt Line.
220   * @retval None
221   */
222 #define __HAL_PWR_PVD_EXTI_ENABLE_IT()   SET_BIT(EXTI->IMR1, PWR_EXTI_LINE_PVD)
223 
224 /**
225   * @brief Disable the PVD Extended Interrupt Line.
226   * @retval None
227   */
228 #define __HAL_PWR_PVD_EXTI_DISABLE_IT()  CLEAR_BIT(EXTI->IMR1, PWR_EXTI_LINE_PVD)
229 
230 /**
231   * @brief Enable the PVD Event Line.
232   * @retval None
233   */
234 #define __HAL_PWR_PVD_EXTI_ENABLE_EVENT()   SET_BIT(EXTI->EMR1, PWR_EVENT_LINE_PVD)
235 
236 /**
237   * @brief Disable the PVD Event Line.
238   * @retval None
239   */
240 #define __HAL_PWR_PVD_EXTI_DISABLE_EVENT()  CLEAR_BIT(EXTI->EMR1, PWR_EVENT_LINE_PVD)
241 
242 /**
243   * @brief Enable the PVD Extended Interrupt Rising Trigger.
244   * @retval None
245   */
246 #define __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE()   SET_BIT(EXTI->RTSR1, PWR_EXTI_LINE_PVD)
247 
248 /**
249   * @brief Disable the PVD Extended Interrupt Rising Trigger.
250   * @retval None
251   */
252 #define __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE()  CLEAR_BIT(EXTI->RTSR1, PWR_EXTI_LINE_PVD)
253 
254 /**
255   * @brief Enable the PVD Extended Interrupt Falling Trigger.
256   * @retval None
257   */
258 #define __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE()   SET_BIT(EXTI->FTSR1, PWR_EXTI_LINE_PVD)
259 
260 
261 /**
262   * @brief Disable the PVD Extended Interrupt Falling Trigger.
263   * @retval None
264   */
265 #define __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE()  CLEAR_BIT(EXTI->FTSR1, PWR_EXTI_LINE_PVD)
266 
267 
268 /**
269   * @brief  Enable the PVD Extended Interrupt Rising & Falling Trigger.
270   * @retval None
271   */
272 #define __HAL_PWR_PVD_EXTI_ENABLE_RISING_FALLING_EDGE()  \
273   do {                                                   \
274     __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();             \
275     __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();            \
276   } while(0)
277 
278 /**
279   * @brief Disable the PVD Extended Interrupt Rising & Falling Trigger.
280   * @retval None
281   */
282 #define __HAL_PWR_PVD_EXTI_DISABLE_RISING_FALLING_EDGE()  \
283   do {                                                    \
284     __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();             \
285     __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();            \
286   } while(0)
287 
288 /**
289   * @brief  Generate a Software interrupt on selected EXTI line.
290   * @retval None
291   */
292 #define __HAL_PWR_PVD_EXTI_GENERATE_SWIT() SET_BIT(EXTI->SWIER1, PWR_EXTI_LINE_PVD)
293 
294 /**
295   * @brief Check whether or not the PVD EXTI interrupt flag is set.
296   * @retval EXTI PVD Line Status.
297   */
298 #define __HAL_PWR_PVD_EXTI_GET_FLAG()  (EXTI->PR1 & PWR_EXTI_LINE_PVD)
299 
300 /**
301   * @brief Clear the PVD EXTI interrupt flag.
302   * @retval None
303   */
304 #define __HAL_PWR_PVD_EXTI_CLEAR_FLAG()  WRITE_REG(EXTI->PR1, PWR_EXTI_LINE_PVD)
305 
306 /**
307   * @}
308   */
309 
310 
311 /* Private macros --------------------------------------------------------*/
312 /** @addtogroup  PWR_Private_Macros   PWR Private Macros
313   * @{
314   */
315 
316 #define IS_PWR_PVD_LEVEL(LEVEL) (((LEVEL) == PWR_PVDLEVEL_0) || ((LEVEL) == PWR_PVDLEVEL_1)|| \
317                                  ((LEVEL) == PWR_PVDLEVEL_2) || ((LEVEL) == PWR_PVDLEVEL_3)|| \
318                                  ((LEVEL) == PWR_PVDLEVEL_4) || ((LEVEL) == PWR_PVDLEVEL_5)|| \
319                                  ((LEVEL) == PWR_PVDLEVEL_6) || ((LEVEL) == PWR_PVDLEVEL_7))
320 
321 #define IS_PWR_PVD_MODE(MODE)  (((MODE) == PWR_PVD_MODE_NORMAL)              ||\
322                                 ((MODE) == PWR_PVD_MODE_IT_RISING)           ||\
323                                 ((MODE) == PWR_PVD_MODE_IT_FALLING)          ||\
324                                 ((MODE) == PWR_PVD_MODE_IT_RISING_FALLING)   ||\
325                                 ((MODE) == PWR_PVD_MODE_EVENT_RISING)        ||\
326                                 ((MODE) == PWR_PVD_MODE_EVENT_FALLING)       ||\
327                                 ((MODE) == PWR_PVD_MODE_EVENT_RISING_FALLING))
328 
329 #define IS_PWR_REGULATOR(REGULATOR)      (((REGULATOR) == PWR_MAINREGULATOR_ON) || \
330                                           ((REGULATOR) == PWR_LOWPOWERREGULATOR_ON))
331 
332 #define IS_PWR_SLEEP_ENTRY(ENTRY) (((ENTRY) == PWR_SLEEPENTRY_WFI) || ((ENTRY) == PWR_SLEEPENTRY_WFE))
333 
334 #define IS_PWR_STOP_ENTRY(ENTRY) (((ENTRY) == PWR_STOPENTRY_WFI) || ((ENTRY) == PWR_STOPENTRY_WFE) )
335 
336 /**
337   * @}
338   */
339 
340 /* Include PWR HAL Extended module */
341 #include "stm32g4xx_hal_pwr_ex.h"
342 
343 /* Exported functions --------------------------------------------------------*/
344 
345 /** @addtogroup PWR_Exported_Functions PWR Exported Functions
346   * @{
347   */
348 
349 /** @addtogroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
350   * @{
351   */
352 
353 /* Initialization and de-initialization functions *******************************/
354 void HAL_PWR_DeInit(void);
355 void HAL_PWR_EnableBkUpAccess(void);
356 void HAL_PWR_DisableBkUpAccess(void);
357 
358 /**
359   * @}
360   */
361 
362 /** @addtogroup PWR_Exported_Functions_Group2 Peripheral Control functions
363   * @{
364   */
365 
366 /* Peripheral Control functions  ************************************************/
367 HAL_StatusTypeDef HAL_PWR_ConfigPVD(PWR_PVDTypeDef *sConfigPVD);
368 void HAL_PWR_EnablePVD(void);
369 void HAL_PWR_DisablePVD(void);
370 
371 
372 /* WakeUp pins configuration functions ****************************************/
373 void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinPolarity);
374 void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx);
375 
376 /* Low Power modes configuration functions ************************************/
377 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry);
378 void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry);
379 void HAL_PWR_EnterSTANDBYMode(void);
380 
381 void HAL_PWR_EnableSleepOnExit(void);
382 void HAL_PWR_DisableSleepOnExit(void);
383 void HAL_PWR_EnableSEVOnPend(void);
384 void HAL_PWR_DisableSEVOnPend(void);
385 
386 void HAL_PWR_PVDCallback(void);
387 
388 
389 /**
390   * @}
391   */
392 
393 /**
394   * @}
395   */
396 
397 /**
398   * @}
399   */
400 
401 /**
402   * @}
403   */
404 
405 #ifdef __cplusplus
406 }
407 #endif
408 
409 
410 #endif /* STM32G4xx_HAL_PWR_H */
411 
412