2 ******************************************************************************
3 * @file stm32h7xx_ll_utils.c
4 * @author MCD Application Team
5 * @brief UTILS LL module driver.
6 ******************************************************************************
9 * <h2><center>© 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 "stm32h7xx_ll_utils.h"
21 #include "stm32h7xx_ll_rcc.h"
22 #include "stm32h7xx_ll_pwr.h"
24 #ifdef USE_FULL_ASSERT
25 #include "stm32_assert.h"
27 #define assert_param(expr) ((void)0U)
28 #endif /* USE_FULL_ASSERT */
30 /** @addtogroup STM32H7xx_LL_Driver
34 /** @addtogroup UTILS_LL
38 /* Private types -------------------------------------------------------------*/
39 /* Private variables ---------------------------------------------------------*/
40 /* Private constants ---------------------------------------------------------*/
41 /** @addtogroup UTILS_LL_Private_Constants
44 #if (STM32H7_DEV_ID == 0x450UL)
45 #define UTILS_MAX_FREQUENCY_SCALE1 480000000U /*!< Maximum frequency for system clock at power scale1, in Hz */
46 #define UTILS_MAX_FREQUENCY_SCALE2 300000000U /*!< Maximum frequency for system clock at power scale2, in Hz */
47 #define UTILS_MAX_FREQUENCY_SCALE3 200000000U /*!< Maximum frequency for system clock at power scale3, in Hz */
48 #elif (STM32H7_DEV_ID == 0x480UL)
49 #define UTILS_MAX_FREQUENCY_SCALE0 280000000U /*!< Maximum frequency for system clock at power scale0, in Hz */
50 #define UTILS_MAX_FREQUENCY_SCALE1 225000000U /*!< Maximum frequency for system clock at power scale1, in Hz */
51 #define UTILS_MAX_FREQUENCY_SCALE2 160000000U /*!< Maximum frequency for system clock at power scale2, in Hz */
52 #define UTILS_MAX_FREQUENCY_SCALE3 88000000U /*!< Maximum frequency for system clock at power scale3, in Hz */
53 #elif (STM32H7_DEV_ID == 0x483UL)
54 #define UTILS_MAX_FREQUENCY_SCALE0 550000000U /*!< Maximum frequency for system clock at power scale0, in Hz */
55 #define UTILS_MAX_FREQUENCY_SCALE1 200000000U /*!< Maximum frequency for system clock at power scale1, in Hz */
56 #define UTILS_MAX_FREQUENCY_SCALE2 150000000U /*!< Maximum frequency for system clock at power scale2, in Hz */
57 #define UTILS_MAX_FREQUENCY_SCALE3 85000000U /*!< Maximum frequency for system clock at power scale3, in Hz */
58 #endif /*STM32H7_DEV_ID == 0x450UL*/
60 /* Defines used for PLL range */
61 #define UTILS_PLLVCO_INPUT_MIN1 1000000U /*!< Frequency min for the low range PLLVCO input, in Hz */
62 #define UTILS_PLLVCO_INPUT_MAX1 2000000U /*!< Frequency max for the wide range PLLVCO input, in Hz */
63 #define UTILS_PLLVCO_INPUT_MIN2 2000000U /*!< Frequency min for the low range PLLVCO input, in Hz */
64 #define UTILS_PLLVCO_INPUT_MAX2 4000000U /*!< Frequency max for the wide range PLLVCO input, in Hz */
65 #define UTILS_PLLVCO_INPUT_MIN3 4000000U /*!< Frequency min for the low range PLLVCO input, in Hz */
66 #define UTILS_PLLVCO_INPUT_MAX3 8000000U /*!< Frequency max for the wide range PLLVCO input, in Hz */
67 #define UTILS_PLLVCO_INPUT_MIN4 8000000U /*!< Frequency min for the low range PLLVCO input, in Hz */
68 #define UTILS_PLLVCO_INPUT_MAX4 16000000U /*!< Frequency max for the wide range PLLVCO input, in Hz */
70 #if (POWER_DOMAINS_NUMBER == 3U)
71 #define UTILS_PLLVCO_MEDIUM_OUTPUT_MIN 150000000U /*!< Frequency min for the medium range PLLVCO output, in Hz */
72 #define UTILS_PLLVCO_WIDE_OUTPUT_MIN 192000000U /*!< Frequency min for the wide range PLLVCO output, in Hz */
73 #define UTILS_PLLVCO_MEDIUM_OUTPUT_MAX 420000000U /*!< Frequency max for the medium range PLLVCO output, in Hz */
74 #define UTILS_PLLVCO_WIDE_OUTPUT_MAX 836000000U /*!< Frequency max for the wide range PLLVCO output, in Hz */
76 #define UTILS_PLLVCO_MEDIUM_OUTPUT_MIN 150000000U /*!< Frequency min for the medium range PLLVCO output, in Hz */
77 #define UTILS_PLLVCO_WIDE_OUTPUT_MIN 128000000U /*!< Frequency min for the wide range PLLVCO output, in Hz */
78 #define UTILS_PLLVCO_MEDIUM_OUTPUT_MAX 420000000U /*!< Frequency max for the medium range PLLVCO output, in Hz */
79 #define UTILS_PLLVCO_WIDE_OUTPUT_MAX 560000000U /*!< Frequency max for the wide range PLLVCO output, in Hz */
80 #endif /*POWER_DOMAINS_NUMBER == 3U*/
82 /* Defines used for HSE range */
83 #define UTILS_HSE_FREQUENCY_MIN 4000000U /*!< Frequency min for HSE frequency, in Hz */
84 #define UTILS_HSE_FREQUENCY_MAX 48000000U /*!< Frequency max for HSE frequency, in Hz */
86 /* Defines used for FLASH latency according to HCLK Frequency */
87 #if (STM32H7_DEV_ID == 0x480UL)
88 #define UTILS_SCALE0_LATENCY0_FREQ 44000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 0 */
89 #define UTILS_SCALE0_LATENCY1_FREQ 88000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 0 */
90 #define UTILS_SCALE0_LATENCY2_FREQ 132000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 0 */
91 #define UTILS_SCALE0_LATENCY3_FREQ 176000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 0 */
92 #define UTILS_SCALE0_LATENCY4_FREQ 220000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 0 */
93 #define UTILS_SCALE0_LATENCY5_FREQ 264000000U /*!< HCLK frequency to set FLASH latency 5 in power scale 0 */
94 #define UTILS_SCALE0_LATENCY6_FREQ 280000000U /*!< HCLK frequency to set FLASH latency 6 in power scale 0 */
96 #define UTILS_SCALE1_LATENCY0_FREQ 42000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 1 */
97 #define UTILS_SCALE1_LATENCY1_FREQ 84000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
98 #define UTILS_SCALE1_LATENCY2_FREQ 126000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
99 #define UTILS_SCALE1_LATENCY3_FREQ 168000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
100 #define UTILS_SCALE1_LATENCY4_FREQ 210000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
101 #define UTILS_SCALE1_LATENCY5_FREQ 225000000U /*!< HCLK frequency to set FLASH latency 5 in power scale 1 */
103 #define UTILS_SCALE2_LATENCY0_FREQ 34000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 2 */
104 #define UTILS_SCALE2_LATENCY1_FREQ 68000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
105 #define UTILS_SCALE2_LATENCY2_FREQ 102000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
106 #define UTILS_SCALE2_LATENCY3_FREQ 136000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 2 */
107 #define UTILS_SCALE2_LATENCY4_FREQ 160000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 2 */
109 #define UTILS_SCALE3_LATENCY0_FREQ 22000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 3 */
110 #define UTILS_SCALE3_LATENCY1_FREQ 44000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 3 */
111 #define UTILS_SCALE3_LATENCY2_FREQ 66000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 3 */
112 #define UTILS_SCALE3_LATENCY3_FREQ 88000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 3 */
114 #elif (STM32H7_DEV_ID == 0x450UL)
116 #define UTILS_SCALE1_LATENCY0_FREQ 70000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 1 */
117 #define UTILS_SCALE1_LATENCY1_FREQ 140000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
118 #define UTILS_SCALE1_LATENCY2_FREQ 240000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
120 #define UTILS_SCALE2_LATENCY0_FREQ 55000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 2 */
121 #define UTILS_SCALE2_LATENCY1_FREQ 110000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
122 #define UTILS_SCALE2_LATENCY2_FREQ 165000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
123 #define UTILS_SCALE2_LATENCY3_FREQ 220000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 2 */
125 #define UTILS_SCALE3_LATENCY0_FREQ 45000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 3 */
126 #define UTILS_SCALE3_LATENCY1_FREQ 90000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 3 */
127 #define UTILS_SCALE3_LATENCY2_FREQ 135000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 3 */
128 #define UTILS_SCALE3_LATENCY3_FREQ 180000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 3 */
129 #define UTILS_SCALE3_LATENCY4_FREQ 225000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 3 */
131 #elif (STM32H7_DEV_ID == 0x483UL)
133 #define UTILS_SCALE0_LATENCY0_FREQ 70000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 0 */
134 #define UTILS_SCALE0_LATENCY1_FREQ 140000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 0 */
135 #define UTILS_SCALE0_LATENCY2_FREQ 210000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 0 */
136 #define UTILS_SCALE0_LATENCY3_FREQ 275000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 0 */
138 #define UTILS_SCALE1_LATENCY0_FREQ 67000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 1 */
139 #define UTILS_SCALE1_LATENCY1_FREQ 133000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
140 #define UTILS_SCALE1_LATENCY2_FREQ 200000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
142 #define UTILS_SCALE2_LATENCY0_FREQ 50000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 2 */
143 #define UTILS_SCALE2_LATENCY1_FREQ 100000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
144 #define UTILS_SCALE2_LATENCY2_FREQ 150000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
146 #define UTILS_SCALE3_LATENCY0_FREQ 35000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 3 */
147 #define UTILS_SCALE3_LATENCY1_FREQ 70000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 3 */
148 #define UTILS_SCALE3_LATENCY2_FREQ 85000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 3 */
150 #endif /*STM32H7_DEV_ID == 0x480UL*/
155 /* Private macros ------------------------------------------------------------*/
156 /** @addtogroup UTILS_LL_Private_Macros
159 #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
160 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
161 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
162 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
163 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
164 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
165 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
166 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
167 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
169 #define IS_LL_UTILS_AHB_DIV(__VALUE__) (((__VALUE__) == LL_RCC_AHB_DIV_1) \
170 || ((__VALUE__) == LL_RCC_AHB_DIV_2) \
171 || ((__VALUE__) == LL_RCC_AHB_DIV_4) \
172 || ((__VALUE__) == LL_RCC_AHB_DIV_8) \
173 || ((__VALUE__) == LL_RCC_AHB_DIV_16) \
174 || ((__VALUE__) == LL_RCC_AHB_DIV_64) \
175 || ((__VALUE__) == LL_RCC_AHB_DIV_128) \
176 || ((__VALUE__) == LL_RCC_AHB_DIV_256) \
177 || ((__VALUE__) == LL_RCC_AHB_DIV_512))
179 #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
180 || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
181 || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
182 || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
183 || ((__VALUE__) == LL_RCC_APB1_DIV_16))
185 #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
186 || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
187 || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
188 || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
189 || ((__VALUE__) == LL_RCC_APB2_DIV_16))
191 #define IS_LL_UTILS_APB3_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB3_DIV_1) \
192 || ((__VALUE__) == LL_RCC_APB3_DIV_2) \
193 || ((__VALUE__) == LL_RCC_APB3_DIV_4) \
194 || ((__VALUE__) == LL_RCC_APB3_DIV_8) \
195 || ((__VALUE__) == LL_RCC_APB3_DIV_16))
197 #define IS_LL_UTILS_APB4_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB4_DIV_1) \
198 || ((__VALUE__) == LL_RCC_APB4_DIV_2) \
199 || ((__VALUE__) == LL_RCC_APB4_DIV_4) \
200 || ((__VALUE__) == LL_RCC_APB4_DIV_8) \
201 || ((__VALUE__) == LL_RCC_APB4_DIV_16))
203 #define IS_LL_UTILS_PLLM_VALUE(__VALUE__) ((1U <= (__VALUE__)) && ((__VALUE__) <= 63U))
205 #if (POWER_DOMAINS_NUMBER == 3U)
206 #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((4U <= (__VALUE__)) && ((__VALUE__) <= 512U))
208 #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((8U <= (__VALUE__)) && ((__VALUE__) <= 420U))
209 #endif /*POWER_DOMAINS_NUMBER == 3U*/
211 #define IS_LL_UTILS_PLLP_VALUE(__VALUE__) ((1U <= (__VALUE__)) && ((__VALUE__) <= 128U))
213 #define IS_LL_UTILS_FRACN_VALUE(__VALUE__) ((__VALUE__) <= 0x1FFFU)
215 #define IS_LL_UTILS_PLLVCO_INPUT(__VALUE__, __RANGE__) ( \
216 (((__RANGE__) == LL_RCC_PLLINPUTRANGE_1_2) && (UTILS_PLLVCO_INPUT_MIN1 <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX1)) || \
217 (((__RANGE__) == LL_RCC_PLLINPUTRANGE_2_4) && (UTILS_PLLVCO_INPUT_MIN2 <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX2)) || \
218 (((__RANGE__) == LL_RCC_PLLINPUTRANGE_4_8) && (UTILS_PLLVCO_INPUT_MIN3 <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX3)) || \
219 (((__RANGE__) == LL_RCC_PLLINPUTRANGE_8_16) && (UTILS_PLLVCO_INPUT_MIN4 <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX4)))
221 #define IS_LL_UTILS_PLLVCO_OUTPUT(__VALUE__, __RANGE__) ( \
222 (((__RANGE__) == LL_RCC_PLLVCORANGE_MEDIUM) && (UTILS_PLLVCO_MEDIUM_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_MEDIUM_OUTPUT_MAX)) || \
223 (((__RANGE__) == LL_RCC_PLLVCORANGE_WIDE) && (UTILS_PLLVCO_WIDE_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_WIDE_OUTPUT_MAX)))
225 #define IS_LL_UTILS_CHECK_VCO_RANGES(__RANGEIN__, __RANGEOUT__) ( \
226 (((__RANGEIN__) == LL_RCC_PLLINPUTRANGE_1_2) && ((__RANGEOUT__) == LL_RCC_PLLVCORANGE_MEDIUM)) || \
227 (((__RANGEIN__) != LL_RCC_PLLINPUTRANGE_1_2) && ((__RANGEOUT__) == LL_RCC_PLLVCORANGE_WIDE)))
229 #if (STM32H7_DEV_ID == 0x450UL)
230 #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
231 (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
232 ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
234 #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE0) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE0) : \
235 (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
236 (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
237 ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
238 #endif /* STM32H7_DEV_ID == 0x450UL */
240 #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
241 || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
243 #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
247 /* Private function prototypes -----------------------------------------------*/
248 /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
251 static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency
, LL_UTILS_PLLInitTypeDef
*UTILS_PLLInitStruct
);
252 static ErrorStatus
UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency
, LL_UTILS_ClkInitTypeDef
*UTILS_ClkInitStruct
);
253 static ErrorStatus
UTILS_IsPLLsReady(void);
258 /* Exported functions --------------------------------------------------------*/
259 /** @addtogroup UTILS_LL_Exported_Functions
263 /** @addtogroup UTILS_LL_EF_DELAY
266 #if defined (DUAL_CORE)
268 * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
269 * @note When a RTOS is used, it is recommended to avoid changing the Systick
270 * configuration by calling this function, for a delay use rather osDelay RTOS service.
271 * @param CPU_Frequency Core frequency in Hz
272 * @note CPU_Frequency can be calculated thanks to RCC helper macro or function
273 * @ref LL_RCC_GetSystemClocksFreq
274 * LL_RCC_GetSystemClocksFreq() is used to calculate the CM7 clock frequency
275 * and __LL_RCC_CALC_HCLK_FREQ is used to caluclate the CM4 clock frequency.
280 * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
281 * @note When a RTOS is used, it is recommended to avoid changing the Systick
282 * configuration by calling this function, for a delay use rather osDelay RTOS service.
283 * @param CPU_Frequency Core frequency in Hz
284 * @note CPU_Frequency can be calculated thanks to RCC helper macro or function
285 * @ref LL_RCC_GetSystemClocksFreq
288 #endif /* DUAL_CORE */
289 void LL_Init1msTick(uint32_t CPU_Frequency
)
291 /* Use frequency provided in argument */
292 LL_InitTick(CPU_Frequency
, 1000U);
297 * @brief This function provides accurate delay (in milliseconds) based
298 * on SysTick counter flag
299 * @note When a RTOS is used, it is recommended to avoid using blocking delay
300 * and use rather osDelay service.
301 * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
302 * will configure Systick to 1ms
303 * @param Delay specifies the delay time length, in milliseconds.
306 void LL_mDelay(uint32_t Delay
)
308 uint32_t count
= Delay
;
309 __IO
uint32_t tmp
= SysTick
->CTRL
; /* Clear the COUNTFLAG first */
310 /* Add this code to indicate that local variable is not used */
313 /* Add a period to guaranty minimum wait */
314 if(count
< LL_MAX_DELAY
)
321 if((SysTick
->CTRL
& SysTick_CTRL_COUNTFLAG_Msk
) != 0U)
332 #if (STM32H7_DEV_ID == 0x450UL)
333 /** @addtogroup UTILS_EF_SYSTEM
334 * @brief System Configuration functions
337 ===============================================================================
338 ##### System Configuration functions #####
339 ===============================================================================
341 System, AHB and APB buses clocks configuration
343 (+) The maximum frequency of the SYSCLK is 480 MHz(*) and HCLK is 240 MHz.
344 (+) The maximum frequency of the PCLK1, PCLK2, PCLK3 and PCLK4 is 120 MHz.
347 Depending on the device voltage range, the maximum frequency should be
349 (++) +----------------------------------------------------------------------------+
350 (++) | Wait states | HCLK clock frequency (MHz) |
351 (++) | |-----------------------------------------------------------|
352 (++) | (Latency) | voltage range 1 | voltage range 2 | voltage range 3 |
353 (++) | | 1.15V - 1.26V | 1.05V - 1.15V | 0.95V - 1.05V |
354 (++) |----------------|-------------------|-------------------|-------------------|
355 (++) |0WS(1CPU cycle) | 0 < HCLK <= 70 | 0 < HCLK <= 55 | 0 < HCLK <= 45 |
356 (++) |----------------|-------------------|-------------------|-------------------|
357 (++) |1WS(2CPU cycle) | 70 < HCLK <= 140 | 55 < HCLK <= 110 | 45 < HCLK <= 90 |
358 (++) |----------------|-------------------|-------------------|-------------------|
359 (++) |2WS(3CPU cycle) | 140 < HCLK <= 240 | 110 < HCLK <= 165 | 90 < HCLK <= 135 |
360 (++) |----------------|-------------------|-------------------|-------------------|
361 (++) |3WS(4CPU cycle) | -- | 165 < HCLK <= 220 | 135 < HCLK <= 180 |
362 (++) |----------------|-------------------|-------------------|-------------------|
363 (++) |4WS(5CPU cycle) | -- | -- | 180 < HCLK <= 225 |
364 (++) +----------------------------------------------------------------------------+
366 (*) : For stm32h74xxx and stm32h75xxx family lines and requires the board to be connected on LDO regulator not SMPS, 400MHZ otherwise.
371 #elif (STM32H7_DEV_ID == 0x480UL)
372 /** @addtogroup UTILS_EF_SYSTEM
373 * @brief System Configuration functions
376 ===============================================================================
377 ##### System Configuration functions #####
378 ===============================================================================
380 System, AHB and APB buses clocks configuration
382 (+) The maximum frequency of the SYSCLK is 280 MHz and HCLK is 280 MHz.
383 (+) The maximum frequency of the PCLK1, PCLK2, PCLK3 and PCLK4 is 140 MHz.
386 Depending on the device voltage range, the maximum frequency should be
388 (++) +------------------------------------------------------------------------------------------------+
389 (++) | Wait states | HCLK clock frequency (MHz) |
390 (++) | |-------------------------------------------------------------------------------|
391 (++) | (Latency) | voltage range 0 | voltage range 1 | voltage range 2 | voltage range 3 |
392 (++) | | 1.26V - 1.35V | 1.15V - 1.26V | 1.05V - 1.15V | 0.95V - 1.05V |
393 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
394 (++) |0WS(1CPU cycle) | 0 < HCLK <= 44 | 0 < HCLK <= 42 | 0 < HCLK <= 34 | 0 < HCLK <= 22 |
395 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
396 (++) |1WS(2CPU cycle) | 44 < HCLK <= 88 | 42 < HCLK <= 84 | 34 < HCLK <= 68 | 22 < HCLK <= 44 |
397 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
398 (++) |2WS(3CPU cycle) | 88 < HCLK <= 132 | 84 < HCLK <= 126 | 68 < HCLK <= 102 | 44 < HCLK <= 66 |
399 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
400 (++) |3WS(4CPU cycle) | 132 < HCLK <= 176 | 126 < HCLK <= 168 | 102 < HCLK <= 136 | 66 < HCLK <= 88 |
401 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
402 (++) |4WS(5CPU cycle) | 176 < HCLK <= 220 | 168 < HCLK <= 210 | 136 < HCLK <= 160 | -- |
403 (++) +------------------------------------------------------------------------------------------------+
404 (++) |5WS(6CPU cycle) | 220 < HCLK <= 264 | 210 < HCLK <= 225 | -- | -- |
405 (++) +------------------------------------------------------------------------------------------------+
406 (++) |6WS(7CPU cycle) | 264 < HCLK <= 280 | -- | -- | -- |
407 (++) +------------------------------------------------------------------------------------------------+
408 (++) |7WS(8CPU cycle) | -- | -- | -- | -- |
409 (++) +------------------------------------------------------------------------------------------------+
415 #elif (STM32H7_DEV_ID == 0x483UL)
416 /** @addtogroup UTILS_EF_SYSTEM
417 * @brief System Configuration functions
420 ===============================================================================
421 ##### System Configuration functions #####
422 ===============================================================================
424 System, AHB and APB buses clocks configuration
426 (+) The maximum frequency of the SYSCLK is 550 MHz(*) and HCLK is 275 MHz.
427 (+) The maximum frequency of the PCLK1, PCLK2, PCLK3 and PCLK4 is 137.5 MHz.
430 Depending on the device voltage range, the maximum frequency should be
432 (++) +------------------------------------------------------------------------------------------------+
433 (++) | Wait states | HCLK clock frequency (MHz) |
434 (++) | |-------------------------------------------------------------------------------|
435 (++) | (Latency) | voltage range 0 | voltage range 1 | voltage range 2 | voltage range 3 |
436 (++) | | 1.26V - 1.40V | 1.15V - 1.26V | 1.05V - 1.15V | 0.95V - 1.05V |
437 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
438 (++) |0WS(1CPU cycle) | 0 < HCLK <= 70 | 0 < HCLK <= 67 | 0 < HCLK <= 50 | 0 < HCLK <= 35 |
439 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
440 (++) |1WS(2CPU cycle) | 70 < HCLK <= 140 | 67 < HCLK <= 133 | 50 < HCLK <= 100 | 35 < HCLK <= 70 |
441 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
442 (++) |2WS(3CPU cycle) | 140 < HCLK <= 210 | 133 < HCLK <= 200 | 100 < HCLK <= 150 | 70 < HCLK <= 85 |
443 (++) |----------------|-------------------|-------------------|-------------------|-------------------|
444 (++) |3WS(4CPU cycle) | 210 < HCLK <= 275 | -- | -- | -- |
445 (++) +----------------|-------------------|-------------------|-------------------|-------------------|
447 (*) : For stm32h72xxx and stm32h73xxx family lines and requires to enable the CPU_FREQ_BOOST flash option byte, 520MHZ otherwise.
451 #endif /* STM32H7_DEV_ID == 0x450UL */
453 #if defined (DUAL_CORE)
455 * @brief This function sets directly SystemCoreClock CMSIS variable.
456 * @note Variable can be calculated also through SystemCoreClockUpdate function.
457 * @param CPU_Frequency Core frequency in Hz
458 * @note CPU_Frequency can be calculated thanks to RCC helper macro or function
459 * @ref LL_RCC_GetSystemClocksFreq
460 * LL_RCC_GetSystemClocksFreq() is used to calculate the CM7 clock frequency
461 * and __LL_RCC_CALC_HCLK_FREQ is used to caluclate the CM4 clock frequency.
466 * @brief This function sets directly SystemCoreClock CMSIS variable.
467 * @note Variable can be calculated also through SystemCoreClockUpdate function.
468 * @param CPU_Frequency Core frequency in Hz
469 * @note CPU_Frequency can be calculated thanks to RCC helper macro or function
470 * @ref LL_RCC_GetSystemClocksFreq
473 #endif /* DUAL_CORE */
474 void LL_SetSystemCoreClock(uint32_t CPU_Frequency
)
476 /* HCLK clock frequency */
477 SystemCoreClock
= CPU_Frequency
;
481 * @brief This function configures system clock at maximum frequency with HSI as clock source of the PLL
482 * @note The application need to ensure that PLL is disabled.
483 * @note Function is based on the following formula:
484 * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
485 * - PLLM: ensure that the VCO input frequency ranges from 1 to 16 MHz (PLLVCO_input = HSI frequency / PLLM)
486 * - PLLN: ensure that the VCO output frequency is between 150 and 836 MHz or 128 to 560 MHz(***) (PLLVCO_output = PLLVCO_input * PLLN)
487 * - PLLP: ensure that max frequency at 550000000 Hz(*), 480000000 Hz(**) or 280000000 Hz(***) is reach (PLLVCO_output / PLLP)
488 * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
489 * the configuration information for the PLL.
490 * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
491 * the configuration information for the BUS prescalers.
492 * @retval An ErrorStatus enumeration value:
493 * - SUCCESS: Max frequency configuration done
494 * - ERROR: Max frequency configuration not done
496 * (*) : For stm32h72xxx and stm32h73xxx family lines and requires to enable the CPU_FREQ_BOOST flash option byte, 520MHZ otherwise.
497 * (**) : For stm32h74xxx and stm32h75xxx family lines and requires the board to be connected on LDO regulator not SMPS, 400MHZ otherwise.
498 * (***): For stm32h7a3xx, stm32h7b3xx and stm32h7b0xx family lines.
501 ErrorStatus
LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef
*UTILS_PLLInitStruct
,
502 LL_UTILS_ClkInitTypeDef
*UTILS_ClkInitStruct
)
505 #ifdef USE_FULL_ASSERT
506 uint32_t vcoinput_freq
, vcooutput_freq
;
508 uint32_t pllfreq
, hsi_clk
;
510 /* Check the parameters */
511 assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct
->PLLM
));
512 assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct
->PLLN
));
513 assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct
->PLLP
));
514 assert_param(IS_LL_UTILS_FRACN_VALUE(UTILS_PLLInitStruct
->FRACN
));
516 hsi_clk
= (HSI_VALUE
>> (LL_RCC_HSI_GetDivider() >> RCC_CR_HSIDIV_Pos
));
518 /* Check VCO Input frequency */
519 #ifdef USE_FULL_ASSERT
520 vcoinput_freq
= hsi_clk
/ UTILS_PLLInitStruct
->PLLM
;
522 assert_param(IS_LL_UTILS_PLLVCO_INPUT(vcoinput_freq
, UTILS_PLLInitStruct
->VCO_Input
));
524 /* Check VCO Output frequency */
525 #ifdef USE_FULL_ASSERT
526 vcooutput_freq
= LL_RCC_CalcPLLClockFreq(hsi_clk
, UTILS_PLLInitStruct
->PLLM
, UTILS_PLLInitStruct
->PLLN
, UTILS_PLLInitStruct
->FRACN
, 1UL);
528 assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(vcooutput_freq
, UTILS_PLLInitStruct
->VCO_Output
));
530 /* Check VCO Input ranges */
531 assert_param(IS_LL_UTILS_CHECK_VCO_RANGES(UTILS_PLLInitStruct
->VCO_Input
, UTILS_PLLInitStruct
->VCO_Output
));
533 /* Check if one of the PLL is enabled */
534 if(UTILS_IsPLLsReady() == SUCCESS
)
536 /* Calculate the new PLL output frequency */
537 pllfreq
= UTILS_GetPLLOutputFrequency(hsi_clk
, UTILS_PLLInitStruct
);
539 /* Enable HSI if not enabled */
540 if(LL_RCC_HSI_IsReady() != 1U)
543 while (LL_RCC_HSI_IsReady() != 1U)
545 /* Wait for HSI ready */
550 LL_RCC_PLL1P_Enable();
551 LL_RCC_PLL1FRACN_Enable();
552 LL_RCC_PLL_SetSource(LL_RCC_PLLSOURCE_HSI
);
553 LL_RCC_PLL1_SetVCOInputRange(UTILS_PLLInitStruct
->VCO_Input
);
554 LL_RCC_PLL1_SetVCOOutputRange(UTILS_PLLInitStruct
->VCO_Output
);
555 LL_RCC_PLL1_SetM(UTILS_PLLInitStruct
->PLLM
);
556 LL_RCC_PLL1_SetN(UTILS_PLLInitStruct
->PLLN
);
557 LL_RCC_PLL1_SetP(UTILS_PLLInitStruct
->PLLP
);
558 LL_RCC_PLL1_SetFRACN(UTILS_PLLInitStruct
->FRACN
);
560 /* Enable PLL and switch system clock to PLL */
561 status
= UTILS_EnablePLLAndSwitchSystem(pllfreq
, UTILS_ClkInitStruct
);
565 /* Current PLL configuration cannot be modified */
573 * @brief This function configures system clock with HSE as clock source of the PLL
574 * @note The application need to ensure that PLL is disabled.
575 * @note Function is based on the following formula:
576 * - PLL output frequency = (((HSE frequency / PLLM) * PLLN) / PLLP)
577 * - PLLM: ensure that the VCO input frequency ranges from 0.95 to 2.10 MHz (PLLVCO_input = HSE frequency / PLLM)
578 * - PLLN: ensure that the VCO output frequency is between 150 and 836 MHz or 128 to 560 MHz(***) (PLLVCO_output = PLLVCO_input * PLLN)
579 * - PLLP: ensure that max frequency at 550000000 Hz(*), 480000000 Hz(**) or 280000000 Hz(***) is reached (PLLVCO_output / PLLP)
580 * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 48000000
581 * @param HSEBypass This parameter can be one of the following values:
582 * @arg @ref LL_UTILS_HSEBYPASS_ON
583 * @arg @ref LL_UTILS_HSEBYPASS_OFF
584 * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
585 * the configuration information for the PLL.
586 * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
587 * the configuration information for the BUS prescalers.
588 * @retval An ErrorStatus enumeration value:
589 * - SUCCESS: Max frequency configuration done
590 * - ERROR: Max frequency configuration not done
592 * (*) : For stm32h72xxx and stm32h73xxx family lines and requires to enable the CPU_FREQ_BOOST flash option byte, 520MHZ otherwise.
593 * (**) : For stm32h74xxx and stm32h75xxx family lines and requires the board to be connected on LDO regulator not SMPS, 400MHZ otherwise.
594 * (***): For stm32h7a3xx, stm32h7b3xx and stm32h7b0xx family lines.
597 ErrorStatus
LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency
, uint32_t HSEBypass
,
598 LL_UTILS_PLLInitTypeDef
*UTILS_PLLInitStruct
, LL_UTILS_ClkInitTypeDef
*UTILS_ClkInitStruct
)
601 #ifdef USE_FULL_ASSERT
602 uint32_t vcoinput_freq
, vcooutput_freq
;
606 /* Check the parameters */
607 assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct
->PLLM
));
608 assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct
->PLLN
));
609 assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct
->PLLP
));
610 assert_param(IS_LL_UTILS_FRACN_VALUE(UTILS_PLLInitStruct
->FRACN
));
611 assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency
));
612 assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass
));
614 /* Check VCO Input frequency */
615 #ifdef USE_FULL_ASSERT
616 vcoinput_freq
= HSEFrequency
/ UTILS_PLLInitStruct
->PLLM
;
618 assert_param(IS_LL_UTILS_PLLVCO_INPUT(vcoinput_freq
, UTILS_PLLInitStruct
->VCO_Input
));
620 /* Check VCO output frequency */
621 #ifdef USE_FULL_ASSERT
622 vcooutput_freq
= LL_RCC_CalcPLLClockFreq(HSEFrequency
, UTILS_PLLInitStruct
->PLLM
, UTILS_PLLInitStruct
->PLLN
, UTILS_PLLInitStruct
->FRACN
, 1U);
624 assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(vcooutput_freq
, UTILS_PLLInitStruct
->VCO_Output
));
626 /* Check VCO Input/output ranges compatibility */
627 assert_param(IS_LL_UTILS_CHECK_VCO_RANGES(UTILS_PLLInitStruct
->VCO_Input
, UTILS_PLLInitStruct
->VCO_Output
));
629 /* Check if one of the PLL is enabled */
630 if(UTILS_IsPLLsReady() == SUCCESS
)
632 /* Calculate the new PLL output frequency */
633 pllfreq
= UTILS_GetPLLOutputFrequency(HSEFrequency
, UTILS_PLLInitStruct
);
635 /* Enable HSE if not enabled */
636 if(LL_RCC_HSE_IsReady() != 1U)
638 /* Check if need to enable HSE bypass feature or not */
639 if(HSEBypass
== LL_UTILS_HSEBYPASS_ON
)
641 LL_RCC_HSE_EnableBypass();
645 LL_RCC_HSE_DisableBypass();
650 while (LL_RCC_HSE_IsReady() != 1U)
652 /* Wait for HSE ready */
657 LL_RCC_PLL1P_Enable();
658 LL_RCC_PLL1FRACN_Enable();
659 LL_RCC_PLL_SetSource(LL_RCC_PLLSOURCE_HSE
);
660 LL_RCC_PLL1_SetVCOInputRange(UTILS_PLLInitStruct
->VCO_Input
);
661 LL_RCC_PLL1_SetVCOOutputRange(UTILS_PLLInitStruct
->VCO_Output
);
662 LL_RCC_PLL1_SetM(UTILS_PLLInitStruct
->PLLM
);
663 LL_RCC_PLL1_SetN(UTILS_PLLInitStruct
->PLLN
);
664 LL_RCC_PLL1_SetP(UTILS_PLLInitStruct
->PLLP
);
665 LL_RCC_PLL1_SetFRACN(UTILS_PLLInitStruct
->FRACN
);
667 /* Enable PLL and switch system clock to PLL */
668 status
= UTILS_EnablePLLAndSwitchSystem(pllfreq
, UTILS_ClkInitStruct
);
672 /* Current PLL configuration cannot be modified */
684 * @brief Update number of Flash wait states in line with new frequency and current
686 * @param HCLK_Frequency HCLK frequency
687 * @retval An ErrorStatus enumeration value:
688 * - SUCCESS: Latency has been modified
689 * - ERROR: Latency cannot be modified
691 ErrorStatus
LL_SetFlashLatency(uint32_t HCLK_Frequency
)
693 ErrorStatus status
= SUCCESS
;
696 uint32_t latency
= LL_FLASH_LATENCY_0
; /* default value 0WS */
700 /* Frequency cannot be equal to 0 */
701 if (HCLK_Frequency
== 0U)
707 #if (STM32H7_DEV_ID == 0x480UL) || (STM32H7_DEV_ID == 0x483UL)
708 if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE0
)
710 #if (STM32H7_DEV_ID == 0x480UL)
711 if((HCLK_Frequency
> UTILS_SCALE0_LATENCY5_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE0_LATENCY6_FREQ
))
713 /* 264 < HCLK <= 280 => 6WS (7 CPU cycles) */
714 latency
= LL_FLASH_LATENCY_6
;
716 else if((HCLK_Frequency
> UTILS_SCALE0_LATENCY4_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE0_LATENCY5_FREQ
))
718 /* 220 < HCLK <= 264 => 5WS (6 CPU cycles) */
719 latency
= LL_FLASH_LATENCY_5
;
721 else if((HCLK_Frequency
> UTILS_SCALE0_LATENCY3_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE0_LATENCY4_FREQ
))
723 /* 176 < HCLK <= 220 => 4WS (5 CPU cycles) */
724 latency
= LL_FLASH_LATENCY_4
;
726 else if((HCLK_Frequency
> UTILS_SCALE0_LATENCY2_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE0_LATENCY3_FREQ
))
727 #elif (STM32H7_DEV_ID == 0x483UL)
728 if((HCLK_Frequency
> UTILS_SCALE0_LATENCY2_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE0_LATENCY3_FREQ
))
729 #endif /* STM32H7_DEV_ID == 0x480UL */
731 /* 132 < HCLK <= 176 => 3WS (4 CPU cycles) */
732 latency
= LL_FLASH_LATENCY_3
;
734 else if((HCLK_Frequency
> UTILS_SCALE0_LATENCY1_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE0_LATENCY2_FREQ
))
736 /* 88 < HCLK <= 132 => 2WS (3 CPU cycles) */
737 latency
= LL_FLASH_LATENCY_2
;
739 else if((HCLK_Frequency
> UTILS_SCALE0_LATENCY0_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE0_LATENCY1_FREQ
))
741 /* 44 < HCLK <= 88 => 1WS (2 CPU cycles) */
742 latency
= LL_FLASH_LATENCY_1
;
744 else if(HCLK_Frequency
<= UTILS_SCALE0_LATENCY0_FREQ
)
746 /* HCLK <= 44 => 0WS (1 CPU cycles) : Do nothing keep latency to default LL_FLASH_LATENCY_0 */
753 #if (STM32H7_DEV_ID == 0x480UL)
754 else if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1
)
756 if((HCLK_Frequency
> UTILS_SCALE1_LATENCY4_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE1_LATENCY5_FREQ
))
758 /* 210 < HCLK <= 225 => 5WS (6 CPU cycles) */
759 latency
= LL_FLASH_LATENCY_5
;
761 else if((HCLK_Frequency
> UTILS_SCALE1_LATENCY3_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE1_LATENCY4_FREQ
))
763 /* 168 < HCLK <= 210 => 4WS (5 CPU cycles) */
764 latency
= LL_FLASH_LATENCY_4
;
766 else if((HCLK_Frequency
> UTILS_SCALE1_LATENCY2_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE1_LATENCY3_FREQ
))
768 /* 126 < HCLK <= 168 => 3WS (4 CPU cycles) */
769 latency
= LL_FLASH_LATENCY_3
;
771 else if((HCLK_Frequency
> UTILS_SCALE1_LATENCY1_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE1_LATENCY2_FREQ
))
773 if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1
)
775 if((HCLK_Frequency
> UTILS_SCALE1_LATENCY1_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE1_LATENCY2_FREQ
))
776 #endif /* STM32H7_DEV_ID == 0x480UL */
778 if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1
)
780 if((HCLK_Frequency
> UTILS_SCALE1_LATENCY1_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE1_LATENCY2_FREQ
))
781 #endif /* STM32H7_DEV_ID == 0x480UL || STM32H7_DEV_ID == 0x483UL */
783 /* 140 < HCLK <= 210 => 2WS (3 CPU cycles) */
784 latency
= LL_FLASH_LATENCY_2
;
786 else if((HCLK_Frequency
> UTILS_SCALE1_LATENCY0_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE1_LATENCY1_FREQ
))
788 /* 70 < HCLK <= 140 => 1WS (2 CPU cycles) */
789 latency
= LL_FLASH_LATENCY_1
;
791 else if(HCLK_Frequency
<= UTILS_SCALE1_LATENCY0_FREQ
)
793 /* HCLK <= 70 => 0WS (1 CPU cycles) : Do nothing keep latency to default LL_FLASH_LATENCY_0 */
800 else if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2
)
802 #if (STM32H7_DEV_ID == 0x480UL) || (STM32H7_DEV_ID == 0x450UL)
803 #if (STM32H7_DEV_ID == 0x480UL)
804 if((HCLK_Frequency
> UTILS_SCALE2_LATENCY3_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE2_LATENCY4_FREQ
))
806 /* 136 < HCLK <= 160 => 4WS (5 CPU cycles) */
807 latency
= LL_FLASH_LATENCY_4
;
809 else if((HCLK_Frequency
> UTILS_SCALE2_LATENCY2_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE2_LATENCY3_FREQ
))
811 if((HCLK_Frequency
> UTILS_SCALE2_LATENCY2_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE2_LATENCY3_FREQ
))
812 #endif /* STM32H7_DEV_ID == 0x480UL */
814 /* 165 < HCLK <= 220 => 3WS (4 CPU cycles) */
815 latency
= LL_FLASH_LATENCY_3
;
817 else if((HCLK_Frequency
> UTILS_SCALE2_LATENCY1_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE2_LATENCY2_FREQ
))
819 if((HCLK_Frequency
> UTILS_SCALE2_LATENCY1_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE2_LATENCY2_FREQ
))
820 #endif /* STM32H7_DEV_ID == 0x480UL || STM32H7_DEV_ID == 0x450UL */
822 /* 110 < HCLK <= 165 => 2WS (3 CPU cycles) */
823 latency
= LL_FLASH_LATENCY_2
;
825 else if((HCLK_Frequency
> UTILS_SCALE2_LATENCY0_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE2_LATENCY1_FREQ
))
827 /* 55 < HCLK <= 110 => 1WS (2 CPU cycles) */
828 latency
= LL_FLASH_LATENCY_1
;
830 else if(HCLK_Frequency
<= UTILS_SCALE2_LATENCY0_FREQ
)
832 /* HCLK <= 55 => 0WS (1 CPU cycles) : Do nothing keep latency to default LL_FLASH_LATENCY_0 */
841 #if (STM32H7_DEV_ID == 0x450UL) || (STM32H7_DEV_ID == 0x480UL)
842 #if (STM32H7_DEV_ID == 0x450UL)
843 if((HCLK_Frequency
> UTILS_SCALE3_LATENCY3_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE3_LATENCY4_FREQ
))
845 /* 180 < HCLK <= 225 => 4WS (5 CPU cycles) */
846 latency
= LL_FLASH_LATENCY_4
;
848 else if((HCLK_Frequency
> UTILS_SCALE3_LATENCY2_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE3_LATENCY3_FREQ
))
850 if((HCLK_Frequency
> UTILS_SCALE3_LATENCY2_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE3_LATENCY3_FREQ
))
851 #endif /*STM32H7_DEV_ID == 0x450UL*/
853 /* 135 < HCLK <= 180 => 3WS (4 CPU cycles) */
854 latency
= LL_FLASH_LATENCY_3
;
856 else if((HCLK_Frequency
> UTILS_SCALE3_LATENCY1_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE3_LATENCY2_FREQ
))
858 if((HCLK_Frequency
> UTILS_SCALE3_LATENCY1_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE3_LATENCY2_FREQ
))
859 #endif /* STM32H7_DEV_ID == 0x450UL || STM32H7_DEV_ID == 0x480UL */
861 /* 90 < HCLK <= 135 => 2WS (3 CPU cycles) */
862 latency
= LL_FLASH_LATENCY_2
;
864 else if((HCLK_Frequency
> UTILS_SCALE3_LATENCY0_FREQ
) && (HCLK_Frequency
<= UTILS_SCALE3_LATENCY1_FREQ
))
866 /* 45 < HCLK <= 90 => 1WS (2 CPU cycles) */
867 latency
= LL_FLASH_LATENCY_1
;
869 else if(HCLK_Frequency
<= UTILS_SCALE3_LATENCY0_FREQ
)
871 /* HCLK <= 45 => 0WS (1 CPU cycles) : Do nothing keep latency to default LL_FLASH_LATENCY_0 */
879 if(status
== SUCCESS
)
881 LL_FLASH_SetLatency(latency
);
883 /* Check that the new number of wait states is taken into account to access the Flash
884 memory by reading the FLASH_ACR register */
888 /* Wait for Flash latency to be updated */
889 getlatency
= LL_FLASH_GetLatency();
891 } while ((getlatency
!= latency
) && (timeout
> 0U));
893 if(getlatency
!= latency
)
908 /** @addtogroup UTILS_LL_Private_Functions
914 * @brief Function to check that PLL can be modified
915 * @param PLL_InputFrequency PLL input frequency (in Hz)
916 * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
917 * the configuration information for the PLL.
918 * @retval PLL output frequency (in Hz)
920 static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency
, LL_UTILS_PLLInitTypeDef
*UTILS_PLLInitStruct
)
924 /* Check the parameters */
925 assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct
->PLLM
));
926 assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct
->PLLN
));
927 assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct
->PLLP
));
928 assert_param(IS_LL_UTILS_FRACN_VALUE(UTILS_PLLInitStruct
->FRACN
));
930 pllfreq
= LL_RCC_CalcPLLClockFreq(PLL_InputFrequency
, UTILS_PLLInitStruct
->PLLM
, UTILS_PLLInitStruct
->PLLN
, UTILS_PLLInitStruct
->FRACN
, UTILS_PLLInitStruct
->PLLP
);
936 * @brief Check that all PLLs are ready therefore configuration can be done
937 * @retval An ErrorStatus enumeration value:
938 * - SUCCESS: All PLLs are ready so configuration can be done
939 * - ERROR: One PLL at least is busy
941 static ErrorStatus
UTILS_IsPLLsReady(void)
943 ErrorStatus status
= SUCCESS
;
945 /* Check if one of the PLL1 is busy */
946 if(LL_RCC_PLL1_IsReady() != 0U)
948 /* PLL1 configuration cannot be done */
952 /* Check if one of the PLL2 is busy */
953 if(LL_RCC_PLL2_IsReady() != 0U)
955 /* PLL2 configuration cannot be done */
959 /* Check if one of the PLL3 is busy */
960 if(LL_RCC_PLL3_IsReady() != 0U)
962 /* PLL3 configuration cannot be done */
970 * @brief Function to enable PLL and switch system clock to PLL
971 * @param SYSCLK_Frequency SYSCLK frequency
972 * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
973 * the configuration information for the BUS prescalers.
974 * @retval An ErrorStatus enumeration value:
975 * - SUCCESS: No problem to switch system to PLL
976 * - ERROR: Problem to switch system to PLL
978 static ErrorStatus
UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency
, LL_UTILS_ClkInitTypeDef
*UTILS_ClkInitStruct
)
980 ErrorStatus status
= SUCCESS
;
981 uint32_t new_hclk_frequency
;
983 assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct
->SYSCLKDivider
));
984 assert_param(IS_LL_UTILS_AHB_DIV(UTILS_ClkInitStruct
->AHBCLKDivider
));
985 assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct
->APB1CLKDivider
));
986 assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct
->APB2CLKDivider
));
987 assert_param(IS_LL_UTILS_APB3_DIV(UTILS_ClkInitStruct
->APB3CLKDivider
));
988 assert_param(IS_LL_UTILS_APB4_DIV(UTILS_ClkInitStruct
->APB4CLKDivider
));
990 /* Calculate the new HCLK frequency */
991 new_hclk_frequency
= LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency
, UTILS_ClkInitStruct
->AHBCLKDivider
);
993 /* Increasing the number of wait states because of higher CPU frequency */
994 if (SystemD2Clock
< new_hclk_frequency
)
996 /* Set FLASH latency to highest latency */
997 status
= LL_SetFlashLatency(new_hclk_frequency
);
1000 /* Update system clock configuration */
1001 if(status
== SUCCESS
)
1004 LL_RCC_PLL1_Enable();
1005 while (LL_RCC_PLL1_IsReady() != 1U)
1007 /* Wait for PLL ready */
1010 /* Set All APBxPrescaler to the Highest Divider */
1011 LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_16
);
1012 LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_16
);
1013 LL_RCC_SetAPB3Prescaler(LL_RCC_APB3_DIV_16
);
1014 LL_RCC_SetAPB4Prescaler(LL_RCC_APB4_DIV_16
);
1016 /* Set SYS prescaler*/
1017 LL_RCC_SetSysPrescaler(UTILS_ClkInitStruct
->SYSCLKDivider
);
1019 /* Set AHB prescaler*/
1020 LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct
->AHBCLKDivider
);
1022 /* Sysclk activation on the main PLL */
1023 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL1
);
1024 while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL1
)
1026 /* Wait for system clock switch to PLL */
1029 /* Set APBn prescaler*/
1030 LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct
->APB1CLKDivider
);
1031 LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct
->APB2CLKDivider
);
1032 LL_RCC_SetAPB3Prescaler(UTILS_ClkInitStruct
->APB3CLKDivider
);
1033 LL_RCC_SetAPB4Prescaler(UTILS_ClkInitStruct
->APB4CLKDivider
);
1035 /* Decreasing the number of wait states because of lower CPU frequency */
1036 if (SystemD2Clock
> new_hclk_frequency
)
1038 /* Set FLASH latency to lowest latency */
1039 status
= LL_SetFlashLatency(new_hclk_frequency
);
1042 /* Update the SystemD2Clock global variable */
1043 #if defined(RCC_D1CFGR_HPRE)
1044 SystemD2Clock
= (SYSCLK_Frequency
>> ((D1CorePrescTable
[(RCC
->D1CFGR
& RCC_D1CFGR_HPRE
)>> RCC_D1CFGR_HPRE_Pos
]) & 0x1FU
));
1046 SystemD2Clock
= (SYSCLK_Frequency
>> ((D1CorePrescTable
[(RCC
->CDCFGR1
& RCC_CDCFGR1_HPRE
)>> RCC_CDCFGR1_HPRE_Pos
]) & 0x1FU
));
1049 /* Update SystemCoreClock variable */
1050 #if defined(DUAL_CORE) && defined(CORE_CM4)
1051 LL_SetSystemCoreClock(SystemD2Clock
);
1053 LL_SetSystemCoreClock(SYSCLK_Frequency
);
1054 #endif /* DUAL_CORE && CORE_CM4 */
1074 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/