1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_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) 2016 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 __STM32L0xx_HAL_PWR_H
38 #define __STM32L0xx_HAL_PWR_H
39 
40 #ifdef __cplusplus
41  extern "C" {
42 #endif
43 
44 /* Includes ------------------------------------------------------------------*/
45 #include "stm32l0xx_hal_def.h"
46 
47 /** @addtogroup STM32L0xx_HAL_Driver
48   * @{
49   */
50 
51 /** @defgroup PWR PWR
52   * @{
53   */
54 
55 /** @defgroup PWR_Exported_Types PWR Exported Types
56   * @{
57   */
58 
59 /**
60   * @brief  PWR PVD configuration structure definition
61   */
62 typedef struct
63 {
64   uint32_t PVDLevel;   /*!< PVDLevel: Specifies the PVD detection level.
65                             This parameter can be a value of @ref PWR_PVD_detection_level */
66 
67   uint32_t Mode;       /*!< Mode: Specifies the operating mode for the selected pins.
68                             This parameter can be a value of @ref PWR_PVD_Mode */
69 }PWR_PVDTypeDef;
70 
71 /**
72   * @}
73   */
74 
75 /** @addtogroup PWR_Private
76   * @{
77   */
78 
79 #define PWR_EXTI_LINE_PVD      EXTI_FTSR_TR16  /*!< External interrupt line 16 Connected to the PVD EXTI Line */
80 
81 /**
82   * @}
83   */
84 
85 /** @defgroup PWR_Exported_Constants PWR Exported Constants
86   * @{
87   */
88 
89 /** @defgroup PWR_register_alias_address PWR Register alias address
90   * @{
91   */
92 #define PWR_WAKEUP_PIN1                PWR_CSR_EWUP1
93 #define PWR_WAKEUP_PIN2                PWR_CSR_EWUP2
94 #if defined (STM32L011xx) || defined (STM32L021xx) ||  defined (STM32L031xx) || defined (STM32L041xx) || defined (STM32L071xx) || \
95     defined (STM32L072xx) || defined (STM32L073xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
96 #define PWR_WAKEUP_PIN3                PWR_CSR_EWUP3
97 #endif
98 /**
99   * @}
100   */
101 
102 /** @defgroup PWR_PVD_detection_level PVD detection level
103   * @{
104   */
105 #define PWR_PVDLEVEL_0                 PWR_CR_PLS_LEV0
106 #define PWR_PVDLEVEL_1                 PWR_CR_PLS_LEV1
107 #define PWR_PVDLEVEL_2                 PWR_CR_PLS_LEV2
108 #define PWR_PVDLEVEL_3                 PWR_CR_PLS_LEV3
109 #define PWR_PVDLEVEL_4                 PWR_CR_PLS_LEV4
110 #define PWR_PVDLEVEL_5                 PWR_CR_PLS_LEV5
111 #define PWR_PVDLEVEL_6                 PWR_CR_PLS_LEV6
112 #define PWR_PVDLEVEL_7                 PWR_CR_PLS_LEV7  /* External input analog voltage
113                                                            (Compare internally to VREFINT) */
114 /**
115   * @}
116   */
117 
118 /** @defgroup PWR_PVD_Mode PWR PVD Mode
119   * @{
120   */
121 #define PWR_PVD_MODE_NORMAL                 ((uint32_t)0x00000000U)   /*!< basic mode is used */
122 #define PWR_PVD_MODE_IT_RISING              ((uint32_t)0x00010001U)   /*!< External Interrupt Mode with Rising edge trigger detection */
123 #define PWR_PVD_MODE_IT_FALLING             ((uint32_t)0x00010002U)   /*!< External Interrupt Mode with Falling edge trigger detection */
124 #define PWR_PVD_MODE_IT_RISING_FALLING      ((uint32_t)0x00010003U)   /*!< External Interrupt Mode with Rising/Falling edge trigger detection */
125 #define PWR_PVD_MODE_EVENT_RISING           ((uint32_t)0x00020001U)   /*!< Event Mode with Rising edge trigger detection */
126 #define PWR_PVD_MODE_EVENT_FALLING          ((uint32_t)0x00020002U)   /*!< Event Mode with Falling edge trigger detection */
127 #define PWR_PVD_MODE_EVENT_RISING_FALLING   ((uint32_t)0x00020003U)   /*!< Event Mode with Rising/Falling edge trigger detection */
128 
129 /**
130   * @}
131   */
132 
133 /** @defgroup PWR_Regulator_state_in_SLEEP_STOP_mode PWR Regulator state in SLEEP/STOP mode
134   * @{
135   */
136 #define PWR_MAINREGULATOR_ON           ((uint32_t)0x00000000U)
137 #define PWR_LOWPOWERREGULATOR_ON       PWR_CR_LPSDSR
138 
139 /**
140   * @}
141   */
142 
143 /** @defgroup PWR_SLEEP_mode_entry PWR SLEEP mode entry
144   * @{
145   */
146 #define PWR_SLEEPENTRY_WFI             ((uint8_t)0x01U)
147 #define PWR_SLEEPENTRY_WFE             ((uint8_t)0x02U)
148 /**
149   * @}
150   */
151 
152 /** @defgroup PWR_STOP_mode_entry PWR STOP mode entry
153   * @{
154   */
155 #define PWR_STOPENTRY_WFI              ((uint8_t)0x01U)
156 #define PWR_STOPENTRY_WFE              ((uint8_t)0x02U)
157 /**
158   * @}
159   */
160 
161 /** @defgroup PWR_Regulator_Voltage_Scale PWR Regulator Voltage Scale
162   * @{
163   */
164 
165 #define PWR_REGULATOR_VOLTAGE_SCALE1   PWR_CR_VOS_0
166 #define PWR_REGULATOR_VOLTAGE_SCALE2   PWR_CR_VOS_1
167 #define PWR_REGULATOR_VOLTAGE_SCALE3   PWR_CR_VOS
168 
169 #define IS_PWR_VOLTAGE_SCALING_RANGE(RANGE) (((RANGE) == PWR_REGULATOR_VOLTAGE_SCALE1) || \
170                                              ((RANGE) == PWR_REGULATOR_VOLTAGE_SCALE2) || \
171                                              ((RANGE) == PWR_REGULATOR_VOLTAGE_SCALE3))
172 /**
173   * @}
174   */
175 
176 /** @defgroup PWR_Flag PWR Flag
177   * @{
178   */
179 #define PWR_FLAG_WU                    PWR_CSR_WUF
180 #define PWR_FLAG_SB                    PWR_CSR_SBF
181 #define PWR_FLAG_PVDO                  PWR_CSR_PVDO
182 #define PWR_FLAG_VREFINTRDY            PWR_CSR_VREFINTRDYF
183 #define PWR_FLAG_VOS                   PWR_CSR_VOSF
184 #define PWR_FLAG_REGLP                 PWR_CSR_REGLPF
185 
186 
187 /**
188   * @}
189   */
190 
191 /**
192   * @}
193   */
194 
195 /** @defgroup PWR_Exported_Macro PWR Exported Macros
196   * @{
197   */
198 /** @brief  macros configure the main internal regulator output voltage.
199   *         When exiting Low Power Run Mode or during dynamic voltage scaling configuration,
200   *         the reference manual recommends to poll PWR_FLAG_REGLP bit to wait for the regulator
201   *         to reach main mode (resp. to get stabilized) for a transition from 0 to 1.
202   *         Only then the clock can be increased.
203   *
204   * @param  __REGULATOR__: specifies the regulator output voltage to achieve
205   *         a tradeoff between performance and power consumption when the device does
206   *         not operate at the maximum frequency (refer to the datasheets for more details).
207   *          This parameter can be one of the following values:
208   *            @arg PWR_REGULATOR_VOLTAGE_SCALE1: Regulator voltage output Scale 1 mode,
209   *                                                System frequency up to 32 MHz.
210   *            @arg PWR_REGULATOR_VOLTAGE_SCALE2: Regulator voltage output Scale 2 mode,
211   *                                                System frequency up to 16 MHz.
212   *            @arg PWR_REGULATOR_VOLTAGE_SCALE3: Regulator voltage output Scale 3 mode,
213   *                                                System frequency up to 4.2 MHz
214   * @retval None
215   */
216 #define __HAL_PWR_VOLTAGESCALING_CONFIG(__REGULATOR__) (MODIFY_REG(PWR->CR, PWR_CR_VOS, (__REGULATOR__)))
217 
218 /** @brief  Check PWR flag is set or not.
219   * @param  __FLAG__: specifies the flag to check.
220   *           This parameter can be one of the following values:
221   *            @arg PWR_FLAG_WU: Wake Up flag. This flag indicates that a wakeup event
222   *                  was received from the WKUP pin or from the RTC alarm (Alarm B),
223   *                  RTC Tamper event, RTC TimeStamp event or RTC Wakeup.
224   *                  An additional wakeup event is detected if the WKUP pin is enabled
225   *                  (by setting the EWUP bit) when the WKUP pin level is already high.
226   *            @arg PWR_FLAG_SB: StandBy flag. This flag indicates that the system was
227   *                  resumed from StandBy mode.
228   *            @arg PWR_FLAG_PVDO: PVD Output. This flag is valid only if PVD is enabled
229   *                  by the HAL_PWR_EnablePVD() function. The PVD is stopped by Standby mode.
230   *                  For this reason, this bit is equal to 0 after Standby or reset
231   *                  until the PVDE bit is set.
232   *            @arg PWR_FLAG_VREFINTRDY: Internal voltage reference (VREFINT) ready flag.
233   *                 This bit indicates the state of the internal voltage reference, VREFINT.
234   *            @arg PWR_FLAG_VOS: Voltage Scaling select flag. A delay is required for
235   *                 the internal regulator to be ready after the voltage range is changed.
236   *                 The VOSF bit indicates that the regulator has reached the voltage level
237   *                 defined with bits VOS of PWR_CR register.
238   *            @arg PWR_FLAG_REGLP: Regulator LP flag. When the MCU exits from Low power run
239   *                 mode, this bit stays at 1 until the regulator is ready in main mode.
240   *                 A polling on this bit is recommended to wait for the regulator main mode.
241   *                 This bit is reset by hardware when the regulator is ready.
242   * @retval The new state of __FLAG__ (TRUE or FALSE).
243   */
244 #define __HAL_PWR_GET_FLAG(__FLAG__)                  ((PWR->CSR & (__FLAG__)) == (__FLAG__))
245 
246 /** @brief  Clear the PWR pending flags.
247   * @param  __FLAG__: specifies the flag to clear.
248   *          This parameter can be one of the following values:
249   *            @arg PWR_FLAG_WU: Wake Up flag
250   *            @arg PWR_FLAG_SB: StandBy flag
251   */
252 #define __HAL_PWR_CLEAR_FLAG(__FLAG__)                SET_BIT(PWR->CR, (__FLAG__) << 2U)
253 
254 /**
255   * @brief Enable interrupt on PVD Exti Line 16.
256   * @retval None.
257   */
258 #define __HAL_PWR_PVD_EXTI_ENABLE_IT()      SET_BIT(EXTI->IMR, PWR_EXTI_LINE_PVD)
259 
260 /**
261   * @brief Disable interrupt on PVD Exti Line 16.
262   * @retval None.
263   */
264 #define __HAL_PWR_PVD_EXTI_DISABLE_IT()     CLEAR_BIT(EXTI->IMR, PWR_EXTI_LINE_PVD)
265 
266 /**
267   * @brief Enable event on PVD Exti Line 16.
268   * @retval None.
269   */
270 #define __HAL_PWR_PVD_EXTI_ENABLE_EVENT()   SET_BIT(EXTI->EMR, PWR_EXTI_LINE_PVD)
271 
272 /**
273   * @brief Disable event on PVD Exti Line 16.
274   * @retval None.
275   */
276 #define __HAL_PWR_PVD_EXTI_DISABLE_EVENT()  CLEAR_BIT(EXTI->EMR, PWR_EXTI_LINE_PVD)
277 
278 
279 /**
280   * @brief  PVD EXTI line configuration: set falling edge trigger.
281   * @retval None.
282   */
283 #define __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE()  SET_BIT(EXTI->FTSR, PWR_EXTI_LINE_PVD)
284 
285 
286 /**
287   * @brief Disable the PVD Extended Interrupt Falling Trigger.
288   * @retval None.
289   */
290 #define __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE()  CLEAR_BIT(EXTI->FTSR, PWR_EXTI_LINE_PVD)
291 
292 
293 /**
294   * @brief  PVD EXTI line configuration: set rising edge trigger.
295   * @retval None.
296   */
297 #define __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE()   SET_BIT(EXTI->RTSR, PWR_EXTI_LINE_PVD)
298 
299 /**
300   * @brief Disable the PVD Extended Interrupt Rising Trigger.
301   * This parameter can be:
302   * @retval None.
303   */
304 #define __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE()  CLEAR_BIT(EXTI->RTSR, PWR_EXTI_LINE_PVD)
305 
306 /**
307   * @brief  PVD EXTI line configuration: set rising & falling edge trigger.
308   * @retval None.
309   */
310 #define __HAL_PWR_PVD_EXTI_ENABLE_RISING_FALLING_EDGE()   do { __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();__HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE(); } while(0);
311 
312 /**
313   * @brief Disable the PVD Extended Interrupt Rising & Falling Trigger.
314   * This parameter can be:
315   * @retval None.
316   */
317 #define __HAL_PWR_PVD_EXTI_DISABLE_RISING_FALLING_EDGE()  do { __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();__HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE(); } while(0);
318 
319 
320 
321 /**
322   * @brief Check whether the specified PVD EXTI interrupt flag is set or not.
323   * @retval EXTI PVD Line Status.
324   */
325 #define __HAL_PWR_PVD_EXTI_GET_FLAG()       (EXTI->PR & (PWR_EXTI_LINE_PVD))
326 
327 /**
328   * @brief Clear the PVD EXTI flag.
329   * @retval None.
330   */
331 #define __HAL_PWR_PVD_EXTI_CLEAR_FLAG()     (EXTI->PR = (PWR_EXTI_LINE_PVD))
332 
333 /**
334   * @brief Generate a Software interrupt on selected EXTI line.
335   * @retval None.
336   */
337 #define __HAL_PWR_PVD_EXTI_GENERATE_SWIT()  SET_BIT(EXTI->SWIER, PWR_EXTI_LINE_PVD)
338 
339 /**
340   * @brief Generate a Software interrupt on selected EXTI line.
341   * @retval None.
342   */
343 #define __HAL_PWR_PVD_EXTI_GENERATE_SWIT()            SET_BIT(EXTI->SWIER, PWR_EXTI_LINE_PVD)
344 
345 /**
346   * @}
347   */
348 
349 /** @addtogroup PWR_Private
350   * @{
351   */
352 #define IS_PWR_PVD_LEVEL(LEVEL) (((LEVEL) == PWR_PVDLEVEL_0) || ((LEVEL) == PWR_PVDLEVEL_1)|| \
353                                  ((LEVEL) == PWR_PVDLEVEL_2) || ((LEVEL) == PWR_PVDLEVEL_3)|| \
354                                  ((LEVEL) == PWR_PVDLEVEL_4) || ((LEVEL) == PWR_PVDLEVEL_5)|| \
355                                  ((LEVEL) == PWR_PVDLEVEL_6) || ((LEVEL) == PWR_PVDLEVEL_7))
356 
357 #define IS_PWR_PVD_MODE(MODE) (((MODE) == PWR_PVD_MODE_IT_RISING)|| ((MODE) == PWR_PVD_MODE_IT_FALLING) || \
358                               ((MODE) == PWR_PVD_MODE_IT_RISING_FALLING) || ((MODE) == PWR_PVD_MODE_EVENT_RISING) || \
359                               ((MODE) == PWR_PVD_MODE_EVENT_FALLING) || ((MODE) == PWR_PVD_MODE_EVENT_RISING_FALLING) || \
360                               ((MODE) == PWR_PVD_MODE_NORMAL))
361 
362 #if defined (STM32L071xx) || defined (STM32L072xx) || defined (STM32L073xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
363 #define IS_PWR_WAKEUP_PIN(PIN) (((PIN) == PWR_WAKEUP_PIN1) || \
364                                 ((PIN) == PWR_WAKEUP_PIN2) || \
365                                 ((PIN) == PWR_WAKEUP_PIN3))
366 #elif defined (STM32L051xx) || defined (STM32L052xx) || defined (STM32L053xx) || defined (STM32L061xx) || defined (STM32L062xx) || defined (STM32L063xx)
367 #define IS_PWR_WAKEUP_PIN(PIN) (((PIN) == PWR_WAKEUP_PIN1) || \
368                                 ((PIN) == PWR_WAKEUP_PIN2))
369 #elif defined (STM32L031xx) || defined (STM32L041xx)
370 #define IS_PWR_WAKEUP_PIN(PIN) (((PIN) == PWR_WAKEUP_PIN1) || \
371                                 ((PIN) == PWR_WAKEUP_PIN2))
372 #elif defined (STM32L011xx) || defined (STM32L021xx)
373 #define IS_PWR_WAKEUP_PIN(PIN) (((PIN) == PWR_WAKEUP_PIN1) || \
374                                 ((PIN) == PWR_WAKEUP_PIN3))
375 #endif
376 
377 #define IS_PWR_REGULATOR(REGULATOR) (((REGULATOR) == PWR_MAINREGULATOR_ON) || \
378                                      ((REGULATOR) == PWR_LOWPOWERREGULATOR_ON))
379 #define IS_PWR_SLEEP_ENTRY(ENTRY) (((ENTRY) == PWR_SLEEPENTRY_WFI) || ((ENTRY) == PWR_SLEEPENTRY_WFE))
380 
381 #define IS_PWR_STOP_ENTRY(ENTRY) (((ENTRY) == PWR_STOPENTRY_WFI) || ((ENTRY) == PWR_STOPENTRY_WFE))
382 
383 /**
384   * @}
385   */
386 
387 /* Include PWR HAL Extension module */
388 #include "stm32l0xx_hal_pwr_ex.h"
389 
390 /** @defgroup PWR_Exported_Functions PWR Exported Functions
391   * @{
392   */
393 
394 /** @defgroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
395   * @{
396   */
397 void HAL_PWR_DeInit(void);
398 void HAL_PWR_EnableBkUpAccess(void);
399 void HAL_PWR_DisableBkUpAccess(void);
400 /**
401   * @}
402   */
403 
404 /** @defgroup PWR_Exported_Functions_Group2 Low Power modes configuration functions
405   * @{
406   */
407 
408 /* PVD control functions  ************************************************/
409 void HAL_PWR_ConfigPVD(PWR_PVDTypeDef *sConfigPVD);
410 void HAL_PWR_EnablePVD(void);
411 void HAL_PWR_DisablePVD(void);
412 void HAL_PWR_PVD_IRQHandler(void);
413 void HAL_PWR_PVDCallback(void);
414 
415 /* WakeUp pins configuration functions ****************************************/
416 void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinx);
417 void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx);
418 
419 /* Low Power modes configuration functions ************************************/
420 void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry);
421 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry);
422 void HAL_PWR_EnterSTANDBYMode(void);
423 
424 void HAL_PWR_EnableSleepOnExit(void);
425 void HAL_PWR_DisableSleepOnExit(void);
426 void HAL_PWR_EnableSEVOnPend(void);
427 void HAL_PWR_DisableSEVOnPend(void);
428 
429 /**
430   * @}
431   */
432 
433 /**
434   * @}
435   */
436 
437 /* Define the private group ***********************************/
438 /**************************************************************/
439 /** @defgroup PWR_Private PWR Private
440   * @{
441   */
442 /**
443   * @}
444   */
445 /**************************************************************/
446 
447 /**
448   * @}
449   */
450 
451 /**
452   * @}
453   */
454 
455 #ifdef __cplusplus
456 }
457 #endif
458 
459 
460 #endif /* __STM32L0xx_HAL_PWR_H */
461 
462 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
463 
464