stm32l0xx_hal_pwr.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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. * Copyright (c) 2016 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. */
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32l0xx_hal.h"
  26. #ifdef HAL_PWR_MODULE_ENABLED
  27. /** @addtogroup STM32L0xx_HAL_Driver
  28. * @{
  29. */
  30. /** @addtogroup PWR
  31. * @{
  32. */
  33. /** @addtogroup PWR_Private
  34. * @{
  35. */
  36. #if defined(PWR_PVD_SUPPORT)
  37. /** @defgroup PWR_PVD_Mode_Mask PWR PVD Mode Mask
  38. * @{
  39. */
  40. #define PVD_MODE_IT (0x00010000U)
  41. #define PVD_MODE_EVT (0x00020000U)
  42. #define PVD_RISING_EDGE (0x00000001U)
  43. #define PVD_FALLING_EDGE (0x00000002U)
  44. /**
  45. * @}
  46. */
  47. #endif
  48. /**
  49. * @}
  50. */
  51. /** @addtogroup PWR_Exported_Functions
  52. * @{
  53. */
  54. /** @addtogroup PWR_Exported_Functions_Group1
  55. * @brief Initialization and de-initialization functions
  56. *
  57. @verbatim
  58. ===============================================================================
  59. ##### Initialization and de-initialization functions #####
  60. ===============================================================================
  61. @endverbatim
  62. * @{
  63. */
  64. /**
  65. * @brief Deinitializes the HAL PWR peripheral registers to their default reset values.
  66. * @retval None
  67. */
  68. void HAL_PWR_DeInit(void)
  69. {
  70. __HAL_RCC_PWR_FORCE_RESET();
  71. __HAL_RCC_PWR_RELEASE_RESET();
  72. }
  73. /**
  74. * @}
  75. */
  76. /** @addtogroup PWR_Exported_Functions_Group2
  77. * @brief Low Power modes configuration functions
  78. *
  79. @verbatim
  80. ===============================================================================
  81. ##### Peripheral Control functions #####
  82. ===============================================================================
  83. *** Backup domain ***
  84. =========================
  85. [..]
  86. After reset, the backup domain (RTC registers, RTC backup data
  87. registers) is protected against possible unwanted
  88. write accesses.
  89. To enable access to the RTC Domain and RTC registers, proceed as follows:
  90. (+) Enable the Power Controller (PWR) APB1 interface clock using the
  91. __HAL_RCC_PWR_CLK_ENABLE() macro.
  92. (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
  93. *** PVD configuration ***
  94. =========================
  95. [..]
  96. (+) The PVD is used to monitor the VDD power supply by comparing it to a
  97. threshold selected by the PVD Level (PLS[2:0] bits in the PWR_CR).
  98. (+) The PVD can use an external input analog voltage (PVD_IN) which is compared
  99. internally to VREFINT. The PVD_IN (PB7) has to be configured in Analog mode
  100. when PWR_PVDLevel_7 is selected (PLS[2:0] = 111).
  101. (+) A PVDO flag is available to indicate if VDD/VDDA is higher or lower
  102. than the PVD threshold. This event is internally connected to the EXTI
  103. line16 and can generate an interrupt if enabled. This is done through
  104. __HAL_PWR_PVD_EXTI_ENABLE_IT() macro.
  105. (+) The PVD is stopped in Standby mode.
  106. (+) The PVD feature is not supported on L0 Value line.
  107. *** WakeUp pin configuration ***
  108. ================================
  109. [..]
  110. (+) WakeUp pin is used to wake up the system from Standby mode. This pin is
  111. forced in input pull-down configuration and is active on rising edges.
  112. (+) There are two WakeUp pins:
  113. WakeUp Pin 1 on PA.00.
  114. WakeUp Pin 2 on PC.13.
  115. WakeUp Pin 3 on PE.06 .
  116. [..]
  117. *** Main and Backup Regulators configuration ***
  118. ================================================
  119. (+) The main internal regulator can be configured to have a tradeoff between
  120. performance and power consumption when the device does not operate at
  121. the maximum frequency. This is done through __HAL_PWR_VOLTAGESCALING_CONFIG()
  122. macro which configures the two VOS bits in PWR_CR register:
  123. (++) PWR_REGULATOR_VOLTAGE_SCALE1 (VOS bits = 01), the regulator voltage output Scale 1 mode selected and
  124. the System frequency can go up to 32 MHz.
  125. (++) PWR_REGULATOR_VOLTAGE_SCALE2 (VOS bits = 10), the regulator voltage output Scale 2 mode selected and
  126. the System frequency can go up to 16 MHz.
  127. (++) PWR_REGULATOR_VOLTAGE_SCALE3 (VOS bits = 11), the regulator voltage output Scale 3 mode selected and
  128. the System frequency can go up to 4.2 MHz.
  129. Refer to the datasheets for more details.
  130. *** Low Power modes configuration ***
  131. =====================================
  132. [..]
  133. The device features 5 low-power modes:
  134. (+) Low power run mode: regulator in low power mode, limited clock frequency,
  135. limited number of peripherals running.
  136. (+) Sleep mode: Cortex-M0+ core stopped, peripherals kept running.
  137. (+) Low power sleep mode: Cortex-M0+ core stopped, limited clock frequency,
  138. limited number of peripherals running, regulator in low power mode.
  139. (+) Stop mode: All clocks are stopped, regulator running, regulator in low power mode.
  140. (+) Standby mode: VCORE domain powered off
  141. *** Low power run mode ***
  142. =========================
  143. [..]
  144. To further reduce the consumption when the system is in Run mode, the regulator can be
  145. configured in low power mode. In this mode, the system frequency should not exceed
  146. MSI frequency range1.
  147. In Low power run mode, all I/O pins keep the same state as in Run mode.
  148. (+) Entry:
  149. (++) VCORE in range2
  150. (++) Decrease the system frequency not to exceed the frequency of MSI frequency range1.
  151. (++) The regulator is forced in low power mode using the HAL_PWREx_EnableLowPowerRunMode()
  152. function.
  153. (+) Exit:
  154. (++) The regulator is forced in Main regulator mode using the HAL_PWREx_DisableLowPowerRunMode()
  155. function.
  156. (++) Increase the system frequency if needed.
  157. *** Sleep mode ***
  158. ==================
  159. [..]
  160. (+) Entry:
  161. The Sleep mode is entered by using the HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFx)
  162. functions with
  163. (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  164. (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  165. (+) Exit:
  166. (++) Any peripheral interrupt acknowledged by the nested vectored interrupt
  167. controller (NVIC) can wake up the device from Sleep mode. If the WFE instruction was used to enter sleep mode,
  168. the MCU exits Sleep mode as soon as an event occurs.
  169. *** Low power sleep mode ***
  170. ============================
  171. [..]
  172. (+) Entry:
  173. The Low power sleep mode is entered by using the HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFx)
  174. functions with
  175. (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  176. (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  177. (+) The Flash memory can be switched off by using the control bits (SLEEP_PD in the FLASH_ACR register.
  178. This reduces power consumption but increases the wake-up time.
  179. (+) Exit:
  180. (++) If the WFI instruction was used to enter Low power sleep mode, any peripheral interrupt
  181. acknowledged by the nested vectored interrupt controller (NVIC) can wake up the device
  182. from Low power sleep mode. If the WFE instruction was used to enter Low power sleep mode,
  183. the MCU exits Sleep mode as soon as an event occurs.
  184. *** Stop mode ***
  185. =================
  186. [..]
  187. The Stop mode is based on the Cortex-M0+ deepsleep mode combined with peripheral
  188. clock gating. The voltage regulator can be configured either in normal or low-power mode.
  189. In Stop mode, all clocks in the VCORE domain are stopped, the PLL, the MSI, the HSI and
  190. the HSE RC oscillators are disabled. Internal SRAM and register contents are preserved.
  191. To get the lowest consumption in Stop mode, the internal Flash memory also enters low
  192. power mode. When the Flash memory is in power-down mode, an additional startup delay is
  193. incurred when waking up from Stop mode.
  194. To minimize the consumption In Stop mode, VREFINT, the BOR, PVD, and temperature
  195. sensor can be switched off before entering Stop mode. They can be switched on again by
  196. software after exiting Stop mode using the ULP bit in the PWR_CR register.
  197. In Stop mode, all I/O pins keep the same state as in Run mode.
  198. (+) Entry:
  199. The Stop mode is entered using the HAL_PWR_EnterSTOPMode
  200. function with:
  201. (++) Main regulator ON.
  202. (++) Low Power regulator ON.
  203. (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  204. (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  205. (+) Exit:
  206. (++) By issuing an interrupt or a wakeup event, the MSI or HSI16 RC
  207. oscillator is selected as system clock depending the bit STOPWUCK in the RCC_CFGR
  208. register
  209. *** Standby mode ***
  210. ====================
  211. [..]
  212. The Standby mode allows to achieve the lowest power consumption. It is based on the
  213. Cortex-M0+ deepsleep mode, with the voltage regulator disabled. The VCORE domain is
  214. consequently powered off. The PLL, the MSI, the HSI oscillator and the HSE oscillator are
  215. also switched off. SRAM and register contents are lost except for the RTC registers, RTC
  216. backup registers and Standby circuitry.
  217. To minimize the consumption In Standby mode, VREFINT, the BOR, PVD, and temperature
  218. sensor can be switched off before entering the Standby mode. They can be switched
  219. on again by software after exiting the Standby mode.
  220. function.
  221. (+) Entry:
  222. (++) The Standby mode is entered using the HAL_PWR_EnterSTANDBYMode() function.
  223. (+) Exit:
  224. (++) WKUP pin rising edge, RTC alarm (Alarm A and Alarm B), RTC wakeup,
  225. tamper event, time-stamp event, external reset in NRST pin, IWDG reset.
  226. *** Auto-wakeup (AWU) from low-power mode ***
  227. =============================================
  228. [..]
  229. The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC
  230. Wakeup event, a tamper event, a time-stamp event, or a comparator event,
  231. without depending on an external interrupt (Auto-wakeup mode).
  232. (+) RTC auto-wakeup (AWU) from the Stop mode
  233. (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to:
  234. (+++) Configure the EXTI Line 17 to be sensitive to rising edges (Interrupt
  235. or Event modes) using the EXTI_Init() function.
  236. (+++) Enable the RTC Alarm Interrupt using the RTC_ITConfig() function
  237. (+++) Configure the RTC to generate the RTC alarm using the RTC_SetAlarm()
  238. and RTC_AlarmCmd() functions.
  239. (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it
  240. is necessary to:
  241. (+++) Configure the EXTI Line 19 to be sensitive to rising edges (Interrupt
  242. or Event modes) using the EXTI_Init() function.
  243. (+++) Enable the RTC Tamper or time stamp Interrupt using the RTC_ITConfig()
  244. function.
  245. (+++) Configure the RTC to detect the tamper or time stamp event using the
  246. RTC_TimeStampConfig(), RTC_TamperTriggerConfig() and RTC_TamperCmd()
  247. functions.
  248. (++) To wake up from the Stop mode with an RTC WakeUp event, it is necessary to:
  249. (+++) Configure the EXTI Line 20 to be sensitive to rising edges (Interrupt
  250. or Event modes) using the EXTI_Init() function.
  251. (+++) Enable the RTC WakeUp Interrupt using the RTC_ITConfig() function.
  252. (+++) Configure the RTC to generate the RTC WakeUp event using the RTC_WakeUpClockConfig(),
  253. RTC_SetWakeUpCounter() and RTC_WakeUpCmd() functions.
  254. (+) RTC auto-wakeup (AWU) from the Standby mode
  255. (++) To wake up from the Standby mode with an RTC alarm event, it is necessary to:
  256. (+++) Enable the RTC Alarm Interrupt using the RTC_ITConfig() function.
  257. (+++) Configure the RTC to generate the RTC alarm using the RTC_SetAlarm()
  258. and RTC_AlarmCmd() functions.
  259. (++) To wake up from the Standby mode with an RTC Tamper or time stamp event, it
  260. is necessary to:
  261. (+++) Enable the RTC Tamper or time stamp Interrupt using the RTC_ITConfig()
  262. function.
  263. (+++) Configure the RTC to detect the tamper or time stamp event using the
  264. RTC_TimeStampConfig(), RTC_TamperTriggerConfig() and RTC_TamperCmd()
  265. functions.
  266. (++) To wake up from the Standby mode with an RTC WakeUp event, it is necessary to:
  267. (+++) Enable the RTC WakeUp Interrupt using the RTC_ITConfig() function
  268. (+++) Configure the RTC to generate the RTC WakeUp event using the RTC_WakeUpClockConfig(),
  269. RTC_SetWakeUpCounter() and RTC_WakeUpCmd() functions.
  270. (+) Comparator auto-wakeup (AWU) from the Stop mode
  271. (++) To wake up from the Stop mode with an comparator 1 or comparator 2 wakeup
  272. event, it is necessary to:
  273. (+++) Configure the EXTI Line 21 for comparator 1 or EXTI Line 22 for comparator 2
  274. to be sensitive to to the selected edges (falling, rising or falling
  275. and rising) (Interrupt or Event modes) using the EXTI_Init() function.
  276. (+++) Configure the comparator to generate the event.
  277. @endverbatim
  278. * @{
  279. */
  280. /**
  281. * @brief Enables access to the backup domain (RTC registers, RTC
  282. * backup data registers ).
  283. * @note If the HSE divided by 2, 4, 8 or 16 is used as the RTC clock, the
  284. * Backup Domain Access should be kept enabled.
  285. * @retval None
  286. */
  287. void HAL_PWR_EnableBkUpAccess(void)
  288. {
  289. /* Enable access to RTC and backup registers */
  290. SET_BIT(PWR->CR, PWR_CR_DBP);
  291. }
  292. /**
  293. * @brief Disables access to the backup domain
  294. * @note Applies to RTC registers, RTC backup data registers.
  295. * @note If the HSE divided by 2, 4, 8 or 16 is used as the RTC clock, the
  296. * Backup Domain Access should be kept enabled.
  297. * @retval None
  298. */
  299. void HAL_PWR_DisableBkUpAccess(void)
  300. {
  301. /* Disable access to RTC and backup registers */
  302. CLEAR_BIT(PWR->CR, PWR_CR_DBP);
  303. }
  304. #if defined(PWR_PVD_SUPPORT)
  305. /**
  306. * @brief Configures the voltage threshold detected by the Power Voltage Detector(PVD).
  307. * @param sConfigPVD pointer to an PWR_PVDTypeDef structure that contains the configuration
  308. * information for the PVD.
  309. * @note Refer to the electrical characteristics of your device datasheet for
  310. * more details about the voltage threshold corresponding to each
  311. * detection level.
  312. * @retval None
  313. */
  314. void HAL_PWR_ConfigPVD(PWR_PVDTypeDef *sConfigPVD)
  315. {
  316. /* Check the parameters */
  317. assert_param(IS_PWR_PVD_LEVEL(sConfigPVD->PVDLevel));
  318. assert_param(IS_PWR_PVD_MODE(sConfigPVD->Mode));
  319. /* Set PLS[7:5] bits according to PVDLevel value */
  320. MODIFY_REG(PWR->CR, PWR_CR_PLS, sConfigPVD->PVDLevel);
  321. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  322. __HAL_PWR_PVD_EXTI_DISABLE_EVENT();
  323. __HAL_PWR_PVD_EXTI_DISABLE_IT();
  324. __HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();
  325. __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();
  326. /* Configure interrupt mode */
  327. if((sConfigPVD->Mode & PVD_MODE_IT) == PVD_MODE_IT)
  328. {
  329. __HAL_PWR_PVD_EXTI_ENABLE_IT();
  330. }
  331. /* Configure event mode */
  332. if((sConfigPVD->Mode & PVD_MODE_EVT) == PVD_MODE_EVT)
  333. {
  334. __HAL_PWR_PVD_EXTI_ENABLE_EVENT();
  335. }
  336. /* Configure the edge */
  337. if((sConfigPVD->Mode & PVD_RISING_EDGE) == PVD_RISING_EDGE)
  338. {
  339. __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
  340. }
  341. if((sConfigPVD->Mode & PVD_FALLING_EDGE) == PVD_FALLING_EDGE)
  342. {
  343. __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();
  344. }
  345. }
  346. /**
  347. * @brief Enables the Power Voltage Detector(PVD).
  348. * @retval None
  349. */
  350. void HAL_PWR_EnablePVD(void)
  351. {
  352. /* Enable the power voltage detector */
  353. SET_BIT(PWR->CR, PWR_CR_PVDE);
  354. }
  355. /**
  356. * @brief Disables the Power Voltage Detector(PVD).
  357. * @retval None
  358. */
  359. void HAL_PWR_DisablePVD(void)
  360. {
  361. /* Disable the power voltage detector */
  362. CLEAR_BIT(PWR->CR, PWR_CR_PVDE);
  363. }
  364. #endif /* PWR_PVD_SUPPORT */
  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 for stm32l07xxx and stm32l08xxx devices only.
  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. SET_BIT(PWR->CSR, WakeUpPinx);
  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 for stm32l07xxx and stm32l08xxx devices only.
  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. CLEAR_BIT(PWR->CSR, WakeUpPinx);
  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. uint32_t tmpreg = 0U;
  415. uint32_t ulpbit, vrefinbit;
  416. /* Check the parameters */
  417. assert_param(IS_PWR_REGULATOR(Regulator));
  418. assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
  419. /* It is forbidden to configure both EN_VREFINT=1 and ULP=1 if the device is
  420. in Stop mode or in Sleep/Low-power sleep mode */
  421. ulpbit = READ_BIT(PWR->CR, PWR_CR_ULP);
  422. vrefinbit = READ_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_EN_VREFINT);
  423. if((ulpbit != 0) && (vrefinbit != 0))
  424. {
  425. CLEAR_BIT(PWR->CR, PWR_CR_ULP);
  426. }
  427. /* Select the regulator state in Sleep mode ---------------------------------*/
  428. tmpreg = PWR->CR;
  429. /* Clear PDDS and LPDS bits */
  430. CLEAR_BIT(tmpreg, (PWR_CR_PDDS | PWR_CR_LPSDSR));
  431. /* Set LPSDSR bit according to PWR_Regulator value */
  432. SET_BIT(tmpreg, Regulator);
  433. /* Store the new value */
  434. PWR->CR = tmpreg;
  435. /* Clear SLEEPDEEP bit of Cortex System Control Register */
  436. CLEAR_BIT(SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  437. /* Select SLEEP mode entry -------------------------------------------------*/
  438. if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
  439. {
  440. /* Request Wait For Interrupt */
  441. __WFI();
  442. }
  443. else
  444. {
  445. /* Request Wait For Event */
  446. __SEV();
  447. __WFE();
  448. __WFE();
  449. }
  450. if((ulpbit != 0) && (vrefinbit != 0))
  451. {
  452. SET_BIT(PWR->CR, PWR_CR_ULP);
  453. }
  454. /* Additional NOP to ensure all pending instructions are flushed before entering low power mode */
  455. __NOP();
  456. }
  457. /**
  458. * @brief Enters Stop mode.
  459. * @note In Stop mode, all I/O pins keep the same state as in Run mode.
  460. * @note When exiting Stop mode by issuing an interrupt or a wakeup event,
  461. * MSI or HSI16 RCoscillator is selected as system clock depending
  462. * the bit STOPWUCK in the RCC_CFGR register.
  463. * @note When the voltage regulator operates in low power mode, an additional
  464. * startup delay is incurred when waking up from Stop mode.
  465. * By keeping the internal regulator ON during Stop mode, the consumption
  466. * is higher although the startup time is reduced.
  467. * @note Before entering in this function, it is important to ensure that the WUF
  468. * wakeup flag is cleared. To perform this action, it is possible to call the
  469. * following macro : __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU)
  470. *
  471. * @param Regulator: Specifies the regulator state in Stop mode.
  472. * This parameter can be one of the following values:
  473. * @arg PWR_MAINREGULATOR_ON: Stop mode with regulator ON
  474. * @arg PWR_LOWPOWERREGULATOR_ON: Stop mode with low power regulator ON
  475. * @param STOPEntry: Specifies if Stop mode in entered with WFI or WFE instruction.
  476. * This parameter can be one of the following values:
  477. * @arg PWR_STOPENTRY_WFI: Enter Stop mode with WFI instruction
  478. * @arg PWR_STOPENTRY_WFE: Enter Stop mode with WFE instruction
  479. * @retval None
  480. */
  481. void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
  482. {
  483. uint32_t tmpreg = 0U;
  484. uint32_t ulpbit, vrefinbit;
  485. /* Check the parameters */
  486. assert_param(IS_PWR_REGULATOR(Regulator));
  487. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  488. /* It is forbidden to configure both EN_VREFINT=1 and ULP=1 if the device is
  489. in Stop mode or in Sleep/Low-power sleep mode */
  490. ulpbit = READ_BIT(PWR->CR, PWR_CR_ULP);
  491. vrefinbit = READ_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_EN_VREFINT);
  492. if((ulpbit != 0) && (vrefinbit != 0))
  493. {
  494. CLEAR_BIT(PWR->CR, PWR_CR_ULP);
  495. }
  496. /* Select the regulator state in Stop mode ---------------------------------*/
  497. tmpreg = PWR->CR;
  498. /* Clear PDDS and LPDS bits */
  499. CLEAR_BIT(tmpreg, (PWR_CR_PDDS | PWR_CR_LPSDSR));
  500. /* Set LPSDSR bit according to PWR_Regulator value */
  501. SET_BIT(tmpreg, Regulator);
  502. /* Store the new value */
  503. PWR->CR = tmpreg;
  504. /* Set SLEEPDEEP bit of Cortex System Control Register */
  505. SET_BIT(SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  506. /* Select Stop mode entry --------------------------------------------------*/
  507. if(STOPEntry == PWR_STOPENTRY_WFI)
  508. {
  509. /* Request Wait For Interrupt */
  510. __WFI();
  511. }
  512. else
  513. {
  514. /* Request Wait For Event */
  515. __SEV();
  516. __WFE();
  517. __WFE();
  518. }
  519. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  520. CLEAR_BIT(SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  521. if((ulpbit != 0) && (vrefinbit != 0))
  522. {
  523. SET_BIT(PWR->CR, PWR_CR_ULP);
  524. }
  525. }
  526. /**
  527. * @brief Enters Standby mode.
  528. * @note In Standby mode, all I/O pins are high impedance except for:
  529. * - Reset pad (still available)
  530. * - RTC_AF1 pin (PC13) if configured for tamper, time-stamp, RTC
  531. * Alarm out, or RTC clock calibration out.
  532. * - RTC_AF2 pin (PC13) if configured for tamper.
  533. * - WKUP pin 1 (PA00) if enabled.
  534. * - WKUP pin 2 (PC13) if enabled.
  535. * - WKUP pin 3 (PE06) if enabled, for stm32l07xxx and stm32l08xxx devices only.
  536. * - WKUP pin 3 (PA02) if enabled, for stm32l031xx devices only.
  537. * @retval None
  538. */
  539. void HAL_PWR_EnterSTANDBYMode(void)
  540. {
  541. /* Select Standby mode */
  542. SET_BIT(PWR->CR, PWR_CR_PDDS);
  543. /* Set SLEEPDEEP bit of Cortex System Control Register */
  544. SET_BIT(SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);
  545. /* This option is used to ensure that store operations are completed */
  546. #if defined ( __CC_ARM)
  547. __force_stores();
  548. #endif
  549. /* Request Wait For Interrupt */
  550. __WFI();
  551. }
  552. /**
  553. * @brief Indicates Sleep-On-Exit when returning from Handler mode to Thread mode.
  554. * @note Set SLEEPONEXIT bit of SCR register. When this bit is set, the processor
  555. * re-enters SLEEP mode when an interruption handling is over.
  556. * Setting this bit is useful when the processor is expected to run only on
  557. * interruptions handling.
  558. * @retval None
  559. */
  560. void HAL_PWR_EnableSleepOnExit(void)
  561. {
  562. /* Set SLEEPONEXIT bit of Cortex System Control Register */
  563. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  564. }
  565. /**
  566. * @brief Disables Sleep-On-Exit feature when returning from Handler mode to Thread mode.
  567. * @note Clears SLEEPONEXIT bit of SCR register. When this bit is set, the processor
  568. * re-enters SLEEP mode when an interruption handling is over.
  569. * @retval None
  570. */
  571. void HAL_PWR_DisableSleepOnExit(void)
  572. {
  573. /* Clear SLEEPONEXIT bit of Cortex System Control Register */
  574. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  575. }
  576. /**
  577. * @brief Enables CORTEX M0+ SEVONPEND bit.
  578. * @note Sets SEVONPEND bit of SCR register. When this bit is set, this causes
  579. * WFE to wake up when an interrupt moves from inactive to pended.
  580. * @retval None
  581. */
  582. void HAL_PWR_EnableSEVOnPend(void)
  583. {
  584. /* Set SEVONPEND bit of Cortex System Control Register */
  585. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  586. }
  587. /**
  588. * @brief Disables CORTEX M0+ SEVONPEND bit.
  589. * @note Clears SEVONPEND bit of SCR register. When this bit is set, this causes
  590. * WFE to wake up when an interrupt moves from inactive to pended.
  591. * @retval None
  592. */
  593. void HAL_PWR_DisableSEVOnPend(void)
  594. {
  595. /* Clear SEVONPEND bit of Cortex System Control Register */
  596. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  597. }
  598. #if defined(PWR_PVD_SUPPORT)
  599. /**
  600. * @brief This function handles the PWR PVD interrupt request.
  601. * @note This API should be called under the PVD_IRQHandler().
  602. * @retval None
  603. */
  604. void HAL_PWR_PVD_IRQHandler(void)
  605. {
  606. /* Check PWR exti flag */
  607. if(__HAL_PWR_PVD_EXTI_GET_FLAG() != RESET)
  608. {
  609. /* PWR PVD interrupt user callback */
  610. HAL_PWR_PVDCallback();
  611. /* Clear PWR Exti pending bit */
  612. __HAL_PWR_PVD_EXTI_CLEAR_FLAG();
  613. }
  614. }
  615. /**
  616. * @brief PWR PVD interrupt callback
  617. * @retval None
  618. */
  619. __weak void HAL_PWR_PVDCallback(void)
  620. {
  621. /* NOTE : This function Should not be modified, when the callback is needed,
  622. the HAL_PWR_PVDCallback could be implemented in the user file
  623. */
  624. }
  625. #endif /* PWR_PVD_SUPPORT */
  626. /**
  627. * @}
  628. */
  629. /**
  630. * @}
  631. */
  632. #endif /* HAL_PWR_MODULE_ENABLED */
  633. /**
  634. * @}
  635. */
  636. /**
  637. * @}
  638. */