stm32l1xx_hal_pwr.c 27 KB

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