stm32g0xx_ll_utils.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_ll_utils.c
  4. * @author MCD Application Team
  5. * @brief UTILS LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2018 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 "stm32g0xx_ll_utils.h"
  21. #include "stm32g0xx_ll_rcc.h"
  22. #include "stm32g0xx_ll_system.h"
  23. #include "stm32g0xx_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 STM32G0xx_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. #define UTILS_MAX_FREQUENCY 64000000U /*!< Maximum frequency for system clock, in Hz */
  42. /* Defines used for PLL range */
  43. #define UTILS_PLLVCO_INPUT_MIN 4000000U /*!< Frequency min for PLLVCO input, in Hz */
  44. #define UTILS_PLLVCO_INPUT_MAX 8000000U /*!< Frequency max for PLLVCO input, in Hz */
  45. #define UTILS_PLLVCO_OUTPUT_MIN 64000000U /*!< Frequency min for PLLVCO output, in Hz */
  46. #define UTILS_PLLVCO_OUTPUT_MAX 344000000U /*!< Frequency max for PLLVCO output, in Hz */
  47. /* Defines used for HSE range */
  48. #define UTILS_HSE_FREQUENCY_MIN 4000000U /*!< Frequency min for HSE frequency, in Hz */
  49. #define UTILS_HSE_FREQUENCY_MAX 48000000U /*!< Frequency max for HSE frequency, in Hz */
  50. /* Defines used for FLASH latency according to HCLK Frequency */
  51. #define UTILS_SCALE1_LATENCY1_FREQ 24000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
  52. #define UTILS_SCALE1_LATENCY2_FREQ 48000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
  53. #define UTILS_SCALE1_LATENCY3_FREQ 64000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
  54. /**
  55. * @}
  56. */
  57. /* Private macros ------------------------------------------------------------*/
  58. /** @addtogroup UTILS_LL_Private_Macros
  59. * @{
  60. */
  61. #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
  62. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
  63. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
  64. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
  65. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
  66. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
  67. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
  68. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
  69. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
  70. #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
  71. || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
  72. || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
  73. || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
  74. || ((__VALUE__) == LL_RCC_APB1_DIV_16))
  75. #define IS_LL_UTILS_HSI_DIV(__VALUE__) (((__VALUE__) == LL_RCC_HSI_DIV_1) \
  76. || ((__VALUE__) == LL_RCC_HSI_DIV_2) \
  77. || ((__VALUE__) == LL_RCC_HSI_DIV_4) \
  78. || ((__VALUE__) == LL_RCC_HSI_DIV_8) \
  79. || ((__VALUE__) == LL_RCC_HSI_DIV_16) \
  80. || ((__VALUE__) == LL_RCC_HSI_DIV_32) \
  81. || ((__VALUE__) == LL_RCC_HSI_DIV_64) \
  82. || ((__VALUE__) == LL_RCC_HSI_DIV_128))
  83. #define IS_LL_UTILS_PLLM_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLM_DIV_1) \
  84. || ((__VALUE__) == LL_RCC_PLLM_DIV_2) \
  85. || ((__VALUE__) == LL_RCC_PLLM_DIV_3) \
  86. || ((__VALUE__) == LL_RCC_PLLM_DIV_4) \
  87. || ((__VALUE__) == LL_RCC_PLLM_DIV_5) \
  88. || ((__VALUE__) == LL_RCC_PLLM_DIV_6) \
  89. || ((__VALUE__) == LL_RCC_PLLM_DIV_7) \
  90. || ((__VALUE__) == LL_RCC_PLLM_DIV_8))
  91. #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((8U <= (__VALUE__)) && ((__VALUE__) <= 86U))
  92. #define IS_LL_UTILS_PLLR_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLR_DIV_2) \
  93. || ((__VALUE__) == LL_RCC_PLLR_DIV_3) \
  94. || ((__VALUE__) == LL_RCC_PLLR_DIV_4) \
  95. || ((__VALUE__) == LL_RCC_PLLR_DIV_5) \
  96. || ((__VALUE__) == LL_RCC_PLLR_DIV_6) \
  97. || ((__VALUE__) == LL_RCC_PLLR_DIV_7) \
  98. || ((__VALUE__) == LL_RCC_PLLR_DIV_8))
  99. #define IS_LL_UTILS_PLLVCO_INPUT(__VALUE__) ((UTILS_PLLVCO_INPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX))
  100. #define IS_LL_UTILS_PLLVCO_OUTPUT(__VALUE__) ((UTILS_PLLVCO_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_OUTPUT_MAX))
  101. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((__VALUE__) <= UTILS_MAX_FREQUENCY)
  102. #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
  103. || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
  104. #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
  105. /**
  106. * @}
  107. */
  108. /* Private function prototypes -----------------------------------------------*/
  109. /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
  110. * @{
  111. */
  112. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency,
  113. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
  114. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
  115. static ErrorStatus UTILS_PLL_IsBusy(void);
  116. /**
  117. * @}
  118. */
  119. /* Exported functions --------------------------------------------------------*/
  120. /** @addtogroup UTILS_LL_Exported_Functions
  121. * @{
  122. */
  123. /** @addtogroup UTILS_LL_EF_DELAY
  124. * @{
  125. */
  126. /**
  127. * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
  128. * @note When a RTOS is used, it is recommended to avoid changing the Systick
  129. * configuration by calling this function, for a delay use rather osDelay RTOS service.
  130. * @param HCLKFrequency HCLK frequency in Hz
  131. * @note HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq
  132. * @retval None
  133. */
  134. void LL_Init1msTick(uint32_t HCLKFrequency)
  135. {
  136. /* Use frequency provided in argument */
  137. LL_InitTick(HCLKFrequency, 1000U);
  138. }
  139. /**
  140. * @brief This function provides accurate delay (in milliseconds) based
  141. * on SysTick counter flag
  142. * @note When a RTOS is used, it is recommended to avoid using blocking delay
  143. * and use rather osDelay service.
  144. * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
  145. * will configure Systick to 1ms
  146. * @param Delay specifies the delay time length, in milliseconds.
  147. * @retval None
  148. */
  149. void LL_mDelay(uint32_t Delay)
  150. {
  151. __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
  152. uint32_t tmpDelay; /* MISRAC2012-Rule-17.8 */
  153. /* Add this code to indicate that local variable is not used */
  154. ((void)tmp);
  155. tmpDelay = Delay;
  156. /* Add a period to guaranty minimum wait */
  157. if (tmpDelay < LL_MAX_DELAY)
  158. {
  159. tmpDelay ++;
  160. }
  161. while (tmpDelay != 0U)
  162. {
  163. if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
  164. {
  165. tmpDelay --;
  166. }
  167. }
  168. }
  169. /**
  170. * @}
  171. */
  172. /** @addtogroup UTILS_EF_SYSTEM
  173. * @brief System Configuration functions
  174. *
  175. @verbatim
  176. ===============================================================================
  177. ##### System Configuration functions #####
  178. ===============================================================================
  179. [..]
  180. System, AHB and APB buses clocks configuration
  181. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 is 64000000 Hz.
  182. @endverbatim
  183. @internal
  184. Depending on the device voltage range, the maximum frequency should be
  185. adapted accordingly:
  186. (++) Table 1. HCLK clock frequency.
  187. (++) +-------------------------------------------------------+
  188. (++) | Latency | HCLK clock frequency (MHz) |
  189. (++) | |-------------------------------------|
  190. (++) | | voltage range 1 | voltage range 2 |
  191. (++) | | 1.08V - 1.32V | 0.9 V - 1.10V |
  192. (++) |-----------------|------------------|------------------|
  193. (++) |0WS(1 CPU cycles)| HCLK <= 24 | HCLK <= 8 |
  194. (++) |-----------------|------------------|------------------|
  195. (++) |1WS(2 CPU cycles)| HCLK <= 48 | HCLK <= 16 |
  196. (++) |-----------------|------------------|------------------|
  197. (++) |2WS(3 CPU cycles)| HCLK <= 64 | - |
  198. (++) |-----------------|------------------|------------------|
  199. @endinternal
  200. * @{
  201. */
  202. /**
  203. * @brief This function sets directly SystemCoreClock CMSIS variable.
  204. * @note Variable can be calculated also through SystemCoreClockUpdate function.
  205. * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
  206. * @retval None
  207. */
  208. void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
  209. {
  210. /* HCLK clock frequency */
  211. SystemCoreClock = HCLKFrequency;
  212. }
  213. /**
  214. * @brief This function configures system clock at maximum frequency with HSI as clock source of the PLL
  215. * @note The application need to ensure that PLL is disabled.
  216. * @note Function is based on the following formula:
  217. * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLR)
  218. * - PLLM: ensure that the VCO input frequency ranges from 4 to 16 MHz (PLLVCO_input = HSI frequency / PLLM)
  219. * - PLLN: ensure that the VCO output frequency is between 64 and 344 MHz (PLLVCO_output = PLLVCO_input * PLLN)
  220. * - PLLR: ensure that max frequency at 64000000 Hz is reach (PLLVCO_output / PLLR)
  221. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  222. * the configuration information for the PLL.
  223. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  224. * the configuration information for the BUS prescalers.
  225. * @retval An ErrorStatus enumeration value:
  226. * - SUCCESS: Max frequency configuration done
  227. * - ERROR: Max frequency configuration not done
  228. */
  229. ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  230. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  231. {
  232. ErrorStatus status;
  233. uint32_t pllfreq;
  234. /* Check if one of the PLL is enabled */
  235. if (UTILS_PLL_IsBusy() == SUCCESS)
  236. {
  237. /* Calculate the new PLL output frequency */
  238. pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct);
  239. /* Enable HSI if not enabled */
  240. if (LL_RCC_HSI_IsReady() != 1U)
  241. {
  242. LL_RCC_HSI_Enable();
  243. while (LL_RCC_HSI_IsReady() != 1U)
  244. {
  245. /* Wait for HSI ready */
  246. }
  247. }
  248. /* Configure PLL */
  249. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  250. UTILS_PLLInitStruct->PLLR);
  251. /* Enable PLL and switch system clock to PLL */
  252. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  253. }
  254. else
  255. {
  256. /* Current PLL configuration cannot be modified */
  257. status = ERROR;
  258. }
  259. return status;
  260. }
  261. /**
  262. * @brief This function configures system clock with HSE as clock source of the PLL
  263. * @note The application need to ensure that PLL is disabled.
  264. * @note Function is based on the following formula:
  265. * - PLL output frequency = (((HSE frequency / PLLM) * PLLN) / PLLR)
  266. * - PLLM: ensure that the VCO input frequency ranges from 4 to 16 MHz (PLLVCO_input = HSE frequency / PLLM)
  267. * - PLLN: ensure that the VCO output frequency is between 64 and 344 MHz (PLLVCO_output = PLLVCO_input * PLLN)
  268. * - PLLR: ensure that max frequency at 64000000 Hz is reached (PLLVCO_output / PLLR)
  269. * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 48000000
  270. * @param HSEBypass This parameter can be one of the following values:
  271. * @arg @ref LL_UTILS_HSEBYPASS_ON
  272. * @arg @ref LL_UTILS_HSEBYPASS_OFF
  273. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  274. * the configuration information for the PLL.
  275. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  276. * the configuration information for the BUS prescalers.
  277. * @retval An ErrorStatus enumeration value:
  278. * - SUCCESS: Max frequency configuration done
  279. * - ERROR: Max frequency configuration not done
  280. */
  281. ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
  282. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  283. {
  284. ErrorStatus status;
  285. uint32_t pllfreq;
  286. /* Check the parameters */
  287. assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
  288. assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
  289. /* Check if one of the PLL is enabled */
  290. if (UTILS_PLL_IsBusy() == SUCCESS)
  291. {
  292. /* Calculate the new PLL output frequency */
  293. pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
  294. /* Enable HSE if not enabled */
  295. if (LL_RCC_HSE_IsReady() != 1U)
  296. {
  297. /* Check if need to enable HSE bypass feature or not */
  298. if (HSEBypass == LL_UTILS_HSEBYPASS_ON)
  299. {
  300. LL_RCC_HSE_EnableBypass();
  301. }
  302. else
  303. {
  304. LL_RCC_HSE_DisableBypass();
  305. }
  306. /* Enable HSE */
  307. LL_RCC_HSE_Enable();
  308. while (LL_RCC_HSE_IsReady() != 1U)
  309. {
  310. /* Wait for HSE ready */
  311. }
  312. }
  313. /* Configure PLL */
  314. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  315. UTILS_PLLInitStruct->PLLR);
  316. /* Enable PLL and switch system clock to PLL */
  317. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  318. }
  319. else
  320. {
  321. /* Current PLL configuration cannot be modified */
  322. status = ERROR;
  323. }
  324. return status;
  325. }
  326. /**
  327. * @brief Update number of Flash wait states in line with new frequency and current
  328. * voltage range.
  329. * @param HCLKFrequency HCLK frequency
  330. * @retval An ErrorStatus enumeration value:
  331. * - SUCCESS: Latency has been modified
  332. * - ERROR: Latency cannot be modified
  333. */
  334. ErrorStatus LL_SetFlashLatency(uint32_t HCLKFrequency)
  335. {
  336. uint32_t timeout;
  337. uint32_t getlatency;
  338. uint32_t latency;
  339. ErrorStatus status;
  340. /* Frequency cannot be equal to 0 or greater than max clock */
  341. if ((HCLKFrequency == 0U) || (HCLKFrequency > UTILS_SCALE1_LATENCY3_FREQ))
  342. {
  343. status = ERROR;
  344. }
  345. else
  346. {
  347. if (HCLKFrequency > UTILS_SCALE1_LATENCY2_FREQ)
  348. {
  349. /* 48 < HCLK <= 64 => 2WS (3 CPU cycles) */
  350. latency = LL_FLASH_LATENCY_2;
  351. }
  352. else
  353. {
  354. if (HCLKFrequency > UTILS_SCALE1_LATENCY1_FREQ)
  355. {
  356. /* 24 < HCLK <= 48 => 1WS (2 CPU cycles) */
  357. latency = LL_FLASH_LATENCY_1;
  358. }
  359. else
  360. {
  361. /* else HCLKFrequency < 24MHz default LL_FLASH_LATENCY_0 0WS */
  362. latency = LL_FLASH_LATENCY_0;
  363. }
  364. }
  365. LL_FLASH_SetLatency(latency);
  366. /* Check that the new number of wait states is taken into account to access the Flash
  367. memory by reading the FLASH_ACR register */
  368. timeout = 2u;
  369. do
  370. {
  371. /* Wait for Flash latency to be updated */
  372. getlatency = LL_FLASH_GetLatency();
  373. timeout--;
  374. } while ((getlatency != latency) && (timeout > 0u));
  375. if(getlatency != latency)
  376. {
  377. status = ERROR;
  378. }
  379. else
  380. {
  381. status = SUCCESS;
  382. }
  383. }
  384. return status;
  385. }
  386. /**
  387. * @}
  388. */
  389. /**
  390. * @}
  391. */
  392. /** @addtogroup UTILS_LL_Private_Functions
  393. * @{
  394. */
  395. /**
  396. * @brief Function to check that PLL can be modified
  397. * @param PLL_InputFrequency PLL input frequency (in Hz)
  398. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  399. * the configuration information for the PLL.
  400. * @retval PLL output frequency (in Hz)
  401. */
  402. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
  403. {
  404. uint32_t pllfreq;
  405. /* Check the parameters */
  406. assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
  407. assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
  408. assert_param(IS_LL_UTILS_PLLR_VALUE(UTILS_PLLInitStruct->PLLR));
  409. /* Check different PLL parameters according to RM */
  410. /* - PLLM: ensure that the VCO input frequency ranges from 4 to 16 MHz. */
  411. pllfreq = PLL_InputFrequency / (((UTILS_PLLInitStruct->PLLM >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  412. assert_param(IS_LL_UTILS_PLLVCO_INPUT(pllfreq));
  413. /* - PLLN: ensure that the VCO output frequency is between 64 and 344 MHz.*/
  414. pllfreq = pllfreq * (UTILS_PLLInitStruct->PLLN & (RCC_PLLCFGR_PLLN >> RCC_PLLCFGR_PLLN_Pos));
  415. assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(pllfreq));
  416. /* - PLLR: ensure that max frequency at 64000000 Hz is reached */
  417. pllfreq = pllfreq / (((UTILS_PLLInitStruct->PLLR >> RCC_PLLCFGR_PLLR_Pos) + 1U));
  418. assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq));
  419. return pllfreq;
  420. }
  421. /**
  422. * @brief Function to check that PLL can be modified
  423. * @retval An ErrorStatus enumeration value:
  424. * - SUCCESS: PLL modification can be done
  425. * - ERROR: PLL is busy
  426. */
  427. static ErrorStatus UTILS_PLL_IsBusy(void)
  428. {
  429. ErrorStatus status = SUCCESS;
  430. /* Check if PLL is busy*/
  431. if (LL_RCC_PLL_IsReady() != 0U)
  432. {
  433. /* PLL configuration cannot be modified */
  434. status = ERROR;
  435. }
  436. return status;
  437. }
  438. /**
  439. * @brief Function to enable PLL and switch system clock to PLL
  440. * @param SYSCLK_Frequency SYSCLK frequency
  441. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  442. * the configuration information for the BUS prescalers.
  443. * @retval An ErrorStatus enumeration value:
  444. * - SUCCESS: No problem to switch system to PLL
  445. * - ERROR: Problem to switch system to PLL
  446. */
  447. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  448. {
  449. ErrorStatus status = SUCCESS;
  450. uint32_t hclk_frequency;
  451. assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
  452. assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
  453. /* Calculate HCLK frequency */
  454. hclk_frequency = __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider);
  455. /* Increasing the number of wait states because of higher CPU frequency */
  456. if (SystemCoreClock < hclk_frequency)
  457. {
  458. /* Set FLASH latency to highest latency */
  459. status = LL_SetFlashLatency(hclk_frequency);
  460. }
  461. /* Update system clock configuration */
  462. if (status == SUCCESS)
  463. {
  464. /* Enable PLL */
  465. LL_RCC_PLL_Enable();
  466. LL_RCC_PLL_EnableDomain_SYS();
  467. while (LL_RCC_PLL_IsReady() != 1U)
  468. {
  469. /* Wait for PLL ready */
  470. }
  471. /* Sysclk activation on the main PLL */
  472. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  473. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  474. while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  475. {
  476. /* Wait for system clock switch to PLL */
  477. }
  478. /* Set APB1 & APB2 prescaler*/
  479. LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
  480. }
  481. /* Decreasing the number of wait states because of lower CPU frequency */
  482. if (SystemCoreClock > hclk_frequency)
  483. {
  484. /* Set FLASH latency to lowest latency */
  485. status = LL_SetFlashLatency(hclk_frequency);
  486. }
  487. /* Update SystemCoreClock variable */
  488. if (status == SUCCESS)
  489. {
  490. LL_SetSystemCoreClock(hclk_frequency);
  491. }
  492. return status;
  493. }
  494. /**
  495. * @}
  496. */
  497. /**
  498. * @}
  499. */
  500. /**
  501. * @}
  502. */
  503. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/