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   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
15   *
16   * Redistribution and use in source and binary forms, with or without modification,
17   * are permitted provided that the following conditions are met:
18   *   1. Redistributions of source code must retain the above copyright notice,
19   *      this list of conditions and the following disclaimer.
20   *   2. Redistributions in binary form must reproduce the above copyright notice,
21   *      this list of conditions and the following disclaimer in the documentation
22   *      and/or other materials provided with the distribution.
23   *   3. Neither the name of STMicroelectronics nor the names of its contributors
24   *      may be used to endorse or promote products derived from this software
25   *      without specific prior written permission.
26   *
27   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   ******************************************************************************
39   */
40 
41 /* Includes ------------------------------------------------------------------*/
42 #include "stm32l0xx_hal.h"
43 
44 #ifdef HAL_PWR_MODULE_ENABLED
45 /** @addtogroup STM32L0xx_HAL_Driver
46   * @{
47   */
48 
49 /** @addtogroup PWREx
50   * @{
51   */
52 
53 /** @addtogroup PWREx_Private
54   * @{
55   */
56 
57 /** @defgroup PWR_Extended_TimeOut_Value PWREx Flag Setting Time Out Value
58   * @{
59   */
60 #define PWR_FLAG_SETTING_DELAY_US 50U
61 /**
62   * @}
63   */
64 
65 /**
66   * @}
67   */
68 
69 
70 /** @addtogroup PWREx_Exported_Functions
71   * @brief      Low Power modes configuration functions
72   *
73 @verbatim
74 
75  ===============================================================================
76                  ##### Peripheral extended features functions #####
77  ===============================================================================
78 @endverbatim
79   * @{
80   */
81 
82 /**
83   * @brief Return Voltage Scaling Range.
84   * @retval VOS bit field (PWR_REGULATOR_VOLTAGE_SCALE1, PWR_REGULATOR_VOLTAGE_SCALE2 or PWR_REGULATOR_VOLTAGE_SCALE3)
85   */
HAL_PWREx_GetVoltageRange(void)86 uint32_t HAL_PWREx_GetVoltageRange(void)
87 {
88   return  (PWR->CR & PWR_CR_VOS);
89 }
90 
91 
92 /**
93   * @brief  Enables the Fast WakeUp from Ultra Low Power mode.
94   * @note This bit works in conjunction with ULP bit.
95   *        Means, when ULP = 1 and FWU = 1 :VREFINT startup time is ignored when
96   *        exiting from low power mode.
97   * @retval None
98   */
HAL_PWREx_EnableFastWakeUp(void)99 void HAL_PWREx_EnableFastWakeUp(void)
100 {
101   /* Enable the fast wake up */
102   SET_BIT(PWR->CR, PWR_CR_FWU);
103 }
104 
105 /**
106   * @brief  Disables the Fast WakeUp from Ultra Low Power mode.
107   * @retval None
108   */
HAL_PWREx_DisableFastWakeUp(void)109 void HAL_PWREx_DisableFastWakeUp(void)
110 {
111   /* Disable the fast wake up */
112   CLEAR_BIT(PWR->CR, PWR_CR_FWU);
113 }
114 
115 /**
116   * @brief  Enables the Ultra Low Power mode
117   * @retval None
118   */
HAL_PWREx_EnableUltraLowPower(void)119 void HAL_PWREx_EnableUltraLowPower(void)
120 {
121   /* Enable the Ultra Low Power mode */
122   SET_BIT(PWR->CR, PWR_CR_ULP);
123 }
124 
125 /**
126   * @brief  Disables the Ultra Low Power mode
127   * @retval None
128   */
HAL_PWREx_DisableUltraLowPower(void)129 void HAL_PWREx_DisableUltraLowPower(void)
130 {
131   /* Disable the Ultra Low Power mode */
132   CLEAR_BIT(PWR->CR, PWR_CR_ULP);
133 }
134 
135 /**
136   * @brief  Enable the Low Power Run mode.
137   * @note   Low power run mode can only be entered when VCORE is in range 2.
138   *         In addition, the dynamic voltage scaling must not be used when Low
139   *         power run mode is selected. Only Stop and Sleep modes with regulator
140   *         configured in Low power mode is allowed when Low power run mode is
141   *         selected.
142   * @note   The frequency of the system clock must be decreased to not exceed the
143   *         frequency of RCC_MSIRANGE_1.
144   * @note   In Low power run mode, all I/O pins keep the same state as in Run mode.
145   * @retval None
146   */
HAL_PWREx_EnableLowPowerRunMode(void)147 void HAL_PWREx_EnableLowPowerRunMode(void)
148 {
149   /* Enters the Low Power Run mode */
150   SET_BIT(PWR->CR, PWR_CR_LPSDSR);
151   SET_BIT(PWR->CR, PWR_CR_LPRUN);
152 }
153 
154 /**
155   * @brief  Disable the Low Power Run mode.
156   * @note  Before HAL_PWREx_DisableLowPowerRunMode() completion, the function checks that
157   *        REGLPF has been properly reset (otherwise, HAL_PWREx_DisableLowPowerRunMode
158   *        returns HAL_TIMEOUT status). The system clock frequency can then be
159   *        increased above 2 MHz.
160   * @retval HAL_StatusTypeDef
161   */
HAL_PWREx_DisableLowPowerRunMode(void)162 HAL_StatusTypeDef HAL_PWREx_DisableLowPowerRunMode(void)
163 {
164   uint32_t wait_loop_index = 0U;
165 
166   /* Exit the Low Power Run mode */
167   CLEAR_BIT(PWR->CR, PWR_CR_LPRUN);
168   CLEAR_BIT(PWR->CR, PWR_CR_LPSDSR);
169 
170   /* Wait until REGLPF is reset */
171   wait_loop_index = (PWR_FLAG_SETTING_DELAY_US * (SystemCoreClock / 1000000U));
172 
173   while ((wait_loop_index != 0U) && (HAL_IS_BIT_SET(PWR->CSR, PWR_CSR_REGLPF)))
174   {
175     wait_loop_index--;
176   }
177 
178   if (HAL_IS_BIT_SET(PWR->CSR, PWR_CSR_REGLPF))
179   {
180     return HAL_TIMEOUT;
181   }
182 
183   return HAL_OK;
184 }
185 
186 /**
187   * @}
188   */
189 
190 /**
191   * @}
192   */
193 
194 /**
195   * @}
196   */
197 #endif /* HAL_PWR_MODULE_ENABLED */
198 
199 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
200 
201