1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_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 __STM32F2xx_HAL_PWR_H
21 #define __STM32F2xx_HAL_PWR_H
22 
23 #ifdef __cplusplus
24  extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32f2xx_hal_def.h"
29 
30 /** @addtogroup STM32F2xx_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 /* Exported constants --------------------------------------------------------*/
61 /** @defgroup PWR_Exported_Constants PWR Exported Constants
62   * @{
63   */
64 
65 /** @defgroup PWR_WakeUp_Pins PWR WakeUp Pins
66   * @{
67   */
68 #define PWR_WAKEUP_PIN1                 0x00000100U
69 /**
70   * @}
71   */
72 
73 /** @defgroup PWR_PVD_detection_level PWR PVD detection level
74   * @{
75   */
76 #define PWR_PVDLEVEL_0                  PWR_CR_PLS_LEV0
77 #define PWR_PVDLEVEL_1                  PWR_CR_PLS_LEV1
78 #define PWR_PVDLEVEL_2                  PWR_CR_PLS_LEV2
79 #define PWR_PVDLEVEL_3                  PWR_CR_PLS_LEV3
80 #define PWR_PVDLEVEL_4                  PWR_CR_PLS_LEV4
81 #define PWR_PVDLEVEL_5                  PWR_CR_PLS_LEV5
82 #define PWR_PVDLEVEL_6                  PWR_CR_PLS_LEV6
83 #define PWR_PVDLEVEL_7                  PWR_CR_PLS_LEV7/* External input analog voltage
84                                                           (Compare internally to VREFINT) */
85 /**
86   * @}
87   */
88 
89 /** @defgroup PWR_PVD_Mode PWR PVD Mode
90   * @{
91   */
92 #define PWR_PVD_MODE_NORMAL                 0x00000000U   /*!< basic mode is used */
93 #define PWR_PVD_MODE_IT_RISING              0x00010001U   /*!< External Interrupt Mode with Rising edge trigger detection */
94 #define PWR_PVD_MODE_IT_FALLING             0x00010002U   /*!< External Interrupt Mode with Falling edge trigger detection */
95 #define PWR_PVD_MODE_IT_RISING_FALLING      0x00010003U   /*!< External Interrupt Mode with Rising/Falling edge trigger detection */
96 #define PWR_PVD_MODE_EVENT_RISING           0x00020001U   /*!< Event Mode with Rising edge trigger detection */
97 #define PWR_PVD_MODE_EVENT_FALLING          0x00020002U   /*!< Event Mode with Falling edge trigger detection */
98 #define PWR_PVD_MODE_EVENT_RISING_FALLING   0x00020003U   /*!< Event Mode with Rising/Falling edge trigger detection */
99 /**
100   * @}
101   */
102 
103 
104 /** @defgroup PWR_Regulator_state_in_STOP_mode PWR Regulator state in SLEEP/STOP mode
105   * @{
106   */
107 #define PWR_MAINREGULATOR_ON                        0x00000000U
108 #define PWR_LOWPOWERREGULATOR_ON                    PWR_CR_LPDS
109 /**
110   * @}
111   */
112 
113 /** @defgroup PWR_SLEEP_mode_entry PWR SLEEP mode entry
114   * @{
115   */
116 #define PWR_SLEEPENTRY_WFI              ((uint8_t)0x01)
117 #define PWR_SLEEPENTRY_WFE              ((uint8_t)0x02)
118 /**
119   * @}
120   */
121 
122 /** @defgroup PWR_STOP_mode_entry PWR STOP mode entry
123   * @{
124   */
125 #define PWR_STOPENTRY_WFI               ((uint8_t)0x01)
126 #define PWR_STOPENTRY_WFE               ((uint8_t)0x02)
127 /**
128   * @}
129   */
130 
131 /** @defgroup PWR_Flag PWR Flag
132   * @{
133   */
134 #define PWR_FLAG_WU                     PWR_CSR_WUF
135 #define PWR_FLAG_SB                     PWR_CSR_SBF
136 #define PWR_FLAG_PVDO                   PWR_CSR_PVDO
137 #define PWR_FLAG_BRR                    PWR_CSR_BRR
138 /**
139   * @}
140   */
141 
142 /**
143   * @}
144   */
145 
146 /* Exported macro ------------------------------------------------------------*/
147 /** @defgroup PWR_Exported_Macro PWR Exported Macro
148   * @{
149   */
150 
151 /** @brief  Check PWR flag is set or not.
152   * @param  __FLAG__ specifies the flag to check.
153   *           This parameter can be one of the following values:
154   *            @arg PWR_FLAG_WU: Wake Up flag. This flag indicates that a wakeup event
155   *                  was received from the WKUP pin or from the RTC alarm (Alarm A
156   *                  or Alarm B), RTC Tamper event, RTC TimeStamp event or RTC Wakeup.
157   *                  An additional wakeup event is detected if the WKUP pin is enabled
158   *                  (by setting the EWUP bit) when the WKUP pin level is already high.
159   *            @arg PWR_FLAG_SB: StandBy flag. This flag indicates that the system was
160   *                  resumed from StandBy mode.
161   *            @arg PWR_FLAG_PVDO: PVD Output. This flag is valid only if PVD is enabled
162   *                  by the HAL_PWR_EnablePVD() function. The PVD is stopped by Standby mode
163   *                  For this reason, this bit is equal to 0 after Standby or reset
164   *                  until the PVDE bit is set.
165   *            @arg PWR_FLAG_BRR: Backup regulator ready flag. This bit is not reset
166   *                  when the device wakes up from Standby mode or by a system reset
167   *                  or power reset.
168   * @retval The new state of __FLAG__ (TRUE or FALSE).
169   */
170 #define __HAL_PWR_GET_FLAG(__FLAG__) ((PWR->CSR & (__FLAG__)) == (__FLAG__))
171 
172 /** @brief  Clear the PWR's pending flags.
173   * @param  __FLAG__ specifies the flag to clear.
174   *          This parameter can be one of the following values:
175   *            @arg PWR_FLAG_WU: Wake Up flag
176   *            @arg PWR_FLAG_SB: StandBy flag
177   */
178 #define __HAL_PWR_CLEAR_FLAG(__FLAG__) (PWR->CR |=  (__FLAG__) << 2U)
179 
180 /**
181   * @brief Enable the PVD Exti Line 16.
182   * @retval None.
183   */
184 #define __HAL_PWR_PVD_EXTI_ENABLE_IT()   (EXTI->IMR |= (PWR_EXTI_LINE_PVD))
185 
186 /**
187   * @brief Disable the PVD EXTI Line 16.
188   * @retval None.
189   */
190 #define __HAL_PWR_PVD_EXTI_DISABLE_IT()  (EXTI->IMR &= ~(PWR_EXTI_LINE_PVD))
191 
192 /**
193   * @brief Enable event on PVD Exti Line 16.
194   * @retval None.
195   */
196 #define __HAL_PWR_PVD_EXTI_ENABLE_EVENT()   (EXTI->EMR |= (PWR_EXTI_LINE_PVD))
197 
198 /**
199   * @brief Disable event on PVD Exti Line 16.
200   * @retval None.
201   */
202 #define __HAL_PWR_PVD_EXTI_DISABLE_EVENT()  (EXTI->EMR &= ~(PWR_EXTI_LINE_PVD))
203 
204 /**
205   * @brief Enable the PVD Extended Interrupt Rising Trigger.
206   * @retval None.
207   */
208 #define __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE()   SET_BIT(EXTI->RTSR, PWR_EXTI_LINE_PVD)
209 
210 /**
211   * @brief Disable the PVD Extended Interrupt Rising Trigger.
212   * @retval None.
213   */
214 #define __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE()  CLEAR_BIT(EXTI->RTSR, PWR_EXTI_LINE_PVD)
215 
216 /**
217   * @brief Enable the PVD Extended Interrupt Falling Trigger.
218   * @retval None.
219   */
220 #define __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE()   SET_BIT(EXTI->FTSR, PWR_EXTI_LINE_PVD)
221 
222 
223 /**
224   * @brief Disable the PVD Extended Interrupt Falling Trigger.
225   * @retval None.
226   */
227 #define __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE()  CLEAR_BIT(EXTI->FTSR, PWR_EXTI_LINE_PVD)
228 
229 
230 /**
231   * @brief  PVD EXTI line configuration: set rising & falling edge trigger.
232   * @retval None.
233   */
234 #define __HAL_PWR_PVD_EXTI_ENABLE_RISING_FALLING_EDGE()   do{ __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();\
235                                                               __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();\
236                                                             }while(0)
237 
238 /**
239   * @brief Disable the PVD Extended Interrupt Rising & Falling Trigger.
240   * This parameter can be:
241   * @retval None.
242   */
243 #define __HAL_PWR_PVD_EXTI_DISABLE_RISING_FALLING_EDGE()  do{ __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();\
244                                                               __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();\
245                                                             }while(0)
246 
247 /**
248   * @brief checks whether the specified PVD Exti interrupt flag is set or not.
249   * @retval EXTI PVD Line Status.
250   */
251 #define __HAL_PWR_PVD_EXTI_GET_FLAG()  (EXTI->PR & (PWR_EXTI_LINE_PVD))
252 
253 /**
254   * @brief Clear the PVD Exti flag.
255   * @retval None.
256   */
257 #define __HAL_PWR_PVD_EXTI_CLEAR_FLAG()  (EXTI->PR = (PWR_EXTI_LINE_PVD))
258 
259 /**
260   * @brief  Generates a Software interrupt on PVD EXTI line.
261   * @retval None
262   */
263 #define __HAL_PWR_PVD_EXTI_GENERATE_SWIT() (EXTI->SWIER |= (PWR_EXTI_LINE_PVD))
264 
265 /**
266   * @}
267   */
268 
269 /* Include PWR HAL Extension module */
270 #include "stm32f2xx_hal_pwr_ex.h"
271 
272 /* Exported functions --------------------------------------------------------*/
273 /** @addtogroup PWR_Exported_Functions PWR Exported Functions
274   * @{
275   */
276 
277 /** @addtogroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
278   * @{
279   */
280 /* Initialization and de-initialization functions *****************************/
281 void HAL_PWR_DeInit(void);
282 void HAL_PWR_EnableBkUpAccess(void);
283 void HAL_PWR_DisableBkUpAccess(void);
284 /**
285   * @}
286   */
287 
288 /** @addtogroup PWR_Exported_Functions_Group2 Peripheral Control functions
289   * @{
290   */
291 /* Peripheral Control functions  **********************************************/
292 /* PVD configuration */
293 void HAL_PWR_ConfigPVD(PWR_PVDTypeDef *sConfigPVD);
294 void HAL_PWR_EnablePVD(void);
295 void HAL_PWR_DisablePVD(void);
296 
297 /* WakeUp pins configuration */
298 void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinx);
299 void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx);
300 
301 /* Low Power modes entry */
302 void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry);
303 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry);
304 void HAL_PWR_EnterSTANDBYMode(void);
305 
306 /* Power PVD IRQ Handler */
307 void HAL_PWR_PVD_IRQHandler(void);
308 void HAL_PWR_PVDCallback(void);
309 
310 /* Cortex System Control functions  *******************************************/
311 void HAL_PWR_EnableSleepOnExit(void);
312 void HAL_PWR_DisableSleepOnExit(void);
313 void HAL_PWR_EnableSEVOnPend(void);
314 void HAL_PWR_DisableSEVOnPend(void);
315 /**
316   * @}
317   */
318 
319 /**
320   * @}
321   */
322 
323 /* Private types -------------------------------------------------------------*/
324 /* Private variables ---------------------------------------------------------*/
325 /* Private constants ---------------------------------------------------------*/
326 /** @defgroup PWR_Private_Constants PWR Private Constants
327   * @{
328   */
329 
330 /** @defgroup PWR_PVD_EXTI_Line PWR PVD EXTI Line
331   * @{
332   */
333 #define PWR_EXTI_LINE_PVD  ((uint32_t)EXTI_IMR_MR16)  /*!< External interrupt line 16 Connected to the PVD EXTI Line */
334 /**
335   * @}
336   */
337 
338 /** @defgroup PWR_register_alias_address PWR Register alias address
339   * @{
340   */
341 /* ------------- PWR registers bit address in the alias region ---------------*/
342 #define PWR_OFFSET               (PWR_BASE - PERIPH_BASE)
343 #define PWR_CR_OFFSET            0x00U
344 #define PWR_CSR_OFFSET           0x04U
345 #define PWR_CR_OFFSET_BB         (PWR_OFFSET + PWR_CR_OFFSET)
346 #define PWR_CSR_OFFSET_BB        (PWR_OFFSET + PWR_CSR_OFFSET)
347 /**
348   * @}
349   */
350 
351 /** @defgroup PWR_CR_register_alias PWR CR Register alias address
352   * @{
353   */
354 /* --- CR Register ---*/
355 /* Alias word address of DBP bit */
356 #define DBP_BIT_NUMBER   POSITION_VAL(PWR_CR_DBP)
357 #define CR_DBP_BB        (uint32_t)(PERIPH_BB_BASE + (PWR_CR_OFFSET_BB * 32U) + (DBP_BIT_NUMBER * 4U))
358 
359 /* Alias word address of PVDE bit */
360 #define PVDE_BIT_NUMBER  POSITION_VAL(PWR_CR_PVDE)
361 #define CR_PVDE_BB       (uint32_t)(PERIPH_BB_BASE + (PWR_CR_OFFSET_BB * 32U) + (PVDE_BIT_NUMBER * 4U))
362 /**
363   * @}
364   */
365 
366 /** @defgroup PWR_CSR_register_alias PWR CSR Register alias address
367   * @{
368   */
369 /* --- CSR Register ---*/
370 /* Alias word address of EWUP bit */
371 #define EWUP_BIT_NUMBER  POSITION_VAL(PWR_CSR_EWUP)
372 #define CSR_EWUP_BB      (PERIPH_BB_BASE + (PWR_CSR_OFFSET_BB * 32U) + (EWUP_BIT_NUMBER * 4U))
373 /**
374   * @}
375   */
376 
377 /**
378   * @}
379   */
380 /* Private macros ------------------------------------------------------------*/
381 /** @defgroup PWR_Private_Macros PWR Private Macros
382   * @{
383   */
384 
385 /** @defgroup PWR_IS_PWR_Definitions PWR Private macros to check input parameters
386   * @{
387   */
388 #define IS_PWR_WAKEUP_PIN(PIN) ((PIN) == PWR_WAKEUP_PIN1)
389 #define IS_PWR_PVD_LEVEL(LEVEL) (((LEVEL) == PWR_PVDLEVEL_0) || ((LEVEL) == PWR_PVDLEVEL_1)|| \
390                                  ((LEVEL) == PWR_PVDLEVEL_2) || ((LEVEL) == PWR_PVDLEVEL_3)|| \
391                                  ((LEVEL) == PWR_PVDLEVEL_4) || ((LEVEL) == PWR_PVDLEVEL_5)|| \
392                                  ((LEVEL) == PWR_PVDLEVEL_6) || ((LEVEL) == PWR_PVDLEVEL_7))
393 #define IS_PWR_PVD_MODE(MODE) (((MODE) == PWR_PVD_MODE_IT_RISING)|| ((MODE) == PWR_PVD_MODE_IT_FALLING) || \
394                                ((MODE) == PWR_PVD_MODE_IT_RISING_FALLING) || ((MODE) == PWR_PVD_MODE_EVENT_RISING) || \
395                                ((MODE) == PWR_PVD_MODE_EVENT_FALLING) || ((MODE) == PWR_PVD_MODE_EVENT_RISING_FALLING) || \
396                                ((MODE) == PWR_PVD_MODE_NORMAL))
397 #define IS_PWR_REGULATOR(REGULATOR) (((REGULATOR) == PWR_MAINREGULATOR_ON) || \
398                                      ((REGULATOR) == PWR_LOWPOWERREGULATOR_ON))
399 #define IS_PWR_SLEEP_ENTRY(ENTRY) (((ENTRY) == PWR_SLEEPENTRY_WFI) || ((ENTRY) == PWR_SLEEPENTRY_WFE))
400 #define IS_PWR_STOP_ENTRY(ENTRY) (((ENTRY) == PWR_STOPENTRY_WFI) || ((ENTRY) == PWR_STOPENTRY_WFE))
401 /**
402   * @}
403   */
404 
405 /**
406   * @}
407   */
408 
409 /**
410   * @}
411   */
412 
413 /**
414   * @}
415   */
416 
417 #ifdef __cplusplus
418 }
419 #endif
420 
421 
422 #endif /* __STM32F2xx_HAL_PWR_H */
423