You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

985 lines
44 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_ll_utils.c
  4. * @author MCD Application Team
  5. * @brief UTILS LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 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. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32h7xx_ll_utils.h"
  21. #include "stm32h7xx_ll_rcc.h"
  22. #include "stm32h7xx_ll_pwr.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif /* USE_FULL_ASSERT */
  28. /** @addtogroup STM32H7xx_LL_Driver
  29. * @{
  30. */
  31. /** @addtogroup UTILS_LL
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /** @addtogroup UTILS_LL_Private_Constants
  38. * @{
  39. */
  40. #if (POWER_DOMAINS_NUMBER == 3U)
  41. #define UTILS_MAX_FREQUENCY_SCALE1 400000000U /*!< Maximum frequency for system clock at power scale1, in Hz */
  42. #define UTILS_MAX_FREQUENCY_SCALE2 300000000U /*!< Maximum frequency for system clock at power scale2, in Hz */
  43. #define UTILS_MAX_FREQUENCY_SCALE3 200000000U /*!< Maximum frequency for system clock at power scale3, in Hz */
  44. #else
  45. #define UTILS_MAX_FREQUENCY_SCALE0 280000000U /*!< Maximum frequency for system clock at power scale0, in Hz */
  46. #define UTILS_MAX_FREQUENCY_SCALE1 225000000U /*!< Maximum frequency for system clock at power scale1, in Hz */
  47. #define UTILS_MAX_FREQUENCY_SCALE2 160000000U /*!< Maximum frequency for system clock at power scale2, in Hz */
  48. #define UTILS_MAX_FREQUENCY_SCALE3 88000000U /*!< Maximum frequency for system clock at power scale3, in Hz */
  49. #endif /*POWER_DOMAINS_NUMBER == 3U*/
  50. /* Defines used for PLL range */
  51. #define UTILS_PLLVCO_INPUT_MIN1 1000000U /*!< Frequency min for the low range PLLVCO input, in Hz */
  52. #define UTILS_PLLVCO_INPUT_MAX1 2000000U /*!< Frequency max for the wide range PLLVCO input, in Hz */
  53. #define UTILS_PLLVCO_INPUT_MIN2 2000000U /*!< Frequency min for the low range PLLVCO input, in Hz */
  54. #define UTILS_PLLVCO_INPUT_MAX2 4000000U /*!< Frequency max for the wide range PLLVCO input, in Hz */
  55. #define UTILS_PLLVCO_INPUT_MIN3 4000000U /*!< Frequency min for the low range PLLVCO input, in Hz */
  56. #define UTILS_PLLVCO_INPUT_MAX3 8000000U /*!< Frequency max for the wide range PLLVCO input, in Hz */
  57. #define UTILS_PLLVCO_INPUT_MIN4 8000000U /*!< Frequency min for the low range PLLVCO input, in Hz */
  58. #define UTILS_PLLVCO_INPUT_MAX4 16000000U /*!< Frequency max for the wide range PLLVCO input, in Hz */
  59. #if (POWER_DOMAINS_NUMBER == 3U)
  60. #define UTILS_PLLVCO_MEDIUM_OUTPUT_MIN 150000000U /*!< Frequency min for the medium range PLLVCO output, in Hz */
  61. #define UTILS_PLLVCO_WIDE_OUTPUT_MIN 192000000U /*!< Frequency min for the wide range PLLVCO output, in Hz */
  62. #define UTILS_PLLVCO_MEDIUM_OUTPUT_MAX 420000000U /*!< Frequency max for the medium range PLLVCO output, in Hz */
  63. #define UTILS_PLLVCO_WIDE_OUTPUT_MAX 836000000U /*!< Frequency max for the wide range PLLVCO output, in Hz */
  64. #else
  65. #define UTILS_PLLVCO_MEDIUM_OUTPUT_MIN 150000000U /*!< Frequency min for the medium range PLLVCO output, in Hz */
  66. #define UTILS_PLLVCO_WIDE_OUTPUT_MIN 128000000U /*!< Frequency min for the wide range PLLVCO output, in Hz */
  67. #define UTILS_PLLVCO_MEDIUM_OUTPUT_MAX 420000000U /*!< Frequency max for the medium range PLLVCO output, in Hz */
  68. #define UTILS_PLLVCO_WIDE_OUTPUT_MAX 560000000U /*!< Frequency max for the wide range PLLVCO output, in Hz */
  69. #endif /*POWER_DOMAINS_NUMBER == 3U*/
  70. /* Defines used for HSE range */
  71. #define UTILS_HSE_FREQUENCY_MIN 4000000U /*!< Frequency min for HSE frequency, in Hz */
  72. #define UTILS_HSE_FREQUENCY_MAX 48000000U /*!< Frequency max for HSE frequency, in Hz */
  73. /* Defines used for FLASH latency according to HCLK Frequency */
  74. #if (POWER_DOMAINS_NUMBER == 2U)
  75. #define UTILS_SCALE0_LATENCY0_FREQ 44000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 0 */
  76. #define UTILS_SCALE0_LATENCY1_FREQ 88000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 0 */
  77. #define UTILS_SCALE0_LATENCY2_FREQ 132000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 0 */
  78. #define UTILS_SCALE0_LATENCY3_FREQ 176000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 0 */
  79. #define UTILS_SCALE0_LATENCY4_FREQ 220000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 0 */
  80. #define UTILS_SCALE0_LATENCY5_FREQ 264000000U /*!< HCLK frequency to set FLASH latency 5 in power scale 0 */
  81. #define UTILS_SCALE0_LATENCY6_FREQ 280000000U /*!< HCLK frequency to set FLASH latency 6 in power scale 0 */
  82. #define UTILS_SCALE1_LATENCY0_FREQ 42000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 1 */
  83. #define UTILS_SCALE1_LATENCY1_FREQ 84000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
  84. #define UTILS_SCALE1_LATENCY2_FREQ 126000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
  85. #define UTILS_SCALE1_LATENCY3_FREQ 168000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
  86. #define UTILS_SCALE1_LATENCY4_FREQ 210000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
  87. #define UTILS_SCALE1_LATENCY5_FREQ 225000000U /*!< HCLK frequency to set FLASH latency 5 in power scale 1 */
  88. #define UTILS_SCALE2_LATENCY0_FREQ 34000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 2 */
  89. #define UTILS_SCALE2_LATENCY1_FREQ 68000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
  90. #define UTILS_SCALE2_LATENCY2_FREQ 102000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
  91. #define UTILS_SCALE2_LATENCY3_FREQ 136000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 2 */
  92. #define UTILS_SCALE2_LATENCY4_FREQ 160000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 2 */
  93. #define UTILS_SCALE3_LATENCY0_FREQ 22000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 3 */
  94. #define UTILS_SCALE3_LATENCY1_FREQ 44000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 3 */
  95. #define UTILS_SCALE3_LATENCY2_FREQ 66000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 3 */
  96. #define UTILS_SCALE3_LATENCY3_FREQ 88000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 3 */
  97. #else
  98. #define UTILS_SCALE1_LATENCY0_FREQ 70000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 1 */
  99. #define UTILS_SCALE1_LATENCY1_FREQ 140000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
  100. #define UTILS_SCALE1_LATENCY2_FREQ 210000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
  101. #define UTILS_SCALE2_LATENCY0_FREQ 55000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 2 */
  102. #define UTILS_SCALE2_LATENCY1_FREQ 110000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
  103. #define UTILS_SCALE2_LATENCY2_FREQ 165000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
  104. #define UTILS_SCALE2_LATENCY3_FREQ 220000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 2 */
  105. #define UTILS_SCALE3_LATENCY0_FREQ 45000000U /*!< HCLK frequency to set FLASH latency 0 in power scale 3 */
  106. #define UTILS_SCALE3_LATENCY1_FREQ 90000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 3 */
  107. #define UTILS_SCALE3_LATENCY2_FREQ 135000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 3 */
  108. #define UTILS_SCALE3_LATENCY3_FREQ 180000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 3 */
  109. #define UTILS_SCALE3_LATENCY4_FREQ 225000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 3 */
  110. #endif /*POWER_DOMAINS_NUMBER == 2U*/
  111. /**
  112. * @}
  113. */
  114. /* Private macros ------------------------------------------------------------*/
  115. /** @addtogroup UTILS_LL_Private_Macros
  116. * @{
  117. */
  118. #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
  119. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
  120. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
  121. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
  122. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
  123. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
  124. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
  125. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
  126. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
  127. #define IS_LL_UTILS_AHB_DIV(__VALUE__) (((__VALUE__) == LL_RCC_AHB_DIV_1) \
  128. || ((__VALUE__) == LL_RCC_AHB_DIV_2) \
  129. || ((__VALUE__) == LL_RCC_AHB_DIV_4) \
  130. || ((__VALUE__) == LL_RCC_AHB_DIV_8) \
  131. || ((__VALUE__) == LL_RCC_AHB_DIV_16) \
  132. || ((__VALUE__) == LL_RCC_AHB_DIV_64) \
  133. || ((__VALUE__) == LL_RCC_AHB_DIV_128) \
  134. || ((__VALUE__) == LL_RCC_AHB_DIV_256) \
  135. || ((__VALUE__) == LL_RCC_AHB_DIV_512))
  136. #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
  137. || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
  138. || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
  139. || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
  140. || ((__VALUE__) == LL_RCC_APB1_DIV_16))
  141. #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
  142. || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
  143. || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
  144. || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
  145. || ((__VALUE__) == LL_RCC_APB2_DIV_16))
  146. #define IS_LL_UTILS_APB3_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB3_DIV_1) \
  147. || ((__VALUE__) == LL_RCC_APB3_DIV_2) \
  148. || ((__VALUE__) == LL_RCC_APB3_DIV_4) \
  149. || ((__VALUE__) == LL_RCC_APB3_DIV_8) \
  150. || ((__VALUE__) == LL_RCC_APB3_DIV_16))
  151. #define IS_LL_UTILS_APB4_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB4_DIV_1) \
  152. || ((__VALUE__) == LL_RCC_APB4_DIV_2) \
  153. || ((__VALUE__) == LL_RCC_APB4_DIV_4) \
  154. || ((__VALUE__) == LL_RCC_APB4_DIV_8) \
  155. || ((__VALUE__) == LL_RCC_APB4_DIV_16))
  156. #define IS_LL_UTILS_PLLM_VALUE(__VALUE__) ((1U <= (__VALUE__)) && ((__VALUE__) <= 63U))
  157. #if (POWER_DOMAINS_NUMBER == 3U)
  158. #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((4U <= (__VALUE__)) && ((__VALUE__) <= 512U))
  159. #else
  160. #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((8U <= (__VALUE__)) && ((__VALUE__) <= 420U))
  161. #endif /*POWER_DOMAINS_NUMBER == 3U*/
  162. #define IS_LL_UTILS_PLLP_VALUE(__VALUE__) ((1U <= (__VALUE__)) && ((__VALUE__) <= 128U))
  163. #define IS_LL_UTILS_FRACN_VALUE(__VALUE__) ((__VALUE__) <= 0x1FFFU)
  164. #define IS_LL_UTILS_PLLVCO_INPUT(__VALUE__, __RANGE__) ( \
  165. (((__RANGE__) == LL_RCC_PLLINPUTRANGE_1_2) && (UTILS_PLLVCO_INPUT_MIN1 <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX1)) || \
  166. (((__RANGE__) == LL_RCC_PLLINPUTRANGE_2_4) && (UTILS_PLLVCO_INPUT_MIN2 <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX2)) || \
  167. (((__RANGE__) == LL_RCC_PLLINPUTRANGE_4_8) && (UTILS_PLLVCO_INPUT_MIN3 <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX3)) || \
  168. (((__RANGE__) == LL_RCC_PLLINPUTRANGE_8_16) && (UTILS_PLLVCO_INPUT_MIN4 <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX4)))
  169. #define IS_LL_UTILS_PLLVCO_OUTPUT(__VALUE__, __RANGE__) ( \
  170. (((__RANGE__) == LL_RCC_PLLVCORANGE_MEDIUM) && (UTILS_PLLVCO_MEDIUM_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_MEDIUM_OUTPUT_MAX)) || \
  171. (((__RANGE__) == LL_RCC_PLLVCORANGE_WIDE) && (UTILS_PLLVCO_WIDE_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_WIDE_OUTPUT_MAX)))
  172. #define IS_LL_UTILS_CHECK_VCO_RANGES(__RANGEIN__, __RANGEOUT__) ( \
  173. (((__RANGEIN__) == LL_RCC_PLLINPUTRANGE_1_2) && ((__RANGEOUT__) == LL_RCC_PLLVCORANGE_MEDIUM)) || \
  174. (((__RANGEIN__) != LL_RCC_PLLINPUTRANGE_1_2) && ((__RANGEOUT__) == LL_RCC_PLLVCORANGE_WIDE)))
  175. #if (POWER_DOMAINS_NUMBER == 3U)
  176. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
  177. (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
  178. ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
  179. #else
  180. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE0) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE0) : \
  181. (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
  182. (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
  183. ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
  184. #endif /*POWER_DOMAINS_NUMBER == 3U*/
  185. #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
  186. || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
  187. #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
  188. /**
  189. * @}
  190. */
  191. /* Private function prototypes -----------------------------------------------*/
  192. /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
  193. * @{
  194. */
  195. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
  196. static ErrorStatus UTILS_CalculateFlashLatency(uint32_t HCLK_Frequency, uint32_t *latency);
  197. static ErrorStatus UTILS_SetFlashLatency(uint32_t latency);
  198. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
  199. static ErrorStatus UTILS_IsPLLsReady(void);
  200. /**
  201. * @}
  202. */
  203. /* Exported functions --------------------------------------------------------*/
  204. /** @addtogroup UTILS_LL_Exported_Functions
  205. * @{
  206. */
  207. /** @addtogroup UTILS_LL_EF_DELAY
  208. * @{
  209. */
  210. #if defined (DUAL_CORE)
  211. /**
  212. * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
  213. * @note When a RTOS is used, it is recommended to avoid changing the Systick
  214. * configuration by calling this function, for a delay use rather osDelay RTOS service.
  215. * @param CPU_Frequency Core frequency in Hz
  216. * @note CPU_Frequency can be calculated thanks to RCC helper macro or function
  217. * @ref LL_RCC_GetSystemClocksFreq
  218. * LL_RCC_GetSystemClocksFreq() is used to calculate the CM7 clock frequency
  219. * and __LL_RCC_CALC_HCLK_FREQ is used to caluclate the CM4 clock frequency.
  220. * @retval None
  221. */
  222. #else
  223. /**
  224. * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
  225. * @note When a RTOS is used, it is recommended to avoid changing the Systick
  226. * configuration by calling this function, for a delay use rather osDelay RTOS service.
  227. * @param CPU_Frequency Core frequency in Hz
  228. * @note CPU_Frequency can be calculated thanks to RCC helper macro or function
  229. * @ref LL_RCC_GetSystemClocksFreq
  230. * @retval None
  231. */
  232. #endif /* DUAL_CORE */
  233. void LL_Init1msTick(uint32_t CPU_Frequency)
  234. {
  235. /* Use frequency provided in argument */
  236. LL_InitTick(CPU_Frequency, 1000U);
  237. }
  238. /**
  239. * @brief This function provides accurate delay (in milliseconds) based
  240. * on SysTick counter flag
  241. * @note When a RTOS is used, it is recommended to avoid using blocking delay
  242. * and use rather osDelay service.
  243. * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
  244. * will configure Systick to 1ms
  245. * @param Delay specifies the delay time length, in milliseconds.
  246. * @retval None
  247. */
  248. void LL_mDelay(uint32_t Delay)
  249. {
  250. uint32_t count = Delay;
  251. __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
  252. /* Add this code to indicate that local variable is not used */
  253. ((void)tmp);
  254. /* Add a period to guaranty minimum wait */
  255. if(count < LL_MAX_DELAY)
  256. {
  257. count++;
  258. }
  259. while (count != 0U)
  260. {
  261. if((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
  262. {
  263. count--;
  264. }
  265. }
  266. }
  267. /**
  268. * @}
  269. */
  270. /** @addtogroup UTILS_EF_SYSTEM
  271. * @brief System Configuration functions
  272. *
  273. @verbatim
  274. ===============================================================================
  275. ##### System Configuration functions #####
  276. ===============================================================================
  277. [..]
  278. System, AHB and APB buses clocks configuration
  279. #if (POWER_DOMAINS_NUMBER == 3U)
  280. (+) The maximum frequency of the SYSCLK is 400 MHz and HCLK is 200 MHz.
  281. (+) The maximum frequency of the PCLK1, PCLK2, PCLK3 and PCLK4 is 100 MHz.
  282. @endverbatim
  283. @internal
  284. Depending on the device voltage range, the maximum frequency should be
  285. adapted accordingly:
  286. (++) +----------------------------------------------------------------------------+
  287. (++) | Wait states | HCLK clock frequency (MHz) |
  288. (++) | |-----------------------------------------------------------|
  289. (++) | (Latency) | voltage range 1 | voltage range 2 | voltage range 3 |
  290. (++) | | 1.15V - 1.26V | 1.05V - 1.15V | 0.95V - 1.05V |
  291. (++) |----------------|-------------------|-------------------|-------------------|
  292. (++) |0WS(1CPU cycle) | 0 < HCLK <= 70 | 0 < HCLK <= 55 | 0 < HCLK <= 45 |
  293. (++) |----------------|-------------------|-------------------|-------------------|
  294. (++) |1WS(2CPU cycle) | 70 < HCLK <= 140 | 55 < HCLK <= 110 | 45 < HCLK <= 90 |
  295. (++) |----------------|-------------------|-------------------|-------------------|
  296. (++) |2WS(3CPU cycle) | 140 < HCLK <= 210 | 110 < HCLK <= 165 | 90 < HCLK <= 135 |
  297. (++) |----------------|-------------------|-------------------|-------------------|
  298. (++) |3WS(4CPU cycle) | -- | 165 < HCLK <= 220 | 135 < HCLK <= 180 |
  299. (++) |----------------|-------------------|-------------------|-------------------|
  300. (++) |4WS(5CPU cycle) | -- | -- | 180 < HCLK <= 225 |
  301. (++) +----------------------------------------------------------------------------+
  302. #else
  303. (+) The maximum frequency of the SYSCLK is 280 MHz and HCLK is 140 MHz.
  304. (+) The maximum frequency of the PCLK1, PCLK2, PCLK3 and PCLK4 is 140 MHz.
  305. @endverbatim
  306. @internal
  307. Depending on the device voltage range, the maximum frequency should be
  308. adapted accordingly:
  309. (++) +------------------------------------------------------------------------------------------------+
  310. (++) | Wait states | HCLK clock frequency (MHz) |
  311. (++) | |-------------------------------------------------------------------------------|
  312. (++) | (Latency) | voltage range 0 | voltage range 1 | voltage range 2 | voltage range 3 |
  313. (++) | | 1.26V - 1.35V | 1.15V - 1.26V | 1.05V - 1.15V | 0.95V - 1.05V |
  314. (++) |----------------|-------------------|-------------------|-------------------|-------------------|
  315. (++) |0WS(1CPU cycle) | 0 < HCLK <= 44 | 0 < HCLK <= 42 | 0 < HCLK <= 34 | 0 < HCLK <= 22 |
  316. (++) |----------------|-------------------|-------------------|-------------------|-------------------|
  317. (++) |1WS(2CPU cycle) | 44 < HCLK <= 88 | 42 < HCLK <= 84 | 34 < HCLK <= 68 | 22 < HCLK <= 44 |
  318. (++) |----------------|-------------------|-------------------|-------------------|-------------------|
  319. (++) |2WS(3CPU cycle) | 88 < HCLK <= 132 | 84 < HCLK <= 126 | 68 < HCLK <= 102 | 44 < HCLK <= 66 |
  320. (++) |----------------|-------------------|-------------------|-------------------|-------------------|
  321. (++) |3WS(4CPU cycle) | 132 < HCLK <= 176 | 126 < HCLK <= 168 | 102 < HCLK <= 136 | 66 < HCLK <= 88 |
  322. (++) |----------------|-------------------|-------------------|-------------------|-------------------|
  323. (++) |4WS(5CPU cycle) | 176 < HCLK <= 220 | 168 < HCLK <= 210 | 136 < HCLK <= 160 | -- |
  324. (++) +------------------------------------------------------------------------------------------------+
  325. (++) |5WS(6CPU cycle) | 220 < HCLK <= 264 | 210 < HCLK <= 225 | -- | -- |
  326. (++) +------------------------------------------------------------------------------------------------+
  327. (++) |6WS(7CPU cycle) | 264 < HCLK <= 280 | -- | -- | -- |
  328. (++) +------------------------------------------------------------------------------------------------+
  329. (++) |7WS(8CPU cycle) | -- | -- | -- | -- |
  330. (++) +------------------------------------------------------------------------------------------------+
  331. #endif
  332. @endinternal
  333. * @{
  334. */
  335. #if defined (DUAL_CORE)
  336. /**
  337. * @brief This function sets directly SystemCoreClock CMSIS variable.
  338. * @note Variable can be calculated also through SystemCoreClockUpdate function.
  339. * @param CPU_Frequency Core frequency in Hz
  340. * @note CPU_Frequency can be calculated thanks to RCC helper macro or function
  341. * @ref LL_RCC_GetSystemClocksFreq
  342. * LL_RCC_GetSystemClocksFreq() is used to calculate the CM7 clock frequency
  343. * and __LL_RCC_CALC_HCLK_FREQ is used to caluclate the CM4 clock frequency.
  344. * @retval None
  345. */
  346. #else
  347. /**
  348. * @brief This function sets directly SystemCoreClock CMSIS variable.
  349. * @note Variable can be calculated also through SystemCoreClockUpdate function.
  350. * @param CPU_Frequency Core frequency in Hz
  351. * @note CPU_Frequency can be calculated thanks to RCC helper macro or function
  352. * @ref LL_RCC_GetSystemClocksFreq
  353. * @retval None
  354. */
  355. #endif /* DUAL_CORE */
  356. void LL_SetSystemCoreClock(uint32_t CPU_Frequency)
  357. {
  358. /* HCLK clock frequency */
  359. SystemCoreClock = CPU_Frequency;
  360. /* Update the SystemD2Clock global variable */
  361. #if defined(RCC_D1CFGR_HPRE)
  362. SystemD2Clock = (SystemCoreClock >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
  363. #else
  364. SystemD2Clock = (SystemCoreClock >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
  365. #endif
  366. }
  367. /**
  368. * @brief This function configures system clock at maximum frequency with HSI as clock source of the PLL
  369. * @note The application need to ensure that PLL is disabled.
  370. * @note Function is based on the following formula:
  371. * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
  372. * - PLLM: ensure that the VCO input frequency ranges from 1 to 16 MHz (PLLVCO_input = HSI frequency / PLLM)
  373. * - PLLN: ensure that the VCO output frequency is between 150 and 836 MHz or 128 to 560 MHz(*) (PLLVCO_output = PLLVCO_input * PLLN)
  374. * - PLLP: ensure that max frequency at 400000000 Hz or 280000000 Hz is reach (PLLVCO_output / PLLP)
  375. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  376. * the configuration information for the PLL.
  377. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  378. * the configuration information for the BUS prescalers.
  379. * @retval An ErrorStatus enumeration value:
  380. * - SUCCESS: Max frequency configuration done
  381. * - ERROR: Max frequency configuration not done
  382. *
  383. * (*) : For stm32h7a3xx, stm32h7b3xx and stm32h7b0xx family lines.
  384. *
  385. */
  386. ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  387. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  388. {
  389. ErrorStatus status;
  390. #ifdef USE_FULL_ASSERT
  391. uint32_t vcoinput_freq, vcooutput_freq;
  392. #endif
  393. uint32_t pllfreq, hsi_clk;
  394. /* Check the parameters */
  395. assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
  396. assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
  397. assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct->PLLP));
  398. assert_param(IS_LL_UTILS_FRACN_VALUE(UTILS_PLLInitStruct->FRACN));
  399. hsi_clk = (HSI_VALUE >> (LL_RCC_HSI_GetDivider() >> RCC_CR_HSIDIV_Pos));
  400. /* Check VCO Input frequency */
  401. #ifdef USE_FULL_ASSERT
  402. vcoinput_freq = hsi_clk / UTILS_PLLInitStruct->PLLM;
  403. #endif
  404. assert_param(IS_LL_UTILS_PLLVCO_INPUT(vcoinput_freq, UTILS_PLLInitStruct->VCO_Input));
  405. /* Check VCO Output frequency */
  406. #ifdef USE_FULL_ASSERT
  407. vcooutput_freq = LL_RCC_CalcPLLClockFreq(hsi_clk, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN, UTILS_PLLInitStruct->FRACN, 1UL);
  408. #endif
  409. assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(vcooutput_freq, UTILS_PLLInitStruct->VCO_Output));
  410. /* Check VCO Input ranges */
  411. assert_param(IS_LL_UTILS_CHECK_VCO_RANGES(UTILS_PLLInitStruct->VCO_Input, UTILS_PLLInitStruct->VCO_Output));
  412. /* Check if one of the PLL is enabled */
  413. if(UTILS_IsPLLsReady() == SUCCESS)
  414. {
  415. /* Calculate the new PLL output frequency */
  416. pllfreq = UTILS_GetPLLOutputFrequency(hsi_clk, UTILS_PLLInitStruct);
  417. /* Enable HSI if not enabled */
  418. if(LL_RCC_HSI_IsReady() != 1U)
  419. {
  420. LL_RCC_HSI_Enable();
  421. while (LL_RCC_HSI_IsReady() != 1U)
  422. {
  423. /* Wait for HSI ready */
  424. }
  425. }
  426. /* Configure PLL */
  427. LL_RCC_PLL1P_Enable();
  428. LL_RCC_PLL1FRACN_Enable();
  429. LL_RCC_PLL_SetSource(LL_RCC_PLLSOURCE_HSI);
  430. LL_RCC_PLL1_SetVCOInputRange(UTILS_PLLInitStruct->VCO_Input);
  431. LL_RCC_PLL1_SetVCOOutputRange(UTILS_PLLInitStruct->VCO_Output);
  432. LL_RCC_PLL1_SetM(UTILS_PLLInitStruct->PLLM);
  433. LL_RCC_PLL1_SetN(UTILS_PLLInitStruct->PLLN);
  434. LL_RCC_PLL1_SetP(UTILS_PLLInitStruct->PLLP);
  435. LL_RCC_PLL1_SetFRACN(UTILS_PLLInitStruct->FRACN);
  436. /* Enable PLL and switch system clock to PLL */
  437. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  438. }
  439. else
  440. {
  441. /* Current PLL configuration cannot be modified */
  442. status = ERROR;
  443. }
  444. return status;
  445. }
  446. /**
  447. * @brief This function configures system clock with HSE as clock source of the PLL
  448. * @note The application need to ensure that PLL is disabled.
  449. * @note Function is based on the following formula:
  450. * - PLL output frequency = (((HSE frequency / PLLM) * PLLN) / PLLP)
  451. * - PLLM: ensure that the VCO input frequency ranges from 0.95 to 2.10 MHz (PLLVCO_input = HSE frequency / PLLM)
  452. * - PLLN: ensure that the VCO output frequency is between 150 and 836 MHz (PLLVCO_output = PLLVCO_input * PLLN)
  453. * - PLLP: ensure that max frequency at 400000000 Hz or 280000000 Hz(*) is reached (PLLVCO_output / PLLP)
  454. * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 48000000
  455. * @param HSEBypass This parameter can be one of the following values:
  456. * @arg @ref LL_UTILS_HSEBYPASS_ON
  457. * @arg @ref LL_UTILS_HSEBYPASS_OFF
  458. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  459. * the configuration information for the PLL.
  460. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  461. * the configuration information for the BUS prescalers.
  462. * @retval An ErrorStatus enumeration value:
  463. * - SUCCESS: Max frequency configuration done
  464. * - ERROR: Max frequency configuration not done
  465. *
  466. * (*) : For stm32h7a3xx, stm32h7b3xx and stm32h7b0xx family lines.
  467. *
  468. */
  469. ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
  470. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  471. {
  472. ErrorStatus status;
  473. #ifdef USE_FULL_ASSERT
  474. uint32_t vcoinput_freq, vcooutput_freq;
  475. #endif
  476. uint32_t pllfreq;
  477. /* Check the parameters */
  478. assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
  479. assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
  480. assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct->PLLP));
  481. assert_param(IS_LL_UTILS_FRACN_VALUE(UTILS_PLLInitStruct->FRACN));
  482. assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
  483. assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
  484. /* Check VCO Input frequency */
  485. #ifdef USE_FULL_ASSERT
  486. vcoinput_freq = HSEFrequency / UTILS_PLLInitStruct->PLLM;
  487. #endif
  488. assert_param(IS_LL_UTILS_PLLVCO_INPUT(vcoinput_freq, UTILS_PLLInitStruct->VCO_Input));
  489. /* Check VCO output frequency */
  490. #ifdef USE_FULL_ASSERT
  491. vcooutput_freq = LL_RCC_CalcPLLClockFreq(HSEFrequency, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN, UTILS_PLLInitStruct->FRACN, 1U);
  492. #endif
  493. assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(vcooutput_freq, UTILS_PLLInitStruct->VCO_Output));
  494. /* Check VCO Input/output ranges compatibility */
  495. assert_param(IS_LL_UTILS_CHECK_VCO_RANGES(UTILS_PLLInitStruct->VCO_Input, UTILS_PLLInitStruct->VCO_Output));
  496. /* Check if one of the PLL is enabled */
  497. if(UTILS_IsPLLsReady() == SUCCESS)
  498. {
  499. /* Calculate the new PLL output frequency */
  500. pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
  501. /* Enable HSE if not enabled */
  502. if(LL_RCC_HSE_IsReady() != 1U)
  503. {
  504. /* Check if need to enable HSE bypass feature or not */
  505. if(HSEBypass == LL_UTILS_HSEBYPASS_ON)
  506. {
  507. LL_RCC_HSE_EnableBypass();
  508. }
  509. else
  510. {
  511. LL_RCC_HSE_DisableBypass();
  512. }
  513. /* Enable HSE */
  514. LL_RCC_HSE_Enable();
  515. while (LL_RCC_HSE_IsReady() != 1U)
  516. {
  517. /* Wait for HSE ready */
  518. }
  519. }
  520. /* Configure PLL */
  521. LL_RCC_PLL1P_Enable();
  522. LL_RCC_PLL1FRACN_Enable();
  523. LL_RCC_PLL_SetSource(LL_RCC_PLLSOURCE_HSE);
  524. LL_RCC_PLL1_SetVCOInputRange(UTILS_PLLInitStruct->VCO_Input);
  525. LL_RCC_PLL1_SetVCOOutputRange(UTILS_PLLInitStruct->VCO_Output);
  526. LL_RCC_PLL1_SetM(UTILS_PLLInitStruct->PLLM);
  527. LL_RCC_PLL1_SetN(UTILS_PLLInitStruct->PLLN);
  528. LL_RCC_PLL1_SetP(UTILS_PLLInitStruct->PLLP);
  529. LL_RCC_PLL1_SetFRACN(UTILS_PLLInitStruct->FRACN);
  530. /* Enable PLL and switch system clock to PLL */
  531. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  532. }
  533. else
  534. {
  535. /* Current PLL configuration cannot be modified */
  536. status = ERROR;
  537. }
  538. return status;
  539. }
  540. /**
  541. * @}
  542. */
  543. /**
  544. * @}
  545. */
  546. /** @addtogroup UTILS_LL_Private_Functions
  547. * @{
  548. */
  549. /**
  550. * @brief Calculate and check the Flash wait states number according to the
  551. new HCLK frequency and current voltage range.
  552. * @param HCLK_Frequency HCLK frequency
  553. * @param latency This parameter can be one of the following values:
  554. * @arg @ref LL_FLASH_LATENCY_0
  555. * @arg @ref LL_FLASH_LATENCY_1
  556. * @arg @ref LL_FLASH_LATENCY_2
  557. * @arg @ref LL_FLASH_LATENCY_3
  558. * @arg @ref LL_FLASH_LATENCY_4
  559. * @arg @ref LL_FLASH_LATENCY_5
  560. * @arg @ref LL_FLASH_LATENCY_6
  561. * @arg @ref LL_FLASH_LATENCY_7
  562. * @retval An ErrorStatus enumeration value:
  563. * - SUCCESS: Latency has been modified
  564. * - ERROR: Latency cannot be modified
  565. */
  566. static ErrorStatus UTILS_CalculateFlashLatency(uint32_t HCLK_Frequency, uint32_t *latency)
  567. {
  568. ErrorStatus status = SUCCESS;
  569. /* Frequency cannot be equal to 0 */
  570. if(HCLK_Frequency == 0U)
  571. {
  572. status = ERROR;
  573. }
  574. else
  575. {
  576. #if (POWER_DOMAINS_NUMBER == 2U)
  577. if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE0)
  578. {
  579. if((HCLK_Frequency > UTILS_SCALE0_LATENCY5_FREQ) && (HCLK_Frequency <= UTILS_SCALE0_LATENCY6_FREQ))
  580. {
  581. /* 264 < HCLK <= 280 => 6WS (7 CPU cycles) */
  582. *latency = LL_FLASH_LATENCY_6;
  583. }
  584. else if((HCLK_Frequency > UTILS_SCALE0_LATENCY4_FREQ) && (HCLK_Frequency <= UTILS_SCALE0_LATENCY5_FREQ))
  585. {
  586. /* 220 < HCLK <= 264 => 5WS (6 CPU cycles) */
  587. *latency = LL_FLASH_LATENCY_5;
  588. }
  589. else if((HCLK_Frequency > UTILS_SCALE0_LATENCY3_FREQ) && (HCLK_Frequency <= UTILS_SCALE0_LATENCY4_FREQ))
  590. {
  591. /* 176 < HCLK <= 220 => 4WS (5 CPU cycles) */
  592. *latency = LL_FLASH_LATENCY_4;
  593. }
  594. else if((HCLK_Frequency > UTILS_SCALE0_LATENCY2_FREQ) && (HCLK_Frequency <= UTILS_SCALE0_LATENCY3_FREQ))
  595. {
  596. /* 132 < HCLK <= 176 => 3WS (4 CPU cycles) */
  597. *latency = LL_FLASH_LATENCY_3;
  598. }
  599. else if((HCLK_Frequency > UTILS_SCALE0_LATENCY1_FREQ) && (HCLK_Frequency <= UTILS_SCALE0_LATENCY2_FREQ))
  600. {
  601. /* 88 < HCLK <= 132 => 2WS (3 CPU cycles) */
  602. *latency = LL_FLASH_LATENCY_2;
  603. }
  604. else if((HCLK_Frequency > UTILS_SCALE0_LATENCY0_FREQ) && (HCLK_Frequency <= UTILS_SCALE0_LATENCY1_FREQ))
  605. {
  606. /* 44 < HCLK <= 88 => 1WS (2 CPU cycles) */
  607. *latency = LL_FLASH_LATENCY_1;
  608. }
  609. else if(HCLK_Frequency <= UTILS_SCALE0_LATENCY0_FREQ)
  610. {
  611. /* HCLK <= 44 => 0WS (1 CPU cycles) */
  612. *latency = LL_FLASH_LATENCY_0;
  613. }
  614. else
  615. {
  616. status = ERROR;
  617. }
  618. }
  619. else if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1)
  620. {
  621. if((HCLK_Frequency > UTILS_SCALE1_LATENCY4_FREQ) && (HCLK_Frequency <= UTILS_SCALE1_LATENCY5_FREQ))
  622. {
  623. /* 210 < HCLK <= 225 => 5WS (6 CPU cycles) */
  624. *latency = LL_FLASH_LATENCY_5;
  625. }
  626. else if((HCLK_Frequency > UTILS_SCALE1_LATENCY3_FREQ) && (HCLK_Frequency <= UTILS_SCALE1_LATENCY4_FREQ))
  627. {
  628. /* 168 < HCLK <= 210 => 4WS (5 CPU cycles) */
  629. *latency = LL_FLASH_LATENCY_4;
  630. }
  631. else if((HCLK_Frequency > UTILS_SCALE1_LATENCY2_FREQ) && (HCLK_Frequency <= UTILS_SCALE1_LATENCY3_FREQ))
  632. {
  633. /* 126 < HCLK <= 168 => 3WS (4 CPU cycles) */
  634. *latency = LL_FLASH_LATENCY_3;
  635. }
  636. else if((HCLK_Frequency > UTILS_SCALE1_LATENCY1_FREQ) && (HCLK_Frequency <= UTILS_SCALE1_LATENCY2_FREQ))
  637. #else
  638. if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1)
  639. {
  640. if((HCLK_Frequency > UTILS_SCALE1_LATENCY1_FREQ) && (HCLK_Frequency <= UTILS_SCALE1_LATENCY2_FREQ))
  641. #endif /*POWER_DOMAINS_NUMBER == 2U*/
  642. {
  643. /* 140 < HCLK <= 210 => 2WS (3 CPU cycles) */
  644. *latency = LL_FLASH_LATENCY_2;
  645. }
  646. else if((HCLK_Frequency > UTILS_SCALE1_LATENCY0_FREQ) && (HCLK_Frequency <= UTILS_SCALE1_LATENCY1_FREQ))
  647. {
  648. /* 70 < HCLK <= 140 => 1WS (2 CPU cycles) */
  649. *latency = LL_FLASH_LATENCY_1;
  650. }
  651. else if(HCLK_Frequency <= UTILS_SCALE1_LATENCY0_FREQ)
  652. {
  653. /* HCLK <= 70 => 0WS (1 CPU cycles) */
  654. *latency = LL_FLASH_LATENCY_0;
  655. }
  656. else
  657. {
  658. status = ERROR;
  659. }
  660. }
  661. else if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2)
  662. {
  663. #if (POWER_DOMAINS_NUMBER == 2U)
  664. if((HCLK_Frequency > UTILS_SCALE2_LATENCY3_FREQ) && (HCLK_Frequency <= UTILS_SCALE2_LATENCY4_FREQ))
  665. {
  666. /* 136 < HCLK <= 160 => 4WS (5 CPU cycles) */
  667. *latency = LL_FLASH_LATENCY_4;
  668. }
  669. else if((HCLK_Frequency > UTILS_SCALE2_LATENCY2_FREQ) && (HCLK_Frequency <= UTILS_SCALE2_LATENCY3_FREQ))
  670. #else
  671. if((HCLK_Frequency > UTILS_SCALE2_LATENCY2_FREQ) && (HCLK_Frequency <= UTILS_SCALE2_LATENCY3_FREQ))
  672. #endif /*POWER_DOMAINS_NUMBER == 2U*/
  673. {
  674. /* 165 < HCLK <= 220 => 3WS (4 CPU cycles) */
  675. *latency = LL_FLASH_LATENCY_3;
  676. }
  677. else if((HCLK_Frequency > UTILS_SCALE2_LATENCY1_FREQ) && (HCLK_Frequency <= UTILS_SCALE2_LATENCY2_FREQ))
  678. {
  679. /* 110 < HCLK <= 165 => 2WS (3 CPU cycles) */
  680. *latency = LL_FLASH_LATENCY_2;
  681. }
  682. else if((HCLK_Frequency > UTILS_SCALE2_LATENCY0_FREQ) && (HCLK_Frequency <= UTILS_SCALE2_LATENCY1_FREQ))
  683. {
  684. /* 55 < HCLK <= 110 => 1WS (2 CPU cycles) */
  685. *latency = LL_FLASH_LATENCY_1;
  686. }
  687. else if(HCLK_Frequency <= UTILS_SCALE2_LATENCY0_FREQ)
  688. {
  689. /* HCLK <= 55 => 0WS (1 CPU cycles) */
  690. *latency = LL_FLASH_LATENCY_0;
  691. }
  692. else
  693. {
  694. status = ERROR;
  695. }
  696. }
  697. else /* Scale 3 */
  698. {
  699. #if (POWER_DOMAINS_NUMBER == 3U)
  700. if((HCLK_Frequency > UTILS_SCALE3_LATENCY3_FREQ) && (HCLK_Frequency <= UTILS_SCALE3_LATENCY4_FREQ))
  701. {
  702. /* 180 < HCLK <= 225 => 4WS (5 CPU cycles) */
  703. *latency = LL_FLASH_LATENCY_4;
  704. }
  705. else if((HCLK_Frequency > UTILS_SCALE3_LATENCY2_FREQ) && (HCLK_Frequency <= UTILS_SCALE3_LATENCY3_FREQ))
  706. #else
  707. if((HCLK_Frequency > UTILS_SCALE3_LATENCY2_FREQ) && (HCLK_Frequency <= UTILS_SCALE3_LATENCY3_FREQ))
  708. #endif /*POWER_DOMAINS_NUMBER == 3U*/
  709. {
  710. /* 135 < HCLK <= 180 => 3WS (4 CPU cycles) */
  711. *latency = LL_FLASH_LATENCY_3;
  712. }
  713. else if((HCLK_Frequency > UTILS_SCALE3_LATENCY1_FREQ) && (HCLK_Frequency <= UTILS_SCALE3_LATENCY2_FREQ))
  714. {
  715. /* 90 < HCLK <= 135 => 2WS (3 CPU cycles) */
  716. *latency = LL_FLASH_LATENCY_2;
  717. }
  718. else if((HCLK_Frequency > UTILS_SCALE3_LATENCY0_FREQ) && (HCLK_Frequency <= UTILS_SCALE3_LATENCY1_FREQ))
  719. {
  720. /* 45 < HCLK <= 90 => 1WS (2 CPU cycles) */
  721. *latency = LL_FLASH_LATENCY_1;
  722. }
  723. else if(HCLK_Frequency <= UTILS_SCALE3_LATENCY0_FREQ)
  724. {
  725. /* HCLK <= 45 => 0WS (1 CPU cycles) */
  726. *latency = LL_FLASH_LATENCY_0;
  727. }
  728. else
  729. {
  730. status = ERROR;
  731. }
  732. }
  733. }
  734. return status;
  735. }
  736. /**
  737. * @brief Update number of Flash wait states
  738. * @param latency Flash Latency
  739. * @retval An ErrorStatus enumeration value:
  740. * - SUCCESS: Latency has been modified
  741. * - ERROR: Latency cannot be modified
  742. */
  743. static ErrorStatus UTILS_SetFlashLatency(uint32_t latency)
  744. {
  745. ErrorStatus status = SUCCESS;
  746. LL_FLASH_SetLatency(latency);
  747. /* Check that the new number of wait states is taken into account to access the Flash
  748. memory by reading the FLASH_ACR register */
  749. if(LL_FLASH_GetLatency() != latency)
  750. {
  751. status = ERROR;
  752. }
  753. return status;
  754. }
  755. /**
  756. * @brief Function to check that PLL can be modified
  757. * @param PLL_InputFrequency PLL input frequency (in Hz)
  758. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  759. * the configuration information for the PLL.
  760. * @retval PLL output frequency (in Hz)
  761. */
  762. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
  763. {
  764. uint32_t pllfreq;
  765. /* Check the parameters */
  766. assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
  767. assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
  768. assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct->PLLP));
  769. assert_param(IS_LL_UTILS_FRACN_VALUE(UTILS_PLLInitStruct->FRACN));
  770. pllfreq = LL_RCC_CalcPLLClockFreq(PLL_InputFrequency, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN, UTILS_PLLInitStruct->FRACN, UTILS_PLLInitStruct->PLLP);
  771. return pllfreq;
  772. }
  773. /**
  774. * @brief Check that all PLLs are ready therefore configuration can be done
  775. * @retval An ErrorStatus enumeration value:
  776. * - SUCCESS: All PLLs are ready so configuration can be done
  777. * - ERROR: One PLL at least is busy
  778. */
  779. static ErrorStatus UTILS_IsPLLsReady(void)
  780. {
  781. ErrorStatus status = SUCCESS;
  782. /* Check if one of the PLL1 is busy */
  783. if(LL_RCC_PLL1_IsReady() != 0U)
  784. {
  785. /* PLL1 configuration cannot be done */
  786. status = ERROR;
  787. }
  788. /* Check if one of the PLL2 is busy */
  789. if(LL_RCC_PLL2_IsReady() != 0U)
  790. {
  791. /* PLL2 configuration cannot be done */
  792. status = ERROR;
  793. }
  794. /* Check if one of the PLL3 is busy */
  795. if(LL_RCC_PLL3_IsReady() != 0U)
  796. {
  797. /* PLL3 configuration cannot be done */
  798. status = ERROR;
  799. }
  800. return status;
  801. }
  802. /**
  803. * @brief Function to enable PLL and switch system clock to PLL
  804. * @param SYSCLK_Frequency SYSCLK frequency
  805. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  806. * the configuration information for the BUS prescalers.
  807. * @retval An ErrorStatus enumeration value:
  808. * - SUCCESS: No problem to switch system to PLL
  809. * - ERROR: Problem to switch system to PLL
  810. */
  811. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  812. {
  813. ErrorStatus status;
  814. uint32_t new_hclk_frequency, new_latency;
  815. assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->SYSCLKDivider));
  816. assert_param(IS_LL_UTILS_AHB_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
  817. assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
  818. assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct->APB2CLKDivider));
  819. assert_param(IS_LL_UTILS_APB3_DIV(UTILS_ClkInitStruct->APB3CLKDivider));
  820. assert_param(IS_LL_UTILS_APB4_DIV(UTILS_ClkInitStruct->APB4CLKDivider));
  821. /* Calculate the new HCLK frequency */
  822. new_hclk_frequency = LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider);
  823. /* Calculate the new FLASH latency according to the new HCLK frequency */
  824. status = UTILS_CalculateFlashLatency(new_hclk_frequency, &new_latency);
  825. if(status == SUCCESS)
  826. {
  827. /* Increasing the number of wait states because of higher CPU frequency */
  828. if(LL_FLASH_GetLatency() < new_latency)
  829. {
  830. status = UTILS_SetFlashLatency(new_latency);
  831. }
  832. /* Update system clock configuration */
  833. if(status == SUCCESS)
  834. {
  835. /* Enable PLL */
  836. LL_RCC_PLL1_Enable();
  837. while (LL_RCC_PLL1_IsReady() != 1U)
  838. {
  839. /* Wait for PLL ready */
  840. }
  841. /* Set All APBxPrescaler to the Highest Divider */
  842. LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_16);
  843. LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_16);
  844. LL_RCC_SetAPB3Prescaler(LL_RCC_APB3_DIV_16);
  845. LL_RCC_SetAPB4Prescaler(LL_RCC_APB4_DIV_16);
  846. /* Set SYS prescaler*/
  847. LL_RCC_SetSysPrescaler(UTILS_ClkInitStruct->SYSCLKDivider);
  848. /* Set AHB prescaler*/
  849. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  850. /* Sysclk activation on the main PLL */
  851. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL1);
  852. while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL1)
  853. {
  854. /* Wait for system clock switch to PLL */
  855. }
  856. /* Set APBn prescaler*/
  857. LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
  858. LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct->APB2CLKDivider);
  859. LL_RCC_SetAPB3Prescaler(UTILS_ClkInitStruct->APB3CLKDivider);
  860. LL_RCC_SetAPB4Prescaler(UTILS_ClkInitStruct->APB4CLKDivider);
  861. /* Update SystemCoreClock variable */
  862. LL_SetSystemCoreClock(SYSCLK_Frequency);
  863. }
  864. /* Decreasing the number of wait states because of lower CPU frequency */
  865. if(LL_FLASH_GetLatency() > new_latency)
  866. {
  867. status = UTILS_SetFlashLatency(new_latency);
  868. }
  869. }
  870. return status;
  871. }
  872. /**
  873. * @}
  874. */
  875. /**
  876. * @}
  877. */
  878. /**
  879. * @}
  880. */
  881. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/