You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f4xx_hal_pwr_ex.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_pwr_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended PWR HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of PWR extension peripheral:
  8. * + Peripheral Extended features functions
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  14. * All rights reserved.</center></h2>
  15. *
  16. * This software component is licensed by ST under BSD 3-Clause license,
  17. * the "License"; You may not use this file except in compliance with the
  18. * License. You may obtain a copy of the License at:
  19. * opensource.org/licenses/BSD-3-Clause
  20. *
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32f4xx_hal.h"
  25. /** @addtogroup STM32F4xx_HAL_Driver
  26. * @{
  27. */
  28. /** @defgroup PWREx PWREx
  29. * @brief PWR HAL module driver
  30. * @{
  31. */
  32. #ifdef HAL_PWR_MODULE_ENABLED
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. /** @addtogroup PWREx_Private_Constants
  36. * @{
  37. */
  38. #define PWR_OVERDRIVE_TIMEOUT_VALUE 1000U
  39. #define PWR_UDERDRIVE_TIMEOUT_VALUE 1000U
  40. #define PWR_BKPREG_TIMEOUT_VALUE 1000U
  41. #define PWR_VOSRDY_TIMEOUT_VALUE 1000U
  42. /**
  43. * @}
  44. */
  45. /* Private macro -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private function prototypes -----------------------------------------------*/
  48. /* Private functions ---------------------------------------------------------*/
  49. /** @defgroup PWREx_Exported_Functions PWREx Exported Functions
  50. * @{
  51. */
  52. /** @defgroup PWREx_Exported_Functions_Group1 Peripheral Extended features functions
  53. * @brief Peripheral Extended features functions
  54. *
  55. @verbatim
  56. ===============================================================================
  57. ##### Peripheral extended features functions #####
  58. ===============================================================================
  59. *** Main and Backup Regulators configuration ***
  60. ================================================
  61. [..]
  62. (+) The backup domain includes 4 Kbytes of backup SRAM accessible only from
  63. the CPU, and address in 32-bit, 16-bit or 8-bit mode. Its content is
  64. retained even in Standby or VBAT mode when the low power backup regulator
  65. is enabled. It can be considered as an internal EEPROM when VBAT is
  66. always present. You can use the HAL_PWREx_EnableBkUpReg() function to
  67. enable the low power backup regulator.
  68. (+) When the backup domain is supplied by VDD (analog switch connected to VDD)
  69. the backup SRAM is powered from VDD which replaces the VBAT power supply to
  70. save battery life.
  71. (+) The backup SRAM is not mass erased by a tamper event. It is read
  72. protected to prevent confidential data, such as cryptographic private
  73. key, from being accessed. The backup SRAM can be erased only through
  74. the Flash interface when a protection level change from level 1 to
  75. level 0 is requested.
  76. -@- Refer to the description of Read protection (RDP) in the Flash
  77. programming manual.
  78. (+) The main internal regulator can be configured to have a tradeoff between
  79. performance and power consumption when the device does not operate at
  80. the maximum frequency. This is done through __HAL_PWR_MAINREGULATORMODE_CONFIG()
  81. macro which configure VOS bit in PWR_CR register
  82. Refer to the product datasheets for more details.
  83. *** FLASH Power Down configuration ****
  84. =======================================
  85. [..]
  86. (+) By setting the FPDS bit in the PWR_CR register by using the
  87. HAL_PWREx_EnableFlashPowerDown() function, the Flash memory also enters power
  88. down mode when the device enters Stop mode. When the Flash memory
  89. is in power down mode, an additional startup delay is incurred when
  90. waking up from Stop mode.
  91. (+) For STM32F42xxx/43xxx/446xx/469xx/479xx Devices, the scale can be modified only when the PLL
  92. is OFF and the HSI or HSE clock source is selected as system clock.
  93. The new value programmed is active only when the PLL is ON.
  94. When the PLL is OFF, the voltage scale 3 is automatically selected.
  95. Refer to the datasheets for more details.
  96. *** Over-Drive and Under-Drive configuration ****
  97. =================================================
  98. [..]
  99. (+) For STM32F42xxx/43xxx/446xx/469xx/479xx Devices, in Run mode: the main regulator has
  100. 2 operating modes available:
  101. (++) Normal mode: The CPU and core logic operate at maximum frequency at a given
  102. voltage scaling (scale 1, scale 2 or scale 3)
  103. (++) Over-drive mode: This mode allows the CPU and the core logic to operate at a
  104. higher frequency than the normal mode for a given voltage scaling (scale 1,
  105. scale 2 or scale 3). This mode is enabled through HAL_PWREx_EnableOverDrive() function and
  106. disabled by HAL_PWREx_DisableOverDrive() function, to enter or exit from Over-drive mode please follow
  107. the sequence described in Reference manual.
  108. (+) For STM32F42xxx/43xxx/446xx/469xx/479xx Devices, in Stop mode: the main regulator or low power regulator
  109. supplies a low power voltage to the 1.2V domain, thus preserving the content of registers
  110. and internal SRAM. 2 operating modes are available:
  111. (++) Normal mode: the 1.2V domain is preserved in nominal leakage mode. This mode is only
  112. available when the main regulator or the low power regulator is used in Scale 3 or
  113. low voltage mode.
  114. (++) Under-drive mode: the 1.2V domain is preserved in reduced leakage mode. This mode is only
  115. available when the main regulator or the low power regulator is in low voltage mode.
  116. @endverbatim
  117. * @{
  118. */
  119. /**
  120. * @brief Enables the Backup Regulator.
  121. * @retval HAL status
  122. */
  123. HAL_StatusTypeDef HAL_PWREx_EnableBkUpReg(void)
  124. {
  125. uint32_t tickstart = 0U;
  126. *(__IO uint32_t *) CSR_BRE_BB = (uint32_t)ENABLE;
  127. /* Get tick */
  128. tickstart = HAL_GetTick();
  129. /* Wait till Backup regulator ready flag is set */
  130. while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) == RESET)
  131. {
  132. if((HAL_GetTick() - tickstart ) > PWR_BKPREG_TIMEOUT_VALUE)
  133. {
  134. return HAL_TIMEOUT;
  135. }
  136. }
  137. return HAL_OK;
  138. }
  139. /**
  140. * @brief Disables the Backup Regulator.
  141. * @retval HAL status
  142. */
  143. HAL_StatusTypeDef HAL_PWREx_DisableBkUpReg(void)
  144. {
  145. uint32_t tickstart = 0U;
  146. *(__IO uint32_t *) CSR_BRE_BB = (uint32_t)DISABLE;
  147. /* Get tick */
  148. tickstart = HAL_GetTick();
  149. /* Wait till Backup regulator ready flag is set */
  150. while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) != RESET)
  151. {
  152. if((HAL_GetTick() - tickstart ) > PWR_BKPREG_TIMEOUT_VALUE)
  153. {
  154. return HAL_TIMEOUT;
  155. }
  156. }
  157. return HAL_OK;
  158. }
  159. /**
  160. * @brief Enables the Flash Power Down in Stop mode.
  161. * @retval None
  162. */
  163. void HAL_PWREx_EnableFlashPowerDown(void)
  164. {
  165. *(__IO uint32_t *) CR_FPDS_BB = (uint32_t)ENABLE;
  166. }
  167. /**
  168. * @brief Disables the Flash Power Down in Stop mode.
  169. * @retval None
  170. */
  171. void HAL_PWREx_DisableFlashPowerDown(void)
  172. {
  173. *(__IO uint32_t *) CR_FPDS_BB = (uint32_t)DISABLE;
  174. }
  175. /**
  176. * @brief Return Voltage Scaling Range.
  177. * @retval The configured scale for the regulator voltage(VOS bit field).
  178. * The returned value can be one of the following:
  179. * - @arg PWR_REGULATOR_VOLTAGE_SCALE1: Regulator voltage output Scale 1 mode
  180. * - @arg PWR_REGULATOR_VOLTAGE_SCALE2: Regulator voltage output Scale 2 mode
  181. * - @arg PWR_REGULATOR_VOLTAGE_SCALE3: Regulator voltage output Scale 3 mode
  182. */
  183. uint32_t HAL_PWREx_GetVoltageRange(void)
  184. {
  185. return (PWR->CR & PWR_CR_VOS);
  186. }
  187. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)
  188. /**
  189. * @brief Configures the main internal regulator output voltage.
  190. * @param VoltageScaling specifies the regulator output voltage to achieve
  191. * a tradeoff between performance and power consumption.
  192. * This parameter can be one of the following values:
  193. * @arg PWR_REGULATOR_VOLTAGE_SCALE1: Regulator voltage output range 1 mode,
  194. * the maximum value of fHCLK = 168 MHz.
  195. * @arg PWR_REGULATOR_VOLTAGE_SCALE2: Regulator voltage output range 2 mode,
  196. * the maximum value of fHCLK = 144 MHz.
  197. * @note When moving from Range 1 to Range 2, the system frequency must be decreased to
  198. * a value below 144 MHz before calling HAL_PWREx_ConfigVoltageScaling() API.
  199. * When moving from Range 2 to Range 1, the system frequency can be increased to
  200. * a value up to 168 MHz after calling HAL_PWREx_ConfigVoltageScaling() API.
  201. * @retval HAL Status
  202. */
  203. HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling(uint32_t VoltageScaling)
  204. {
  205. uint32_t tickstart = 0U;
  206. assert_param(IS_PWR_VOLTAGE_SCALING_RANGE(VoltageScaling));
  207. /* Enable PWR RCC Clock Peripheral */
  208. __HAL_RCC_PWR_CLK_ENABLE();
  209. /* Set Range */
  210. __HAL_PWR_VOLTAGESCALING_CONFIG(VoltageScaling);
  211. /* Get Start Tick*/
  212. tickstart = HAL_GetTick();
  213. while((__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY) == RESET))
  214. {
  215. if((HAL_GetTick() - tickstart ) > PWR_VOSRDY_TIMEOUT_VALUE)
  216. {
  217. return HAL_TIMEOUT;
  218. }
  219. }
  220. return HAL_OK;
  221. }
  222. #elif defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
  223. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || \
  224. defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F469xx) || \
  225. defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || \
  226. defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  227. /**
  228. * @brief Configures the main internal regulator output voltage.
  229. * @param VoltageScaling specifies the regulator output voltage to achieve
  230. * a tradeoff between performance and power consumption.
  231. * This parameter can be one of the following values:
  232. * @arg PWR_REGULATOR_VOLTAGE_SCALE1: Regulator voltage output range 1 mode,
  233. * the maximum value of fHCLK is 168 MHz. It can be extended to
  234. * 180 MHz by activating the over-drive mode.
  235. * @arg PWR_REGULATOR_VOLTAGE_SCALE2: Regulator voltage output range 2 mode,
  236. * the maximum value of fHCLK is 144 MHz. It can be extended to,
  237. * 168 MHz by activating the over-drive mode.
  238. * @arg PWR_REGULATOR_VOLTAGE_SCALE3: Regulator voltage output range 3 mode,
  239. * the maximum value of fHCLK is 120 MHz.
  240. * @note To update the system clock frequency(SYSCLK):
  241. * - Set the HSI or HSE as system clock frequency using the HAL_RCC_ClockConfig().
  242. * - Call the HAL_RCC_OscConfig() to configure the PLL.
  243. * - Call HAL_PWREx_ConfigVoltageScaling() API to adjust the voltage scale.
  244. * - Set the new system clock frequency using the HAL_RCC_ClockConfig().
  245. * @note The scale can be modified only when the HSI or HSE clock source is selected
  246. * as system clock source, otherwise the API returns HAL_ERROR.
  247. * @note When the PLL is OFF, the voltage scale 3 is automatically selected and the VOS bits
  248. * value in the PWR_CR1 register are not taken in account.
  249. * @note This API forces the PLL state ON to allow the possibility to configure the voltage scale 1 or 2.
  250. * @note The new voltage scale is active only when the PLL is ON.
  251. * @retval HAL Status
  252. */
  253. HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling(uint32_t VoltageScaling)
  254. {
  255. uint32_t tickstart = 0U;
  256. assert_param(IS_PWR_VOLTAGE_SCALING_RANGE(VoltageScaling));
  257. /* Enable PWR RCC Clock Peripheral */
  258. __HAL_RCC_PWR_CLK_ENABLE();
  259. /* Check if the PLL is used as system clock or not */
  260. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  261. {
  262. /* Disable the main PLL */
  263. __HAL_RCC_PLL_DISABLE();
  264. /* Get Start Tick */
  265. tickstart = HAL_GetTick();
  266. /* Wait till PLL is disabled */
  267. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  268. {
  269. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  270. {
  271. return HAL_TIMEOUT;
  272. }
  273. }
  274. /* Set Range */
  275. __HAL_PWR_VOLTAGESCALING_CONFIG(VoltageScaling);
  276. /* Enable the main PLL */
  277. __HAL_RCC_PLL_ENABLE();
  278. /* Get Start Tick */
  279. tickstart = HAL_GetTick();
  280. /* Wait till PLL is ready */
  281. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  282. {
  283. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  284. {
  285. return HAL_TIMEOUT;
  286. }
  287. }
  288. /* Get Start Tick */
  289. tickstart = HAL_GetTick();
  290. while((__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY) == RESET))
  291. {
  292. if((HAL_GetTick() - tickstart ) > PWR_VOSRDY_TIMEOUT_VALUE)
  293. {
  294. return HAL_TIMEOUT;
  295. }
  296. }
  297. }
  298. else
  299. {
  300. return HAL_ERROR;
  301. }
  302. return HAL_OK;
  303. }
  304. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx */
  305. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
  306. defined(STM32F411xE) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) ||\
  307. defined(STM32F413xx) || defined(STM32F423xx)
  308. /**
  309. * @brief Enables Main Regulator low voltage mode.
  310. * @note This mode is only available for STM32F401xx/STM32F410xx/STM32F411xx/STM32F412Zx/STM32F412Rx/STM32F412Vx/STM32F412Cx/
  311. * STM32F413xx/STM32F423xx devices.
  312. * @retval None
  313. */
  314. void HAL_PWREx_EnableMainRegulatorLowVoltage(void)
  315. {
  316. *(__IO uint32_t *) CR_MRLVDS_BB = (uint32_t)ENABLE;
  317. }
  318. /**
  319. * @brief Disables Main Regulator low voltage mode.
  320. * @note This mode is only available for STM32F401xx/STM32F410xx/STM32F411xx/STM32F412Zx/STM32F412Rx/STM32F412Vx/STM32F412Cx/
  321. * STM32F413xx/STM32F423xxdevices.
  322. * @retval None
  323. */
  324. void HAL_PWREx_DisableMainRegulatorLowVoltage(void)
  325. {
  326. *(__IO uint32_t *) CR_MRLVDS_BB = (uint32_t)DISABLE;
  327. }
  328. /**
  329. * @brief Enables Low Power Regulator low voltage mode.
  330. * @note This mode is only available for STM32F401xx/STM32F410xx/STM32F411xx/STM32F412Zx/STM32F412Rx/STM32F412Vx/STM32F412Cx/
  331. * STM32F413xx/STM32F423xx devices.
  332. * @retval None
  333. */
  334. void HAL_PWREx_EnableLowRegulatorLowVoltage(void)
  335. {
  336. *(__IO uint32_t *) CR_LPLVDS_BB = (uint32_t)ENABLE;
  337. }
  338. /**
  339. * @brief Disables Low Power Regulator low voltage mode.
  340. * @note This mode is only available for STM32F401xx/STM32F410xx/STM32F411xx/STM32F412Zx/STM32F412Rx/STM32F412Vx/STM32F412Cx/
  341. * STM32F413xx/STM32F423xx devices.
  342. * @retval None
  343. */
  344. void HAL_PWREx_DisableLowRegulatorLowVoltage(void)
  345. {
  346. *(__IO uint32_t *) CR_LPLVDS_BB = (uint32_t)DISABLE;
  347. }
  348. #endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F412Zx || STM32F412Rx || STM32F412Vx || STM32F412Cx ||
  349. STM32F413xx || STM32F423xx */
  350. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  351. defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  352. /**
  353. * @brief Activates the Over-Drive mode.
  354. * @note This function can be used only for STM32F42xx/STM32F43xx/STM32F446xx/STM32F469xx/STM32F479xx devices.
  355. * This mode allows the CPU and the core logic to operate at a higher frequency
  356. * than the normal mode for a given voltage scaling (scale 1, scale 2 or scale 3).
  357. * @note It is recommended to enter or exit Over-drive mode when the application is not running
  358. * critical tasks and when the system clock source is either HSI or HSE.
  359. * During the Over-drive switch activation, no peripheral clocks should be enabled.
  360. * The peripheral clocks must be enabled once the Over-drive mode is activated.
  361. * @retval HAL status
  362. */
  363. HAL_StatusTypeDef HAL_PWREx_EnableOverDrive(void)
  364. {
  365. uint32_t tickstart = 0U;
  366. __HAL_RCC_PWR_CLK_ENABLE();
  367. /* Enable the Over-drive to extend the clock frequency to 180 Mhz */
  368. __HAL_PWR_OVERDRIVE_ENABLE();
  369. /* Get tick */
  370. tickstart = HAL_GetTick();
  371. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_ODRDY))
  372. {
  373. if((HAL_GetTick() - tickstart) > PWR_OVERDRIVE_TIMEOUT_VALUE)
  374. {
  375. return HAL_TIMEOUT;
  376. }
  377. }
  378. /* Enable the Over-drive switch */
  379. __HAL_PWR_OVERDRIVESWITCHING_ENABLE();
  380. /* Get tick */
  381. tickstart = HAL_GetTick();
  382. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_ODSWRDY))
  383. {
  384. if((HAL_GetTick() - tickstart ) > PWR_OVERDRIVE_TIMEOUT_VALUE)
  385. {
  386. return HAL_TIMEOUT;
  387. }
  388. }
  389. return HAL_OK;
  390. }
  391. /**
  392. * @brief Deactivates the Over-Drive mode.
  393. * @note This function can be used only for STM32F42xx/STM32F43xx/STM32F446xx/STM32F469xx/STM32F479xx devices.
  394. * This mode allows the CPU and the core logic to operate at a higher frequency
  395. * than the normal mode for a given voltage scaling (scale 1, scale 2 or scale 3).
  396. * @note It is recommended to enter or exit Over-drive mode when the application is not running
  397. * critical tasks and when the system clock source is either HSI or HSE.
  398. * During the Over-drive switch activation, no peripheral clocks should be enabled.
  399. * The peripheral clocks must be enabled once the Over-drive mode is activated.
  400. * @retval HAL status
  401. */
  402. HAL_StatusTypeDef HAL_PWREx_DisableOverDrive(void)
  403. {
  404. uint32_t tickstart = 0U;
  405. __HAL_RCC_PWR_CLK_ENABLE();
  406. /* Disable the Over-drive switch */
  407. __HAL_PWR_OVERDRIVESWITCHING_DISABLE();
  408. /* Get tick */
  409. tickstart = HAL_GetTick();
  410. while(__HAL_PWR_GET_FLAG(PWR_FLAG_ODSWRDY))
  411. {
  412. if((HAL_GetTick() - tickstart) > PWR_OVERDRIVE_TIMEOUT_VALUE)
  413. {
  414. return HAL_TIMEOUT;
  415. }
  416. }
  417. /* Disable the Over-drive */
  418. __HAL_PWR_OVERDRIVE_DISABLE();
  419. /* Get tick */
  420. tickstart = HAL_GetTick();
  421. while(__HAL_PWR_GET_FLAG(PWR_FLAG_ODRDY))
  422. {
  423. if((HAL_GetTick() - tickstart) > PWR_OVERDRIVE_TIMEOUT_VALUE)
  424. {
  425. return HAL_TIMEOUT;
  426. }
  427. }
  428. return HAL_OK;
  429. }
  430. /**
  431. * @brief Enters in Under-Drive STOP mode.
  432. *
  433. * @note This mode is only available for STM32F42xxx/STM32F43xxx/STM32F446xx/STM32F469xx/STM32F479xx devices.
  434. *
  435. * @note This mode can be selected only when the Under-Drive is already active
  436. *
  437. * @note This mode is enabled only with STOP low power mode.
  438. * In this mode, the 1.2V domain is preserved in reduced leakage mode. This
  439. * mode is only available when the main regulator or the low power regulator
  440. * is in low voltage mode
  441. *
  442. * @note If the Under-drive mode was enabled, it is automatically disabled after
  443. * exiting Stop mode.
  444. * When the voltage regulator operates in Under-drive mode, an additional
  445. * startup delay is induced when waking up from Stop mode.
  446. *
  447. * @note In Stop mode, all I/O pins keep the same state as in Run mode.
  448. *
  449. * @note When exiting Stop mode by issuing an interrupt or a wake-up event,
  450. * the HSI RC oscillator is selected as system clock.
  451. *
  452. * @note When the voltage regulator operates in low power mode, an additional
  453. * startup delay is incurred when waking up from Stop mode.
  454. * By keeping the internal regulator ON during Stop mode, the consumption
  455. * is higher although the startup time is reduced.
  456. *
  457. * @param Regulator specifies the regulator state in STOP mode.
  458. * This parameter can be one of the following values:
  459. * @arg PWR_MAINREGULATOR_UNDERDRIVE_ON: Main Regulator in under-drive mode
  460. * and Flash memory in power-down when the device is in Stop under-drive mode
  461. * @arg PWR_LOWPOWERREGULATOR_UNDERDRIVE_ON: Low Power Regulator in under-drive mode
  462. * and Flash memory in power-down when the device is in Stop under-drive mode
  463. * @param STOPEntry specifies if STOP mode in entered with WFI or WFE instruction.
  464. * This parameter can be one of the following values:
  465. * @arg PWR_SLEEPENTRY_WFI: enter STOP mode with WFI instruction
  466. * @arg PWR_SLEEPENTRY_WFE: enter STOP mode with WFE instruction
  467. * @retval None
  468. */
  469. HAL_StatusTypeDef HAL_PWREx_EnterUnderDriveSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
  470. {
  471. uint32_t tmpreg1 = 0U;
  472. /* Check the parameters */
  473. assert_param(IS_PWR_REGULATOR_UNDERDRIVE(Regulator));
  474. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  475. /* Enable Power ctrl clock */
  476. __HAL_RCC_PWR_CLK_ENABLE();
  477. /* Enable the Under-drive Mode ---------------------------------------------*/
  478. /* Clear Under-drive flag */
  479. __HAL_PWR_CLEAR_ODRUDR_FLAG();
  480. /* Enable the Under-drive */
  481. __HAL_PWR_UNDERDRIVE_ENABLE();
  482. /* Select the regulator state in STOP mode ---------------------------------*/
  483. tmpreg1 = PWR->CR;
  484. /* Clear PDDS, LPDS, MRLUDS and LPLUDS bits */
  485. tmpreg1 &= (uint32_t)~(PWR_CR_PDDS | PWR_CR_LPDS | PWR_CR_LPUDS | PWR_CR_MRUDS);
  486. /* Set LPDS, MRLUDS and LPLUDS bits according to PWR_Regulator value */
  487. tmpreg1 |= Regulator;
  488. /* Store the new value */
  489. PWR->CR = tmpreg1;
  490. /* Set SLEEPDEEP bit of Cortex System Control Register */
  491. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  492. /* Select STOP mode entry --------------------------------------------------*/
  493. if(STOPEntry == PWR_SLEEPENTRY_WFI)
  494. {
  495. /* Request Wait For Interrupt */
  496. __WFI();
  497. }
  498. else
  499. {
  500. /* Request Wait For Event */
  501. __WFE();
  502. }
  503. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  504. SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  505. return HAL_OK;
  506. }
  507. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */
  508. /**
  509. * @}
  510. */
  511. /**
  512. * @}
  513. */
  514. #endif /* HAL_PWR_MODULE_ENABLED */
  515. /**
  516. * @}
  517. */
  518. /**
  519. * @}
  520. */
  521. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/