Updated and Validated
[betaflight.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_ll_utils.c
blob0717d8970abf32dd0a07208d6a10dc02d2515dc9
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_ll_utils.c
4 * @author MCD Application Team
5 * @brief UTILS LL module driver.
6 ******************************************************************************
7 * @attention
9 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
10 * All rights reserved.</center></h2>
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
17 ******************************************************************************
19 /* Includes ------------------------------------------------------------------*/
20 #include "stm32f7xx_ll_utils.h"
21 #include "stm32f7xx_ll_rcc.h"
22 #include "stm32f7xx_ll_system.h"
23 #include "stm32f7xx_ll_pwr.h"
24 #ifdef USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif /* USE_FULL_ASSERT */
30 /** @addtogroup STM32F7xx_LL_Driver
31 * @{
34 /** @addtogroup UTILS_LL
35 * @{
38 /* Private types -------------------------------------------------------------*/
39 /* Private variables ---------------------------------------------------------*/
40 /* Private constants ---------------------------------------------------------*/
41 /** @addtogroup UTILS_LL_Private_Constants
42 * @{
44 #define UTILS_MAX_FREQUENCY_SCALE1 216000000U /*!< Maximum frequency for system clock at power scale1, in Hz */
45 #define UTILS_MAX_FREQUENCY_SCALE2 180000000U /*!< Maximum frequency for system clock at power scale2, in Hz */
46 #define UTILS_MAX_FREQUENCY_SCALE3 144000000U /*!< Maximum frequency for system clock at power scale3, in Hz */
48 /* Defines used for PLL range */
49 #define UTILS_PLLVCO_INPUT_MIN 950000U /*!< Frequency min for PLLVCO input, in Hz */
50 #define UTILS_PLLVCO_INPUT_MAX 2100000U /*!< Frequency max for PLLVCO input, in Hz */
51 #define UTILS_PLLVCO_OUTPUT_MIN 100000000U /*!< Frequency min for PLLVCO output, in Hz */
52 #define UTILS_PLLVCO_OUTPUT_MAX 432000000U /*!< Frequency max for PLLVCO output, in Hz */
54 /* Defines used for HSE range */
55 #define UTILS_HSE_FREQUENCY_MIN 4000000U /*!< Frequency min for HSE frequency, in Hz */
56 #define UTILS_HSE_FREQUENCY_MAX 26000000U /*!< Frequency max for HSE frequency, in Hz */
58 /* Defines used for FLASH latency according to HCLK Frequency */
59 #define UTILS_SCALE1_LATENCY1_FREQ 30000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
60 #define UTILS_SCALE1_LATENCY2_FREQ 60000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
61 #define UTILS_SCALE1_LATENCY3_FREQ 90000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
62 #define UTILS_SCALE1_LATENCY4_FREQ 120000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
63 #define UTILS_SCALE1_LATENCY5_FREQ 150000000U /*!< HCLK frequency to set FLASH latency 5 in power scale 1 */
64 #define UTILS_SCALE1_LATENCY6_FREQ 180000000U /*!< HCLK frequency to set FLASH latency 6 in power scale 1 with over-drive mode */
65 #define UTILS_SCALE1_LATENCY7_FREQ 210000000U /*!< HCLK frequency to set FLASH latency 7 in power scale 1 with over-drive mode */
66 #define UTILS_SCALE2_LATENCY1_FREQ 30000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
67 #define UTILS_SCALE2_LATENCY2_FREQ 60000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
68 #define UTILS_SCALE2_LATENCY3_FREQ 90000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 2 */
69 #define UTILS_SCALE2_LATENCY4_FREQ 120000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 2 */
70 #define UTILS_SCALE2_LATENCY5_FREQ 150000000U /*!< HCLK frequency to set FLASH latency 5 in power scale 2 */
71 #define UTILS_SCALE3_LATENCY1_FREQ 30000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 3 */
72 #define UTILS_SCALE3_LATENCY2_FREQ 60000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 3 */
73 #define UTILS_SCALE3_LATENCY3_FREQ 90000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 3 */
74 #define UTILS_SCALE3_LATENCY4_FREQ 120000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 3 */
75 /**
76 * @}
79 /* Private macros ------------------------------------------------------------*/
80 /** @addtogroup UTILS_LL_Private_Macros
81 * @{
83 #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
84 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
85 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
86 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
87 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
88 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
89 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
90 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
91 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
93 #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
94 || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
95 || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
96 || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
97 || ((__VALUE__) == LL_RCC_APB1_DIV_16))
99 #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
100 || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
101 || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
102 || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
103 || ((__VALUE__) == LL_RCC_APB2_DIV_16))
105 #define IS_LL_UTILS_PLLM_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLM_DIV_2) \
106 || ((__VALUE__) == LL_RCC_PLLM_DIV_3) \
107 || ((__VALUE__) == LL_RCC_PLLM_DIV_4) \
108 || ((__VALUE__) == LL_RCC_PLLM_DIV_5) \
109 || ((__VALUE__) == LL_RCC_PLLM_DIV_6) \
110 || ((__VALUE__) == LL_RCC_PLLM_DIV_7) \
111 || ((__VALUE__) == LL_RCC_PLLM_DIV_8) \
112 || ((__VALUE__) == LL_RCC_PLLM_DIV_9) \
113 || ((__VALUE__) == LL_RCC_PLLM_DIV_10) \
114 || ((__VALUE__) == LL_RCC_PLLM_DIV_11) \
115 || ((__VALUE__) == LL_RCC_PLLM_DIV_12) \
116 || ((__VALUE__) == LL_RCC_PLLM_DIV_13) \
117 || ((__VALUE__) == LL_RCC_PLLM_DIV_14) \
118 || ((__VALUE__) == LL_RCC_PLLM_DIV_15) \
119 || ((__VALUE__) == LL_RCC_PLLM_DIV_16) \
120 || ((__VALUE__) == LL_RCC_PLLM_DIV_17) \
121 || ((__VALUE__) == LL_RCC_PLLM_DIV_18) \
122 || ((__VALUE__) == LL_RCC_PLLM_DIV_19) \
123 || ((__VALUE__) == LL_RCC_PLLM_DIV_20) \
124 || ((__VALUE__) == LL_RCC_PLLM_DIV_21) \
125 || ((__VALUE__) == LL_RCC_PLLM_DIV_22) \
126 || ((__VALUE__) == LL_RCC_PLLM_DIV_23) \
127 || ((__VALUE__) == LL_RCC_PLLM_DIV_24) \
128 || ((__VALUE__) == LL_RCC_PLLM_DIV_25) \
129 || ((__VALUE__) == LL_RCC_PLLM_DIV_26) \
130 || ((__VALUE__) == LL_RCC_PLLM_DIV_27) \
131 || ((__VALUE__) == LL_RCC_PLLM_DIV_28) \
132 || ((__VALUE__) == LL_RCC_PLLM_DIV_29) \
133 || ((__VALUE__) == LL_RCC_PLLM_DIV_30) \
134 || ((__VALUE__) == LL_RCC_PLLM_DIV_31) \
135 || ((__VALUE__) == LL_RCC_PLLM_DIV_32) \
136 || ((__VALUE__) == LL_RCC_PLLM_DIV_33) \
137 || ((__VALUE__) == LL_RCC_PLLM_DIV_34) \
138 || ((__VALUE__) == LL_RCC_PLLM_DIV_35) \
139 || ((__VALUE__) == LL_RCC_PLLM_DIV_36) \
140 || ((__VALUE__) == LL_RCC_PLLM_DIV_37) \
141 || ((__VALUE__) == LL_RCC_PLLM_DIV_38) \
142 || ((__VALUE__) == LL_RCC_PLLM_DIV_39) \
143 || ((__VALUE__) == LL_RCC_PLLM_DIV_40) \
144 || ((__VALUE__) == LL_RCC_PLLM_DIV_41) \
145 || ((__VALUE__) == LL_RCC_PLLM_DIV_42) \
146 || ((__VALUE__) == LL_RCC_PLLM_DIV_43) \
147 || ((__VALUE__) == LL_RCC_PLLM_DIV_44) \
148 || ((__VALUE__) == LL_RCC_PLLM_DIV_45) \
149 || ((__VALUE__) == LL_RCC_PLLM_DIV_46) \
150 || ((__VALUE__) == LL_RCC_PLLM_DIV_47) \
151 || ((__VALUE__) == LL_RCC_PLLM_DIV_48) \
152 || ((__VALUE__) == LL_RCC_PLLM_DIV_49) \
153 || ((__VALUE__) == LL_RCC_PLLM_DIV_50) \
154 || ((__VALUE__) == LL_RCC_PLLM_DIV_51) \
155 || ((__VALUE__) == LL_RCC_PLLM_DIV_52) \
156 || ((__VALUE__) == LL_RCC_PLLM_DIV_53) \
157 || ((__VALUE__) == LL_RCC_PLLM_DIV_54) \
158 || ((__VALUE__) == LL_RCC_PLLM_DIV_55) \
159 || ((__VALUE__) == LL_RCC_PLLM_DIV_56) \
160 || ((__VALUE__) == LL_RCC_PLLM_DIV_57) \
161 || ((__VALUE__) == LL_RCC_PLLM_DIV_58) \
162 || ((__VALUE__) == LL_RCC_PLLM_DIV_59) \
163 || ((__VALUE__) == LL_RCC_PLLM_DIV_60) \
164 || ((__VALUE__) == LL_RCC_PLLM_DIV_61) \
165 || ((__VALUE__) == LL_RCC_PLLM_DIV_62) \
166 || ((__VALUE__) == LL_RCC_PLLM_DIV_63))
168 #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((50 <= (__VALUE__)) && ((__VALUE__) <= 432))
170 #define IS_LL_UTILS_PLLP_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLP_DIV_2) \
171 || ((__VALUE__) == LL_RCC_PLLP_DIV_4) \
172 || ((__VALUE__) == LL_RCC_PLLP_DIV_6) \
173 || ((__VALUE__) == LL_RCC_PLLP_DIV_8))
175 #define IS_LL_UTILS_PLLVCO_INPUT(__VALUE__) ((UTILS_PLLVCO_INPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX))
177 #define IS_LL_UTILS_PLLVCO_OUTPUT(__VALUE__) ((UTILS_PLLVCO_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_OUTPUT_MAX))
179 #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
180 (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
181 ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
183 #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
184 || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
186 #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
188 * @}
190 /* Private function prototypes -----------------------------------------------*/
191 /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
192 * @{
194 static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency,
195 LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
196 static ErrorStatus UTILS_SetFlashLatency(uint32_t HCLK_Frequency);
197 static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
198 static ErrorStatus UTILS_PLL_IsBusy(void);
200 * @}
203 /* Exported functions --------------------------------------------------------*/
204 /** @addtogroup UTILS_LL_Exported_Functions
205 * @{
208 /** @addtogroup UTILS_LL_EF_DELAY
209 * @{
213 * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
214 * @note When a RTOS is used, it is recommended to avoid changing the Systick
215 * configuration by calling this function, for a delay use rather osDelay RTOS service.
216 * @param HCLKFrequency HCLK frequency in Hz
217 * @note HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq
218 * @retval None
220 void LL_Init1msTick(uint32_t HCLKFrequency)
222 /* Use frequency provided in argument */
223 LL_InitTick(HCLKFrequency, 1000U);
227 * @brief This function provides accurate delay (in milliseconds) based
228 * on SysTick counter flag
229 * @note When a RTOS is used, it is recommended to avoid using blocking delay
230 * and use rather osDelay service.
231 * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
232 * will configure Systick to 1ms
233 * @param Delay specifies the delay time length, in milliseconds.
234 * @retval None
236 void LL_mDelay(uint32_t Delay)
238 __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
239 /* Add this code to indicate that local variable is not used */
240 ((void)tmp);
242 /* Add a period to guaranty minimum wait */
243 if(Delay < LL_MAX_DELAY)
245 Delay++;
248 while (Delay)
250 if((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
252 Delay--;
258 * @}
261 /** @addtogroup UTILS_EF_SYSTEM
262 * @brief System Configuration functions
264 @verbatim
265 ===============================================================================
266 ##### System Configuration functions #####
267 ===============================================================================
268 [..]
269 System, AHB and APB buses clocks configuration
271 (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 216000000 Hz.
272 @endverbatim
273 @internal
274 Depending on the device voltage range, the maximum frequency should be
275 adapted accordingly:
276 (++) +------------------------------------------------------------------------------------------------+
277 (++) | Wait states | HCLK clock frequency (MHz) |
278 (++) | |-------------------------------------------------------------------------------|
279 (++) | (Latency) | voltage range | voltage range | voltage range | voltage range |
280 (++) | | 2.7V - 3.6V | 2.4V - 2.7V | 2.1V - 2.7V | 1.8V - 2.1V |
281 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
282 (++) |0WS(1CPU cycle) | 0 < HCLK <= 30 | 0 < HCLK <= 24 | 0 < HCLK <= 22 | 0 < HCLK <= 20 |
283 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
284 (++) |1WS(2CPU cycle) | 30 < HCLK <= 60 | 24 < HCLK <= 48 | 22 < HCLK <= 44 | 20 < HCLK <= 44 |
285 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
286 (++) |2WS(3CPU cycle) | 60 < HCLK <= 90 | 48 < HCLK <= 72 | 44 < HCLK <= 66 | 40 < HCLK <= 60 |
287 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
288 (++) |3WS(4CPU cycle) | 90 < HCLK <= 120 | 72 < HCLK <= 96 | 66 < HCLK <= 88 | 60 < HCLK <= 80 |
289 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
290 (++) |4WS(5CPU cycle) | 120 < HCLK <= 150 | 96 < HCLK <= 120 | 88 < HCLK <= 110 | 80 < HCLK <= 100 |
291 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
292 (++) |5WS(6CPU cycle) | 150 < HCLK <= 180 | 120 < HCLK <= 144 | 110 < HCLK <= 132 | 100 < HCLK <= 120 |
293 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
294 (++) |6WS(7CPU cycle) | 180 < HCLK <= 210 | 144 < HCLK <= 168 | 132 < HCLK <= 154 | 120 < HCLK <= 140 |
295 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
296 (++) |7WS(8CPU cycle) | 210 < HCLK <= 216 | 168 < HCLK <= 192 | 154 < HCLK <= 176 | 140 < HCLK <= 160 |
297 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
298 (++) |8WS(9CPU cycle) | -- | 192 < HCLK <= 216 | 176 < HCLK <= 198 | 160 < HCLK <= 180 |
299 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
300 (++) |9WS(10CPU cycle)| -- | -- | 198 < HCLK <= 216 | -- |
301 (++) +------------------------------------------------------------------------------------------------+
303 @endinternal
304 * @{
308 * @brief This function sets directly SystemCoreClock CMSIS variable.
309 * @note Variable can be calculated also through SystemCoreClockUpdate function.
310 * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
311 * @retval None
313 void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
315 /* HCLK clock frequency */
316 SystemCoreClock = HCLKFrequency;
320 * @brief This function configures system clock at maximum frequency with HSI as clock source of the PLL
321 * @note The application need to ensure that PLL is disabled.
322 * @note Function is based on the following formula:
323 * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
324 * - PLLM: ensure that the VCO input frequency ranges from 0.95 to 2.1 MHz (PLLVCO_input = HSI frequency / PLLM)
325 * - PLLN: ensure that the VCO output frequency is between 100 and 432 MHz (PLLVCO_output = PLLVCO_input * PLLN)
326 * - PLLP: ensure that max frequency at 216000000 Hz is reach (PLLVCO_output / PLLP)
327 * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
328 * the configuration information for the PLL.
329 * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
330 * the configuration information for the BUS prescalers.
331 * @retval An ErrorStatus enumeration value:
332 * - SUCCESS: Max frequency configuration done
333 * - ERROR: Max frequency configuration not done
335 ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
336 LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
338 ErrorStatus status = SUCCESS;
339 uint32_t pllfreq = 0U;
341 /* Check if one of the PLL is enabled */
342 if(UTILS_PLL_IsBusy() == SUCCESS)
344 /* Calculate the new PLL output frequency */
345 pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct);
347 /* Enable HSI if not enabled */
348 if(LL_RCC_HSI_IsReady() != 1U)
350 LL_RCC_HSI_Enable();
351 while (LL_RCC_HSI_IsReady() != 1U)
353 /* Wait for HSI ready */
357 /* Configure PLL */
358 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
359 UTILS_PLLInitStruct->PLLP);
361 /* Enable PLL and switch system clock to PLL */
362 status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
364 else
366 /* Current PLL configuration cannot be modified */
367 status = ERROR;
370 return status;
374 * @brief This function configures system clock with HSE as clock source of the PLL
375 * @note The application need to ensure that PLL is disabled.
376 * @note Function is based on the following formula:
377 * - PLL output frequency = (((HSE frequency / PLLM) * PLLN) / PLLP)
378 * - PLLM: ensure that the VCO input frequency ranges from 0.95 to 2.10 MHz (PLLVCO_input = HSE frequency / PLLM)
379 * - PLLN: ensure that the VCO output frequency is between 100 and 432 MHz (PLLVCO_output = PLLVCO_input * PLLN)
380 * - PLLP: ensure that max frequency at 216000000 Hz is reached (PLLVCO_output / PLLP)
381 * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 26000000
382 * @param HSEBypass This parameter can be one of the following values:
383 * @arg @ref LL_UTILS_HSEBYPASS_ON
384 * @arg @ref LL_UTILS_HSEBYPASS_OFF
385 * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
386 * the configuration information for the PLL.
387 * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
388 * the configuration information for the BUS prescalers.
389 * @retval An ErrorStatus enumeration value:
390 * - SUCCESS: Max frequency configuration done
391 * - ERROR: Max frequency configuration not done
393 ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
394 LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
396 ErrorStatus status = SUCCESS;
397 uint32_t pllfreq = 0U;
399 /* Check the parameters */
400 assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
401 assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
403 /* Check if one of the PLL is enabled */
404 if(UTILS_PLL_IsBusy() == SUCCESS)
406 /* Calculate the new PLL output frequency */
407 pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
409 /* Enable HSE if not enabled */
410 if(LL_RCC_HSE_IsReady() != 1U)
412 /* Check if need to enable HSE bypass feature or not */
413 if(HSEBypass == LL_UTILS_HSEBYPASS_ON)
415 LL_RCC_HSE_EnableBypass();
417 else
419 LL_RCC_HSE_DisableBypass();
422 /* Enable HSE */
423 LL_RCC_HSE_Enable();
424 while (LL_RCC_HSE_IsReady() != 1U)
426 /* Wait for HSE ready */
430 /* Configure PLL */
431 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
432 UTILS_PLLInitStruct->PLLP);
434 /* Enable PLL and switch system clock to PLL */
435 status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
437 else
439 /* Current PLL configuration cannot be modified */
440 status = ERROR;
443 return status;
447 * @}
451 * @}
454 /** @addtogroup UTILS_LL_Private_Functions
455 * @{
458 * @brief Update number of Flash wait states in line with new frequency and current
459 voltage range.
460 * @note This Function support ONLY devices with supply voltage (voltage range) between 2.7V and 3.6V
461 * @param HCLK_Frequency HCLK frequency
462 * @retval An ErrorStatus enumeration value:
463 * - SUCCESS: Latency has been modified
464 * - ERROR: Latency cannot be modified
466 static ErrorStatus UTILS_SetFlashLatency(uint32_t HCLK_Frequency)
468 ErrorStatus status = SUCCESS;
470 uint32_t latency = LL_FLASH_LATENCY_0; /* default value 0WS */
472 /* Frequency cannot be equal to 0 */
473 if(HCLK_Frequency == 0U)
475 status = ERROR;
477 else
479 if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1)
481 if(LL_PWR_IsEnabledOverDriveMode() != 0U)
483 if(HCLK_Frequency > UTILS_SCALE1_LATENCY7_FREQ)
485 /* 210 < HCLK <= 216 => 7WS (8 CPU cycles) */
486 latency = LL_FLASH_LATENCY_7;
488 else /* (HCLK_Frequency > UTILS_SCALE1_LATENCY6_FREQ) */
490 /* 180 < HCLK <= 210 => 6WS (7 CPU cycles) */
491 latency = LL_FLASH_LATENCY_6;
494 if((HCLK_Frequency > UTILS_SCALE1_LATENCY5_FREQ) && (latency == LL_FLASH_LATENCY_0))
496 /* 150 < HCLK <= 180 => 5WS (6 CPU cycles) */
497 latency = LL_FLASH_LATENCY_5;
499 else if((HCLK_Frequency > UTILS_SCALE1_LATENCY4_FREQ) && (latency == LL_FLASH_LATENCY_0))
501 /* 120 < HCLK <= 150 => 4WS (5 CPU cycles) */
502 latency = LL_FLASH_LATENCY_4;
504 else if((HCLK_Frequency > UTILS_SCALE1_LATENCY3_FREQ) && (latency == LL_FLASH_LATENCY_0))
506 /* 90 < HCLK <= 120 => 3WS (4 CPU cycles) */
507 latency = LL_FLASH_LATENCY_3;
509 else if((HCLK_Frequency > UTILS_SCALE1_LATENCY2_FREQ) && (latency == LL_FLASH_LATENCY_0))
511 /* 60 < HCLK <= 90 => 2WS (3 CPU cycles) */
512 latency = LL_FLASH_LATENCY_2;
514 else
516 if((HCLK_Frequency > UTILS_SCALE1_LATENCY1_FREQ) && (latency == LL_FLASH_LATENCY_0))
518 /* 30 < HCLK <= 60 => 1WS (2 CPU cycles) */
519 latency = LL_FLASH_LATENCY_1;
521 /* else HCLK_Frequency < 30MHz default LL_FLASH_LATENCY_0 0WS */
524 else if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2)
526 if(HCLK_Frequency > UTILS_SCALE2_LATENCY5_FREQ)
528 /* 150 < HCLK <= 168 OR 150 < HCLK <= 180 (when OverDrive mode is enable) => 5WS (6 CPU cycles) */
529 latency = LL_FLASH_LATENCY_5;
531 else if(HCLK_Frequency > UTILS_SCALE2_LATENCY4_FREQ)
533 /* 120 < HCLK <= 150 => 4WS (5 CPU cycles) */
534 latency = LL_FLASH_LATENCY_4;
536 else if(HCLK_Frequency > UTILS_SCALE2_LATENCY3_FREQ)
538 /* 90 < HCLK <= 120 => 3WS (4 CPU cycles) */
539 latency = LL_FLASH_LATENCY_3;
541 else if(HCLK_Frequency > UTILS_SCALE2_LATENCY2_FREQ)
543 /* 60 < HCLK <= 90 => 2WS (3 CPU cycles) */
544 latency = LL_FLASH_LATENCY_2;
546 else
548 if(HCLK_Frequency > UTILS_SCALE2_LATENCY1_FREQ)
550 /* 30 < HCLK <= 60 => 1WS (2 CPU cycles) */
551 latency = LL_FLASH_LATENCY_1;
553 /* else HCLK_Frequency < 24MHz default LL_FLASH_LATENCY_0 0WS */
556 else /* Scale 3 */
558 if(HCLK_Frequency > UTILS_SCALE3_LATENCY4_FREQ)
560 /* 120 < HCLK <= 144 => 4WS (5 CPU cycles) */
561 latency = LL_FLASH_LATENCY_4;
563 else if(HCLK_Frequency > UTILS_SCALE3_LATENCY3_FREQ)
565 /* 90 < HCLK <= 120 => 3WS (4 CPU cycles) */
566 latency = LL_FLASH_LATENCY_3;
568 else if(HCLK_Frequency > UTILS_SCALE3_LATENCY2_FREQ)
570 /* 60 < HCLK <= 90 => 2WS (3 CPU cycles) */
571 latency = LL_FLASH_LATENCY_2;
573 else
575 if(HCLK_Frequency > UTILS_SCALE3_LATENCY1_FREQ)
577 /* 30 < HCLK <= 60 => 1WS (2 CPU cycles) */
578 latency = LL_FLASH_LATENCY_1;
580 /* else HCLK_Frequency < 22MHz default LL_FLASH_LATENCY_0 0WS */
584 LL_FLASH_SetLatency(latency);
586 /* Check that the new number of wait states is taken into account to access the Flash
587 memory by reading the FLASH_ACR register */
588 if(LL_FLASH_GetLatency() != latency)
590 status = ERROR;
593 return status;
597 * @brief Function to check that PLL can be modified
598 * @param PLL_InputFrequency PLL input frequency (in Hz)
599 * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
600 * the configuration information for the PLL.
601 * @retval PLL output frequency (in Hz)
603 static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
605 uint32_t pllfreq = 0U;
607 /* Check the parameters */
608 assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
609 assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
610 assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct->PLLP));
612 /* Check different PLL parameters according to RM */
613 /* - PLLM: ensure that the VCO input frequency ranges from 0.95 to 2.1 MHz. */
614 pllfreq = PLL_InputFrequency / (UTILS_PLLInitStruct->PLLM & (RCC_PLLCFGR_PLLM >> RCC_PLLCFGR_PLLM_Pos));
615 assert_param(IS_LL_UTILS_PLLVCO_INPUT(pllfreq));
617 /* - PLLN: ensure that the VCO output frequency is between 100 and 432 MHz.*/
618 pllfreq = pllfreq * (UTILS_PLLInitStruct->PLLN & (RCC_PLLCFGR_PLLN >> RCC_PLLCFGR_PLLN_Pos));
619 assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(pllfreq));
621 /* - PLLP: ensure that max frequency at 216000000 Hz is reached */
622 pllfreq = pllfreq / (((UTILS_PLLInitStruct->PLLP >> RCC_PLLCFGR_PLLP_Pos) + 1) * 2);
623 assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq));
625 return pllfreq;
629 * @brief Function to check that PLL can be modified
630 * @retval An ErrorStatus enumeration value:
631 * - SUCCESS: PLL modification can be done
632 * - ERROR: PLL is busy
634 static ErrorStatus UTILS_PLL_IsBusy(void)
636 ErrorStatus status = SUCCESS;
638 /* Check if PLL is busy*/
639 if(LL_RCC_PLL_IsReady() != 0U)
641 /* PLL configuration cannot be modified */
642 status = ERROR;
645 /* Check if PLLSAI is busy*/
646 if(LL_RCC_PLLSAI_IsReady() != 0U)
648 /* PLLSAI1 configuration cannot be modified */
649 status = ERROR;
651 /* Check if PLLI2S is busy*/
652 if(LL_RCC_PLLI2S_IsReady() != 0U)
654 /* PLLI2S configuration cannot be modified */
655 status = ERROR;
657 return status;
661 * @brief Function to enable PLL and switch system clock to PLL
662 * @param SYSCLK_Frequency SYSCLK frequency
663 * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
664 * the configuration information for the BUS prescalers.
665 * @retval An ErrorStatus enumeration value:
666 * - SUCCESS: No problem to switch system to PLL
667 * - ERROR: Problem to switch system to PLL
669 static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
671 ErrorStatus status = SUCCESS;
672 uint32_t hclk_frequency = 0U;
674 assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
675 assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
676 assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct->APB2CLKDivider));
678 /* Calculate HCLK frequency */
679 hclk_frequency = __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider);
681 /* Increasing the number of wait states because of higher CPU frequency */
682 if(SystemCoreClock < hclk_frequency)
684 /* Set FLASH latency to highest latency */
685 status = UTILS_SetFlashLatency(hclk_frequency);
688 /* Update system clock configuration */
689 if(status == SUCCESS)
691 /* Enable PLL */
692 LL_RCC_PLL_Enable();
693 while (LL_RCC_PLL_IsReady() != 1U)
695 /* Wait for PLL ready */
698 /* Sysclk activation on the main PLL */
699 LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
700 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
701 while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
703 /* Wait for system clock switch to PLL */
706 /* Set APB1 & APB2 prescaler*/
707 LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
708 LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct->APB2CLKDivider);
711 /* Decreasing the number of wait states because of lower CPU frequency */
712 if(SystemCoreClock > hclk_frequency)
714 /* Set FLASH latency to lowest latency */
715 status = UTILS_SetFlashLatency(hclk_frequency);
718 /* Update SystemCoreClock variable */
719 if(status == SUCCESS)
721 LL_SetSystemCoreClock(hclk_frequency);
724 return status;
728 * @}
732 * @}
736 * @}
739 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/