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.

stm32l1xx_hal_pwr.c 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_pwr.c
  4. * @author MCD Application Team
  5. * @brief PWR HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Power Controller (PWR) peripheral:
  9. * + Initialization/de-initialization functions
  10. * + Peripheral Control functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  16. * All rights reserved.</center></h2>
  17. *
  18. * This software component is licensed by ST under BSD 3-Clause license,
  19. * the "License"; You may not use this file except in compliance with the
  20. * License. You may obtain a copy of the License at:
  21. * opensource.org/licenses/BSD-3-Clause
  22. *
  23. ******************************************************************************
  24. */
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32l1xx_hal.h"
  27. /** @addtogroup STM32L1xx_HAL_Driver
  28. * @{
  29. */
  30. /** @defgroup PWR PWR
  31. * @brief PWR HAL module driver
  32. * @{
  33. */
  34. #ifdef HAL_PWR_MODULE_ENABLED
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. #define PVD_MODE_IT (0x00010000U)
  38. #define PVD_MODE_EVT (0x00020000U)
  39. #define PVD_RISING_EDGE (0x00000001U)
  40. #define PVD_FALLING_EDGE (0x00000002U)
  41. /* Private macro -------------------------------------------------------------*/
  42. /* Private variables ---------------------------------------------------------*/
  43. /* Private function prototypes -----------------------------------------------*/
  44. /* Private functions ---------------------------------------------------------*/
  45. /** @defgroup PWR_Exported_Functions PWR Exported Functions
  46. * @{
  47. */
  48. /** @defgroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
  49. * @brief Initialization and de-initialization functions
  50. *
  51. @verbatim
  52. ===============================================================================
  53. ##### Initialization and de-initialization functions #####
  54. ===============================================================================
  55. [..]
  56. After reset, the backup domain (RTC registers, RTC backup data
  57. registers) is protected against possible unwanted
  58. write accesses.
  59. To enable access to the RTC Domain and RTC registers, proceed as follows:
  60. (+) Enable the Power Controller (PWR) APB1 interface clock using the
  61. __HAL_RCC_PWR_CLK_ENABLE() macro.
  62. (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
  63. @endverbatim
  64. * @{
  65. */
  66. /**
  67. * @brief Deinitializes the PWR peripheral registers to their default reset values.
  68. * @note Before calling this function, the VOS[1:0] bits should be configured
  69. * to "10" and the system frequency has to be configured accordingly.
  70. * To configure the VOS[1:0] bits, use the PWR_VoltageScalingConfig()
  71. * function.
  72. * @note ULP and FWU bits are not reset by this function.
  73. * @retval None
  74. */
  75. void HAL_PWR_DeInit(void)
  76. {
  77. __HAL_RCC_PWR_FORCE_RESET();
  78. __HAL_RCC_PWR_RELEASE_RESET();
  79. }
  80. /**
  81. * @brief Enables access to the backup domain (RTC registers, RTC
  82. * backup data registers ).
  83. * @note If the HSE divided by 2, 4, 8 or 16 is used as the RTC clock, the
  84. * Backup Domain Access should be kept enabled.
  85. * @retval None
  86. */
  87. void HAL_PWR_EnableBkUpAccess(void)
  88. {
  89. /* Enable access to RTC and backup registers */
  90. *(__IO uint32_t *) CR_DBP_BB = (uint32_t)ENABLE;
  91. }
  92. /**
  93. * @brief Disables access to the backup domain (RTC registers, RTC
  94. * backup data registers).
  95. * @note If the HSE divided by 2, 4, 8 or 16 is used as the RTC clock, the
  96. * Backup Domain Access should be kept enabled.
  97. * @retval None
  98. */
  99. void HAL_PWR_DisableBkUpAccess(void)
  100. {
  101. /* Disable access to RTC and backup registers */
  102. *(__IO uint32_t *) CR_DBP_BB = (uint32_t)DISABLE;
  103. }
  104. /**
  105. * @}
  106. */
  107. /** @defgroup PWR_Exported_Functions_Group2 Peripheral Control functions
  108. * @brief Low Power modes configuration functions
  109. *
  110. @verbatim
  111. ===============================================================================
  112. ##### Peripheral Control functions #####
  113. ===============================================================================
  114. *** PVD configuration ***
  115. =========================
  116. [..]
  117. (+) The PVD is used to monitor the VDD power supply by comparing it to a
  118. threshold selected by the PVD Level (PLS[2:0] bits in the PWR_CR).
  119. (+) The PVD can use an external input analog voltage (PVD_IN) which is compared
  120. internally to VREFINT. The PVD_IN (PB7) has to be configured in Analog mode
  121. when PWR_PVDLevel_7 is selected (PLS[2:0] = 111).
  122. (+) A PVDO flag is available to indicate if VDD/VDDA is higher or lower
  123. than the PVD threshold. This event is internally connected to the EXTI
  124. line16 and can generate an interrupt if enabled. This is done through
  125. __HAL_PWR_PVD_EXTI_ENABLE_IT() macro.
  126. (+) The PVD is stopped in Standby mode.
  127. *** WakeUp pin configuration ***
  128. ================================
  129. [..]
  130. (+) WakeUp pin is used to wake up the system from Standby mode. This pin is
  131. forced in input pull-down configuration and is active on rising edges.
  132. (+) There are two or three WakeUp pins:
  133. WakeUp Pin 1 on PA.00.
  134. WakeUp Pin 2 on PC.13.
  135. WakeUp Pin 3 on PE.06. : Only on product with GPIOE available
  136. [..]
  137. *** Main and Backup Regulators configuration ***
  138. ================================================
  139. (+) The main internal regulator can be configured to have a tradeoff between
  140. performance and power consumption when the device does not operate at
  141. the maximum frequency. This is done through __HAL_PWR_VOLTAGESCALING_CONFIG()
  142. macro which configure VOS bit in PWR_CR register:
  143. (++) When this bit is set (Regulator voltage output Scale 1 mode selected)
  144. the System frequency can go up to 32 MHz.
  145. (++) When this bit is reset (Regulator voltage output Scale 2 mode selected)
  146. the System frequency can go up to 16 MHz.
  147. (++) When this bit is reset (Regulator voltage output Scale 3 mode selected)
  148. the System frequency can go up to 4.2 MHz.
  149. Refer to the datasheets for more details.
  150. *** Low Power modes configuration ***
  151. =====================================
  152. [..]
  153. The device features 5 low-power modes:
  154. (+) Low power run mode: regulator in low power mode, limited clock frequency,
  155. limited number of peripherals running.
  156. (+) Sleep mode: Cortex-M3 core stopped, peripherals kept running.
  157. (+) Low power sleep mode: Cortex-M3 core stopped, limited clock frequency,
  158. limited number of peripherals running, regulator in low power mode.
  159. (+) Stop mode: All clocks are stopped, regulator running, regulator in low power mode.
  160. (+) Standby mode: VCORE domain powered off
  161. *** Low power run mode ***
  162. =========================
  163. [..]
  164. To further reduce the consumption when the system is in Run mode, the regulator can be
  165. configured in low power mode. In this mode, the system frequency should not exceed
  166. MSI frequency range1.
  167. In Low power run mode, all I/O pins keep the same state as in Run mode.
  168. (+) Entry:
  169. (++) VCORE in range2
  170. (++) Decrease the system frequency tonot exceed the frequency of MSI frequency range1.
  171. (++) The regulator is forced in low power mode using the HAL_PWREx_EnableLowPowerRunMode()
  172. function.
  173. (+) Exit:
  174. (++) The regulator is forced in Main regulator mode using the HAL_PWREx_DisableLowPowerRunMode()
  175. function.
  176. (++) Increase the system frequency if needed.
  177. *** Sleep mode ***
  178. ==================
  179. [..]
  180. (+) Entry:
  181. The Sleep mode is entered by using the HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFx)
  182. functions with
  183. (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  184. (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  185. (+) Exit:
  186. (++) Any peripheral interrupt acknowledged by the nested vectored interrupt
  187. controller (NVIC) can wake up the device from Sleep mode.
  188. *** Low power sleep mode ***
  189. ============================
  190. [..]
  191. (+) Entry:
  192. The Low power sleep mode is entered by using the HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFx)
  193. functions with
  194. (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  195. (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  196. (+) The Flash memory can be switched off by using the control bits (SLEEP_PD in the FLASH_ACR register.
  197. This reduces power consumption but increases the wake-up time.
  198. (+) Exit:
  199. (++) If the WFI instruction was used to enter Low power sleep mode, any peripheral interrupt
  200. acknowledged by the nested vectored interrupt controller (NVIC) can wake up the device
  201. from Low power sleep mode. If the WFE instruction was used to enter Low power sleep mode,
  202. the MCU exits Sleep mode as soon as an event occurs.
  203. *** Stop mode ***
  204. =================
  205. [..]
  206. The Stop mode is based on the Cortex-M3 deepsleep mode combined with peripheral
  207. clock gating. The voltage regulator can be configured either in normal or low-power mode.
  208. In Stop mode, all clocks in the VCORE domain are stopped, the PLL, the MSI, the HSI and
  209. the HSE RC oscillators are disabled. Internal SRAM and register contents are preserved.
  210. To get the lowest consumption in Stop mode, the internal Flash memory also enters low
  211. power mode. When the Flash memory is in power-down mode, an additional startup delay is
  212. incurred when waking up from Stop mode.
  213. To minimize the consumption In Stop mode, VREFINT, the BOR, PVD, and temperature
  214. sensor can be switched off before entering Stop mode. They can be switched on again by
  215. software after exiting Stop mode using the ULP bit in the PWR_CR register.
  216. In Stop mode, all I/O pins keep the same state as in Run mode.
  217. (+) Entry:
  218. The Stop mode is entered using the HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI )
  219. function with:
  220. (++) Main regulator ON.
  221. (++) Low Power regulator ON.
  222. (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  223. (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  224. (+) Exit:
  225. (++) By issuing an interrupt or a wakeup event, the MSI RC oscillator is selected as system clock.
  226. *** Standby mode ***
  227. ====================
  228. [..]
  229. The Standby mode allows to achieve the lowest power consumption. It is based on the
  230. Cortex-M3 deepsleep mode, with the voltage regulator disabled. The VCORE domain is
  231. consequently powered off. The PLL, the MSI, the HSI oscillator and the HSE oscillator are
  232. also switched off. SRAM and register contents are lost except for the RTC registers, RTC
  233. backup registers and Standby circuitry.
  234. To minimize the consumption In Standby mode, VREFINT, the BOR, PVD, and temperature
  235. sensor can be switched off before entering the Standby mode. They can be switched
  236. on again by software after exiting the Standby mode.
  237. function.
  238. (+) Entry:
  239. (++) The Standby mode is entered using the HAL_PWR_EnterSTANDBYMode() function.
  240. (+) Exit:
  241. (++) WKUP pin rising edge, RTC alarm (Alarm A and Alarm B), RTC wakeup,
  242. tamper event, time-stamp event, external reset in NRST pin, IWDG reset.
  243. *** Auto-wakeup (AWU) from low-power mode ***
  244. =============================================
  245. [..]
  246. The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC
  247. Wakeup event, a tamper event, a time-stamp event, or a comparator event,
  248. without depending on an external interrupt (Auto-wakeup mode).
  249. (+) RTC auto-wakeup (AWU) from the Stop mode
  250. (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to:
  251. (+++) Configure the EXTI Line 17 to be sensitive to rising edges (Interrupt
  252. or Event modes) and Enable the RTC Alarm Interrupt using the HAL_RTC_SetAlarm_IT()
  253. function
  254. (+++) Configure the RTC to generate the RTC alarm using the HAL_RTC_Init()
  255. and HAL_RTC_SetTime() functions.
  256. (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it
  257. is necessary to:
  258. (+++) Configure the EXTI Line 19 to be sensitive to rising edges (Interrupt or Event modes) and
  259. Enable the RTC Tamper or time stamp Interrupt using the HAL_RTCEx_SetTamper_IT()
  260. or HAL_RTCEx_SetTimeStamp_IT() functions.
  261. (++) To wake up from the Stop mode with an RTC WakeUp event, it is necessary to:
  262. (+++) Configure the EXTI Line 20 to be sensitive to rising edges (Interrupt or Event modes) and
  263. Enable the RTC WakeUp Interrupt using the HAL_RTCEx_SetWakeUpTimer_IT() function.
  264. (+++) Configure the RTC to generate the RTC WakeUp event using the HAL_RTCEx_SetWakeUpTimer()
  265. function.
  266. (+) RTC auto-wakeup (AWU) from the Standby mode
  267. (++) To wake up from the Standby mode with an RTC alarm event, it is necessary to:
  268. (+++) Enable the RTC Alarm Interrupt using the HAL_RTC_SetAlarm_IT() function.
  269. (+++) Configure the RTC to generate the RTC alarm using the HAL_RTC_Init()
  270. and HAL_RTC_SetTime() functions.
  271. (++) To wake up from the Standby mode with an RTC Tamper or time stamp event, it
  272. is necessary to:
  273. (+++) Enable the RTC Tamper or time stamp Interrupt and Configure the RTC to
  274. detect the tamper or time stamp event using the HAL_RTCEx_SetTimeStamp_IT()
  275. or HAL_RTCEx_SetTamper_IT()functions.
  276. (++) To wake up from the Standby mode with an RTC WakeUp event, it is necessary to:
  277. (+++) Enable the RTC WakeUp Interrupt and Configure the RTC to generate the RTC WakeUp event
  278. using the HAL_RTCEx_SetWakeUpTimer_IT() and HAL_RTCEx_SetWakeUpTimer() functions.
  279. (+) Comparator auto-wakeup (AWU) from the Stop mode
  280. (++) To wake up from the Stop mode with an comparator 1 or comparator 2 wakeup
  281. event, it is necessary to:
  282. (+++) Configure the EXTI Line 21 or EXTI Line 22 for comparator to be sensitive to to the
  283. selected edges (falling, rising or falling and rising) (Interrupt or Event modes) using
  284. the COMP functions.
  285. (+++) Configure the comparator to generate the event.
  286. @endverbatim
  287. * @{
  288. */
  289. /**
  290. * @brief Configures the voltage threshold detected by the Power Voltage Detector(PVD).
  291. * @param sConfigPVD pointer to an PWR_PVDTypeDef structure that contains the configuration
  292. * information for the PVD.
  293. * @note Refer to the electrical characteristics of your device datasheet for
  294. * more details about the voltage threshold corresponding to each
  295. * detection level.
  296. * @retval None
  297. */
  298. void HAL_PWR_ConfigPVD(PWR_PVDTypeDef *sConfigPVD)
  299. {
  300. /* Check the parameters */
  301. assert_param(IS_PWR_PVD_LEVEL(sConfigPVD->PVDLevel));
  302. assert_param(IS_PWR_PVD_MODE(sConfigPVD->Mode));
  303. /* Set PLS[7:5] bits according to PVDLevel value */
  304. MODIFY_REG(PWR->CR, PWR_CR_PLS, sConfigPVD->PVDLevel);
  305. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  306. __HAL_PWR_PVD_EXTI_DISABLE_EVENT();
  307. __HAL_PWR_PVD_EXTI_DISABLE_IT();
  308. __HAL_PWR_PVD_EXTI_DISABLE_RISING_FALLING_EDGE();
  309. /* Configure interrupt mode */
  310. if((sConfigPVD->Mode & PVD_MODE_IT) == PVD_MODE_IT)
  311. {
  312. __HAL_PWR_PVD_EXTI_ENABLE_IT();
  313. }
  314. /* Configure event mode */
  315. if((sConfigPVD->Mode & PVD_MODE_EVT) == PVD_MODE_EVT)
  316. {
  317. __HAL_PWR_PVD_EXTI_ENABLE_EVENT();
  318. }
  319. /* Configure the edge */
  320. if((sConfigPVD->Mode & PVD_RISING_EDGE) == PVD_RISING_EDGE)
  321. {
  322. __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
  323. }
  324. if((sConfigPVD->Mode & PVD_FALLING_EDGE) == PVD_FALLING_EDGE)
  325. {
  326. __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();
  327. }
  328. }
  329. /**
  330. * @brief Enables the Power Voltage Detector(PVD).
  331. * @retval None
  332. */
  333. void HAL_PWR_EnablePVD(void)
  334. {
  335. /* Enable the power voltage detector */
  336. *(__IO uint32_t *) CR_PVDE_BB = (uint32_t)ENABLE;
  337. }
  338. /**
  339. * @brief Disables the Power Voltage Detector(PVD).
  340. * @retval None
  341. */
  342. void HAL_PWR_DisablePVD(void)
  343. {
  344. /* Disable the power voltage detector */
  345. *(__IO uint32_t *) CR_PVDE_BB = (uint32_t)DISABLE;
  346. }
  347. /**
  348. * @brief Enables the WakeUp PINx functionality.
  349. * @param WakeUpPinx: Specifies the Power Wake-Up pin to enable.
  350. * This parameter can be one of the following values:
  351. * @arg PWR_WAKEUP_PIN1
  352. * @arg PWR_WAKEUP_PIN2
  353. * @arg PWR_WAKEUP_PIN3: Only on product with GPIOE available
  354. * @retval None
  355. */
  356. void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinx)
  357. {
  358. /* Check the parameter */
  359. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
  360. /* Enable the EWUPx pin */
  361. *(__IO uint32_t *) CSR_EWUP_BB(WakeUpPinx) = (uint32_t)ENABLE;
  362. }
  363. /**
  364. * @brief Disables the WakeUp PINx functionality.
  365. * @param WakeUpPinx: Specifies the Power Wake-Up pin to disable.
  366. * This parameter can be one of the following values:
  367. * @arg PWR_WAKEUP_PIN1
  368. * @arg PWR_WAKEUP_PIN2
  369. * @arg PWR_WAKEUP_PIN3: Only on product with GPIOE available
  370. * @retval None
  371. */
  372. void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx)
  373. {
  374. /* Check the parameter */
  375. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
  376. /* Disable the EWUPx pin */
  377. *(__IO uint32_t *) CSR_EWUP_BB(WakeUpPinx) = (uint32_t)DISABLE;
  378. }
  379. /**
  380. * @brief Enters Sleep mode.
  381. * @note In Sleep mode, all I/O pins keep the same state as in Run mode.
  382. * @param Regulator: Specifies the regulator state in SLEEP mode.
  383. * This parameter can be one of the following values:
  384. * @arg PWR_MAINREGULATOR_ON: SLEEP mode with regulator ON
  385. * @arg PWR_LOWPOWERREGULATOR_ON: SLEEP mode with low power regulator ON
  386. * @param SLEEPEntry: Specifies if SLEEP mode is entered with WFI or WFE instruction.
  387. * When WFI entry is used, tick interrupt have to be disabled if not desired as
  388. * the interrupt wake up source.
  389. * This parameter can be one of the following values:
  390. * @arg PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  391. * @arg PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  392. * @retval None
  393. */
  394. void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
  395. {
  396. /* Check the parameters */
  397. assert_param(IS_PWR_REGULATOR(Regulator));
  398. assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
  399. /* Select the regulator state in Sleep mode: Set PDDS and LPSDSR bit according to PWR_Regulator value */
  400. MODIFY_REG(PWR->CR, (PWR_CR_PDDS | PWR_CR_LPSDSR), Regulator);
  401. /* Clear SLEEPDEEP bit of Cortex System Control Register */
  402. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  403. /* Select SLEEP mode entry -------------------------------------------------*/
  404. if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
  405. {
  406. /* Request Wait For Interrupt */
  407. __WFI();
  408. }
  409. else
  410. {
  411. /* Request Wait For Event */
  412. __SEV();
  413. __WFE();
  414. __WFE();
  415. }
  416. }
  417. /**
  418. * @brief Enters Stop mode.
  419. * @note In Stop mode, all I/O pins keep the same state as in Run mode.
  420. * @note When exiting Stop mode by using an interrupt or a wakeup event,
  421. * MSI RC oscillator is selected as system clock.
  422. * @note When the voltage regulator operates in low power mode, an additional
  423. * startup delay is incurred when waking up from Stop mode.
  424. * By keeping the internal regulator ON during Stop mode, the consumption
  425. * is higher although the startup time is reduced.
  426. * @param Regulator: Specifies the regulator state in Stop mode.
  427. * This parameter can be one of the following values:
  428. * @arg PWR_MAINREGULATOR_ON: Stop mode with regulator ON
  429. * @arg PWR_LOWPOWERREGULATOR_ON: Stop mode with low power regulator ON
  430. * @param STOPEntry: Specifies if Stop mode in entered with WFI or WFE instruction.
  431. * This parameter can be one of the following values:
  432. * @arg PWR_STOPENTRY_WFI: Enter Stop mode with WFI instruction
  433. * @arg PWR_STOPENTRY_WFE: Enter Stop mode with WFE instruction
  434. * @retval None
  435. */
  436. void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
  437. {
  438. /* Check the parameters */
  439. assert_param(IS_PWR_REGULATOR(Regulator));
  440. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  441. /* Select the regulator state in Stop mode: Set PDDS and LPSDSR bit according to PWR_Regulator value */
  442. MODIFY_REG(PWR->CR, (PWR_CR_PDDS | PWR_CR_LPSDSR), Regulator);
  443. /* Set SLEEPDEEP bit of Cortex System Control Register */
  444. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  445. /* Select Stop mode entry --------------------------------------------------*/
  446. if(STOPEntry == PWR_STOPENTRY_WFI)
  447. {
  448. /* Request Wait For Interrupt */
  449. __WFI();
  450. }
  451. else
  452. {
  453. /* Request Wait For Event */
  454. __SEV();
  455. __WFE();
  456. __WFE();
  457. }
  458. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  459. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  460. }
  461. /**
  462. * @brief Enters Standby mode.
  463. * @note In Standby mode, all I/O pins are high impedance except for:
  464. * - Reset pad (still available)
  465. * - RTC_AF1 pin (PC13) if configured for tamper, time-stamp, RTC
  466. * Alarm out, or RTC clock calibration out.
  467. * - WKUP pin 1 (PA0) if enabled.
  468. * - WKUP pin 2 (PC13) if enabled.
  469. * - WKUP pin 3 (PE6) if enabled.
  470. * @retval None
  471. */
  472. void HAL_PWR_EnterSTANDBYMode(void)
  473. {
  474. /* Select Standby mode */
  475. SET_BIT(PWR->CR, PWR_CR_PDDS);
  476. /* Set SLEEPDEEP bit of Cortex System Control Register */
  477. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  478. /* This option is used to ensure that store operations are completed */
  479. #if defined ( __CC_ARM)
  480. __force_stores();
  481. #endif
  482. /* Request Wait For Interrupt */
  483. __WFI();
  484. }
  485. /**
  486. * @brief Indicates Sleep-On-Exit when returning from Handler mode to Thread mode.
  487. * @note Set SLEEPONEXIT bit of SCR register. When this bit is set, the processor
  488. * re-enters SLEEP mode when an interruption handling is over.
  489. * Setting this bit is useful when the processor is expected to run only on
  490. * interruptions handling.
  491. * @retval None
  492. */
  493. void HAL_PWR_EnableSleepOnExit(void)
  494. {
  495. /* Set SLEEPONEXIT bit of Cortex System Control Register */
  496. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  497. }
  498. /**
  499. * @brief Disables Sleep-On-Exit feature when returning from Handler mode to Thread mode.
  500. * @note Clears SLEEPONEXIT bit of SCR register. When this bit is set, the processor
  501. * re-enters SLEEP mode when an interruption handling is over.
  502. * @retval None
  503. */
  504. void HAL_PWR_DisableSleepOnExit(void)
  505. {
  506. /* Clear SLEEPONEXIT bit of Cortex System Control Register */
  507. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  508. }
  509. /**
  510. * @brief Enables CORTEX M3 SEVONPEND bit.
  511. * @note Sets SEVONPEND bit of SCR register. When this bit is set, this causes
  512. * WFE to wake up when an interrupt moves from inactive to pended.
  513. * @retval None
  514. */
  515. void HAL_PWR_EnableSEVOnPend(void)
  516. {
  517. /* Set SEVONPEND bit of Cortex System Control Register */
  518. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  519. }
  520. /**
  521. * @brief Disables CORTEX M3 SEVONPEND bit.
  522. * @note Clears SEVONPEND bit of SCR register. When this bit is set, this causes
  523. * WFE to wake up when an interrupt moves from inactive to pended.
  524. * @retval None
  525. */
  526. void HAL_PWR_DisableSEVOnPend(void)
  527. {
  528. /* Clear SEVONPEND bit of Cortex System Control Register */
  529. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  530. }
  531. /**
  532. * @brief This function handles the PWR PVD interrupt request.
  533. * @note This API should be called under the PVD_IRQHandler().
  534. * @retval None
  535. */
  536. void HAL_PWR_PVD_IRQHandler(void)
  537. {
  538. /* Check PWR exti flag */
  539. if(__HAL_PWR_PVD_EXTI_GET_FLAG() != RESET)
  540. {
  541. /* PWR PVD interrupt user callback */
  542. HAL_PWR_PVDCallback();
  543. /* Clear PWR Exti pending bit */
  544. __HAL_PWR_PVD_EXTI_CLEAR_FLAG();
  545. }
  546. }
  547. /**
  548. * @brief PWR PVD interrupt callback
  549. * @retval None
  550. */
  551. __weak void HAL_PWR_PVDCallback(void)
  552. {
  553. /* NOTE : This function Should not be modified, when the callback is needed,
  554. the HAL_PWR_PVDCallback could be implemented in the user file
  555. */
  556. }
  557. /**
  558. * @}
  559. */
  560. /**
  561. * @}
  562. */
  563. #endif /* HAL_PWR_MODULE_ENABLED */
  564. /**
  565. * @}
  566. */
  567. /**
  568. * @}
  569. */
  570. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/