stm32g4xx_ll_utils.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_ll_utils.c
  4. * @author MCD Application Team
  5. * @brief UTILS LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2019 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in 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 "stm32g4xx_ll_utils.h"
  20. #include "stm32g4xx_ll_rcc.h"
  21. #include "stm32g4xx_ll_system.h"
  22. #include "stm32g4xx_ll_pwr.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif /* USE_FULL_ASSERT */
  28. /** @addtogroup STM32G4xx_LL_Driver
  29. * @{
  30. */
  31. /** @addtogroup UTILS_LL
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /** @addtogroup UTILS_LL_Private_Constants
  38. * @{
  39. */
  40. #define UTILS_MAX_FREQUENCY_SCALE1 170000000U /*!< Maximum frequency for system clock at power scale1, in Hz */
  41. #define UTILS_MAX_FREQUENCY_SCALE2 26000000U /*!< Maximum frequency for system clock at power scale2, in Hz */
  42. /* Defines used for PLL range */
  43. #define UTILS_PLLVCO_INPUT_MIN 2660000U /*!< Frequency min for PLLVCO input, in Hz */
  44. #define UTILS_PLLVCO_INPUT_MAX 16000000U /*!< 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_BOOST_FREQ 34000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
  52. #define UTILS_SCALE1_LATENCY2_BOOST_FREQ 68000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
  53. #define UTILS_SCALE1_LATENCY3_BOOST_FREQ 102000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
  54. #define UTILS_SCALE1_LATENCY4_BOOST_FREQ 136000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
  55. #define UTILS_SCALE1_LATENCY5_BOOST_FREQ 170000000U /*!< HCLK frequency to set FLASH latency 5 in power scale 1 */
  56. #define UTILS_SCALE1_LATENCY1_FREQ 30000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 normal mode */
  57. #define UTILS_SCALE1_LATENCY2_FREQ 60000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 normal mode */
  58. #define UTILS_SCALE1_LATENCY3_FREQ 90000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 1 normal mode */
  59. #define UTILS_SCALE1_LATENCY4_FREQ 120000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 1 normal mode */
  60. #define UTILS_SCALE1_LATENCY5_FREQ 150000000U /*!< HCLK frequency to set FLASH latency 5 in power scale 1 normal mode */
  61. #define UTILS_SCALE2_LATENCY1_FREQ 12000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
  62. #define UTILS_SCALE2_LATENCY2_FREQ 24000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
  63. #define UTILS_SCALE2_LATENCY3_FREQ 26000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 2 */
  64. /**
  65. * @}
  66. */
  67. /* Private macros ------------------------------------------------------------*/
  68. /** @addtogroup UTILS_LL_Private_Macros
  69. * @{
  70. */
  71. #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
  72. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
  73. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
  74. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
  75. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
  76. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
  77. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
  78. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
  79. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
  80. #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
  81. || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
  82. || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
  83. || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
  84. || ((__VALUE__) == LL_RCC_APB1_DIV_16))
  85. #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
  86. || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
  87. || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
  88. || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
  89. || ((__VALUE__) == LL_RCC_APB2_DIV_16))
  90. #define IS_LL_UTILS_PLLM_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLM_DIV_1) \
  91. || ((__VALUE__) == LL_RCC_PLLM_DIV_2) \
  92. || ((__VALUE__) == LL_RCC_PLLM_DIV_3) \
  93. || ((__VALUE__) == LL_RCC_PLLM_DIV_4) \
  94. || ((__VALUE__) == LL_RCC_PLLM_DIV_5) \
  95. || ((__VALUE__) == LL_RCC_PLLM_DIV_6) \
  96. || ((__VALUE__) == LL_RCC_PLLM_DIV_7) \
  97. || ((__VALUE__) == LL_RCC_PLLM_DIV_8) \
  98. || ((__VALUE__) == LL_RCC_PLLM_DIV_9) \
  99. || ((__VALUE__) == LL_RCC_PLLM_DIV_10) \
  100. || ((__VALUE__) == LL_RCC_PLLM_DIV_11) \
  101. || ((__VALUE__) == LL_RCC_PLLM_DIV_12) \
  102. || ((__VALUE__) == LL_RCC_PLLM_DIV_13) \
  103. || ((__VALUE__) == LL_RCC_PLLM_DIV_14) \
  104. || ((__VALUE__) == LL_RCC_PLLM_DIV_15) \
  105. || ((__VALUE__) == LL_RCC_PLLM_DIV_16))
  106. #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((8U <= (__VALUE__)) && ((__VALUE__) <= 127U))
  107. #define IS_LL_UTILS_PLLR_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLR_DIV_2) \
  108. || ((__VALUE__) == LL_RCC_PLLR_DIV_4) \
  109. || ((__VALUE__) == LL_RCC_PLLR_DIV_6) \
  110. || ((__VALUE__) == LL_RCC_PLLR_DIV_8))
  111. #define IS_LL_UTILS_PLLVCO_INPUT(__VALUE__) ((UTILS_PLLVCO_INPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX))
  112. #define IS_LL_UTILS_PLLVCO_OUTPUT(__VALUE__) ((UTILS_PLLVCO_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_OUTPUT_MAX))
  113. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
  114. ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2))
  115. #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
  116. || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
  117. #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
  118. /**
  119. * @}
  120. */
  121. /* Private function prototypes -----------------------------------------------*/
  122. /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
  123. * @{
  124. */
  125. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency,
  126. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
  127. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
  128. static ErrorStatus UTILS_PLL_IsBusy(void);
  129. /**
  130. * @}
  131. */
  132. /* Exported functions --------------------------------------------------------*/
  133. /** @addtogroup UTILS_LL_Exported_Functions
  134. * @{
  135. */
  136. /** @addtogroup UTILS_LL_EF_DELAY
  137. * @{
  138. */
  139. /**
  140. * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
  141. * @note When a RTOS is used, it is recommended to avoid changing the Systick
  142. * configuration by calling this function, for a delay use rather osDelay RTOS service.
  143. * @param HCLKFrequency HCLK frequency in Hz
  144. * @note HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq
  145. * @retval None
  146. */
  147. void LL_Init1msTick(uint32_t HCLKFrequency)
  148. {
  149. /* Use frequency provided in argument */
  150. LL_InitTick(HCLKFrequency, 1000U);
  151. }
  152. /**
  153. * @brief This function provides accurate delay (in milliseconds) based
  154. * on SysTick counter flag
  155. * @note When a RTOS is used, it is recommended to avoid using blocking delay
  156. * and use rather osDelay service.
  157. * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
  158. * will configure Systick to 1ms
  159. * @param Delay specifies the delay time length, in milliseconds.
  160. * @retval None
  161. */
  162. void LL_mDelay(uint32_t Delay)
  163. {
  164. __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
  165. uint32_t tmpDelay; /* MISRAC2012-Rule-17.8 */
  166. /* Add this code to indicate that local variable is not used */
  167. ((void)tmp);
  168. tmpDelay = Delay;
  169. /* Add a period to guaranty minimum wait */
  170. if(tmpDelay < LL_MAX_DELAY)
  171. {
  172. tmpDelay++;
  173. }
  174. while (tmpDelay != 0U)
  175. {
  176. if((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
  177. {
  178. tmpDelay--;
  179. }
  180. }
  181. }
  182. /**
  183. * @}
  184. */
  185. /** @addtogroup UTILS_EF_SYSTEM
  186. * @brief System Configuration functions
  187. *
  188. @verbatim
  189. ===============================================================================
  190. ##### System Configuration functions #####
  191. ===============================================================================
  192. [..]
  193. System, AHB and APB buses clocks configuration
  194. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is
  195. 170000000 Hz for STM32G4xx.
  196. @endverbatim
  197. @internal
  198. Depending on the device voltage range, the maximum frequency should be
  199. adapted accordingly:
  200. +----------------------------------------------------------------------------+
  201. | Latency | HCLK clock frequency (MHz) |
  202. | |----------------------------------------------------------|
  203. | | voltage range 1 | voltage range 1 | voltage range 2 |
  204. | | boost mode 1.28 V | normal mode 1.2 V | 1.0 V |
  205. |-----------------|-------------------|-------------------|------------------|
  206. |0WS(1 CPU cycles)| HCLK <= 34 | HCLK <= 30 | HCLK <= 12 |
  207. |-----------------|-------------------|-------------------|------------------|
  208. |1WS(2 CPU cycles)| HCLK <= 68 | HCLK <= 60 | HCLK <= 24 |
  209. |-----------------|-------------------|-------------------|------------------|
  210. |2WS(3 CPU cycles)| HCLK <= 102 | HCLK <= 90 | HCLK <= 26 |
  211. |-----------------|-------------------|-------------------|------------------|
  212. |3WS(4 CPU cycles)| HCLK <= 136 | HCLK <= 120 | - |
  213. |-----------------|-------------------|-------------------|------------------|
  214. |4WS(5 CPU cycles)| HCLK <= 170 | HCLK <= 150 | - |
  215. +----------------------------------------------------------------------------+
  216. @endinternal
  217. * @{
  218. */
  219. /**
  220. * @brief This function sets directly SystemCoreClock CMSIS variable.
  221. * @note Variable can be calculated also through SystemCoreClockUpdate function.
  222. * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
  223. * @retval None
  224. */
  225. void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
  226. {
  227. /* HCLK clock frequency */
  228. SystemCoreClock = HCLKFrequency;
  229. }
  230. /**
  231. * @brief Update number of Flash wait states in line with new frequency and current
  232. voltage range.
  233. * @param HCLKFrequency HCLK frequency
  234. * @retval An ErrorStatus enumeration value:
  235. * - SUCCESS: Latency has been modified
  236. * - ERROR: Latency cannot be modified
  237. */
  238. ErrorStatus LL_SetFlashLatency(uint32_t HCLKFrequency)
  239. {
  240. uint32_t timeout;
  241. uint32_t getlatency;
  242. ErrorStatus status = SUCCESS;
  243. uint32_t regulatorstatus = LL_PWR_GetRegulVoltageScaling();
  244. uint32_t regulatorbooststatus = LL_PWR_IsEnabledRange1BoostMode();
  245. uint32_t latency = LL_FLASH_LATENCY_0; /* default value 0WS */
  246. /* Frequency cannot be equal to 0 or greater than max clock */
  247. if((HCLKFrequency == 0U) || (HCLKFrequency > UTILS_SCALE1_LATENCY5_BOOST_FREQ))
  248. {
  249. status = ERROR;
  250. }
  251. else
  252. {
  253. if((regulatorstatus == LL_PWR_REGU_VOLTAGE_SCALE1) && (regulatorbooststatus == 1U))
  254. {
  255. if(HCLKFrequency > UTILS_SCALE1_LATENCY4_BOOST_FREQ)
  256. {
  257. /* 136 < HCLK <= 170 => 4WS (5 CPU cycles) */
  258. latency = LL_FLASH_LATENCY_4;
  259. }
  260. else if(HCLKFrequency > UTILS_SCALE1_LATENCY3_BOOST_FREQ)
  261. {
  262. /* 102 < HCLK <= 136 => 3WS (4 CPU cycles) */
  263. latency = LL_FLASH_LATENCY_3;
  264. }
  265. else if(HCLKFrequency > UTILS_SCALE1_LATENCY2_BOOST_FREQ)
  266. {
  267. /* 68 < HCLK <= 102 => 2WS (3 CPU cycles) */
  268. latency = LL_FLASH_LATENCY_2;
  269. }
  270. else
  271. {
  272. if(HCLKFrequency > UTILS_SCALE1_LATENCY1_BOOST_FREQ)
  273. {
  274. /* 34 < HCLK <= 68 => 1WS (2 CPU cycles) */
  275. latency = LL_FLASH_LATENCY_1;
  276. }
  277. /* else HCLKFrequency <= 10MHz default LL_FLASH_LATENCY_0 0WS */
  278. }
  279. }
  280. /* SCALE1 normal mode*/
  281. else if(regulatorstatus == LL_PWR_REGU_VOLTAGE_SCALE1)
  282. {
  283. if(HCLKFrequency > UTILS_SCALE1_LATENCY4_FREQ)
  284. {
  285. /* 120 < HCLK <= 150 => 4WS (5 CPU cycles) */
  286. latency = LL_FLASH_LATENCY_4;
  287. }
  288. else if(HCLKFrequency > UTILS_SCALE1_LATENCY3_FREQ)
  289. {
  290. /* 90 < HCLK <= 120 => 3WS (4 CPU cycles) */
  291. latency = LL_FLASH_LATENCY_3;
  292. }
  293. else if(HCLKFrequency > UTILS_SCALE1_LATENCY2_FREQ)
  294. {
  295. /* 60 < HCLK <= 90 => 2WS (3 CPU cycles) */
  296. latency = LL_FLASH_LATENCY_2;
  297. }
  298. else
  299. {
  300. if(HCLKFrequency > UTILS_SCALE1_LATENCY1_FREQ)
  301. {
  302. /* 30 < HCLK <= 60 => 1WS (2 CPU cycles) */
  303. latency = LL_FLASH_LATENCY_1;
  304. }
  305. /* else HCLKFrequency <= 10MHz default LL_FLASH_LATENCY_0 0WS */
  306. }
  307. }
  308. /* SCALE2 */
  309. else if(regulatorstatus == LL_PWR_REGU_VOLTAGE_SCALE2)
  310. {
  311. if(HCLKFrequency > UTILS_SCALE2_LATENCY2_FREQ)
  312. {
  313. /* 24 < HCLK <= 26 => 2WS (3 CPU cycles) */
  314. latency = LL_FLASH_LATENCY_2;
  315. }
  316. else
  317. {
  318. if(HCLKFrequency > UTILS_SCALE2_LATENCY1_FREQ)
  319. {
  320. /* 12 < HCLK <= 24 => 1WS (2 CPU cycles) */
  321. latency = LL_FLASH_LATENCY_1;
  322. }
  323. /* else HCLKFrequency <= 8MHz default LL_FLASH_LATENCY_0 0WS */
  324. }
  325. }
  326. else
  327. {
  328. /* Nothing to do */
  329. }
  330. if (status != ERROR)
  331. {
  332. LL_FLASH_SetLatency(latency);
  333. /* Check that the new number of wait states is taken into account to access the Flash
  334. memory by reading the FLASH_ACR register */
  335. timeout = 2U;
  336. do
  337. {
  338. /* Wait for Flash latency to be updated */
  339. getlatency = LL_FLASH_GetLatency();
  340. timeout--;
  341. } while ((getlatency != latency) && (timeout > 0U));
  342. if(getlatency != latency)
  343. {
  344. status = ERROR;
  345. }
  346. }
  347. }
  348. return status;
  349. }
  350. /**
  351. * @brief This function configures system clock at maximum frequency with HSI as clock source of the PLL
  352. * @note The application need to ensure that PLL is disabled.
  353. * @note Function is based on the following formula:
  354. * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLR)
  355. * - PLLM: ensure that the VCO input frequency ranges from 2.66 to 8 MHz (PLLVCO_input = HSI frequency / PLLM)
  356. * - PLLN: ensure that the VCO output frequency is between 64 and 344 MHz (PLLVCO_output = PLLVCO_input * PLLN)
  357. * - PLLR: ensure that max frequency at 170000000 Hz is reach (PLLVCO_output / PLLR)
  358. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  359. * the configuration information for the PLL.
  360. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  361. * the configuration information for the BUS prescalers.
  362. * @retval An ErrorStatus enumeration value:
  363. * - SUCCESS: Max frequency configuration done
  364. * - ERROR: Max frequency configuration not done
  365. */
  366. ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  367. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  368. {
  369. ErrorStatus status;
  370. uint32_t pllfreq;
  371. uint32_t hpre = LL_RCC_SYSCLK_DIV_1;
  372. /* Check if one of the PLL is enabled */
  373. if(UTILS_PLL_IsBusy() == SUCCESS)
  374. {
  375. /* Calculate the new PLL output frequency */
  376. pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct);
  377. /* Enable HSI if not enabled */
  378. if(LL_RCC_HSI_IsReady() != 1U)
  379. {
  380. LL_RCC_HSI_Enable();
  381. while (LL_RCC_HSI_IsReady() != 1U)
  382. {
  383. /* Wait for HSI ready */
  384. }
  385. }
  386. /* Configure PLL */
  387. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  388. UTILS_PLLInitStruct->PLLR);
  389. /* Prevent undershoot at highest frequency by applying intermediate AHB prescaler 2 */
  390. if(pllfreq > 80000000U)
  391. {
  392. if (UTILS_ClkInitStruct->AHBCLKDivider == LL_RCC_SYSCLK_DIV_1)
  393. {
  394. UTILS_ClkInitStruct->AHBCLKDivider = LL_RCC_SYSCLK_DIV_2;
  395. hpre = LL_RCC_SYSCLK_DIV_2;
  396. }
  397. }
  398. /* Enable PLL and switch system clock to PLL */
  399. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  400. /* Apply definitive AHB prescaler value if necessary */
  401. if ((status == SUCCESS) && (hpre != LL_RCC_SYSCLK_DIV_1))
  402. {
  403. /* Set FLASH latency to highest latency */
  404. status = LL_SetFlashLatency(pllfreq);
  405. if (status == SUCCESS)
  406. {
  407. UTILS_ClkInitStruct->AHBCLKDivider = LL_RCC_SYSCLK_DIV_1;
  408. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  409. LL_SetSystemCoreClock(pllfreq);
  410. }
  411. }
  412. }
  413. else
  414. {
  415. /* Current PLL configuration cannot be modified */
  416. status = ERROR;
  417. }
  418. return status;
  419. }
  420. /**
  421. * @brief This function configures system clock with HSE as clock source of the PLL
  422. * @note The application need to ensure that PLL is disabled.
  423. * @note Function is based on the following formula:
  424. * - PLL output frequency = (((HSE frequency / PLLM) * PLLN) / PLLR)
  425. * - PLLM: ensure that the VCO input frequency ranges from 2.66 to 8 MHz (PLLVCO_input = HSE frequency / PLLM)
  426. * - PLLN: ensure that the VCO output frequency is between 64 and 344 MHz (PLLVCO_output = PLLVCO_input * PLLN)
  427. * - PLLR: ensure that max frequency at 170000000 Hz is reached (PLLVCO_output / PLLR)
  428. * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 48000000
  429. * @param HSEBypass This parameter can be one of the following values:
  430. * @arg @ref LL_UTILS_HSEBYPASS_ON
  431. * @arg @ref LL_UTILS_HSEBYPASS_OFF
  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. ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
  441. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  442. {
  443. ErrorStatus status;
  444. uint32_t pllfreq;
  445. uint32_t hpre = LL_RCC_SYSCLK_DIV_1;
  446. /* Check the parameters */
  447. assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
  448. assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
  449. /* Check if one of the PLL is enabled */
  450. if(UTILS_PLL_IsBusy() == SUCCESS)
  451. {
  452. /* Calculate the new PLL output frequency */
  453. pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
  454. /* Enable HSE if not enabled */
  455. if(LL_RCC_HSE_IsReady() != 1U)
  456. {
  457. /* Check if need to enable HSE bypass feature or not */
  458. if(HSEBypass == LL_UTILS_HSEBYPASS_ON)
  459. {
  460. LL_RCC_HSE_EnableBypass();
  461. }
  462. else
  463. {
  464. LL_RCC_HSE_DisableBypass();
  465. }
  466. /* Enable HSE */
  467. LL_RCC_HSE_Enable();
  468. while (LL_RCC_HSE_IsReady() != 1U)
  469. {
  470. /* Wait for HSE ready */
  471. }
  472. }
  473. /* Configure PLL */
  474. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  475. UTILS_PLLInitStruct->PLLR);
  476. /* Prevent undershoot at highest frequency by applying intermediate AHB prescaler 2 */
  477. if(pllfreq > 80000000U)
  478. {
  479. if (UTILS_ClkInitStruct->AHBCLKDivider == LL_RCC_SYSCLK_DIV_1)
  480. {
  481. UTILS_ClkInitStruct->AHBCLKDivider = LL_RCC_SYSCLK_DIV_2;
  482. hpre = LL_RCC_SYSCLK_DIV_2;
  483. }
  484. }
  485. /* Enable PLL and switch system clock to PLL */
  486. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  487. /* Apply definitive AHB prescaler value if necessary */
  488. if ((status == SUCCESS) && (hpre != LL_RCC_SYSCLK_DIV_1))
  489. {
  490. /* Set FLASH latency to highest latency */
  491. status = LL_SetFlashLatency(pllfreq);
  492. if (status == SUCCESS)
  493. {
  494. UTILS_ClkInitStruct->AHBCLKDivider = LL_RCC_SYSCLK_DIV_1;
  495. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  496. LL_SetSystemCoreClock(pllfreq);
  497. }
  498. }
  499. }
  500. else
  501. {
  502. /* Current PLL configuration cannot be modified */
  503. status = ERROR;
  504. }
  505. return status;
  506. }
  507. /**
  508. * @}
  509. */
  510. /**
  511. * @}
  512. */
  513. /** @addtogroup UTILS_LL_Private_Functions
  514. * @{
  515. */
  516. /**
  517. * @brief Function to check that PLL can be modified
  518. * @param PLL_InputFrequency PLL input frequency (in Hz)
  519. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  520. * the configuration information for the PLL.
  521. * @retval PLL output frequency (in Hz)
  522. */
  523. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
  524. {
  525. uint32_t pllfreq;
  526. /* Check the parameters */
  527. assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
  528. assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
  529. assert_param(IS_LL_UTILS_PLLR_VALUE(UTILS_PLLInitStruct->PLLR));
  530. /* Check different PLL parameters according to RM */
  531. /* - PLLM: ensure that the VCO input frequency ranges from 2.66 to 8 MHz. */
  532. pllfreq = PLL_InputFrequency / (((UTILS_PLLInitStruct->PLLM >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  533. assert_param(IS_LL_UTILS_PLLVCO_INPUT(pllfreq));
  534. /* - PLLN: ensure that the VCO output frequency is between 64 and 344 MHz.*/
  535. pllfreq = pllfreq * (UTILS_PLLInitStruct->PLLN & (RCC_PLLCFGR_PLLN >> RCC_PLLCFGR_PLLN_Pos));
  536. assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(pllfreq));
  537. /* - PLLR: ensure that max frequency at 170000000 Hz is reached */
  538. pllfreq = pllfreq / (((UTILS_PLLInitStruct->PLLR >> RCC_PLLCFGR_PLLR_Pos) + 1U) * 2U);
  539. assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq));
  540. return pllfreq;
  541. }
  542. /**
  543. * @brief Function to check that PLL can be modified
  544. * @retval An ErrorStatus enumeration value:
  545. * - SUCCESS: PLL modification can be done
  546. * - ERROR: PLL is busy
  547. */
  548. static ErrorStatus UTILS_PLL_IsBusy(void)
  549. {
  550. ErrorStatus status = SUCCESS;
  551. /* Check if PLL is busy*/
  552. if(LL_RCC_PLL_IsReady() != 0U)
  553. {
  554. /* PLL configuration cannot be modified */
  555. status = ERROR;
  556. }
  557. return status;
  558. }
  559. /**
  560. * @brief Function to enable PLL and switch system clock to PLL
  561. * @param SYSCLK_Frequency SYSCLK frequency
  562. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  563. * the configuration information for the BUS prescalers.
  564. * @retval An ErrorStatus enumeration value:
  565. * - SUCCESS: No problem to switch system to PLL
  566. * - ERROR: Problem to switch system to PLL
  567. */
  568. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  569. {
  570. ErrorStatus status = SUCCESS;
  571. uint32_t hclk_frequency;
  572. assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
  573. assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
  574. assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct->APB2CLKDivider));
  575. /* Calculate HCLK frequency */
  576. hclk_frequency = __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider);
  577. /* Increasing the number of wait states because of higher CPU frequency */
  578. if(SystemCoreClock < hclk_frequency)
  579. {
  580. /* Set FLASH latency to highest latency */
  581. status = LL_SetFlashLatency(hclk_frequency);
  582. }
  583. /* Update system clock configuration */
  584. if(status == SUCCESS)
  585. {
  586. /* Enable PLL */
  587. LL_RCC_PLL_Enable();
  588. LL_RCC_PLL_EnableDomain_SYS();
  589. while (LL_RCC_PLL_IsReady() != 1U)
  590. {
  591. /* Wait for PLL ready */
  592. }
  593. /* Sysclk activation on the main PLL */
  594. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  595. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  596. while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  597. {
  598. /* Wait for system clock switch to PLL */
  599. }
  600. /* Set APB1 & APB2 prescaler*/
  601. LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
  602. LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct->APB2CLKDivider);
  603. }
  604. /* Decreasing the number of wait states because of lower CPU frequency */
  605. if(SystemCoreClock > hclk_frequency)
  606. {
  607. /* Set FLASH latency to lowest latency */
  608. status = LL_SetFlashLatency(hclk_frequency);
  609. }
  610. /* Update SystemCoreClock variable */
  611. if(status == SUCCESS)
  612. {
  613. LL_SetSystemCoreClock(hclk_frequency);
  614. }
  615. return status;
  616. }
  617. /**
  618. * @}
  619. */
  620. /**
  621. * @}
  622. */
  623. /**
  624. * @}
  625. */