2 ******************************************************************************
3 * @file stm32f7xx_hal_pwr.c
4 * @author MCD Application Team
5 * @brief PWR HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Power Controller (PWR) peripheral:
8 * + Initialization and de-initialization functions
9 * + Peripheral Control functions
11 ******************************************************************************
14 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
15 * All rights reserved.</center></h2>
17 * This software component is licensed by ST under BSD 3-Clause license,
18 * the "License"; You may not use this file except in compliance with the
19 * License. You may obtain a copy of the License at:
20 * opensource.org/licenses/BSD-3-Clause
22 ******************************************************************************
25 /* Includes ------------------------------------------------------------------*/
26 #include "stm32f7xx_hal.h"
28 /** @addtogroup STM32F7xx_HAL_Driver
33 * @brief PWR HAL module driver
37 #ifdef HAL_PWR_MODULE_ENABLED
39 /* Private typedef -----------------------------------------------------------*/
40 /* Private define ------------------------------------------------------------*/
41 /** @addtogroup PWR_Private_Constants
45 /** @defgroup PWR_PVD_Mode_Mask PWR PVD Mode Mask
48 #define PVD_MODE_IT ((uint32_t)0x00010000U)
49 #define PVD_MODE_EVT ((uint32_t)0x00020000U)
50 #define PVD_RISING_EDGE ((uint32_t)0x00000001U)
51 #define PVD_FALLING_EDGE ((uint32_t)0x00000002U)
56 /** @defgroup PWR_ENABLE_WUP_Mask PWR Enable WUP Mask
59 #define PWR_EWUP_MASK ((uint32_t)0x00003F00)
67 /* Private macro -------------------------------------------------------------*/
68 /* Private variables ---------------------------------------------------------*/
69 /* Private function prototypes -----------------------------------------------*/
70 /* Private functions ---------------------------------------------------------*/
72 /** @defgroup PWR_Exported_Functions PWR Exported Functions
76 /** @defgroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
77 * @brief Initialization and de-initialization functions
80 ===============================================================================
81 ##### Initialization and de-initialization functions #####
82 ===============================================================================
84 After reset, the backup domain (RTC registers, RTC backup data
85 registers and backup SRAM) is protected against possible unwanted
87 To enable access to the RTC Domain and RTC registers, proceed as follows:
88 (+) Enable the Power Controller (PWR) APB1 interface clock using the
89 __HAL_RCC_PWR_CLK_ENABLE() macro.
90 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
97 * @brief Deinitializes the HAL PWR peripheral registers to their default reset values.
100 void HAL_PWR_DeInit(void)
102 __HAL_RCC_PWR_FORCE_RESET();
103 __HAL_RCC_PWR_RELEASE_RESET();
107 * @brief Enables access to the backup domain (RTC registers, RTC
108 * backup data registers and backup SRAM).
109 * @note If the HSE divided by 2, 3, ..31 is used as the RTC clock, the
110 * Backup Domain Access should be kept enabled.
113 void HAL_PWR_EnableBkUpAccess(void)
115 /* Enable access to RTC and backup registers */
116 SET_BIT(PWR
->CR1
, PWR_CR1_DBP
);
120 * @brief Disables access to the backup domain (RTC registers, RTC
121 * backup data registers and backup SRAM).
122 * @note If the HSE divided by 2, 3, ..31 is used as the RTC clock, the
123 * Backup Domain Access should be kept enabled.
126 void HAL_PWR_DisableBkUpAccess(void)
128 /* Disable access to RTC and backup registers */
129 CLEAR_BIT(PWR
->CR1
, PWR_CR1_DBP
);
136 /** @defgroup PWR_Exported_Functions_Group2 Peripheral Control functions
137 * @brief Low Power modes configuration functions
141 ===============================================================================
142 ##### Peripheral Control functions #####
143 ===============================================================================
145 *** PVD configuration ***
146 =========================
148 (+) The PVD is used to monitor the VDD power supply by comparing it to a
149 threshold selected by the PVD Level (PLS[2:0] bits in the PWR_CR).
150 (+) A PVDO flag is available to indicate if VDD/VDDA is higher or lower
151 than the PVD threshold. This event is internally connected to the EXTI
152 line16 and can generate an interrupt if enabled. This is done through
153 __HAL_PWR_PVD_EXTI_ENABLE_IT() macro.
154 (+) The PVD is stopped in Standby mode.
156 *** Wake-up pin configuration ***
157 ================================
159 (+) Wake-up pin is used to wake up the system from Standby mode. This pin is
160 forced in input pull-down configuration and is active on rising edges.
161 (+) There are up to 6 Wake-up pin in the STM32F7 devices family
163 *** Low Power modes configuration ***
164 =====================================
166 The devices feature 3 low-power modes:
167 (+) Sleep mode: Cortex-M7 core stopped, peripherals kept running.
168 (+) Stop mode: all clocks are stopped, regulator running, regulator
170 (+) Standby mode: 1.2V domain powered off.
176 The Sleep mode is entered by using the HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI)
178 (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
179 (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
181 -@@- The Regulator parameter is not used for the STM32F7 family
182 and is kept as parameter just to maintain compatibility with the
183 lower power families (STM32L).
185 Any peripheral interrupt acknowledged by the nested vectored interrupt
186 controller (NVIC) can wake up the device from Sleep mode.
191 In Stop mode, all clocks in the 1.2V domain are stopped, the PLL, the HSI,
192 and the HSE RC oscillators are disabled. Internal SRAM and register contents
194 The voltage regulator can be configured either in normal or low-power mode.
195 To minimize the consumption In Stop mode, FLASH can be powered off before
196 entering the Stop mode using the HAL_PWREx_EnableFlashPowerDown() function.
197 It can be switched on again by software after exiting the Stop mode using
198 the HAL_PWREx_DisableFlashPowerDown() function.
201 The Stop mode is entered using the HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON)
203 (++) Main regulator ON.
204 (++) Low Power regulator ON.
206 Any EXTI Line (Internal or External) configured in Interrupt/Event mode.
212 The Standby mode allows to achieve the lowest power consumption. It is based
213 on the Cortex-M7 deep sleep mode, with the voltage regulator disabled.
214 The 1.2V domain is consequently powered off. The PLL, the HSI oscillator and
215 the HSE oscillator are also switched off. SRAM and register contents are lost
216 except for the RTC registers, RTC backup registers, backup SRAM and Standby
219 The voltage regulator is OFF.
222 (+++) The Standby mode is entered using the HAL_PWR_EnterSTANDBYMode() function.
224 (+++) WKUP pin rising or falling edge, RTC alarm (Alarm A and Alarm B), RTC
225 wakeup, tamper event, time stamp event, external reset in NRST pin, IWDG reset.
227 *** Auto-wakeup (AWU) from low-power mode ***
228 =============================================
231 (+) The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC
232 Wakeup event, a tamper event or a time-stamp event, without depending on
233 an external interrupt (Auto-wakeup mode).
235 (+) RTC auto-wakeup (AWU) from the Stop and Standby modes
237 (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to
238 configure the RTC to generate the RTC alarm using the HAL_RTC_SetAlarm_IT() function.
240 (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it
241 is necessary to configure the RTC to detect the tamper or time stamp event using the
242 HAL_RTCEx_SetTimeStamp_IT() or HAL_RTCEx_SetTamper_IT() functions.
244 (++) To wake up from the Stop mode with an RTC WakeUp event, it is necessary to
245 configure the RTC to generate the RTC WakeUp event using the HAL_RTCEx_SetWakeUpTimer_IT() function.
252 * @brief Configures the voltage threshold detected by the Power Voltage Detector(PVD).
253 * @param sConfigPVD pointer to an PWR_PVDTypeDef structure that contains the configuration
254 * information for the PVD.
255 * @note Refer to the electrical characteristics of your device datasheet for
256 * more details about the voltage threshold corresponding to each
260 void HAL_PWR_ConfigPVD(PWR_PVDTypeDef
*sConfigPVD
)
262 /* Check the parameters */
263 assert_param(IS_PWR_PVD_LEVEL(sConfigPVD
->PVDLevel
));
264 assert_param(IS_PWR_PVD_MODE(sConfigPVD
->Mode
));
266 /* Set PLS[7:5] bits according to PVDLevel value */
267 MODIFY_REG(PWR
->CR1
, PWR_CR1_PLS
, sConfigPVD
->PVDLevel
);
269 /* Clear any previous config. Keep it clear if no event or IT mode is selected */
270 __HAL_PWR_PVD_EXTI_DISABLE_EVENT();
271 __HAL_PWR_PVD_EXTI_DISABLE_IT();
272 __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();
273 __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();
275 /* Configure interrupt mode */
276 if((sConfigPVD
->Mode
& PVD_MODE_IT
) == PVD_MODE_IT
)
278 __HAL_PWR_PVD_EXTI_ENABLE_IT();
281 /* Configure event mode */
282 if((sConfigPVD
->Mode
& PVD_MODE_EVT
) == PVD_MODE_EVT
)
284 __HAL_PWR_PVD_EXTI_ENABLE_EVENT();
287 /* Configure the edge */
288 if((sConfigPVD
->Mode
& PVD_RISING_EDGE
) == PVD_RISING_EDGE
)
290 __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
293 if((sConfigPVD
->Mode
& PVD_FALLING_EDGE
) == PVD_FALLING_EDGE
)
295 __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();
300 * @brief Enables the Power Voltage Detector(PVD).
303 void HAL_PWR_EnablePVD(void)
305 /* Enable the power voltage detector */
306 SET_BIT(PWR
->CR1
, PWR_CR1_PVDE
);
310 * @brief Disables the Power Voltage Detector(PVD).
313 void HAL_PWR_DisablePVD(void)
315 /* Disable the power voltage detector */
316 CLEAR_BIT(PWR
->CR1
, PWR_CR1_PVDE
);
320 * @brief Enable the WakeUp PINx functionality.
321 * @param WakeUpPinPolarity Specifies which Wake-Up pin to enable.
322 * This parameter can be one of the following legacy values, which sets the default polarity:
323 * detection on high level (rising edge):
324 * @arg PWR_WAKEUP_PIN1, PWR_WAKEUP_PIN2, PWR_WAKEUP_PIN3, PWR_WAKEUP_PIN4, PWR_WAKEUP_PIN5, PWR_WAKEUP_PIN6
325 * or one of the following value where the user can explicitly states the enabled pin and
326 * the chosen polarity
327 * @arg PWR_WAKEUP_PIN1_HIGH or PWR_WAKEUP_PIN1_LOW
328 * @arg PWR_WAKEUP_PIN2_HIGH or PWR_WAKEUP_PIN2_LOW
329 * @arg PWR_WAKEUP_PIN3_HIGH or PWR_WAKEUP_PIN3_LOW
330 * @arg PWR_WAKEUP_PIN4_HIGH or PWR_WAKEUP_PIN4_LOW
331 * @arg PWR_WAKEUP_PIN5_HIGH or PWR_WAKEUP_PIN5_LOW
332 * @arg PWR_WAKEUP_PIN6_HIGH or PWR_WAKEUP_PIN6_LOW
333 * @note PWR_WAKEUP_PINx and PWR_WAKEUP_PINx_HIGH are equivalent.
336 void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinPolarity
)
338 assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinPolarity
));
340 /* Enable wake-up pin */
341 SET_BIT(PWR
->CSR2
, (PWR_EWUP_MASK
& WakeUpPinPolarity
));
343 /* Specifies the Wake-Up pin polarity for the event detection
344 (rising or falling edge) */
345 MODIFY_REG(PWR
->CR2
, (PWR_EWUP_MASK
& WakeUpPinPolarity
), (WakeUpPinPolarity
>> 0x06));
349 * @brief Disables the WakeUp PINx functionality.
350 * @param WakeUpPinx Specifies the Power Wake-Up pin to disable.
351 * This parameter can be one of the following values:
352 * @arg PWR_WAKEUP_PIN1
353 * @arg PWR_WAKEUP_PIN2
354 * @arg PWR_WAKEUP_PIN3
355 * @arg PWR_WAKEUP_PIN4
356 * @arg PWR_WAKEUP_PIN5
357 * @arg PWR_WAKEUP_PIN6
360 void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx
)
362 assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx
));
364 CLEAR_BIT(PWR
->CSR2
, WakeUpPinx
);
368 * @brief Enters Sleep mode.
370 * @note In Sleep mode, all I/O pins keep the same state as in Run mode.
372 * @note In Sleep mode, the systick is stopped to avoid exit from this mode with
373 * systick interrupt when used as time base for Timeout
375 * @param Regulator Specifies the regulator state in SLEEP mode.
376 * This parameter can be one of the following values:
377 * @arg PWR_MAINREGULATOR_ON: SLEEP mode with regulator ON
378 * @arg PWR_LOWPOWERREGULATOR_ON: SLEEP mode with low power regulator ON
379 * @note This parameter is not used for the STM32F7 family and is kept as parameter
380 * just to maintain compatibility with the lower power families.
381 * @param SLEEPEntry Specifies if SLEEP mode in entered with WFI or WFE instruction.
382 * This parameter can be one of the following values:
383 * @arg PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
384 * @arg PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
387 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator
, uint8_t SLEEPEntry
)
391 /* Check the parameters */
392 assert_param(IS_PWR_REGULATOR(Regulator
));
393 assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry
));
395 /* Clear SLEEPDEEP bit of Cortex System Control Register */
396 CLEAR_BIT(SCB
->SCR
, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk
));
398 /* Ensure that all instructions done before entering SLEEP mode */
402 /* Select SLEEP mode entry -------------------------------------------------*/
403 if(SLEEPEntry
== PWR_SLEEPENTRY_WFI
)
405 /* Request Wait For Interrupt */
410 /* Request Wait For Event */
418 * @brief Enters Stop mode.
419 * @note In Stop mode, all I/O pins keep the same state as in Run mode.
420 * @note When exiting Stop mode by issuing an interrupt or a wakeup event,
421 * the HSI RC oscillator is selected as system clock.
422 * @note When the voltage regulator operates in low power mode, an additional
423 * startup delay is incurred when waking up from Stop mode.
424 * By keeping the internal regulator ON during Stop mode, the consumption
425 * is higher although the startup time is reduced.
426 * @param Regulator Specifies the regulator state in Stop mode.
427 * This parameter can be one of the following values:
428 * @arg PWR_MAINREGULATOR_ON: Stop mode with regulator ON
429 * @arg PWR_LOWPOWERREGULATOR_ON: Stop mode with low power regulator ON
430 * @param STOPEntry Specifies if Stop mode in entered with WFI or WFE instruction.
431 * This parameter can be one of the following values:
432 * @arg PWR_STOPENTRY_WFI: Enter Stop mode with WFI instruction
433 * @arg PWR_STOPENTRY_WFE: Enter Stop mode with WFE instruction
436 void HAL_PWR_EnterSTOPMode(uint32_t Regulator
, uint8_t STOPEntry
)
440 /* Check the parameters */
441 assert_param(IS_PWR_REGULATOR(Regulator
));
442 assert_param(IS_PWR_STOP_ENTRY(STOPEntry
));
444 /* Select the regulator state in Stop mode ---------------------------------*/
446 /* Clear PDDS and LPDS bits */
447 tmpreg
&= (uint32_t)~(PWR_CR1_PDDS
| PWR_CR1_LPDS
);
449 /* Set LPDS, MRLVDS and LPLVDS bits according to Regulator value */
452 /* Store the new value */
455 /* Set SLEEPDEEP bit of Cortex System Control Register */
456 SCB
->SCR
|= SCB_SCR_SLEEPDEEP_Msk
;
458 /* Ensure that all instructions done before entering STOP mode */
462 /* Select Stop mode entry --------------------------------------------------*/
463 if(STOPEntry
== PWR_STOPENTRY_WFI
)
465 /* Request Wait For Interrupt */
470 /* Request Wait For Event */
475 /* Reset SLEEPDEEP bit of Cortex System Control Register */
476 SCB
->SCR
&= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk
);
480 * @brief Enters Standby mode.
481 * @note In Standby mode, all I/O pins are high impedance except for:
482 * - Reset pad (still available)
483 * - RTC_AF1 pin (PC13) if configured for tamper, time-stamp, RTC
484 * Alarm out, or RTC clock calibration out.
485 * - RTC_AF2 pin (PI8) if configured for tamper or time-stamp.
486 * - WKUP pins if enabled.
489 void HAL_PWR_EnterSTANDBYMode(void)
491 /* Select Standby mode */
492 PWR
->CR1
|= PWR_CR1_PDDS
;
494 /* Set SLEEPDEEP bit of Cortex System Control Register */
495 SCB
->SCR
|= SCB_SCR_SLEEPDEEP_Msk
;
497 /* This option is used to ensure that store operations are completed */
498 #if defined ( __CC_ARM)
501 /* Request Wait For Interrupt */
506 * @brief This function handles the PWR PVD interrupt request.
507 * @note This API should be called under the PVD_IRQHandler().
510 void HAL_PWR_PVD_IRQHandler(void)
512 /* Check PWR Exti flag */
513 if(__HAL_PWR_PVD_EXTI_GET_FLAG() != RESET
)
515 /* PWR PVD interrupt user callback */
516 HAL_PWR_PVDCallback();
518 /* Clear PWR Exti pending bit */
519 __HAL_PWR_PVD_EXTI_CLEAR_FLAG();
524 * @brief PWR PVD interrupt callback
527 __weak
void HAL_PWR_PVDCallback(void)
529 /* NOTE : This function Should not be modified, when the callback is needed,
530 the HAL_PWR_PVDCallback could be implemented in the user file
535 * @brief Indicates Sleep-On-Exit when returning from Handler mode to Thread mode.
536 * @note Set SLEEPONEXIT bit of SCR register. When this bit is set, the processor
537 * re-enters SLEEP mode when an interruption handling is over.
538 * Setting this bit is useful when the processor is expected to run only on
539 * interruptions handling.
542 void HAL_PWR_EnableSleepOnExit(void)
544 /* Set SLEEPONEXIT bit of Cortex System Control Register */
545 SET_BIT(SCB
->SCR
, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk
));
549 * @brief Disables Sleep-On-Exit feature when returning from Handler mode to Thread mode.
550 * @note Clears SLEEPONEXIT bit of SCR register. When this bit is set, the processor
551 * re-enters SLEEP mode when an interruption handling is over.
554 void HAL_PWR_DisableSleepOnExit(void)
556 /* Clear SLEEPONEXIT bit of Cortex System Control Register */
557 CLEAR_BIT(SCB
->SCR
, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk
));
561 * @brief Enables CORTEX M4 SEVONPEND bit.
562 * @note Sets SEVONPEND bit of SCR register. When this bit is set, this causes
563 * WFE to wake up when an interrupt moves from inactive to pended.
566 void HAL_PWR_EnableSEVOnPend(void)
568 /* Set SEVONPEND bit of Cortex System Control Register */
569 SET_BIT(SCB
->SCR
, ((uint32_t)SCB_SCR_SEVONPEND_Msk
));
573 * @brief Disables CORTEX M4 SEVONPEND bit.
574 * @note Clears SEVONPEND bit of SCR register. When this bit is set, this causes
575 * WFE to wake up when an interrupt moves from inactive to pended.
578 void HAL_PWR_DisableSEVOnPend(void)
580 /* Clear SEVONPEND bit of Cortex System Control Register */
581 CLEAR_BIT(SCB
->SCR
, ((uint32_t)SCB_SCR_SEVONPEND_Msk
));
592 #endif /* HAL_PWR_MODULE_ENABLED */
601 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/