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.
 
 
 

718 lines
28 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_pwr.c
  4. * @author MCD Application Team
  5. * @brief PWR HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Power Controller (PWR) peripheral:
  8. * + Initialization/de-initialization functions
  9. * + Peripheral Control functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  15. * All rights reserved.</center></h2>
  16. *
  17. * This software component is licensed by ST under BSD 3-Clause license,
  18. * the "License"; You may not use this file except in compliance with the
  19. * License. You may obtain a copy of the License at:
  20. * opensource.org/licenses/BSD-3-Clause
  21. *
  22. ******************************************************************************
  23. */
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32wbxx_hal.h"
  26. /** @addtogroup STM32WBxx_HAL_Driver
  27. * @{
  28. */
  29. /** @addtogroup PWR
  30. * @{
  31. */
  32. #ifdef HAL_PWR_MODULE_ENABLED
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. /* Private macro -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /** @addtogroup PWR_Private_Defines
  39. * @{
  40. */
  41. /** @defgroup PWR_Register_Reset_Values PWR Register Reset Values
  42. * @{
  43. */
  44. /* Definitions of PWR registers reset value */
  45. #define PWR_CR1_RESET_VALUE (0x00000200U)
  46. #define PWR_CR2_RESET_VALUE (0x00000000U)
  47. #define PWR_CR3_RESET_VALUE (0x00008000U)
  48. #define PWR_CR4_RESET_VALUE (0x00000000U)
  49. #define PWR_CR5_RESET_VALUE (0x00004204U)
  50. #define PWR_PUCRA_RESET_VALUE (0x00000000U)
  51. #define PWR_PDCRA_RESET_VALUE (0x00000000U)
  52. #define PWR_PUCRB_RESET_VALUE (0x00000000U)
  53. #define PWR_PDCRB_RESET_VALUE (0x00000000U)
  54. #define PWR_PUCRC_RESET_VALUE (0x00000000U)
  55. #define PWR_PDCRC_RESET_VALUE (0x00000000U)
  56. #define PWR_PUCRD_RESET_VALUE (0x00000000U)
  57. #define PWR_PDCRD_RESET_VALUE (0x00000000U)
  58. #define PWR_PUCRE_RESET_VALUE (0x00000000U)
  59. #define PWR_PDCRE_RESET_VALUE (0x00000000U)
  60. #define PWR_PUCRH_RESET_VALUE (0x00000000U)
  61. #define PWR_PDCRH_RESET_VALUE (0x00000000U)
  62. #define PWR_C2CR1_RESET_VALUE (0x00000000U)
  63. #define PWR_C2CR3_RESET_VALUE (0x00008000U)
  64. /**
  65. * @}
  66. */
  67. /**
  68. * @}
  69. */
  70. /* Private function prototypes -----------------------------------------------*/
  71. /* Exported functions --------------------------------------------------------*/
  72. /** @addtogroup PWR_Exported_Functions PWR Exported Functions
  73. * @{
  74. */
  75. /** @addtogroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
  76. * @brief Initialization and de-initialization functions
  77. *
  78. @verbatim
  79. ===============================================================================
  80. ##### Initialization and de-initialization functions #####
  81. ===============================================================================
  82. [..]
  83. @endverbatim
  84. * @{
  85. */
  86. /**
  87. * @brief Deinitialize the HAL PWR peripheral registers to their default reset values.
  88. * @retval None
  89. */
  90. void HAL_PWR_DeInit(void)
  91. {
  92. /* Apply reset values to all PWR registers */
  93. /* Note: Update of each register required since PWR global reset is not */
  94. /* available at RCC level on this STM32 serie. */
  95. LL_PWR_WriteReg(CR1, PWR_CR1_RESET_VALUE);
  96. LL_PWR_WriteReg(CR2, PWR_CR2_RESET_VALUE);
  97. LL_PWR_WriteReg(CR3, PWR_CR3_RESET_VALUE);
  98. LL_PWR_WriteReg(CR4, PWR_CR4_RESET_VALUE);
  99. LL_PWR_WriteReg(CR5, PWR_CR5_RESET_VALUE);
  100. LL_PWR_WriteReg(PUCRA, PWR_PUCRA_RESET_VALUE);
  101. LL_PWR_WriteReg(PDCRA, PWR_PDCRA_RESET_VALUE);
  102. LL_PWR_WriteReg(PUCRB, PWR_PUCRB_RESET_VALUE);
  103. LL_PWR_WriteReg(PDCRB, PWR_PDCRB_RESET_VALUE);
  104. LL_PWR_WriteReg(PUCRC, PWR_PUCRC_RESET_VALUE);
  105. LL_PWR_WriteReg(PDCRC, PWR_PDCRC_RESET_VALUE);
  106. #if defined(GPIOD)
  107. LL_PWR_WriteReg(PUCRD, PWR_PUCRD_RESET_VALUE);
  108. LL_PWR_WriteReg(PDCRD, PWR_PDCRD_RESET_VALUE);
  109. #endif
  110. LL_PWR_WriteReg(PUCRE, PWR_PUCRE_RESET_VALUE);
  111. LL_PWR_WriteReg(PDCRE, PWR_PDCRE_RESET_VALUE);
  112. LL_PWR_WriteReg(PUCRH, PWR_PUCRH_RESET_VALUE);
  113. LL_PWR_WriteReg(PDCRH, PWR_PDCRH_RESET_VALUE);
  114. LL_PWR_WriteReg(C2CR1, PWR_C2CR1_RESET_VALUE);
  115. LL_PWR_WriteReg(C2CR3, PWR_C2CR3_RESET_VALUE);
  116. /* Clear all flags */
  117. LL_PWR_WriteReg(SCR,
  118. LL_PWR_SCR_CC2HF
  119. | LL_PWR_SCR_C802AF
  120. | LL_PWR_SCR_CBLEAF
  121. | LL_PWR_SCR_CCRPEF
  122. | LL_PWR_SCR_C802WUF
  123. | LL_PWR_SCR_CBLEWUF
  124. #if defined(PWR_CR5_SMPSEN)
  125. | LL_PWR_SCR_CBORHF
  126. | LL_PWR_SCR_CSMPSFBF
  127. #endif
  128. | LL_PWR_SCR_CWUF
  129. );
  130. LL_PWR_WriteReg(EXTSCR,
  131. LL_PWR_EXTSCR_CCRPF
  132. | LL_PWR_EXTSCR_C2CSSF
  133. | LL_PWR_EXTSCR_C1CSSF
  134. );
  135. }
  136. /**
  137. * @brief Enable access to the backup domain
  138. * (RTC registers, RTC backup data registers).
  139. * @note After reset, the backup domain is protected against
  140. * possible unwanted write accesses.
  141. * @note RTCSEL that sets the RTC clock source selection is in the RTC back-up domain.
  142. * In order to set or modify the RTC clock, the backup domain access must be
  143. * disabled.
  144. * @note LSEON bit that switches on and off the LSE crystal belongs as well to the
  145. * back-up domain.
  146. * @retval None
  147. */
  148. void HAL_PWR_EnableBkUpAccess(void)
  149. {
  150. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  151. }
  152. /**
  153. * @brief Disable access to the backup domain
  154. * (RTC registers, RTC backup data registers).
  155. * @retval None
  156. */
  157. void HAL_PWR_DisableBkUpAccess(void)
  158. {
  159. CLEAR_BIT(PWR->CR1, PWR_CR1_DBP);
  160. }
  161. /**
  162. * @}
  163. */
  164. /** @addtogroup PWR_Exported_Functions_Group2 Peripheral Control functions
  165. * @brief Low Power modes configuration functions
  166. *
  167. @verbatim
  168. ===============================================================================
  169. ##### Peripheral Control functions #####
  170. ===============================================================================
  171. [..]
  172. *** PVD configuration ***
  173. =========================
  174. [..]
  175. (+) The PVD is used to monitor the VDD power supply by comparing it to a
  176. threshold selected by the PVD Level (PLS[2:0] bits in PWR_CR2 register).
  177. (+) PVDO flag is available to indicate if VDD/VDDA is higher or lower
  178. than the PVD threshold. This event is internally connected to the EXTI
  179. line16 and can generate an interrupt if enabled. This is done through
  180. __HAL_PVD_EXTI_ENABLE_IT() macro.
  181. (+) The PVD is stopped in Standby mode.
  182. *** WakeUp pin configuration ***
  183. ================================
  184. [..]
  185. (+) WakeUp pins are used to wakeup the system from Standby mode or Shutdown mode.
  186. The polarity of these pins can be set to configure event detection on high
  187. level (rising edge) or low level (falling edge).
  188. *** Low Power modes configuration ***
  189. =====================================
  190. [..]
  191. The devices feature 8 low-power modes:
  192. (+) Low-power Run mode: core and peripherals are running, main regulator off, low power regulator on.
  193. (+) Sleep mode: Cortex-M4 core stopped, peripherals kept running, main and low power regulators on.
  194. (+) Low-power Sleep mode: Cortex-M4 core stopped, peripherals kept running, main regulator off, low power regulator on.
  195. (+) Stop 0 mode: all clocks are stopped except LSI and LSE, main and low power regulators on.
  196. (+) Stop 1 mode: all clocks are stopped except LSI and LSE, main regulator off, low power regulator on.
  197. (+) Stop 2 mode: all clocks are stopped except LSI and LSE, main regulator off, low power regulator on, reduced set of waking up IPs compared to Stop 1 mode.
  198. (+) Standby mode with SRAM2: all clocks are stopped except LSI and LSE, SRAM2 content preserved, main regulator off, low power regulator on.
  199. (+) Standby mode without SRAM2: all clocks are stopped except LSI and LSE, main and low power regulators off.
  200. (+) Shutdown mode: all clocks are stopped except LSE, main and low power regulators off.
  201. *** Low-power run mode ***
  202. ==========================
  203. [..]
  204. (+) Entry: (from main run mode)
  205. (++) set LPR bit with HAL_PWREx_EnableLowPowerRunMode() API after having decreased the system clock below 2 MHz.
  206. (+) Exit:
  207. (++) clear LPR bit then wait for REGLP bit to be reset with HAL_PWREx_DisableLowPowerRunMode() API. Only
  208. then can the system clock frequency be increased above 2 MHz.
  209. *** Sleep mode / Low-power sleep mode ***
  210. =========================================
  211. [..]
  212. (+) Entry:
  213. The Sleep mode / Low-power Sleep mode is entered thru HAL_PWR_EnterSLEEPMode() API
  214. in specifying whether or not the regulator is forced to low-power mode and if exit is interrupt or event-triggered.
  215. (++) PWR_MAINREGULATOR_ON: Sleep mode (regulator in main mode).
  216. (++) PWR_LOWPOWERREGULATOR_ON: Low-power sleep (regulator in low power mode).
  217. In the latter case, the system clock frequency must have been decreased below 2 MHz beforehand.
  218. (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  219. (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  220. (+) WFI Exit:
  221. (++) Any peripheral interrupt acknowledged by the nested vectored interrupt
  222. controller (NVIC) or any wake-up event.
  223. (+) WFE Exit:
  224. (++) Any wake-up event such as an EXTI line configured in event mode.
  225. [..] When exiting the Low-power sleep mode by issuing an interrupt or a wakeup event,
  226. the MCU is in Low-power Run mode.
  227. *** Stop 0, Stop 1 and Stop 2 modes ***
  228. ===============================
  229. [..]
  230. (+) Entry:
  231. The Stop 0, Stop 1 or Stop 2 modes are entered thru the following API's:
  232. (++) HAL_PWREx_EnterSTOP0Mode() for mode 0, HAL_PWREx_EnterSTOP1Mode() for mode 1, HAL_PWREx_EnterSTOP2Mode() for mode 2
  233. or for porting reasons HAL_PWR_EnterSTOPMode().
  234. (+) Regulator setting (applicable to HAL_PWR_EnterSTOPMode() only):
  235. (++) PWR_MAINREGULATOR_ON: Regulator in main mode (STOP0 mode)
  236. (++) PWR_LOWPOWERREGULATOR_ON: Regulator in low-power mode (STOP1 mode)
  237. (+) Exit (interrupt or event-triggered, specified when entering STOP mode):
  238. (++) PWR_STOPENTRY_WFI: enter Stop mode with WFI instruction
  239. (++) PWR_STOPENTRY_WFE: enter Stop mode with WFE instruction
  240. (+) WFI Exit:
  241. (++) Any EXTI Line (Internal or External) configured in Interrupt mode.
  242. (++) Some specific communication peripherals (USART, LPUART, I2C) interrupts
  243. when programmed in wakeup mode.
  244. (+) WFE Exit:
  245. (++) Any EXTI Line (Internal or External) configured in Event mode.
  246. [..]
  247. When exiting Stop 0 and Stop 1 modes, the MCU is either in Run mode or in Low-power Run mode
  248. depending on the LPR bit setting.
  249. When exiting Stop 2 mode, the MCU is in Run mode.
  250. *** Standby mode ***
  251. ====================
  252. [..] The Standby mode offers two options:
  253. (+) option a) all clocks off except LSI and LSE, RRS bit set (keeps voltage regulator in low power mode).
  254. SRAM and registers contents are lost except for the SRAM2 content, the RTC registers, RTC backup registers
  255. and Standby circuitry.
  256. (+) option b) all clocks off except LSI and LSE, RRS bit cleared (voltage regulator then disabled).
  257. SRAM and register contents are lost except for the RTC registers, RTC backup registers
  258. and Standby circuitry.
  259. (++) Entry:
  260. (+++) The Standby mode is entered thru HAL_PWR_EnterSTANDBYMode() API.
  261. SRAM1 and register contents are lost except for registers in the Backup domain and
  262. Standby circuitry. SRAM2 content can be preserved if the bit RRS is set in PWR_CR3 register.
  263. To enable this feature, the user can resort to HAL_PWREx_EnableBKRAMContentRetention() API
  264. to set RRS bit.
  265. (++) Exit:
  266. (+++) WKUP pin rising edge, RTC alarm or wakeup, tamper event, time-stamp event,
  267. external reset in NRST pin, IWDG reset.
  268. [..] After waking up from Standby mode, program execution restarts in the same way as after a Reset.
  269. *** Shutdown mode ***
  270. ======================
  271. [..]
  272. In Shutdown mode,
  273. voltage regulator is disabled, all clocks are off except LSE, RRS bit is cleared.
  274. SRAM and registers contents are lost except for backup domain registers.
  275. (+) Entry:
  276. The Shutdown mode is entered thru HAL_PWREx_EnterSHUTDOWNMode() API.
  277. (+) Exit:
  278. (++) WKUP pin rising edge, RTC alarm or wakeup, tamper event, time-stamp event,
  279. external reset in NRST pin.
  280. [..] After waking up from Shutdown mode, program execution restarts in the same way as after a Reset.
  281. *** Auto-wakeup (AWU) from low-power mode ***
  282. =============================================
  283. [..]
  284. The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC
  285. Wakeup event, a tamper event or a time-stamp event, without depending on
  286. an external interrupt (Auto-wakeup mode).
  287. (+) RTC auto-wakeup (AWU) from the Stop, Standby and Shutdown modes
  288. (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to
  289. configure the RTC to generate the RTC alarm using the HAL_RTC_SetAlarm_IT() function.
  290. (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it
  291. is necessary to configure the RTC to detect the tamper or time stamp event using the
  292. HAL_RTCEx_SetTimeStamp_IT() or HAL_RTCEx_SetTamper_IT() functions.
  293. (++) To wake up from the Stop mode with an RTC WakeUp event, it is necessary to
  294. configure the RTC to generate the RTC WakeUp event using the HAL_RTCEx_SetWakeUpTimer_IT() function.
  295. @endverbatim
  296. * @{
  297. */
  298. /**
  299. * @brief Configure the voltage threshold detected by the Power Voltage Detector (PVD).
  300. * @param sConfigPVD pointer to a PWR_PVDTypeDef structure that contains the PVD
  301. * configuration information.
  302. * @note Refer to the electrical characteristics of your device datasheet for
  303. * more details about the voltage thresholds corresponding to each
  304. * detection level.
  305. * @note If "sConfigPVD->Mode" is set to PVD_MODE_IT,
  306. * wake-up target is set by default to wake-up target CPU1.
  307. * To select wake-up target to CPU2, additional configuration must be
  308. * performed using macro "__HAL_PWR_PVD_EXTIC2_ENABLE_IT()"
  309. * (and optionally, to select CPU2 only (not both CPU1 and CPU2):
  310. * "__HAL_PWR_PVD_EXTI_DISABLE_IT()").
  311. * @retval None
  312. */
  313. HAL_StatusTypeDef HAL_PWR_ConfigPVD(PWR_PVDTypeDef *sConfigPVD)
  314. {
  315. /* Check the parameters */
  316. assert_param(IS_PWR_PVD_LEVEL(sConfigPVD->PVDLevel));
  317. assert_param(IS_PWR_PVD_MODE(sConfigPVD->Mode));
  318. /* Set PLS bits according to PVDLevel value */
  319. MODIFY_REG(PWR->CR2, PWR_CR2_PLS, sConfigPVD->PVDLevel);
  320. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  321. /* Note: On STM32WB serie, power PVD event is not available on AIEC lines */
  322. /* (only interruption is available through AIEC line 16). */
  323. __HAL_PWR_PVD_EXTI_DISABLE_IT(); /*CPU1*/
  324. __HAL_PWR_PVD_EXTIC2_DISABLE_IT(); /*CPU2*/
  325. __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();
  326. __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();
  327. /* Configure interrupt mode */
  328. if((sConfigPVD->Mode & PVD_MODE_IT) == PVD_MODE_IT)
  329. {
  330. /* Set CPU1 as wakeup target */
  331. __HAL_PWR_PVD_EXTI_ENABLE_IT();
  332. }
  333. /* Configure the edge */
  334. if((sConfigPVD->Mode & PVD_RISING_EDGE) == PVD_RISING_EDGE)
  335. {
  336. __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
  337. }
  338. if((sConfigPVD->Mode & PVD_FALLING_EDGE) == PVD_FALLING_EDGE)
  339. {
  340. __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();
  341. }
  342. return HAL_OK;
  343. }
  344. /**
  345. * @brief Enables the Power Voltage Detector(PVD).
  346. * @retval None
  347. */
  348. void HAL_PWR_EnablePVD(void)
  349. {
  350. /* Enable the power voltage detector */
  351. SET_BIT(PWR->CR2, PWR_CR2_PVDE);
  352. }
  353. /**
  354. * @brief Disables the Power Voltage Detector(PVD).
  355. * @retval None
  356. */
  357. void HAL_PWR_DisablePVD(void)
  358. {
  359. /* Disable the power voltage detector */
  360. CLEAR_BIT(PWR->CR2, PWR_CR2_PVDE);
  361. }
  362. /**
  363. * @brief Enable the WakeUp PINx functionality.
  364. * @param WakeUpPinPolarity Specifies which Wake-Up pin to enable.
  365. * This parameter can be one of the following legacy values which set the default polarity
  366. * i.e. detection on high level (rising edge):
  367. * @arg @ref PWR_WAKEUP_PIN1, PWR_WAKEUP_PIN2, PWR_WAKEUP_PIN3, PWR_WAKEUP_PIN4, PWR_WAKEUP_PIN5
  368. *
  369. * or one of the following value where the user can explicitly specify the enabled pin and
  370. * the chosen polarity:
  371. * @arg @ref PWR_WAKEUP_PIN1_HIGH or PWR_WAKEUP_PIN1_LOW
  372. * @arg @ref PWR_WAKEUP_PIN2_HIGH or PWR_WAKEUP_PIN2_LOW
  373. * @arg @ref PWR_WAKEUP_PIN3_HIGH or PWR_WAKEUP_PIN3_LOW
  374. * @arg @ref PWR_WAKEUP_PIN4_HIGH or PWR_WAKEUP_PIN4_LOW
  375. * @arg @ref PWR_WAKEUP_PIN5_HIGH or PWR_WAKEUP_PIN5_LOW
  376. * @note PWR_WAKEUP_PINx and PWR_WAKEUP_PINx_HIGH are equivalent.
  377. * @retval None
  378. */
  379. void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinPolarity)
  380. {
  381. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinPolarity));
  382. /* Specifies the Wake-Up pin polarity for the event detection
  383. (rising or falling edge) */
  384. MODIFY_REG(PWR->CR4, (PWR_CR3_EWUP & WakeUpPinPolarity), (WakeUpPinPolarity >> PWR_WUP_POLARITY_SHIFT));
  385. /* Enable wake-up pin */
  386. SET_BIT(PWR->CR3, (PWR_CR3_EWUP & WakeUpPinPolarity));
  387. }
  388. /**
  389. * @brief Disable the WakeUp PINx functionality.
  390. * @param WakeUpPinx Specifies the Power Wake-Up pin to disable.
  391. * This parameter can be one of the following values:
  392. * @arg PWR_WAKEUP_PIN1: An event on PA0 PIN wakes-up the system from Standby mode.
  393. * @arg PWR_WAKEUP_PIN2: An event on PC13 PIN wakes-up the system from Standby mode.
  394. * @arg PWR_WAKEUP_PIN3: An event on PC12 PIN wakes-up the system from Standby mode.
  395. * @arg PWR_WAKEUP_PIN4: An event on PA2 PIN wakes-up the system from Standby mode.
  396. * @arg PWR_WAKEUP_PIN5: An event on PC5 PIN wakes-up the system from Standby mode.
  397. * @retval None
  398. */
  399. void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx)
  400. {
  401. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
  402. CLEAR_BIT(PWR->CR3, (PWR_CR3_EWUP & WakeUpPinx));
  403. }
  404. /**
  405. * @brief Enter Sleep or Low-power Sleep mode.
  406. * @note In Sleep/Low-power Sleep mode, all I/O pins keep the same state as in Run mode.
  407. * @param Regulator Specifies the regulator state in Sleep/Low-power Sleep mode.
  408. * This parameter can be one of the following values:
  409. * @arg @ref PWR_MAINREGULATOR_ON Sleep mode (regulator in main mode)
  410. * @arg @ref PWR_LOWPOWERREGULATOR_ON Low-power Sleep mode (regulator in low-power mode)
  411. * @note Low-power Sleep mode is entered from Low-power Run mode. Therefore, if not yet
  412. * in Low-power Run mode before calling HAL_PWR_EnterSLEEPMode() with Regulator set
  413. * to PWR_LOWPOWERREGULATOR_ON, the user can optionally configure the
  414. * Flash in power-down mode in setting the SLEEP_PD bit in FLASH_ACR register.
  415. * Additionally, the clock frequency must be reduced below 2 MHz.
  416. * Setting SLEEP_PD in FLASH_ACR then appropriately reducing the clock frequency must
  417. * be done before calling HAL_PWR_EnterSLEEPMode() API.
  418. * @note When exiting Low-power Sleep mode, the MCU is in Low-power Run mode. To move in
  419. * Run mode, the user must resort to HAL_PWREx_DisableLowPowerRunMode() API.
  420. * @param SLEEPEntry Specifies if Sleep mode is entered with WFI or WFE instruction.
  421. * This parameter can be one of the following values:
  422. * @arg @ref PWR_SLEEPENTRY_WFI enter Sleep or Low-power Sleep mode with WFI instruction
  423. * @arg @ref PWR_SLEEPENTRY_WFE enter Sleep or Low-power Sleep mode with WFE instruction
  424. * @note When WFI entry is used, tick interrupt have to be disabled if not desired as
  425. * the interrupt wake up source.
  426. * @retval None
  427. */
  428. void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
  429. {
  430. /* Check the parameters */
  431. assert_param(IS_PWR_REGULATOR(Regulator));
  432. assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
  433. /* Set Regulator parameter */
  434. if (Regulator == PWR_MAINREGULATOR_ON)
  435. {
  436. /* If in low-power run mode at this point, exit it */
  437. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF))
  438. {
  439. if (HAL_PWREx_DisableLowPowerRunMode() != HAL_OK)
  440. {
  441. return ;
  442. }
  443. }
  444. /* Regulator now in main mode. */
  445. }
  446. else
  447. {
  448. /* If in run mode, first move to low-power run mode.
  449. The system clock frequency must be below 2 MHz at this point. */
  450. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF) == RESET)
  451. {
  452. HAL_PWREx_EnableLowPowerRunMode();
  453. }
  454. }
  455. /* Clear SLEEPDEEP bit of Cortex System Control Register */
  456. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  457. /* Select SLEEP mode entry -------------------------------------------------*/
  458. if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
  459. {
  460. /* Request Wait For Interrupt */
  461. __WFI();
  462. }
  463. else
  464. {
  465. /* Request Wait For Event */
  466. __SEV();
  467. __WFE();
  468. __WFE();
  469. }
  470. }
  471. /**
  472. * @brief Enter Stop mode
  473. * @note This API is named HAL_PWR_EnterSTOPMode to ensure compatibility with legacy code running
  474. * on devices where only "Stop mode" is mentioned with main or low power regulator ON.
  475. * @note In Stop mode, all I/O pins keep the same state as in Run mode.
  476. * @note All clocks in the VCORE domain are stopped; the PLL, the MSI,
  477. * the HSI and the HSE oscillators are disabled. Some peripherals with the wakeup capability
  478. * (I2Cx, USARTx and LPUART) can switch on the HSI to receive a frame, and switch off the HSI
  479. * after receiving the frame if it is not a wakeup frame. In this case, the HSI clock is propagated
  480. * only to the peripheral requesting it.
  481. * SRAM1, SRAM2 and register contents are preserved.
  482. * The BOR is available.
  483. * The voltage regulator can be configured either in normal (Stop 0) or low-power mode (Stop 1).
  484. * @note When exiting Stop 0 or Stop 1 mode by issuing an interrupt or a wakeup event,
  485. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  486. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  487. * @note When the voltage regulator operates in low power mode (Stop 1), an additional
  488. * startup delay is incurred when waking up.
  489. * By keeping the internal regulator ON during Stop mode (Stop 0), the consumption
  490. * is higher although the startup time is reduced.
  491. * @note Case of Stop0 mode with SMPS: Before entering Stop 0 mode with SMPS Step Down converter enabled,
  492. * the HSI16 must be kept on by enabling HSI kernel clock (set HSIKERON register bit).
  493. * @note According to system power policy, system entering in Stop mode
  494. * is depending on other CPU power mode.
  495. * @param Regulator Specifies the regulator state in Stop mode.
  496. * This parameter can be one of the following values:
  497. * @arg @ref PWR_MAINREGULATOR_ON Stop 0 mode (main regulator ON)
  498. * @arg @ref PWR_LOWPOWERREGULATOR_ON Stop 1 mode (low power regulator ON)
  499. * @param STOPEntry Specifies Stop 0, Stop 1 or Stop 2 mode is entered with WFI or WFE instruction.
  500. * This parameter can be one of the following values:
  501. * @arg @ref PWR_STOPENTRY_WFI Enter Stop 0 or Stop 1 mode with WFI instruction.
  502. * @arg @ref PWR_STOPENTRY_WFE Enter Stop 0 or Stop 1 mode with WFE instruction.
  503. * @retval None
  504. */
  505. void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
  506. {
  507. /* Check the parameters */
  508. assert_param(IS_PWR_REGULATOR(Regulator));
  509. if(Regulator == PWR_LOWPOWERREGULATOR_ON)
  510. {
  511. HAL_PWREx_EnterSTOP1Mode(STOPEntry);
  512. }
  513. else
  514. {
  515. HAL_PWREx_EnterSTOP0Mode(STOPEntry);
  516. }
  517. }
  518. /**
  519. * @brief Enter Standby mode.
  520. * @note In Standby mode, the PLL, the HSI, the MSI and the HSE oscillators are switched
  521. * off. The voltage regulator is disabled, except when BKRAM content is preserved
  522. * in which case the regulator is in low-power mode.
  523. * SRAM and register contents are lost except for registers in the Backup domain and
  524. * Standby circuitry. BKRAM content can be preserved if the bit RRS is set in PWR_CR3 register.
  525. * To enable this feature, the user can resort to HAL_PWREx_EnableBKRAMContentRetention() API
  526. * to set RRS bit.
  527. * The BOR is available.
  528. * @note The I/Os can be configured either with a pull-up or pull-down or can be kept in analog state.
  529. * HAL_PWREx_EnableGPIOPullUp() and HAL_PWREx_EnableGPIOPullDown() respectively enable Pull Up and
  530. * Pull Down state, HAL_PWREx_DisableGPIOPullUp() and HAL_PWREx_DisableGPIOPullDown() disable the
  531. * same.
  532. * These states are effective in Standby mode only if APC bit is set through
  533. * HAL_PWREx_EnablePullUpPullDownConfig() API.
  534. * @note According to system power policy, system entering in Standby mode
  535. * is depending on other CPU power mode.
  536. * @retval None
  537. */
  538. void HAL_PWR_EnterSTANDBYMode(void)
  539. {
  540. /* Set Stand-by mode */
  541. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_LOWPOWERMODE_STANDBY);
  542. /* Set SLEEPDEEP bit of Cortex System Control Register */
  543. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  544. /* This option is used to ensure that store operations are completed */
  545. #if defined ( __CC_ARM)
  546. __force_stores();
  547. #endif
  548. /* Request Wait For Interrupt */
  549. __WFI();
  550. /* Following code is executed after wake up if system did not go to STANDBY
  551. mode according to system power policy */
  552. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  553. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  554. }
  555. /**
  556. * @brief Indicate Sleep-On-Exit when returning from Handler mode to Thread mode.
  557. * @note Set SLEEPONEXIT bit of SCR register. When this bit is set, the processor
  558. * re-enters SLEEP mode when an interruption handling is over.
  559. * Setting this bit is useful when the processor is expected to run only on
  560. * interruptions handling.
  561. * @retval None
  562. */
  563. void HAL_PWR_EnableSleepOnExit(void)
  564. {
  565. /* Set SLEEPONEXIT bit of Cortex System Control Register */
  566. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  567. }
  568. /**
  569. * @brief Disable Sleep-On-Exit feature when returning from Handler mode to Thread mode.
  570. * @note Clear SLEEPONEXIT bit of SCR register. When this bit is set, the processor
  571. * re-enters SLEEP mode when an interruption handling is over.
  572. * @retval None
  573. */
  574. void HAL_PWR_DisableSleepOnExit(void)
  575. {
  576. /* Clear SLEEPONEXIT bit of Cortex System Control Register */
  577. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  578. }
  579. /**
  580. * @brief Enable CORTEX M4 SEVONPEND bit.
  581. * @note Set SEVONPEND bit of SCR register. When this bit is set, this causes
  582. * WFE to wake up when an interrupt moves from inactive to pended.
  583. * @retval None
  584. */
  585. void HAL_PWR_EnableSEVOnPend(void)
  586. {
  587. /* Set SEVONPEND bit of Cortex System Control Register */
  588. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  589. }
  590. /**
  591. * @brief Disable CORTEX M4 SEVONPEND bit.
  592. * @note Clear SEVONPEND bit of SCR register. When this bit is set, this causes
  593. * WFE to wake up when an interrupt moves from inactive to pended.
  594. * @retval None
  595. */
  596. void HAL_PWR_DisableSEVOnPend(void)
  597. {
  598. /* Clear SEVONPEND bit of Cortex System Control Register */
  599. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  600. }
  601. /**
  602. * @brief PWR PVD interrupt callback
  603. * @retval None
  604. */
  605. __weak void HAL_PWR_PVDCallback(void)
  606. {
  607. /* NOTE : This function should not be modified; when the callback is needed,
  608. the HAL_PWR_PVDCallback can be implemented in the user file
  609. */
  610. }
  611. /**
  612. * @}
  613. */
  614. /**
  615. * @}
  616. */
  617. #endif /* HAL_PWR_MODULE_ENABLED */
  618. /**
  619. * @}
  620. */
  621. /**
  622. * @}
  623. */
  624. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/