1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_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 "stm32f3xx_hal.h"
26 
27 /** @addtogroup STM32F3xx_HAL_Driver
28   * @{
29   */
30 
31 /** @defgroup PWREx PWREx
32   * @brief    PWREx HAL module driver
33   * @{
34   */
35 
36 #ifdef HAL_PWR_MODULE_ENABLED
37 
38 /* Private typedef -----------------------------------------------------------*/
39 /* Private define ------------------------------------------------------------*/
40 /** @defgroup PWREx_Private_Constants PWR Extended Private Constants
41   * @{
42   */
43 #define PVD_MODE_IT               (0x00010000U)
44 #define PVD_MODE_EVT              (0x00020000U)
45 #define PVD_RISING_EDGE           (0x00000001U)
46 #define PVD_FALLING_EDGE          (0x00000002U)
47 /**
48   * @}
49   */
50 
51 /* Private macro -------------------------------------------------------------*/
52 /* Private variables ---------------------------------------------------------*/
53 /* Private function prototypes -----------------------------------------------*/
54 /* Exported functions ---------------------------------------------------------*/
55 
56 /** @defgroup PWREx_Exported_Functions PWR Extended Exported Functions
57   * @{
58   */
59 
60 /** @defgroup PWREx_Exported_Functions_Group1 Peripheral Extended Control Functions
61   *  @brief   Extended Peripheral Control functions
62   *
63 @verbatim
64 
65  ===============================================================================
66                  ##### Peripheral Extended control functions #####
67  ===============================================================================
68     *** PVD configuration (present on all other devices than STM32F3x8 devices) ***
69     =========================
70     [..]
71       (+) The PVD is used to monitor the VDD power supply by comparing it to a
72           threshold selected by the PVD Level (PLS[2:0] bits in the PWR_CR).
73       (+) A PVDO flag is available to indicate if VDD/VDDA is higher or lower
74           than the PVD threshold. This event is internally connected to the EXTI
75           line16 and can generate an interrupt if enabled. This is done through
76           __HAL_PWR_PVD_EXTI_ENABLE_IT() macro
77       (+) The PVD is stopped in Standby mode.
78       -@- PVD is not available on STM32F3x8 Product Line
79 
80 
81     *** Voltage regulator ***
82     =========================
83     [..]
84       (+) The voltage regulator is always enabled after Reset. It works in three different
85           modes.
86           In Run mode, the regulator supplies full power to the 1.8V domain (core, memories
87           and digital peripherals).
88           In Stop mode, the regulator supplies low power to the 1.8V domain, preserving
89           contents of registers and SRAM.
90           In Stop mode, the regulator is powered off. The contents of the registers and SRAM
91           are lost except for the Standby circuitry and the Backup Domain.
92           Note: in the STM32F3x8xx devices, the voltage regulator is bypassed and the
93           microcontroller must be powered from a nominal VDD = 1.8V +/-8U% voltage.
94 
95 
96       (+) A PVDO flag is available to indicate if VDD/VDDA is higher or lower
97           than the PVD threshold. This event is internally connected to the EXTI
98           line16 and can generate an interrupt if enabled. This is done through
99           __HAL_PWR_PVD_EXTI_ENABLE_IT() macro
100       (+) The PVD is stopped in Standby mode.
101 
102 
103     *** SDADC power configuration ***
104     ================================
105     [..]
106       (+) On STM32F373xC/STM32F378xx devices, there are up to
107           3 SDADC instances that can be enabled/disabled.
108 
109 @endverbatim
110   * @{
111   */
112 
113 #if defined(STM32F302xE) || defined(STM32F303xE) || \
114     defined(STM32F302xC) || defined(STM32F303xC) || \
115     defined(STM32F303x8) || defined(STM32F334x8) || \
116     defined(STM32F301x8) || defined(STM32F302x8) || \
117     defined(STM32F373xC)
118 
119 /**
120   * @brief Configures the voltage threshold detected by the Power Voltage Detector(PVD).
121   * @param sConfigPVD pointer to an PWR_PVDTypeDef structure that contains the configuration
122   *        information for the PVD.
123   * @note Refer to the electrical characteristics of your device datasheet for
124   *         more details about the voltage threshold corresponding to each
125   *         detection level.
126   * @retval None
127   */
HAL_PWR_ConfigPVD(PWR_PVDTypeDef * sConfigPVD)128 void HAL_PWR_ConfigPVD(PWR_PVDTypeDef *sConfigPVD)
129 {
130   /* Check the parameters */
131   assert_param(IS_PWR_PVD_LEVEL(sConfigPVD->PVDLevel));
132   assert_param(IS_PWR_PVD_MODE(sConfigPVD->Mode));
133 
134   /* Set PLS[7:5] bits according to PVDLevel value */
135   MODIFY_REG(PWR->CR, PWR_CR_PLS, sConfigPVD->PVDLevel);
136 
137   /* Clear any previous config. Keep it clear if no event or IT mode is selected */
138   __HAL_PWR_PVD_EXTI_DISABLE_EVENT();
139   __HAL_PWR_PVD_EXTI_DISABLE_IT();
140   __HAL_PWR_PVD_EXTI_DISABLE_RISING_EDGE();__HAL_PWR_PVD_EXTI_DISABLE_FALLING_EDGE();
141 
142   /* Configure interrupt mode */
143   if((sConfigPVD->Mode & PVD_MODE_IT) == PVD_MODE_IT)
144   {
145     __HAL_PWR_PVD_EXTI_ENABLE_IT();
146   }
147 
148   /* Configure event mode */
149   if((sConfigPVD->Mode & PVD_MODE_EVT) == PVD_MODE_EVT)
150   {
151     __HAL_PWR_PVD_EXTI_ENABLE_EVENT();
152   }
153 
154   /* Configure the edge */
155   if((sConfigPVD->Mode & PVD_RISING_EDGE) == PVD_RISING_EDGE)
156   {
157     __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
158   }
159 
160   if((sConfigPVD->Mode & PVD_FALLING_EDGE) == PVD_FALLING_EDGE)
161   {
162     __HAL_PWR_PVD_EXTI_ENABLE_FALLING_EDGE();
163   }
164 }
165 
166 /**
167   * @brief Enables the Power Voltage Detector(PVD).
168   * @retval None
169   */
HAL_PWR_EnablePVD(void)170 void HAL_PWR_EnablePVD(void)
171 {
172   SET_BIT(PWR->CR, PWR_CR_PVDE);
173 }
174 
175 /**
176   * @brief Disables the Power Voltage Detector(PVD).
177   * @retval None
178   */
HAL_PWR_DisablePVD(void)179 void HAL_PWR_DisablePVD(void)
180 {
181   CLEAR_BIT(PWR->CR, PWR_CR_PVDE);
182 }
183 
184 /**
185   * @brief This function handles the PWR PVD interrupt request.
186   * @note This API should be called under the PVD_IRQHandler().
187   * @retval None
188   */
HAL_PWR_PVD_IRQHandler(void)189 void HAL_PWR_PVD_IRQHandler(void)
190 {
191   /* Check PWR exti flag */
192   if(__HAL_PWR_PVD_EXTI_GET_FLAG() != RESET)
193   {
194     /* PWR PVD interrupt user callback */
195     HAL_PWR_PVDCallback();
196 
197     /* Clear PWR Exti pending bit */
198     __HAL_PWR_PVD_EXTI_CLEAR_FLAG();
199   }
200 }
201 
202 /**
203   * @brief PWR PVD interrupt callback
204   * @retval None
205   */
HAL_PWR_PVDCallback(void)206 __weak void HAL_PWR_PVDCallback(void)
207 {
208   /* NOTE : This function Should not be modified, when the callback is needed,
209             the HAL_PWR_PVDCallback could be implemented in the user file
210    */
211 }
212 
213 #endif /* STM32F302xE || STM32F303xE || */
214        /* STM32F302xC || STM32F303xC || */
215        /* STM32F303x8 || STM32F334x8 || */
216        /* STM32F301x8 || STM32F302x8 || */
217        /* STM32F373xC                   */
218 
219 #if defined(STM32F373xC) || defined(STM32F378xx)
220 
221 /**
222   * @brief  Enables the SDADC peripheral functionaliy
223   * @param  Analogx specifies the SDADC peripheral instance.
224   *   This parameter can be: PWR_SDADC_ANALOG1, PWR_SDADC_ANALOG2 or PWR_SDADC_ANALOG3.
225   * @retval None
226   */
HAL_PWREx_EnableSDADC(uint32_t Analogx)227 void HAL_PWREx_EnableSDADC(uint32_t Analogx)
228 {
229   /* Check the parameters */
230   assert_param(IS_PWR_SDADC_ANALOG(Analogx));
231 
232   /* Enable PWR clock interface for SDADC use */
233   __HAL_RCC_PWR_CLK_ENABLE();
234 
235   PWR->CR |= Analogx;
236 }
237 
238 /**
239   * @brief  Disables the SDADC peripheral functionaliy
240   * @param  Analogx specifies the SDADC peripheral instance.
241   *   This parameter can be: PWR_SDADC_ANALOG1, PWR_SDADC_ANALOG2 or PWR_SDADC_ANALOG3.
242   * @retval None
243   */
HAL_PWREx_DisableSDADC(uint32_t Analogx)244 void HAL_PWREx_DisableSDADC(uint32_t Analogx)
245 {
246   /* Check the parameters */
247   assert_param(IS_PWR_SDADC_ANALOG(Analogx));
248 
249   PWR->CR &= ~Analogx;
250 }
251 
252 #endif /* STM32F373xC || STM32F378xx */
253 
254 /**
255   * @}
256   */
257 
258 /**
259   * @}
260   */
261 
262 #endif /* HAL_PWR_MODULE_ENABLED */
263 /**
264   * @}
265   */
266 
267 /**
268   * @}
269   */
270