stm32f0xx_hal_pwr_ex.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_pwr_ex.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 04-November-2016
  7. * @brief Extended PWR HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Power Controller (PWR) peripheral:
  10. * + Extended Initialization and de-initialization functions
  11. * + Extended Peripheral Control functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  17. *
  18. * Redistribution and use in source and binary forms, with or without modification,
  19. * are permitted provided that the following conditions are met:
  20. * 1. Redistributions of source code must retain the above copyright notice,
  21. * this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright notice,
  23. * this list of conditions and the following disclaimer in the documentation
  24. * and/or other materials provided with the distribution.
  25. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  26. * may be used to endorse or promote products derived from this software
  27. * without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  35. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  37. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ******************************************************************************
  41. */
  42. /* Includes ------------------------------------------------------------------*/
  43. #include "stm32f0xx_hal.h"
  44. /** @addtogroup STM32F0xx_HAL_Driver
  45. * @{
  46. */
  47. /** @defgroup PWREx PWREx
  48. * @brief PWREx HAL module driver
  49. * @{
  50. */
  51. #ifdef HAL_PWR_MODULE_ENABLED
  52. /* Private typedef -----------------------------------------------------------*/
  53. /* Private define ------------------------------------------------------------*/
  54. /** @defgroup PWREx_Private_Constants PWREx Private Constants
  55. * @{
  56. */
  57. #define PVD_MODE_IT (0x00010000U)
  58. #define PVD_MODE_EVT (0x00020000U)
  59. #define PVD_RISING_EDGE (0x00000001U)
  60. #define PVD_FALLING_EDGE (0x00000002U)
  61. /**
  62. * @}
  63. */
  64. /* Private macro -------------------------------------------------------------*/
  65. /* Private variables ---------------------------------------------------------*/
  66. /* Private function prototypes -----------------------------------------------*/
  67. /* Exported functions ---------------------------------------------------------*/
  68. /** @defgroup PWREx_Exported_Functions PWREx Exported Functions
  69. * @{
  70. */
  71. /** @defgroup PWREx_Exported_Functions_Group1 Peripheral Extended Control Functions
  72. * @brief Extended Peripheral Control functions
  73. *
  74. @verbatim
  75. ===============================================================================
  76. ##### Peripheral extended control functions #####
  77. ===============================================================================
  78. *** PVD configuration ***
  79. =========================
  80. [..]
  81. (+) The PVD is used to monitor the VDD power supply by comparing it to a
  82. threshold selected by the PVD Level (PLS[2:0] bits in the PWR_CR).
  83. (+) A PVDO flag is available to indicate if VDD/VDDA is higher or lower
  84. than the PVD threshold. This event is internally connected to the EXTI
  85. line16 and can generate an interrupt if enabled. This is done through
  86. HAL_PWR_ConfigPVD(), HAL_PWR_EnablePVD() functions.
  87. (+) The PVD is stopped in Standby mode.
  88. -@- PVD is not available on STM32F030x4/x6/x8
  89. *** VDDIO2 Monitor Configuration ***
  90. ====================================
  91. [..]
  92. (+) VDDIO2 monitor is used to monitor the VDDIO2 power supply by comparing it
  93. to VREFInt Voltage
  94. (+) This monitor is internally connected to the EXTI line31
  95. and can generate an interrupt if enabled. This is done through
  96. HAL_PWREx_EnableVddio2Monitor() function.
  97. -@- VDDIO2 is available on STM32F07x/09x/04x
  98. @endverbatim
  99. * @{
  100. */
  101. #if defined (STM32F031x6) || defined (STM32F051x8) || \
  102. defined (STM32F071xB) || defined (STM32F091xC) || \
  103. defined (STM32F042x6) || defined (STM32F072xB)
  104. /**
  105. * @brief Configures the voltage threshold detected by the Power Voltage Detector(PVD).
  106. * @param sConfigPVD: pointer to an PWR_PVDTypeDef structure that contains the configuration
  107. * information for the PVD.
  108. * @note Refer to the electrical characteristics of your device datasheet for
  109. * more details about the voltage threshold corresponding to each
  110. * detection level.
  111. * @retval None
  112. */
  113. void HAL_PWR_ConfigPVD(PWR_PVDTypeDef *sConfigPVD)
  114. {
  115. /* Check the parameters */
  116. assert_param(IS_PWR_PVD_LEVEL(sConfigPVD->PVDLevel));
  117. assert_param(IS_PWR_PVD_MODE(sConfigPVD->Mode));
  118. /* Set PLS[7:5] bits according to PVDLevel value */
  119. MODIFY_REG(PWR->CR, PWR_CR_PLS, sConfigPVD->PVDLevel);
  120. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  121. __HAL_PWR_PVD_EXTI_DISABLE_EVENT();
  122. __HAL_PWR_PVD_EXTI_DISABLE_IT();
  123. __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();__HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();
  124. /* Configure interrupt mode */
  125. if((sConfigPVD->Mode & PVD_MODE_IT) == PVD_MODE_IT)
  126. {
  127. __HAL_PWR_PVD_EXTI_ENABLE_IT();
  128. }
  129. /* Configure event mode */
  130. if((sConfigPVD->Mode & PVD_MODE_EVT) == PVD_MODE_EVT)
  131. {
  132. __HAL_PWR_PVD_EXTI_ENABLE_EVENT();
  133. }
  134. /* Configure the edge */
  135. if((sConfigPVD->Mode & PVD_RISING_EDGE) == PVD_RISING_EDGE)
  136. {
  137. __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
  138. }
  139. if((sConfigPVD->Mode & PVD_FALLING_EDGE) == PVD_FALLING_EDGE)
  140. {
  141. __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();
  142. }
  143. }
  144. /**
  145. * @brief Enables the Power Voltage Detector(PVD).
  146. * @retval None
  147. */
  148. void HAL_PWR_EnablePVD(void)
  149. {
  150. PWR->CR |= (uint32_t)PWR_CR_PVDE;
  151. }
  152. /**
  153. * @brief Disables the Power Voltage Detector(PVD).
  154. * @retval None
  155. */
  156. void HAL_PWR_DisablePVD(void)
  157. {
  158. PWR->CR &= ~((uint32_t)PWR_CR_PVDE);
  159. }
  160. /**
  161. * @brief This function handles the PWR PVD interrupt request.
  162. * @note This API should be called under the PVD_IRQHandler() or PVD_VDDIO2_IRQHandler().
  163. * @retval None
  164. */
  165. void HAL_PWR_PVD_IRQHandler(void)
  166. {
  167. /* Check PWR exti flag */
  168. if(__HAL_PWR_PVD_EXTI_GET_FLAG() != RESET)
  169. {
  170. /* PWR PVD interrupt user callback */
  171. HAL_PWR_PVDCallback();
  172. /* Clear PWR Exti pending bit */
  173. __HAL_PWR_PVD_EXTI_CLEAR_FLAG();
  174. }
  175. }
  176. /**
  177. * @brief PWR PVD interrupt callback
  178. * @retval None
  179. */
  180. __weak void HAL_PWR_PVDCallback(void)
  181. {
  182. /* NOTE : This function Should not be modified, when the callback is needed,
  183. the HAL_PWR_PVDCallback could be implemented in the user file
  184. */
  185. }
  186. #endif /* defined (STM32F031x6) || defined (STM32F051x8) || */
  187. /* defined (STM32F071xB) || defined (STM32F091xC) || */
  188. /* defined (STM32F042x6) || defined (STM32F072xB) */
  189. #if defined (STM32F042x6) || defined (STM32F048xx) || \
  190. defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || \
  191. defined (STM32F091xC) || defined (STM32F098xx)
  192. /**
  193. * @brief Enable VDDIO2 monitor: enable Exti 31 and falling edge detection.
  194. * @note If Exti 31 is enable correlty and VDDIO2 voltage goes below Vrefint,
  195. an interrupt is generated Irq line 1.
  196. NVIS has to be enable by user.
  197. * @retval None
  198. */
  199. void HAL_PWREx_EnableVddio2Monitor(void)
  200. {
  201. __HAL_PWR_VDDIO2_EXTI_ENABLE_IT();
  202. __HAL_PWR_VDDIO2_EXTI_ENABLE_FALLING_EDGE();
  203. }
  204. /**
  205. * @brief Disable the Vddio2 Monitor.
  206. * @retval None
  207. */
  208. void HAL_PWREx_DisableVddio2Monitor(void)
  209. {
  210. __HAL_PWR_VDDIO2_EXTI_DISABLE_IT();
  211. __HAL_PWR_VDDIO2_EXTI_DISABLE_FALLING_EDGE();
  212. }
  213. /**
  214. * @brief This function handles the PWR Vddio2 monitor interrupt request.
  215. * @note This API should be called under the VDDIO2_IRQHandler() PVD_VDDIO2_IRQHandler().
  216. * @retval None
  217. */
  218. void HAL_PWREx_Vddio2Monitor_IRQHandler(void)
  219. {
  220. /* Check PWR exti flag */
  221. if(__HAL_PWR_VDDIO2_EXTI_GET_FLAG() != RESET)
  222. {
  223. /* PWR Vddio2 monitor interrupt user callback */
  224. HAL_PWREx_Vddio2MonitorCallback();
  225. /* Clear PWR Exti pending bit */
  226. __HAL_PWR_VDDIO2_EXTI_CLEAR_FLAG();
  227. }
  228. }
  229. /**
  230. * @brief PWR Vddio2 Monitor interrupt callback
  231. * @retval None
  232. */
  233. __weak void HAL_PWREx_Vddio2MonitorCallback(void)
  234. {
  235. /* NOTE : This function Should not be modified, when the callback is needed,
  236. the HAL_PWREx_Vddio2MonitorCallback could be implemented in the user file
  237. */
  238. }
  239. #endif /* defined (STM32F042x6) || defined (STM32F048xx) || \
  240. defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || \
  241. defined (STM32F091xC) || defined (STM32F098xx) */
  242. /**
  243. * @}
  244. */
  245. /**
  246. * @}
  247. */
  248. #endif /* HAL_PWR_MODULE_ENABLED */
  249. /**
  250. * @}
  251. */
  252. /**
  253. * @}
  254. */
  255. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/