STM32G474xx HAL 用户手册
stm32g4xx_hal_pwr_ex.c
转到此文件的文档。
00001 /**
00002   ******************************************************************************
00003   * @file    stm32g4xx_hal_pwr_ex.c
00004   * @author  MCD Application Team
00005   * @brief   Extended PWR HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Power Controller (PWR) peripheral:
00008   *           + Extended Initialization and de-initialization functions
00009   *           + Extended Peripheral Control functions
00010   *
00011   ******************************************************************************
00012   * @attention
00013   *
00014   * Copyright (c) 2019 STMicroelectronics.
00015   * All rights reserved.
00016   *
00017   * This software is licensed under terms that can be found in the LICENSE file
00018   * in the root directory of this software component.
00019   * If no LICENSE file comes with this software, it is provided AS-IS.
00020   *
00021   ******************************************************************************
00022   */
00023 
00024 /* Includes ------------------------------------------------------------------*/
00025 #include "stm32g4xx_hal.h"
00026 
00027 /** @addtogroup STM32G4xx_HAL_Driver
00028   * @{
00029   */
00030 
00031 /** @defgroup PWREx PWREx
00032   * @brief PWR Extended HAL module driver
00033   * @{
00034   */
00035 
00036 #ifdef HAL_PWR_MODULE_ENABLED
00037 
00038 /* Private typedef -----------------------------------------------------------*/
00039 /* Private define ------------------------------------------------------------*/
00040 
00041 
00042 #if defined (STM32G471xx) || defined (STM32G473xx) || defined (STM32G414xx) || defined (STM32G474xx) || defined (STM32G483xx) || defined (STM32G484xx)
00043 #define PWR_PORTF_AVAILABLE_PINS   0x0000FFFFU /* PF0..PF15 */
00044 #define PWR_PORTG_AVAILABLE_PINS   0x000007FFU /* PG0..PG10 */
00045 #elif defined (STM32G411xB) || defined (STM32G411xC) || defined (STM32G431xx) || defined (STM32G441xx) || defined (STM32GBK1CB) || defined (STM32G491xx) || defined (STM32G4A1xx)
00046 #define PWR_PORTF_AVAILABLE_PINS   0x00000607U /* PF0..PF2 and PF9 and PF10 */
00047 #define PWR_PORTG_AVAILABLE_PINS   0x00000400U /* PG10 */
00048 #endif /* STM32G471xx || STM32G473xx || STM32G414xx || STM32G474xx || STM32G483xx || STM32G484xx */
00049 
00050 /** @defgroup PWR_Extended_Private_Defines PWR Extended Private Defines
00051   * @{
00052   */
00053 
00054 /** @defgroup PWREx_PVM_Mode_Mask PWR PVM Mode Mask
00055   * @{
00056   */
00057 #define PVM_MODE_IT               0x00010000U    /*!< Mask for interruption yielded by PVM threshold crossing */
00058 #define PVM_MODE_EVT              0x00020000U    /*!< Mask for event yielded by PVM threshold crossing        */
00059 #define PVM_RISING_EDGE           0x00000001U    /*!< Mask for rising edge set as PVM trigger                 */
00060 #define PVM_FALLING_EDGE          0x00000002U    /*!< Mask for falling edge set as PVM trigger                */
00061 /**
00062   * @}
00063   */
00064 
00065 /** @defgroup PWREx_TimeOut_Value PWR Extended Flag Setting Time Out Value
00066   * @{
00067   */
00068 #define PWR_FLAG_SETTING_DELAY_US                      50UL   /*!< Time out value for REGLPF and VOSF flags setting */
00069 /**
00070   * @}
00071   */
00072 
00073 
00074 /**
00075   * @}
00076   */
00077 
00078 
00079 
00080 /* Private macro -------------------------------------------------------------*/
00081 /* Private variables ---------------------------------------------------------*/
00082 /* Private function prototypes -----------------------------------------------*/
00083 /* Exported functions --------------------------------------------------------*/
00084 
00085 /** @defgroup PWREx_Exported_Functions PWR Extended Exported Functions
00086   * @{
00087   */
00088 
00089 /** @defgroup PWREx_Exported_Functions_Group1 Extended Peripheral Control functions
00090   *  @brief   Extended Peripheral Control functions
00091   *
00092 @verbatim
00093  ===============================================================================
00094               ##### Extended Peripheral Initialization and de-initialization functions #####
00095  ===============================================================================
00096     [..]
00097 
00098 @endverbatim
00099   * @{
00100   */
00101 
00102 
00103 /**
00104   * @brief Return Voltage Scaling Range.
00105   * @retval VOS bit field (PWR_REGULATOR_VOLTAGE_SCALE1 or PWR_REGULATOR_VOLTAGE_SCALE2
00106   *         or PWR_REGULATOR_VOLTAGE_SCALE1_BOOST when applicable)
00107   */
00108 
00109 uint32_t HAL_PWREx_GetVoltageRange(void)
00110 {
00111   if (READ_BIT(PWR->CR1, PWR_CR1_VOS) == PWR_REGULATOR_VOLTAGE_SCALE2)
00112   {
00113     return PWR_REGULATOR_VOLTAGE_SCALE2;
00114   }
00115   else if (READ_BIT(PWR->CR5, PWR_CR5_R1MODE) == PWR_CR5_R1MODE)
00116   {
00117     /* PWR_CR5_R1MODE bit set means that Range 1 Boost is disabled */
00118     return PWR_REGULATOR_VOLTAGE_SCALE1;
00119   }
00120   else
00121   {
00122     return PWR_REGULATOR_VOLTAGE_SCALE1_BOOST;
00123   }
00124 }
00125 
00126 
00127 
00128 /**
00129   * @brief Configure the main internal regulator output voltage.
00130   * @param  VoltageScaling: specifies the regulator output voltage to achieve
00131   *         a tradeoff between performance and power consumption.
00132   *          This parameter can be one of the following values:
00133   *            @arg @ref PWR_REGULATOR_VOLTAGE_SCALE1_BOOST when available, Regulator voltage output range 1 boost mode,
00134   *                                                typical output voltage at 1.28 V,
00135   *                                                system frequency up to 170 MHz.
00136   *            @arg @ref PWR_REGULATOR_VOLTAGE_SCALE1 Regulator voltage output range 1 mode,
00137   *                                                typical output voltage at 1.2 V,
00138   *                                                system frequency up to 150 MHz.
00139   *            @arg @ref PWR_REGULATOR_VOLTAGE_SCALE2 Regulator voltage output range 2 mode,
00140   *                                                typical output voltage at 1.0 V,
00141   *                                                system frequency up to 26 MHz.
00142   * @note  When moving from Range 1 to Range 2, the system frequency must be decreased to
00143   *        a value below 26 MHz before calling HAL_PWREx_ControlVoltageScaling() API.
00144   *        When moving from Range 2 to Range 1, the system frequency can be increased to
00145   *        a value up to 150 MHz after calling HAL_PWREx_ControlVoltageScaling() API.
00146   *        When moving from Range 1 to Boost Mode Range 1, the system frequency can be increased to
00147   *        a value up to 170 MHz after calling HAL_PWREx_ControlVoltageScaling() API.
00148   * @note  When moving from Range 2 to Range 1, the API waits for VOSF flag to be
00149   *        cleared before returning the status. If the flag is not cleared within
00150   *        50 microseconds, HAL_TIMEOUT status is reported.
00151   * @retval HAL Status
00152   */
00153 HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling(uint32_t VoltageScaling)
00154 {
00155   uint32_t wait_loop_index;
00156 
00157   assert_param(IS_PWR_VOLTAGE_SCALING_RANGE(VoltageScaling));
00158 
00159   if (VoltageScaling == PWR_REGULATOR_VOLTAGE_SCALE1_BOOST)
00160   {
00161     /* If current range is range 2 */
00162     if (READ_BIT(PWR->CR1, PWR_CR1_VOS) == PWR_REGULATOR_VOLTAGE_SCALE2)
00163     {
00164       /* Make sure Range 1 Boost is enabled */
00165       CLEAR_BIT(PWR->CR5, PWR_CR5_R1MODE);
00166 
00167       /* Set Range 1 */
00168       MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE1);
00169 
00170       /* Wait until VOSF is cleared */
00171       wait_loop_index = ((PWR_FLAG_SETTING_DELAY_US * SystemCoreClock) / 1000000U) + 1U;
00172       while ((