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.
 
 
 

1294 lines
45 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_pwr_ex.c
  4. * @author MCD Application Team
  5. * @version V1.2.0
  6. * @date 29-December-2017
  7. * @brief Extended PWR HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of PWR extension peripheral:
  10. * + Peripheral Extended features functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  16. *
  17. * Redistribution and use in source and binary forms, with or without modification,
  18. * are permitted provided that the following conditions are met:
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ******************************************************************************
  40. */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32h7xx_hal.h"
  43. /** @addtogroup STM32H7xx_HAL_Driver
  44. * @{
  45. */
  46. /** @defgroup PWREx PWREx
  47. * @brief PWR Extended HAL module driver
  48. * @{
  49. */
  50. #ifdef HAL_PWR_MODULE_ENABLED
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. /** @addtogroup PWREx_Private_Constants
  54. * @{
  55. */
  56. /** @defgroup PWREx_AVD_Mode_Mask PWR Extended AVD Mode Mask
  57. * @{
  58. */
  59. #define AVD_MODE_IT ((uint32_t)0x00010000U)
  60. #define AVD_MODE_EVT ((uint32_t)0x00020000U)
  61. #define AVD_RISING_EDGE ((uint32_t)0x00000001U)
  62. #define AVD_FALLING_EDGE ((uint32_t)0x00000002U)
  63. #define AVD_RISING_FALLING_EDGE ((uint32_t)0x00000003U)
  64. /**
  65. * @}
  66. */
  67. /** @defgroup PWREx_REG_SET_TIMEOUT PWR Extended Flag Setting Time Out Value
  68. * @{
  69. */
  70. #define PWR_FLAG_SETTING_DELAY_US ((uint32_t)1000U)
  71. /**
  72. * @}
  73. */
  74. /**
  75. * @}
  76. */
  77. /* Private macro -------------------------------------------------------------*/
  78. /* Private variables ---------------------------------------------------------*/
  79. /* Private function prototypes -----------------------------------------------*/
  80. /* Private functions ---------------------------------------------------------*/
  81. /* Exported types ------------------------------------------------------------*/
  82. /* Exported functions --------------------------------------------------------*/
  83. /** @defgroup PWREx_Exported_Functions PWREx Exported Functions
  84. * @{
  85. */
  86. /** @defgroup PWREx_Exported_Functions_Group1 Power supply control functions
  87. * @brief Power supply control functions
  88. *
  89. @verbatim
  90. ===============================================================================
  91. ##### Power supply control functions #####
  92. ===============================================================================
  93. *** Power supply configuration ***
  94. ==================================
  95. [..]
  96. When the system is powered on, the POR monitors VDD supply. Once VDD is above the
  97. POR threshold level, the voltage regulator is enabled in the default supply
  98. configuration:
  99. (+) The Voltage converter output level is set at 1.0 V in accordance with the VOS3
  100. level configured in PWR D3 domain control register (PWR_D3CR).
  101. (+) The system is kept in reset mode as long as VCORE is not ok.
  102. (+) Once VCORE is ok, the system is taken out of reset and the HSI oscillator is enabled.
  103. (+) Once the oscillator is stable, the system is initialized: Flash memory and option
  104. bytes are loaded and the CPU starts in Run* mode.
  105. (+) The software shall then initialize the system including supply configuration
  106. programming using the HAL_PWREx_ConfigSupply(SupplySource) with:
  107. (++) SupplySource:
  108. (+++) PWR_LDO_SUPPLY: VCORE Power Domains are supplied from the LDO according to
  109. VOS. LDO power mode (Main, LP, Off) will follow system low-power
  110. modes.
  111. (+++) PWR_EXTERNAL_SOURCE_SUPPLY: VCORE supplied from external source and LDO bypassed,
  112. voltage monitoring still active.
  113. (+) Once the supply configuration has been configured, the HAL_PWREx_ConfigSupply
  114. function checks the ACTVOSRDY bit in PWR control status register 1 (PWR_CSR1)
  115. to guarantee a valid voltage levels:
  116. (++) As long as ACTVOSRDY indicates that voltage levels are invalid, the system is in
  117. limited Run* mode, write accesses to the RAMs are not permitted and VOS shall
  118. not be changed.
  119. (++) Once ACTVOSRDY indicates that voltage levels are valid, the system is in normal
  120. Run mode, write accesses to RAMs are allowed and VOS can be changed.
  121. @endverbatim
  122. * @{
  123. */
  124. /**
  125. * @brief Configure the system Power Supply.
  126. * @param SupplySource: Specifies the Power Supply source to set after a system startup.
  127. * This parameter can be one of the following values:
  128. * @arg PWR_LDO_SUPPLY The LDO regulator supplies the Vcore Power Domains.
  129. *
  130. * @arg PWR_EXTERNAL_SOURCE_SUPPLY The LDO regulator is Bypassed.
  131. * The Vcore Power Domains are supplied from external source.
  132. * @retval HAL status.
  133. */
  134. HAL_StatusTypeDef HAL_PWREx_ConfigSupply(uint32_t SupplySource)
  135. {
  136. uint32_t tickstart = 0;
  137. /* Check the parameters */
  138. assert_param(IS_PWR_SUPPLY(SupplySource));
  139. /* Set the power supply configuration */
  140. MODIFY_REG(PWR->CR3, PWR_SUPPLY_CONFIG_MASK, SupplySource);
  141. /* Get tick */
  142. tickstart = HAL_GetTick();
  143. /* Wait till voltage level flag is set and supply configuration update flag is reset */
  144. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_ACTVOSRDY) && __HAL_PWR_GET_FLAG(PWR_FLAG_SCUEN))
  145. {
  146. if((HAL_GetTick() - tickstart ) > PWR_FLAG_SETTING_DELAY_US)
  147. {
  148. return HAL_TIMEOUT;
  149. }
  150. }
  151. return HAL_OK;
  152. }
  153. /**
  154. * @brief Get the power supply configuration.
  155. * @retval The supply configuration.
  156. */
  157. uint32_t HAL_PWREx_GetSupplyConfig(void)
  158. {
  159. return (PWR->CR3 & PWR_SUPPLY_CONFIG_MASK);
  160. }
  161. /**
  162. * @brief Configure the main internal regulator output voltage.
  163. * @param VoltageScaling: Specifies the regulator output voltage to achieve
  164. * a tradeoff between performance and power consumption.
  165. * This parameter can be one of the following values:
  166. * @arg PWR_REGULATOR_VOLTAGE_SCALE1: Regulator voltage output range 1 mode.
  167. * @arg PWR_REGULATOR_VOLTAGE_SCALE2: Regulator voltage output range 2 mode.
  168. * @arg PWR_REGULATOR_VOLTAGE_SCALE3: Regulator voltage output range 3 mode.
  169. * @note When moving from Range 1 to Range 2, the system frequency must be decreased
  170. * before calling HAL_PWREx_ControlVoltageScaling() API.
  171. * When moving from Range 2 to Range 1, the system frequency can be increased
  172. * after calling HAL_PWREx_ControlVoltageScaling() API.
  173. * @note When moving from a Range to an other one, the API waits for VOSRDY flag to be
  174. * set before returning the status. If the flag is not set within 1000 microseconds,
  175. * HAL_TIMEOUT status is reported.
  176. * @retval HAL Status
  177. */
  178. HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling(uint32_t VoltageScaling)
  179. {
  180. uint32_t tickstart = 0;
  181. assert_param(IS_PWR_REGULATOR_VOLTAGE(VoltageScaling));
  182. /* Set the voltage range */
  183. MODIFY_REG(PWR->D3CR, PWR_D3CR_VOS, VoltageScaling);
  184. /* Get tick */
  185. tickstart = HAL_GetTick();
  186. /* Wait until the VOSRDY flag is set */
  187. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY))
  188. {
  189. if((HAL_GetTick() - tickstart ) > PWR_FLAG_SETTING_DELAY_US)
  190. {
  191. return HAL_TIMEOUT;
  192. }
  193. }
  194. return HAL_OK;
  195. }
  196. /**
  197. * @brief Get the main internal regulator output voltage.
  198. * Reflecting the last VOS value applied to the PMU.
  199. * @retval The actual applied VOS for VDD11 Voltage Scaling selection.
  200. */
  201. uint32_t HAL_PWREx_GetVoltageRange(void)
  202. {
  203. return (PWR->CSR1 & PWR_CSR1_ACTVOS);
  204. }
  205. /**
  206. * @brief Configure the main internal regulator output voltage in STOP mode.
  207. * @param VoltageScaling: Specifies the regulator output voltage when the system enters
  208. * STOP mode to achieve a tradeoff between performance and power consumption.
  209. * This parameter can be one of the following values:
  210. * @arg PWR_REGULATOR_SVOS_SCALE3: Regulator voltage output range 3 mode.
  211. * @arg PWR_REGULATOR_SVOS_SCALE4: Regulator voltage output range 4 mode.
  212. * @arg PWR_REGULATOR_SVOS_SCALE5: Regulator voltage output range 5 mode.
  213. * @note The Stop mode voltage scaling for SVOS4 and SVOS5 sets the voltage regulator
  214. * in Low-power (LP) mode to further reduce power consumption.
  215. * When preselecting SVOS3, the use of the voltage regulator low-power mode (LP)
  216. * can be selected by LPDS register bit.
  217. * @note The selected SVOS4 and SVOS5 levels add an additional startup delay when exiting
  218. * from system Stop mode.
  219. * @retval HAL Status
  220. */
  221. HAL_StatusTypeDef HAL_PWREx_ControlStopModeVoltageScaling(uint32_t VoltageScaling)
  222. {
  223. assert_param(IS_PWR_STOP_MODE_REGULATOR_VOLTAGE(VoltageScaling));
  224. /* Set the stop mode voltage range */
  225. MODIFY_REG(PWR->CR1, PWR_CR1_SVOS, VoltageScaling);
  226. return HAL_OK;
  227. }
  228. /**
  229. * @brief Get the main internal regulator output voltage in STOP mode.
  230. * @retval The actual applied VOS for VDD11 Voltage Scaling selection.
  231. */
  232. uint32_t HAL_PWREx_GetStopModeVoltageRange(void)
  233. {
  234. return (PWR->CR1 & PWR_CR1_SVOS);
  235. }
  236. /**
  237. * @}
  238. */
  239. /** @defgroup PWREx_Exported_Functions_Group2 Low power control functions
  240. * @brief Low power control functions
  241. *
  242. @verbatim
  243. ===============================================================================
  244. ##### Low power control functions #####
  245. ===============================================================================
  246. *** Domains Low Power modes configuration ***
  247. =============================================
  248. [..]
  249. The system present 3 principles domains (D1, D2 and D3) that can be operated
  250. in low-power modes (DSTOP or DSTANDBY mode):
  251. (+) DSTOP mode to enters a domain to STOP mode:
  252. (++) D1 domain and/or D2 domain enters DSTOP mode only when the CPU
  253. subsystem is in CSTOP mode and has allocated peripheral in the domain.
  254. In DSTOP mode the domain bus matrix clock is stopped.
  255. (++) The system enters STOP mode using one of the following scenarios:
  256. (+++) D1 domain enters DSTANDBY mode (powered off) and D2, D3 domains enter DSTOP mode.
  257. (+++) D2 domain enters DSTANDBY mode (powered off) and D1, D3 domains enter DSTOP mode.
  258. (+++) D3 domain enters DSTANDBY mode (powered off) and D1, D2 domains enter DSTOP mode.
  259. (+++) D1 and D2 domains enter DSTANDBY mode (powered off) and D3 domain enters DSTOP mode.
  260. (+++) D1 and D3 domains enter DSTANDBY mode (powered off) and D2 domain enters DSTOP mode.
  261. (+++) D2 and D3 domains enter DSTANDBY mode (powered off) and D1 domain enters DSTOP mode.
  262. (+++) D1, D2 and D3 domains enter DSTOP mode.
  263. (++) When the system enters STOP mode, the clocks are stopped and the regulator is running
  264. in main or low power mode.
  265. (++) D3 domain can be kept in Run mode regardless of the CPU status when enter
  266. STOP mode by using HAL_PWREx_ConfigD3Domain(D3State) function.
  267. (+) DSTANDBY mode to enters a domain to STANDBY mode:
  268. (++) The DSTANDBY mode is entered when the PDDS_Dn bit in PWR CPU control register
  269. (PWR_CPUCR) for the Dn domain selects Standby mode.
  270. (++) The system enters STANDBY mode only when D1, D2 and D3 domains enter DSTANDBY mode.
  271. Consequently the VCORE supply regulator is powered off.
  272. *** DSTOP mode ***
  273. ==================
  274. [..]
  275. In DStop mode the domain bus matrix clock is stopped.
  276. The Flash memory can enter low-power Stop mode when it is enabled through FLPS in
  277. PWR_CR1 register. This allows a trade-off between domain DStop restart time and low
  278. power consumption.
  279. [..]
  280. In DStop mode domain peripherals using the LSI or LSE clock and peripherals having a
  281. kernel clock request are still able to operate.
  282. [..]
  283. Before entering DSTOP mode it is recommended to call SCB_CleanDCache function
  284. in order to clean the D-Cache and guarantee the data integrity for the SRAM memories.
  285. (+) Entry:
  286. The DSTOP mode is entered using the HAL_PWREx_EnterSTOPMode(Regulator, STOPEntry, Domain)
  287. function with:
  288. (++) Regulator:
  289. (+++) PWR_MAINREGULATOR_ON: Main regulator ON.
  290. (+++) PWR_LOWPOWERREGULATOR_ON: Low Power regulator ON.
  291. (++) STOPEntry:
  292. (+++) PWR_STOPENTRY_WFI: enter STOP mode with WFI instruction
  293. (+++) PWR_STOPENTRY_WFE: enter STOP mode with WFE instruction
  294. (++) Domain:
  295. (+++) PWR_D1_DOMAIN: Enters D1 domain to DSTOP mode.
  296. (+++) PWR_D2_DOMAIN: Enters D2 domain to DSTOP mode.
  297. (+++) PWR_D3_DOMAIN: Enters D3 domain to DSTOP mode.
  298. (+) Exit:
  299. Any EXTI Line (Internal or External) configured in Interrupt/Event mode.
  300. *** DSTANDBY mode ***
  301. ====================
  302. [..]
  303. In DStandby mode:
  304. (+) The domain bus matrix clock is stopped.
  305. (+) The domain is powered down and the domain RAM and register contents are lost.
  306. [..]
  307. Before entering DSTANDBY mode it is recommended to call SCB_CleanDCache function
  308. in order to clean the D-Cache and guarantee the data integrity for the SRAM memories.
  309. (+) Entry:
  310. The DSTANDBY mode is entered using the HAL_PWREx_EnterSTANDBYMode(Domain) function with:
  311. (++) Domain:
  312. (+++) PWR_D1_DOMAIN: Enters D1 domain to DSTANDBY mode.
  313. (+++) PWR_D2_DOMAIN: Enters D2 domain to DSTANDBY mode.
  314. (+++) PWR_D3_DOMAIN: Enters D3 domain to DSTANDBY mode.
  315. (+) Exit:
  316. WKUP pin rising or falling edge, RTC alarm (Alarm A and Alarm B), RTC
  317. wakeup, tamper event, time stamp event, external reset in NRST pin, IWDG reset.
  318. *** Keep D3 in RUN mode ***
  319. ===========================
  320. [..]
  321. D3 domain can be kept in Run mode regardless of the CPU status when enter
  322. STOP mode by using HAL_PWREx_ConfigD3Domain(D3State) function with:
  323. (+) D3State:
  324. (++) PWR_D3_DOMAIN_STOP: D3 domain will follow the CPU sub-system mode.
  325. (++) PWR_D3_DOMAIN_RUN: D3 domain remains in Run mode regardless of CPU subsystem mode.
  326. *** FLASH Power Down configuration ****
  327. =======================================
  328. [..]
  329. By setting the FLPS bit in the PWR_CR1 register using the HAL_PWREx_EnableFlashPowerDown()
  330. function, the Flash memory also enters power down mode when the device enters Stop mode.
  331. When the Flash memory is in power down mode, an additional startup delay is incurred when
  332. waking up from Stop mode.
  333. *** Wakeup Pins configuration ****
  334. ===================================
  335. [..]
  336. Wakeup pins allow the system to exit from Standby mode. The configuration of
  337. wakeup pins is done with the HAL_PWREx_EnableWakeUpPin(sPinParams) function with:
  338. (+) sPinParams: structure to enable and configure a wakeup pin:
  339. (++) WakeUpPin: Wakeup pin to be enabled.
  340. (++) PinPolarity: Wakeup pin polarity (rising or falling edge).
  341. (++) PinPull: Wakeup pin pull (no pull, pull-up or pull-down).
  342. [..]
  343. The wakeup pins are internally connected to the EXTI lines [55-60] to generate an interrupt
  344. if enabled. The EXTI lines configuration is done by the HAL_EXTI_Dx_EventInputConfig() functions
  345. defined in the stm32h7xxhal.c file.
  346. [..]
  347. When a wakeup pin event is received the HAL_PWREx_WAKEUP_PIN_IRQHandler is called
  348. and the appropriate flag is set in the PWR_WKUPFR register. Then in the HAL_PWREx_WAKEUP_PIN_IRQHandler
  349. function the wakeup pin flag will be cleared and the appropriate user callback will be called.
  350. The user can add his own code by customization of function pointer HAL_PWREx_WKUPx_Callback.
  351. @endverbatim
  352. * @{
  353. */
  354. /**
  355. * @brief Enter a Domain to DSTOP mode.
  356. * @note In DStop mode the domain bus matrix clock is stopped.
  357. * @note The system D3 domain enters Stop mode only when the CPU subsystem is in CStop mode,
  358. * the EXTI wakeup sources are inactive and at least one PDDS_Dn bit in PWR CPU
  359. * control register (PWR_CPUCR) for any domain request Stop.
  360. * @note In system D3 domain Stop mode, D1 domain and D2 domain are either in DStop and/or
  361. * DStandby mode.
  362. * @note Before entering DSTOP mode it is recommended to call SCB_CleanDCache function
  363. * in order to clean the D-Cache and guarantee the data integrity for the SRAM memories.
  364. * @note In System Stop mode, the domain peripherals that use the LSI or LSE clock, and the
  365. * peripherals that have a kernel clock request to select HSI or CSI as source,
  366. * are still able to operate.
  367. * @param Regulator: Specifies the regulator state in Stop mode.
  368. * This parameter can be one of the following values:
  369. * @arg PWR_MAINREGULATOR_ON: Stop mode with regulator ON
  370. * @arg PWR_LOWPOWERREGULATOR_ON: Stop mode with low power regulator ON
  371. * @param STOPEntry: Specifies if Stop mode in entered with WFI or WFE instruction.
  372. * This parameter can be one of the following values:
  373. * @arg PWR_STOPENTRY_WFI: Enter DStop mode with WFI instruction
  374. * @arg PWR_STOPENTRY_WFE: Enter DStop mode with WFE instruction
  375. * @param Domain: Specifies the Domain to enter STOP mode.
  376. * This parameter can be one of the following values:
  377. * @arg PWR_D1_DOMAIN: Enter D1 Domain to DSTOP mode.
  378. * @arg PWR_D2_DOMAIN: Enter D2 Domain to DSTOP mode.
  379. * @arg PWR_D3_DOMAIN: Enter D3 Domain to DSTOP mode.
  380. * @retval None
  381. */
  382. void HAL_PWREx_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry, uint32_t Domain)
  383. {
  384. /* Check the parameters */
  385. assert_param(IS_PWR_REGULATOR(Regulator));
  386. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  387. assert_param(IS_PWR_DOMAIN(Domain));
  388. /* Select the regulator state in Stop mode */
  389. MODIFY_REG(PWR->CR1, PWR_CR1_LPDS, Regulator);
  390. /* Select the domain Power Down DeepSleep */
  391. if (Domain == PWR_D1_DOMAIN)
  392. {
  393. /* Keep DSTOP mode when D1 domain enters Deepsleep */
  394. CLEAR_BIT(PWR->CPUCR, PWR_CPUCR_PDDS_D1);
  395. /* Set SLEEPDEEP bit of Cortex System Control Register */
  396. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  397. /* Ensure that all instructions done before entering STOP mode */
  398. __DSB();
  399. __ISB();
  400. /* Select Stop mode entry */
  401. if(STOPEntry == PWR_STOPENTRY_WFI)
  402. {
  403. /* Request Wait For Interrupt */
  404. __WFI();
  405. }
  406. else
  407. {
  408. /* Request Wait For Event */
  409. __SEV();
  410. __WFE();
  411. __WFE();
  412. }
  413. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  414. SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  415. }
  416. else if (Domain == PWR_D2_DOMAIN)
  417. {
  418. /* Keep DSTOP mode when D2 domain enters Deepsleep */
  419. CLEAR_BIT(PWR->CPUCR, PWR_CPUCR_PDDS_D2);
  420. }
  421. else
  422. {
  423. /* Keep DSTOP mode when D3 domain enters Deepsleep */
  424. CLEAR_BIT(PWR->CPUCR, PWR_CPUCR_PDDS_D3);
  425. }
  426. }
  427. /**
  428. * @brief Enter a Domain to DSTANDBY mode.
  429. * @note The DStandby mode is entered when all PDDS_Dn bits in PWR_CPUCR for the Dn domain
  430. * select Standby mode. When the system enters Standby mode, the voltage regulator
  431. * is disabled.
  432. * @note When D2 or D3 domain is in DStandby mode and the CPU sets the domain PDDS_Dn
  433. * bit to select Stop mode, the domain remains in DStandby mode. The domain will only
  434. * exit DStandby when the CPU allocates a peripheral in the domain.
  435. * @note The system D3 domain enters Standby mode only when the D1 and D2 domain are in
  436. * DStandby.
  437. * @note Before entering DSTANDBY mode it is recommended to call SCB_CleanDCache function
  438. * in order to clean the D-Cache and guarantee the data integrity for the SRAM memories.
  439. * @param Domain: Specifies the Domain to enter to STANDBY mode.
  440. * This parameter can be one of the following values:
  441. * @arg PWR_D1_DOMAIN: Enter D1 Domain to DSTANDBY mode.
  442. * @arg PWR_D2_DOMAIN: Enter D2 Domain to DSTANDBY mode.
  443. * @arg PWR_D3_DOMAIN: Enter D3 Domain to DSTANDBY mode.
  444. * @retval None
  445. */
  446. void HAL_PWREx_EnterSTANDBYMode(uint32_t Domain)
  447. {
  448. /* Check the parameters */
  449. assert_param(IS_PWR_DOMAIN(Domain));
  450. /* Select the domain Power Down DeepSleep */
  451. if (Domain == PWR_D1_DOMAIN)
  452. {
  453. /* Allow DSTANDBY mode when D1 domain enters to Deepsleep */
  454. SET_BIT(PWR-> CPUCR, PWR_CPUCR_PDDS_D1);
  455. /* Set SLEEPDEEP bit of Cortex System Control Register */
  456. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  457. /* This option is used to ensure that store operations are completed */
  458. #if defined ( __CC_ARM)
  459. __force_stores();
  460. #endif
  461. /* Request Wait For Interrupt */
  462. __WFI();
  463. }
  464. else if (Domain == PWR_D2_DOMAIN)
  465. {
  466. /* Allow DSTANDBY mode when D2 domain enters to Deepsleep */
  467. SET_BIT(PWR-> CPUCR, PWR_CPUCR_PDDS_D2);
  468. }
  469. else
  470. {
  471. /* Allow DSTANDBY mode when D3 domain enters to Deepsleep */
  472. SET_BIT(PWR-> CPUCR, PWR_CPUCR_PDDS_D3);
  473. }
  474. }
  475. /**
  476. * @brief Configure the D3 Domain state when the CPU is in low power mode.
  477. * @param D3State: Specifies the D3 state.
  478. * This parameter can be one of the following values:
  479. * @arg PWR_D3_DOMAIN_STOP: D3 domain will follow the CPU sub-system mode.
  480. * @arg PWR_D3_DOMAIN_RUN : D3 domain will stay in RUN mode regardless of the
  481. * CPU sub-system mode.
  482. * @retval None
  483. */
  484. void HAL_PWREx_ConfigD3Domain(uint32_t D3State)
  485. {
  486. /* Check the parameters */
  487. assert_param(IS_D3_STATE(D3State));
  488. /* Keep D3 in run mode */
  489. MODIFY_REG(PWR->CPUCR, PWR_CPUCR_RUN_D3, D3State);
  490. }
  491. /**
  492. * @brief Enable the Flash Power Down in Stop mode.
  493. * @retval None
  494. */
  495. void HAL_PWREx_EnableFlashPowerDown(void)
  496. {
  497. /* Enable the Flash Power Down */
  498. SET_BIT(PWR->CR1, PWR_CR1_FLPS);
  499. }
  500. /**
  501. * @brief Disable the Flash Power Down in Stop mode.
  502. * @retval None
  503. */
  504. void HAL_PWREx_DisableFlashPowerDown(void)
  505. {
  506. /* Disable the Flash Power Down */
  507. CLEAR_BIT(PWR->CR1, PWR_CR1_FLPS);
  508. }
  509. /**
  510. * @brief Enable the Wake-up PINx functionality.
  511. * @param sPinParams: pointer to an PWREx_WakeupPinTypeDef structure that contains
  512. * the configuration informations for the wake-up Pin.
  513. * @retval None
  514. */
  515. void HAL_PWREx_EnableWakeUpPin(PWREx_WakeupPinTypeDef *sPinParams)
  516. {
  517. uint32_t pinConfig;
  518. uint32_t regMask;
  519. const uint32_t pullMask = PWR_WKUPEPR_WKUPPUPD1;
  520. /* Check the parameters */
  521. assert_param(IS_PWR_WAKEUP_PIN(sPinParams->WakeUpPin));
  522. assert_param(IS_PWR_WAKEUP_PIN_POLARITY(sPinParams->PinPolarity));
  523. assert_param(IS_PWR_WAKEUP_PIN_PULL(sPinParams->PinPull));
  524. pinConfig = sPinParams->WakeUpPin | \
  525. (sPinParams->PinPolarity << (POSITION_VAL(sPinParams->WakeUpPin) + PWR_WAKEUP_PINS_POLARITY_REGISTER_OFFSET)) | \
  526. (sPinParams->PinPull << ((POSITION_VAL(sPinParams->WakeUpPin) * PWR_WAKEUP_PINS_PULL_POSITION_OFFSET) + PWR_WAKEUP_PINS_PULL_REGISTER_OFFSET));
  527. regMask = sPinParams->WakeUpPin | \
  528. (PWR_WKUPEPR_WKUPP_1 << POSITION_VAL(sPinParams->WakeUpPin)) | \
  529. (pullMask << (POSITION_VAL(sPinParams->WakeUpPin) * PWR_WAKEUP_PINS_PULL_POSITION_OFFSET));
  530. /* Enable and Specify the Wake-Up pin polarity and the pull configuration
  531. for the event detection (rising or falling edge) */
  532. MODIFY_REG(PWR->WKUPEPR, regMask, pinConfig);
  533. /* Configure the Wakeup Pin EXTI Line */
  534. MODIFY_REG(EXTI_D1->IMR2, PWR_EXTI_WAKEUP_PINS_MASK, (sPinParams->WakeUpPin << PWR_EXTI_WAKEUP_PINS_PULL_POSITION_OFFSET));
  535. }
  536. /**
  537. * @brief Disable the Wake-up PINx functionality.
  538. * @param WakeUpPin: Specifies the Wake-Up pin to be disabled.
  539. * This parameter can be one of the following values:
  540. * @arg PWR_WAKEUP_PIN1: Disable PA0 wake-up PIN.
  541. * @arg PWR_WAKEUP_PIN2: Disable PA2 wake-up PIN..
  542. * @arg PWR_WAKEUP_PIN3: Disable PI8 wake-up PIN..
  543. * @arg PWR_WAKEUP_PIN4: Disable PC13 wake-up PIN..
  544. * @arg PWR_WAKEUP_PIN5: Disable PI11 wake-up PIN..
  545. * @arg PWR_WAKEUP_PIN6: Disable PC1 wake-up PIN..
  546. * @retval None
  547. */
  548. void HAL_PWREx_DisableWakeUpPin(uint32_t WakeUpPin)
  549. {
  550. /* Check the parameters */
  551. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPin));
  552. /* Disable the WakeUpPin */
  553. CLEAR_BIT(PWR->WKUPEPR, WakeUpPin);
  554. }
  555. /**
  556. * @brief Get the Wake-Up Pin flag.
  557. * @param WakeUpFlag: Specifies the Wake-Up PIN flag to check.
  558. * This parameter can be one of the following values:
  559. * @arg PWR_WAKEUP_FLAG1: A wakeup event was received from PA0.
  560. * @arg PWR_WAKEUP_FLAG2: A wakeup event was received from PA2.
  561. * @arg PWR_WAKEUP_FLAG3: A wakeup event was received from PC1.
  562. * @arg PWR_WAKEUP_FLAG4: A wakeup event was received from PC13.
  563. * @arg PWR_WAKEUP_FLAG5: A wakeup event was received from PI8.
  564. * @arg PWR_WAKEUP_FLAG6: A wakeup event was received from PI11.
  565. * @retval The Wake-Up pin flag.
  566. */
  567. uint32_t HAL_PWREx_GetWakeupFlag(uint32_t WakeUpFlag)
  568. {
  569. /* Check the parameters */
  570. assert_param(IS_PWR_WAKEUP_FLAG(WakeUpFlag));
  571. return (PWR->WKUPFR & WakeUpFlag);
  572. }
  573. /**
  574. * @brief Clear the Wake-Up pin flag.
  575. * @param WakeUpFlag: Specifies the Wake-Up PIN flag to clear.
  576. * This parameter can be one of the following values:
  577. * @arg PWR_WAKEUP_FLAG1: Clear the wakeup event received from PA0.
  578. * @arg PWR_WAKEUP_FLAG2: Clear the wakeup event received from PA2.
  579. * @arg PWR_WAKEUP_FLAG3: Clear the wakeup event received from PC1.
  580. * @arg PWR_WAKEUP_FLAG4: Clear the wakeup event received from PC13.
  581. * @arg PWR_WAKEUP_FLAG5: Clear the wakeup event received from PI8.
  582. * @arg PWR_WAKEUP_FLAG6: Clear the wakeup event received from PI11.
  583. * @retval HAL status.
  584. */
  585. HAL_StatusTypeDef HAL_PWREx_ClearWakeupFlag(uint32_t WakeUpFlag)
  586. {
  587. /* Check the parameters */
  588. assert_param(IS_PWR_WAKEUP_FLAG(WakeUpFlag));
  589. SET_BIT(PWR->WKUPCR, WakeUpFlag);
  590. if((PWR->WKUPFR & WakeUpFlag) != RESET)
  591. {
  592. return HAL_ERROR;
  593. }
  594. return HAL_OK;
  595. }
  596. /**
  597. * @brief This function handles the PWR WAKEUP PIN interrupt request.
  598. * @note This API should be called under the WAKEUP_PIN_IRQHandler().
  599. * @retval None
  600. */
  601. void HAL_PWREx_WAKEUP_PIN_IRQHandler(void)
  602. {
  603. /* Wakeup pin EXTI line interrupt detected */
  604. if(READ_BIT(PWR->WKUPFR, PWR_WKUPFR_WKUPF1) != RESET)
  605. {
  606. /* Clear PWR WKUPF1 flag */
  607. SET_BIT(PWR->WKUPCR, PWR_WKUPCR_WKUPC1);
  608. /* PWR WKUP1 interrupt user callback */
  609. HAL_PWREx_WKUP1_Callback();
  610. }
  611. else if(READ_BIT(PWR->WKUPFR, PWR_WKUPFR_WKUPF2) != RESET)
  612. {
  613. /* Clear PWR WKUPF2 flag */
  614. SET_BIT(PWR->WKUPCR, PWR_WKUPCR_WKUPC2);
  615. /* PWR WKUP2 interrupt user callback */
  616. HAL_PWREx_WKUP2_Callback();
  617. }
  618. else if(READ_BIT(PWR->WKUPFR, PWR_WKUPFR_WKUPF3) != RESET)
  619. {
  620. /* Clear PWR WKUPF3 flag */
  621. SET_BIT(PWR->WKUPCR, PWR_WKUPCR_WKUPC3);
  622. /* PWR WKUP3 interrupt user callback */
  623. HAL_PWREx_WKUP3_Callback();
  624. }
  625. else if(READ_BIT(PWR->WKUPFR, PWR_WKUPFR_WKUPF4) != RESET)
  626. {
  627. /* Clear PWR WKUPF4 flag */
  628. SET_BIT(PWR->WKUPCR, PWR_WKUPCR_WKUPC4);
  629. /* PWR WKUP4 interrupt user callback */
  630. HAL_PWREx_WKUP4_Callback();
  631. }
  632. else if(READ_BIT(PWR->WKUPFR, PWR_WKUPFR_WKUPF5) != RESET)
  633. {
  634. /* Clear PWR WKUPF5 flag */
  635. SET_BIT(PWR->WKUPCR, PWR_WKUPCR_WKUPC5);
  636. /* PWR WKUP5 interrupt user callback */
  637. HAL_PWREx_WKUP5_Callback();
  638. }
  639. else
  640. {
  641. /* Clear PWR WKUPF6 flag */
  642. SET_BIT(PWR->WKUPCR, PWR_WKUPCR_WKUPC6);
  643. /* PWR WKUP6 interrupt user callback */
  644. HAL_PWREx_WKUP6_Callback();
  645. }
  646. }
  647. /**
  648. * @brief PWR WKUP1 interrupt callback
  649. * @retval None
  650. */
  651. __weak void HAL_PWREx_WKUP1_Callback(void)
  652. {
  653. /* NOTE : This function Should not be modified, when the callback is needed,
  654. the HAL_PWREx_WKUP1Callback could be implemented in the user file
  655. */
  656. }
  657. /**
  658. * @brief PWR WKUP2 interrupt callback
  659. * @retval None
  660. */
  661. __weak void HAL_PWREx_WKUP2_Callback(void)
  662. {
  663. /* NOTE : This function Should not be modified, when the callback is needed,
  664. the HAL_PWREx_WKUP2Callback could be implemented in the user file
  665. */
  666. }
  667. /**
  668. * @brief PWR WKUP3 interrupt callback
  669. * @retval None
  670. */
  671. __weak void HAL_PWREx_WKUP3_Callback(void)
  672. {
  673. /* NOTE : This function Should not be modified, when the callback is needed,
  674. the HAL_PWREx_WKUP3Callback could be implemented in the user file
  675. */
  676. }
  677. /**
  678. * @brief PWR WKUP4 interrupt callback
  679. * @retval None
  680. */
  681. __weak void HAL_PWREx_WKUP4_Callback(void)
  682. {
  683. /* NOTE : This function Should not be modified, when the callback is needed,
  684. the HAL_PWREx_WKUP4Callback could be implemented in the user file
  685. */
  686. }
  687. /**
  688. * @brief PWR WKUP5 interrupt callback
  689. * @retval None
  690. */
  691. __weak void HAL_PWREx_WKUP5_Callback(void)
  692. {
  693. /* NOTE : This function Should not be modified, when the callback is needed,
  694. the HAL_PWREx_WKUP5Callback could be implemented in the user file
  695. */
  696. }
  697. /**
  698. * @brief PWR WKUP6 interrupt callback
  699. * @retval None
  700. */
  701. __weak void HAL_PWREx_WKUP6_Callback(void)
  702. {
  703. /* NOTE : This function Should not be modified, when the callback is needed,
  704. the HAL_PWREx_WKUP6Callback could be implemented in the user file
  705. */
  706. }
  707. /**
  708. * @}
  709. */
  710. /** @defgroup PWREx_Exported_Functions_Group3 Peripherals control functions
  711. * @brief Peripherals control functions
  712. *
  713. @verbatim
  714. ===============================================================================
  715. ##### Peripherals control functions #####
  716. ===============================================================================
  717. *** Main and Backup Regulators configuration ***
  718. ================================================
  719. [..]
  720. (+) The backup domain includes 4 Kbytes of backup SRAM accessible only from
  721. the CPU, and address in 32-bit, 16-bit or 8-bit mode. Its content is
  722. retained even in Standby or VBAT mode when the low power backup regulator
  723. is enabled. It can be considered as an internal EEPROM when VBAT is
  724. always present. You can use the HAL_PWREx_EnableBkUpReg() function to
  725. enable the low power backup regulator.
  726. (+) When the backup domain is supplied by VDD (analog switch connected to VDD)
  727. the backup SRAM is powered from VDD which replaces the VBAT power supply to
  728. save battery life.
  729. (+) The backup SRAM is not mass erased by a tamper event. It is read
  730. protected to prevent confidential data, such as cryptographic private
  731. key, from being accessed. The backup SRAM can be erased only through
  732. the Flash interface when a protection level change from level 1 to
  733. level 0 is requested.
  734. -@- Refer to the description of Read protection (RDP) in the Flash
  735. programming manual.
  736. (+) The main internal regulator can be configured to have a tradeoff between
  737. performance and power consumption when the device does not operate at
  738. the maximum frequency. This is done through HAL_PWREx_ControlVoltageScaling(VOS)
  739. function which configure the VOS bit in PWR_D3CR register.
  740. (+) The main internal regulator can be configured to operate in Low Power mode
  741. when the system enter STOP mode to further reduce power consumption.
  742. This is done through HAL_PWREx_ControlStopModeVoltageScaling(SVOS)
  743. function which configure the SVOS bit in PWR_CR1 register.
  744. The selected SVOS4 and SVOS5 levels add an additional startup delay when exiting from
  745. system Stop mode.
  746. -@- Refer to the product datasheets for more details.
  747. *** USB Regulator configuration ***
  748. ===================================
  749. [..]
  750. (+) The USB transceivers are supplied from a dedicated VDD33USB supply that can be
  751. provided either by the integrated USB regulator, or by an external USB supply.
  752. (+) The USB regulator is enabled by HAL_PWREx_EnableUSBReg() function, the VDD33USB
  753. is then provided from the USB regulator.
  754. (+) When the USB regulator is enabled, the VDD33USB supply level detector shall
  755. be enabled through HAL_PWREx_EnableUSBVoltageDetector() function.
  756. (+) The USB regulator is disabled through HAL_PWREx_DisableUSBReg() function and VDD33USB
  757. can be provided from an external supply. In this case VDD33USB and VDD50USB shall
  758. be connected together
  759. *** VBAT battery charging ***
  760. =============================
  761. [..]
  762. (+) When VDD is present, the external battery connected to VBAT can be charged through an
  763. internal resistance. VBAT charging can be performed either through a 5 KOhm resistor
  764. or through a 1.5 KOhm resistor.
  765. (+) VBAT charging is enabled by HAL_PWREx_EnableBatteryCharging(ResistorValue) function
  766. with:
  767. (++) ResistorValue:
  768. (+++) PWR_BATTERY_CHARGING_RESISTOR_5: 5 KOhm resistor.
  769. (+++) PWR_BATTERY_CHARGING_RESISTOR_1_5: 1.5 KOhm resistor.
  770. (+) VBAT charging is disabled by HAL_PWREx_DisableBatteryCharging() function.
  771. @endverbatim
  772. * @{
  773. */
  774. /**
  775. * @brief Enable the Backup Regulator.
  776. * @retval HAL status
  777. */
  778. HAL_StatusTypeDef HAL_PWREx_EnableBkUpReg(void)
  779. {
  780. uint32_t tickstart = 0;
  781. /* Enable the Backup regulator */
  782. SET_BIT(PWR->CR2, PWR_CR2_BREN);
  783. /* Get tick */
  784. tickstart = HAL_GetTick();
  785. /* Wait till Backup regulator ready flag is set */
  786. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_BRR))
  787. {
  788. if((HAL_GetTick() - tickstart ) > PWR_FLAG_SETTING_DELAY_US)
  789. {
  790. return HAL_TIMEOUT;
  791. }
  792. }
  793. return HAL_OK;
  794. }
  795. /**
  796. * @brief Disable the Backup Regulator.
  797. * @retval HAL status
  798. */
  799. HAL_StatusTypeDef HAL_PWREx_DisableBkUpReg(void)
  800. {
  801. uint32_t tickstart = 0;
  802. /* Disable the Backup regulator */
  803. CLEAR_BIT(PWR->CR2, PWR_CR2_BREN);
  804. /* Get tick */
  805. tickstart = HAL_GetTick();
  806. /* Wait till Backup regulator ready flag is reset */
  807. while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) != RESET)
  808. {
  809. if((HAL_GetTick() - tickstart ) > PWR_FLAG_SETTING_DELAY_US)
  810. {
  811. return HAL_TIMEOUT;
  812. }
  813. }
  814. return HAL_OK;
  815. }
  816. /**
  817. * @brief Enable the USB Regulator.
  818. * @retval HAL status
  819. */
  820. HAL_StatusTypeDef HAL_PWREx_EnableUSBReg(void)
  821. {
  822. uint32_t tickstart = 0;
  823. /* Enable the USB regulator */
  824. SET_BIT(PWR->CR3, PWR_CR3_USBREGEN);
  825. /* Get tick */
  826. tickstart = HAL_GetTick();
  827. /* Wait till the USB regulator ready flag is set */
  828. while(READ_BIT(PWR->CR3, PWR_CR3_USB33RDY) == RESET)
  829. {
  830. if((HAL_GetTick() - tickstart ) > PWR_FLAG_SETTING_DELAY_US)
  831. {
  832. return HAL_TIMEOUT;
  833. }
  834. }
  835. return HAL_OK;
  836. }
  837. /**
  838. * @brief Disable the USB Regulator.
  839. * @retval HAL status
  840. */
  841. HAL_StatusTypeDef HAL_PWREx_DisableUSBReg(void)
  842. {
  843. uint32_t tickstart = 0;
  844. /* Disable the USB regulator */
  845. CLEAR_BIT(PWR->CR3, PWR_CR3_USBREGEN);
  846. /* Get tick */
  847. tickstart = HAL_GetTick();
  848. /* Wait till the USB regulator ready flag is reset */
  849. while(READ_BIT(PWR->CR3, PWR_CR3_USB33RDY) != RESET)
  850. {
  851. if((HAL_GetTick() - tickstart ) > PWR_FLAG_SETTING_DELAY_US)
  852. {
  853. return HAL_TIMEOUT;
  854. }
  855. }
  856. return HAL_OK;
  857. }
  858. /**
  859. * @brief Enable the USB voltage level detector.
  860. * @retval None
  861. */
  862. void HAL_PWREx_EnableUSBVoltageDetector(void)
  863. {
  864. /* Enable the USB voltage detector */
  865. SET_BIT(PWR->CR3, PWR_CR3_USB33DEN);
  866. }
  867. /**
  868. * @brief Disable the USB voltage level detector.
  869. * @retval None
  870. */
  871. void HAL_PWREx_DisableUSBVoltageDetector(void)
  872. {
  873. /* Disable the USB voltage detector */
  874. CLEAR_BIT(PWR->CR3, PWR_CR3_USB33DEN);
  875. }
  876. /**
  877. * @brief Enable the Battery charging.
  878. * When VDD is present, charge the external battery through an internal resistor.
  879. * @param ResistorValue: Specifies the charging resistor.
  880. * This parameter can be one of the following values:
  881. * @arg PWR_BATTERY_CHARGING_RESISTOR_5: 5 KOhm resistor.
  882. * @arg PWR_BATTERY_CHARGING_RESISTOR_1_5: 1.5 KOhm resistor.
  883. * @retval None
  884. */
  885. void HAL_PWREx_EnableBatteryCharging(uint32_t ResistorValue)
  886. {
  887. assert_param(IS_PWR_BATTERY_RESISTOR_SELECT(ResistorValue));
  888. /* Specify the charging resistor */
  889. MODIFY_REG(PWR->CR3, PWR_CR3_VBRS, ResistorValue);
  890. /* Enable the Battery charging */
  891. SET_BIT(PWR->CR3, PWR_CR3_VBE);
  892. }
  893. /**
  894. * @brief Disable the Battery charging.
  895. * @retval None
  896. */
  897. void HAL_PWREx_DisableBatteryCharging(void)
  898. {
  899. /* Disable the Battery charging */
  900. CLEAR_BIT(PWR->CR3, PWR_CR3_VBE);
  901. }
  902. /**
  903. * @}
  904. */
  905. /** @defgroup PWREx_Exported_Functions_Group4 Power Monitoring functions
  906. * @brief Power Monitoring functions
  907. *
  908. @verbatim
  909. ===============================================================================
  910. ##### Power Monitoring functions #####
  911. ===============================================================================
  912. *** VBAT and Temperature supervision ***
  913. ========================================
  914. [..]
  915. (+) The VBAT battery voltage supply can be monitored by comparing it with two threshold
  916. levels: VBAThigh and VBATlow. VBATH flag and VBATL flags in the PWR control register 2
  917. (PWR_CR2), indicate if VBAT is higher or lower than the threshold.
  918. (+) The temperature can be monitored by comparing it with two threshold levels, TEMPhigh
  919. and TEMPlow. TEMPH and TEMPL flags, in the PWR control register 2 (PWR_CR2),
  920. indicate whether the device temperature is higher or lower than the threshold.
  921. (+) The VBAT and the temperature monitoring is enabled by HAL_PWREx_EnableMonitoring()
  922. function and disabled by HAL_PWREx_DisableMonitoring() function.
  923. (+) The HAL_PWREx_GetVBATLevel() function return the VBAT level which can be:
  924. PWR_VBAT_BELOW_LOW_THRESHOLD or PWR_VBAT_ABOVE_HIGH_THRESHOLD or
  925. PWR_VBAT_BETWEEN_HIGH_LOW_THRESHOLD.
  926. (+) The HAL_PWREx_GetTemperatureLevel() function return the Temperature level which
  927. can be: PWR_TEMP_BELOW_LOW_THRESHOLD or PWR_TEMP_ABOVE_HIGH_THRESHOLD or
  928. PWR_TEMP_BETWEEN_HIGH_LOW_THRESHOLD.
  929. *** AVD configuration ***
  930. =========================
  931. [..]
  932. (+) The AVD is used to monitor the VDDA power supply by comparing it to a
  933. threshold selected by the AVD Level (ALS[3:0] bits in the PWR_CR1 register).
  934. (+) A AVDO flag is available to indicate if VDDA is higher or lower
  935. than the AVD threshold. This event is internally connected to the EXTI
  936. line 16 to generate an interrupt if enabled.
  937. It is configurable through __HAL_PWR_AVD_EXTI_ENABLE_IT() macro.
  938. (+) The AVD is stopped in System Standby mode.
  939. @endverbatim
  940. * @{
  941. */
  942. /**
  943. * @brief Enable the VBAT and temperature monitoring.
  944. * @retval HAL status
  945. */
  946. void HAL_PWREx_EnableMonitoring(void)
  947. {
  948. /* Enable the VBAT and Temperature monitoring */
  949. SET_BIT(PWR->CR2, PWR_CR2_MONEN);
  950. }
  951. /**
  952. * @brief Disable the VBAT and temperature monitoring.
  953. * @retval HAL status
  954. */
  955. void HAL_PWREx_DisableMonitoring(void)
  956. {
  957. /* Disable the VBAT and Temperature monitoring */
  958. CLEAR_BIT(PWR->CR2, PWR_CR2_MONEN);
  959. }
  960. /**
  961. * @brief Indicate whether the junction temperature is between, above or below the threshold.
  962. * @retval Temperature level.
  963. */
  964. uint32_t HAL_PWREx_GetTemperatureLevel(void)
  965. {
  966. uint32_t tempLevel;
  967. uint32_t regValue;
  968. /* Read the temperature flags */
  969. regValue = PWR->CR2 & (PWR_CR2_TEMPH | PWR_CR2_TEMPL);
  970. /* Compare the read value to the temperature threshold */
  971. if(regValue == PWR_CR2_TEMPL)
  972. {
  973. tempLevel = PWR_TEMP_BELOW_LOW_THRESHOLD;
  974. }
  975. else if(regValue == PWR_CR2_TEMPH)
  976. {
  977. tempLevel = PWR_TEMP_ABOVE_HIGH_THRESHOLD;
  978. }
  979. else
  980. {
  981. tempLevel = PWR_TEMP_BETWEEN_HIGH_LOW_THRESHOLD;
  982. }
  983. return tempLevel;
  984. }
  985. /**
  986. * @brief Indicate whether the Battery voltage level is between, above or below the threshold.
  987. * @retval VBAT level.
  988. */
  989. uint32_t HAL_PWREx_GetVBATLevel(void)
  990. {
  991. uint32_t VBATLevel;
  992. uint32_t regValue;
  993. /* Read the VBAT flags */
  994. regValue = PWR->CR2 & (PWR_CR2_VBATH | PWR_CR2_VBATL);
  995. /* Compare the read value to the VBAT threshold */
  996. if(regValue == PWR_CR2_VBATL)
  997. {
  998. VBATLevel = PWR_VBAT_BELOW_LOW_THRESHOLD;
  999. }
  1000. else if(regValue == PWR_CR2_VBATH)
  1001. {
  1002. VBATLevel = PWR_VBAT_ABOVE_HIGH_THRESHOLD;
  1003. }
  1004. else
  1005. {
  1006. VBATLevel = PWR_VBAT_BETWEEN_HIGH_LOW_THRESHOLD;
  1007. }
  1008. return VBATLevel;
  1009. }
  1010. /**
  1011. * @brief Configure the analog voltage threshold detected by the Analog Voltage Detector(AVD).
  1012. * @param sConfigAVD: pointer to an PWR_AVDTypeDef structure that contains the configuration
  1013. * information for the AVD.
  1014. * @note Refer to the electrical characteristics of your device datasheet for more details
  1015. * about the voltage threshold corresponding to each detection level.
  1016. * @retval None
  1017. */
  1018. void HAL_PWREx_ConfigAVD(PWREx_AVDTypeDef *sConfigAVD)
  1019. {
  1020. /* Check the parameters */
  1021. assert_param(IS_PWR_AVD_LEVEL(sConfigAVD->AVDLevel));
  1022. assert_param(IS_PWR_AVD_MODE(sConfigAVD->Mode));
  1023. /* Set the ALS[18:17] bits according to AVDLevel value */
  1024. MODIFY_REG(PWR->CR1, PWR_CR1_ALS, sConfigAVD->AVDLevel);
  1025. /* Clear any previous config */
  1026. __HAL_PWR_AVD_EXTI_DISABLE_EVENT();
  1027. __HAL_PWR_AVD_EXTI_DISABLE_IT();
  1028. __HAL_PWR_AVD_EXTI_DISABLE_RISING_EDGE();
  1029. __HAL_PWR_AVD_EXTI_DISABLE_FALLING_EDGE();
  1030. /* Configure the interrupt mode */
  1031. if(AVD_MODE_IT == (sConfigAVD->Mode & AVD_MODE_IT))
  1032. {
  1033. __HAL_PWR_AVD_EXTI_ENABLE_IT();
  1034. }
  1035. /* Configure the event mode */
  1036. if(AVD_MODE_EVT == (sConfigAVD->Mode & AVD_MODE_EVT))
  1037. {
  1038. __HAL_PWR_AVD_EXTI_ENABLE_EVENT();
  1039. }
  1040. /* Configure the edge */
  1041. if(AVD_RISING_EDGE == (sConfigAVD->Mode & AVD_RISING_EDGE))
  1042. {
  1043. __HAL_PWR_AVD_EXTI_ENABLE_RISING_EDGE();
  1044. }
  1045. if(AVD_FALLING_EDGE == (sConfigAVD->Mode & AVD_FALLING_EDGE))
  1046. {
  1047. __HAL_PWR_AVD_EXTI_ENABLE_FALLING_EDGE();
  1048. }
  1049. }
  1050. /**
  1051. * @brief Enable the Analog Voltage Detector(AVD).
  1052. * @retval None
  1053. */
  1054. void HAL_PWREx_EnableAVD(void)
  1055. {
  1056. /* Enable the Analog Voltage Detector */
  1057. SET_BIT(PWR->CR1, PWR_CR1_AVDEN);
  1058. }
  1059. /**
  1060. * @brief Disable the Analog Voltage Detector(AVD).
  1061. * @retval None
  1062. */
  1063. void HAL_PWREx_DisableAVD(void)
  1064. {
  1065. /* Disable the Analog Voltage Detector */
  1066. CLEAR_BIT(PWR->CR1, PWR_CR1_AVDEN);
  1067. }
  1068. /**
  1069. * @brief This function handles the PWR PVD/AVD interrupt request.
  1070. * @note This API should be called under the PVD_AVD_IRQHandler().
  1071. * @retval None
  1072. */
  1073. void HAL_PWREx_PVD_AVD_IRQHandler(void)
  1074. {
  1075. /* PVD EXTI line interrupt detected */
  1076. if(READ_BIT(PWR->CR1, PWR_CR1_PVDEN) != RESET)
  1077. {
  1078. /* Check PWR EXTI flag */
  1079. if(__HAL_PWR_PVD_EXTI_GET_FLAG() != RESET)
  1080. {
  1081. /* PWR PVD interrupt user callback */
  1082. HAL_PWR_PVDCallback();
  1083. /* Clear PWR EXTI pending bit */
  1084. __HAL_PWR_PVD_EXTI_CLEAR_FLAG();
  1085. }
  1086. }
  1087. /* AVD EXTI line interrupt detected */
  1088. if(READ_BIT(PWR->CR1, PWR_CR1_AVDEN) != RESET)
  1089. {
  1090. /* Check PWR EXTI flag */
  1091. if(__HAL_PWR_AVD_EXTI_GET_FLAG() != RESET)
  1092. {
  1093. /* PWR AVD interrupt user callback */
  1094. HAL_PWREx_AVDCallback();
  1095. /* Clear PWR EXTI pending bit */
  1096. __HAL_PWR_AVD_EXTI_CLEAR_FLAG();
  1097. }
  1098. }
  1099. }
  1100. /**
  1101. * @brief PWR AVD interrupt callback
  1102. * @retval None
  1103. */
  1104. __weak void HAL_PWREx_AVDCallback(void)
  1105. {
  1106. /* NOTE : This function Should not be modified, when the callback is needed,
  1107. the HAL_PWR_AVDCallback could be implemented in the user file
  1108. */
  1109. }
  1110. /**
  1111. * @}
  1112. */
  1113. /**
  1114. * @}
  1115. */
  1116. #endif /* HAL_PWR_MODULE_ENABLED */
  1117. /**
  1118. * @}
  1119. */
  1120. /**
  1121. * @}
  1122. */
  1123. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/