]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3XX/stm32f30x_pwr.c
2 ******************************************************************************
3 * @file stm32f30x_pwr.c
4 * @author MCD Application Team
6 * @date 27-February-2014
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Power Controller (PWR) peripheral:
9 * + Backup Domain Access
11 * + WakeUp pins configuration
12 * + Low Power modes configuration
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 "stm32f30x_pwr.h"
47 #include "stm32f30x_rcc.h"
49 /** @addtogroup STM32F30x_StdPeriph_Driver
54 * @brief PWR driver modules
58 /* Private typedef -----------------------------------------------------------*/
59 /* Private define ------------------------------------------------------------*/
60 /* --------- PWR registers bit address in the alias region ---------- */
61 #define PWR_OFFSET (PWR_BASE - PERIPH_BASE)
63 /* --- CR Register ---*/
65 /* Alias word address of DBP bit */
66 #define CR_OFFSET (PWR_OFFSET + 0x00)
67 #define DBP_BitNumber 0x08
68 #define CR_DBP_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (DBP_BitNumber * 4))
70 /* Alias word address of PVDE bit */
71 #define PVDE_BitNumber 0x04
72 #define CR_PVDE_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PVDE_BitNumber * 4))
74 /* ------------------ PWR registers bit mask ------------------------ */
76 /* CR register bit mask */
77 #define CR_DS_MASK ((uint32_t)0xFFFFFFFC)
78 #define CR_PLS_MASK ((uint32_t)0xFFFFFF1F)
80 /* Private macro -------------------------------------------------------------*/
81 /* Private variables ---------------------------------------------------------*/
82 /* Private function prototypes -----------------------------------------------*/
83 /* Private functions ---------------------------------------------------------*/
85 /** @defgroup PWR_Private_Functions
89 /** @defgroup PWR_Group1 Backup Domain Access function
90 * @brief Backup Domain Access function
93 ==============================================================================
94 ##### Backup Domain Access function #####
95 ==============================================================================
97 [..] After reset, the Backup Domain Registers (RCC BDCR Register, RTC registers
98 and RTC backup registers) are protected against possible stray write accesses.
99 [..] To enable access to Backup domain use the PWR_BackupAccessCmd(ENABLE) function.
106 * @brief Deinitializes the PWR peripheral registers to their default reset values.
110 void PWR_DeInit(void)
112 RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR
, ENABLE
);
113 RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR
, DISABLE
);
117 * @brief Enables or disables access to the RTC and backup registers.
118 * @note If the HSE divided by 32 is used as the RTC clock, the
119 * Backup Domain Access should be kept enabled.
120 * @param NewState: new state of the access to the RTC and backup registers.
121 * This parameter can be: ENABLE or DISABLE.
124 void PWR_BackupAccessCmd(FunctionalState NewState
)
126 /* Check the parameters */
127 assert_param(IS_FUNCTIONAL_STATE(NewState
));
128 *(__IO
uint32_t *) CR_DBP_BB
= (uint32_t)NewState
;
135 /** @defgroup PWR_Group2 PVD configuration functions
136 * @brief PVD configuration functions
139 ===============================================================================
140 ##### PVD configuration functions #####
141 ==============================================================================
143 (+) The PVD is used to monitor the VDD power supply by comparing it to a threshold
144 selected by the PVD Level (PLS[2:0] bits in the PWR_CR).
145 (+) A PVDO flag is available to indicate if VDD/VDDA is higher or lower than the
146 PVD threshold. This event is internally connected to the EXTI line16
147 and can generate an interrupt if enabled through the EXTI registers.
148 (+) The PVD is stopped in Standby mode.
155 * @brief Configures the voltage threshold detected by the Power Voltage Detector(PVD).
156 * @param PWR_PVDLevel: specifies the PVD detection level
157 * This parameter can be one of the following values:
158 * @arg PWR_PVDLevel_0: PVD detection level set to 2.18V
159 * @arg PWR_PVDLevel_1: PVD detection level set to 2.28V
160 * @arg PWR_PVDLevel_2: PVD detection level set to 2.38V
161 * @arg PWR_PVDLevel_3: PVD detection level set to 2.48V
162 * @arg PWR_PVDLevel_4: PVD detection level set to 2.58V
163 * @arg PWR_PVDLevel_5: PVD detection level set to 2.68V
164 * @arg PWR_PVDLevel_6: PVD detection level set to 2.78V
165 * @arg PWR_PVDLevel_7: PVD detection level set to 2.88V
168 void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel
)
172 /* Check the parameters */
173 assert_param(IS_PWR_PVD_LEVEL(PWR_PVDLevel
));
177 /* Clear PLS[7:5] bits */
178 tmpreg
&= CR_PLS_MASK
;
180 /* Set PLS[7:5] bits according to PWR_PVDLevel value */
181 tmpreg
|= PWR_PVDLevel
;
183 /* Store the new value */
188 * @brief Enables or disables the Power Voltage Detector(PVD).
189 * @param NewState: new state of the PVD.
190 * This parameter can be: ENABLE or DISABLE.
193 void PWR_PVDCmd(FunctionalState NewState
)
195 /* Check the parameters */
196 assert_param(IS_FUNCTIONAL_STATE(NewState
));
197 *(__IO
uint32_t *) CR_PVDE_BB
= (uint32_t)NewState
;
204 /** @defgroup PWR_Group3 WakeUp pins configuration functions
205 * @brief WakeUp pins configuration functions
208 ===============================================================================
209 ##### WakeUp pins configuration functions #####
210 ===============================================================================
212 (+) WakeUp pins are used to wakeup the system from Standby mode. These pins are
213 forced in input pull down configuration and are active on rising edges.
214 (+) There are three WakeUp pins: WakeUp Pin 1 on PA.00, WakeUp Pin 2 on PC.13 and
215 WakeUp Pin 3 on PE.06.
222 * @brief Enables or disables the WakeUp Pin functionality.
223 * @param PWR_WakeUpPin: specifies the WakeUpPin.
224 * This parameter can be: PWR_WakeUpPin_1, PWR_WakeUpPin_2 or PWR_WakeUpPin_3.
225 * @param NewState: new state of the WakeUp Pin functionality.
226 * This parameter can be: ENABLE or DISABLE.
229 void PWR_WakeUpPinCmd(uint32_t PWR_WakeUpPin
, FunctionalState NewState
)
231 /* Check the parameters */
232 assert_param(IS_PWR_WAKEUP_PIN(PWR_WakeUpPin
));
233 assert_param(IS_FUNCTIONAL_STATE(NewState
));
235 if (NewState
!= DISABLE
)
237 /* Enable the EWUPx pin */
238 PWR
->CSR
|= PWR_WakeUpPin
;
242 /* Disable the EWUPx pin */
243 PWR
->CSR
&= ~PWR_WakeUpPin
;
252 /** @defgroup PWR_Group4 Low Power modes configuration functions
253 * @brief Low Power modes configuration functions
256 ===============================================================================
257 ##### Low Power modes configuration functions #####
258 ==============================================================================
260 [..] The devices feature three low-power modes:
261 (+) Sleep mode: Cortex-M4 core stopped, peripherals kept running.
262 (+) Stop mode: all clocks are stopped, regulator running, regulator in low power mode
263 (+) Standby mode: VCORE domain powered off
269 (++) The Sleep mode is entered by executing the WFE() or WFI() instructions.
271 (++) Any peripheral interrupt acknowledged by the nested vectored interrupt
272 controller (NVIC) can wake up the device from Sleep mode.
276 [..] In Stop mode, all clocks in the VCORE domain are stopped, the PLL, the HSI,
277 and the HSE RC oscillators are disabled. Internal SRAM and register
278 contents are preserved.
279 The voltage regulator can be configured either in normal or low-power mode.
282 (++) The Stop mode is entered using the PWR_EnterSTOPMode(PWR_Regulator_LowPower,)
283 function with regulator in LowPower or with Regulator ON.
285 (++) Any EXTI Line (Internal or External) configured in Interrupt/Event mode
286 or any internal IPs (I2C or UASRT) wakeup event.
290 [..] The Standby mode allows to achieve the lowest power consumption. It is based
291 on the Cortex-M4 deepsleep mode, with the voltage regulator disabled.
292 The VCORE domain is consequently powered off. The PLL, the HSI, and the HSE
293 oscillator are also switched off. SRAM and register
294 contents are lost except for the Backup domain (RTC registers, RTC backup
295 registers and Standby circuitry).
297 [..] The voltage regulator is OFF.
300 (++) The Standby mode is entered using the PWR_EnterSTANDBYMode() function.
302 (++) WKUP pin rising edge, RTC alarm (Alarm A and Alarm B), RTC wakeup,
303 tamper event, time-stamp event, external reset in NRST pin, IWDG reset.
305 *** Auto-wakeup (AWU) from low-power mode ***
306 =============================================
307 [..] The MCU can be woken up from low-power mode by an RTC Alarm event, a tamper
308 event, a time-stamp event, or a comparator event, without depending on an
309 external interrupt (Auto-wakeup mode).
311 (+) RTC auto-wakeup (AWU) from the Stop mode
312 (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to:
313 (+++) Configure the EXTI Line 17 to be sensitive to rising edges (Interrupt
314 or Event modes) using the EXTI_Init() function.
315 (+++) Enable the RTC Alarm Interrupt using the RTC_ITConfig() function
316 (+++) Configure the RTC to generate the RTC alarm using the RTC_SetAlarm()
317 and RTC_AlarmCmd() functions.
318 (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it
320 (+++) Configure the EXTI Line 19 to be sensitive to rising edges (Interrupt
321 or Event modes) using the EXTI_Init() function.
322 (+++) Enable the RTC Tamper or time stamp Interrupt using the RTC_ITConfig()
324 (+++) Configure the RTC to detect the tamper or time stamp event using the
325 RTC_TimeStampConfig(), RTC_TamperTriggerConfig() and RTC_TamperCmd()
328 (+) RTC auto-wakeup (AWU) from the Standby mode
329 (++) To wake up from the Standby mode with an RTC alarm event, it is necessary to:
330 (+++) Enable the RTC Alarm Interrupt using the RTC_ITConfig() function.
331 (+++) Configure the RTC to generate the RTC alarm using the RTC_SetAlarm()
332 and RTC_AlarmCmd() functions.
333 (++) To wake up from the Standby mode with an RTC Tamper or time stamp event, it
335 (+++) Enable the RTC Tamper or time stamp Interrupt using the RTC_ITConfig()
337 (+++) Configure the RTC to detect the tamper or time stamp event using the
338 RTC_TimeStampConfig(), RTC_TamperTriggerConfig() and RTC_TamperCmd()
341 (+) Comparator auto-wakeup (AWU) from the Stop mode
342 (++) To wake up from the Stop mode with a comparator wakeup event, it is necessary to:
343 (+++) Configure the correspondant comparator EXTI Line to be sensitive to
344 the selected edges (falling, rising or falling and rising)
345 (Interrupt or Event modes) using the EXTI_Init() function.
346 (+++) Configure the comparator to generate the event.
353 * @brief Enters Sleep mode.
354 * @note In Sleep mode, all I/O pins keep the same state as in Run mode.
355 * @param PWR_SLEEPEntry: specifies if SLEEP mode in entered with WFI or WFE instruction.
356 * This parameter can be one of the following values:
357 * @arg PWR_SLEEPEntry_WFI: enter SLEEP mode with WFI instruction
358 * @arg PWR_SLEEPEntry_WFE: enter SLEEP mode with WFE instruction
361 void PWR_EnterSleepMode(uint8_t PWR_SLEEPEntry
)
363 /* Check the parameters */
364 assert_param(IS_PWR_SLEEP_ENTRY(PWR_SLEEPEntry
));
366 /* Clear SLEEPDEEP bit of Cortex System Control Register */
367 SCB
->SCR
&= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk
);
369 /* Select SLEEP mode entry -------------------------------------------------*/
370 if(PWR_SLEEPEntry
== PWR_SLEEPEntry_WFI
)
372 /* Request Wait For Interrupt */
377 /* Request Wait For Event */
383 * @brief Enters STOP mode.
384 * @note In Stop mode, all I/O pins keep the same state as in Run mode.
385 * @note When exiting Stop mode by issuing an interrupt or a wakeup event,
386 * the HSI RC oscillator is selected as system clock.
387 * @note When the voltage regulator operates in low power mode, an additional
388 * startup delay is incurred when waking up from Stop mode.
389 * By keeping the internal regulator ON during Stop mode, the consumption
390 * is higher although the startup time is reduced.
391 * @param PWR_Regulator: specifies the regulator state in STOP mode.
392 * This parameter can be one of the following values:
393 * @arg PWR_Regulator_ON: STOP mode with regulator ON
394 * @arg PWR_Regulator_LowPower: STOP mode with regulator in low power mode
395 * @param PWR_STOPEntry: specifies if STOP mode in entered with WFI or WFE instruction.
396 * This parameter can be one of the following values:
397 * @arg PWR_STOPEntry_WFI: enter STOP mode with WFI instruction
398 * @arg PWR_STOPEntry_WFE: enter STOP mode with WFE instruction
401 void PWR_EnterSTOPMode(uint32_t PWR_Regulator
, uint8_t PWR_STOPEntry
)
405 /* Check the parameters */
406 assert_param(IS_PWR_REGULATOR(PWR_Regulator
));
407 assert_param(IS_PWR_STOP_ENTRY(PWR_STOPEntry
));
409 /* Select the regulator state in STOP mode ---------------------------------*/
411 /* Clear PDDS and LPDSR bits */
412 tmpreg
&= CR_DS_MASK
;
414 /* Set LPDSR bit according to PWR_Regulator value */
415 tmpreg
|= PWR_Regulator
;
417 /* Store the new value */
420 /* Set SLEEPDEEP bit of Cortex System Control Register */
421 SCB
->SCR
|= SCB_SCR_SLEEPDEEP_Msk
;
423 /* Select STOP mode entry --------------------------------------------------*/
424 if(PWR_STOPEntry
== PWR_STOPEntry_WFI
)
426 /* Request Wait For Interrupt */
431 /* Request Wait For Event */
434 /* Reset SLEEPDEEP bit of Cortex System Control Register */
435 SCB
->SCR
&= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk
);
439 * @brief Enters STANDBY mode.
440 * @note In Standby mode, all I/O pins are high impedance except for:
441 * @note Reset pad (still available)
442 * @note RTC_AF1 pin (PC13) if configured for Wakeup pin 2 (WKUP2), tamper,
443 * time-stamp, RTC Alarm out, or RTC clock calibration out.
444 * @note WKUP pin 1 (PA0) and WKUP pin 3 (PE6), if enabled.
448 void PWR_EnterSTANDBYMode(void)
450 /* Clear Wakeup flag */
451 PWR
->CR
|= PWR_CR_CWUF
;
453 /* Select STANDBY mode */
454 PWR
->CR
|= PWR_CR_PDDS
;
456 /* Set SLEEPDEEP bit of Cortex System Control Register */
457 SCB
->SCR
|= SCB_SCR_SLEEPDEEP_Msk
;
459 /* This option is used to ensure that store operations are completed */
460 #if defined ( __CC_ARM )
463 /* Request Wait For Interrupt */
471 /** @defgroup PWR_Group5 Flags management functions
472 * @brief Flags management functions
475 ===============================================================================
476 ##### Flags management functions #####
477 ===============================================================================
484 * @brief Checks whether the specified PWR flag is set or not.
485 * @param PWR_FLAG: specifies the flag to check.
486 * This parameter can be one of the following values:
487 * @arg PWR_FLAG_WU: Wake Up flag. This flag indicates that a wakeup event
488 * was received from the WKUP pin or from the RTC alarm (Alarm A or Alarm B),
489 * RTC Tamper event, RTC TimeStamp event or RTC Wakeup.
490 * @arg PWR_FLAG_SB: StandBy flag. This flag indicates that the system was
491 * resumed from StandBy mode.
492 * @arg PWR_FLAG_PVDO: PVD Output. This flag is valid only if PVD is enabled
493 * by the PWR_PVDCmd() function.
494 * @arg PWR_FLAG_VREFINTRDY: Internal Voltage Reference Ready flag. This
495 * flag indicates the state of the internal voltage reference, VREFINT.
496 * @retval The new state of PWR_FLAG (SET or RESET).
498 FlagStatus
PWR_GetFlagStatus(uint32_t PWR_FLAG
)
500 FlagStatus bitstatus
= RESET
;
501 /* Check the parameters */
502 assert_param(IS_PWR_GET_FLAG(PWR_FLAG
));
504 if ((PWR
->CSR
& PWR_FLAG
) != (uint32_t)RESET
)
512 /* Return the flag status */
517 * @brief Clears the PWR's pending flags.
518 * @param PWR_FLAG: specifies the flag to clear.
519 * This parameter can be one of the following values:
520 * @arg PWR_FLAG_WU: Wake Up flag
521 * @arg PWR_FLAG_SB: StandBy flag
524 void PWR_ClearFlag(uint32_t PWR_FLAG
)
526 /* Check the parameters */
527 assert_param(IS_PWR_CLEAR_FLAG(PWR_FLAG
));
529 PWR
->CR
|= PWR_FLAG
<< 2;
548 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/