stm32l4xx_hal_pwr.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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) 2017 STMicroelectronics</center></h2>
  15. *
  16. * Redistribution and use in source and binary forms, with or without modification,
  17. * are permitted provided that the following conditions are met:
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  35. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ******************************************************************************
  39. */
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "stm32l4xx_hal.h"
  42. /** @addtogroup STM32L4xx_HAL_Driver
  43. * @{
  44. */
  45. /** @defgroup PWR PWR
  46. * @brief PWR HAL module driver
  47. * @{
  48. */
  49. #ifdef HAL_PWR_MODULE_ENABLED
  50. /* Private typedef -----------------------------------------------------------*/
  51. /* Private define ------------------------------------------------------------*/
  52. /** @defgroup PWR_Private_Defines PWR Private Defines
  53. * @{
  54. */
  55. /** @defgroup PWR_PVD_Mode_Mask PWR PVD Mode Mask
  56. * @{
  57. */
  58. #define PVD_MODE_IT ((uint32_t)0x00010000) /*!< Mask for interruption yielded by PVD threshold crossing */
  59. #define PVD_MODE_EVT ((uint32_t)0x00020000) /*!< Mask for event yielded by PVD threshold crossing */
  60. #define PVD_RISING_EDGE ((uint32_t)0x00000001) /*!< Mask for rising edge set as PVD trigger */
  61. #define PVD_FALLING_EDGE ((uint32_t)0x00000002) /*!< Mask for falling edge set as PVD trigger */
  62. /**
  63. * @}
  64. */
  65. /**
  66. * @}
  67. */
  68. /* Private macro -------------------------------------------------------------*/
  69. /* Private variables ---------------------------------------------------------*/
  70. /* Private function prototypes -----------------------------------------------*/
  71. /* Exported functions --------------------------------------------------------*/
  72. /** @defgroup PWR_Exported_Functions PWR Exported Functions
  73. * @{
  74. */
  75. /** @defgroup 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. __HAL_RCC_PWR_FORCE_RESET();
  93. __HAL_RCC_PWR_RELEASE_RESET();
  94. }
  95. /**
  96. * @brief Enable access to the backup domain
  97. * (RTC registers, RTC backup data registers).
  98. * @note After reset, the backup domain is protected against
  99. * possible unwanted write accesses.
  100. * @note RTCSEL that sets the RTC clock source selection is in the RTC back-up domain.
  101. * In order to set or modify the RTC clock, the backup domain access must be
  102. * disabled.
  103. * @note LSEON bit that switches on and off the LSE crystal belongs as well to the
  104. * back-up domain.
  105. * @retval None
  106. */
  107. void HAL_PWR_EnableBkUpAccess(void)
  108. {
  109. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  110. }
  111. /**
  112. * @brief Disable access to the backup domain
  113. * (RTC registers, RTC backup data registers).
  114. * @retval None
  115. */
  116. void HAL_PWR_DisableBkUpAccess(void)
  117. {
  118. CLEAR_BIT(PWR->CR1, PWR_CR1_DBP);
  119. }
  120. /**
  121. * @}
  122. */
  123. /** @defgroup PWR_Exported_Functions_Group2 Peripheral Control functions
  124. * @brief Low Power modes configuration functions
  125. *
  126. @verbatim
  127. ===============================================================================
  128. ##### Peripheral Control functions #####
  129. ===============================================================================
  130. [..]
  131. *** PVD configuration ***
  132. =========================
  133. [..]
  134. (+) The PVD is used to monitor the VDD power supply by comparing it to a
  135. threshold selected by the PVD Level (PLS[2:0] bits in PWR_CR2 register).
  136. (+) PVDO flag is available to indicate if VDD/VDDA is higher or lower
  137. than the PVD threshold. This event is internally connected to the EXTI
  138. line16 and can generate an interrupt if enabled. This is done through
  139. __HAL_PVD_EXTI_ENABLE_IT() macro.
  140. (+) The PVD is stopped in Standby mode.
  141. *** WakeUp pin configuration ***
  142. ================================
  143. [..]
  144. (+) WakeUp pins are used to wakeup the system from Standby mode or Shutdown mode.
  145. The polarity of these pins can be set to configure event detection on high
  146. level (rising edge) or low level (falling edge).
  147. *** Low Power modes configuration ***
  148. =====================================
  149. [..]
  150. The devices feature 8 low-power modes:
  151. (+) Low-power Run mode: core and peripherals are running, main regulator off, low power regulator on.
  152. (+) Sleep mode: Cortex-M4 core stopped, peripherals kept running, main and low power regulators on.
  153. (+) Low-power Sleep mode: Cortex-M4 core stopped, peripherals kept running, main regulator off, low power regulator on.
  154. (+) Stop 0 mode: all clocks are stopped except LSI and LSE, main and low power regulators on.
  155. (+) Stop 1 mode: all clocks are stopped except LSI and LSE, main regulator off, low power regulator on.
  156. (+) 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.
  157. (+) Standby mode with SRAM2: all clocks are stopped except LSI and LSE, SRAM2 content preserved, main regulator off, low power regulator on.
  158. (+) Standby mode without SRAM2: all clocks are stopped except LSI and LSE, main and low power regulators off.
  159. (+) Shutdown mode: all clocks are stopped except LSE, main and low power regulators off.
  160. *** Low-power run mode ***
  161. ==========================
  162. [..]
  163. (+) Entry: (from main run mode)
  164. (++) set LPR bit with HAL_PWREx_EnableLowPowerRunMode() API after having decreased the system clock below 2 MHz.
  165. (+) Exit:
  166. (++) clear LPR bit then wait for REGLP bit to be reset with HAL_PWREx_DisableLowPowerRunMode() API. Only
  167. then can the system clock frequency be increased above 2 MHz.
  168. *** Sleep mode / Low-power sleep mode ***
  169. =========================================
  170. [..]
  171. (+) Entry:
  172. The Sleep mode / Low-power Sleep mode is entered thru HAL_PWR_EnterSLEEPMode() API
  173. in specifying whether or not the regulator is forced to low-power mode and if exit is interrupt or event-triggered.
  174. (++) PWR_MAINREGULATOR_ON: Sleep mode (regulator in main mode).
  175. (++) PWR_LOWPOWERREGULATOR_ON: Low-power sleep (regulator in low power mode).
  176. In the latter case, the system clock frequency must have been decreased below 2 MHz beforehand.
  177. (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  178. (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  179. (+) WFI Exit:
  180. (++) Any peripheral interrupt acknowledged by the nested vectored interrupt
  181. controller (NVIC) or any wake-up event.
  182. (+) WFE Exit:
  183. (++) Any wake-up event such as an EXTI line configured in event mode.
  184. [..] When exiting the Low-power sleep mode by issuing an interrupt or a wakeup event,
  185. the MCU is in Low-power Run mode.
  186. *** Stop 0, Stop 1 and Stop 2 modes ***
  187. ===============================
  188. [..]
  189. (+) Entry:
  190. The Stop 0, Stop 1 or Stop 2 modes are entered thru the following API's:
  191. (++) HAL_PWREx_EnterSTOP0Mode() for mode 0 or HAL_PWREx_EnterSTOP1Mode() for mode 1 or for porting reasons HAL_PWR_EnterSTOPMode().
  192. (++) HAL_PWREx_EnterSTOP2Mode() for mode 2.
  193. (+) Regulator setting (applicable to HAL_PWR_EnterSTOPMode() only):
  194. (++) PWR_MAINREGULATOR_ON
  195. (++) PWR_LOWPOWERREGULATOR_ON
  196. (+) Exit (interrupt or event-triggered, specified when entering STOP mode):
  197. (++) PWR_STOPENTRY_WFI: enter Stop mode with WFI instruction
  198. (++) PWR_STOPENTRY_WFE: enter Stop mode with WFE instruction
  199. (+) WFI Exit:
  200. (++) Any EXTI Line (Internal or External) configured in Interrupt mode.
  201. (++) Some specific communication peripherals (USART, LPUART, I2C) interrupts
  202. when programmed in wakeup mode.
  203. (+) WFE Exit:
  204. (++) Any EXTI Line (Internal or External) configured in Event mode.
  205. [..]
  206. When exiting Stop 0 and Stop 1 modes, the MCU is either in Run mode or in Low-power Run mode
  207. depending on the LPR bit setting.
  208. When exiting Stop 2 mode, the MCU is in Run mode.
  209. *** Standby mode ***
  210. ====================
  211. [..]
  212. The Standby mode offers two options:
  213. (+) option a) all clocks off except LSI and LSE, RRS bit set (keeps voltage regulator in low power mode).
  214. SRAM and registers contents are lost except for the SRAM2 content, the RTC registers, RTC backup registers
  215. and Standby circuitry.
  216. (+) option b) all clocks off except LSI and LSE, RRS bit cleared (voltage regulator then disabled).
  217. SRAM and register contents are lost except for the RTC registers, RTC backup registers
  218. and Standby circuitry.
  219. (++) Entry:
  220. (+++) The Standby mode is entered thru HAL_PWR_EnterSTANDBYMode() API.
  221. SRAM1 and register contents are lost except for registers in the Backup domain and
  222. Standby circuitry. SRAM2 content can be preserved if the bit RRS is set in PWR_CR3 register.
  223. To enable this feature, the user can resort to HAL_PWREx_EnableSRAM2ContentRetention() API
  224. to set RRS bit.
  225. (++) Exit:
  226. (+++) WKUP pin rising edge, RTC alarm or wakeup, tamper event, time-stamp event,
  227. external reset in NRST pin, IWDG reset.
  228. [..] After waking up from Standby mode, program execution restarts in the same way as after a Reset.
  229. *** Shutdown mode ***
  230. ======================
  231. [..]
  232. In Shutdown mode,
  233. voltage regulator is disabled, all clocks are off except LSE, RRS bit is cleared.
  234. SRAM and registers contents are lost except for backup domain registers.
  235. (+) Entry:
  236. The Shutdown mode is entered thru HAL_PWREx_EnterSHUTDOWNMode() API.
  237. (+) Exit:
  238. (++) WKUP pin rising edge, RTC alarm or wakeup, tamper event, time-stamp event,
  239. external reset in NRST pin.
  240. [..] After waking up from Shutdown mode, program execution restarts in the same way as after a Reset.
  241. *** Auto-wakeup (AWU) from low-power mode ***
  242. =============================================
  243. [..]
  244. The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC
  245. Wakeup event, a tamper event or a time-stamp event, without depending on
  246. an external interrupt (Auto-wakeup mode).
  247. (+) RTC auto-wakeup (AWU) from the Stop, Standby and Shutdown modes
  248. (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to
  249. configure the RTC to generate the RTC alarm using the HAL_RTC_SetAlarm_IT() function.
  250. (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it
  251. is necessary to configure the RTC to detect the tamper or time stamp event using the
  252. HAL_RTCEx_SetTimeStamp_IT() or HAL_RTCEx_SetTamper_IT() functions.
  253. (++) To wake up from the Stop mode with an RTC WakeUp event, it is necessary to
  254. configure the RTC to generate the RTC WakeUp event using the HAL_RTCEx_SetWakeUpTimer_IT() function.
  255. @endverbatim
  256. * @{
  257. */
  258. /**
  259. * @brief Configure the voltage threshold detected by the Power Voltage Detector (PVD).
  260. * @param sConfigPVD: pointer to a PWR_PVDTypeDef structure that contains the PVD
  261. * configuration information.
  262. * @note Refer to the electrical characteristics of your device datasheet for
  263. * more details about the voltage thresholds corresponding to each
  264. * detection level.
  265. * @retval None
  266. */
  267. HAL_StatusTypeDef HAL_PWR_ConfigPVD(PWR_PVDTypeDef *sConfigPVD)
  268. {
  269. /* Check the parameters */
  270. assert_param(IS_PWR_PVD_LEVEL(sConfigPVD->PVDLevel));
  271. assert_param(IS_PWR_PVD_MODE(sConfigPVD->Mode));
  272. /* Set PLS bits according to PVDLevel value */
  273. MODIFY_REG(PWR->CR2, PWR_CR2_PLS, sConfigPVD->PVDLevel);
  274. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  275. __HAL_PWR_PVD_EXTI_DISABLE_EVENT();
  276. __HAL_PWR_PVD_EXTI_DISABLE_IT();
  277. __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();
  278. __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();
  279. /* Configure interrupt mode */
  280. if((sConfigPVD->Mode & PVD_MODE_IT) == PVD_MODE_IT)
  281. {
  282. __HAL_PWR_PVD_EXTI_ENABLE_IT();
  283. }
  284. /* Configure event mode */
  285. if((sConfigPVD->Mode & PVD_MODE_EVT) == PVD_MODE_EVT)
  286. {
  287. __HAL_PWR_PVD_EXTI_ENABLE_EVENT();
  288. }
  289. /* Configure the edge */
  290. if((sConfigPVD->Mode & PVD_RISING_EDGE) == PVD_RISING_EDGE)
  291. {
  292. __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
  293. }
  294. if((sConfigPVD->Mode & PVD_FALLING_EDGE) == PVD_FALLING_EDGE)
  295. {
  296. __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();
  297. }
  298. return HAL_OK;
  299. }
  300. /**
  301. * @brief Enable the Power Voltage Detector (PVD).
  302. * @retval None
  303. */
  304. void HAL_PWR_EnablePVD(void)
  305. {
  306. SET_BIT(PWR->CR2, PWR_CR2_PVDE);
  307. }
  308. /**
  309. * @brief Disable the Power Voltage Detector (PVD).
  310. * @retval None
  311. */
  312. void HAL_PWR_DisablePVD(void)
  313. {
  314. CLEAR_BIT(PWR->CR2, PWR_CR2_PVDE);
  315. }
  316. /**
  317. * @brief Enable the WakeUp PINx functionality.
  318. * @param WakeUpPinPolarity: Specifies which Wake-Up pin to enable.
  319. * This parameter can be one of the following legacy values which set the default polarity
  320. * i.e. detection on high level (rising edge):
  321. * @arg @ref PWR_WAKEUP_PIN1, PWR_WAKEUP_PIN2, PWR_WAKEUP_PIN3, PWR_WAKEUP_PIN4, PWR_WAKEUP_PIN5
  322. *
  323. * or one of the following value where the user can explicitly specify the enabled pin and
  324. * the chosen polarity:
  325. * @arg @ref PWR_WAKEUP_PIN1_HIGH or PWR_WAKEUP_PIN1_LOW
  326. * @arg @ref PWR_WAKEUP_PIN2_HIGH or PWR_WAKEUP_PIN2_LOW
  327. * @arg @ref PWR_WAKEUP_PIN3_HIGH or PWR_WAKEUP_PIN3_LOW
  328. * @arg @ref PWR_WAKEUP_PIN4_HIGH or PWR_WAKEUP_PIN4_LOW
  329. * @arg @ref PWR_WAKEUP_PIN5_HIGH or PWR_WAKEUP_PIN5_LOW
  330. * @note PWR_WAKEUP_PINx and PWR_WAKEUP_PINx_HIGH are equivalent.
  331. * @retval None
  332. */
  333. void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinPolarity)
  334. {
  335. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinPolarity));
  336. /* Specifies the Wake-Up pin polarity for the event detection
  337. (rising or falling edge) */
  338. MODIFY_REG(PWR->CR4, (PWR_CR3_EWUP & WakeUpPinPolarity), (WakeUpPinPolarity >> PWR_WUP_POLARITY_SHIFT));
  339. /* Enable wake-up pin */
  340. SET_BIT(PWR->CR3, (PWR_CR3_EWUP & WakeUpPinPolarity));
  341. }
  342. /**
  343. * @brief Disable the WakeUp PINx functionality.
  344. * @param WakeUpPinx: Specifies the Power Wake-Up pin to disable.
  345. * This parameter can be one of the following values:
  346. * @arg @ref PWR_WAKEUP_PIN1, PWR_WAKEUP_PIN2, PWR_WAKEUP_PIN3, PWR_WAKEUP_PIN4, PWR_WAKEUP_PIN5
  347. * @retval None
  348. */
  349. void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx)
  350. {
  351. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
  352. CLEAR_BIT(PWR->CR3, (PWR_CR3_EWUP & WakeUpPinx));
  353. }
  354. /**
  355. * @brief Enter Sleep or Low-power Sleep mode.
  356. * @note In Sleep/Low-power Sleep mode, all I/O pins keep the same state as in Run mode.
  357. * @param Regulator: Specifies the regulator state in Sleep/Low-power Sleep mode.
  358. * This parameter can be one of the following values:
  359. * @arg @ref PWR_MAINREGULATOR_ON Sleep mode (regulator in main mode)
  360. * @arg @ref PWR_LOWPOWERREGULATOR_ON Low-power Sleep mode (regulator in low-power mode)
  361. * @note Low-power Sleep mode is entered from Low-power Run mode. Therefore, if not yet
  362. * in Low-power Run mode before calling HAL_PWR_EnterSLEEPMode() with Regulator set
  363. * to PWR_LOWPOWERREGULATOR_ON, the user can optionally configure the
  364. * Flash in power-down monde in setting the SLEEP_PD bit in FLASH_ACR register.
  365. * Additionally, the clock frequency must be reduced below 2 MHz.
  366. * Setting SLEEP_PD in FLASH_ACR then appropriately reducing the clock frequency must
  367. * be done before calling HAL_PWR_EnterSLEEPMode() API.
  368. * @note When exiting Low-power Sleep mode, the MCU is in Low-power Run mode. To move in
  369. * Run mode, the user must resort to HAL_PWREx_DisableLowPowerRunMode() API.
  370. * @param SLEEPEntry: Specifies if Sleep mode is entered with WFI or WFE instruction.
  371. * This parameter can be one of the following values:
  372. * @arg @ref PWR_SLEEPENTRY_WFI enter Sleep or Low-power Sleep mode with WFI instruction
  373. * @arg @ref PWR_SLEEPENTRY_WFE enter Sleep or Low-power Sleep mode with WFE instruction
  374. * @note When WFI entry is used, tick interrupt have to be disabled if not desired as
  375. * the interrupt wake up source.
  376. * @retval None
  377. */
  378. void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
  379. {
  380. /* Check the parameters */
  381. assert_param(IS_PWR_REGULATOR(Regulator));
  382. assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
  383. /* Set Regulator parameter */
  384. if (Regulator == PWR_MAINREGULATOR_ON)
  385. {
  386. /* If in low-power run mode at this point, exit it */
  387. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF))
  388. {
  389. HAL_PWREx_DisableLowPowerRunMode();
  390. }
  391. /* Regulator now in main mode. */
  392. }
  393. else
  394. {
  395. /* If in run mode, first move to low-power run mode.
  396. The system clock frequency must be below 2 MHz at this point. */
  397. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF) == RESET)
  398. {
  399. HAL_PWREx_EnableLowPowerRunMode();
  400. }
  401. }
  402. /* Clear SLEEPDEEP bit of Cortex System Control Register */
  403. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  404. /* Select SLEEP mode entry -------------------------------------------------*/
  405. if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
  406. {
  407. /* Request Wait For Interrupt */
  408. __WFI();
  409. }
  410. else
  411. {
  412. /* Request Wait For Event */
  413. __SEV();
  414. __WFE();
  415. __WFE();
  416. }
  417. }
  418. /**
  419. * @brief Enter Stop mode
  420. * @note This API is named HAL_PWR_EnterSTOPMode to ensure compatibility with legacy code running
  421. * on devices where only "Stop mode" is mentioned with main or low power regulator ON.
  422. * @note In Stop mode, all I/O pins keep the same state as in Run mode.
  423. * @note All clocks in the VCORE domain are stopped; the PLL, the MSI,
  424. * the HSI and the HSE oscillators are disabled. Some peripherals with the wakeup capability
  425. * (I2Cx, USARTx and LPUART) can switch on the HSI to receive a frame, and switch off the HSI
  426. * after receiving the frame if it is not a wakeup frame. In this case, the HSI clock is propagated
  427. * only to the peripheral requesting it.
  428. * SRAM1, SRAM2 and register contents are preserved.
  429. * The BOR is available.
  430. * The voltage regulator can be configured either in normal (Stop 0) or low-power mode (Stop 1).
  431. * @note When exiting Stop 0 or Stop 1 mode by issuing an interrupt or a wakeup event,
  432. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  433. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  434. * @note When the voltage regulator operates in low power mode (Stop 1), an additional
  435. * startup delay is incurred when waking up.
  436. * By keeping the internal regulator ON during Stop mode (Stop 0), the consumption
  437. * is higher although the startup time is reduced.
  438. * @param Regulator: Specifies the regulator state in Stop mode.
  439. * This parameter can be one of the following values:
  440. * @arg @ref PWR_MAINREGULATOR_ON Stop 0 mode (main regulator ON)
  441. * @arg @ref PWR_LOWPOWERREGULATOR_ON Stop 1 mode (low power regulator ON)
  442. * @param STOPEntry: Specifies Stop 0 or Stop 1 mode is entered with WFI or WFE instruction.
  443. * This parameter can be one of the following values:
  444. * @arg @ref PWR_STOPENTRY_WFI Enter Stop 0 or Stop 1 mode with WFI instruction.
  445. * @arg @ref PWR_STOPENTRY_WFE Enter Stop 0 or Stop 1 mode with WFE instruction.
  446. * @retval None
  447. */
  448. void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
  449. {
  450. /* Check the parameters */
  451. assert_param(IS_PWR_REGULATOR(Regulator));
  452. if(Regulator == PWR_LOWPOWERREGULATOR_ON)
  453. {
  454. HAL_PWREx_EnterSTOP1Mode(STOPEntry);
  455. }
  456. else
  457. {
  458. HAL_PWREx_EnterSTOP0Mode(STOPEntry);
  459. }
  460. }
  461. /**
  462. * @brief Enter Standby mode.
  463. * @note In Standby mode, the PLL, the HSI, the MSI and the HSE oscillators are switched
  464. * off. The voltage regulator is disabled, except when SRAM2 content is preserved
  465. * in which case the regulator is in low-power mode.
  466. * SRAM1 and register contents are lost except for registers in the Backup domain and
  467. * Standby circuitry. SRAM2 content can be preserved if the bit RRS is set in PWR_CR3 register.
  468. * To enable this feature, the user can resort to HAL_PWREx_EnableSRAM2ContentRetention() API
  469. * to set RRS bit.
  470. * The BOR is available.
  471. * @note The I/Os can be configured either with a pull-up or pull-down or can be kept in analog state.
  472. * HAL_PWREx_EnableGPIOPullUp() and HAL_PWREx_EnableGPIOPullDown() respectively enable Pull Up and
  473. * Pull Down state, HAL_PWREx_DisableGPIOPullUp() and HAL_PWREx_DisableGPIOPullDown() disable the
  474. * same.
  475. * These states are effective in Standby mode only if APC bit is set through
  476. * HAL_PWREx_EnablePullUpPullDownConfig() API.
  477. * @retval None
  478. */
  479. void HAL_PWR_EnterSTANDBYMode(void)
  480. {
  481. /* Set Stand-by mode */
  482. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_STANDBY);
  483. /* Set SLEEPDEEP bit of Cortex System Control Register */
  484. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  485. /* This option is used to ensure that store operations are completed */
  486. #if defined ( __CC_ARM)
  487. __force_stores();
  488. #endif
  489. /* Request Wait For Interrupt */
  490. __WFI();
  491. }
  492. /**
  493. * @brief Indicate Sleep-On-Exit when returning from Handler mode to Thread mode.
  494. * @note Set SLEEPONEXIT bit of SCR register. When this bit is set, the processor
  495. * re-enters SLEEP mode when an interruption handling is over.
  496. * Setting this bit is useful when the processor is expected to run only on
  497. * interruptions handling.
  498. * @retval None
  499. */
  500. void HAL_PWR_EnableSleepOnExit(void)
  501. {
  502. /* Set SLEEPONEXIT bit of Cortex System Control Register */
  503. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  504. }
  505. /**
  506. * @brief Disable Sleep-On-Exit feature when returning from Handler mode to Thread mode.
  507. * @note Clear SLEEPONEXIT bit of SCR register. When this bit is set, the processor
  508. * re-enters SLEEP mode when an interruption handling is over.
  509. * @retval None
  510. */
  511. void HAL_PWR_DisableSleepOnExit(void)
  512. {
  513. /* Clear SLEEPONEXIT bit of Cortex System Control Register */
  514. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  515. }
  516. /**
  517. * @brief Enable CORTEX M4 SEVONPEND bit.
  518. * @note Set SEVONPEND bit of SCR register. When this bit is set, this causes
  519. * WFE to wake up when an interrupt moves from inactive to pended.
  520. * @retval None
  521. */
  522. void HAL_PWR_EnableSEVOnPend(void)
  523. {
  524. /* Set SEVONPEND bit of Cortex System Control Register */
  525. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  526. }
  527. /**
  528. * @brief Disable CORTEX M4 SEVONPEND bit.
  529. * @note Clear SEVONPEND bit of SCR register. When this bit is set, this causes
  530. * WFE to wake up when an interrupt moves from inactive to pended.
  531. * @retval None
  532. */
  533. void HAL_PWR_DisableSEVOnPend(void)
  534. {
  535. /* Clear SEVONPEND bit of Cortex System Control Register */
  536. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  537. }
  538. /**
  539. * @brief PWR PVD interrupt callback
  540. * @retval None
  541. */
  542. __weak void HAL_PWR_PVDCallback(void)
  543. {
  544. /* NOTE : This function should not be modified; when the callback is needed,
  545. the HAL_PWR_PVDCallback can be implemented in the user file
  546. */
  547. }
  548. /**
  549. * @}
  550. */
  551. /**
  552. * @}
  553. */
  554. #endif /* HAL_PWR_MODULE_ENABLED */
  555. /**
  556. * @}
  557. */
  558. /**
  559. * @}
  560. */
  561. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/