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