Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /** |
| 2 | ****************************************************************************** |
||
| 3 | * @file stm32f0xx_ll_utils.c |
||
| 4 | * @author MCD Application Team |
||
| 5 | * @brief UTILS LL module driver. |
||
| 6 | ****************************************************************************** |
||
| 7 | * @attention |
||
| 8 | * |
||
| 9 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
||
| 10 | * All rights reserved.</center></h2> |
||
| 11 | * |
||
| 12 | * This software component is licensed by ST under BSD 3-Clause license, |
||
| 13 | * the "License"; You may not use this file except in compliance with the |
||
| 14 | * License. You may obtain a copy of the License at: |
||
| 15 | * opensource.org/licenses/BSD-3-Clause |
||
| 16 | * |
||
| 17 | ****************************************************************************** |
||
| 18 | */ |
||
| 19 | |||
| 20 | /* Includes ------------------------------------------------------------------*/ |
||
| 21 | #include "stm32f0xx_ll_rcc.h" |
||
| 22 | #include "stm32f0xx_ll_utils.h" |
||
| 23 | #include "stm32f0xx_ll_system.h" |
||
| 24 | #ifdef USE_FULL_ASSERT |
||
| 25 | #include "stm32_assert.h" |
||
| 26 | #else |
||
| 27 | #define assert_param(expr) ((void)0U) |
||
| 28 | #endif |
||
| 29 | |||
| 30 | /** @addtogroup STM32F0xx_LL_Driver |
||
| 31 | * @{ |
||
| 32 | */ |
||
| 33 | |||
| 34 | /** @addtogroup UTILS_LL |
||
| 35 | * @{ |
||
| 36 | */ |
||
| 37 | |||
| 38 | /* Private types -------------------------------------------------------------*/ |
||
| 39 | /* Private variables ---------------------------------------------------------*/ |
||
| 40 | /* Private constants ---------------------------------------------------------*/ |
||
| 41 | /** @addtogroup UTILS_LL_Private_Constants |
||
| 42 | * @{ |
||
| 43 | */ |
||
| 44 | |||
| 45 | /* Defines used for PLL range */ |
||
| 46 | #define UTILS_PLL_OUTPUT_MIN 16000000U /*!< Frequency min for PLL output, in Hz */ |
||
| 47 | #define UTILS_PLL_OUTPUT_MAX 48000000U /*!< Frequency max for PLL output, in Hz */ |
||
| 48 | |||
| 49 | /* Defines used for HSE range */ |
||
| 50 | #define UTILS_HSE_FREQUENCY_MIN 4000000U /*!< Frequency min for HSE frequency, in Hz */ |
||
| 51 | #define UTILS_HSE_FREQUENCY_MAX 32000000U /*!< Frequency max for HSE frequency, in Hz */ |
||
| 52 | |||
| 53 | /* Defines used for FLASH latency according to SYSCLK Frequency */ |
||
| 54 | #define UTILS_LATENCY1_FREQ 24000000U /*!< SYSCLK frequency to set FLASH latency 1 */ |
||
| 55 | /** |
||
| 56 | * @} |
||
| 57 | */ |
||
| 58 | /* Private macros ------------------------------------------------------------*/ |
||
| 59 | /** @addtogroup UTILS_LL_Private_Macros |
||
| 60 | * @{ |
||
| 61 | */ |
||
| 62 | #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \ |
||
| 63 | || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \ |
||
| 64 | || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \ |
||
| 65 | || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \ |
||
| 66 | || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \ |
||
| 67 | || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \ |
||
| 68 | || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \ |
||
| 69 | || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \ |
||
| 70 | || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512)) |
||
| 71 | |||
| 72 | #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \ |
||
| 73 | || ((__VALUE__) == LL_RCC_APB1_DIV_2) \ |
||
| 74 | || ((__VALUE__) == LL_RCC_APB1_DIV_4) \ |
||
| 75 | || ((__VALUE__) == LL_RCC_APB1_DIV_8) \ |
||
| 76 | || ((__VALUE__) == LL_RCC_APB1_DIV_16)) |
||
| 77 | |||
| 78 | #define IS_LL_UTILS_PLLMUL_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLL_MUL_2) \ |
||
| 79 | || ((__VALUE__) == LL_RCC_PLL_MUL_3) \ |
||
| 80 | || ((__VALUE__) == LL_RCC_PLL_MUL_4) \ |
||
| 81 | || ((__VALUE__) == LL_RCC_PLL_MUL_5) \ |
||
| 82 | || ((__VALUE__) == LL_RCC_PLL_MUL_6) \ |
||
| 83 | || ((__VALUE__) == LL_RCC_PLL_MUL_7) \ |
||
| 84 | || ((__VALUE__) == LL_RCC_PLL_MUL_8) \ |
||
| 85 | || ((__VALUE__) == LL_RCC_PLL_MUL_9) \ |
||
| 86 | || ((__VALUE__) == LL_RCC_PLL_MUL_10) \ |
||
| 87 | || ((__VALUE__) == LL_RCC_PLL_MUL_11) \ |
||
| 88 | || ((__VALUE__) == LL_RCC_PLL_MUL_12) \ |
||
| 89 | || ((__VALUE__) == LL_RCC_PLL_MUL_13) \ |
||
| 90 | || ((__VALUE__) == LL_RCC_PLL_MUL_14) \ |
||
| 91 | || ((__VALUE__) == LL_RCC_PLL_MUL_15) \ |
||
| 92 | || ((__VALUE__) == LL_RCC_PLL_MUL_16)) |
||
| 93 | |||
| 94 | #define IS_LL_UTILS_PREDIV_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PREDIV_DIV_1) || ((__VALUE__) == LL_RCC_PREDIV_DIV_2) || \ |
||
| 95 | ((__VALUE__) == LL_RCC_PREDIV_DIV_3) || ((__VALUE__) == LL_RCC_PREDIV_DIV_4) || \ |
||
| 96 | ((__VALUE__) == LL_RCC_PREDIV_DIV_5) || ((__VALUE__) == LL_RCC_PREDIV_DIV_6) || \ |
||
| 97 | ((__VALUE__) == LL_RCC_PREDIV_DIV_7) || ((__VALUE__) == LL_RCC_PREDIV_DIV_8) || \ |
||
| 98 | ((__VALUE__) == LL_RCC_PREDIV_DIV_9) || ((__VALUE__) == LL_RCC_PREDIV_DIV_10) || \ |
||
| 99 | ((__VALUE__) == LL_RCC_PREDIV_DIV_11) || ((__VALUE__) == LL_RCC_PREDIV_DIV_12) || \ |
||
| 100 | ((__VALUE__) == LL_RCC_PREDIV_DIV_13) || ((__VALUE__) == LL_RCC_PREDIV_DIV_14) || \ |
||
| 101 | ((__VALUE__) == LL_RCC_PREDIV_DIV_15) || ((__VALUE__) == LL_RCC_PREDIV_DIV_16)) |
||
| 102 | |||
| 103 | #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((UTILS_PLL_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLL_OUTPUT_MAX)) |
||
| 104 | |||
| 105 | |||
| 106 | #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \ |
||
| 107 | || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF)) |
||
| 108 | |||
| 109 | #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX)) |
||
| 110 | /** |
||
| 111 | * @} |
||
| 112 | */ |
||
| 113 | /* Private function prototypes -----------------------------------------------*/ |
||
| 114 | /** @defgroup UTILS_LL_Private_Functions UTILS Private functions |
||
| 115 | * @{ |
||
| 116 | */ |
||
| 117 | static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, |
||
| 118 | LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct); |
||
| 119 | static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct); |
||
| 120 | static ErrorStatus UTILS_PLL_IsBusy(void); |
||
| 121 | /** |
||
| 122 | * @} |
||
| 123 | */ |
||
| 124 | |||
| 125 | /* Exported functions --------------------------------------------------------*/ |
||
| 126 | /** @addtogroup UTILS_LL_Exported_Functions |
||
| 127 | * @{ |
||
| 128 | */ |
||
| 129 | |||
| 130 | /** @addtogroup UTILS_LL_EF_DELAY |
||
| 131 | * @{ |
||
| 132 | */ |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @brief This function configures the Cortex-M SysTick source to have 1ms time base. |
||
| 136 | * @note When a RTOS is used, it is recommended to avoid changing the Systick |
||
| 137 | * configuration by calling this function, for a delay use rather osDelay RTOS service. |
||
| 138 | * @param HCLKFrequency HCLK frequency in Hz |
||
| 139 | * @note HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq |
||
| 140 | * @retval None |
||
| 141 | */ |
||
| 142 | void LL_Init1msTick(uint32_t HCLKFrequency) |
||
| 143 | { |
||
| 144 | /* Use frequency provided in argument */ |
||
| 145 | LL_InitTick(HCLKFrequency, 1000U); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @brief This function provides accurate delay (in milliseconds) based |
||
| 150 | * on SysTick counter flag |
||
| 151 | * @note When a RTOS is used, it is recommended to avoid using blocking delay |
||
| 152 | * and use rather osDelay service. |
||
| 153 | * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which |
||
| 154 | * will configure Systick to 1ms |
||
| 155 | * @param Delay specifies the delay time length, in milliseconds. |
||
| 156 | * @retval None |
||
| 157 | */ |
||
| 158 | void LL_mDelay(uint32_t Delay) |
||
| 159 | { |
||
| 160 | __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */ |
||
| 161 | /* Add this code to indicate that local variable is not used */ |
||
| 162 | ((void)tmp); |
||
| 163 | |||
| 164 | /* Add a period to guaranty minimum wait */ |
||
| 165 | if (Delay < LL_MAX_DELAY) |
||
| 166 | { |
||
| 167 | Delay++; |
||
| 168 | } |
||
| 169 | |||
| 170 | while (Delay) |
||
| 171 | { |
||
| 172 | if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U) |
||
| 173 | { |
||
| 174 | Delay--; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @} |
||
| 181 | */ |
||
| 182 | |||
| 183 | /** @addtogroup UTILS_EF_SYSTEM |
||
| 184 | * @brief System Configuration functions |
||
| 185 | * |
||
| 186 | @verbatim |
||
| 187 | =============================================================================== |
||
| 188 | ##### System Configuration functions ##### |
||
| 189 | =============================================================================== |
||
| 190 | [..] |
||
| 191 | System, AHB and APB buses clocks configuration |
||
| 192 | |||
| 193 | (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 48000000 Hz. |
||
| 194 | @endverbatim |
||
| 195 | @internal |
||
| 196 | Depending on the SYSCLK frequency, the flash latency should be adapted accordingly: |
||
| 197 | (++) +-----------------------------------------------+ |
||
| 198 | (++) | Latency | SYSCLK clock frequency (MHz) | |
||
| 199 | (++) |---------------|-------------------------------| |
||
| 200 | (++) |0WS(1CPU cycle)| 0 < SYSCLK <= 24 | |
||
| 201 | (++) |---------------|-------------------------------| |
||
| 202 | (++) |1WS(2CPU cycle)| 24 < SYSCLK <= 48 | |
||
| 203 | (++) +-----------------------------------------------+ |
||
| 204 | @endinternal |
||
| 205 | * @{ |
||
| 206 | */ |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @brief This function sets directly SystemCoreClock CMSIS variable. |
||
| 210 | * @note Variable can be calculated also through SystemCoreClockUpdate function. |
||
| 211 | * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro) |
||
| 212 | * @retval None |
||
| 213 | */ |
||
| 214 | void LL_SetSystemCoreClock(uint32_t HCLKFrequency) |
||
| 215 | { |
||
| 216 | /* HCLK clock frequency */ |
||
| 217 | SystemCoreClock = HCLKFrequency; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @brief Update number of Flash wait states in line with new frequency and current |
||
| 222 | voltage range. |
||
| 223 | * @param Frequency SYSCLK frequency |
||
| 224 | * @retval An ErrorStatus enumeration value: |
||
| 225 | * - SUCCESS: Latency has been modified |
||
| 226 | * - ERROR: Latency cannot be modified |
||
| 227 | */ |
||
| 228 | #if defined(FLASH_ACR_LATENCY) |
||
| 229 | ErrorStatus LL_SetFlashLatency(uint32_t Frequency) |
||
| 230 | { |
||
| 231 | uint32_t timeout; |
||
| 232 | uint32_t getlatency; |
||
| 233 | uint32_t latency; |
||
| 234 | ErrorStatus status = SUCCESS; |
||
| 235 | |||
| 236 | /* Frequency cannot be equal to 0 */ |
||
| 237 | if (Frequency == 0U) |
||
| 238 | { |
||
| 239 | status = ERROR; |
||
| 240 | } |
||
| 241 | else |
||
| 242 | { |
||
| 243 | if (Frequency > UTILS_LATENCY1_FREQ) |
||
| 244 | { |
||
| 245 | /* 24 < SYSCLK <= 48 => 1WS (2 CPU cycles) */ |
||
| 246 | latency = LL_FLASH_LATENCY_1; |
||
| 247 | } |
||
| 248 | else |
||
| 249 | { |
||
| 250 | /* else SYSCLK < 24MHz default LL_FLASH_LATENCY_0 0WS */ |
||
| 251 | latency = LL_FLASH_LATENCY_0; |
||
| 252 | } |
||
| 253 | if (status != ERROR) |
||
| 254 | { |
||
| 255 | LL_FLASH_SetLatency(latency); |
||
| 256 | |||
| 257 | /* Check that the new number of wait states is taken into account to access the Flash |
||
| 258 | memory by reading the FLASH_ACR register */ |
||
| 259 | timeout = 2; |
||
| 260 | do |
||
| 261 | { |
||
| 262 | /* Wait for Flash latency to be updated */ |
||
| 263 | getlatency = LL_FLASH_GetLatency(); |
||
| 264 | timeout--; |
||
| 265 | } while ((getlatency != latency) && (timeout > 0)); |
||
| 266 | |||
| 267 | if(getlatency != latency) |
||
| 268 | { |
||
| 269 | status = ERROR; |
||
| 270 | } |
||
| 271 | else |
||
| 272 | { |
||
| 273 | status = SUCCESS; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | return status; |
||
| 279 | } |
||
| 280 | #endif /* FLASH_ACR_LATENCY */ |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @brief This function configures system clock with HSI as clock source of the PLL |
||
| 284 | * @note The application need to ensure that PLL is disabled. |
||
| 285 | * @note Function is based on the following formula: |
||
| 286 | * - PLL output frequency = ((HSI frequency / PREDIV) * PLLMUL) |
||
| 287 | * - PREDIV: Set to 2 for few devices |
||
| 288 | * - PLLMUL: The application software must set correctly the PLL multiplication factor to |
||
| 289 | * be in the range 16-48MHz |
||
| 290 | * @note FLASH latency can be modified through this function. |
||
| 291 | * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains |
||
| 292 | * the configuration information for the PLL. |
||
| 293 | * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains |
||
| 294 | * the configuration information for the BUS prescalers. |
||
| 295 | * @retval An ErrorStatus enumeration value: |
||
| 296 | * - SUCCESS: Max frequency configuration done |
||
| 297 | * - ERROR: Max frequency configuration not done |
||
| 298 | */ |
||
| 299 | ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, |
||
| 300 | LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct) |
||
| 301 | { |
||
| 302 | ErrorStatus status = SUCCESS; |
||
| 303 | uint32_t pllfreq = 0U; |
||
| 304 | |||
| 305 | /* Check if one of the PLL is enabled */ |
||
| 306 | if (UTILS_PLL_IsBusy() == SUCCESS) |
||
| 307 | { |
||
| 308 | #if defined(RCC_PLLSRC_PREDIV1_SUPPORT) |
||
| 309 | /* Check PREDIV value */ |
||
| 310 | assert_param(IS_LL_UTILS_PREDIV_VALUE(UTILS_PLLInitStruct->PLLDiv)); |
||
| 311 | #else |
||
| 312 | /* Force PREDIV value to 2 */ |
||
| 313 | UTILS_PLLInitStruct->Prediv = LL_RCC_PREDIV_DIV_2; |
||
| 314 | #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/ |
||
| 315 | /* Calculate the new PLL output frequency */ |
||
| 316 | pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct); |
||
| 317 | |||
| 318 | /* Enable HSI if not enabled */ |
||
| 319 | if (LL_RCC_HSI_IsReady() != 1U) |
||
| 320 | { |
||
| 321 | LL_RCC_HSI_Enable(); |
||
| 322 | while (LL_RCC_HSI_IsReady() != 1U) |
||
| 323 | { |
||
| 324 | /* Wait for HSI ready */ |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /* Configure PLL */ |
||
| 329 | #if defined(RCC_PLLSRC_PREDIV1_SUPPORT) |
||
| 330 | LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, UTILS_PLLInitStruct->PLLMul, UTILS_PLLInitStruct->PLLDiv); |
||
| 331 | #else |
||
| 332 | LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, UTILS_PLLInitStruct->PLLMul); |
||
| 333 | #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/ |
||
| 334 | |||
| 335 | /* Enable PLL and switch system clock to PLL */ |
||
| 336 | status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct); |
||
| 337 | } |
||
| 338 | else |
||
| 339 | { |
||
| 340 | /* Current PLL configuration cannot be modified */ |
||
| 341 | status = ERROR; |
||
| 342 | } |
||
| 343 | |||
| 344 | return status; |
||
| 345 | } |
||
| 346 | |||
| 347 | #if defined(RCC_CFGR_SW_HSI48) |
||
| 348 | /** |
||
| 349 | * @brief This function configures system clock with HSI48 as clock source of the PLL |
||
| 350 | * @note The application need to ensure that PLL is disabled. |
||
| 351 | * @note Function is based on the following formula: |
||
| 352 | * - PLL output frequency = ((HSI48 frequency / PREDIV) * PLLMUL) |
||
| 353 | * - PLLMUL: The application software must set correctly the PLL multiplication factor to |
||
| 354 | * be in the range 16-48MHz |
||
| 355 | * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains |
||
| 356 | * the configuration information for the PLL. |
||
| 357 | * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains |
||
| 358 | * the configuration information for the BUS prescalers. |
||
| 359 | * @retval An ErrorStatus enumeration value: |
||
| 360 | * - SUCCESS: Max frequency configuration done |
||
| 361 | * - ERROR: Max frequency configuration not done |
||
| 362 | */ |
||
| 363 | ErrorStatus LL_PLL_ConfigSystemClock_HSI48(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, |
||
| 364 | LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct) |
||
| 365 | { |
||
| 366 | ErrorStatus status = SUCCESS; |
||
| 367 | uint32_t pllfreq = 0U; |
||
| 368 | |||
| 369 | /* Check if one of the PLL is enabled */ |
||
| 370 | if (UTILS_PLL_IsBusy() == SUCCESS) |
||
| 371 | { |
||
| 372 | /* Check PREDIV value */ |
||
| 373 | assert_param(IS_LL_UTILS_PREDIV_VALUE(UTILS_PLLInitStruct->PLLDiv)); |
||
| 374 | |||
| 375 | /* Calculate the new PLL output frequency */ |
||
| 376 | pllfreq = UTILS_GetPLLOutputFrequency(HSI48_VALUE, UTILS_PLLInitStruct); |
||
| 377 | |||
| 378 | /* Enable HSI48 if not enabled */ |
||
| 379 | if (LL_RCC_HSI48_IsReady() != 1U) |
||
| 380 | { |
||
| 381 | LL_RCC_HSI48_Enable(); |
||
| 382 | while (LL_RCC_HSI48_IsReady() != 1U) |
||
| 383 | { |
||
| 384 | /* Wait for HSI48 ready */ |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | /* Configure PLL */ |
||
| 389 | LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI48, UTILS_PLLInitStruct->PLLMul, UTILS_PLLInitStruct->PLLDiv); |
||
| 390 | |||
| 391 | /* Enable PLL and switch system clock to PLL */ |
||
| 392 | status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct); |
||
| 393 | } |
||
| 394 | else |
||
| 395 | { |
||
| 396 | /* Current PLL configuration cannot be modified */ |
||
| 397 | status = ERROR; |
||
| 398 | } |
||
| 399 | |||
| 400 | return status; |
||
| 401 | } |
||
| 402 | |||
| 403 | #endif /*RCC_CFGR_SW_HSI48*/ |
||
| 404 | /** |
||
| 405 | * @brief This function configures system clock with HSE as clock source of the PLL |
||
| 406 | * @note The application need to ensure that PLL is disabled. |
||
| 407 | * @note Function is based on the following formula: |
||
| 408 | * - PLL output frequency = ((HSE frequency / PREDIV) * PLLMUL) |
||
| 409 | * - PLLMUL: The application software must set correctly the PLL multiplication factor to |
||
| 410 | * be in the range 16-48MHz |
||
| 411 | * @note FLASH latency can be modified through this function. |
||
| 412 | * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 32000000 |
||
| 413 | * @param HSEBypass This parameter can be one of the following values: |
||
| 414 | * @arg @ref LL_UTILS_HSEBYPASS_ON |
||
| 415 | * @arg @ref LL_UTILS_HSEBYPASS_OFF |
||
| 416 | * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains |
||
| 417 | * the configuration information for the PLL. |
||
| 418 | * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains |
||
| 419 | * the configuration information for the BUS prescalers. |
||
| 420 | * @retval An ErrorStatus enumeration value: |
||
| 421 | * - SUCCESS: Max frequency configuration done |
||
| 422 | * - ERROR: Max frequency configuration not done |
||
| 423 | */ |
||
| 424 | ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass, |
||
| 425 | LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct) |
||
| 426 | { |
||
| 427 | ErrorStatus status = SUCCESS; |
||
| 428 | uint32_t pllfreq = 0U; |
||
| 429 | |||
| 430 | /* Check the parameters */ |
||
| 431 | assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency)); |
||
| 432 | assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass)); |
||
| 433 | |||
| 434 | /* Check if one of the PLL is enabled */ |
||
| 435 | if (UTILS_PLL_IsBusy() == SUCCESS) |
||
| 436 | { |
||
| 437 | /* Check PREDIV value */ |
||
| 438 | #if defined(RCC_PLLSRC_PREDIV1_SUPPORT) |
||
| 439 | assert_param(IS_LL_UTILS_PREDIV_VALUE(UTILS_PLLInitStruct->PLLDiv)); |
||
| 440 | #else |
||
| 441 | assert_param(IS_LL_UTILS_PREDIV_VALUE(UTILS_PLLInitStruct->Prediv)); |
||
| 442 | #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/ |
||
| 443 | |||
| 444 | /* Calculate the new PLL output frequency */ |
||
| 445 | pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct); |
||
| 446 | |||
| 447 | /* Enable HSE if not enabled */ |
||
| 448 | if (LL_RCC_HSE_IsReady() != 1U) |
||
| 449 | { |
||
| 450 | /* Check if need to enable HSE bypass feature or not */ |
||
| 451 | if (HSEBypass == LL_UTILS_HSEBYPASS_ON) |
||
| 452 | { |
||
| 453 | LL_RCC_HSE_EnableBypass(); |
||
| 454 | } |
||
| 455 | else |
||
| 456 | { |
||
| 457 | LL_RCC_HSE_DisableBypass(); |
||
| 458 | } |
||
| 459 | |||
| 460 | /* Enable HSE */ |
||
| 461 | LL_RCC_HSE_Enable(); |
||
| 462 | while (LL_RCC_HSE_IsReady() != 1U) |
||
| 463 | { |
||
| 464 | /* Wait for HSE ready */ |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | /* Configure PLL */ |
||
| 469 | #if defined(RCC_PLLSRC_PREDIV1_SUPPORT) |
||
| 470 | LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, UTILS_PLLInitStruct->PLLMul, UTILS_PLLInitStruct->PLLDiv); |
||
| 471 | #else |
||
| 472 | LL_RCC_PLL_ConfigDomain_SYS((RCC_CFGR_PLLSRC_HSE_PREDIV | UTILS_PLLInitStruct->Prediv), UTILS_PLLInitStruct->PLLMul); |
||
| 473 | #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/ |
||
| 474 | |||
| 475 | /* Enable PLL and switch system clock to PLL */ |
||
| 476 | status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct); |
||
| 477 | } |
||
| 478 | else |
||
| 479 | { |
||
| 480 | /* Current PLL configuration cannot be modified */ |
||
| 481 | status = ERROR; |
||
| 482 | } |
||
| 483 | |||
| 484 | return status; |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @} |
||
| 489 | */ |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @} |
||
| 493 | */ |
||
| 494 | |||
| 495 | /** @addtogroup UTILS_LL_Private_Functions |
||
| 496 | * @{ |
||
| 497 | */ |
||
| 498 | /** |
||
| 499 | * @brief Function to check that PLL can be modified |
||
| 500 | * @param PLL_InputFrequency PLL input frequency (in Hz) |
||
| 501 | * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains |
||
| 502 | * the configuration information for the PLL. |
||
| 503 | * @retval PLL output frequency (in Hz) |
||
| 504 | */ |
||
| 505 | static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct) |
||
| 506 | { |
||
| 507 | uint32_t pllfreq = 0U; |
||
| 508 | |||
| 509 | /* Check the parameters */ |
||
| 510 | assert_param(IS_LL_UTILS_PLLMUL_VALUE(UTILS_PLLInitStruct->PLLMul)); |
||
| 511 | |||
| 512 | /* Check different PLL parameters according to RM */ |
||
| 513 | /* The application software must set correctly the PLL multiplication factor to |
||
| 514 | be in the range 16-48MHz */ |
||
| 515 | #if defined(RCC_PLLSRC_PREDIV1_SUPPORT) |
||
| 516 | pllfreq = __LL_RCC_CALC_PLLCLK_FREQ(PLL_InputFrequency, UTILS_PLLInitStruct->PLLMul, UTILS_PLLInitStruct->PLLDiv); |
||
| 517 | #else |
||
| 518 | pllfreq = __LL_RCC_CALC_PLLCLK_FREQ(PLL_InputFrequency / (UTILS_PLLInitStruct->Prediv + 1U), UTILS_PLLInitStruct->PLLMul); |
||
| 519 | #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/ |
||
| 520 | assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq)); |
||
| 521 | |||
| 522 | return pllfreq; |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @brief Function to check that PLL can be modified |
||
| 527 | * @retval An ErrorStatus enumeration value: |
||
| 528 | * - SUCCESS: PLL modification can be done |
||
| 529 | * - ERROR: PLL is busy |
||
| 530 | */ |
||
| 531 | static ErrorStatus UTILS_PLL_IsBusy(void) |
||
| 532 | { |
||
| 533 | ErrorStatus status = SUCCESS; |
||
| 534 | |||
| 535 | /* Check if PLL is busy*/ |
||
| 536 | if (LL_RCC_PLL_IsReady() != 0U) |
||
| 537 | { |
||
| 538 | /* PLL configuration cannot be modified */ |
||
| 539 | status = ERROR; |
||
| 540 | } |
||
| 541 | |||
| 542 | return status; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @brief Function to enable PLL and switch system clock to PLL |
||
| 547 | * @param SYSCLK_Frequency SYSCLK frequency |
||
| 548 | * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains |
||
| 549 | * the configuration information for the BUS prescalers. |
||
| 550 | * @retval An ErrorStatus enumeration value: |
||
| 551 | * - SUCCESS: No problem to switch system to PLL |
||
| 552 | * - ERROR: Problem to switch system to PLL |
||
| 553 | */ |
||
| 554 | static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct) |
||
| 555 | { |
||
| 556 | ErrorStatus status = SUCCESS; |
||
| 557 | uint32_t sysclk_frequency_current = 0U; |
||
| 558 | |||
| 559 | assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider)); |
||
| 560 | assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider)); |
||
| 561 | |||
| 562 | /* Calculate current SYSCLK frequency */ |
||
| 563 | sysclk_frequency_current = (SystemCoreClock << AHBPrescTable[LL_RCC_GetAHBPrescaler() >> RCC_POSITION_HPRE]); |
||
| 564 | |||
| 565 | /* Increasing the number of wait states because of higher CPU frequency */ |
||
| 566 | if (sysclk_frequency_current < SYSCLK_Frequency) |
||
| 567 | { |
||
| 568 | /* Set FLASH latency to highest latency */ |
||
| 569 | status = LL_SetFlashLatency(SYSCLK_Frequency); |
||
| 570 | } |
||
| 571 | |||
| 572 | /* Update system clock configuration */ |
||
| 573 | if (status == SUCCESS) |
||
| 574 | { |
||
| 575 | /* Enable PLL */ |
||
| 576 | LL_RCC_PLL_Enable(); |
||
| 577 | while (LL_RCC_PLL_IsReady() != 1U) |
||
| 578 | { |
||
| 579 | /* Wait for PLL ready */ |
||
| 580 | } |
||
| 581 | |||
| 582 | /* Sysclk activation on the main PLL */ |
||
| 583 | LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider); |
||
| 584 | LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); |
||
| 585 | while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) |
||
| 586 | { |
||
| 587 | /* Wait for system clock switch to PLL */ |
||
| 588 | } |
||
| 589 | |||
| 590 | /* Set APB1 & APB2 prescaler*/ |
||
| 591 | LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider); |
||
| 592 | } |
||
| 593 | |||
| 594 | /* Decreasing the number of wait states because of lower CPU frequency */ |
||
| 595 | if (sysclk_frequency_current > SYSCLK_Frequency) |
||
| 596 | { |
||
| 597 | /* Set FLASH latency to lowest latency */ |
||
| 598 | status = LL_SetFlashLatency(SYSCLK_Frequency); |
||
| 599 | } |
||
| 600 | |||
| 601 | /* Update SystemCoreClock variable */ |
||
| 602 | if (status == SUCCESS) |
||
| 603 | { |
||
| 604 | LL_SetSystemCoreClock(__LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider)); |
||
| 605 | } |
||
| 606 | |||
| 607 | return status; |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @} |
||
| 612 | */ |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @} |
||
| 616 | */ |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @} |
||
| 620 | */ |
||
| 621 | |||
| 622 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |