1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_pwr_ex.c
4   * @author  MCD Application Team
5   * @brief   Extended PWR HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the Power Controller (PWR) peripheral:
8   *           + Extended Initialization and de-initialization functions
9   *           + Extended Peripheral Control functions
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 "stm32l0xx_hal.h"
26 
27 #ifdef HAL_PWR_MODULE_ENABLED
28 /** @addtogroup STM32L0xx_HAL_Driver
29   * @{
30   */
31 
32 /** @addtogroup PWREx
33   * @{
34   */
35 
36 /** @addtogroup PWREx_Private
37   * @{
38   */
39 
40 /** @defgroup PWR_Extended_TimeOut_Value PWREx Flag Setting Time Out Value
41   * @{
42   */
43 #define PWR_FLAG_SETTING_DELAY_US 50U
44 /**
45   * @}
46   */
47 
48 /**
49   * @}
50   */
51 
52 
53 /** @addtogroup PWREx_Exported_Functions
54   * @brief      Low Power modes configuration functions
55   *
56 @verbatim
57 
58  ===============================================================================
59                  ##### Peripheral extended features functions #####
60  ===============================================================================
61 @endverbatim
62   * @{
63   */
64 
65 /**
66   * @brief Return Voltage Scaling Range.
67   * @retval VOS bit field (PWR_REGULATOR_VOLTAGE_SCALE1, PWR_REGULATOR_VOLTAGE_SCALE2 or PWR_REGULATOR_VOLTAGE_SCALE3)
68   */
HAL_PWREx_GetVoltageRange(void)69 uint32_t HAL_PWREx_GetVoltageRange(void)
70 {
71   return  (PWR->CR & PWR_CR_VOS);
72 }
73 
74 
75 /**
76   * @brief  Enables the Fast WakeUp from Ultra Low Power mode.
77   * @note This bit works in conjunction with ULP bit.
78   *        Means, when ULP = 1 and FWU = 1 :VREFINT startup time is ignored when
79   *        exiting from low power mode.
80   * @retval None
81   */
HAL_PWREx_EnableFastWakeUp(void)82 void HAL_PWREx_EnableFastWakeUp(void)
83 {
84   /* Enable the fast wake up */
85   SET_BIT(PWR->CR, PWR_CR_FWU);
86 }
87 
88 /**
89   * @brief  Disables the Fast WakeUp from Ultra Low Power mode.
90   * @retval None
91   */
HAL_PWREx_DisableFastWakeUp(void)92 void HAL_PWREx_DisableFastWakeUp(void)
93 {
94   /* Disable the fast wake up */
95   CLEAR_BIT(PWR->CR, PWR_CR_FWU);
96 }
97 
98 /**
99   * @brief  Enables the Ultra Low Power mode
100   * @retval None
101   */
HAL_PWREx_EnableUltraLowPower(void)102 void HAL_PWREx_EnableUltraLowPower(void)
103 {
104   /* Enable the Ultra Low Power mode */
105   SET_BIT(PWR->CR, PWR_CR_ULP);
106 }
107 
108 /**
109   * @brief  Disables the Ultra Low Power mode
110   * @retval None
111   */
HAL_PWREx_DisableUltraLowPower(void)112 void HAL_PWREx_DisableUltraLowPower(void)
113 {
114   /* Disable the Ultra Low Power mode */
115   CLEAR_BIT(PWR->CR, PWR_CR_ULP);
116 }
117 
118 /**
119   * @brief  Enable the Low Power Run mode.
120   * @note   Low power run mode can only be entered when VCORE is in range 2.
121   *         In addition, the dynamic voltage scaling must not be used when Low
122   *         power run mode is selected. Only Stop and Sleep modes with regulator
123   *         configured in Low power mode is allowed when Low power run mode is
124   *         selected.
125   * @note   The frequency of the system clock must be decreased to not exceed the
126   *         frequency of RCC_MSIRANGE_1.
127   * @note   In Low power run mode, all I/O pins keep the same state as in Run mode.
128   * @retval None
129   */
HAL_PWREx_EnableLowPowerRunMode(void)130 void HAL_PWREx_EnableLowPowerRunMode(void)
131 {
132   /* Enters the Low Power Run mode */
133   SET_BIT(PWR->CR, PWR_CR_LPSDSR);
134   SET_BIT(PWR->CR, PWR_CR_LPRUN);
135 }
136 
137 /**
138   * @brief  Disable the Low Power Run mode.
139   * @note  Before HAL_PWREx_DisableLowPowerRunMode() completion, the function checks that
140   *        REGLPF has been properly reset (otherwise, HAL_PWREx_DisableLowPowerRunMode
141   *        returns HAL_TIMEOUT status). The system clock frequency can then be
142   *        increased above 2 MHz.
143   * @retval HAL_StatusTypeDef
144   */
HAL_PWREx_DisableLowPowerRunMode(void)145 HAL_StatusTypeDef HAL_PWREx_DisableLowPowerRunMode(void)
146 {
147   uint32_t wait_loop_index = 0U;
148 
149   /* Exit the Low Power Run mode */
150   CLEAR_BIT(PWR->CR, PWR_CR_LPRUN);
151   CLEAR_BIT(PWR->CR, PWR_CR_LPSDSR);
152 
153   /* Wait until REGLPF is reset */
154   wait_loop_index = (PWR_FLAG_SETTING_DELAY_US * (SystemCoreClock / 1000000U));
155 
156   while ((wait_loop_index != 0U) && (HAL_IS_BIT_SET(PWR->CSR, PWR_CSR_REGLPF)))
157   {
158     wait_loop_index--;
159   }
160 
161   if (HAL_IS_BIT_SET(PWR->CSR, PWR_CSR_REGLPF))
162   {
163     return HAL_TIMEOUT;
164   }
165 
166   return HAL_OK;
167 }
168 
169 /**
170   * @}
171   */
172 
173 /**
174   * @}
175   */
176 
177 /**
178   * @}
179   */
180 #endif /* HAL_PWR_MODULE_ENABLED */
181