stm32h7xx_ll_utils.c 51 KB

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