1 /**
2   ******************************************************************************
3   * @file    stm32u0xx_hal_pwr.h
4   * @author  MCD Application Team
5   * @brief   Header file of PWR HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2023 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 STM32U0xx_HAL_PWR_H
21 #define STM32U0xx_HAL_PWR_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32u0xx_hal_def.h"
29 
30 /** @addtogroup STM32U0xx_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 
62 /** @defgroup PWR_Exported_Constants PWR Exported Constants
63   * @{
64   */
65 
66 /** @defgroup PWR_PVD_detection_level Programmable Voltage Detection levels
67   * @{
68   */
69 #define PWR_PVDLEVEL_0                      0x00000000U                                                                       /*!< PVD threshold around 2.0 V                                     */
70 #define PWR_PVDLEVEL_1                      PWR_CR2_PLS_0                                                                     /*!< PVD threshold around 2.2 V                                     */
71 #define PWR_PVDLEVEL_2                      PWR_CR2_PLS_1                                                                     /*!< PVD threshold around 2.4 V                                     */
72 #define PWR_PVDLEVEL_3                      (PWR_CR2_PLS_1 | PWR_CR2_PLS_0)                                                   /*!< PVD threshold around 2.5 V                                     */
73 #define PWR_PVDLEVEL_4                      PWR_CR2_PLS_2                                                                     /*!< PVD threshold around 2.6 V                                     */
74 #define PWR_PVDLEVEL_5                      (PWR_CR2_PLS_2 | PWR_CR2_PLS_0)                                                   /*!< PVD threshold around 2.8 V                                     */
75 #define PWR_PVDLEVEL_6                      (PWR_CR2_PLS_2 | PWR_CR2_PLS_1)                                                   /*!< PVD threshold around 2.9 V                                     */
76 #define PWR_PVDLEVEL_7                      (PWR_CR2_PLS_2 | PWR_CR2_PLS_1 | PWR_CR2_PLS_0)                                   /*!< External input analog voltage (compared internally to VREFINT) */
77 
78 /**
79   * @}
80   */
81 
82 /** @defgroup PWR_PVD_Mode  PWR PVD interrupt and event mode
83   * @{
84   */
85 #define PWR_PVD_MODE_NORMAL                  0x00000000U                                                                    /*!< Basic mode is used                                                 */
86 #define PWR_PVD_MODE_IT_RISING               0x00010001U                                                                    /*!< External Interrupt Mode with Rising edge trigger detection         */
87 #define PWR_PVD_MODE_IT_FALLING              0x00010002U                                                                    /*!< External Interrupt Mode with Falling edge trigger detection        */
88 #define PWR_PVD_MODE_IT_RISING_FALLING       0x00010003U                                                                    /*!< External Interrupt Mode with Rising/Falling edge trigger detection */
89 #define PWR_PVD_MODE_EVENT_RISING            0x00020001U                                                                    /*!< Event Mode with Rising edge trigger detection                      */
90 #define PWR_PVD_MODE_EVENT_FALLING           0x00020002U                                                                    /*!< Event Mode with Falling edge trigger detection                     */
91 #define PWR_PVD_MODE_EVENT_RISING_FALLING    0x00020003U                                                                    /*!< Event Mode with Rising/Falling edge trigger detection              */
92 /**
93   * @}
94   */
95 
96 /** @defgroup PWR_Regulator_state_in_SLEEP_STOP_mode  PWR regulator mode
97   * @{
98   */
99 #define PWR_MAINREGULATOR_ON                  0U                                                                            /*!< Regulator in main mode      */
100 #define PWR_LOWPOWERREGULATOR_ON              PWR_CR1_LPR                                                                   /*!< Regulator in low-power mode */
101 /**
102   * @}
103   */
104 
105 /** @defgroup PWR_SLEEP_mode_entry  PWR SLEEP mode entry
106   * @{
107   */
108 #define PWR_SLEEPENTRY_WFI                     ((uint8_t)0x01)                                                             /*!< Wait For Interruption instruction to enter Sleep mode */
109 #define PWR_SLEEPENTRY_WFE                     ((uint8_t)0x02)                                                             /*!< Wait For Event instruction to enter Sleep mode        */
110 /**
111   * @}
112   */
113 
114 /** @defgroup PWR_STOP_mode_entry  PWR STOP mode entry
115   * @{
116   */
117 #define PWR_STOPENTRY_WFI                       ((uint8_t)0x01)                                                             /*!< Wait For Interruption instruction to enter Stop mode */
118 #define PWR_STOPENTRY_WFE                       ((uint8_t)0x02)                                                             /*!< Wait For Event instruction to enter Stop mode        */
119 /**
120   * @}
121   */
122 
123 /** @defgroup PWR_PVD_EXTI_LINE  PWR PVD external interrupt line
124   * @{
125   */
126 #define PWR_EXTI_LINE_PVD                         0x00010000U                                                               /*!< External interrupt line 16 Connected to the PVD EXTI Line */
127 /**
128   * @}
129   */
130 
131 /** @defgroup PWR_PVD_EVENT_LINE  PWR PVD event line
132   * @{
133   */
134 #define PWR_EVENT_LINE_PVD                         0x00010000U                                                               /*!< Event line 16 Connected to the PVD Event Line */
135 /**
136   * @}
137   */
138 
139 /**
140   * @}
141   */
142 
143 /* Exported macros -----------------------------------------------------------*/
144 /** @defgroup PWR_Exported_Macros  PWR Exported Macros
145   * @{
146   */
147 
148 /** @brief  Check whether or not a specific PWR flag is set.
149   * @param  __FLAG__ specifies the flag to check.
150   *           This parameter can be one of the following values:
151   *            @arg @ref PWR_FLAG_WUF1 Wake Up Flag 1. Indicates that a wakeup event
152   *                  was received from the WKUP pin 1.
153   *            @arg @ref PWR_FLAG_WUF2 Wake Up Flag 2. Indicates that a wakeup event
154   *                  was received from the WKUP pin 2.
155   *            @arg @ref PWR_FLAG_WUF3 Wake Up Flag 3. Indicates that a wakeup event
156   *                  was received from the WKUP pin 3.
157   *            @arg @ref PWR_FLAG_WUF4 Wake Up Flag 4. Indicates that a wakeup event
158   *                  was received from the WKUP pin 4.
159   *            @arg @ref PWR_FLAG_WUF5 Wake Up Flag 5. Indicates that a wakeup event
160   *                  was received from the WKUP pin 5.
161   *            @arg @ref PWR_FLAG_WUF7 Wake Up Flag 7. Indicates that a wakeup event
162   *                  was received from the WKUP pin 7.
163   *            @arg @ref PWR_FLAG_SB StandBy Flag. Indicates that the system
164   *                  entered StandBy mode.
165   *            @arg @ref PWR_FLAG_STOP0 Stop Flag. Indicates that the system
166   *                  entered Stop mode 0.
167   *            @arg @ref PWR_FLAG_STOP1 Stop Flag. Indicates that the system
168   *                  entered Stop mode 1.
169   *            @arg @ref PWR_FLAG_STOP2 Stop Flag. Indicates that the system
170   *                  entered Stop mode 2.
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_PVMO_USB Peripheral Voltage Monitoring Output 1. Indicates whether VDDUSB voltage
182   *                  is below or above PVM1 threshold.
183   *            @arg @ref PWR_FLAG_PVMO_ADC Peripheral Voltage Monitoring Output 3. Indicates whether VDDA voltage
184   *                  is below or above PVM3 threshold.
185   *            @arg @ref PWR_FLAG_PVMO_DAC Peripheral Voltage Monitoring Output 4. Indicates whether VDDA voltage
186   *                  is below or above PVM4 threshold.
187   *
188   * @retval The new state of __FLAG__ (TRUE or FALSE).
189   */
190 #define __HAL_PWR_GET_FLAG(__FLAG__)   ( (((__FLAG__) >> 8U) == 1)        ?\
191                                          ((PWR->SR1 & (1U << ((__FLAG__) & 0xFFU))) >> ( (__FLAG__) & 0xFFU))  :\
192                                          ((PWR->SR2 & (1U << ((__FLAG__) & 0xFFU))) >> ( (__FLAG__) & 0xFFU)) )
193 
194 /** @brief  Clear a specific PWR flag.
195   * @param  __FLAG__ specifies the flag to clear.
196   *          This parameter can be one of the following values:
197   *            @arg @ref PWR_FLAG_WUF1 Wake Up Flag 1. Indicates that a wakeup event
198   *                  was received from the WKUP pin 1.
199   *            @arg @ref PWR_FLAG_WUF2 Wake Up Flag 2. Indicates that a wakeup event
200   *                  was received from the WKUP pin 2.
201   *            @arg @ref PWR_FLAG_WUF3 Wake Up Flag 3. Indicates that a wakeup event
202   *                  was received from the WKUP pin 3.
203   *            @arg @ref PWR_FLAG_WUF4 Wake Up Flag 4. Indicates that a wakeup event
204   *                  was received from the WKUP pin 4.
205   *            @arg @ref PWR_FLAG_WUF5 Wake Up Flag 5. Indicates that a wakeup event
206   *                  was received from the WKUP pin 5.
207   *            @arg @ref PWR_FLAG_WUF7 Wake Up Flag 7. Indicates that a wakeup event
208   *                  was received from the WKUP pin 7.
209   *            @arg @ref PWR_FLAG_WU Encompasses all five Wake Up Flags.
210   *            @arg @ref PWR_FLAG_SB Standby Flag. Indicates that the system
211   *                  entered Standby mode.
212   * @retval None
213   */
214 #define __HAL_PWR_CLEAR_FLAG(__FLAG__)   ( (((uint8_t)(__FLAG__)) == PWR_FLAG_WU)  ?\
215                                            (PWR->SCR = (__FLAG__))                  :\
216                                            (PWR->SCR = (1U << ((__FLAG__) & 0xFFU))) )
217 /**
218   * @brief Enable the PVD Extended Interrupt Line.
219   * @retval None
220   */
221 #define __HAL_PWR_PVD_EXTI_ENABLE_IT()   SET_BIT(EXTI->IMR1, PWR_EXTI_LINE_PVD)
222 
223 /**
224   * @brief Disable the PVD Extended Interrupt Line.
225   * @retval None
226   */
227 #define __HAL_PWR_PVD_EXTI_DISABLE_IT()  CLEAR_BIT(EXTI->IMR1, PWR_EXTI_LINE_PVD)
228 
229 /**
230   * @brief Enable the PVD Event Line.
231   * @retval None
232   */
233 #define __HAL_PWR_PVD_EXTI_ENABLE_EVENT()   SET_BIT(EXTI->EMR1, PWR_EVENT_LINE_PVD)
234 
235 /**
236   * @brief Disable the PVD Event Line.
237   * @retval None
238   */
239 #define __HAL_PWR_PVD_EXTI_DISABLE_EVENT()  CLEAR_BIT(EXTI->EMR1, PWR_EVENT_LINE_PVD)
240 
241 /**
242   * @brief Enable the PVD Extended Interrupt Rising Trigger.
243   * @retval None
244   */
245 #define __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE()   SET_BIT(EXTI->RTSR1, PWR_EXTI_LINE_PVD)
246 
247 /**
248   * @brief Disable the PVD Extended Interrupt Rising Trigger.
249   * @retval None
250   */
251 #define __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE()  CLEAR_BIT(EXTI->RTSR1, PWR_EXTI_LINE_PVD)
252 
253 /**
254   * @brief Enable the PVD Extended Interrupt Falling Trigger.
255   * @retval None
256   */
257 #define __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE()   SET_BIT(EXTI->FTSR1, PWR_EXTI_LINE_PVD)
258 
259 /**
260   * @brief Disable the PVD Extended Interrupt Falling Trigger.
261   * @retval None
262   */
263 #define __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE()  CLEAR_BIT(EXTI->FTSR1, PWR_EXTI_LINE_PVD)
264 
265 /**
266   * @brief  Enable the PVD Extended Interrupt Rising & Falling Trigger.
267   * @retval None
268   */
269 #define __HAL_PWR_PVD_EXTI_ENABLE_RISING_FALLING_EDGE()  \
270   do {                                                   \
271     __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();             \
272     __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();            \
273   } while(0)
274 
275 /**
276   * @brief Disable the PVD Extended Interrupt Rising & Falling Trigger.
277   * @retval None
278   */
279 #define __HAL_PWR_PVD_EXTI_DISABLE_RISING_FALLING_EDGE()  \
280   do {                                                    \
281     __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();             \
282     __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();            \
283   } while(0)
284 
285 /**
286   * @brief  Generate a Software interrupt on selected EXTI line.
287   * @retval None
288   */
289 #define __HAL_PWR_PVD_EXTI_GENERATE_SWIT() SET_BIT(EXTI->SWIER1, PWR_EXTI_LINE_PVD)
290 
291 /**
292   * @brief Check whether the specified PVD EXTI Rising interrupt flag is set or not.
293   * @retval EXTI PVD Line Status.
294   */
295 
296 #define __HAL_PWR_PVD_EXTI_GET_RISING_FLAG() ((READ_BIT(EXTI->RPR1, PWR_EXTI_LINE_PVD)\
297                                                == PWR_EXTI_LINE_PVD) ? 1UL : 0UL)
298 
299 /**
300   * @brief Check whether the specified PVD EXTI Falling interrupt flag is set or not.
301   * @retval EXTI PVD Line Status.
302   */
303 
304 #define __HAL_PWR_PVD_EXTI_GET_FALLING_FLAG() ((READ_BIT(EXTI->FPR1, PWR_EXTI_LINE_PVD)\
305                                                 == PWR_EXTI_LINE_PVD) ? 1UL : 0UL)
306 
307 /**
308   * @brief  Clear the PVD EXTI Interrupt flag.
309   * @retval None.
310   */
311 #define __HAL_PWR_PVD_EXTI_CLEAR_FLAG()       \
312   do                                          \
313   {                                           \
314     WRITE_REG(EXTI->RPR1, PWR_EXTI_LINE_PVD); \
315     WRITE_REG(EXTI->FPR1, PWR_EXTI_LINE_PVD); \
316   } while(0)
317 
318 /**
319   * @}
320   */
321 
322 /* Private macros --------------------------------------------------------*/
323 /** @addtogroup  PWR_Private_Macros   PWR Private Macros
324   * @{
325   */
326 
327 #define IS_PWR_PVD_LEVEL(LEVEL) (((LEVEL) == PWR_PVDLEVEL_0) || ((LEVEL) == PWR_PVDLEVEL_1)|| \
328                                  ((LEVEL) == PWR_PVDLEVEL_2) || ((LEVEL) == PWR_PVDLEVEL_3)|| \
329                                  ((LEVEL) == PWR_PVDLEVEL_4) || ((LEVEL) == PWR_PVDLEVEL_5)|| \
330                                  ((LEVEL) == PWR_PVDLEVEL_6) || ((LEVEL) == PWR_PVDLEVEL_7))
331 
332 #define IS_PWR_PVD_MODE(MODE)  (((MODE) == PWR_PVD_MODE_NORMAL)              ||\
333                                 ((MODE) == PWR_PVD_MODE_IT_RISING)           ||\
334                                 ((MODE) == PWR_PVD_MODE_IT_FALLING)          ||\
335                                 ((MODE) == PWR_PVD_MODE_IT_RISING_FALLING)   ||\
336                                 ((MODE) == PWR_PVD_MODE_EVENT_RISING)        ||\
337                                 ((MODE) == PWR_PVD_MODE_EVENT_FALLING)       ||\
338                                 ((MODE) == PWR_PVD_MODE_EVENT_RISING_FALLING))
339 
340 #define IS_PWR_REGULATOR(REGULATOR)      (((REGULATOR) == PWR_MAINREGULATOR_ON) || \
341                                           ((REGULATOR) == PWR_LOWPOWERREGULATOR_ON))
342 
343 #define IS_PWR_SLEEP_ENTRY(ENTRY) (((ENTRY) == PWR_SLEEPENTRY_WFI) || ((ENTRY) == PWR_SLEEPENTRY_WFE))
344 
345 #define IS_PWR_STOP_ENTRY(ENTRY) (((ENTRY) == PWR_STOPENTRY_WFI) || ((ENTRY) == PWR_STOPENTRY_WFE) )
346 
347 /**
348   * @}
349   */
350 
351 /* Include PWR HAL Extended module */
352 #include "stm32u0xx_hal_pwr_ex.h"
353 
354 /* Exported functions --------------------------------------------------------*/
355 
356 /** @addtogroup PWR_Exported_Functions PWR Exported Functions
357   * @{
358   */
359 
360 /** @addtogroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
361   * @{
362   */
363 
364 /* Initialization and de-initialization functions *******************************/
365 void HAL_PWR_DeInit(void);
366 void HAL_PWR_EnableBkUpAccess(void);
367 void HAL_PWR_DisableBkUpAccess(void);
368 
369 /**
370   * @}
371   */
372 
373 /** @addtogroup PWR_Exported_Functions_Group2 Peripheral Control functions
374   * @{
375   */
376 
377 /* Peripheral Control functions  ************************************************/
378 HAL_StatusTypeDef HAL_PWR_ConfigPVD(const PWR_PVDTypeDef *sConfigPVD);
379 void HAL_PWR_EnablePVD(void);
380 void HAL_PWR_DisablePVD(void);
381 
382 /* WakeUp pins configuration functions ****************************************/
383 void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinPolarity);
384 void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx);
385 
386 /* Low Power modes configuration functions ************************************/
387 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry);
388 void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry);
389 void HAL_PWR_EnterSTANDBYMode(void);
390 void HAL_PWR_EnterSHUTDOWNMode(void);
391 void HAL_PWR_EnableSleepOnExit(void);
392 void HAL_PWR_DisableSleepOnExit(void);
393 void HAL_PWR_EnableSEVOnPend(void);
394 void HAL_PWR_DisableSEVOnPend(void);
395 
396 void HAL_PWR_PVDCallback(void);
397 
398 /**
399   * @}
400   */
401 
402 /**
403   * @}
404   */
405 
406 /**
407   * @}
408   */
409 
410 /**
411   * @}
412   */
413 
414 #ifdef __cplusplus
415 }
416 #endif
417 
418 #endif /* STM32U0xx_HAL_PWR_H */
419