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