2 ******************************************************************************
3 * @file stm32f3xx_hal_pwr.c
4 * @author MCD Application Team
7 * @brief PWR HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Power Controller (PWR) peripheral:
11 * + Initialization/de-initialization functions
12 * + Peripheral Control functions
15 ******************************************************************************
18 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
20 * Redistribution and use in source and binary forms, with or without modification,
21 * are permitted provided that the following conditions are met:
22 * 1. Redistributions of source code must retain the above copyright notice,
23 * this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright notice,
25 * this list of conditions and the following disclaimer in the documentation
26 * and/or other materials provided with the distribution.
27 * 3. Neither the name of STMicroelectronics nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
39 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 ******************************************************************************
45 /* Includes ------------------------------------------------------------------*/
46 #include "stm32f3xx_hal.h"
48 /** @addtogroup STM32F3xx_HAL_Driver
52 /** @defgroup PWR PWR HAL module driver
53 * @brief PWR HAL module driver
57 #ifdef HAL_PWR_MODULE_ENABLED
59 /* Private typedef -----------------------------------------------------------*/
60 /* Private define ------------------------------------------------------------*/
61 /* Private macro -------------------------------------------------------------*/
62 /* Private variables ---------------------------------------------------------*/
63 /* Private function prototypes -----------------------------------------------*/
64 /* Private functions ---------------------------------------------------------*/
66 /** @defgroup PWR_Exported_Functions PWR Exported Functions
70 /** @defgroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions
71 * @brief Initialization and de-initialization functions
74 ===============================================================================
75 ##### Initialization/de-initialization functions #####
76 ===============================================================================
78 After reset, the backup domain (RTC registers, RTC backup data
79 registers and backup SRAM) is protected against possible unwanted
81 To enable access to the RTC Domain and RTC registers, proceed as follows:
82 (+) Enable the Power Controller (PWR) APB1 interface clock using the
83 __PWR_CLK_ENABLE() macro.
84 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
91 * @brief Deinitializes the HAL PWR peripheral registers to their default reset values.
94 void HAL_PWR_DeInit(void)
97 __PWR_RELEASE_RESET();
101 * @brief Enables access to the backup domain (RTC registers, RTC
102 * backup data registers and backup SRAM).
103 * @note If the HSE divided by 32 is used as the RTC clock, the
104 * Backup Domain Access should be kept enabled.
107 void HAL_PWR_EnableBkUpAccess(void)
109 *(__IO uint32_t *) CR_DBP_BB = (uint32_t)ENABLE;
113 * @brief Disables access to the backup domain (RTC registers, RTC
114 * backup data registers and backup SRAM).
115 * @note If the HSE divided by 32 is used as the RTC clock, the
116 * Backup Domain Access should be kept enabled.
119 void HAL_PWR_DisableBkUpAccess(void)
121 *(__IO uint32_t *) CR_DBP_BB = (uint32_t)DISABLE;
128 /** @defgroup PWR_Exported_Functions_Group2 Peripheral Control functions
129 * @brief Low Power modes configuration functions
133 ===============================================================================
134 ##### Peripheral Control functions #####
135 ===============================================================================
137 *** WakeUp pin configuration ***
138 ================================
139 (+) WakeUp pin is used to wakeup the system from Standby mode. This pin is
140 forced in input pull down configuration and is active on rising edges.
141 (+) There are up to three WakeUp pins:
142 WakeUp Pin 1 on PA.00.
143 WakeUp Pin 2 on PC.13 (STM32F303xC, STM32F303xE only).
144 WakeUp Pin 3 on PE.06.
146 *** Main and Backup Regulators configuration ***
147 ================================================
149 (+) When the backup domain is supplied by VDD (analog switch connected to VDD)
150 the backup SRAM is powered from VDD which replaces the VBAT power supply to
153 (+) The backup SRAM is not mass erased by an tamper event. It is read
154 protected to prevent confidential data, such as cryptographic private
155 key, from being accessed. The backup SRAM can be erased only through
156 the Flash interface when a protection level change from level 1 to
157 level 0 is requested.
158 -@- Refer to the description of Read protection (RDP) in the Flash
161 Refer to the datasheets for more details.
163 *** Low Power modes configuration ***
164 =====================================
166 The devices feature 3 low-power modes:
167 (+) Sleep mode: Cortex-M4 core stopped, peripherals kept running.
168 (+) Stop mode: all clocks are stopped, regulator running, regulator
170 (+) Standby mode: 1.2V domain powered off (mode not available on STM32F3x8 devices).
176 The Sleep mode is entered by using the HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFx)
178 (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
179 (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
182 (++) Any peripheral interrupt acknowledged by the nested vectored interrupt
183 controller (NVIC) can wake up the device from Sleep mode.
188 In Stop mode, all clocks in the 1.8V domain are stopped, the PLL, the HSI,
189 and the HSE RC oscillators are disabled. Internal SRAM and register contents
191 The voltage regulator can be configured either in normal or low-power mode.
192 To minimize the consumption.
195 The Stop mode is entered using the HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI )
197 (++) Main regulator ON.
198 (++) Low Power regulator ON.
199 (++) PWR_STOPENTRY_WFI: enter STOP mode with WFI instruction
200 (++) PWR_STOPENTRY_WFE: enter STOP mode with WFE instruction
202 (++) Any EXTI Line (Internal or External) configured in Interrupt/Event mode.
203 (++) Some specific communication peripherals (CEC, USART, I2C) interrupts,
204 when programmed in wakeup mode (the peripheral must be
205 programmed in wakeup mode and the corresponding interrupt vector
206 must be enabled in the NVIC)
211 The Standby mode allows to achieve the lowest power consumption. It is based
212 on the Cortex-M4 deep sleep mode, with the voltage regulator disabled.
213 The 1.8V domain is consequently powered off. The PLL, the HSI oscillator and
214 the HSE oscillator are also switched off. SRAM and register contents are lost
215 except for the RTC registers, RTC backup registers, backup SRAM and Standby
217 The voltage regulator is OFF.
220 (++) The Standby mode is entered using the HAL_PWR_EnterSTANDBYMode() function.
222 (++) WKUP pin rising edge, RTC alarm (Alarm A and Alarm B), RTC wakeup,
223 tamper event, time-stamp event, external reset in NRST pin, IWDG reset.
225 *** Auto-wakeup (AWU) from low-power mode ***
226 =============================================
228 The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC
229 Wakeup event, a tamper event, a time-stamp event, or a comparator event,
230 without depending on an external interrupt (Auto-wakeup mode).
232 (+) RTC auto-wakeup (AWU) from the Stop and Standby modes
234 (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to
235 configure the RTC to generate the RTC alarm using the HAL_RTC_SetAlarm_IT() function.
237 (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it
238 is necessary to configure the RTC to detect the tamper or time stamp event using the
239 HAL_RTC_SetTimeStamp_IT() or HAL_RTC_SetTamper_IT() functions.
241 (++) To wake up from the Stop mode with an RTC WakeUp event, it is necessary to
242 configure the RTC to generate the RTC WakeUp event using the HAL_RTC_SetWakeUpTimer_IT() function.
244 (+) Comparator auto-wakeup (AWU) from the Stop mode
246 (++) To wake up from the Stop mode with a comparator wakeup event, it is necessary to:
247 (+++) Configure the EXTI Line associated with the comparator (example EXTI Line 22 for comparator 2)
248 to be sensitive to to the selected edges (falling, rising or falling
249 and rising) (Interrupt or Event modes) using the EXTI_Init() function.
250 (+++) Configure the comparator to generate the event.
256 * @brief Enables the WakeUp PINx functionality.
257 * @param WakeUpPinx: Specifies the Power Wake-Up pin to enable.
258 * This parameter can be one of the following values:
259 * @arg PWR_WAKEUP_PIN1, PWR_WAKEUP_PIN2, PWR_WAKEUP_PIN3
262 void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinx)
264 __IO uint32_t tmp = 0;
266 /* Check the parameters */
267 assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
268 tmp = CSR_EWUP1_BB + (WakeUpPinx << 2);
269 *(__IO uint32_t *) (tmp) = (uint32_t)ENABLE;
273 * @brief Disables the WakeUp PINx functionality.
274 * @param WakeUpPinx: Specifies the Power Wake-Up pin to disable.
275 * This parameter can be one of the following values:
276 * @arg PWR_WAKEUP_PIN1, PWR_WAKEUP_PIN2, PWR_WAKEUP_PIN3
279 void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx)
281 __IO uint32_t tmp = 0;
283 /* Check the parameters */
284 assert_param(IS_PWR_WAKEUP_PIN(WakeUpPinx));
285 tmp = CSR_EWUP1_BB + (WakeUpPinx << 2);
286 *(__IO uint32_t *) (tmp) = (uint32_t)DISABLE;
290 * @brief Enters Sleep mode.
291 * @note In Sleep mode, all I/O pins keep the same state as in Run mode.
292 * @param Regulator: Specifies the regulator state in SLEEP mode.
293 * This parameter can be one of the following values:
294 * @arg PWR_MAINREGULATOR_ON: SLEEP mode with regulator ON
295 * @arg PWR_LOWPOWERREGULATOR_ON: SLEEP mode with low power regulator ON
296 * @param SLEEPEntry: Specifies if SLEEP mode is entered with WFI or WFE instruction.
297 * When WFI entry is used, tick interrupt have to be disabled if not desired as
298 * the interrupt wake up source.
299 * This parameter can be one of the following values:
300 * @arg PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
301 * @arg PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
304 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
308 /* Check the parameters */
309 assert_param(IS_PWR_REGULATOR(Regulator));
310 assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
312 /* Select the regulator state in SLEEP mode ---------------------------------*/
315 /* Clear PDDS and LPDS bits */
316 tmpreg &= (uint32_t)~(PWR_CR_PDDS | PWR_CR_LPDS);
318 /* Set LPDS bit according to Regulator value */
321 /* Store the new value */
324 /* Clear SLEEPDEEP bit of Cortex System Control Register */
325 SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
327 /* Select SLEEP mode entry -------------------------------------------------*/
328 if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
330 /* Request Wait For Interrupt */
335 /* Request Wait For Event */
343 * @brief Enters STOP mode.
344 * @note In Stop mode, all I/O pins keep the same state as in Run mode.
345 * @note When exiting Stop mode by issuing an interrupt or a wakeup event,
346 * the HSI RC oscillator is selected as system clock.
347 * @note When the voltage regulator operates in low power mode, an additional
348 * startup delay is incurred when waking up from Stop mode.
349 * By keeping the internal regulator ON during Stop mode, the consumption
350 * is higher although the startup time is reduced.
351 * @param Regulator: Specifies the regulator state in STOP mode.
352 * This parameter can be one of the following values:
353 * @arg PWR_MAINREGULATOR_ON: STOP mode with regulator ON
354 * @arg PWR_LOWPOWERREGULATOR_ON: STOP mode with low power regulator ON
355 * @param STOPEntry: specifies if STOP mode in entered with WFI or WFE instruction.
356 * This parameter can be one of the following values:
357 * @arg PWR_STOPENTRY_WFI:Enter STOP mode with WFI instruction
358 * @arg PWR_STOPENTRY_WFE: Enter STOP mode with WFE instruction
361 void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
365 /* Check the parameters */
366 assert_param(IS_PWR_REGULATOR(Regulator));
367 assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
369 /* Select the regulator state in STOP mode ---------------------------------*/
372 /* Clear PDDS and LPDS bits */
373 tmpreg &= (uint32_t)~(PWR_CR_PDDS | PWR_CR_LPDS);
375 /* Set LPDS bit according to Regulator value */
378 /* Store the new value */
381 /* Set SLEEPDEEP bit of Cortex System Control Register */
382 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
384 /* Select STOP mode entry --------------------------------------------------*/
385 if(STOPEntry == PWR_STOPENTRY_WFI)
387 /* Request Wait For Interrupt */
392 /* Request Wait For Event */
398 /* Reset SLEEPDEEP bit of Cortex System Control Register */
399 SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
403 * @brief Enters STANDBY mode.
404 * @note In Standby mode, all I/O pins are high impedance except for:
405 * - Reset pad (still available)
406 * - RTC_AF1 pin (PC13) if configured for tamper, time-stamp, RTC
407 * Alarm out, or RTC clock calibration out.
408 * - RTC_AF2 pin (PI8) if configured for tamper or time-stamp.
409 * - WKUP pin 1 (PA0) if enabled.
412 void HAL_PWR_EnterSTANDBYMode(void)
414 /* Select STANDBY mode */
415 PWR->CR |= PWR_CR_PDDS;
417 /* Set SLEEPDEEP bit of Cortex System Control Register */
418 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
420 /* This option is used to ensure that store operations are completed */
421 #if defined ( __CC_ARM)
424 /* Request Wait For Interrupt */
436 #endif /* HAL_PWR_MODULE_ENABLED */
445 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/