stm32f3xx_hal_pwr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_hal_pwr.c
  4. * @author MCD Application Team
  5. * @brief PWR HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Power Controller (PWR) peripheral:
  8. * + Initialization/de-initialization functions
  9. * + Peripheral Control functions
  10. *
  11. @verbatim
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * <h2><center>&copy; COPYRIGHT(c) 2016 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 "stm32f3xx_hal.h"
  43. /** @addtogroup STM32F3xx_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. /* Private macro -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* Private function prototypes -----------------------------------------------*/
  56. /* Private functions ---------------------------------------------------------*/
  57. /** @defgroup PWR_Exported_Functions PWR Exported Functions
  58. * @{
  59. */
  60. /** @defgroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
  61. * @brief Initialization and de-initialization functions
  62. *
  63. @verbatim
  64. ===============================================================================
  65. ##### Initialization and de-initialization functions #####
  66. ===============================================================================
  67. [..]
  68. After reset, the backup domain (RTC registers, RTC backup data
  69. registers and backup SRAM) is protected against possible unwanted
  70. write accesses.
  71. To enable access to the RTC Domain and RTC registers, proceed as follows:
  72. (+) Enable the Power Controller (PWR) APB1 interface clock using the
  73. __HAL_RCC_PWR_CLK_ENABLE() macro.
  74. (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
  75. @endverbatim
  76. * @{
  77. */
  78. /**
  79. * @brief Deinitializes the PWR peripheral registers to their default reset values.
  80. * @retval None
  81. */
  82. void HAL_PWR_DeInit(void)
  83. {
  84. __HAL_RCC_PWR_FORCE_RESET();
  85. __HAL_RCC_PWR_RELEASE_RESET();
  86. }
  87. /**
  88. * @brief Enables access to the backup domain (RTC registers, RTC
  89. * backup data registers and backup SRAM).
  90. * @note If the HSE divided by 32 is used as the RTC clock, the
  91. * Backup Domain Access should be kept enabled.
  92. * @retval None
  93. */
  94. void HAL_PWR_EnableBkUpAccess(void)
  95. {
  96. SET_BIT(PWR->CR, PWR_CR_DBP);
  97. }
  98. /**
  99. * @brief Disables access to the backup domain (RTC registers, RTC
  100. * backup data registers and backup SRAM).
  101. * @note If the HSE divided by 32 is used as the RTC clock, the
  102. * Backup Domain Access should be kept enabled.
  103. * @retval None
  104. */
  105. void HAL_PWR_DisableBkUpAccess(void)
  106. {
  107. CLEAR_BIT(PWR->CR, PWR_CR_DBP);
  108. }
  109. /**
  110. * @}
  111. */
  112. /** @defgroup PWR_Exported_Functions_Group2 Peripheral Control functions
  113. * @brief Low Power modes configuration functions
  114. *
  115. @verbatim
  116. ===============================================================================
  117. ##### Peripheral Control functions #####
  118. ===============================================================================
  119. *** WakeUp pin configuration ***
  120. ================================
  121. [..]
  122. (+) WakeUp pin is used to wakeup the system from Standby mode. This pin is
  123. forced in input pull down configuration and is active on rising edges.
  124. (+) There are up to three WakeUp pins:
  125. (++)WakeUp Pin 1 on PA.00.
  126. (++)WakeUp Pin 2 on PC.13 (STM32F303xC, STM32F303xE only).
  127. (++)WakeUp Pin 3 on PE.06.
  128. *** Main and Backup Regulators configuration ***
  129. ================================================
  130. [..]
  131. (+) When the backup domain is supplied by VDD (analog switch connected to VDD)
  132. the backup SRAM is powered from VDD which replaces the VBAT power supply to
  133. save battery life.
  134. (+) The backup SRAM is not mass erased by a tamper event. It is read
  135. protected to prevent confidential data, such as cryptographic private
  136. key, from being accessed. The backup SRAM can be erased only through
  137. the Flash interface when a protection level change from level 1 to
  138. level 0 is requested.
  139. -@- Refer to the description of Read protection (RDP) in the Flash
  140. programming manual.
  141. Refer to the datasheets for more details.
  142. *** Low Power modes configuration ***
  143. =====================================
  144. [..]
  145. The devices feature 3 low-power modes:
  146. (+) Sleep mode: Cortex-M4 core stopped, peripherals kept running.
  147. (+) Stop mode: all clocks are stopped, regulator running, regulator
  148. in low power mode
  149. (+) Standby mode: 1.2V domain powered off (mode not available on STM32F3x8 devices).
  150. *** Sleep mode ***
  151. ==================
  152. [..]
  153. (+) Entry:
  154. The Sleep mode is entered by using the HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFx)
  155. functions with
  156. (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  157. (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  158. (+) Exit:
  159. (++) Any peripheral interrupt acknowledged by the nested vectored interrupt
  160. controller (NVIC) can wake up the device from Sleep mode.
  161. *** Stop mode ***
  162. =================
  163. [..]
  164. In Stop mode, all clocks in the 1.8V domain are stopped, the PLL, the HSI,
  165. and the HSE RC oscillators are disabled. Internal SRAM and register contents
  166. are preserved.
  167. The voltage regulator can be configured either in normal or low-power mode to minimize the consumption.
  168. (+) Entry:
  169. The Stop mode is entered using the HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI )
  170. function with:
  171. (++) Main regulator ON or
  172. (++) Low Power regulator ON.
  173. (++) PWR_STOPENTRY_WFI: enter STOP mode with WFI instruction or
  174. (++) PWR_STOPENTRY_WFE: enter STOP mode with WFE instruction
  175. (+) Exit:
  176. (++) Any EXTI Line (Internal or External) configured in Interrupt/Event mode.
  177. (++) Some specific communication peripherals (CEC, USART, I2C) interrupts,
  178. when programmed in wakeup mode (the peripheral must be
  179. programmed in wakeup mode and the corresponding interrupt vector
  180. must be enabled in the NVIC).
  181. *** Standby mode ***
  182. ====================
  183. [..]
  184. The Standby mode allows to achieve the lowest power consumption. It is based
  185. on the Cortex-M4 deep sleep mode, with the voltage regulator disabled.
  186. The 1.8V domain is consequently powered off. The PLL, the HSI oscillator and
  187. the HSE oscillator are also switched off. SRAM and register contents are lost
  188. except for the RTC registers, RTC backup registers, backup SRAM and Standby
  189. circuitry.
  190. The voltage regulator is OFF.
  191. (+) Entry:
  192. (++) The Standby mode is entered using the HAL_PWR_EnterSTANDBYMode() function.
  193. (+) Exit:
  194. (++) WKUP pin rising edge, RTC alarm (Alarm A and Alarm B), RTC wakeup,
  195. tamper event, time-stamp event, external reset in NRST pin, IWDG reset.
  196. *** Auto-wakeup (AWU) from low-power mode ***
  197. =============================================
  198. [..]
  199. The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC
  200. Wakeup event, a tamper event, a time-stamp event, or a comparator event,
  201. without depending on an external interrupt (Auto-wakeup mode).
  202. (+) RTC auto-wakeup (AWU) from the Stop and Standby modes
  203. (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to
  204. configure the RTC to generate the RTC alarm using the HAL_RTC_SetAlarm_IT() function.
  205. (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it
  206. is necessary to configure the RTC to detect the tamper or time stamp event using the
  207. HAL_RTC_SetTimeStamp_IT() or HAL_RTC_SetTamper_IT() functions.
  208. (++) To wake up from the Stop mode with an RTC WakeUp event, it is necessary to
  209. configure the RTC to generate the RTC WakeUp event using the HAL_RTC_SetWakeUpTimer_IT() function.
  210. (+) Comparator auto-wakeup (AWU) from the Stop mode
  211. (++) To wake up from the Stop mode with a comparator wakeup event, it is necessary to:
  212. (+++) Configure the EXTI Line associated with the comparator (example EXTI Line 22 for comparator 2U)
  213. to be sensitive to to the selected edges (falling, rising or falling
  214. and rising) (Interrupt or Event modes) using the EXTI_Init() function.
  215. (+++) Configure the comparator to generate the event.
  216. @endverbatim
  217. * @{
  218. */
  219. /**
  220. * @brief Enables the WakeUp PINx functionality.
  221. * @param WakeUpPinx Specifies the Power Wake-Up pin to enable.
  222. * This parameter can be value of :
  223. * @ref PWR_WakeUp_Pins
  224. * @retval None
  225. */
  226. void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinx)
  227. {
  228. /* Check the parameters */
  229. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
  230. /* Enable the EWUPx pin */
  231. SET_BIT(PWR->CSR, WakeUpPinx);
  232. }
  233. /**
  234. * @brief Disables the WakeUp PINx functionality.
  235. * @param WakeUpPinx Specifies the Power Wake-Up pin to disable.
  236. * This parameter can be values of :
  237. * @ref PWR_WakeUp_Pins
  238. * @retval None
  239. */
  240. void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx)
  241. {
  242. /* Check the parameters */
  243. assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
  244. /* Disable the EWUPx pin */
  245. CLEAR_BIT(PWR->CSR, WakeUpPinx);
  246. }
  247. /**
  248. * @brief Enters Sleep mode.
  249. * @note In Sleep mode, all I/O pins keep the same state as in Run mode.
  250. * @param Regulator Specifies the regulator state in SLEEP mode.
  251. * This parameter can be one of the following values:
  252. * @arg PWR_MAINREGULATOR_ON: SLEEP mode with regulator ON
  253. * @arg PWR_LOWPOWERREGULATOR_ON: SLEEP mode with low power regulator ON
  254. * @note This parameter has no effect in F3 family and is just maintained to
  255. * offer full portability of other STM32 families softwares.
  256. * @param SLEEPEntry Specifies if SLEEP mode is entered with WFI or WFE instruction.
  257. * When WFI entry is used, tick interrupt have to be disabled if not desired as
  258. * the interrupt wake up source.
  259. * This parameter can be one of the following values:
  260. * @arg PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
  261. * @arg PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
  262. * @retval None
  263. */
  264. void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
  265. {
  266. /* Check the parameters */
  267. assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
  268. /* Clear SLEEPDEEP bit of Cortex System Control Register */
  269. SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  270. /* Select SLEEP mode entry -------------------------------------------------*/
  271. if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
  272. {
  273. /* Request Wait For Interrupt */
  274. __WFI();
  275. }
  276. else
  277. {
  278. /* Request Wait For Event */
  279. __SEV();
  280. __WFE();
  281. __WFE();
  282. }
  283. }
  284. /**
  285. * @brief Enters STOP mode.
  286. * @note In Stop mode, all I/O pins keep the same state as in Run mode.
  287. * @note When exiting Stop mode by issuing an interrupt or a wakeup event,
  288. * the HSI RC oscillator is selected as system clock.
  289. * @note When the voltage regulator operates in low power mode, an additional
  290. * startup delay is incurred when waking up from Stop mode.
  291. * By keeping the internal regulator ON during Stop mode, the consumption
  292. * is higher although the startup time is reduced.
  293. * @param Regulator Specifies the regulator state in STOP mode.
  294. * This parameter can be one of the following values:
  295. * @arg PWR_MAINREGULATOR_ON: STOP mode with regulator ON
  296. * @arg PWR_LOWPOWERREGULATOR_ON: STOP mode with low power regulator ON
  297. * @param STOPEntry specifies if STOP mode in entered with WFI or WFE instruction.
  298. * This parameter can be one of the following values:
  299. * @arg PWR_STOPENTRY_WFI:Enter STOP mode with WFI instruction
  300. * @arg PWR_STOPENTRY_WFE: Enter STOP mode with WFE instruction
  301. * @retval None
  302. */
  303. void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
  304. {
  305. uint32_t tmpreg = 0U;
  306. /* Check the parameters */
  307. assert_param(IS_PWR_REGULATOR(Regulator));
  308. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  309. /* Select the regulator state in STOP mode ---------------------------------*/
  310. tmpreg = PWR->CR;
  311. /* Clear PDDS and LPDS bits */
  312. tmpreg &= (uint32_t)~(PWR_CR_PDDS | PWR_CR_LPDS);
  313. /* Set LPDS bit according to Regulator value */
  314. tmpreg |= Regulator;
  315. /* Store the new value */
  316. PWR->CR = tmpreg;
  317. /* Set SLEEPDEEP bit of Cortex System Control Register */
  318. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  319. /* Select STOP mode entry --------------------------------------------------*/
  320. if(STOPEntry == PWR_STOPENTRY_WFI)
  321. {
  322. /* Request Wait For Interrupt */
  323. __WFI();
  324. }
  325. else
  326. {
  327. /* Request Wait For Event */
  328. __SEV();
  329. __WFE();
  330. __WFE();
  331. }
  332. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  333. SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  334. }
  335. /**
  336. * @brief Enters STANDBY mode.
  337. * @note In Standby mode, all I/O pins are high impedance except for:
  338. * - Reset pad (still available),
  339. * - RTC alternate function pins if configured for tamper, time-stamp, RTC
  340. * Alarm out, or RTC clock calibration out,
  341. * - WKUP pins if enabled.
  342. * @retval None
  343. */
  344. void HAL_PWR_EnterSTANDBYMode(void)
  345. {
  346. /* Select STANDBY mode */
  347. PWR->CR |= PWR_CR_PDDS;
  348. /* Set SLEEPDEEP bit of Cortex System Control Register */
  349. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  350. /* This option is used to ensure that store operations are completed */
  351. #if defined ( __CC_ARM)
  352. __force_stores();
  353. #endif
  354. /* Request Wait For Interrupt */
  355. __WFI();
  356. }
  357. /**
  358. * @brief Indicates Sleep-On-Exit when returning from Handler mode to Thread mode.
  359. * @note Set SLEEPONEXIT bit of SCR register. When this bit is set, the processor
  360. * re-enters SLEEP mode when an interruption handling is over.
  361. * Setting this bit is useful when the processor is expected to run only on
  362. * interruptions handling.
  363. * @retval None
  364. */
  365. void HAL_PWR_EnableSleepOnExit(void)
  366. {
  367. /* Set SLEEPONEXIT bit of Cortex System Control Register */
  368. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  369. }
  370. /**
  371. * @brief Disables Sleep-On-Exit feature when returning from Handler mode to Thread mode.
  372. * @note Clears SLEEPONEXIT bit of SCR register. When this bit is set, the processor
  373. * re-enters SLEEP mode when an interruption handling is over.
  374. * @retval None
  375. */
  376. void HAL_PWR_DisableSleepOnExit(void)
  377. {
  378. /* Clear SLEEPONEXIT bit of Cortex System Control Register */
  379. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
  380. }
  381. /**
  382. * @brief Enables CORTEX M4 SEVONPEND bit.
  383. * @note Sets SEVONPEND bit of SCR register. When this bit is set, this causes
  384. * WFE to wake up when an interrupt moves from inactive to pended.
  385. * @retval None
  386. */
  387. void HAL_PWR_EnableSEVOnPend(void)
  388. {
  389. /* Set SEVONPEND bit of Cortex System Control Register */
  390. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  391. }
  392. /**
  393. * @brief Disables CORTEX M4 SEVONPEND bit.
  394. * @note Clears SEVONPEND bit of SCR register. When this bit is set, this causes
  395. * WFE to wake up when an interrupt moves from inactive to pended.
  396. * @retval None
  397. */
  398. void HAL_PWR_DisableSEVOnPend(void)
  399. {
  400. /* Clear SEVONPEND bit of Cortex System Control Register */
  401. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
  402. }
  403. /**
  404. * @}
  405. */
  406. /**
  407. * @}
  408. */
  409. #endif /* HAL_PWR_MODULE_ENABLED */
  410. /**
  411. * @}
  412. */
  413. /**
  414. * @}
  415. */
  416. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/