stm32l1xx_hal_pwr.c 25 KB

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