]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_pwr.c
bb7f7f9dbdf872cf288e96f542398b75ff4832f2
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_pwr.c
1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_hal_pwr.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    12-Sept-2014
7   * @brief   PWR HAL module driver.
8   *
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
13   *
14   @verbatim
15   ******************************************************************************
16   * @attention
17   *
18   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
19   *
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.
30   *
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.
41   *
42   ******************************************************************************
43   */
44
45 /* Includes ------------------------------------------------------------------*/
46 #include "stm32f3xx_hal.h"
47
48 /** @addtogroup STM32F3xx_HAL_Driver
49   * @{
50   */
51
52 /** @defgroup PWR PWR HAL module driver
53   * @brief PWR HAL module driver
54   * @{
55   */
56
57 #ifdef HAL_PWR_MODULE_ENABLED
58
59 /* Private typedef -----------------------------------------------------------*/
60 /* Private define ------------------------------------------------------------*/
61 /* Private macro -------------------------------------------------------------*/
62 /* Private variables ---------------------------------------------------------*/
63 /* Private function prototypes -----------------------------------------------*/
64 /* Private functions ---------------------------------------------------------*/
65
66 /** @defgroup PWR_Exported_Functions PWR Exported Functions
67   * @{
68   */
69
70 /** @defgroup PWR_Exported_Functions_Group1 Initialization and de-initialization functions 
71   *  @brief    Initialization and de-initialization functions
72   *
73 @verbatim
74  ===============================================================================
75               ##### Initialization/de-initialization functions #####
76  ===============================================================================
77     [..]
78       After reset, the backup domain (RTC registers, RTC backup data
79       registers and backup SRAM) is protected against possible unwanted
80       write accesses.
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.
85
86 @endverbatim
87   * @{
88   */
89
90 /**
91   * @brief Deinitializes the HAL PWR peripheral registers to their default reset values.
92   * @retval None
93   */
94 void HAL_PWR_DeInit(void)
95 {
96   __PWR_FORCE_RESET();
97   __PWR_RELEASE_RESET();
98 }
99
100 /**
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.
105   * @retval None
106   */
107 void HAL_PWR_EnableBkUpAccess(void)
108 {
109   *(__IO uint32_t *) CR_DBP_BB = (uint32_t)ENABLE;
110 }
111
112 /**
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.
117   * @retval None
118   */
119 void HAL_PWR_DisableBkUpAccess(void)
120 {
121   *(__IO uint32_t *) CR_DBP_BB = (uint32_t)DISABLE;
122 }
123
124 /**
125   * @}
126   */
127
128 /** @defgroup PWR_Exported_Functions_Group2 Peripheral Control functions 
129   *  @brief Low Power modes configuration functions
130   *
131 @verbatim
132
133  ===============================================================================
134                  ##### Peripheral Control functions #####
135  ===============================================================================
136     [..]
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.
145
146     *** Main and Backup Regulators configuration ***
147     ================================================
148     [..]
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
151           save battery life.
152
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
159           programming manual.
160
161         Refer to the datasheets for more details.
162
163     *** Low Power modes configuration ***
164     =====================================
165     [..]
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
169           in low power mode
170       (+) Standby mode: 1.2V domain powered off (mode not available on STM32F3x8 devices).
171
172    *** Sleep mode ***
173    ==================
174     [..]
175       (+) Entry:
176           The Sleep mode is entered by using the HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFx)
177               functions with
178           (++) PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
179           (++) PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
180      
181       (+) Exit:
182         (++) Any peripheral interrupt acknowledged by the nested vectored interrupt
183               controller (NVIC) can wake up the device from Sleep mode.
184
185    *** Stop mode ***
186    =================
187     [..]
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
190       are preserved.
191       The voltage regulator can be configured either in normal or low-power mode.
192       To minimize the consumption.
193
194       (+) Entry:
195           The Stop mode is entered using the HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI )
196              function with:
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
201       (+) Exit:
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)
207
208    *** Standby mode ***
209    ====================
210      [..]
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
216       circuitry.
217       The voltage regulator is OFF.
218
219       (+) Entry:
220           (++) The Standby mode is entered using the HAL_PWR_EnterSTANDBYMode() function.
221       (+) Exit:
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.
224
225    *** Auto-wakeup (AWU) from low-power mode ***
226    =============================================
227     [..]
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).
231
232     (+) RTC auto-wakeup (AWU) from the Stop and Standby modes
233
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.
236
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.
240
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.
243
244     (+) Comparator auto-wakeup (AWU) from the Stop mode
245
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.      
251 @endverbatim
252   * @{
253   */
254
255 /**
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
260   * @retval None
261   */
262 void HAL_PWR_EnableWakeUpPin(uint32_t WakeUpPinx)
263 {
264   __IO uint32_t tmp = 0;
265   
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;
270 }
271
272 /**
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
277   * @retval None
278   */
279 void HAL_PWR_DisableWakeUpPin(uint32_t WakeUpPinx)
280 {
281   __IO uint32_t tmp = 0;
282   
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;
287 }
288
289 /**
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
302   * @retval None
303   */
304 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
305 {
306    uint32_t tmpreg = 0;
307
308   /* Check the parameters */
309   assert_param(IS_PWR_REGULATOR(Regulator));
310   assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
311
312   /* Select the regulator state in SLEEP mode ---------------------------------*/
313   tmpreg = PWR->CR;
314
315   /* Clear PDDS and LPDS bits */
316   tmpreg &= (uint32_t)~(PWR_CR_PDDS | PWR_CR_LPDS);
317
318   /* Set LPDS bit according to Regulator value */
319   tmpreg |= Regulator;
320
321   /* Store the new value */
322   PWR->CR = tmpreg;
323
324   /* Clear SLEEPDEEP bit of Cortex System Control Register */
325   SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
326
327   /* Select SLEEP mode entry -------------------------------------------------*/
328   if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
329   {
330     /* Request Wait For Interrupt */
331     __WFI();
332   }
333   else
334   {
335     /* Request Wait For Event */
336     __SEV();
337     __WFE();
338     __WFE();
339   }
340 }
341
342 /**
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
359   * @retval None
360   */
361 void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
362 {
363   uint32_t tmpreg = 0;
364
365   /* Check the parameters */
366   assert_param(IS_PWR_REGULATOR(Regulator));
367   assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
368
369   /* Select the regulator state in STOP mode ---------------------------------*/
370   tmpreg = PWR->CR;
371   
372   /* Clear PDDS and LPDS bits */
373   tmpreg &= (uint32_t)~(PWR_CR_PDDS | PWR_CR_LPDS);
374
375   /* Set LPDS bit according to Regulator value */
376   tmpreg |= Regulator;
377
378   /* Store the new value */
379   PWR->CR = tmpreg;
380
381   /* Set SLEEPDEEP bit of Cortex System Control Register */
382   SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
383
384   /* Select STOP mode entry --------------------------------------------------*/
385   if(STOPEntry == PWR_STOPENTRY_WFI)
386   {
387     /* Request Wait For Interrupt */
388     __WFI();
389   }
390   else
391   {
392     /* Request Wait For Event */
393     __SEV();
394     __WFE();
395     __WFE();
396   }
397
398   /* Reset SLEEPDEEP bit of Cortex System Control Register */
399   SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
400 }
401
402 /**
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.
410   * @retval None
411   */
412 void HAL_PWR_EnterSTANDBYMode(void)
413 {
414   /* Select STANDBY mode */
415   PWR->CR |= PWR_CR_PDDS;
416
417   /* Set SLEEPDEEP bit of Cortex System Control Register */
418   SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
419
420   /* This option is used to ensure that store operations are completed */
421 #if defined ( __CC_ARM)
422   __force_stores();
423 #endif
424   /* Request Wait For Interrupt */
425   __WFI();
426 }
427
428 /**
429   * @}
430   */
431
432 /**
433   * @}
434   */
435
436 #endif /* HAL_PWR_MODULE_ENABLED */
437 /**
438   * @}
439   */
440
441 /**
442   * @}
443   */
444
445 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/