1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_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 function
9   *           + Peripheral Control function
10   *
11   ******************************************************************************
12   * @attention
13   *
14   * Copyright (c) 2016 STMicroelectronics.
15   * All rights reserved.
16   *
17   * This software is licensed under terms that can be found in the LICENSE file
18   * in the root directory of this software component.
19   * If no LICENSE file comes with this software, it is provided AS-IS.
20   *
21   ******************************************************************************
22   */
23 
24 /* Includes ------------------------------------------------------------------*/
25 #include "stm32f0xx_hal.h"
26 
27 /** @addtogroup STM32F0xx_HAL_Driver
28   * @{
29   */
30 
31 /** @defgroup PWR PWR
32   * @brief PWR HAL module driver
33   * @{
34   */
35 
36 #ifdef HAL_PWR_MODULE_ENABLED
37 
38 /* Private typedef -----------------------------------------------------------*/
39 /* Private define ------------------------------------------------------------*/
40 /* Private macro -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private function prototypes -----------------------------------------------*/
43 /* Private functions ---------------------------------------------------------*/
44 
45 /** @defgroup PWR_Exported_Functions PWR Exported Functions
46   * @{
47   */
48 
49 /** @defgroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
50   *  @brief    Initialization and de-initialization functions
51   *
52 @verbatim
53  ===============================================================================
54               ##### Initialization and de-initialization functions #####
55  ===============================================================================
56     [..]
57       After reset, the backup domain (RTC registers, RTC backup data
58       registers) is protected against possible unwanted
59       write accesses.
60       To enable access to the RTC Domain and RTC registers, proceed as follows:
61         (+) Enable the Power Controller (PWR) APB1 interface clock using the
62             __HAL_RCC_PWR_CLK_ENABLE() macro.
63         (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
64 
65 @endverbatim
66   * @{
67   */
68 
69 /**
70   * @brief Deinitializes the PWR peripheral registers to their default reset values.
71   * @retval None
72   */
HAL_PWR_DeInit(void)73 void HAL_PWR_DeInit(void)
74 {
75   __HAL_RCC_PWR_FORCE_RESET();
76   __HAL_RCC_PWR_RELEASE_RESET();
77 }
78 
79 /**
80   * @brief Enables access to the backup domain (RTC registers, RTC
81   *         backup data registers when present).
82   * @note  If the HSE divided by 32 is used as the RTC clock, the
83   *         Backup Domain Access should be kept enabled.
84   * @retval None
85   */
HAL_PWR_EnableBkUpAccess(void)86 void HAL_PWR_EnableBkUpAccess(void)
87 {
88   PWR->CR |= (uint32_t)PWR_CR_DBP;
89 }
90 
91 /**
92   * @brief Disables access to the backup domain (RTC registers, RTC
93   *         backup data registers when present).
94   * @note  If the HSE divided by 32 is used as the RTC clock, the
95   *         Backup Domain Access should be kept enabled.
96   * @retval None
97   */
HAL_PWR_DisableBkUpAccess(void)98 void HAL_PWR_DisableBkUpAccess(void)
99 {
100   PWR->CR &= ~((uint32_t)PWR_CR_DBP);
101 }
102 
103 /**
104   * @}
105   */
106 
107 /** @defgroup PWR_Exported_Functions_Group2 Peripheral Control functions
108   *  @brief Low Power modes configuration functions
109   *
110 @verbatim
111 
112  ===============================================================================
113                  ##### Peripheral Control functions #####
114  ===============================================================================
115 
116     *** WakeUp pin configuration ***
117     ================================
118     [..]
119       (+) WakeUp pin is used to wakeup the system from Standby mode. This pin is
120           forced in input pull down configuration and is active on rising edges.
121       (+) There are two WakeUp pins, and up to eight Wakeup pins on STM32F07x & STM32F09x devices.
122          (++)WakeUp Pin 1 on PA.00.
123          (++)WakeUp Pin 2 on PC.13.
124          (++)WakeUp Pin 3 on PE.06.(STM32F07x/STM32F09x)
125          (++)WakeUp Pin 4 on PA.02.(STM32F07x/STM32F09x)
126          (++)WakeUp Pin 5 on PC.05.(STM32F07x/STM32F09x)
127          (++)WakeUp Pin 6 on PB.05.(STM32F07x/STM32F09x)
128          (++)WakeUp Pin 7 on PB.15.(STM32F07x/STM32F09x)
129          (++)WakeUp Pin 8 on PF.02.(STM32F07x/STM32F09x)
130 
131     *** Low Power modes configuration ***
132     =====================================
133     [..]
134       The devices feature 3 low-power modes:
135       (+) Sleep mode: Cortex-M0 core stopped, peripherals kept running.
136       (+) Stop mode: all clocks are stopped, regulator running, regulator
137           in low power mode
138       (+) Standby mode: 1.2V domain powered off (mode not available on STM32F0x8 devices).
139 
140    *** Sleep mode ***
141    ==================
142     [..]
143       (+) Entry:
144           The Sleep mode is entered by using the HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFx)
145               functions with
146           (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
147           (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
148 
149       (+) Exit:
150         (++) Any peripheral interrupt acknowledged by the nested vectored interrupt
151               controller (NVIC) can wake up the device from Sleep mode.
152 
153    *** Stop mode ***
154    =================
155     [..]
156       In Stop mode, all clocks in the 1.8V domain are stopped, the PLL, the HSI,
157       and the HSE RC oscillators are disabled. Internal SRAM and register contents
158       are preserved.
159       The voltage regulator can be configured either in normal or low-power mode.
160       To minimize the consumption.
161 
162       (+) Entry:
163           The Stop mode is entered using the HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI )
164              function with:
165           (++) Main regulator ON.
166           (++) Low Power regulator ON.
167           (++) PWR_STOPENTRY_WFI: enter STOP mode with WFI instruction
168           (++) PWR_STOPENTRY_WFE: enter STOP mode with WFE instruction
169       (+) Exit:
170           (++) Any EXTI Line (Internal or External) configured in Interrupt/Event mode.
171           (++) Some specific communication peripherals (CEC, USART, I2C) interrupts,
172                when programmed in wakeup mode (the peripheral must be
173                programmed in wakeup mode and the corresponding interrupt vector
174                must be enabled in the NVIC)
175 
176    *** Standby mode ***
177    ====================
178      [..]
179       The Standby mode allows to achieve the lowest power consumption. It is based
180       on the Cortex-M0 deep sleep mode, with the voltage regulator disabled.
181       The 1.8V domain is consequently powered off. The PLL, the HSI oscillator and
182       the HSE oscillator are also switched off. SRAM and register contents are lost
183       except for the RTC registers, RTC backup registers and Standby circuitry.
184       The voltage regulator is OFF.
185 
186       (+) Entry:
187           (++) The Standby mode is entered using the HAL_PWR_EnterSTANDBYMode() function.
188       (+) Exit:
189           (++) WKUP pin rising edge, RTC alarm (Alarm A), RTC wakeup,
190                tamper event, time-stamp event, external reset in NRST pin, IWDG reset.
191 
192    *** Auto-wakeup (AWU) from low-power mode ***
193    =============================================
194     [..]
195       The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC
196       Wakeup event, a tamper event, a time-stamp event, or a comparator event,
197       without depending on an external interrupt (Auto-wakeup mode).
198 
199     (+) RTC auto-wakeup (AWU) from the Stop and Standby modes
200 
201       (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to
202             configure the RTC to generate the RTC alarm using the HAL_RTC_SetAlarm_IT() function.
203 
204       (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it
205            is necessary to configure the RTC to detect the tamper or time stamp event using the
206            HAL_RTC_SetTimeStamp_IT() or HAL_RTC_SetTamper_IT() functions.
207 
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 
211     (+) Comparator auto-wakeup (AWU) from the Stop mode
212 
213       (++) To wake up from the Stop mode with a comparator wakeup event, it is necessary to:
214            (+++) Configure the EXTI Line associated with the comparator (example EXTI Line 22 for comparator 2)
215                  to be sensitive to to the selected edges (falling, rising or falling
216                  and rising) (Interrupt or Event modes) using the EXTI_Init() function.
217            (+++) Configure the comparator to generate the event.
218 @endverbatim
219   * @{
220   */
221 
222 /**
223   * @brief Enables the WakeUp PINx functionality.
224   * @param WakeUpPinx Specifies the Power Wake-Up pin to enable.
225   *         This parameter can be value of :
226   *           @ref PWREx_WakeUp_Pins
227   * @retval None
228   */
HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinx)229 void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinx)
230 {
231   /* Check the parameters */
232   assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
233   /* Enable the EWUPx pin */
234   SET_BIT(PWR->CSR, WakeUpPinx);
235 }
236 
237 /**
238   * @brief Disables the WakeUp PINx functionality.
239   * @param WakeUpPinx Specifies the Power Wake-Up pin to disable.
240   *         This parameter can be values of :
241   *           @ref PWREx_WakeUp_Pins
242   * @retval None
243   */
HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx)244 void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx)
245 {
246   /* Check the parameters */
247   assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
248   /* Disable the EWUPx pin */
249   CLEAR_BIT(PWR->CSR, WakeUpPinx);
250 }
251 
252 /**
253   * @brief Enters Sleep mode.
254   * @note  In Sleep mode, all I/O pins keep the same state as in Run mode.
255   * @param Regulator Specifies the regulator state in SLEEP mode.
256   *           On STM32F0 devices, this parameter is a dummy value and it is ignored
257   *           as regulator can't be modified in this mode. Parameter is kept for platform
258   *           compatibility.
259   * @param SLEEPEntry Specifies if SLEEP mode is entered with WFI or WFE instruction.
260   *           When WFI entry is used, tick interrupt have to be disabled if not desired as
261   *           the interrupt wake up source.
262   *           This parameter can be one of the following values:
263   *            @arg PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
264   *            @arg PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
265   * @retval None
266   */
HAL_PWR_EnterSLEEPMode(uint32_t Regulator,uint8_t SLEEPEntry)267 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
268 {
269   /* Check the parameters */
270   assert_param(IS_PWR_REGULATOR(Regulator));
271   assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
272 
273   /* Clear SLEEPDEEP bit of Cortex System Control Register */
274   SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
275 
276   /* Select SLEEP mode entry -------------------------------------------------*/
277   if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
278   {
279     /* Request Wait For Interrupt */
280     __WFI();
281   }
282   else
283   {
284     /* Request Wait For Event */
285     __SEV();
286     __WFE();
287     __WFE();
288   }
289 }
290 
291 /**
292   * @brief Enters STOP mode.
293   * @note  In Stop mode, all I/O pins keep the same state as in Run mode.
294   * @note  When exiting Stop mode by issuing an interrupt or a wakeup event,
295   *         the HSI RC oscillator is selected as system clock.
296   * @note  When the voltage regulator operates in low power mode, an additional
297   *         startup delay is incurred when waking up from Stop mode.
298   *         By keeping the internal regulator ON during Stop mode, the consumption
299   *         is higher although the startup time is reduced.
300   * @param Regulator Specifies the regulator state in STOP mode.
301   *          This parameter can be one of the following values:
302   *            @arg PWR_MAINREGULATOR_ON: STOP mode with regulator ON
303   *            @arg PWR_LOWPOWERREGULATOR_ON: STOP mode with low power regulator ON
304   * @param STOPEntry specifies if STOP mode in entered with WFI or WFE instruction.
305   *          This parameter can be one of the following values:
306   *            @arg PWR_STOPENTRY_WFI:Enter STOP mode with WFI instruction
307   *            @arg PWR_STOPENTRY_WFE: Enter STOP mode with WFE instruction
308   * @retval None
309   */
HAL_PWR_EnterSTOPMode(uint32_t Regulator,uint8_t STOPEntry)310 void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
311 {
312   uint32_t tmpreg = 0;
313 
314   /* Check the parameters */
315   assert_param(IS_PWR_REGULATOR(Regulator));
316   assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
317 
318   /* Select the regulator state in STOP mode ---------------------------------*/
319   tmpreg = PWR->CR;
320 
321   /* Clear PDDS and LPDS bits */
322   tmpreg &= (uint32_t)~(PWR_CR_PDDS | PWR_CR_LPDS);
323 
324   /* Set LPDS bit according to Regulator value */
325   tmpreg |= Regulator;
326 
327   /* Store the new value */
328   PWR->CR = tmpreg;
329 
330   /* Set SLEEPDEEP bit of Cortex System Control Register */
331   SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
332 
333   /* Select STOP mode entry --------------------------------------------------*/
334   if(STOPEntry == PWR_STOPENTRY_WFI)
335   {
336     /* Request Wait For Interrupt */
337     __WFI();
338   }
339   else
340   {
341     /* Request Wait For Event */
342     __SEV();
343     __WFE();
344     __WFE();
345   }
346 
347   /* Reset SLEEPDEEP bit of Cortex System Control Register */
348   SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
349 }
350 
351 /**
352   * @brief Enters STANDBY mode.
353   * @note  In Standby mode, all I/O pins are high impedance except for:
354   *          - Reset pad (still available)
355   *          - RTC alternate function pins if configured for tamper, time-stamp, RTC
356   *            Alarm out, or RTC clock calibration out.
357   *          - WKUP pins if enabled.
358   *            STM32F0x8 devices, the Stop mode is available, but it is
359   *            aningless to distinguish between voltage regulator in Low power
360   *            mode and voltage regulator in Run mode because the regulator
361   *            not used and the core is supplied directly from an external source.
362   *            Consequently, the Standby mode is not available on those devices.
363   * @retval None
364   */
HAL_PWR_EnterSTANDBYMode(void)365 void HAL_PWR_EnterSTANDBYMode(void)
366 {
367   /* Select STANDBY mode */
368   PWR->CR |= (uint32_t)PWR_CR_PDDS;
369 
370   /* Set SLEEPDEEP bit of Cortex System Control Register */
371   SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
372 
373   /* This option is used to ensure that store operations are completed */
374 #if defined ( __CC_ARM)
375   __force_stores();
376 #endif
377   /* Request Wait For Interrupt */
378   __WFI();
379 }
380 
381 /**
382   * @brief Indicates Sleep-On-Exit when returning from Handler mode to Thread mode.
383   * @note Set SLEEPONEXIT bit of SCR register. When this bit is set, the processor
384   *       re-enters SLEEP mode when an interruption handling is over.
385   *       Setting this bit is useful when the processor is expected to run only on
386   *       interruptions handling.
387   * @retval None
388   */
HAL_PWR_EnableSleepOnExit(void)389 void HAL_PWR_EnableSleepOnExit(void)
390 {
391   /* Set SLEEPONEXIT bit of Cortex System Control Register */
392   SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
393 }
394 
395 
396 /**
397   * @brief Disables Sleep-On-Exit feature when returning from Handler mode to Thread mode.
398   * @note Clears SLEEPONEXIT bit of SCR register. When this bit is set, the processor
399   *       re-enters SLEEP mode when an interruption handling is over.
400   * @retval None
401   */
HAL_PWR_DisableSleepOnExit(void)402 void HAL_PWR_DisableSleepOnExit(void)
403 {
404   /* Clear SLEEPONEXIT bit of Cortex System Control Register */
405   CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPONEXIT_Msk));
406 }
407 
408 
409 
410 /**
411   * @brief Enables CORTEX M4 SEVONPEND bit.
412   * @note Sets SEVONPEND bit of SCR register. When this bit is set, this causes
413   *       WFE to wake up when an interrupt moves from inactive to pended.
414   * @retval None
415   */
HAL_PWR_EnableSEVOnPend(void)416 void HAL_PWR_EnableSEVOnPend(void)
417 {
418   /* Set SEVONPEND bit of Cortex System Control Register */
419   SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
420 }
421 
422 
423 /**
424   * @brief Disables CORTEX M4 SEVONPEND bit.
425   * @note Clears SEVONPEND bit of SCR register. When this bit is set, this causes
426   *       WFE to wake up when an interrupt moves from inactive to pended.
427   * @retval None
428   */
HAL_PWR_DisableSEVOnPend(void)429 void HAL_PWR_DisableSEVOnPend(void)
430 {
431   /* Clear SEVONPEND bit of Cortex System Control Register */
432   CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
433 }
434 
435 /**
436   * @}
437   */
438 
439 /**
440   * @}
441   */
442 
443 #endif /* HAL_PWR_MODULE_ENABLED */
444 /**
445   * @}
446   */
447 
448 /**
449   * @}
450   */
451