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.
 
 
 

2101 lines
73 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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. @verbatim
  10. ==============================================================================
  11. ##### How to use this driver #####
  12. ==============================================================================
  13. [..]
  14. (#) Call HAL_PWREx_ConfigSupply() function to configure the regulator supply
  15. with the following different setups according to hardware (support SMPS):
  16. (+) PWR_DIRECT_SMPS_SUPPLY
  17. (+) PWR_SMPS_1V8_SUPPLIES_LDO
  18. (+) PWR_SMPS_2V5_SUPPLIES_LDO
  19. (+) PWR_SMPS_1V8_SUPPLIES_EXT_AND_LDO
  20. (+) PWR_SMPS_2V5_SUPPLIES_EXT_AND_LDO
  21. (+) PWR_SMPS_1V8_SUPPLIES_EXT
  22. (+) PWR_SMPS_2V5_SUPPLIES_EXT
  23. (+) PWR_LDO_SUPPLY
  24. (+) PWR_EXTERNAL_SOURCE_SUPPLY
  25. (#) Call HAL_PWREx_GetSupplyConfig() function to get the current supply setup.
  26. (#) Call HAL_PWREx_ControlVoltageScaling() function to configure the main
  27. internal regulator output voltage. The voltage scaling could be one of
  28. the following scales :
  29. (+) PWR_REGULATOR_VOLTAGE_SCALE0
  30. (+) PWR_REGULATOR_VOLTAGE_SCALE1
  31. (+) PWR_REGULATOR_VOLTAGE_SCALE2
  32. (+) PWR_REGULATOR_VOLTAGE_SCALE3
  33. (#) Call HAL_PWREx_GetVoltageRange() function to get the current output
  34. voltage applied to the main regulator.
  35. (#) Call HAL_PWREx_ControlStopModeVoltageScaling() function to configure the
  36. main internal regulator output voltage in STOP mode. The voltage scaling
  37. in STOP mode could be one of the following scales :
  38. (+) PWR_REGULATOR_SVOS_SCALE3
  39. (+) PWR_REGULATOR_SVOS_SCALE4
  40. (+) PWR_REGULATOR_SVOS_SCALE5
  41. (#) Call HAL_PWREx_GetStopModeVoltageRange() function to get the current
  42. output voltage applied to the main regulator in STOP mode.
  43. (#) Call HAL_PWREx_EnterSTOP2Mode() function to enter the system in STOP mode
  44. with core domain in D2STOP mode. This API is used only for STM32H7Axxx
  45. and STM32H7Bxxx devices.
  46. Please ensure to clear all CPU pending events by calling
  47. HAL_PWREx_ClearPendingEvent() function when trying to enter the Cortex-Mx
  48. in DEEP-SLEEP mode with __WFE() entry.
  49. (#) Call HAL_PWREx_EnterSTOPMode() function to enter the selected domain in
  50. DSTOP mode. Call this API with all available power domains to enter the
  51. system in STOP mode.
  52. Please ensure to clear all CPU pending events by calling
  53. HAL_PWREx_ClearPendingEvent() function when trying to enter the Cortex-Mx
  54. in DEEP-SLEEP mode with __WFE() entry.
  55. (#) Call HAL_PWREx_ClearPendingEvent() function always before entring the
  56. Cortex-Mx in any low power mode (SLEEP/DEEP-SLEEP) using WFE entry.
  57. (#) Call HAL_PWREx_EnterSTANDBYMode() function to enter the selected domain
  58. in DSTANDBY mode. Call this API with all available power domains to enter
  59. the system in STANDBY mode.
  60. (#) Call HAL_PWREx_ConfigD3Domain() function to setup the D3/SRD domain state
  61. (RUN/STOP) when the system enter to low power mode.
  62. (#) Call HAL_PWREx_ClearDomainFlags() function to clear the CPU flags for the
  63. selected power domain. This API is used only for dual core devices.
  64. (#) Call HAL_PWREx_HoldCore() and HAL_PWREx_ReleaseCore() functions to hold
  65. and release the selected CPU and and their domain peripherals when
  66. exiting STOP mode. These APIs are used only for dual core devices.
  67. (#) Call HAL_PWREx_EnableFlashPowerDown() and
  68. HAL_PWREx_DisableFlashPowerDown() functions to enable and disable the
  69. Flash Power Down in STOP mode.
  70. (#) Call HAL_PWREx_EnableMemoryShutOff() and
  71. HAL_PWREx_DisableMemoryShutOff() functions to enable and disable the
  72. memory block shut-off in DStop or DStop2. These APIs are used only for
  73. STM32H7Axxx and STM32H7Bxxx lines.
  74. (#) Call HAL_PWREx_EnableWakeUpPin() and HAL_PWREx_DisableWakeUpPin()
  75. functions to enable and disable the Wake-up pin functionality for
  76. the selected pin.
  77. (#) Call HAL_PWREx_GetWakeupFlag() and HAL_PWREx_ClearWakeupFlag()
  78. functions to manage wake-up flag for the selected pin.
  79. (#) Call HAL_PWREx_WAKEUP_PIN_IRQHandler() function to handle all wake-up
  80. pins interrupts.
  81. (#) Call HAL_PWREx_EnableBkUpReg() and HAL_PWREx_DisableBkUpReg() functions
  82. to enable and disable the backup domain regulator.
  83. (#) Call HAL_PWREx_EnableUSBReg(), HAL_PWREx_DisableUSBReg(),
  84. HAL_PWREx_EnableUSBVoltageDetector() and
  85. HAL_PWREx_DisableUSBVoltageDetector() functions to manage USB power
  86. regulation functionnalities.
  87. (#) Call HAL_PWREx_EnableBatteryCharging() and
  88. HAL_PWREx_DisableBatteryCharging() functions to enable and disable the
  89. battery charging feature with the selected resistor.
  90. (#) Call HAL_PWREx_EnableAnalogBooster() and
  91. HAL_PWREx_DisableAnalogBooster() functions to enable and disable the
  92. AVD boost feature when the VDD supply voltage is below 2V7.
  93. (#) Call HAL_PWREx_EnableMonitoring() and HAL_PWREx_DisableMonitoring()
  94. functions to enable and disable the VBAT and Temperature monitoring.
  95. When VBAT and Temperature monitoring feature is enables, use
  96. HAL_PWREx_GetTemperatureLevel() and HAL_PWREx_GetVBATLevel() to get
  97. respectively the Temperature level and VBAT level.
  98. (#) Call HAL_PWREx_GetMMCVoltage() and HAL_PWREx_DisableMonitoring()
  99. function to get VDDMMC voltage level. This API is used only for
  100. STM32H7AxxQ, STM32H7BxxQ, STM32H7Axxx and STM32H7Bxxx lines
  101. (#) Call HAL_PWREx_ConfigAVD() after setting parameter to be configured
  102. (event mode and voltage threshold) in order to set up the Analog Voltage
  103. Detector then use HAL_PWREx_EnableAVD() and HAL_PWREx_DisableAVD()
  104. functions to start and stop the AVD detection.
  105. (+) AVD level could be one of the following values :
  106. (++) 1V7
  107. (++) 2V1
  108. (++) 2V5
  109. (++) 2V8
  110. (#) Call HAL_PWREx_PVD_AVD_IRQHandler() function to handle the PWR PVD and
  111. AVD interrupt request.
  112. @endverbatim
  113. ******************************************************************************
  114. * @attention
  115. *
  116. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics.
  117. * All rights reserved.</center></h2>
  118. *
  119. * This software component is licensed by ST under BSD 3-Clause license,
  120. * the "License"; You may not use this file except in compliance with the
  121. * License. You may obtain a copy of the License at:
  122. * opensource.org/licenses/BSD-3-Clause
  123. *
  124. ******************************************************************************
  125. */
  126. /* Includes ------------------------------------------------------------------*/
  127. #include "stm32h7xx_hal.h"
  128. /** @addtogroup STM32H7xx_HAL_Driver
  129. * @{
  130. */
  131. /** @defgroup PWREx PWREx
  132. * @brief PWR Extended HAL module driver
  133. * @{
  134. */
  135. #ifdef HAL_PWR_MODULE_ENABLED
  136. /* Private typedef -----------------------------------------------------------*/
  137. /* Private define ------------------------------------------------------------*/
  138. /** @addtogroup PWREx_Private_Constants
  139. * @{
  140. */
  141. /** @defgroup PWREx_AVD_Mode_Mask PWR Extended AVD Mode Mask
  142. * @{
  143. */
  144. #define AVD_MODE_IT (0x00010000U)
  145. #define AVD_MODE_EVT (0x00020000U)
  146. #define AVD_RISING_EDGE (0x00000001U)
  147. #define AVD_FALLING_EDGE (0x00000002U)
  148. #define AVD_RISING_FALLING_EDGE (0x00000003U)
  149. /**
  150. * @}
  151. */
  152. /** @defgroup PWREx_REG_SET_TIMEOUT PWR Extended Flag Setting Time Out Value
  153. * @{
  154. */
  155. #define PWR_FLAG_SETTING_DELAY (1000U)
  156. /**
  157. * @}
  158. */
  159. /** @defgroup PWREx_WakeUp_Pins_Offsets PWREx Wake-Up Pins masks and offsets
  160. * @{
  161. */
  162. /* Wake-Up Pins EXTI register mask */
  163. #define PWR_EXTI_WAKEUP_PINS_MASK (EXTI_IMR2_IM55 | EXTI_IMR2_IM56 |\
  164. EXTI_IMR2_IM57 | EXTI_IMR2_IM58 |\
  165. EXTI_IMR2_IM59 | EXTI_IMR2_IM60)
  166. /* Wake-Up Pins PWR Pin Pull shift offsets */
  167. #define PWR_WAKEUP_PINS_PULL_SHIFT_OFFSET (2U)
  168. /**
  169. * @}
  170. */
  171. /**
  172. * @}
  173. */
  174. /* Private macro -------------------------------------------------------------*/
  175. /* Private variables ---------------------------------------------------------*/
  176. /* Private function prototypes -----------------------------------------------*/
  177. /* Private functions ---------------------------------------------------------*/
  178. /* Exported types ------------------------------------------------------------*/
  179. /* Exported functions --------------------------------------------------------*/
  180. /** @defgroup PWREx_Exported_Functions PWREx Exported Functions
  181. * @{
  182. */
  183. /** @defgroup PWREx_Exported_Functions_Group1 Power Supply Control Functions
  184. * @brief Power supply control functions
  185. *
  186. @verbatim
  187. ===============================================================================
  188. ##### Power supply control functions #####
  189. ===============================================================================
  190. [..]
  191. (#) When the system is powered on, the POR monitors VDD supply. Once VDD is
  192. above the POR threshold level, the voltage regulator is enabled in the
  193. default supply configuration:
  194. (+) The Voltage converter output level is set at 1V0 in accordance with
  195. the VOS3 level configured in PWR (D3/SRD) domain control register
  196. (PWR_D3CR/PWR_SRDCR).
  197. (+) The system is kept in reset mode as long as VCORE is not ok.
  198. (+) Once VCORE is ok, the system is taken out of reset and the HSI
  199. oscillator is enabled.
  200. (+) Once the oscillator is stable, the system is initialized: Flash memory
  201. and option bytes are loaded and the CPU starts in Run* mode.
  202. (+) The software shall then initialize the system including supply
  203. configuration programming using the HAL_PWREx_ConfigSupply().
  204. (+) Once the supply configuration has been configured, the
  205. HAL_PWREx_ConfigSupply() function checks the ACTVOSRDY bit in PWR
  206. control status register 1 (PWR_CSR1) to guarantee a valid voltage
  207. levels:
  208. (++) As long as ACTVOSRDY indicates that voltage levels are invalid, the
  209. system is in limited Run* mode, write accesses to the RAMs are not
  210. permitted and VOS shall not be changed.
  211. (++) Once ACTVOSRDY indicates that voltage levels are valid, the system
  212. is in normal Run mode, write accesses to RAMs are allowed and VOS
  213. can be changed.
  214. @endverbatim
  215. * @{
  216. */
  217. /**
  218. * @brief Configure the system Power Supply.
  219. * @param SupplySource : Specifies the Power Supply source to set after a
  220. * system startup.
  221. * This parameter can be one of the following values :
  222. * @arg PWR_DIRECT_SMPS_SUPPLY : The SMPS supplies the Vcore Power
  223. * Domains. The LDO is Bypassed.
  224. * @arg PWR_SMPS_1V8_SUPPLIES_LDO : The SMPS 1.8V output supplies
  225. * the LDO. The Vcore Power Domains
  226. * are supplied from the LDO.
  227. * @arg PWR_SMPS_2V5_SUPPLIES_LDO : The SMPS 2.5V output supplies
  228. * the LDO. The Vcore Power Domains
  229. * are supplied from the LDO.
  230. * @arg PWR_SMPS_1V8_SUPPLIES_EXT_AND_LDO : The SMPS 1.8V output
  231. * supplies external
  232. * circuits and the LDO.
  233. * The Vcore Power Domains
  234. * are supplied from the
  235. * LDO.
  236. * @arg PWR_SMPS_2V5_SUPPLIES_EXT_AND_LDO : The SMPS 2.5V output
  237. * supplies external
  238. * circuits and the LDO.
  239. * The Vcore Power Domains
  240. * are supplied from the
  241. * LDO.
  242. * @arg PWR_SMPS_1V8_SUPPLIES_EXT : The SMPS 1.8V output supplies
  243. * external circuits. The LDO is
  244. * Bypassed. The Vcore Power
  245. * Domains are supplied from
  246. * external source.
  247. * @arg PWR_SMPS_2V5_SUPPLIES_EXT : The SMPS 2.5V output supplies
  248. * external circuits. The LDO is
  249. * Bypassed. The Vcore Power
  250. * Domains are supplied from
  251. * external source.
  252. * @arg PWR_LDO_SUPPLY : The LDO regulator supplies the Vcore Power
  253. * Domains. The SMPS regulator is Bypassed.
  254. * @arg PWR_EXTERNAL_SOURCE_SUPPLY : The SMPS and the LDO are
  255. * Bypassed. The Vcore Power
  256. * Domains are supplied from
  257. * external source.
  258. * @note The PWR_LDO_SUPPLY and PWR_EXTERNAL_SOURCE_SUPPLY are used by all
  259. * H7 lines.
  260. * The PWR_DIRECT_SMPS_SUPPLY, PWR_SMPS_1V8_SUPPLIES_LDO,
  261. * PWR_SMPS_2V5_SUPPLIES_LDO, PWR_SMPS_1V8_SUPPLIES_EXT_AND_LDO,
  262. * PWR_SMPS_2V5_SUPPLIES_EXT_AND_LDO, PWR_SMPS_1V8_SUPPLIES_EXT and
  263. * PWR_SMPS_2V5_SUPPLIES_EXT are used only for lines that supports SMPS
  264. * regulator.
  265. * @retval HAL status.
  266. */
  267. HAL_StatusTypeDef HAL_PWREx_ConfigSupply (uint32_t SupplySource)
  268. {
  269. uint32_t tickstart;
  270. /* Check the parameters */
  271. assert_param (IS_PWR_SUPPLY (SupplySource));
  272. /* Check if supply source was configured */
  273. #if defined (PWR_FLAG_SCUEN)
  274. if (__HAL_PWR_GET_FLAG (PWR_FLAG_SCUEN) == 0U)
  275. #else
  276. if ((PWR->CR3 & (PWR_CR3_SMPSEN | PWR_CR3_LDOEN | PWR_CR3_BYPASS)) != (PWR_CR3_SMPSEN | PWR_CR3_LDOEN))
  277. #endif /* defined (PWR_FLAG_SCUEN) */
  278. {
  279. /* Check supply configuration */
  280. if ((PWR->CR3 & PWR_SUPPLY_CONFIG_MASK) != SupplySource)
  281. {
  282. /* Supply configuration update locked, can't apply a new supply config */
  283. return HAL_ERROR;
  284. }
  285. else
  286. {
  287. /* Supply configuration update locked, but new supply configuration
  288. matches with old supply configuration : nothing to do
  289. */
  290. return HAL_OK;
  291. }
  292. }
  293. /* Set the power supply configuration */
  294. MODIFY_REG (PWR->CR3, PWR_SUPPLY_CONFIG_MASK, SupplySource);
  295. /* Get tick */
  296. tickstart = HAL_GetTick ();
  297. /* Wait till voltage level flag is set */
  298. while (__HAL_PWR_GET_FLAG (PWR_FLAG_ACTVOSRDY) == 0U)
  299. {
  300. if ((HAL_GetTick () - tickstart) > PWR_FLAG_SETTING_DELAY)
  301. {
  302. return HAL_ERROR;
  303. }
  304. }
  305. #if defined (SMPS)
  306. /* When the SMPS supplies external circuits verify that SDEXTRDY flag is set */
  307. if ((SupplySource == PWR_SMPS_1V8_SUPPLIES_EXT_AND_LDO) ||
  308. (SupplySource == PWR_SMPS_2V5_SUPPLIES_EXT_AND_LDO) ||
  309. (SupplySource == PWR_SMPS_1V8_SUPPLIES_EXT) ||
  310. (SupplySource == PWR_SMPS_2V5_SUPPLIES_EXT))
  311. {
  312. /* Get the current tick number */
  313. tickstart = HAL_GetTick ();
  314. /* Wait till SMPS external supply ready flag is set */
  315. while (__HAL_PWR_GET_FLAG (PWR_FLAG_SMPSEXTRDY) == 0U)
  316. {
  317. if ((HAL_GetTick () - tickstart) > PWR_FLAG_SETTING_DELAY)
  318. {
  319. return HAL_ERROR;
  320. }
  321. }
  322. }
  323. #endif /* defined (SMPS) */
  324. return HAL_OK;
  325. }
  326. /**
  327. * @brief Get the power supply configuration.
  328. * @retval The supply configuration.
  329. */
  330. uint32_t HAL_PWREx_GetSupplyConfig (void)
  331. {
  332. return (PWR->CR3 & PWR_SUPPLY_CONFIG_MASK);
  333. }
  334. /**
  335. * @brief Configure the main internal regulator output voltage.
  336. * @note For STM32H7x3, STM32H7x5, STM32H7x7, STM32H742 and STM32H750 lines,
  337. * configuring Voltage Scale 0 is only possible when Vcore is supplied
  338. * from LDO (Low DropOut). The SYSCFG Clock must be enabled through
  339. * __HAL_RCC_SYSCFG_CLK_ENABLE() macro before configuring Voltage
  340. * Scale 0.
  341. * @param VoltageScaling : Specifies the regulator output voltage to achieve
  342. * a tradeoff between performance and power
  343. * consumption.
  344. * This parameter can be one of the following values :
  345. * @arg PWR_REGULATOR_VOLTAGE_SCALE0 : Regulator voltage output
  346. * Scale 0 mode.
  347. * @arg PWR_REGULATOR_VOLTAGE_SCALE1 : Regulator voltage output
  348. * range 1 mode.
  349. * @arg PWR_REGULATOR_VOLTAGE_SCALE2 : Regulator voltage output
  350. * range 2 mode.
  351. * @arg PWR_REGULATOR_VOLTAGE_SCALE3 : Regulator voltage output
  352. * range 3 mode.
  353. * @retval HAL Status
  354. */
  355. HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling (uint32_t VoltageScaling)
  356. {
  357. uint32_t tickstart;
  358. /* Check the parameters */
  359. assert_param (IS_PWR_REGULATOR_VOLTAGE (VoltageScaling));
  360. /* Get the voltage scaling */
  361. if ((PWR->CSR1 & PWR_CSR1_ACTVOS) == VoltageScaling)
  362. {
  363. /* Old and new voltage scaling configuration match : nothing to do */
  364. return HAL_OK;
  365. }
  366. #if defined (PWR_SRDCR_VOS)
  367. /* Set the voltage range */
  368. MODIFY_REG (PWR->SRDCR, PWR_SRDCR_VOS, VoltageScaling);
  369. #else
  370. if (VoltageScaling == PWR_REGULATOR_VOLTAGE_SCALE0)
  371. {
  372. if ((PWR->CR3 & PWR_CR3_LDOEN) == PWR_CR3_LDOEN)
  373. {
  374. /* Set the voltage range */
  375. MODIFY_REG (PWR->D3CR, PWR_D3CR_VOS, PWR_REGULATOR_VOLTAGE_SCALE1);
  376. /* Get tick */
  377. tickstart = HAL_GetTick ();
  378. /* Wait till voltage level flag is set */
  379. while (__HAL_PWR_GET_FLAG (PWR_FLAG_ACTVOSRDY) == 0U)
  380. {
  381. if ((HAL_GetTick () - tickstart) > PWR_FLAG_SETTING_DELAY)
  382. {
  383. return HAL_ERROR;
  384. }
  385. }
  386. /* Enable the PWR overdrive */
  387. SET_BIT (SYSCFG->PWRCR, SYSCFG_PWRCR_ODEN);
  388. }
  389. else
  390. {
  391. /* The voltage scale 0 is only possible when LDO regulator is enabled */
  392. return HAL_ERROR;
  393. }
  394. }
  395. else
  396. {
  397. if ((PWR->CSR1 & PWR_CSR1_ACTVOS) == PWR_REGULATOR_VOLTAGE_SCALE1)
  398. {
  399. if ((SYSCFG->PWRCR & SYSCFG_PWRCR_ODEN) != 0U)
  400. {
  401. /* Disable the PWR overdrive */
  402. CLEAR_BIT(SYSCFG->PWRCR, SYSCFG_PWRCR_ODEN);
  403. /* Get tick */
  404. tickstart = HAL_GetTick ();
  405. /* Wait till voltage level flag is set */
  406. while (__HAL_PWR_GET_FLAG (PWR_FLAG_ACTVOSRDY) == 0U)
  407. {
  408. if ((HAL_GetTick () - tickstart) > PWR_FLAG_SETTING_DELAY)
  409. {
  410. return HAL_ERROR;
  411. }
  412. }
  413. }
  414. }
  415. /* Set the voltage range */
  416. MODIFY_REG (PWR->D3CR, PWR_D3CR_VOS, VoltageScaling);
  417. }
  418. #endif /* defined (PWR_SRDCR_VOS) */
  419. /* Get tick */
  420. tickstart = HAL_GetTick ();
  421. /* Wait till voltage level flag is set */
  422. while (__HAL_PWR_GET_FLAG (PWR_FLAG_ACTVOSRDY) == 0U)
  423. {
  424. if ((HAL_GetTick() - tickstart) > PWR_FLAG_SETTING_DELAY)
  425. {
  426. return HAL_ERROR;
  427. }
  428. }
  429. return HAL_OK;
  430. }
  431. /**
  432. * @brief Get the main internal regulator output voltage. Reflecting the last
  433. * VOS value applied to the PMU.
  434. * @retval The current applied VOS selection.
  435. */
  436. uint32_t HAL_PWREx_GetVoltageRange (void)
  437. {
  438. /* Get the active voltage scaling */
  439. return (PWR->CSR1 & PWR_CSR1_ACTVOS);
  440. }
  441. /**
  442. * @brief Configure the main internal regulator output voltage in STOP mode.
  443. * @param VoltageScaling : Specifies the regulator output voltage when the
  444. * system enters Stop mode to achieve a tradeoff between performance
  445. * and power consumption.
  446. * This parameter can be one of the following values:
  447. * @arg PWR_REGULATOR_SVOS_SCALE3 : Regulator voltage output range
  448. * 3 mode.
  449. * @arg PWR_REGULATOR_SVOS_SCALE4 : Regulator voltage output range
  450. * 4 mode.
  451. * @arg PWR_REGULATOR_SVOS_SCALE5 : Regulator voltage output range
  452. * 5 mode.
  453. * @note The Stop mode voltage scaling for SVOS4 and SVOS5 sets the voltage
  454. * regulator in Low-power (LP) mode to further reduce power consumption.
  455. * When preselecting SVOS3, the use of the voltage regulator low-power
  456. * mode (LP) can be selected by LPDS register bit.
  457. * @note The selected SVOS4 and SVOS5 levels add an additional startup delay
  458. * when exiting from system Stop mode.
  459. * @retval HAL Status.
  460. */
  461. HAL_StatusTypeDef HAL_PWREx_ControlStopModeVoltageScaling (uint32_t VoltageScaling)
  462. {
  463. /* Check the parameters */
  464. assert_param (IS_PWR_STOP_MODE_REGULATOR_VOLTAGE (VoltageScaling));
  465. /* Return the stop mode voltage range */
  466. MODIFY_REG (PWR->CR1, PWR_CR1_SVOS, VoltageScaling);
  467. return HAL_OK;
  468. }
  469. /**
  470. * @brief Get the main internal regulator output voltage in STOP mode.
  471. * @retval The actual applied VOS selection.
  472. */
  473. uint32_t HAL_PWREx_GetStopModeVoltageRange (void)
  474. {
  475. /* Return the stop voltage scaling */
  476. return (PWR->CR1 & PWR_CR1_SVOS);
  477. }
  478. /**
  479. * @}
  480. */
  481. /** @defgroup PWREx_Exported_Functions_Group2 Low Power Control Functions
  482. * @brief Low power control functions
  483. *
  484. @verbatim
  485. ===============================================================================
  486. ##### Low power control functions #####
  487. ===============================================================================
  488. *** Domains Low Power modes configuration ***
  489. =============================================
  490. [..]
  491. This section provides the extended low power mode control APIs.
  492. The system presents 3 principles domains (D1, D2 and D3) that can be
  493. operated in low-power modes (DSTOP or DSTANDBY mode):
  494. (+) DSTOP mode to enters a domain to STOP mode:
  495. (++) D1 domain and/or D2 domain enters DSTOP mode only when the CPU
  496. subsystem is in CSTOP mode and has allocated peripheral in the
  497. domain.
  498. In DSTOP mode the domain bus matrix clock is stopped.
  499. (++) The system enters STOP mode using one of the following scenarios:
  500. (+++) D1 domain enters DSTANDBY mode (powered off) and D2, D3 domains
  501. enter DSTOP mode.
  502. (+++) D2 domain enters DSTANDBY mode (powered off) and D1, D3 domains
  503. enter DSTOP mode.
  504. (+++) D3 domain enters DSTANDBY mode (powered off) and D1, D2 domains
  505. enter DSTOP mode.
  506. (+++) D1 and D2 domains enter DSTANDBY mode (powered off) and D3 domain
  507. enters DSTOP mode.
  508. (+++) D1 and D3 domains enter DSTANDBY mode (powered off) and D2 domain
  509. enters DSTOP mode.
  510. (+++) D2 and D3 domains enter DSTANDBY mode (powered off) and D1 domain
  511. enters DSTOP mode.
  512. (+++) D1, D2 and D3 domains enter DSTOP mode.
  513. (++) When the system enters STOP mode, the clocks are stopped and the
  514. regulator is running in main or low power mode.
  515. (++) D3 domain can be kept in Run mode regardless of the CPU status when
  516. enter STOP mode by using HAL_PWREx_ConfigD3Domain(D3State) function.
  517. (+) DSTANDBY mode to enters a domain to STANDBY mode:
  518. (++) The DSTANDBY mode is entered when the PDDS_Dn bit in PWR CPU control
  519. register (PWR_CPUCR) for the Dn domain selects Standby mode.
  520. (++) The system enters STANDBY mode only when D1, D2 and D3 domains enter
  521. DSTANDBY mode. Consequently the VCORE supply regulator is powered
  522. off.
  523. *** DSTOP mode ***
  524. ==================
  525. [..]
  526. In DStop mode the domain bus matrix clock is stopped.
  527. The Flash memory can enter low-power Stop mode when it is enabled through
  528. FLPS in PWR_CR1 register. This allows a trade-off between domain DStop
  529. restart time and low power consumption.
  530. [..]
  531. In DStop mode domain peripherals using the LSI or LSE clock and
  532. peripherals having a kernel clock request are still able to operate.
  533. [..]
  534. Before entering DSTOP mode it is recommended to call SCB_CleanDCache
  535. function in order to clean the D-Cache and guarantee the data integrity
  536. for the SRAM memories.
  537. (+) Entry:
  538. The DSTOP mode is entered using the HAL_PWREx_EnterSTOPMode(Regulator,
  539. STOPEntry, Domain) function with:
  540. (++) Regulator:
  541. (+++) PWR_MAINREGULATOR_ON : Main regulator ON.
  542. (+++) PWR_LOWPOWERREGULATOR_ON : Low Power regulator ON.
  543. (++) STOPEntry:
  544. (+++) PWR_STOPENTRY_WFI : enter STOP mode with WFI instruction
  545. (+++) PWR_STOPENTRY_WFE : enter STOP mode with WFE instruction
  546. (++) Domain:
  547. (+++) PWR_D1_DOMAIN : Enters D1 domain to DSTOP mode.
  548. (+++) PWR_D2_DOMAIN : Enters D2 domain to DSTOP mode.
  549. (+++) PWR_D3_DOMAIN : Enters D3 domain to DSTOP mode.
  550. (+) Exit:
  551. Any EXTI Line (Internal or External) configured in Interrupt/Event mode.
  552. *** DSTANDBY mode ***
  553. =====================
  554. [..]
  555. In DStandby mode:
  556. (+) The domain bus matrix clock is stopped.
  557. (+) The domain is powered down and the domain RAM and register contents
  558. are lost.
  559. [..]
  560. Before entering DSTANDBY mode it is recommended to call SCB_CleanDCache
  561. function in order to clean the D-Cache and guarantee the data integrity
  562. for the SRAM memories.
  563. (+) Entry:
  564. The DSTANDBY mode is entered using the HAL_PWREx_EnterSTANDBYMode
  565. (Domain) function with:
  566. (++) Domain:
  567. (+++) PWR_D1_DOMAIN : Enters D1 domain to DSTANDBY mode.
  568. (+++) PWR_D2_DOMAIN : Enters D2 domain to DSTANDBY mode.
  569. (+++) PWR_D3_DOMAIN : Enters D3 domain to DSTANDBY mode.
  570. (+) Exit:
  571. WKUP pin rising or falling edge, RTC alarm (Alarm A and Alarm B), RTC
  572. wakeup, tamper event, time stamp event, external reset in NRST pin,
  573. IWDG reset.
  574. *** Keep D3/SRD in RUN mode ***
  575. ===============================
  576. [..]
  577. D3/SRD domain can be kept in Run mode regardless of the CPU status when
  578. entering STOP mode by using HAL_PWREx_ConfigD3Domain(D3State) function
  579. with :
  580. (+) D3State:
  581. (++) PWR_D3_DOMAIN_STOP : D3/SDR domain follows the CPU sub-system
  582. mode.
  583. (++) PWR_D3_DOMAIN_RUN : D3/SRD domain remains in Run mode regardless
  584. of CPU subsystem mode.
  585. *** FLASH Power Down configuration ****
  586. =======================================
  587. [..]
  588. By setting the FLPS bit in the PWR_CR1 register using the
  589. HAL_PWREx_EnableFlashPowerDown() function, the Flash memory also enters
  590. power down mode when the device enters STOP mode. When the Flash memory is
  591. in power down mode, an additional startup delay is incurred when waking up
  592. from STOP mode.
  593. *** Wakeup Pins configuration ****
  594. ===================================
  595. [..]
  596. Wakeup pins allow the system to exit from Standby mode. The configuration
  597. of wakeup pins is done with the HAL_PWREx_EnableWakeUpPin(sPinParams)
  598. function with:
  599. (+) sPinParams: structure to enable and configure a wakeup pin:
  600. (++) WakeUpPin: Wakeup pin to be enabled.
  601. (++) PinPolarity: Wakeup pin polarity (rising or falling edge).
  602. (++) PinPull: Wakeup pin pull (no pull, pull-up or pull-down).
  603. [..]
  604. The wakeup pins are internally connected to the EXTI lines [55-60] to
  605. generate an interrupt if enabled. The EXTI lines configuration is done by
  606. the HAL_EXTI_Dx_EventInputConfig() functions defined in the stm32h7xxhal.c
  607. file.
  608. [..]
  609. When a wakeup pin event is received the HAL_PWREx_WAKEUP_PIN_IRQHandler is
  610. called and the appropriate flag is set in the PWR_WKUPFR register. Then in
  611. the HAL_PWREx_WAKEUP_PIN_IRQHandler function the wakeup pin flag will be
  612. cleared and the appropriate user callback will be called. The user can add
  613. his own code by customization of function pointer HAL_PWREx_WKUPx_Callback.
  614. @endverbatim
  615. * @{
  616. */
  617. #if defined (PWR_CPUCR_RETDS_CD)
  618. /**
  619. * @brief Enter the system to STOP mode with main domain in DSTOP2.
  620. * @note In STOP mode, the domain bus matrix clock is stalled.
  621. * @note In STOP mode, memories and registers are maintained and peripherals
  622. * in CPU domain are no longer operational.
  623. * @note All clocks in the VCORE domain are stopped, the PLL, the HSI and the
  624. * HSE oscillators are disabled. Only Peripherals that have wakeup
  625. * capability can switch on the HSI to receive a frame, and switch off
  626. * the HSI after receiving the frame if it is not a wakeup frame. In
  627. * this case the HSI clock is propagated only to the peripheral
  628. * requesting it.
  629. * @note When exiting STOP mode by issuing an interrupt or a wakeup event,
  630. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in
  631. * RCC_CFGR register is set.
  632. * @param Regulator : Specifies the regulator state in STOP mode.
  633. * This parameter can be one of the following values:
  634. * @arg PWR_MAINREGULATOR_ON : STOP mode with regulator ON.
  635. * @arg PWR_LOWPOWERREGULATOR_ON : STOP mode with low power
  636. * regulator ON.
  637. * @param STOPEntry : Specifies if STOP mode in entered with WFI or WFE
  638. * intrinsic instruction.
  639. * This parameter can be one of the following values:
  640. * @arg PWR_STOPENTRY_WFI : Enter STOP mode with WFI instruction.
  641. * @arg PWR_STOPENTRY_WFE : Enter STOP mode with WFE instruction.
  642. * @retval None.
  643. */
  644. void HAL_PWREx_EnterSTOP2Mode (uint32_t Regulator, uint8_t STOPEntry)
  645. {
  646. /* Check the parameters */
  647. assert_param (IS_PWR_REGULATOR (Regulator));
  648. assert_param (IS_PWR_STOP_ENTRY (STOPEntry));
  649. /* Select the regulator state in Stop mode */
  650. MODIFY_REG (PWR->CR1, PWR_CR1_LPDS, Regulator);
  651. /* Go to DStop2 mode (deep retention) when CPU domain enters Deepsleep */
  652. SET_BIT (PWR->CPUCR, PWR_CPUCR_RETDS_CD);
  653. /* Keep DSTOP mode when SmartRun domain enters Deepsleep */
  654. CLEAR_BIT (PWR->CPUCR, PWR_CPUCR_PDDS_SRD);
  655. /* Set SLEEPDEEP bit of Cortex System Control Register */
  656. SET_BIT (SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  657. /* Ensure that all instructions are done before entering STOP mode */
  658. __ISB ();
  659. __DSB ();
  660. /* Select Stop mode entry */
  661. if (STOPEntry == PWR_STOPENTRY_WFI)
  662. {
  663. /* Request Wait For Interrupt */
  664. __WFI ();
  665. }
  666. else
  667. {
  668. /* Request Wait For Event */
  669. __WFE ();
  670. }
  671. /* Clear SLEEPDEEP bit of Cortex-Mx in the System Control Register */
  672. CLEAR_BIT (SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  673. }
  674. #endif /* defined (PWR_CPUCR_RETDS_CD) */
  675. /**
  676. * @brief Enter a Domain to DSTOP mode.
  677. * @note This API gives flexibility to manage independently each domain STOP
  678. * mode. For dual core lines, this API should be executed with the
  679. * corresponding Cortex-Mx to enter domain to DSTOP mode. When it is
  680. * executed by all available Cortex-Mx, the system enter to STOP mode.
  681. * For single core lines, calling this API with domain parameter set to
  682. * PWR_D1_DOMAIN (D1/CD), the whole system will enter in STOP mode
  683. * independently of PWR_CPUCR_PDDS_Dx bits values if RUN_D3 bit in the
  684. * CPUCR_RUN_D3 is cleared.
  685. * @note In DStop mode the domain bus matrix clock is stopped.
  686. * @note The system D3/SRD domain enter Stop mode only when the CPU subsystem
  687. * is in CStop mode, the EXTI wakeup sources are inactive and at least
  688. * one PDDS_Dn bit in PWR CPU control register (PWR_CPUCR) for
  689. * any domain request Stop.
  690. * @note Before entering DSTOP mode it is recommended to call SCB_CleanDCache
  691. * function in order to clean the D-Cache and guarantee the data
  692. * integrity for the SRAM memories.
  693. * @note In System Stop mode, the domain peripherals that use the LSI or LSE
  694. * clock, and the peripherals that have a kernel clock request to
  695. * select HSI or CSI as source, are still able to operate.
  696. * @param Regulator : Specifies the regulator state in STOP mode.
  697. * This parameter can be one of the following values:
  698. * @arg PWR_MAINREGULATOR_ON : STOP mode with regulator ON.
  699. * @arg PWR_LOWPOWERREGULATOR_ON : STOP mode with low power
  700. * regulator ON.
  701. * @param STOPEntry : Specifies if STOP mode in entered with WFI or WFE
  702. * intrinsic instruction.
  703. * This parameter can be one of the following values:
  704. * @arg PWR_STOPENTRY_WFI : Enter STOP mode with WFI instruction.
  705. * @arg PWR_STOPENTRY_WFE : Enter STOP mode with WFE instruction.
  706. * @param Domain : Specifies the Domain to enter in DSTOP mode.
  707. * This parameter can be one of the following values:
  708. * @arg PWR_D1_DOMAIN : Enter D1/CD Domain to DSTOP mode.
  709. * @arg PWR_D2_DOMAIN : Enter D2 Domain to DSTOP mode.
  710. * @arg PWR_D3_DOMAIN : Enter D3/SRD Domain to DSTOP mode.
  711. * @retval None.
  712. */
  713. void HAL_PWREx_EnterSTOPMode (uint32_t Regulator, uint8_t STOPEntry, uint32_t Domain)
  714. {
  715. /* Check the parameters */
  716. assert_param (IS_PWR_REGULATOR (Regulator));
  717. assert_param (IS_PWR_STOP_ENTRY (STOPEntry));
  718. assert_param (IS_PWR_DOMAIN (Domain));
  719. /* Select the regulator state in Stop mode */
  720. MODIFY_REG (PWR->CR1, PWR_CR1_LPDS, Regulator);
  721. /* Select the domain Power Down DeepSleep */
  722. if (Domain == PWR_D1_DOMAIN)
  723. {
  724. #if defined (DUAL_CORE)
  725. /* Check current core */
  726. if (HAL_GetCurrentCPUID () != CM7_CPUID)
  727. {
  728. /*
  729. When the domain selected and the cortex-mx don't match, entering stop
  730. mode will not be performed
  731. */
  732. return;
  733. }
  734. #endif /* defined (DUAL_CORE) */
  735. /* Keep DSTOP mode when D1/CD domain enters Deepsleep */
  736. CLEAR_BIT (PWR->CPUCR, PWR_CPUCR_PDDS_D1);
  737. /* Set SLEEPDEEP bit of Cortex System Control Register */
  738. SET_BIT (SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  739. /* Ensure that all instructions are done before entering STOP mode */
  740. __DSB ();
  741. __ISB ();
  742. /* Select Stop mode entry */
  743. if (STOPEntry == PWR_STOPENTRY_WFI)
  744. {
  745. /* Request Wait For Interrupt */
  746. __WFI ();
  747. }
  748. else
  749. {
  750. /* Request Wait For Event */
  751. __WFE ();
  752. }
  753. /* Clear SLEEPDEEP bit of Cortex-Mx in the System Control Register */
  754. CLEAR_BIT (SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  755. }
  756. #if defined (PWR_CPUCR_PDDS_D2)
  757. else if (Domain == PWR_D2_DOMAIN)
  758. {
  759. /* Keep DSTOP mode when D2 domain enters Deepsleep */
  760. CLEAR_BIT (PWR->CPUCR, PWR_CPUCR_PDDS_D2);
  761. #if defined (DUAL_CORE)
  762. /* Check current core */
  763. if (HAL_GetCurrentCPUID () != CM4_CPUID)
  764. {
  765. /*
  766. When the domain selected and the cortex-mx don't match, entering stop
  767. mode will not be performed
  768. */
  769. return;
  770. }
  771. /* Set SLEEPDEEP bit of Cortex System Control Register */
  772. SET_BIT (SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  773. /* Ensure that all instructions are done before entering STOP mode */
  774. __DSB ();
  775. __ISB ();
  776. /* Select Stop mode entry */
  777. if (STOPEntry == PWR_STOPENTRY_WFI)
  778. {
  779. /* Request Wait For Interrupt */
  780. __WFI ();
  781. }
  782. else
  783. {
  784. /* Request Wait For Event */
  785. __WFE ();
  786. }
  787. /* Clear SLEEPDEEP bit of Cortex-Mx in the System Control Register */
  788. CLEAR_BIT (SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  789. #endif /* defined (DUAL_CORE) */
  790. }
  791. #endif /* defined (PWR_CPUCR_PDDS_D2) */
  792. else
  793. {
  794. /* Keep DSTOP mode when D3/SRD domain enters Deepsleep */
  795. CLEAR_BIT (PWR->CPUCR, PWR_CPUCR_PDDS_D3);
  796. }
  797. }
  798. /**
  799. * @brief Clear pending event.
  800. * @note This API clears the pending event in order to enter a given CPU
  801. * to CSLEEP or CSTOP. It should be called just before APIs performing
  802. * enter low power mode using Wait For Event request.
  803. * @retval None.
  804. */
  805. void HAL_PWREx_ClearPendingEvent (void)
  806. {
  807. #if defined (DUAL_CORE)
  808. /* Check the current Core */
  809. if (HAL_GetCurrentCPUID () == CM7_CPUID)
  810. {
  811. __WFE ();
  812. }
  813. else
  814. {
  815. __SEV ();
  816. __WFE ();
  817. }
  818. #else
  819. __WFE ();
  820. #endif /* defined (DUAL_CORE) */
  821. }
  822. /**
  823. * @brief Enter a Domain to DSTANDBY mode.
  824. * @note This API gives flexibility to manage independently each domain
  825. * STANDBY mode. For dual core lines, this API should be executed with
  826. * the corresponding Cortex-Mx to enter domain to DSTANDBY mode. When
  827. * it is executed by all available Cortex-Mx, the system enter STANDBY
  828. * mode.
  829. * For single core lines, calling this API with D1/SRD the selected
  830. * domain will enter the whole system in STOP if PWR_CPUCR_PDDS_D3 = 0
  831. * and enter the whole system in STANDBY if PWR_CPUCR_PDDS_D3 = 1.
  832. * @note The DStandby mode is entered when all PDDS_Dn bits in PWR_CPUCR for
  833. * the Dn domain select Standby mode. When the system enters Standby
  834. * mode, the voltage regulator is disabled.
  835. * @note When D2 or D3 domain is in DStandby mode and the CPU sets the
  836. * domain PDDS_Dn bit to select Stop mode, the domain remains in
  837. * DStandby mode. The domain will only exit DStandby when the CPU
  838. * allocates a peripheral in the domain.
  839. * @note The system D3/SRD domain enters Standby mode only when the D1 and D2
  840. * domain are in DStandby.
  841. * @note Before entering DSTANDBY mode it is recommended to call
  842. * SCB_CleanDCache function in order to clean the D-Cache and guarantee
  843. * the data integrity for the SRAM memories.
  844. * @param Domain : Specifies the Domain to enter to STANDBY mode.
  845. * This parameter can be one of the following values:
  846. * @arg PWR_D1_DOMAIN: Enter D1/CD Domain to DSTANDBY mode.
  847. * @arg PWR_D2_DOMAIN: Enter D2 Domain to DSTANDBY mode.
  848. * @arg PWR_D3_DOMAIN: Enter D3/SRD Domain to DSTANDBY mode.
  849. * @retval None
  850. */
  851. void HAL_PWREx_EnterSTANDBYMode (uint32_t Domain)
  852. {
  853. /* Check the parameters */
  854. assert_param (IS_PWR_DOMAIN (Domain));
  855. /* Select the domain Power Down DeepSleep */
  856. if (Domain == PWR_D1_DOMAIN)
  857. {
  858. #if defined (DUAL_CORE)
  859. /* Check current core */
  860. if (HAL_GetCurrentCPUID () != CM7_CPUID)
  861. {
  862. /*
  863. When the domain selected and the cortex-mx don't match, entering
  864. standby mode will not be performed
  865. */
  866. return;
  867. }
  868. #endif /* defined (DUAL_CORE) */
  869. /* Allow DSTANDBY mode when D1/CD domain enters Deepsleep */
  870. SET_BIT (PWR-> CPUCR, PWR_CPUCR_PDDS_D1);
  871. #if defined (DUAL_CORE)
  872. /* Allow DSTANDBY mode when D1/CD domain enters Deepsleep */
  873. SET_BIT (PWR-> CPU2CR, PWR_CPU2CR_PDDS_D1);
  874. #endif /*DUAL_CORE*/
  875. /* Set SLEEPDEEP bit of Cortex System Control Register */
  876. SET_BIT (SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  877. /* This option is used to ensure that store operations are completed */
  878. #if defined (__CC_ARM)
  879. __force_stores ();
  880. #endif /* defined (__CC_ARM) */
  881. /* Request Wait For Interrupt */
  882. __WFI ();
  883. }
  884. #if defined (PWR_CPUCR_PDDS_D2)
  885. else if (Domain == PWR_D2_DOMAIN)
  886. {
  887. /* Allow DSTANDBY mode when D2 domain enters Deepsleep */
  888. SET_BIT (PWR-> CPUCR, PWR_CPUCR_PDDS_D2);
  889. #if defined (DUAL_CORE)
  890. /* Check current core */
  891. if (HAL_GetCurrentCPUID () != CM4_CPUID)
  892. {
  893. /*
  894. When the domain selected and the cortex-mx don't match, entering
  895. standby mode will not be performed
  896. */
  897. return;
  898. }
  899. /* Allow DSTANDBY mode when D2 domain enters Deepsleep */
  900. SET_BIT (PWR-> CPU2CR, PWR_CPU2CR_PDDS_D2);
  901. /* Set SLEEPDEEP bit of Cortex System Control Register */
  902. SET_BIT (SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  903. /* This option is used to ensure that store operations are completed */
  904. #if defined (__CC_ARM)
  905. __force_stores ();
  906. #endif /* defined (__CC_ARM) */
  907. /* Request Wait For Interrupt */
  908. __WFI ();
  909. #endif /* defined (DUAL_CORE) */
  910. }
  911. #endif /* defined (PWR_CPUCR_PDDS_D2) */
  912. else
  913. {
  914. /* Allow DSTANDBY mode when D3/SRD domain enters Deepsleep */
  915. SET_BIT (PWR-> CPUCR, PWR_CPUCR_PDDS_D3);
  916. #if defined (DUAL_CORE)
  917. /* Allow DSTANDBY mode when D3/SRD domain enters Deepsleep */
  918. SET_BIT (PWR-> CPU2CR, PWR_CPU2CR_PDDS_D3);
  919. #endif /* defined (DUAL_CORE) */
  920. }
  921. }
  922. /**
  923. * @brief Configure the D3/SRD Domain state when the System in low power mode.
  924. * @param D3State : Specifies the D3/SRD state.
  925. * This parameter can be one of the following values :
  926. * @arg PWR_D3_DOMAIN_STOP : D3/SRD domain will follow the most deep
  927. * CPU sub-system low power mode.
  928. * @arg PWR_D3_DOMAIN_RUN : D3/SRD domain will stay in RUN mode
  929. * regardless of the CPU sub-system low
  930. * power mode.
  931. * @retval None
  932. */
  933. void HAL_PWREx_ConfigD3Domain (uint32_t D3State)
  934. {
  935. /* Check the parameter */
  936. assert_param (IS_D3_STATE (D3State));
  937. /* Keep D3/SRD in run mode */
  938. MODIFY_REG (PWR->CPUCR, PWR_CPUCR_RUN_D3, D3State);
  939. }
  940. #if defined (DUAL_CORE)
  941. /**
  942. * @brief Clear HOLD2F, HOLD1F, STOPF, SBF, SBF_D1, and SBF_D2 flags for a
  943. * given domain.
  944. * @param DomainFlags : Specifies the Domain flags to be cleared.
  945. * This parameter can be one of the following values:
  946. * @arg PWR_D1_DOMAIN_FLAGS : Clear D1 Domain flags.
  947. * @arg PWR_D2_DOMAIN_FLAGS : Clear D2 Domain flags.
  948. * @arg PWR_ALL_DOMAIN_FLAGS : Clear D1 and D2 Domain flags.
  949. * @retval None.
  950. */
  951. void HAL_PWREx_ClearDomainFlags (uint32_t DomainFlags)
  952. {
  953. /* Check the parameter */
  954. assert_param (IS_PWR_DOMAIN_FLAG (DomainFlags));
  955. /* D1 CPU flags */
  956. if (DomainFlags == PWR_D1_DOMAIN_FLAGS)
  957. {
  958. /* Clear D1 domain flags (HOLD2F, STOPF, SBF, SBF_D1, and SBF_D2) */
  959. SET_BIT (PWR->CPUCR, PWR_CPUCR_CSSF);
  960. }
  961. /* D2 CPU flags */
  962. else if (DomainFlags == PWR_D2_DOMAIN_FLAGS)
  963. {
  964. /* Clear D2 domain flags (HOLD1F, STOPF, SBF, SBF_D1, and SBF_D2) */
  965. SET_BIT (PWR->CPU2CR, PWR_CPU2CR_CSSF);
  966. }
  967. else
  968. {
  969. /* Clear D1 domain flags (HOLD2F, STOPF, SBF, SBF_D1, and SBF_D2) */
  970. SET_BIT (PWR->CPUCR, PWR_CPUCR_CSSF);
  971. /* Clear D2 domain flags (HOLD1F, STOPF, SBF, SBF_D1, and SBF_D2) */
  972. SET_BIT (PWR->CPU2CR, PWR_CPU2CR_CSSF);
  973. }
  974. }
  975. /**
  976. * @brief Hold the CPU and their domain peripherals when exiting STOP mode.
  977. * @param CPU : Specifies the core to be held.
  978. * This parameter can be one of the following values:
  979. * @arg PWR_CORE_CPU1: Hold CPU1 and set CPU2 as master.
  980. * @arg PWR_CORE_CPU2: Hold CPU2 and set CPU1 as master.
  981. * @retval HAL status
  982. */
  983. HAL_StatusTypeDef HAL_PWREx_HoldCore (uint32_t CPU)
  984. {
  985. HAL_StatusTypeDef status = HAL_OK;
  986. /* Check the parameters */
  987. assert_param (IS_PWR_CORE (CPU));
  988. /* Check CPU index */
  989. if (CPU == PWR_CORE_CPU2)
  990. {
  991. /* If CPU1 is not held */
  992. if ((PWR->CPU2CR & PWR_CPU2CR_HOLD1) != PWR_CPU2CR_HOLD1)
  993. {
  994. /* Set HOLD2 bit */
  995. SET_BIT (PWR->CPUCR, PWR_CPUCR_HOLD2);
  996. }
  997. else
  998. {
  999. status = HAL_ERROR;
  1000. }
  1001. }
  1002. else
  1003. {
  1004. /* If CPU2 is not held */
  1005. if ((PWR->CPUCR & PWR_CPUCR_HOLD2) != PWR_CPUCR_HOLD2)
  1006. {
  1007. /* Set HOLD1 bit */
  1008. SET_BIT (PWR->CPU2CR, PWR_CPU2CR_HOLD1);
  1009. }
  1010. else
  1011. {
  1012. status = HAL_ERROR;
  1013. }
  1014. }
  1015. return status;
  1016. }
  1017. /**
  1018. * @brief Release the CPU and their domain peripherals after a wake-up from
  1019. * STOP mode.
  1020. * @param CPU: Specifies the core to be released.
  1021. * This parameter can be one of the following values:
  1022. * @arg PWR_CORE_CPU1: Release the CPU1 and their domain
  1023. * peripherals from holding.
  1024. * @arg PWR_CORE_CPU2: Release the CPU2 and their domain
  1025. * peripherals from holding.
  1026. * @retval None
  1027. */
  1028. void HAL_PWREx_ReleaseCore (uint32_t CPU)
  1029. {
  1030. /* Check the parameters */
  1031. assert_param (IS_PWR_CORE (CPU));
  1032. /* Check CPU index */
  1033. if (CPU == PWR_CORE_CPU2)
  1034. {
  1035. /* Reset HOLD2 bit */
  1036. CLEAR_BIT (PWR->CPUCR, PWR_CPUCR_HOLD2);
  1037. }
  1038. else
  1039. {
  1040. /* Reset HOLD1 bit */
  1041. CLEAR_BIT (PWR->CPU2CR, PWR_CPU2CR_HOLD1);
  1042. }
  1043. }
  1044. #endif /* defined (DUAL_CORE) */
  1045. /**
  1046. * @brief Enable the Flash Power Down in Stop mode.
  1047. * @note When Flash Power Down is enabled the Flash memory enters low-power
  1048. * mode when D1/SRD domain is in DStop mode. This feature allows to
  1049. * obtain the best trade-off between low-power consumption and restart
  1050. * time when exiting from DStop mode.
  1051. * @retval None.
  1052. */
  1053. void HAL_PWREx_EnableFlashPowerDown (void)
  1054. {
  1055. /* Enable the Flash Power Down */
  1056. SET_BIT (PWR->CR1, PWR_CR1_FLPS);
  1057. }
  1058. /**
  1059. * @brief Disable the Flash Power Down in Stop mode.
  1060. * @note When Flash Power Down is disabled the Flash memory is kept on
  1061. * normal mode when D1/SRD domain is in DStop mode. This feature allows
  1062. * to obtain the best trade-off between low-power consumption and
  1063. * restart time when exiting from DStop mode.
  1064. * @retval None.
  1065. */
  1066. void HAL_PWREx_DisableFlashPowerDown (void)
  1067. {
  1068. /* Disable the Flash Power Down */
  1069. CLEAR_BIT (PWR->CR1, PWR_CR1_FLPS);
  1070. }
  1071. #if defined (PWR_CR1_SRDRAMSO)
  1072. /**
  1073. * @brief Enable memory block shut-off in DStop or DStop2 modes
  1074. * @note In DStop or DStop2 mode, the content of the memory blocks is
  1075. * maintained. Further power optimization can be obtained by switching
  1076. * off some memory blocks. This optimization implies loss of the memory
  1077. * content. The user can select which memory is discarded during STOP
  1078. * mode by means of xxSO bits.
  1079. * @param MemoryBlock : Specifies the memory block to shut-off during DStop or
  1080. * DStop2 mode.
  1081. * This parameter can be one of the following values:
  1082. * @arg PWR_SRD_AHB_MEMORY_BLOCK : SmartRun domain AHB memory.
  1083. * @arg PWR_USB_FDCAN_MEMORY_BLOCK : High-speed interfaces USB and
  1084. * FDCAN memories.
  1085. * @arg PWR_GFXMMU_JPEG_MEMORY_BLOCK : GFXMMU and JPEG memories.
  1086. * @arg PWR_TCM_ECM_MEMORY_BLOCK : Instruction TCM and ETM memories.
  1087. * @arg PWR_RAM1_AHB_MEMORY_BLOCK : AHB RAM1 memory.
  1088. * @arg PWR_RAM2_AHB_MEMORY_BLOCK : AHB RAM2 memory.
  1089. * @arg PWR_RAM1_AXI_MEMORY_BLOCK : AXI RAM1 memory.
  1090. * @arg PWR_RAM2_AXI_MEMORY_BLOCK : AXI RAM2 memory.
  1091. * @arg PWR_RAM3_AXI_MEMORY_BLOCK : AXI RAM3 memory.
  1092. * @retval None.
  1093. */
  1094. void HAL_PWREx_EnableMemoryShutOff (uint32_t MemoryBlock)
  1095. {
  1096. /* Check the parameter */
  1097. assert_param (IS_PWR_MEMORY_BLOCK (MemoryBlock));
  1098. /* Enable memory block shut-off */
  1099. SET_BIT (PWR->CR1, MemoryBlock);
  1100. }
  1101. /**
  1102. * @brief Disable memory block shut-off in DStop or DStop2 modes
  1103. * @param MemoryBlock : Specifies the memory block to keep content during
  1104. * DStop or DStop2 mode.
  1105. * This parameter can be one of the following values:
  1106. * @arg PWR_SRD_AHB_MEMORY_BLOCK : SmartRun domain AHB memory.
  1107. * @arg PWR_USB_FDCAN_MEMORY_BLOCK : High-speed interfaces USB and
  1108. * FDCAN memories.
  1109. * @arg PWR_GFXMMU_JPEG_MEMORY_BLOCK : GFXMMU and JPEG memories.
  1110. * @arg PWR_TCM_ECM_MEMORY_BLOCK : Instruction TCM and ETM memories.
  1111. * @arg PWR_RAM1_AHB_MEMORY_BLOCK : AHB RAM1 memory.
  1112. * @arg PWR_RAM2_AHB_MEMORY_BLOCK : AHB RAM2 memory.
  1113. * @arg PWR_RAM1_AXI_MEMORY_BLOCK : AXI RAM1 memory.
  1114. * @arg PWR_RAM2_AXI_MEMORY_BLOCK : AXI RAM2 memory.
  1115. * @arg PWR_RAM3_AXI_MEMORY_BLOCK : AXI RAM3 memory.
  1116. * @retval None.
  1117. */
  1118. void HAL_PWREx_DisableMemoryShutOff (uint32_t MemoryBlock)
  1119. {
  1120. /* Check the parameter */
  1121. assert_param (IS_PWR_MEMORY_BLOCK (MemoryBlock));
  1122. /* Disable memory block shut-off */
  1123. CLEAR_BIT (PWR->CR1, MemoryBlock);
  1124. }
  1125. #endif /* defined (PWR_CR1_SRDRAMSO) */
  1126. /**
  1127. * @brief Enable the Wake-up PINx functionality.
  1128. * @param sPinParams : Pointer to a PWREx_WakeupPinTypeDef structure that
  1129. * contains the configuration information for the wake-up
  1130. * Pin.
  1131. * @note For dual core devices, please ensure to configure the EXTI lines for
  1132. * the different Cortex-Mx. All combination are allowed: wake up only
  1133. * Cortex-M7, wake up only Cortex-M4 and wake up Cortex-M7 and
  1134. * Cortex-M4.
  1135. * @retval None.
  1136. */
  1137. void HAL_PWREx_EnableWakeUpPin (PWREx_WakeupPinTypeDef *sPinParams)
  1138. {
  1139. uint32_t pinConfig;
  1140. uint32_t regMask;
  1141. const uint32_t pullMask = PWR_WKUPEPR_WKUPPUPD1;
  1142. /* Check the parameters */
  1143. assert_param (IS_PWR_WAKEUP_PIN (sPinParams->WakeUpPin));
  1144. assert_param (IS_PWR_WAKEUP_PIN_POLARITY (sPinParams->PinPolarity));
  1145. assert_param (IS_PWR_WAKEUP_PIN_PULL (sPinParams->PinPull));
  1146. pinConfig = sPinParams->WakeUpPin | \
  1147. (sPinParams->PinPolarity << ((POSITION_VAL(sPinParams->WakeUpPin) + PWR_WKUPEPR_WKUPP1_Pos) & 0x1FU)) | \
  1148. (sPinParams->PinPull << (((POSITION_VAL(sPinParams->WakeUpPin) * PWR_WAKEUP_PINS_PULL_SHIFT_OFFSET) + PWR_WKUPEPR_WKUPPUPD1_Pos) & 0x1FU));
  1149. regMask = sPinParams->WakeUpPin | \
  1150. (PWR_WKUPEPR_WKUPP1 << (POSITION_VAL(sPinParams->WakeUpPin) & 0x1FU)) | \
  1151. (pullMask << ((POSITION_VAL(sPinParams->WakeUpPin) * PWR_WAKEUP_PINS_PULL_SHIFT_OFFSET) & 0x1FU));
  1152. /* Enable and Specify the Wake-Up pin polarity and the pull configuration
  1153. for the event detection (rising or falling edge) */
  1154. MODIFY_REG (PWR->WKUPEPR, regMask, pinConfig);
  1155. #ifndef DUAL_CORE
  1156. /* Configure the Wakeup Pin EXTI Line */
  1157. MODIFY_REG (EXTI->IMR2, PWR_EXTI_WAKEUP_PINS_MASK, (sPinParams->WakeUpPin << EXTI_IMR2_IM55_Pos));
  1158. #endif /* !DUAL_CORE */
  1159. }
  1160. /**
  1161. * @brief Disable the Wake-up PINx functionality.
  1162. * @param WakeUpPin : Specifies the Wake-Up pin to be disabled.
  1163. * This parameter can be one of the following values:
  1164. * @arg PWR_WAKEUP_PIN1 : Disable PA0 wake-up PIN.
  1165. * @arg PWR_WAKEUP_PIN2 : Disable PA2 wake-up PIN.
  1166. * @arg PWR_WAKEUP_PIN3 : Disable PI8 wake-up PIN.
  1167. * @arg PWR_WAKEUP_PIN4 : Disable PC13 wake-up PIN.
  1168. * @arg PWR_WAKEUP_PIN5 : Disable PI11 wake-up PIN.
  1169. * @arg PWR_WAKEUP_PIN6 : Disable PC1 wake-up PIN.
  1170. * @retval None
  1171. */
  1172. void HAL_PWREx_DisableWakeUpPin (uint32_t WakeUpPin)
  1173. {
  1174. /* Check the parameter */
  1175. assert_param (IS_PWR_WAKEUP_PIN (WakeUpPin));
  1176. /* Disable the WakeUpPin */
  1177. CLEAR_BIT (PWR->WKUPEPR, WakeUpPin);
  1178. }
  1179. /**
  1180. * @brief Get the Wake-Up Pin pending flags.
  1181. * @param WakeUpFlag : Specifies the Wake-Up PIN flag to be checked.
  1182. * This parameter can be one of the following values:
  1183. * @arg PWR_WAKEUP_FLAG1 : Get wakeup event received from PA0.
  1184. * @arg PWR_WAKEUP_FLAG2 : Get wakeup event received from PA2.
  1185. * @arg PWR_WAKEUP_FLAG3 : Get wakeup event received from PI8.
  1186. * @arg PWR_WAKEUP_FLAG4 : Get wakeup event received from PC13.
  1187. * @arg PWR_WAKEUP_FLAG5 : Get wakeup event received from PI11.
  1188. * @arg PWR_WAKEUP_FLAG6 : Get wakeup event received from PC1.
  1189. * @arg PWR_WAKEUP_FLAG_ALL : Get Wakeup event received from all
  1190. * wake up pins.
  1191. * @retval The Wake-Up pin flag.
  1192. */
  1193. uint32_t HAL_PWREx_GetWakeupFlag (uint32_t WakeUpFlag)
  1194. {
  1195. /* Check the parameters */
  1196. assert_param (IS_PWR_WAKEUP_FLAG (WakeUpFlag));
  1197. /* Return the wake up pin flag */
  1198. return (PWR->WKUPFR & WakeUpFlag);
  1199. }
  1200. /**
  1201. * @brief Clear the Wake-Up pin pending flag.
  1202. * @param WakeUpFlag: Specifies the Wake-Up PIN flag to clear.
  1203. * This parameter can be one of the following values:
  1204. * @arg PWR_WAKEUP_FLAG1 : Clear the wakeup event received from PA0.
  1205. * @arg PWR_WAKEUP_FLAG2 : Clear the wakeup event received from PA2.
  1206. * @arg PWR_WAKEUP_FLAG3 : Clear the wakeup event received from PI8.
  1207. * @arg PWR_WAKEUP_FLAG4 : Clear the wakeup event received from PC13.
  1208. * @arg PWR_WAKEUP_FLAG5 : Clear the wakeup event received from PI11.
  1209. * @arg PWR_WAKEUP_FLAG6 : Clear the wakeup event received from PC1.
  1210. * @arg PWR_WAKEUP_FLAG_ALL : Clear the wakeup events received from
  1211. * all wake up pins.
  1212. * @retval HAL status.
  1213. */
  1214. HAL_StatusTypeDef HAL_PWREx_ClearWakeupFlag (uint32_t WakeUpFlag)
  1215. {
  1216. /* Check the parameter */
  1217. assert_param (IS_PWR_WAKEUP_FLAG (WakeUpFlag));
  1218. /* Clear the wake up event received from wake up pin x */
  1219. SET_BIT (PWR->WKUPCR, WakeUpFlag);
  1220. /* Check if the wake up event is well cleared */
  1221. if ((PWR->WKUPFR & WakeUpFlag) != 0U)
  1222. {
  1223. return HAL_ERROR;
  1224. }
  1225. return HAL_OK;
  1226. }
  1227. /**
  1228. * @brief This function handles the PWR WAKEUP PIN interrupt request.
  1229. * @note This API should be called under the WAKEUP_PIN_IRQHandler().
  1230. * @retval None.
  1231. */
  1232. void HAL_PWREx_WAKEUP_PIN_IRQHandler (void)
  1233. {
  1234. /* Wakeup pin EXTI line interrupt detected */
  1235. if (READ_BIT(PWR->WKUPFR, PWR_WKUPFR_WKUPF1) != 0U)
  1236. {
  1237. /* Clear PWR WKUPF1 flag */
  1238. __HAL_PWR_CLEAR_WAKEUPFLAG (PWR_FLAG_WKUP1);
  1239. /* PWR WKUP1 interrupt user callback */
  1240. HAL_PWREx_WKUP1_Callback ();
  1241. }
  1242. else if (READ_BIT (PWR->WKUPFR, PWR_WKUPFR_WKUPF2) != 0U)
  1243. {
  1244. /* Clear PWR WKUPF2 flag */
  1245. __HAL_PWR_CLEAR_WAKEUPFLAG (PWR_FLAG_WKUP2);
  1246. /* PWR WKUP2 interrupt user callback */
  1247. HAL_PWREx_WKUP2_Callback ();
  1248. }
  1249. else if (READ_BIT (PWR->WKUPFR, PWR_WKUPFR_WKUPF3) != 0U)
  1250. {
  1251. /* Clear PWR WKUPF3 flag */
  1252. __HAL_PWR_CLEAR_WAKEUPFLAG (PWR_FLAG_WKUP3);
  1253. /* PWR WKUP3 interrupt user callback */
  1254. HAL_PWREx_WKUP3_Callback ();
  1255. }
  1256. else if (READ_BIT (PWR->WKUPFR, PWR_WKUPFR_WKUPF4) != 0U)
  1257. {
  1258. /* Clear PWR WKUPF4 flag */
  1259. __HAL_PWR_CLEAR_WAKEUPFLAG (PWR_FLAG_WKUP4);
  1260. /* PWR WKUP4 interrupt user callback */
  1261. HAL_PWREx_WKUP4_Callback ();
  1262. }
  1263. else if (READ_BIT (PWR->WKUPFR, PWR_WKUPFR_WKUPF5) != 0U)
  1264. {
  1265. /* Clear PWR WKUPF5 flag */
  1266. __HAL_PWR_CLEAR_WAKEUPFLAG (PWR_FLAG_WKUP5);
  1267. /* PWR WKUP5 interrupt user callback */
  1268. HAL_PWREx_WKUP5_Callback ();
  1269. }
  1270. else
  1271. {
  1272. /* Clear PWR WKUPF6 flag */
  1273. __HAL_PWR_CLEAR_WAKEUPFLAG (PWR_FLAG_WKUP6);
  1274. /* PWR WKUP6 interrupt user callback */
  1275. HAL_PWREx_WKUP6_Callback ();
  1276. }
  1277. }
  1278. /**
  1279. * @brief PWR WKUP1 interrupt callback.
  1280. * @retval None.
  1281. */
  1282. __weak void HAL_PWREx_WKUP1_Callback (void)
  1283. {
  1284. /* NOTE : This function should not be modified, when the callback is needed,
  1285. the HAL_PWREx_WKUP1Callback can be implemented in the user file
  1286. */
  1287. }
  1288. /**
  1289. * @brief PWR WKUP2 interrupt callback.
  1290. * @retval None.
  1291. */
  1292. __weak void HAL_PWREx_WKUP2_Callback (void)
  1293. {
  1294. /* NOTE : This function should not be modified, when the callback is needed,
  1295. the HAL_PWREx_WKUP2Callback can be implemented in the user file
  1296. */
  1297. }
  1298. /**
  1299. * @brief PWR WKUP3 interrupt callback.
  1300. * @retval None.
  1301. */
  1302. __weak void HAL_PWREx_WKUP3_Callback (void)
  1303. {
  1304. /* NOTE : This function should not be modified, when the callback is needed,
  1305. the HAL_PWREx_WKUP3Callback can be implemented in the user file
  1306. */
  1307. }
  1308. /**
  1309. * @brief PWR WKUP4 interrupt callback.
  1310. * @retval None.
  1311. */
  1312. __weak void HAL_PWREx_WKUP4_Callback (void)
  1313. {
  1314. /* NOTE : This function should not be modified, when the callback is needed,
  1315. the HAL_PWREx_WKUP4Callback can be implemented in the user file
  1316. */
  1317. }
  1318. /**
  1319. * @brief PWR WKUP5 interrupt callback.
  1320. * @retval None.
  1321. */
  1322. __weak void HAL_PWREx_WKUP5_Callback (void)
  1323. {
  1324. /* NOTE : This function should not be modified, when the callback is needed,
  1325. the HAL_PWREx_WKUP5Callback can be implemented in the user file
  1326. */
  1327. }
  1328. /**
  1329. * @brief PWR WKUP6 interrupt callback.
  1330. * @retval None.
  1331. */
  1332. __weak void HAL_PWREx_WKUP6_Callback (void)
  1333. {
  1334. /* NOTE : This function should not be modified, when the callback is needed,
  1335. the HAL_PWREx_WKUP6Callback can be implemented in the user file
  1336. */
  1337. }
  1338. /**
  1339. * @}
  1340. */
  1341. /** @defgroup PWREx_Exported_Functions_Group3 Peripherals control functions
  1342. * @brief Peripherals control functions
  1343. *
  1344. @verbatim
  1345. ===============================================================================
  1346. ##### Peripherals control functions #####
  1347. ===============================================================================
  1348. *** Main and Backup Regulators configuration ***
  1349. ================================================
  1350. [..]
  1351. (+) The backup domain includes 4 Kbytes of backup SRAM accessible only
  1352. from the CPU, and addressed in 32-bit, 16-bit or 8-bit mode. Its
  1353. content is retained even in Standby or VBAT mode when the low power
  1354. backup regulator is enabled. It can be considered as an internal
  1355. EEPROM when VBAT is always present. You can use the
  1356. HAL_PWREx_EnableBkUpReg() function to enable the low power backup
  1357. regulator.
  1358. (+) When the backup domain is supplied by VDD (analog switch connected to
  1359. VDD) the backup SRAM is powered from VDD which replaces the VBAT power
  1360. supply to save battery life.
  1361. (+) The backup SRAM is not mass erased by a tamper event. It is read
  1362. protected to prevent confidential data, such as cryptographic private
  1363. key, from being accessed. The backup SRAM can be erased only through
  1364. the Flash interface when a protection level change from level 1 to
  1365. level 0 is requested.
  1366. -@- Refer to the description of Read protection (RDP) in the Flash
  1367. programming manual.
  1368. (+) The main internal regulator can be configured to have a tradeoff
  1369. between performance and power consumption when the device does not
  1370. operate at the maximum frequency. This is done through
  1371. HAL_PWREx_ControlVoltageScaling(VOS) function which configure the VOS
  1372. bit in PWR_D3CR register.
  1373. (+) The main internal regulator can be configured to operate in Low Power
  1374. mode when the system enters STOP mode to further reduce power
  1375. consumption.
  1376. This is done through HAL_PWREx_ControlStopModeVoltageScaling(SVOS)
  1377. function which configure the SVOS bit in PWR_CR1 register.
  1378. The selected SVOS4 and SVOS5 levels add an additional startup delay
  1379. when exiting from system Stop mode.
  1380. -@- Refer to the product datasheets for more details.
  1381. *** USB Regulator configuration ***
  1382. ===================================
  1383. [..]
  1384. (+) The USB transceivers are supplied from a dedicated VDD33USB supply
  1385. that can be provided either by the integrated USB regulator, or by an
  1386. external USB supply.
  1387. (+) The USB regulator is enabled by HAL_PWREx_EnableUSBReg() function, the
  1388. VDD33USB is then provided from the USB regulator.
  1389. (+) When the USB regulator is enabled, the VDD33USB supply level detector
  1390. shall be enabled through HAL_PWREx_EnableUSBVoltageDetector()
  1391. function.
  1392. (+) The USB regulator is disabled through HAL_PWREx_DisableUSBReg()
  1393. function and VDD33USB can be provided from an external supply. In this
  1394. case VDD33USB and VDD50USB shall be connected together.
  1395. *** VBAT battery charging ***
  1396. =============================
  1397. [..]
  1398. (+) When VDD is present, the external battery connected to VBAT can be
  1399. charged through an internal resistance. VBAT charging can be performed
  1400. either through a 5 KOhm resistor or through a 1.5 KOhm resistor.
  1401. (+) VBAT charging is enabled by HAL_PWREx_EnableBatteryCharging
  1402. (ResistorValue) function with:
  1403. (++) ResistorValue:
  1404. (+++) PWR_BATTERY_CHARGING_RESISTOR_5: 5 KOhm resistor.
  1405. (+++) PWR_BATTERY_CHARGING_RESISTOR_1_5: 1.5 KOhm resistor.
  1406. (+) VBAT charging is disabled by HAL_PWREx_DisableBatteryCharging()
  1407. function.
  1408. @endverbatim
  1409. * @{
  1410. */
  1411. /**
  1412. * @brief Enable the Backup Regulator.
  1413. * @retval HAL status.
  1414. */
  1415. HAL_StatusTypeDef HAL_PWREx_EnableBkUpReg (void)
  1416. {
  1417. uint32_t tickstart;
  1418. /* Enable the Backup regulator */
  1419. SET_BIT (PWR->CR2, PWR_CR2_BREN);
  1420. /* Get tick */
  1421. tickstart = HAL_GetTick ();
  1422. /* Wait till Backup regulator ready flag is set */
  1423. while (__HAL_PWR_GET_FLAG (PWR_FLAG_BRR) == 0U)
  1424. {
  1425. if ((HAL_GetTick() - tickstart ) > PWR_FLAG_SETTING_DELAY)
  1426. {
  1427. return HAL_ERROR;
  1428. }
  1429. }
  1430. return HAL_OK;
  1431. }
  1432. /**
  1433. * @brief Disable the Backup Regulator.
  1434. * @retval HAL status.
  1435. */
  1436. HAL_StatusTypeDef HAL_PWREx_DisableBkUpReg (void)
  1437. {
  1438. uint32_t tickstart;
  1439. /* Disable the Backup regulator */
  1440. CLEAR_BIT (PWR->CR2, PWR_CR2_BREN);
  1441. /* Get tick */
  1442. tickstart = HAL_GetTick ();
  1443. /* Wait till Backup regulator ready flag is reset */
  1444. while (__HAL_PWR_GET_FLAG (PWR_FLAG_BRR) != 0U)
  1445. {
  1446. if ((HAL_GetTick() - tickstart ) > PWR_FLAG_SETTING_DELAY)
  1447. {
  1448. return HAL_ERROR;
  1449. }
  1450. }
  1451. return HAL_OK;
  1452. }
  1453. /**
  1454. * @brief Enable the USB Regulator.
  1455. * @retval HAL status.
  1456. */
  1457. HAL_StatusTypeDef HAL_PWREx_EnableUSBReg (void)
  1458. {
  1459. uint32_t tickstart;
  1460. /* Enable the USB regulator */
  1461. SET_BIT (PWR->CR3, PWR_CR3_USBREGEN);
  1462. /* Get tick */
  1463. tickstart = HAL_GetTick ();
  1464. /* Wait till the USB regulator ready flag is set */
  1465. while (__HAL_PWR_GET_FLAG (PWR_FLAG_USB33RDY) == 0U)
  1466. {
  1467. if ((HAL_GetTick() - tickstart ) > PWR_FLAG_SETTING_DELAY)
  1468. {
  1469. return HAL_ERROR;
  1470. }
  1471. }
  1472. return HAL_OK;
  1473. }
  1474. /**
  1475. * @brief Disable the USB Regulator.
  1476. * @retval HAL status.
  1477. */
  1478. HAL_StatusTypeDef HAL_PWREx_DisableUSBReg (void)
  1479. {
  1480. uint32_t tickstart;
  1481. /* Disable the USB regulator */
  1482. CLEAR_BIT (PWR->CR3, PWR_CR3_USBREGEN);
  1483. /* Get tick */
  1484. tickstart = HAL_GetTick ();
  1485. /* Wait till the USB regulator ready flag is reset */
  1486. while(__HAL_PWR_GET_FLAG (PWR_FLAG_USB33RDY) != 0U)
  1487. {
  1488. if ((HAL_GetTick() - tickstart ) > PWR_FLAG_SETTING_DELAY)
  1489. {
  1490. return HAL_ERROR;
  1491. }
  1492. }
  1493. return HAL_OK;
  1494. }
  1495. /**
  1496. * @brief Enable the USB voltage level detector.
  1497. * @retval None.
  1498. */
  1499. void HAL_PWREx_EnableUSBVoltageDetector (void)
  1500. {
  1501. /* Enable the USB voltage detector */
  1502. SET_BIT (PWR->CR3, PWR_CR3_USB33DEN);
  1503. }
  1504. /**
  1505. * @brief Disable the USB voltage level detector.
  1506. * @retval None.
  1507. */
  1508. void HAL_PWREx_DisableUSBVoltageDetector (void)
  1509. {
  1510. /* Disable the USB voltage detector */
  1511. CLEAR_BIT (PWR->CR3, PWR_CR3_USB33DEN);
  1512. }
  1513. /**
  1514. * @brief Enable the Battery charging.
  1515. * @note When VDD is present, charge the external battery through an internal
  1516. * resistor.
  1517. * @param ResistorValue : Specifies the charging resistor.
  1518. * This parameter can be one of the following values :
  1519. * @arg PWR_BATTERY_CHARGING_RESISTOR_5 : 5 KOhm resistor.
  1520. * @arg PWR_BATTERY_CHARGING_RESISTOR_1_5 : 1.5 KOhm resistor.
  1521. * @retval None.
  1522. */
  1523. void HAL_PWREx_EnableBatteryCharging (uint32_t ResistorValue)
  1524. {
  1525. /* Check the parameter */
  1526. assert_param (IS_PWR_BATTERY_RESISTOR_SELECT (ResistorValue));
  1527. /* Specify the charging resistor */
  1528. MODIFY_REG (PWR->CR3, PWR_CR3_VBRS, ResistorValue);
  1529. /* Enable the Battery charging */
  1530. SET_BIT (PWR->CR3, PWR_CR3_VBE);
  1531. }
  1532. /**
  1533. * @brief Disable the Battery charging.
  1534. * @retval None.
  1535. */
  1536. void HAL_PWREx_DisableBatteryCharging (void)
  1537. {
  1538. /* Disable the Battery charging */
  1539. CLEAR_BIT (PWR->CR3, PWR_CR3_VBE);
  1540. }
  1541. #if defined (PWR_CR1_BOOSTE)
  1542. /**
  1543. * @brief Enable the booster to guarantee the analog switch AC performance when
  1544. * the VDD supply voltage is below 2V7.
  1545. * @note The VDD supply voltage can be monitored through the PVD and the PLS
  1546. * field bits.
  1547. * @retval None.
  1548. */
  1549. void HAL_PWREx_EnableAnalogBooster (void)
  1550. {
  1551. /* Enable the Analog voltage */
  1552. SET_BIT (PWR->CR1, PWR_CR1_AVD_READY);
  1553. /* Enable VDDA booster */
  1554. SET_BIT (PWR->CR1, PWR_CR1_BOOSTE);
  1555. }
  1556. /**
  1557. * @brief Disable the analog booster.
  1558. * @retval None.
  1559. */
  1560. void HAL_PWREx_DisableAnalogBooster (void)
  1561. {
  1562. /* Disable VDDA booster */
  1563. CLEAR_BIT (PWR->CR1, PWR_CR1_BOOSTE);
  1564. /* Disable the Analog voltage */
  1565. CLEAR_BIT (PWR->CR1, PWR_CR1_AVD_READY);
  1566. }
  1567. #endif /* defined (PWR_CR1_BOOSTE) */
  1568. /**
  1569. * @}
  1570. */
  1571. /** @defgroup PWREx_Exported_Functions_Group4 Power Monitoring functions
  1572. * @brief Power Monitoring functions
  1573. *
  1574. @verbatim
  1575. ===============================================================================
  1576. ##### Power Monitoring functions #####
  1577. ===============================================================================
  1578. *** VBAT and Temperature supervision ***
  1579. ========================================
  1580. [..]
  1581. (+) The VBAT battery voltage supply can be monitored by comparing it with
  1582. two threshold levels: VBAThigh and VBATlow. VBATH flag and VBATL flags
  1583. in the PWR control register 2 (PWR_CR2), indicate if VBAT is higher or
  1584. lower than the threshold.
  1585. (+) The temperature can be monitored by comparing it with two threshold
  1586. levels, TEMPhigh and TEMPlow. TEMPH and TEMPL flags, in the PWR
  1587. control register 2 (PWR_CR2), indicate whether the device temperature
  1588. is higher or lower than the threshold.
  1589. (+) The VBAT and the temperature monitoring is enabled by
  1590. HAL_PWREx_EnableMonitoring() function and disabled by
  1591. HAL_PWREx_DisableMonitoring() function.
  1592. (+) The HAL_PWREx_GetVBATLevel() function returns the VBAT level which can
  1593. be : PWR_VBAT_BELOW_LOW_THRESHOLD or PWR_VBAT_ABOVE_HIGH_THRESHOLD or
  1594. PWR_VBAT_BETWEEN_HIGH_LOW_THRESHOLD.
  1595. (+) The HAL_PWREx_GetTemperatureLevel() function returns the Temperature
  1596. level which can be :
  1597. PWR_TEMP_BELOW_LOW_THRESHOLD or PWR_TEMP_ABOVE_HIGH_THRESHOLD or
  1598. PWR_TEMP_BETWEEN_HIGH_LOW_THRESHOLD.
  1599. *** AVD configuration ***
  1600. =========================
  1601. [..]
  1602. (+) The AVD is used to monitor the VDDA power supply by comparing it to a
  1603. threshold selected by the AVD Level (ALS[3:0] bits in the PWR_CR1
  1604. register).
  1605. (+) A AVDO flag is available to indicate if VDDA is higher or lower
  1606. than the AVD threshold. This event is internally connected to the EXTI
  1607. line 16 to generate an interrupt if enabled.
  1608. It is configurable through __HAL_PWR_AVD_EXTI_ENABLE_IT() macro.
  1609. (+) The AVD is stopped in System Standby mode.
  1610. @endverbatim
  1611. * @{
  1612. */
  1613. /**
  1614. * @brief Enable the VBAT and temperature monitoring.
  1615. * @retval HAL status.
  1616. */
  1617. void HAL_PWREx_EnableMonitoring (void)
  1618. {
  1619. /* Enable the VBAT and Temperature monitoring */
  1620. SET_BIT (PWR->CR2, PWR_CR2_MONEN);
  1621. }
  1622. /**
  1623. * @brief Disable the VBAT and temperature monitoring.
  1624. * @retval HAL status.
  1625. */
  1626. void HAL_PWREx_DisableMonitoring (void)
  1627. {
  1628. /* Disable the VBAT and Temperature monitoring */
  1629. CLEAR_BIT (PWR->CR2, PWR_CR2_MONEN);
  1630. }
  1631. /**
  1632. * @brief Indicate whether the junction temperature is between, above or below
  1633. * the thresholds.
  1634. * @retval Temperature level.
  1635. */
  1636. uint32_t HAL_PWREx_GetTemperatureLevel (void)
  1637. {
  1638. uint32_t tempLevel, regValue;
  1639. /* Read the temperature flags */
  1640. regValue = READ_BIT (PWR->CR2, (PWR_CR2_TEMPH | PWR_CR2_TEMPL));
  1641. /* Check if the temperature is below the threshold */
  1642. if (regValue == PWR_CR2_TEMPL)
  1643. {
  1644. tempLevel = PWR_TEMP_BELOW_LOW_THRESHOLD;
  1645. }
  1646. /* Check if the temperature is above the threshold */
  1647. else if (regValue == PWR_CR2_TEMPH)
  1648. {
  1649. tempLevel = PWR_TEMP_ABOVE_HIGH_THRESHOLD;
  1650. }
  1651. /* The temperature is between the thresholds */
  1652. else
  1653. {
  1654. tempLevel = PWR_TEMP_BETWEEN_HIGH_LOW_THRESHOLD;
  1655. }
  1656. return tempLevel;
  1657. }
  1658. /**
  1659. * @brief Indicate whether the Battery voltage level is between, above or below
  1660. * the thresholds.
  1661. * @retval VBAT level.
  1662. */
  1663. uint32_t HAL_PWREx_GetVBATLevel (void)
  1664. {
  1665. uint32_t VBATLevel, regValue;
  1666. /* Read the VBAT flags */
  1667. regValue = READ_BIT (PWR->CR2, (PWR_CR2_VBATH | PWR_CR2_VBATL));
  1668. /* Check if the VBAT is below the threshold */
  1669. if (regValue == PWR_CR2_VBATL)
  1670. {
  1671. VBATLevel = PWR_VBAT_BELOW_LOW_THRESHOLD;
  1672. }
  1673. /* Check if the VBAT is above the threshold */
  1674. else if (regValue == PWR_CR2_VBATH)
  1675. {
  1676. VBATLevel = PWR_VBAT_ABOVE_HIGH_THRESHOLD;
  1677. }
  1678. /* The VBAT is between the thresholds */
  1679. else
  1680. {
  1681. VBATLevel = PWR_VBAT_BETWEEN_HIGH_LOW_THRESHOLD;
  1682. }
  1683. return VBATLevel;
  1684. }
  1685. #if defined (PWR_CSR1_MMCVDO)
  1686. /**
  1687. * @brief Get the VDDMMC voltage level.
  1688. * @retval The VDDMMC voltage level.
  1689. */
  1690. PWREx_MMC_VoltageLevel HAL_PWREx_GetMMCVoltage (void)
  1691. {
  1692. PWREx_MMC_VoltageLevel mmc_voltage;
  1693. /* Check voltage detector output on VDDMMC value */
  1694. if ((PWR->CSR1 & PWR_CSR1_MMCVDO_Msk) == 0U)
  1695. {
  1696. mmc_voltage = PWR_MMC_VOLTAGE_BELOW_1V2;
  1697. }
  1698. else
  1699. {
  1700. mmc_voltage = PWR_MMC_VOLTAGE_EQUAL_ABOVE_1V2;
  1701. }
  1702. return mmc_voltage;
  1703. }
  1704. #endif /* defined (PWR_CSR1_MMCVDO) */
  1705. /**
  1706. * @brief Configure the event mode and the voltage threshold detected by the
  1707. * Analog Voltage Detector (AVD).
  1708. * @param sConfigAVD : Pointer to an PWREx_AVDTypeDef structure that contains
  1709. * the configuration information for the AVD.
  1710. * @note Refer to the electrical characteristics of your device datasheet for
  1711. * more details about the voltage threshold corresponding to each
  1712. * detection level.
  1713. * @note For dual core devices, please ensure to configure the EXTI lines for
  1714. * the different Cortex-Mx through PWR_Exported_Macro provided by this
  1715. * driver. All combination are allowed: wake up only Cortex-M7, wake up
  1716. * only Cortex-M4 and wake up Cortex-M7 and Cortex-M4.
  1717. * @retval None.
  1718. */
  1719. void HAL_PWREx_ConfigAVD (PWREx_AVDTypeDef *sConfigAVD)
  1720. {
  1721. /* Check the parameters */
  1722. assert_param (IS_PWR_AVD_LEVEL (sConfigAVD->AVDLevel));
  1723. assert_param (IS_PWR_AVD_MODE (sConfigAVD->Mode));
  1724. /* Set the ALS[18:17] bits according to AVDLevel value */
  1725. MODIFY_REG (PWR->CR1, PWR_CR1_ALS, sConfigAVD->AVDLevel);
  1726. /* Clear any previous config */
  1727. #if !defined (DUAL_CORE)
  1728. __HAL_PWR_AVD_EXTI_DISABLE_EVENT ();
  1729. __HAL_PWR_AVD_EXTI_DISABLE_IT ();
  1730. #endif /* !defined (DUAL_CORE) */
  1731. __HAL_PWR_AVD_EXTI_DISABLE_RISING_EDGE ();
  1732. __HAL_PWR_AVD_EXTI_DISABLE_FALLING_EDGE ();
  1733. #if !defined (DUAL_CORE)
  1734. /* Configure the interrupt mode */
  1735. if ((sConfigAVD->Mode & AVD_MODE_IT) == AVD_MODE_IT)
  1736. {
  1737. __HAL_PWR_AVD_EXTI_ENABLE_IT ();
  1738. }
  1739. /* Configure the event mode */
  1740. if ((sConfigAVD->Mode & AVD_MODE_EVT) == AVD_MODE_EVT)
  1741. {
  1742. __HAL_PWR_AVD_EXTI_ENABLE_EVENT ();
  1743. }
  1744. #endif /* !defined (DUAL_CORE) */
  1745. /* Rising edge configuration */
  1746. if ((sConfigAVD->Mode & AVD_RISING_EDGE) == AVD_RISING_EDGE)
  1747. {
  1748. __HAL_PWR_AVD_EXTI_ENABLE_RISING_EDGE ();
  1749. }
  1750. /* Falling edge configuration */
  1751. if ((sConfigAVD->Mode & AVD_FALLING_EDGE) == AVD_FALLING_EDGE)
  1752. {
  1753. __HAL_PWR_AVD_EXTI_ENABLE_FALLING_EDGE ();
  1754. }
  1755. }
  1756. /**
  1757. * @brief Enable the Analog Voltage Detector (AVD).
  1758. * @retval None.
  1759. */
  1760. void HAL_PWREx_EnableAVD (void)
  1761. {
  1762. /* Enable the Analog Voltage Detector */
  1763. SET_BIT (PWR->CR1, PWR_CR1_AVDEN);
  1764. }
  1765. /**
  1766. * @brief Disable the Analog Voltage Detector(AVD).
  1767. * @retval None.
  1768. */
  1769. void HAL_PWREx_DisableAVD (void)
  1770. {
  1771. /* Disable the Analog Voltage Detector */
  1772. CLEAR_BIT (PWR->CR1, PWR_CR1_AVDEN);
  1773. }
  1774. /**
  1775. * @brief This function handles the PWR PVD/AVD interrupt request.
  1776. * @note This API should be called under the PVD_AVD_IRQHandler().
  1777. * @retval None
  1778. */
  1779. void HAL_PWREx_PVD_AVD_IRQHandler (void)
  1780. {
  1781. /* Check if the Programmable Voltage Detector is enabled (PVD) */
  1782. if(READ_BIT (PWR->CR1, PWR_CR1_PVDEN) != 0U)
  1783. {
  1784. #if defined (DUAL_CORE)
  1785. if (HAL_GetCurrentCPUID () == CM7_CPUID)
  1786. #endif /* defined (DUAL_CORE) */
  1787. {
  1788. /* Check PWR D1/CD EXTI flag */
  1789. if(__HAL_PWR_PVD_EXTI_GET_FLAG () != 0U)
  1790. {
  1791. /* PWR PVD interrupt user callback */
  1792. HAL_PWR_PVDCallback ();
  1793. /* Clear PWR EXTI D1/CD pending bit */
  1794. __HAL_PWR_PVD_EXTI_CLEAR_FLAG ();
  1795. }
  1796. }
  1797. #if defined (DUAL_CORE)
  1798. else
  1799. {
  1800. /* Check PWR EXTI D2 flag */
  1801. if (__HAL_PWR_PVD_EXTID2_GET_FLAG () != 0U)
  1802. {
  1803. /* PWR PVD interrupt user callback */
  1804. HAL_PWR_PVDCallback ();
  1805. /* Clear PWR EXTI D2 pending bit */
  1806. __HAL_PWR_PVD_EXTID2_CLEAR_FLAG();
  1807. }
  1808. }
  1809. #endif /* defined (DUAL_CORE) */
  1810. }
  1811. /* Check if the Analog Voltage Detector is enabled (AVD) */
  1812. if (READ_BIT (PWR->CR1, PWR_CR1_AVDEN) != 0U)
  1813. {
  1814. #if defined (DUAL_CORE)
  1815. if (HAL_GetCurrentCPUID () == CM7_CPUID)
  1816. #endif /* defined (DUAL_CORE) */
  1817. {
  1818. /* Check PWR EXTI D1/CD flag */
  1819. if (__HAL_PWR_AVD_EXTI_GET_FLAG () != 0U)
  1820. {
  1821. /* PWR AVD interrupt user callback */
  1822. HAL_PWREx_AVDCallback ();
  1823. /* Clear PWR EXTI D1/CD pending bit */
  1824. __HAL_PWR_AVD_EXTI_CLEAR_FLAG ();
  1825. }
  1826. }
  1827. #if defined (DUAL_CORE)
  1828. else
  1829. {
  1830. /* Check PWR EXTI D2 flag */
  1831. if (__HAL_PWR_AVD_EXTID2_GET_FLAG () != 0U)
  1832. {
  1833. /* PWR AVD interrupt user callback */
  1834. HAL_PWREx_AVDCallback ();
  1835. /* Clear PWR EXTI D2 pending bit */
  1836. __HAL_PWR_AVD_EXTID2_CLEAR_FLAG ();
  1837. }
  1838. }
  1839. #endif /* defined (DUAL_CORE) */
  1840. }
  1841. }
  1842. /**
  1843. * @brief PWR AVD interrupt callback.
  1844. * @retval None.
  1845. */
  1846. __weak void HAL_PWREx_AVDCallback (void)
  1847. {
  1848. /* NOTE : This function should not be modified, when the callback is needed,
  1849. the HAL_PWR_AVDCallback can be implemented in the user file
  1850. */
  1851. }
  1852. /**
  1853. * @}
  1854. */
  1855. /**
  1856. * @}
  1857. */
  1858. #endif /* HAL_PWR_MODULE_ENABLED */
  1859. /**
  1860. * @}
  1861. */
  1862. /**
  1863. * @}
  1864. */
  1865. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/