stm32l0xx_hal_pwr_ex.c 4.8 KB

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