2 ******************************************************************************
\r
3 * @file stm32f7xx_hal_pwr_ex.c
\r
4 * @author MCD Application Team
\r
5 * @brief Extended PWR HAL module driver.
\r
6 * This file provides firmware functions to manage the following
\r
7 * functionalities of PWR extension peripheral:
\r
8 * + Peripheral Extended features functions
\r
10 ******************************************************************************
\r
13 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
14 * All rights reserved.</center></h2>
\r
16 * This software component is licensed by ST under BSD 3-Clause license,
\r
17 * the "License"; You may not use this file except in compliance with the
\r
18 * License. You may obtain a copy of the License at:
\r
19 * opensource.org/licenses/BSD-3-Clause
\r
21 ******************************************************************************
\r
24 /* Includes ------------------------------------------------------------------*/
\r
25 #include "stm32f7xx_hal.h"
\r
27 /** @addtogroup STM32F7xx_HAL_Driver
\r
31 /** @defgroup PWREx PWREx
\r
32 * @brief PWR HAL module driver
\r
36 #ifdef HAL_PWR_MODULE_ENABLED
\r
38 /* Private typedef -----------------------------------------------------------*/
\r
39 /* Private define ------------------------------------------------------------*/
\r
40 /** @addtogroup PWREx_Private_Constants
\r
43 #define PWR_OVERDRIVE_TIMEOUT_VALUE 1000
\r
44 #define PWR_UDERDRIVE_TIMEOUT_VALUE 1000
\r
45 #define PWR_BKPREG_TIMEOUT_VALUE 1000
\r
46 #define PWR_VOSRDY_TIMEOUT_VALUE 1000
\r
51 /* Private macro -------------------------------------------------------------*/
\r
52 /* Private variables ---------------------------------------------------------*/
\r
53 /* Private function prototypes -----------------------------------------------*/
\r
54 /* Private functions ---------------------------------------------------------*/
\r
55 /** @defgroup PWREx_Exported_Functions PWREx Exported Functions
\r
59 /** @defgroup PWREx_Exported_Functions_Group1 Peripheral Extended features functions
\r
60 * @brief Peripheral Extended features functions
\r
64 ===============================================================================
\r
65 ##### Peripheral extended features functions #####
\r
66 ===============================================================================
\r
68 *** Main and Backup Regulators configuration ***
\r
69 ================================================
\r
71 (+) The backup domain includes 4 Kbytes of backup SRAM accessible only from
\r
72 the CPU, and address in 32-bit, 16-bit or 8-bit mode. Its content is
\r
73 retained even in Standby or VBAT mode when the low power backup regulator
\r
74 is enabled. It can be considered as an internal EEPROM when VBAT is
\r
75 always present. You can use the HAL_PWREx_EnableBkUpReg() function to
\r
76 enable the low power backup regulator.
\r
78 (+) When the backup domain is supplied by VDD (analog switch connected to VDD)
\r
79 the backup SRAM is powered from VDD which replaces the VBAT power supply to
\r
82 (+) The backup SRAM is not mass erased by a tamper event. It is read
\r
83 protected to prevent confidential data, such as cryptographic private
\r
84 key, from being accessed. The backup SRAM can be erased only through
\r
85 the Flash interface when a protection level change from level 1 to
\r
86 level 0 is requested.
\r
87 -@- Refer to the description of Read protection (RDP) in the Flash
\r
90 (+) The main internal regulator can be configured to have a tradeoff between
\r
91 performance and power consumption when the device does not operate at
\r
92 the maximum frequency. This is done through __HAL_PWR_MAINREGULATORMODE_CONFIG()
\r
93 macro which configure VOS bit in PWR_CR register
\r
95 Refer to the product datasheets for more details.
\r
97 *** FLASH Power Down configuration ****
\r
98 =======================================
\r
100 (+) By setting the FPDS bit in the PWR_CR register by using the
\r
101 HAL_PWREx_EnableFlashPowerDown() function, the Flash memory also enters power
\r
102 down mode when the device enters Stop mode. When the Flash memory
\r
103 is in power down mode, an additional startup delay is incurred when
\r
104 waking up from Stop mode.
\r
106 *** Over-Drive and Under-Drive configuration ****
\r
107 =================================================
\r
109 (+) In Run mode: the main regulator has 2 operating modes available:
\r
110 (++) Normal mode: The CPU and core logic operate at maximum frequency at a given
\r
111 voltage scaling (scale 1, scale 2 or scale 3)
\r
112 (++) Over-drive mode: This mode allows the CPU and the core logic to operate at a
\r
113 higher frequency than the normal mode for a given voltage scaling (scale 1,
\r
114 scale 2 or scale 3). This mode is enabled through HAL_PWREx_EnableOverDrive() function and
\r
115 disabled by HAL_PWREx_DisableOverDrive() function, to enter or exit from Over-drive mode please follow
\r
116 the sequence described in Reference manual.
\r
118 (+) In Stop mode: the main regulator or low power regulator supplies a low power
\r
119 voltage to the 1.2V domain, thus preserving the content of registers
\r
120 and internal SRAM. 2 operating modes are available:
\r
121 (++) Normal mode: the 1.2V domain is preserved in nominal leakage mode. This mode is only
\r
122 available when the main regulator or the low power regulator is used in Scale 3 or
\r
124 (++) Under-drive mode: the 1.2V domain is preserved in reduced leakage mode. This mode is only
\r
125 available when the main regulator or the low power regulator is in low voltage mode.
\r
132 * @brief Enables the Backup Regulator.
\r
133 * @retval HAL status
\r
135 HAL_StatusTypeDef HAL_PWREx_EnableBkUpReg(void)
\r
137 uint32_t tickstart = 0;
\r
139 /* Enable Backup regulator */
\r
140 PWR->CSR1 |= PWR_CSR1_BRE;
\r
142 /* Workaround for the following hardware bug: */
\r
143 /* Id 19: PWR : No STANDBY wake-up when Back-up RAM enabled (ref. Errata Sheet p23) */
\r
144 PWR->CSR1 |= PWR_CSR1_EIWUP;
\r
147 tickstart = HAL_GetTick();
\r
149 /* Wait till Backup regulator ready flag is set */
\r
150 while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) == RESET)
\r
152 if((HAL_GetTick() - tickstart ) > PWR_BKPREG_TIMEOUT_VALUE)
\r
154 return HAL_TIMEOUT;
\r
161 * @brief Disables the Backup Regulator.
\r
162 * @retval HAL status
\r
164 HAL_StatusTypeDef HAL_PWREx_DisableBkUpReg(void)
\r
166 uint32_t tickstart = 0;
\r
168 /* Disable Backup regulator */
\r
169 PWR->CSR1 &= (uint32_t)~((uint32_t)PWR_CSR1_BRE);
\r
171 /* Workaround for the following hardware bug: */
\r
172 /* Id 19: PWR : No STANDBY wake-up when Back-up RAM enabled (ref. Errata Sheet p23) */
\r
173 PWR->CSR1 |= PWR_CSR1_EIWUP;
\r
176 tickstart = HAL_GetTick();
\r
178 /* Wait till Backup regulator ready flag is set */
\r
179 while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) != RESET)
\r
181 if((HAL_GetTick() - tickstart ) > PWR_BKPREG_TIMEOUT_VALUE)
\r
183 return HAL_TIMEOUT;
\r
190 * @brief Enables the Flash Power Down in Stop mode.
\r
193 void HAL_PWREx_EnableFlashPowerDown(void)
\r
195 /* Enable the Flash Power Down */
\r
196 PWR->CR1 |= PWR_CR1_FPDS;
\r
200 * @brief Disables the Flash Power Down in Stop mode.
\r
203 void HAL_PWREx_DisableFlashPowerDown(void)
\r
205 /* Disable the Flash Power Down */
\r
206 PWR->CR1 &= (uint32_t)~((uint32_t)PWR_CR1_FPDS);
\r
210 * @brief Enables Main Regulator low voltage mode.
\r
213 void HAL_PWREx_EnableMainRegulatorLowVoltage(void)
\r
215 /* Enable Main regulator low voltage */
\r
216 PWR->CR1 |= PWR_CR1_MRUDS;
\r
220 * @brief Disables Main Regulator low voltage mode.
\r
223 void HAL_PWREx_DisableMainRegulatorLowVoltage(void)
\r
225 /* Disable Main regulator low voltage */
\r
226 PWR->CR1 &= (uint32_t)~((uint32_t)PWR_CR1_MRUDS);
\r
230 * @brief Enables Low Power Regulator low voltage mode.
\r
233 void HAL_PWREx_EnableLowRegulatorLowVoltage(void)
\r
235 /* Enable low power regulator */
\r
236 PWR->CR1 |= PWR_CR1_LPUDS;
\r
240 * @brief Disables Low Power Regulator low voltage mode.
\r
243 void HAL_PWREx_DisableLowRegulatorLowVoltage(void)
\r
245 /* Disable low power regulator */
\r
246 PWR->CR1 &= (uint32_t)~((uint32_t)PWR_CR1_LPUDS);
\r
250 * @brief Activates the Over-Drive mode.
\r
251 * @note This mode allows the CPU and the core logic to operate at a higher frequency
\r
252 * than the normal mode for a given voltage scaling (scale 1, scale 2 or scale 3).
\r
253 * @note It is recommended to enter or exit Over-drive mode when the application is not running
\r
254 * critical tasks and when the system clock source is either HSI or HSE.
\r
255 * During the Over-drive switch activation, no peripheral clocks should be enabled.
\r
256 * The peripheral clocks must be enabled once the Over-drive mode is activated.
\r
257 * @retval HAL status
\r
259 HAL_StatusTypeDef HAL_PWREx_EnableOverDrive(void)
\r
261 uint32_t tickstart = 0;
\r
263 __HAL_RCC_PWR_CLK_ENABLE();
\r
265 /* Enable the Over-drive to extend the clock frequency to 216 MHz */
\r
266 __HAL_PWR_OVERDRIVE_ENABLE();
\r
269 tickstart = HAL_GetTick();
\r
271 while(!__HAL_PWR_GET_FLAG(PWR_FLAG_ODRDY))
\r
273 if((HAL_GetTick() - tickstart ) > PWR_OVERDRIVE_TIMEOUT_VALUE)
\r
275 return HAL_TIMEOUT;
\r
279 /* Enable the Over-drive switch */
\r
280 __HAL_PWR_OVERDRIVESWITCHING_ENABLE();
\r
283 tickstart = HAL_GetTick();
\r
285 while(!__HAL_PWR_GET_FLAG(PWR_FLAG_ODSWRDY))
\r
287 if((HAL_GetTick() - tickstart ) > PWR_OVERDRIVE_TIMEOUT_VALUE)
\r
289 return HAL_TIMEOUT;
\r
296 * @brief Deactivates the Over-Drive mode.
\r
297 * @note This mode allows the CPU and the core logic to operate at a higher frequency
\r
298 * than the normal mode for a given voltage scaling (scale 1, scale 2 or scale 3).
\r
299 * @note It is recommended to enter or exit Over-drive mode when the application is not running
\r
300 * critical tasks and when the system clock source is either HSI or HSE.
\r
301 * During the Over-drive switch activation, no peripheral clocks should be enabled.
\r
302 * The peripheral clocks must be enabled once the Over-drive mode is activated.
\r
303 * @retval HAL status
\r
305 HAL_StatusTypeDef HAL_PWREx_DisableOverDrive(void)
\r
307 uint32_t tickstart = 0;
\r
309 __HAL_RCC_PWR_CLK_ENABLE();
\r
311 /* Disable the Over-drive switch */
\r
312 __HAL_PWR_OVERDRIVESWITCHING_DISABLE();
\r
315 tickstart = HAL_GetTick();
\r
317 while(__HAL_PWR_GET_FLAG(PWR_FLAG_ODSWRDY))
\r
319 if((HAL_GetTick() - tickstart ) > PWR_OVERDRIVE_TIMEOUT_VALUE)
\r
321 return HAL_TIMEOUT;
\r
325 /* Disable the Over-drive */
\r
326 __HAL_PWR_OVERDRIVE_DISABLE();
\r
329 tickstart = HAL_GetTick();
\r
331 while(__HAL_PWR_GET_FLAG(PWR_FLAG_ODRDY))
\r
333 if((HAL_GetTick() - tickstart ) > PWR_OVERDRIVE_TIMEOUT_VALUE)
\r
335 return HAL_TIMEOUT;
\r
343 * @brief Enters in Under-Drive STOP mode.
\r
345 * @note This mode can be selected only when the Under-Drive is already active
\r
347 * @note This mode is enabled only with STOP low power mode.
\r
348 * In this mode, the 1.2V domain is preserved in reduced leakage mode. This
\r
349 * mode is only available when the main regulator or the low power regulator
\r
350 * is in low voltage mode
\r
352 * @note If the Under-drive mode was enabled, it is automatically disabled after
\r
353 * exiting Stop mode.
\r
354 * When the voltage regulator operates in Under-drive mode, an additional
\r
355 * startup delay is induced when waking up from Stop mode.
\r
357 * @note In Stop mode, all I/O pins keep the same state as in Run mode.
\r
359 * @note When exiting Stop mode by issuing an interrupt or a wakeup event,
\r
360 * the HSI RC oscillator is selected as system clock.
\r
362 * @note When the voltage regulator operates in low power mode, an additional
\r
363 * startup delay is incurred when waking up from Stop mode.
\r
364 * By keeping the internal regulator ON during Stop mode, the consumption
\r
365 * is higher although the startup time is reduced.
\r
367 * @param Regulator specifies the regulator state in STOP mode.
\r
368 * This parameter can be one of the following values:
\r
369 * @arg PWR_MAINREGULATOR_UNDERDRIVE_ON: Main Regulator in under-drive mode
\r
370 * and Flash memory in power-down when the device is in Stop under-drive mode
\r
371 * @arg PWR_LOWPOWERREGULATOR_UNDERDRIVE_ON: Low Power Regulator in under-drive mode
\r
372 * and Flash memory in power-down when the device is in Stop under-drive mode
\r
373 * @param STOPEntry specifies if STOP mode in entered with WFI or WFE instruction.
\r
374 * This parameter can be one of the following values:
\r
375 * @arg PWR_SLEEPENTRY_WFI: enter STOP mode with WFI instruction
\r
376 * @arg PWR_SLEEPENTRY_WFE: enter STOP mode with WFE instruction
\r
379 HAL_StatusTypeDef HAL_PWREx_EnterUnderDriveSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
\r
381 uint32_t tempreg = 0;
\r
382 uint32_t tickstart = 0;
\r
384 /* Check the parameters */
\r
385 assert_param(IS_PWR_REGULATOR_UNDERDRIVE(Regulator));
\r
386 assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
\r
388 /* Enable Power ctrl clock */
\r
389 __HAL_RCC_PWR_CLK_ENABLE();
\r
390 /* Enable the Under-drive Mode ---------------------------------------------*/
\r
391 /* Clear Under-drive flag */
\r
392 __HAL_PWR_CLEAR_ODRUDR_FLAG();
\r
394 /* Enable the Under-drive */
\r
395 __HAL_PWR_UNDERDRIVE_ENABLE();
\r
398 tickstart = HAL_GetTick();
\r
400 /* Wait for UnderDrive mode is ready */
\r
401 while(__HAL_PWR_GET_FLAG(PWR_FLAG_UDRDY))
\r
403 if((HAL_GetTick() - tickstart ) > PWR_UDERDRIVE_TIMEOUT_VALUE)
\r
405 return HAL_TIMEOUT;
\r
409 /* Select the regulator state in STOP mode ---------------------------------*/
\r
410 tempreg = PWR->CR1;
\r
411 /* Clear PDDS, LPDS, MRLUDS and LPLUDS bits */
\r
412 tempreg &= (uint32_t)~(PWR_CR1_PDDS | PWR_CR1_LPDS | PWR_CR1_LPUDS | PWR_CR1_MRUDS);
\r
414 /* Set LPDS, MRLUDS and LPLUDS bits according to PWR_Regulator value */
\r
415 tempreg |= Regulator;
\r
417 /* Store the new value */
\r
418 PWR->CR1 = tempreg;
\r
420 /* Set SLEEPDEEP bit of Cortex System Control Register */
\r
421 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
\r
423 /* Select STOP mode entry --------------------------------------------------*/
\r
424 if(STOPEntry == PWR_SLEEPENTRY_WFI)
\r
426 /* Request Wait For Interrupt */
\r
431 /* Request Wait For Event */
\r
434 /* Reset SLEEPDEEP bit of Cortex System Control Register */
\r
435 SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
\r
441 * @brief Returns Voltage Scaling Range.
\r
442 * @retval VOS bit field (PWR_REGULATOR_VOLTAGE_SCALE1, PWR_REGULATOR_VOLTAGE_SCALE2 or
\r
443 * PWR_REGULATOR_VOLTAGE_SCALE3)PWR_REGULATOR_VOLTAGE_SCALE1
\r
445 uint32_t HAL_PWREx_GetVoltageRange(void)
\r
447 return (PWR->CR1 & PWR_CR1_VOS);
\r
451 * @brief Configures the main internal regulator output voltage.
\r
452 * @param VoltageScaling specifies the regulator output voltage to achieve
\r
453 * a tradeoff between performance and power consumption.
\r
454 * This parameter can be one of the following values:
\r
455 * @arg PWR_REGULATOR_VOLTAGE_SCALE1: Regulator voltage output range 1 mode,
\r
456 * typical output voltage at 1.4 V,
\r
457 * system frequency up to 216 MHz.
\r
458 * @arg PWR_REGULATOR_VOLTAGE_SCALE2: Regulator voltage output range 2 mode,
\r
459 * typical output voltage at 1.2 V,
\r
460 * system frequency up to 180 MHz.
\r
461 * @arg PWR_REGULATOR_VOLTAGE_SCALE3: Regulator voltage output range 2 mode,
\r
462 * typical output voltage at 1.00 V,
\r
463 * system frequency up to 151 MHz.
\r
464 * @note To update the system clock frequency(SYSCLK):
\r
465 * - Set the HSI or HSE as system clock frequency using the HAL_RCC_ClockConfig().
\r
466 * - Call the HAL_RCC_OscConfig() to configure the PLL.
\r
467 * - Call HAL_PWREx_ConfigVoltageScaling() API to adjust the voltage scale.
\r
468 * - Set the new system clock frequency using the HAL_RCC_ClockConfig().
\r
469 * @note The scale can be modified only when the HSI or HSE clock source is selected
\r
470 * as system clock source, otherwise the API returns HAL_ERROR.
\r
471 * @note When the PLL is OFF, the voltage scale 3 is automatically selected and the VOS bits
\r
472 * value in the PWR_CR1 register are not taken in account.
\r
473 * @note This API forces the PLL state ON to allow the possibility to configure the voltage scale 1 or 2.
\r
474 * @note The new voltage scale is active only when the PLL is ON.
\r
475 * @retval HAL Status
\r
477 HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling(uint32_t VoltageScaling)
\r
479 uint32_t tickstart = 0;
\r
481 assert_param(IS_PWR_REGULATOR_VOLTAGE(VoltageScaling));
\r
483 /* Enable Power ctrl clock */
\r
484 __HAL_RCC_PWR_CLK_ENABLE();
\r
486 /* Check if the PLL is used as system clock or not */
\r
487 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
\r
489 /* Disable the main PLL */
\r
490 __HAL_RCC_PLL_DISABLE();
\r
492 /* Get Start Tick */
\r
493 tickstart = HAL_GetTick();
\r
494 /* Wait till PLL is disabled */
\r
495 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
\r
497 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
\r
499 return HAL_TIMEOUT;
\r
504 __HAL_PWR_VOLTAGESCALING_CONFIG(VoltageScaling);
\r
506 /* Enable the main PLL */
\r
507 __HAL_RCC_PLL_ENABLE();
\r
509 /* Get Start Tick */
\r
510 tickstart = HAL_GetTick();
\r
511 /* Wait till PLL is ready */
\r
512 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
\r
514 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
\r
516 return HAL_TIMEOUT;
\r
520 /* Get Start Tick */
\r
521 tickstart = HAL_GetTick();
\r
522 while((__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY) == RESET))
\r
524 if((HAL_GetTick() - tickstart ) > PWR_VOSRDY_TIMEOUT_VALUE)
\r
526 return HAL_TIMEOUT;
\r
545 #endif /* HAL_PWR_MODULE_ENABLED */
\r
554 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r