1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_hal_pwr.h
4   * @author  MCD Application Team
5   * @brief   Header file of PWR HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2017 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 STM32L4xx_HAL_PWR_H
21 #define STM32L4xx_HAL_PWR_H
22 
23 #ifdef __cplusplus
24  extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32l4xx_hal_def.h"
29 
30 /** @addtogroup STM32L4xx_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_EXT_SMPS External SMPS Ready Flag. When available on device, indicates
169   *                 that external switch can be closed to connect to the external SMPS, when the Range 2
170   *                 of internal regulator is ready.
171   *            @arg @ref PWR_FLAG_WUFI Wake-Up Flag Internal. Set when a wakeup is detected on
172   *                 the internal wakeup line.
173   *            @arg @ref PWR_FLAG_REGLPS Low Power Regulator Started. Indicates whether or not the
174   *                 low-power regulator is ready.
175   *            @arg @ref PWR_FLAG_REGLPF Low Power Regulator Flag. Indicates whether the
176   *                 regulator is ready in main mode or is in low-power mode.
177   *            @arg @ref PWR_FLAG_VOSF Voltage Scaling Flag. Indicates whether the regulator is ready
178   *                 in the selected voltage range or is still changing to the required voltage level.
179   *            @arg @ref PWR_FLAG_PVDO Power Voltage Detector Output. Indicates whether VDD voltage is
180   *                  below or above the selected PVD threshold.
181   *            @arg @ref PWR_FLAG_PVMO1 Peripheral Voltage Monitoring Output 1. Indicates whether VDDUSB voltage is
182   *                  is below or above PVM1 threshold (applicable when USB feature is supported).
183   @if STM32L486xx
184   *            @arg @ref PWR_FLAG_PVMO2 Peripheral Voltage Monitoring Output 2. Indicates whether VDDIO2 voltage is
185   *                  is below or above PVM2 threshold (applicable when VDDIO2 is present on device).
186   @endif
187   *            @arg @ref PWR_FLAG_PVMO3 Peripheral Voltage Monitoring Output 3. Indicates whether VDDA voltage is
188   *                  is below or above PVM3 threshold.
189   *            @arg @ref PWR_FLAG_PVMO4 Peripheral Voltage Monitoring Output 4. Indicates whether VDDA voltage is
190   *                  is below or above PVM4 threshold.
191   *
192   * @retval The new state of __FLAG__ (TRUE or FALSE).
193   */
194 #define __HAL_PWR_GET_FLAG(__FLAG__)  ( ((((uint8_t)(__FLAG__)) >> 5U) == 1)  ?\
195                                       (PWR->SR1 & (1U << ((__FLAG__) & 31U))) :\
196                                       (PWR->SR2 & (1U << ((__FLAG__) & 31U))) )
197 
198 /** @brief  Clear a specific PWR flag.
199   * @param  __FLAG__ specifies the flag to clear.
200   *          This parameter can be one of the following values:
201   *            @arg @ref PWR_FLAG_WUF1 Wake Up Flag 1. Indicates that a wakeup event
202   *                  was received from the WKUP pin 1.
203   *            @arg @ref PWR_FLAG_WUF2 Wake Up Flag 2. Indicates that a wakeup event
204   *                  was received from the WKUP pin 2.
205   *            @arg @ref PWR_FLAG_WUF3 Wake Up Flag 3. Indicates that a wakeup event
206   *                  was received from the WKUP pin 3.
207   *            @arg @ref PWR_FLAG_WUF4 Wake Up Flag 4. Indicates that a wakeup event
208   *                  was received from the WKUP pin 4.
209   *            @arg @ref PWR_FLAG_WUF5 Wake Up Flag 5. Indicates that a wakeup event
210   *                  was received from the WKUP pin 5.
211   *            @arg @ref PWR_FLAG_WU Encompasses all five Wake Up Flags.
212   *            @arg @ref PWR_FLAG_SB Standby Flag. Indicates that the system
213   *                  entered Standby mode.
214   * @retval None
215   */
216 #define __HAL_PWR_CLEAR_FLAG(__FLAG__)   ( (((uint8_t)(__FLAG__)) == PWR_FLAG_WU) ?\
217                                          (PWR->SCR  = (__FLAG__)) :\
218                                          (PWR->SCR = (1U << ((__FLAG__) & 31U))) )
219 /**
220   * @brief Enable the PVD Extended Interrupt Line.
221   * @retval None
222   */
223 #define __HAL_PWR_PVD_EXTI_ENABLE_IT()   SET_BIT(EXTI->IMR1, PWR_EXTI_LINE_PVD)
224 
225 /**
226   * @brief Disable the PVD Extended Interrupt Line.
227   * @retval None
228   */
229 #define __HAL_PWR_PVD_EXTI_DISABLE_IT()  CLEAR_BIT(EXTI->IMR1, PWR_EXTI_LINE_PVD)
230 
231 /**
232   * @brief Enable the PVD Event Line.
233   * @retval None
234   */
235 #define __HAL_PWR_PVD_EXTI_ENABLE_EVENT()   SET_BIT(EXTI->EMR1, PWR_EVENT_LINE_PVD)
236 
237 /**
238   * @brief Disable the PVD Event Line.
239   * @retval None
240   */
241 #define __HAL_PWR_PVD_EXTI_DISABLE_EVENT()  CLEAR_BIT(EXTI->EMR1, PWR_EVENT_LINE_PVD)
242 
243 /**
244   * @brief Enable the PVD Extended Interrupt Rising Trigger.
245   * @retval None
246   */
247 #define __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE()   SET_BIT(EXTI->RTSR1, PWR_EXTI_LINE_PVD)
248 
249 /**
250   * @brief Disable the PVD Extended Interrupt Rising Trigger.
251   * @retval None
252   */
253 #define __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE()  CLEAR_BIT(EXTI->RTSR1, PWR_EXTI_LINE_PVD)
254 
255 /**
256   * @brief Enable the PVD Extended Interrupt Falling Trigger.
257   * @retval None
258   */
259 #define __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE()   SET_BIT(EXTI->FTSR1, PWR_EXTI_LINE_PVD)
260 
261 
262 /**
263   * @brief Disable the PVD Extended Interrupt Falling Trigger.
264   * @retval None
265   */
266 #define __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE()  CLEAR_BIT(EXTI->FTSR1, PWR_EXTI_LINE_PVD)
267 
268 
269 /**
270   * @brief  Enable the PVD Extended Interrupt Rising & Falling Trigger.
271   * @retval None
272   */
273 #define __HAL_PWR_PVD_EXTI_ENABLE_RISING_FALLING_EDGE()  \
274   do {                                                   \
275     __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();             \
276     __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();            \
277   } while(0)
278 
279 /**
280   * @brief Disable the PVD Extended Interrupt Rising & Falling Trigger.
281   * @retval None
282   */
283 #define __HAL_PWR_PVD_EXTI_DISABLE_RISING_FALLING_EDGE()  \
284   do {                                                    \
285     __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();             \
286     __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();            \
287   } while(0)
288 
289 /**
290   * @brief  Generate a Software interrupt on selected EXTI line.
291   * @retval None
292   */
293 #define __HAL_PWR_PVD_EXTI_GENERATE_SWIT() SET_BIT(EXTI->SWIER1, PWR_EXTI_LINE_PVD)
294 
295 /**
296   * @brief Check whether or not the PVD EXTI interrupt flag is set.
297   * @retval EXTI PVD Line Status.
298   */
299 #define __HAL_PWR_PVD_EXTI_GET_FLAG()  (EXTI->PR1 & PWR_EXTI_LINE_PVD)
300 
301 /**
302   * @brief Clear the PVD EXTI interrupt flag.
303   * @retval None
304   */
305 #define __HAL_PWR_PVD_EXTI_CLEAR_FLAG()  WRITE_REG(EXTI->PR1, PWR_EXTI_LINE_PVD)
306 
307 /**
308   * @}
309   */
310 
311 
312 /* Private macros --------------------------------------------------------*/
313 /** @addtogroup  PWR_Private_Macros   PWR Private Macros
314   * @{
315   */
316 
317 #define IS_PWR_PVD_LEVEL(LEVEL) (((LEVEL) == PWR_PVDLEVEL_0) || ((LEVEL) == PWR_PVDLEVEL_1)|| \
318                                  ((LEVEL) == PWR_PVDLEVEL_2) || ((LEVEL) == PWR_PVDLEVEL_3)|| \
319                                  ((LEVEL) == PWR_PVDLEVEL_4) || ((LEVEL) == PWR_PVDLEVEL_5)|| \
320                                  ((LEVEL) == PWR_PVDLEVEL_6) || ((LEVEL) == PWR_PVDLEVEL_7))
321 
322 #define IS_PWR_PVD_MODE(MODE)  (((MODE) == PWR_PVD_MODE_NORMAL)              ||\
323                                 ((MODE) == PWR_PVD_MODE_IT_RISING)           ||\
324                                 ((MODE) == PWR_PVD_MODE_IT_FALLING)          ||\
325                                 ((MODE) == PWR_PVD_MODE_IT_RISING_FALLING)   ||\
326                                 ((MODE) == PWR_PVD_MODE_EVENT_RISING)        ||\
327                                 ((MODE) == PWR_PVD_MODE_EVENT_FALLING)       ||\
328                                 ((MODE) == PWR_PVD_MODE_EVENT_RISING_FALLING))
329 
330 #define IS_PWR_REGULATOR(REGULATOR)      (((REGULATOR) == PWR_MAINREGULATOR_ON) || \
331                                           ((REGULATOR) == PWR_LOWPOWERREGULATOR_ON))
332 
333 #define IS_PWR_SLEEP_ENTRY(ENTRY) (((ENTRY) == PWR_SLEEPENTRY_WFI) || ((ENTRY) == PWR_SLEEPENTRY_WFE))
334 
335 #define IS_PWR_STOP_ENTRY(ENTRY) (((ENTRY) == PWR_STOPENTRY_WFI) || ((ENTRY) == PWR_STOPENTRY_WFE) )
336 
337 /**
338   * @}
339   */
340 
341 /* Include PWR HAL Extended module */
342 #include "stm32l4xx_hal_pwr_ex.h"
343 
344 /* Exported functions --------------------------------------------------------*/
345 
346 /** @addtogroup PWR_Exported_Functions PWR Exported Functions
347   * @{
348   */
349 
350 /** @addtogroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
351   * @{
352   */
353 
354 /* Initialization and de-initialization functions *******************************/
355 void HAL_PWR_DeInit(void);
356 void HAL_PWR_EnableBkUpAccess(void);
357 void HAL_PWR_DisableBkUpAccess(void);
358 
359 /**
360   * @}
361   */
362 
363 /** @addtogroup PWR_Exported_Functions_Group2 Peripheral Control functions
364   * @{
365   */
366 
367 /* Peripheral Control functions  ************************************************/
368 HAL_StatusTypeDef HAL_PWR_ConfigPVD(PWR_PVDTypeDef *sConfigPVD);
369 void HAL_PWR_EnablePVD(void);
370 void HAL_PWR_DisablePVD(void);
371 
372 
373 /* WakeUp pins configuration functions ****************************************/
374 void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinPolarity);
375 void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx);
376 
377 /* Low Power modes configuration functions ************************************/
378 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry);
379 void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry);
380 void HAL_PWR_EnterSTANDBYMode(void);
381 
382 void HAL_PWR_EnableSleepOnExit(void);
383 void HAL_PWR_DisableSleepOnExit(void);
384 void HAL_PWR_EnableSEVOnPend(void);
385 void HAL_PWR_DisableSEVOnPend(void);
386 
387 void HAL_PWR_PVDCallback(void);
388 
389 
390 /**
391   * @}
392   */
393 
394 /**
395   * @}
396   */
397 
398 /**
399   * @}
400   */
401 
402 /**
403   * @}
404   */
405 
406 #ifdef __cplusplus
407 }
408 #endif
409 
410 
411 #endif /* STM32L4xx_HAL_PWR_H */
412