STM32F479xx HAL User Manual
stm32f4xx_ll_utils.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_ll_utils.c
00004   * @author  MCD Application Team
00005   * @brief   UTILS LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
00010   * All rights reserved.</center></h2>
00011   *
00012   * This software component is licensed by ST under BSD 3-Clause license,
00013   * the "License"; You may not use this file except in compliance with the
00014   * License. You may obtain a copy of the License at:
00015   *                        opensource.org/licenses/BSD-3-Clause
00016   *
00017   ******************************************************************************
00018   */
00019 /* Includes ------------------------------------------------------------------*/
00020 #include "stm32f4xx_ll_utils.h"
00021 #include "stm32f4xx_ll_rcc.h"
00022 #include "stm32f4xx_ll_system.h"
00023 #include "stm32f4xx_ll_pwr.h"
00024 #ifdef  USE_FULL_ASSERT
00025 #include "stm32_assert.h"
00026 #else
00027 #define assert_param(expr) ((void)0U)
00028 #endif /* USE_FULL_ASSERT */
00029 
00030 /** @addtogroup STM32F4xx_LL_Driver
00031   * @{
00032   */
00033 
00034 /** @addtogroup UTILS_LL
00035   * @{
00036   */
00037 
00038 /* Private types -------------------------------------------------------------*/
00039 /* Private variables ---------------------------------------------------------*/
00040 /* Private constants ---------------------------------------------------------*/
00041 /** @addtogroup UTILS_LL_Private_Constants
00042   * @{
00043   */
00044 #if defined(RCC_MAX_FREQUENCY_SCALE1)
00045 #define UTILS_MAX_FREQUENCY_SCALE1  RCC_MAX_FREQUENCY           /*!< Maximum frequency for system clock at power scale1, in Hz */
00046 #endif /*RCC_MAX_FREQUENCY_SCALE1 */
00047 #define UTILS_MAX_FREQUENCY_SCALE2  RCC_MAX_FREQUENCY_SCALE2    /*!< Maximum frequency for system clock at power scale2, in Hz */
00048 #if defined(RCC_MAX_FREQUENCY_SCALE3)
00049 #define UTILS_MAX_FREQUENCY_SCALE3  RCC_MAX_FREQUENCY_SCALE3    /*!< Maximum frequency for system clock at power scale3, in Hz */
00050 #endif /* MAX_FREQUENCY_SCALE3 */
00051 
00052 /* Defines used for PLL range */
00053 #define UTILS_PLLVCO_INPUT_MIN      RCC_PLLVCO_INPUT_MIN        /*!< Frequency min for PLLVCO input, in Hz   */
00054 #define UTILS_PLLVCO_INPUT_MAX      RCC_PLLVCO_INPUT_MAX        /*!< Frequency max for PLLVCO input, in Hz   */
00055 #define UTILS_PLLVCO_OUTPUT_MIN     RCC_PLLVCO_OUTPUT_MIN       /*!< Frequency min for PLLVCO output, in Hz  */
00056 #define UTILS_PLLVCO_OUTPUT_MAX     RCC_PLLVCO_OUTPUT_MAX       /*!< Frequency max for PLLVCO output, in Hz  */
00057 
00058 /* Defines used for HSE range */
00059 #define UTILS_HSE_FREQUENCY_MIN      4000000U        /*!< Frequency min for HSE frequency, in Hz   */
00060 #define UTILS_HSE_FREQUENCY_MAX     26000000U        /*!< Frequency max for HSE frequency, in Hz   */
00061 
00062 /* Defines used for FLASH latency according to HCLK Frequency */
00063 #if defined(FLASH_SCALE1_LATENCY1_FREQ)
00064 #define UTILS_SCALE1_LATENCY1_FREQ  FLASH_SCALE1_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
00065 #endif
00066 #if defined(FLASH_SCALE1_LATENCY2_FREQ)
00067 #define UTILS_SCALE1_LATENCY2_FREQ  FLASH_SCALE1_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
00068 #endif
00069 #if defined(FLASH_SCALE1_LATENCY3_FREQ)
00070 #define UTILS_SCALE1_LATENCY3_FREQ  FLASH_SCALE1_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
00071 #endif
00072 #if defined(FLASH_SCALE1_LATENCY4_FREQ)
00073 #define UTILS_SCALE1_LATENCY4_FREQ  FLASH_SCALE1_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
00074 #endif
00075 #if defined(FLASH_SCALE1_LATENCY5_FREQ)
00076 #define UTILS_SCALE1_LATENCY5_FREQ  FLASH_SCALE1_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 1 */
00077 #endif
00078 #define UTILS_SCALE2_LATENCY1_FREQ  FLASH_SCALE2_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
00079 #define UTILS_SCALE2_LATENCY2_FREQ  FLASH_SCALE2_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
00080 #if defined(FLASH_SCALE2_LATENCY3_FREQ)
00081 #define UTILS_SCALE2_LATENCY3_FREQ  FLASH_SCALE2_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
00082 #endif
00083 #if defined(FLASH_SCALE2_LATENCY4_FREQ)
00084 #define UTILS_SCALE2_LATENCY4_FREQ  FLASH_SCALE2_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 2 */
00085 #endif
00086 #if defined(FLASH_SCALE2_LATENCY5_FREQ)
00087 #define UTILS_SCALE2_LATENCY5_FREQ  FLASH_SCALE2_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 2 */
00088 #endif
00089 #if defined(FLASH_SCALE3_LATENCY1_FREQ)
00090 #define UTILS_SCALE3_LATENCY1_FREQ  FLASH_SCALE3_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 3 */
00091 #endif
00092 #if defined(FLASH_SCALE3_LATENCY2_FREQ)
00093 #define UTILS_SCALE3_LATENCY2_FREQ  FLASH_SCALE3_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 3 */
00094 #endif
00095 #if defined(FLASH_SCALE3_LATENCY3_FREQ)
00096 #define UTILS_SCALE3_LATENCY3_FREQ  FLASH_SCALE3_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 3 in power scale 3 */
00097 #endif
00098 #if defined(FLASH_SCALE3_LATENCY4_FREQ)
00099 #define UTILS_SCALE3_LATENCY4_FREQ  FLASH_SCALE3_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 3 */
00100 #endif
00101 #if defined(FLASH_SCALE3_LATENCY5_FREQ)
00102 #define UTILS_SCALE3_LATENCY5_FREQ  FLASH_SCALE3_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 3 */
00103 #endif
00104 /**
00105   * @}
00106   */
00107 
00108 /* Private macros ------------------------------------------------------------*/
00109 /** @addtogroup UTILS_LL_Private_Macros
00110   * @{
00111   */
00112 #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1)   \
00113                                         || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2)   \
00114                                         || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4)   \
00115                                         || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8)   \
00116                                         || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16)  \
00117                                         || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64)  \
00118                                         || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
00119                                         || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
00120                                         || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
00121 
00122 #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
00123                                       || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
00124                                       || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
00125                                       || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
00126                                       || ((__VALUE__) == LL_RCC_APB1_DIV_16))
00127 
00128 #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
00129                                       || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
00130                                       || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
00131                                       || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
00132                                       || ((__VALUE__) == LL_RCC_APB2_DIV_16))
00133 
00134 #define IS_LL_UTILS_PLLM_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLM_DIV_2)  \
00135                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_3)  \
00136                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_4)  \
00137                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_5)  \
00138                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_6)  \
00139                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_7)  \
00140                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_8)  \
00141                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_9)  \
00142                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_10) \
00143                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_11) \
00144                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_12) \
00145                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_13) \
00146                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_14) \
00147                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_15) \
00148                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_16) \
00149                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_17) \
00150                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_18) \
00151                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_19) \
00152                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_20) \
00153                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_21) \
00154                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_22) \
00155                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_23) \
00156                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_24) \
00157                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_25) \
00158                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_26) \
00159                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_27) \
00160                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_28) \
00161                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_29) \
00162                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_30) \
00163                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_31) \
00164                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_32) \
00165                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_33) \
00166                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_34) \
00167                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_35) \
00168                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_36) \
00169                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_37) \
00170                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_38) \
00171                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_39) \
00172                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_40) \
00173                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_41) \
00174                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_42) \
00175                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_43) \
00176                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_44) \
00177                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_45) \
00178                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_46) \
00179                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_47) \
00180                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_48) \
00181                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_49) \
00182                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_50) \
00183                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_51) \
00184                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_52) \
00185                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_53) \
00186                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_54) \
00187                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_55) \
00188                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_56) \
00189                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_57) \
00190                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_58) \
00191                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_59) \
00192                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_60) \
00193                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_61) \
00194                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_62) \
00195                                         || ((__VALUE__) == LL_RCC_PLLM_DIV_63))
00196 
00197 #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((RCC_PLLN_MIN_VALUE <= (__VALUE__)) && ((__VALUE__) <= RCC_PLLN_MAX_VALUE))
00198 
00199 #define IS_LL_UTILS_PLLP_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLP_DIV_2) \
00200                                         || ((__VALUE__) == LL_RCC_PLLP_DIV_4) \
00201                                         || ((__VALUE__) == LL_RCC_PLLP_DIV_6) \
00202                                         || ((__VALUE__) == LL_RCC_PLLP_DIV_8))
00203 
00204 #define IS_LL_UTILS_PLLVCO_INPUT(__VALUE__)  ((UTILS_PLLVCO_INPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX))
00205 
00206 #define IS_LL_UTILS_PLLVCO_OUTPUT(__VALUE__) ((UTILS_PLLVCO_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_OUTPUT_MAX))
00207 
00208 #if !defined(RCC_MAX_FREQUENCY_SCALE1)
00209 #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
00210                                              ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
00211                                              
00212 #elif defined(RCC_MAX_FREQUENCY_SCALE3) 
00213 #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
00214                                               (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
00215                                               ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
00216 
00217 #else
00218 #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
00219                                              ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2))
00220 
00221 #endif /* RCC_MAX_FREQUENCY_SCALE1*/
00222 #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
00223                                         || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
00224 
00225 #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
00226 /**
00227   * @}
00228   */
00229 /* Private function prototypes -----------------------------------------------*/
00230 /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
00231   * @{
00232   */
00233 static uint32_t    UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency,
00234                                                LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
00235 static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
00236 static ErrorStatus UTILS_PLL_IsBusy(void);
00237 /**
00238   * @}
00239   */
00240 
00241 /* Exported functions --------------------------------------------------------*/
00242 /** @addtogroup UTILS_LL_Exported_Functions
00243   * @{
00244   */
00245 
00246 /** @addtogroup UTILS_LL_EF_DELAY
00247   * @{
00248   */
00249 
00250 /**
00251   * @brief  This function configures the Cortex-M SysTick source to have 1ms time base.
00252   * @note   When a RTOS is used, it is recommended to avoid changing the Systick
00253   *         configuration by calling this function, for a delay use rather osDelay RTOS service.
00254   * @param  HCLKFrequency HCLK frequency in Hz
00255   * @note   HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq
00256   * @retval None
00257   */
00258 void LL_Init1msTick(uint32_t HCLKFrequency)
00259 {
00260   /* Use frequency provided in argument */
00261   LL_InitTick(HCLKFrequency, 1000U);
00262 }
00263 
00264 /**
00265   * @brief  This function provides accurate delay (in milliseconds) based
00266   *         on SysTick counter flag
00267   * @note   When a RTOS is used, it is recommended to avoid using blocking delay
00268   *         and use rather osDelay service.
00269   * @note   To respect 1ms timebase, user should call @ref LL_Init1msTick function which
00270   *         will configure Systick to 1ms
00271   * @param  Delay specifies the delay time length, in milliseconds.
00272   * @retval None
00273   */
00274 void LL_mDelay(uint32_t Delay)
00275 {
00276   __IO uint32_t  tmp = SysTick->CTRL;  /* Clear the COUNTFLAG first */
00277   /* Add this code to indicate that local variable is not used */
00278   ((void)tmp);
00279 
00280   /* Add a period to guaranty minimum wait */
00281   if(Delay < LL_MAX_DELAY)
00282   {
00283     Delay++;
00284   }
00285 
00286   while (Delay)
00287   {
00288     if((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
00289     {
00290       Delay--;
00291     }
00292   }
00293 }
00294 
00295 /**
00296   * @}
00297   */
00298 
00299 /** @addtogroup UTILS_EF_SYSTEM
00300   *  @brief    System Configuration functions
00301   *
00302   @verbatim
00303  ===============================================================================
00304            ##### System Configuration functions #####
00305  ===============================================================================
00306     [..]
00307          System, AHB and APB buses clocks configuration
00308 
00309          (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 180000000 Hz.
00310   @endverbatim
00311   @internal
00312              Depending on the device voltage range, the maximum frequency should be
00313              adapted accordingly to the Refenece manual.
00314   @endinternal
00315   * @{
00316   */
00317 
00318 /**
00319   * @brief  This function sets directly SystemCoreClock CMSIS variable.
00320   * @note   Variable can be calculated also through SystemCoreClockUpdate function.
00321   * @param  HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
00322   * @retval None
00323   */
00324 void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
00325 {
00326   /* HCLK clock frequency */
00327   SystemCoreClock = HCLKFrequency;
00328 }
00329 
00330 /**
00331   * @brief  Update number of Flash wait states in line with new frequency and current
00332             voltage range.
00333   * @note   This Function support ONLY devices with supply voltage (voltage range) between 2.7V and 3.6V
00334   * @param  HCLK_Frequency  HCLK frequency
00335   * @retval An ErrorStatus enumeration value:
00336   *          - SUCCESS: Latency has been modified
00337   *          - ERROR: Latency cannot be modified
00338   */
00339 ErrorStatus LL_SetFlashLatency(uint32_t HCLK_Frequency)
00340 {
00341   uint32_t timeout;
00342   uint32_t getlatency;
00343   uint32_t latency = LL_FLASH_LATENCY_0;  /* default value 0WS */
00344   ErrorStatus status = SUCCESS;
00345 
00346 
00347   /* Frequency cannot be equal to 0 */
00348   if(HCLK_Frequency == 0U)
00349   {
00350     status = ERROR;
00351   }
00352   else
00353   {
00354     if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1)
00355     {
00356 #if defined (UTILS_SCALE1_LATENCY5_FREQ)
00357       if((HCLK_Frequency > UTILS_SCALE1_LATENCY5_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00358       {
00359         latency = LL_FLASH_LATENCY_5;
00360       }
00361 #endif /*UTILS_SCALE1_LATENCY5_FREQ */
00362 #if defined (UTILS_SCALE1_LATENCY4_FREQ)
00363       if((HCLK_Frequency > UTILS_SCALE1_LATENCY4_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00364       {
00365         latency = LL_FLASH_LATENCY_4;
00366       }
00367 #endif /* UTILS_SCALE1_LATENCY4_FREQ */
00368 #if defined (UTILS_SCALE1_LATENCY3_FREQ)
00369       if((HCLK_Frequency > UTILS_SCALE1_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00370       {
00371         latency = LL_FLASH_LATENCY_3;
00372       }
00373 #endif /* UTILS_SCALE1_LATENCY3_FREQ */
00374 #if defined (UTILS_SCALE1_LATENCY2_FREQ) 
00375       if((HCLK_Frequency > UTILS_SCALE1_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00376       {
00377         latency = LL_FLASH_LATENCY_2;
00378       }
00379       else
00380       {
00381         if((HCLK_Frequency > UTILS_SCALE1_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00382         {
00383           latency = LL_FLASH_LATENCY_1;
00384         }
00385       }
00386 #endif /* UTILS_SCALE1_LATENCY2_FREQ */
00387     }
00388     if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2)
00389     {
00390 #if defined (UTILS_SCALE2_LATENCY5_FREQ)
00391       if((HCLK_Frequency > UTILS_SCALE2_LATENCY5_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00392       {
00393         latency = LL_FLASH_LATENCY_5;
00394       }
00395 #endif /*UTILS_SCALE1_LATENCY5_FREQ */
00396 #if defined (UTILS_SCALE2_LATENCY4_FREQ)
00397       if((HCLK_Frequency > UTILS_SCALE2_LATENCY4_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00398       {
00399         latency = LL_FLASH_LATENCY_4;
00400       }
00401 #endif /*UTILS_SCALE1_LATENCY4_FREQ */
00402 #if defined (UTILS_SCALE2_LATENCY3_FREQ)
00403       if((HCLK_Frequency > UTILS_SCALE2_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00404       {
00405         latency = LL_FLASH_LATENCY_3;
00406       }
00407 #endif /*UTILS_SCALE1_LATENCY3_FREQ */
00408       if((HCLK_Frequency > UTILS_SCALE2_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00409       {
00410         latency = LL_FLASH_LATENCY_2;
00411       }
00412       else
00413       {
00414         if((HCLK_Frequency > UTILS_SCALE2_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00415         {
00416           latency = LL_FLASH_LATENCY_1;
00417         }
00418       }
00419     }
00420 #if defined (LL_PWR_REGU_VOLTAGE_SCALE3)
00421     if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE3)
00422     {
00423 #if defined (UTILS_SCALE3_LATENCY3_FREQ)
00424       if((HCLK_Frequency > UTILS_SCALE3_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00425       {
00426         latency = LL_FLASH_LATENCY_3;
00427       }
00428 #endif /*UTILS_SCALE1_LATENCY3_FREQ */
00429 #if defined (UTILS_SCALE3_LATENCY2_FREQ)
00430       if((HCLK_Frequency > UTILS_SCALE3_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00431       {
00432         latency = LL_FLASH_LATENCY_2;
00433       }
00434       else
00435       {
00436         if((HCLK_Frequency > UTILS_SCALE3_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
00437         {
00438           latency = LL_FLASH_LATENCY_1;
00439         }
00440       }
00441     }
00442 #endif /*UTILS_SCALE1_LATENCY2_FREQ */
00443 #endif /* LL_PWR_REGU_VOLTAGE_SCALE3 */
00444 
00445     LL_FLASH_SetLatency(latency);
00446     /* Check that the new number of wait states is taken into account to access the Flash
00447        memory by reading the FLASH_ACR register */
00448     timeout = 2;
00449     do
00450     {
00451     /* Wait for Flash latency to be updated */
00452     getlatency = LL_FLASH_GetLatency();
00453     timeout--;
00454     } while ((getlatency != latency) && (timeout > 0));
00455 
00456     if(getlatency != latency)
00457     {
00458       status = ERROR;
00459     }
00460     else
00461     {
00462       status = SUCCESS;
00463     }
00464   }
00465   return status;
00466 }
00467 
00468 /**
00469   * @brief  This function configures system clock at maximum frequency with HSI as clock source of the PLL
00470   * @note   The application need to ensure that PLL is disabled.
00471   * @note   Function is based on the following formula:
00472   *         - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
00473   *         - PLLM: ensure that the VCO input frequency ranges from @ref RCC_PLLVCO_INPUT_MIN to @ref RCC_PLLVCO_INPUT_MAX (PLLVCO_input = HSI frequency / PLLM)
00474   *         - PLLN: ensure that the VCO output frequency is between @ref RCC_PLLVCO_OUTPUT_MIN and @ref RCC_PLLVCO_OUTPUT_MAX (PLLVCO_output = PLLVCO_input * PLLN)
00475   *         - PLLP: ensure that max frequency at 180000000 Hz is reach (PLLVCO_output / PLLP)
00476   * @param  UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
00477   *                             the configuration information for the PLL.
00478   * @param  UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
00479   *                             the configuration information for the BUS prescalers.
00480   * @retval An ErrorStatus enumeration value:
00481   *          - SUCCESS: Max frequency configuration done
00482   *          - ERROR: Max frequency configuration not done
00483   */
00484 ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
00485                                          LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
00486 {
00487   ErrorStatus status = SUCCESS;
00488   uint32_t pllfreq = 0U;
00489 
00490   /* Check if one of the PLL is enabled */
00491   if(UTILS_PLL_IsBusy() == SUCCESS)
00492   {
00493     /* Calculate the new PLL output frequency */
00494     pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct);
00495 
00496     /* Enable HSI if not enabled */
00497     if(LL_RCC_HSI_IsReady() != 1U)
00498     {
00499       LL_RCC_HSI_Enable();
00500       while (LL_RCC_HSI_IsReady() != 1U)
00501       {
00502         /* Wait for HSI ready */
00503       }
00504     }
00505 
00506     /* Configure PLL */
00507     LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
00508                                 UTILS_PLLInitStruct->PLLP);
00509 
00510     /* Enable PLL and switch system clock to PLL */
00511     status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
00512   }
00513   else
00514   {
00515     /* Current PLL configuration cannot be modified */
00516     status = ERROR;
00517   }
00518 
00519   return status;
00520 }
00521 
00522 /**
00523   * @brief  This function configures system clock with HSE as clock source of the PLL
00524   * @note   The application need to ensure that PLL is disabled.
00525   *         - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
00526   *         - PLLM: ensure that the VCO input frequency ranges from @ref RCC_PLLVCO_INPUT_MIN to @ref RCC_PLLVCO_INPUT_MAX (PLLVCO_input = HSI frequency / PLLM)
00527   *         - PLLN: ensure that the VCO output frequency is between @ref RCC_PLLVCO_OUTPUT_MIN and @ref RCC_PLLVCO_OUTPUT_MAX (PLLVCO_output = PLLVCO_input * PLLN)
00528   *         - PLLP: ensure that max frequency at 180000000 Hz is reach (PLLVCO_output / PLLP)
00529   * @param  HSEFrequency Value between Min_Data = 4000000 and Max_Data = 26000000
00530   * @param  HSEBypass This parameter can be one of the following values:
00531   *         @arg @ref LL_UTILS_HSEBYPASS_ON
00532   *         @arg @ref LL_UTILS_HSEBYPASS_OFF
00533   * @param  UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
00534   *                             the configuration information for the PLL.
00535   * @param  UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
00536   *                             the configuration information for the BUS prescalers.
00537   * @retval An ErrorStatus enumeration value:
00538   *          - SUCCESS: Max frequency configuration done
00539   *          - ERROR: Max frequency configuration not done
00540   */
00541 ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
00542                                          LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
00543 {
00544   ErrorStatus status = SUCCESS;
00545   uint32_t pllfreq = 0U;
00546 
00547   /* Check the parameters */
00548   assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
00549   assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
00550 
00551   /* Check if one of the PLL is enabled */
00552   if(UTILS_PLL_IsBusy() == SUCCESS)
00553   {
00554     /* Calculate the new PLL output frequency */
00555     pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
00556 
00557     /* Enable HSE if not enabled */
00558     if(LL_RCC_HSE_IsReady() != 1U)
00559     {
00560       /* Check if need to enable HSE bypass feature or not */
00561       if(HSEBypass == LL_UTILS_HSEBYPASS_ON)
00562       {
00563         LL_RCC_HSE_EnableBypass();
00564       }
00565       else
00566       {
00567         LL_RCC_HSE_DisableBypass();
00568       }
00569 
00570       /* Enable HSE */
00571       LL_RCC_HSE_Enable();
00572       while (LL_RCC_HSE_IsReady() != 1U)
00573       {
00574         /* Wait for HSE ready */
00575       }
00576     }
00577 
00578     /* Configure PLL */
00579     LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
00580                                 UTILS_PLLInitStruct->PLLP);
00581 
00582     /* Enable PLL and switch system clock to PLL */
00583     status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
00584   }
00585   else
00586   {
00587     /* Current PLL configuration cannot be modified */
00588     status = ERROR;
00589   }
00590 
00591   return status;
00592 }
00593 
00594 /**
00595   * @}
00596   */
00597 
00598 /**
00599   * @}
00600   */
00601 
00602 /** @addtogroup UTILS_LL_Private_Functions
00603   * @{
00604   */
00605 /**
00606   * @brief  Function to check that PLL can be modified
00607   * @param  PLL_InputFrequency  PLL input frequency (in Hz)
00608   * @param  UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
00609   *                             the configuration information for the PLL.
00610   * @retval PLL output frequency (in Hz)
00611   */
00612 static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
00613 {
00614   uint32_t pllfreq = 0U;
00615 
00616   /* Check the parameters */
00617   assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
00618   assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
00619   assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct->PLLP));
00620   
00621   /* Check different PLL parameters according to RM                          */
00622   /*  - PLLM: ensure that the VCO input frequency ranges from @ref UTILS_PLLVCO_INPUT_MIN to @ref UTILS_PLLVCO_INPUT_MAX MHz.   */
00623   pllfreq = PLL_InputFrequency / (UTILS_PLLInitStruct->PLLM & (RCC_PLLCFGR_PLLM >> RCC_PLLCFGR_PLLM_Pos));
00624   assert_param(IS_LL_UTILS_PLLVCO_INPUT(pllfreq));
00625 
00626   /*  - PLLN: ensure that the VCO output frequency is between @ref UTILS_PLLVCO_OUTPUT_MIN and @ref UTILS_PLLVCO_OUTPUT_MAX .*/
00627   pllfreq = pllfreq * (UTILS_PLLInitStruct->PLLN & (RCC_PLLCFGR_PLLN >> RCC_PLLCFGR_PLLN_Pos));
00628   assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(pllfreq));
00629   
00630   /*  - PLLP: ensure that max frequency at @ref RCC_MAX_FREQUENCY Hz is reached     */
00631   pllfreq = pllfreq / (((UTILS_PLLInitStruct->PLLP >> RCC_PLLCFGR_PLLP_Pos) + 1) * 2);
00632   assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq));
00633 
00634   return pllfreq;
00635 }
00636 
00637 /**
00638   * @brief  Function to check that PLL can be modified
00639   * @retval An ErrorStatus enumeration value:
00640   *          - SUCCESS: PLL modification can be done
00641   *          - ERROR: PLL is busy
00642   */
00643 static ErrorStatus UTILS_PLL_IsBusy(void)
00644 {
00645   ErrorStatus status = SUCCESS;
00646 
00647   /* Check if PLL is busy*/
00648   if(LL_RCC_PLL_IsReady() != 0U)
00649   {
00650     /* PLL configuration cannot be modified */
00651     status = ERROR;
00652   }
00653 
00654 #if defined(RCC_PLLSAI_SUPPORT)
00655   /* Check if PLLSAI is busy*/
00656   if(LL_RCC_PLLSAI_IsReady() != 0U)
00657   {
00658     /* PLLSAI1 configuration cannot be modified */
00659     status = ERROR;
00660   }
00661 #endif /*RCC_PLLSAI_SUPPORT*/
00662 #if defined(RCC_PLLI2S_SUPPORT)
00663   /* Check if PLLI2S is busy*/
00664   if(LL_RCC_PLLI2S_IsReady() != 0U)
00665   {
00666     /* PLLI2S configuration cannot be modified */
00667     status = ERROR;
00668   }
00669 #endif /*RCC_PLLI2S_SUPPORT*/
00670   return status;
00671 }
00672 
00673 /**
00674   * @brief  Function to enable PLL and switch system clock to PLL
00675   * @param  SYSCLK_Frequency SYSCLK frequency
00676   * @param  UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
00677   *                             the configuration information for the BUS prescalers.
00678   * @retval An ErrorStatus enumeration value:
00679   *          - SUCCESS: No problem to switch system to PLL
00680   *          - ERROR: Problem to switch system to PLL
00681   */
00682 static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
00683 {
00684   ErrorStatus status = SUCCESS;
00685   uint32_t hclk_frequency = 0U;
00686 
00687   assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
00688   assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
00689   assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct->APB2CLKDivider));
00690 
00691   /* Calculate HCLK frequency */
00692   hclk_frequency = __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider);
00693 
00694   /* Increasing the number of wait states because of higher CPU frequency */
00695   if(SystemCoreClock < hclk_frequency)
00696   {
00697     /* Set FLASH latency to highest latency */
00698     status = LL_SetFlashLatency(hclk_frequency);
00699   }
00700 
00701   /* Update system clock configuration */
00702   if(status == SUCCESS)
00703   {
00704     /* Enable PLL */
00705     LL_RCC_PLL_Enable();
00706     while (LL_RCC_PLL_IsReady() != 1U)
00707     {
00708       /* Wait for PLL ready */
00709     }
00710 
00711     /* Sysclk activation on the main PLL */
00712     LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
00713     LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
00714     while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
00715     {
00716       /* Wait for system clock switch to PLL */
00717     }
00718 
00719     /* Set APB1 & APB2 prescaler*/
00720     LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
00721     LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct->APB2CLKDivider);
00722   }
00723     
00724   /* Decreasing the number of wait states because of lower CPU frequency */
00725   if(SystemCoreClock > hclk_frequency)
00726   {
00727     /* Set FLASH latency to lowest latency */
00728     status = LL_SetFlashLatency(hclk_frequency);
00729   }
00730 
00731   /* Update SystemCoreClock variable */
00732   if(status == SUCCESS)
00733   {
00734     LL_SetSystemCoreClock(hclk_frequency);
00735   }
00736 
00737   return status;
00738 }
00739 
00740 /**
00741   * @}
00742   */
00743 
00744 /**
00745   * @}
00746   */
00747 
00748 /**
00749   * @}
00750   */
00751 
00752 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/