stm32f4xx_ll_utils.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 "stm32f4xx_ll_utils.h"
  21. #include "stm32f4xx_ll_rcc.h"
  22. #include "stm32f4xx_ll_system.h"
  23. #include "stm32f4xx_ll_pwr.h"
  24. #ifdef USE_FULL_ASSERT
  25. #include "stm32_assert.h"
  26. #else
  27. #define assert_param(expr) ((void)0U)
  28. #endif /* USE_FULL_ASSERT */
  29. /** @addtogroup STM32F4xx_LL_Driver
  30. * @{
  31. */
  32. /** @addtogroup UTILS_LL
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /** @addtogroup UTILS_LL_Private_Constants
  39. * @{
  40. */
  41. #if defined(RCC_MAX_FREQUENCY_SCALE1)
  42. #define UTILS_MAX_FREQUENCY_SCALE1 RCC_MAX_FREQUENCY /*!< Maximum frequency for system clock at power scale1, in Hz */
  43. #endif /*RCC_MAX_FREQUENCY_SCALE1 */
  44. #define UTILS_MAX_FREQUENCY_SCALE2 RCC_MAX_FREQUENCY_SCALE2 /*!< Maximum frequency for system clock at power scale2, in Hz */
  45. #if defined(RCC_MAX_FREQUENCY_SCALE3)
  46. #define UTILS_MAX_FREQUENCY_SCALE3 RCC_MAX_FREQUENCY_SCALE3 /*!< Maximum frequency for system clock at power scale3, in Hz */
  47. #endif /* MAX_FREQUENCY_SCALE3 */
  48. /* Defines used for PLL range */
  49. #define UTILS_PLLVCO_INPUT_MIN RCC_PLLVCO_INPUT_MIN /*!< Frequency min for PLLVCO input, in Hz */
  50. #define UTILS_PLLVCO_INPUT_MAX RCC_PLLVCO_INPUT_MAX /*!< Frequency max for PLLVCO input, in Hz */
  51. #define UTILS_PLLVCO_OUTPUT_MIN RCC_PLLVCO_OUTPUT_MIN /*!< Frequency min for PLLVCO output, in Hz */
  52. #define UTILS_PLLVCO_OUTPUT_MAX RCC_PLLVCO_OUTPUT_MAX /*!< Frequency max for PLLVCO output, in Hz */
  53. /* Defines used for HSE range */
  54. #define UTILS_HSE_FREQUENCY_MIN 4000000U /*!< Frequency min for HSE frequency, in Hz */
  55. #define UTILS_HSE_FREQUENCY_MAX 26000000U /*!< Frequency max for HSE frequency, in Hz */
  56. /* Defines used for FLASH latency according to HCLK Frequency */
  57. #if defined(FLASH_SCALE1_LATENCY1_FREQ)
  58. #define UTILS_SCALE1_LATENCY1_FREQ FLASH_SCALE1_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
  59. #endif
  60. #if defined(FLASH_SCALE1_LATENCY2_FREQ)
  61. #define UTILS_SCALE1_LATENCY2_FREQ FLASH_SCALE1_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
  62. #endif
  63. #if defined(FLASH_SCALE1_LATENCY3_FREQ)
  64. #define UTILS_SCALE1_LATENCY3_FREQ FLASH_SCALE1_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
  65. #endif
  66. #if defined(FLASH_SCALE1_LATENCY4_FREQ)
  67. #define UTILS_SCALE1_LATENCY4_FREQ FLASH_SCALE1_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
  68. #endif
  69. #if defined(FLASH_SCALE1_LATENCY5_FREQ)
  70. #define UTILS_SCALE1_LATENCY5_FREQ FLASH_SCALE1_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 1 */
  71. #endif
  72. #define UTILS_SCALE2_LATENCY1_FREQ FLASH_SCALE2_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
  73. #define UTILS_SCALE2_LATENCY2_FREQ FLASH_SCALE2_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
  74. #if defined(FLASH_SCALE2_LATENCY3_FREQ)
  75. #define UTILS_SCALE2_LATENCY3_FREQ FLASH_SCALE2_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
  76. #endif
  77. #if defined(FLASH_SCALE2_LATENCY4_FREQ)
  78. #define UTILS_SCALE2_LATENCY4_FREQ FLASH_SCALE2_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 2 */
  79. #endif
  80. #if defined(FLASH_SCALE2_LATENCY5_FREQ)
  81. #define UTILS_SCALE2_LATENCY5_FREQ FLASH_SCALE2_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 2 */
  82. #endif
  83. #if defined(FLASH_SCALE3_LATENCY1_FREQ)
  84. #define UTILS_SCALE3_LATENCY1_FREQ FLASH_SCALE3_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 3 */
  85. #endif
  86. #if defined(FLASH_SCALE3_LATENCY2_FREQ)
  87. #define UTILS_SCALE3_LATENCY2_FREQ FLASH_SCALE3_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 3 */
  88. #endif
  89. #if defined(FLASH_SCALE3_LATENCY3_FREQ)
  90. #define UTILS_SCALE3_LATENCY3_FREQ FLASH_SCALE3_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 3 in power scale 3 */
  91. #endif
  92. #if defined(FLASH_SCALE3_LATENCY4_FREQ)
  93. #define UTILS_SCALE3_LATENCY4_FREQ FLASH_SCALE3_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 3 */
  94. #endif
  95. #if defined(FLASH_SCALE3_LATENCY5_FREQ)
  96. #define UTILS_SCALE3_LATENCY5_FREQ FLASH_SCALE3_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 3 */
  97. #endif
  98. /**
  99. * @}
  100. */
  101. /* Private macros ------------------------------------------------------------*/
  102. /** @addtogroup UTILS_LL_Private_Macros
  103. * @{
  104. */
  105. #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
  106. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
  107. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
  108. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
  109. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
  110. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
  111. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
  112. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
  113. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
  114. #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
  115. || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
  116. || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
  117. || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
  118. || ((__VALUE__) == LL_RCC_APB1_DIV_16))
  119. #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
  120. || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
  121. || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
  122. || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
  123. || ((__VALUE__) == LL_RCC_APB2_DIV_16))
  124. #define IS_LL_UTILS_PLLM_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLM_DIV_2) \
  125. || ((__VALUE__) == LL_RCC_PLLM_DIV_3) \
  126. || ((__VALUE__) == LL_RCC_PLLM_DIV_4) \
  127. || ((__VALUE__) == LL_RCC_PLLM_DIV_5) \
  128. || ((__VALUE__) == LL_RCC_PLLM_DIV_6) \
  129. || ((__VALUE__) == LL_RCC_PLLM_DIV_7) \
  130. || ((__VALUE__) == LL_RCC_PLLM_DIV_8) \
  131. || ((__VALUE__) == LL_RCC_PLLM_DIV_9) \
  132. || ((__VALUE__) == LL_RCC_PLLM_DIV_10) \
  133. || ((__VALUE__) == LL_RCC_PLLM_DIV_11) \
  134. || ((__VALUE__) == LL_RCC_PLLM_DIV_12) \
  135. || ((__VALUE__) == LL_RCC_PLLM_DIV_13) \
  136. || ((__VALUE__) == LL_RCC_PLLM_DIV_14) \
  137. || ((__VALUE__) == LL_RCC_PLLM_DIV_15) \
  138. || ((__VALUE__) == LL_RCC_PLLM_DIV_16) \
  139. || ((__VALUE__) == LL_RCC_PLLM_DIV_17) \
  140. || ((__VALUE__) == LL_RCC_PLLM_DIV_18) \
  141. || ((__VALUE__) == LL_RCC_PLLM_DIV_19) \
  142. || ((__VALUE__) == LL_RCC_PLLM_DIV_20) \
  143. || ((__VALUE__) == LL_RCC_PLLM_DIV_21) \
  144. || ((__VALUE__) == LL_RCC_PLLM_DIV_22) \
  145. || ((__VALUE__) == LL_RCC_PLLM_DIV_23) \
  146. || ((__VALUE__) == LL_RCC_PLLM_DIV_24) \
  147. || ((__VALUE__) == LL_RCC_PLLM_DIV_25) \
  148. || ((__VALUE__) == LL_RCC_PLLM_DIV_26) \
  149. || ((__VALUE__) == LL_RCC_PLLM_DIV_27) \
  150. || ((__VALUE__) == LL_RCC_PLLM_DIV_28) \
  151. || ((__VALUE__) == LL_RCC_PLLM_DIV_29) \
  152. || ((__VALUE__) == LL_RCC_PLLM_DIV_30) \
  153. || ((__VALUE__) == LL_RCC_PLLM_DIV_31) \
  154. || ((__VALUE__) == LL_RCC_PLLM_DIV_32) \
  155. || ((__VALUE__) == LL_RCC_PLLM_DIV_33) \
  156. || ((__VALUE__) == LL_RCC_PLLM_DIV_34) \
  157. || ((__VALUE__) == LL_RCC_PLLM_DIV_35) \
  158. || ((__VALUE__) == LL_RCC_PLLM_DIV_36) \
  159. || ((__VALUE__) == LL_RCC_PLLM_DIV_37) \
  160. || ((__VALUE__) == LL_RCC_PLLM_DIV_38) \
  161. || ((__VALUE__) == LL_RCC_PLLM_DIV_39) \
  162. || ((__VALUE__) == LL_RCC_PLLM_DIV_40) \
  163. || ((__VALUE__) == LL_RCC_PLLM_DIV_41) \
  164. || ((__VALUE__) == LL_RCC_PLLM_DIV_42) \
  165. || ((__VALUE__) == LL_RCC_PLLM_DIV_43) \
  166. || ((__VALUE__) == LL_RCC_PLLM_DIV_44) \
  167. || ((__VALUE__) == LL_RCC_PLLM_DIV_45) \
  168. || ((__VALUE__) == LL_RCC_PLLM_DIV_46) \
  169. || ((__VALUE__) == LL_RCC_PLLM_DIV_47) \
  170. || ((__VALUE__) == LL_RCC_PLLM_DIV_48) \
  171. || ((__VALUE__) == LL_RCC_PLLM_DIV_49) \
  172. || ((__VALUE__) == LL_RCC_PLLM_DIV_50) \
  173. || ((__VALUE__) == LL_RCC_PLLM_DIV_51) \
  174. || ((__VALUE__) == LL_RCC_PLLM_DIV_52) \
  175. || ((__VALUE__) == LL_RCC_PLLM_DIV_53) \
  176. || ((__VALUE__) == LL_RCC_PLLM_DIV_54) \
  177. || ((__VALUE__) == LL_RCC_PLLM_DIV_55) \
  178. || ((__VALUE__) == LL_RCC_PLLM_DIV_56) \
  179. || ((__VALUE__) == LL_RCC_PLLM_DIV_57) \
  180. || ((__VALUE__) == LL_RCC_PLLM_DIV_58) \
  181. || ((__VALUE__) == LL_RCC_PLLM_DIV_59) \
  182. || ((__VALUE__) == LL_RCC_PLLM_DIV_60) \
  183. || ((__VALUE__) == LL_RCC_PLLM_DIV_61) \
  184. || ((__VALUE__) == LL_RCC_PLLM_DIV_62) \
  185. || ((__VALUE__) == LL_RCC_PLLM_DIV_63))
  186. #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((RCC_PLLN_MIN_VALUE <= (__VALUE__)) && ((__VALUE__) <= RCC_PLLN_MAX_VALUE))
  187. #define IS_LL_UTILS_PLLP_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLP_DIV_2) \
  188. || ((__VALUE__) == LL_RCC_PLLP_DIV_4) \
  189. || ((__VALUE__) == LL_RCC_PLLP_DIV_6) \
  190. || ((__VALUE__) == LL_RCC_PLLP_DIV_8))
  191. #define IS_LL_UTILS_PLLVCO_INPUT(__VALUE__) ((UTILS_PLLVCO_INPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX))
  192. #define IS_LL_UTILS_PLLVCO_OUTPUT(__VALUE__) ((UTILS_PLLVCO_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_OUTPUT_MAX))
  193. #if !defined(RCC_MAX_FREQUENCY_SCALE1)
  194. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
  195. ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
  196. #elif defined(RCC_MAX_FREQUENCY_SCALE3)
  197. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
  198. (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
  199. ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
  200. #else
  201. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
  202. ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2))
  203. #endif /* RCC_MAX_FREQUENCY_SCALE1*/
  204. #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
  205. || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
  206. #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
  207. /**
  208. * @}
  209. */
  210. /* Private function prototypes -----------------------------------------------*/
  211. /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
  212. * @{
  213. */
  214. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency,
  215. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
  216. static ErrorStatus UTILS_SetFlashLatency(uint32_t HCLK_Frequency);
  217. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
  218. static ErrorStatus UTILS_PLL_IsBusy(void);
  219. /**
  220. * @}
  221. */
  222. /* Exported functions --------------------------------------------------------*/
  223. /** @addtogroup UTILS_LL_Exported_Functions
  224. * @{
  225. */
  226. /** @addtogroup UTILS_LL_EF_DELAY
  227. * @{
  228. */
  229. /**
  230. * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
  231. * @note When a RTOS is used, it is recommended to avoid changing the Systick
  232. * configuration by calling this function, for a delay use rather osDelay RTOS service.
  233. * @param HCLKFrequency HCLK frequency in Hz
  234. * @note HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq
  235. * @retval None
  236. */
  237. void LL_Init1msTick(uint32_t HCLKFrequency)
  238. {
  239. /* Use frequency provided in argument */
  240. LL_InitTick(HCLKFrequency, 1000U);
  241. }
  242. /**
  243. * @brief This function provides accurate delay (in milliseconds) based
  244. * on SysTick counter flag
  245. * @note When a RTOS is used, it is recommended to avoid using blocking delay
  246. * and use rather osDelay service.
  247. * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
  248. * will configure Systick to 1ms
  249. * @param Delay specifies the delay time length, in milliseconds.
  250. * @retval None
  251. */
  252. void LL_mDelay(uint32_t Delay)
  253. {
  254. __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
  255. /* Add this code to indicate that local variable is not used */
  256. ((void)tmp);
  257. /* Add a period to guaranty minimum wait */
  258. if(Delay < LL_MAX_DELAY)
  259. {
  260. Delay++;
  261. }
  262. while (Delay)
  263. {
  264. if((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
  265. {
  266. Delay--;
  267. }
  268. }
  269. }
  270. /**
  271. * @}
  272. */
  273. /** @addtogroup UTILS_EF_SYSTEM
  274. * @brief System Configuration functions
  275. *
  276. @verbatim
  277. ===============================================================================
  278. ##### System Configuration functions #####
  279. ===============================================================================
  280. [..]
  281. System, AHB and APB buses clocks configuration
  282. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 180000000 Hz.
  283. @endverbatim
  284. @internal
  285. Depending on the device voltage range, the maximum frequency should be
  286. adapted accordingly to the Refenece manual.
  287. @endinternal
  288. * @{
  289. */
  290. /**
  291. * @brief This function sets directly SystemCoreClock CMSIS variable.
  292. * @note Variable can be calculated also through SystemCoreClockUpdate function.
  293. * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
  294. * @retval None
  295. */
  296. void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
  297. {
  298. /* HCLK clock frequency */
  299. SystemCoreClock = HCLKFrequency;
  300. }
  301. /**
  302. * @brief This function configures system clock at maximum frequency with HSI as clock source of the PLL
  303. * @note The application need to ensure that PLL is disabled.
  304. * @note Function is based on the following formula:
  305. * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
  306. * - PLLM: ensure that the VCO input frequency ranges from @ref RCC_PLLVCO_INPUT_MIN to @ref RCC_PLLVCO_INPUT_MAX (PLLVCO_input = HSI frequency / PLLM)
  307. * - PLLN: ensure that the VCO output frequency is between @ref RCC_PLLVCO_OUTPUT_MIN and @ref RCC_PLLVCO_OUTPUT_MAX (PLLVCO_output = PLLVCO_input * PLLN)
  308. * - PLLP: ensure that max frequency at 180000000 Hz is reach (PLLVCO_output / PLLP)
  309. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  310. * the configuration information for the PLL.
  311. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  312. * the configuration information for the BUS prescalers.
  313. * @retval An ErrorStatus enumeration value:
  314. * - SUCCESS: Max frequency configuration done
  315. * - ERROR: Max frequency configuration not done
  316. */
  317. ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  318. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  319. {
  320. ErrorStatus status = SUCCESS;
  321. uint32_t pllfreq = 0U;
  322. /* Check if one of the PLL is enabled */
  323. if(UTILS_PLL_IsBusy() == SUCCESS)
  324. {
  325. /* Calculate the new PLL output frequency */
  326. pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct);
  327. /* Enable HSI if not enabled */
  328. if(LL_RCC_HSI_IsReady() != 1U)
  329. {
  330. LL_RCC_HSI_Enable();
  331. while (LL_RCC_HSI_IsReady() != 1U)
  332. {
  333. /* Wait for HSI ready */
  334. }
  335. }
  336. /* Configure PLL */
  337. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  338. UTILS_PLLInitStruct->PLLP);
  339. /* Enable PLL and switch system clock to PLL */
  340. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  341. }
  342. else
  343. {
  344. /* Current PLL configuration cannot be modified */
  345. status = ERROR;
  346. }
  347. return status;
  348. }
  349. /**
  350. * @brief This function configures system clock with HSE as clock source of the PLL
  351. * @note The application need to ensure that PLL is disabled.
  352. * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
  353. * - PLLM: ensure that the VCO input frequency ranges from @ref RCC_PLLVCO_INPUT_MIN to @ref RCC_PLLVCO_INPUT_MAX (PLLVCO_input = HSI frequency / PLLM)
  354. * - PLLN: ensure that the VCO output frequency is between @ref RCC_PLLVCO_OUTPUT_MIN and @ref RCC_PLLVCO_OUTPUT_MAX (PLLVCO_output = PLLVCO_input * PLLN)
  355. * - PLLP: ensure that max frequency at 180000000 Hz is reach (PLLVCO_output / PLLP)
  356. * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 26000000
  357. * @param HSEBypass This parameter can be one of the following values:
  358. * @arg @ref LL_UTILS_HSEBYPASS_ON
  359. * @arg @ref LL_UTILS_HSEBYPASS_OFF
  360. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  361. * the configuration information for the PLL.
  362. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  363. * the configuration information for the BUS prescalers.
  364. * @retval An ErrorStatus enumeration value:
  365. * - SUCCESS: Max frequency configuration done
  366. * - ERROR: Max frequency configuration not done
  367. */
  368. ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
  369. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  370. {
  371. ErrorStatus status = SUCCESS;
  372. uint32_t pllfreq = 0U;
  373. /* Check the parameters */
  374. assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
  375. assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
  376. /* Check if one of the PLL is enabled */
  377. if(UTILS_PLL_IsBusy() == SUCCESS)
  378. {
  379. /* Calculate the new PLL output frequency */
  380. pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
  381. /* Enable HSE if not enabled */
  382. if(LL_RCC_HSE_IsReady() != 1U)
  383. {
  384. /* Check if need to enable HSE bypass feature or not */
  385. if(HSEBypass == LL_UTILS_HSEBYPASS_ON)
  386. {
  387. LL_RCC_HSE_EnableBypass();
  388. }
  389. else
  390. {
  391. LL_RCC_HSE_DisableBypass();
  392. }
  393. /* Enable HSE */
  394. LL_RCC_HSE_Enable();
  395. while (LL_RCC_HSE_IsReady() != 1U)
  396. {
  397. /* Wait for HSE ready */
  398. }
  399. }
  400. /* Configure PLL */
  401. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  402. UTILS_PLLInitStruct->PLLP);
  403. /* Enable PLL and switch system clock to PLL */
  404. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  405. }
  406. else
  407. {
  408. /* Current PLL configuration cannot be modified */
  409. status = ERROR;
  410. }
  411. return status;
  412. }
  413. /**
  414. * @}
  415. */
  416. /**
  417. * @}
  418. */
  419. /** @addtogroup UTILS_LL_Private_Functions
  420. * @{
  421. */
  422. /**
  423. * @brief Update number of Flash wait states in line with new frequency and current
  424. voltage range.
  425. * @note This Function support ONLY devices with supply voltage (voltage range) between 2.7V and 3.6V
  426. * @param HCLK_Frequency HCLK frequency
  427. * @retval An ErrorStatus enumeration value:
  428. * - SUCCESS: Latency has been modified
  429. * - ERROR: Latency cannot be modified
  430. */
  431. static ErrorStatus UTILS_SetFlashLatency(uint32_t HCLK_Frequency)
  432. {
  433. ErrorStatus status = SUCCESS;
  434. uint32_t latency = LL_FLASH_LATENCY_0; /* default value 0WS */
  435. /* Frequency cannot be equal to 0 */
  436. if(HCLK_Frequency == 0U)
  437. {
  438. status = ERROR;
  439. }
  440. else
  441. {
  442. if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1)
  443. {
  444. #if defined (UTILS_SCALE1_LATENCY5_FREQ)
  445. if((HCLK_Frequency > UTILS_SCALE1_LATENCY5_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  446. {
  447. latency = LL_FLASH_LATENCY_5;
  448. }
  449. #endif /*UTILS_SCALE1_LATENCY5_FREQ */
  450. #if defined (UTILS_SCALE1_LATENCY4_FREQ)
  451. if((HCLK_Frequency > UTILS_SCALE1_LATENCY4_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  452. {
  453. latency = LL_FLASH_LATENCY_4;
  454. }
  455. #endif /* UTILS_SCALE1_LATENCY4_FREQ */
  456. #if defined (UTILS_SCALE1_LATENCY3_FREQ)
  457. if((HCLK_Frequency > UTILS_SCALE1_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  458. {
  459. latency = LL_FLASH_LATENCY_3;
  460. }
  461. #endif /* UTILS_SCALE1_LATENCY3_FREQ */
  462. #if defined (UTILS_SCALE1_LATENCY2_FREQ)
  463. if((HCLK_Frequency > UTILS_SCALE1_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  464. {
  465. latency = LL_FLASH_LATENCY_2;
  466. }
  467. else
  468. {
  469. if((HCLK_Frequency > UTILS_SCALE1_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  470. {
  471. latency = LL_FLASH_LATENCY_1;
  472. }
  473. }
  474. #endif /* UTILS_SCALE1_LATENCY2_FREQ */
  475. }
  476. if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2)
  477. {
  478. #if defined (UTILS_SCALE2_LATENCY5_FREQ)
  479. if((HCLK_Frequency > UTILS_SCALE2_LATENCY5_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  480. {
  481. latency = LL_FLASH_LATENCY_5;
  482. }
  483. #endif /*UTILS_SCALE1_LATENCY5_FREQ */
  484. #if defined (UTILS_SCALE2_LATENCY4_FREQ)
  485. if((HCLK_Frequency > UTILS_SCALE2_LATENCY4_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  486. {
  487. latency = LL_FLASH_LATENCY_4;
  488. }
  489. #endif /*UTILS_SCALE1_LATENCY4_FREQ */
  490. #if defined (UTILS_SCALE2_LATENCY3_FREQ)
  491. if((HCLK_Frequency > UTILS_SCALE2_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  492. {
  493. latency = LL_FLASH_LATENCY_3;
  494. }
  495. #endif /*UTILS_SCALE1_LATENCY3_FREQ */
  496. if((HCLK_Frequency > UTILS_SCALE2_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  497. {
  498. latency = LL_FLASH_LATENCY_2;
  499. }
  500. else
  501. {
  502. if((HCLK_Frequency > UTILS_SCALE2_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  503. {
  504. latency = LL_FLASH_LATENCY_1;
  505. }
  506. }
  507. }
  508. #if defined (LL_PWR_REGU_VOLTAGE_SCALE3)
  509. if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE3)
  510. {
  511. #if defined (UTILS_SCALE3_LATENCY3_FREQ)
  512. if((HCLK_Frequency > UTILS_SCALE3_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  513. {
  514. latency = LL_FLASH_LATENCY_3;
  515. }
  516. #endif /*UTILS_SCALE1_LATENCY3_FREQ */
  517. #if defined (UTILS_SCALE3_LATENCY2_FREQ)
  518. if((HCLK_Frequency > UTILS_SCALE3_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  519. {
  520. latency = LL_FLASH_LATENCY_2;
  521. }
  522. else
  523. {
  524. if((HCLK_Frequency > UTILS_SCALE3_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  525. {
  526. latency = LL_FLASH_LATENCY_1;
  527. }
  528. }
  529. }
  530. #endif /*UTILS_SCALE1_LATENCY2_FREQ */
  531. #endif /* LL_PWR_REGU_VOLTAGE_SCALE3 */
  532. LL_FLASH_SetLatency(latency);
  533. /* Check that the new number of wait states is taken into account to access the Flash
  534. memory by reading the FLASH_ACR register */
  535. if(LL_FLASH_GetLatency() != latency)
  536. {
  537. status = ERROR;
  538. }
  539. }
  540. return status;
  541. }
  542. /**
  543. * @brief Function to check that PLL can be modified
  544. * @param PLL_InputFrequency PLL input frequency (in Hz)
  545. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  546. * the configuration information for the PLL.
  547. * @retval PLL output frequency (in Hz)
  548. */
  549. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
  550. {
  551. uint32_t pllfreq = 0U;
  552. /* Check the parameters */
  553. assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
  554. assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
  555. assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct->PLLP));
  556. /* Check different PLL parameters according to RM */
  557. /* - PLLM: ensure that the VCO input frequency ranges from @ref UTILS_PLLVCO_INPUT_MIN to @ref UTILS_PLLVCO_INPUT_MAX MHz. */
  558. pllfreq = PLL_InputFrequency / (UTILS_PLLInitStruct->PLLM & (RCC_PLLCFGR_PLLM >> RCC_PLLCFGR_PLLM_Pos));
  559. assert_param(IS_LL_UTILS_PLLVCO_INPUT(pllfreq));
  560. /* - PLLN: ensure that the VCO output frequency is between @ref UTILS_PLLVCO_OUTPUT_MIN and @ref UTILS_PLLVCO_OUTPUT_MAX .*/
  561. pllfreq = pllfreq * (UTILS_PLLInitStruct->PLLN & (RCC_PLLCFGR_PLLN >> RCC_PLLCFGR_PLLN_Pos));
  562. assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(pllfreq));
  563. /* - PLLP: ensure that max frequency at @ref RCC_MAX_FREQUENCY Hz is reached */
  564. pllfreq = pllfreq / (((UTILS_PLLInitStruct->PLLP >> RCC_PLLCFGR_PLLP_Pos) + 1) * 2);
  565. assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq));
  566. return pllfreq;
  567. }
  568. /**
  569. * @brief Function to check that PLL can be modified
  570. * @retval An ErrorStatus enumeration value:
  571. * - SUCCESS: PLL modification can be done
  572. * - ERROR: PLL is busy
  573. */
  574. static ErrorStatus UTILS_PLL_IsBusy(void)
  575. {
  576. ErrorStatus status = SUCCESS;
  577. /* Check if PLL is busy*/
  578. if(LL_RCC_PLL_IsReady() != 0U)
  579. {
  580. /* PLL configuration cannot be modified */
  581. status = ERROR;
  582. }
  583. #if defined(RCC_PLLSAI_SUPPORT)
  584. /* Check if PLLSAI is busy*/
  585. if(LL_RCC_PLLSAI_IsReady() != 0U)
  586. {
  587. /* PLLSAI1 configuration cannot be modified */
  588. status = ERROR;
  589. }
  590. #endif /*RCC_PLLSAI_SUPPORT*/
  591. #if defined(RCC_PLLI2S_SUPPORT)
  592. /* Check if PLLI2S is busy*/
  593. if(LL_RCC_PLLI2S_IsReady() != 0U)
  594. {
  595. /* PLLI2S configuration cannot be modified */
  596. status = ERROR;
  597. }
  598. #endif /*RCC_PLLI2S_SUPPORT*/
  599. return status;
  600. }
  601. /**
  602. * @brief Function to enable PLL and switch system clock to PLL
  603. * @param SYSCLK_Frequency SYSCLK frequency
  604. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  605. * the configuration information for the BUS prescalers.
  606. * @retval An ErrorStatus enumeration value:
  607. * - SUCCESS: No problem to switch system to PLL
  608. * - ERROR: Problem to switch system to PLL
  609. */
  610. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  611. {
  612. ErrorStatus status = SUCCESS;
  613. uint32_t hclk_frequency = 0U;
  614. assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
  615. assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
  616. assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct->APB2CLKDivider));
  617. /* Calculate HCLK frequency */
  618. hclk_frequency = __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider);
  619. /* Increasing the number of wait states because of higher CPU frequency */
  620. if(SystemCoreClock < hclk_frequency)
  621. {
  622. /* Set FLASH latency to highest latency */
  623. status = UTILS_SetFlashLatency(hclk_frequency);
  624. }
  625. /* Update system clock configuration */
  626. if(status == SUCCESS)
  627. {
  628. /* Enable PLL */
  629. LL_RCC_PLL_Enable();
  630. while (LL_RCC_PLL_IsReady() != 1U)
  631. {
  632. /* Wait for PLL ready */
  633. }
  634. /* Sysclk activation on the main PLL */
  635. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  636. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  637. while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  638. {
  639. /* Wait for system clock switch to PLL */
  640. }
  641. /* Set APB1 & APB2 prescaler*/
  642. LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
  643. LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct->APB2CLKDivider);
  644. }
  645. /* Decreasing the number of wait states because of lower CPU frequency */
  646. if(SystemCoreClock > hclk_frequency)
  647. {
  648. /* Set FLASH latency to lowest latency */
  649. status = UTILS_SetFlashLatency(hclk_frequency);
  650. }
  651. /* Update SystemCoreClock variable */
  652. if(status == SUCCESS)
  653. {
  654. LL_SetSystemCoreClock(hclk_frequency);
  655. }
  656. return status;
  657. }
  658. /**
  659. * @}
  660. */
  661. /**
  662. * @}
  663. */
  664. /**
  665. * @}
  666. */
  667. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/