stm32l4xx_ll_utils.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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
  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 "stm32l4xx_ll_utils.h"
  20. #include "stm32l4xx_ll_rcc.h"
  21. #include "stm32l4xx_ll_system.h"
  22. #include "stm32l4xx_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 STM32L4xx_LL_Driver
  29. * @{
  30. */
  31. /** @addtogroup UTILS_LL
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /** @addtogroup UTILS_LL_Private_Constants
  38. * @{
  39. */
  40. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  41. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  42. #define UTILS_MAX_FREQUENCY_SCALE1 120000000U /*!< Maximum frequency for system clock at power scale1, in Hz */
  43. #define UTILS_MAX_FREQUENCY_SCALE2 26000000U /*!< Maximum frequency for system clock at power scale2, in Hz */
  44. #else
  45. #define UTILS_MAX_FREQUENCY_SCALE1 80000000U /*!< Maximum frequency for system clock at power scale1, in Hz */
  46. #define UTILS_MAX_FREQUENCY_SCALE2 26000000U /*!< Maximum frequency for system clock at power scale2, in Hz */
  47. #endif
  48. /* Defines used for PLL range */
  49. #define UTILS_PLLVCO_INPUT_MIN 4000000U /*!< Frequency min for PLLVCO input, in Hz */
  50. #define UTILS_PLLVCO_INPUT_MAX 16000000U /*!< Frequency max for PLLVCO input, in Hz */
  51. #define UTILS_PLLVCO_OUTPUT_MIN 64000000U /*!< Frequency min for PLLVCO output, in Hz */
  52. #define UTILS_PLLVCO_OUTPUT_MAX 344000000U /*!< 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 48000000U /*!< Frequency max for HSE frequency, in Hz */
  56. /* Defines used for FLASH latency according to HCLK Frequency */
  57. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  58. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  59. #define UTILS_SCALE1_LATENCY1_FREQ 20000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
  60. #define UTILS_SCALE1_LATENCY2_FREQ 40000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
  61. #define UTILS_SCALE1_LATENCY3_FREQ 60000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
  62. #define UTILS_SCALE1_LATENCY4_FREQ 80000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
  63. #define UTILS_SCALE1_LATENCY5_FREQ 100000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
  64. #define UTILS_SCALE2_LATENCY1_FREQ 8000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
  65. #define UTILS_SCALE2_LATENCY2_FREQ 16000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
  66. #else
  67. #define UTILS_SCALE1_LATENCY1_FREQ 16000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
  68. #define UTILS_SCALE1_LATENCY2_FREQ 32000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
  69. #define UTILS_SCALE1_LATENCY3_FREQ 48000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
  70. #define UTILS_SCALE1_LATENCY4_FREQ 64000000U /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
  71. #define UTILS_SCALE2_LATENCY1_FREQ 6000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
  72. #define UTILS_SCALE2_LATENCY2_FREQ 12000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
  73. #define UTILS_SCALE2_LATENCY3_FREQ 18000000U /*!< HCLK frequency to set FLASH latency 3 in power scale 2 */
  74. #endif
  75. /**
  76. * @}
  77. */
  78. /* Private macros ------------------------------------------------------------*/
  79. /** @addtogroup UTILS_LL_Private_Macros
  80. * @{
  81. */
  82. #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
  83. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
  84. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
  85. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
  86. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
  87. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
  88. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
  89. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
  90. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
  91. #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
  92. || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
  93. || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
  94. || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
  95. || ((__VALUE__) == LL_RCC_APB1_DIV_16))
  96. #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
  97. || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
  98. || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
  99. || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
  100. || ((__VALUE__) == LL_RCC_APB2_DIV_16))
  101. #define IS_LL_UTILS_PLLM_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLM_DIV_1) \
  102. || ((__VALUE__) == LL_RCC_PLLM_DIV_2) \
  103. || ((__VALUE__) == LL_RCC_PLLM_DIV_3) \
  104. || ((__VALUE__) == LL_RCC_PLLM_DIV_4) \
  105. || ((__VALUE__) == LL_RCC_PLLM_DIV_5) \
  106. || ((__VALUE__) == LL_RCC_PLLM_DIV_6) \
  107. || ((__VALUE__) == LL_RCC_PLLM_DIV_7) \
  108. || ((__VALUE__) == LL_RCC_PLLM_DIV_8))
  109. #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((8U <= (__VALUE__)) && ((__VALUE__) <= 86U))
  110. #define IS_LL_UTILS_PLLR_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLR_DIV_2) \
  111. || ((__VALUE__) == LL_RCC_PLLR_DIV_4) \
  112. || ((__VALUE__) == LL_RCC_PLLR_DIV_6) \
  113. || ((__VALUE__) == LL_RCC_PLLR_DIV_8))
  114. #define IS_LL_UTILS_PLLVCO_INPUT(__VALUE__) ((UTILS_PLLVCO_INPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX))
  115. #define IS_LL_UTILS_PLLVCO_OUTPUT(__VALUE__) ((UTILS_PLLVCO_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_OUTPUT_MAX))
  116. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
  117. ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2))
  118. #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
  119. || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
  120. #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
  121. /**
  122. * @}
  123. */
  124. /* Private function prototypes -----------------------------------------------*/
  125. /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
  126. * @{
  127. */
  128. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency,
  129. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
  130. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
  131. static ErrorStatus UTILS_PLL_IsBusy(void);
  132. /**
  133. * @}
  134. */
  135. /* Exported functions --------------------------------------------------------*/
  136. /** @addtogroup UTILS_LL_Exported_Functions
  137. * @{
  138. */
  139. /** @addtogroup UTILS_LL_EF_DELAY
  140. * @{
  141. */
  142. /**
  143. * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
  144. * @note When a RTOS is used, it is recommended to avoid changing the Systick
  145. * configuration by calling this function, for a delay use rather osDelay RTOS service.
  146. * @param HCLKFrequency HCLK frequency in Hz
  147. * @note HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq
  148. * @retval None
  149. */
  150. void LL_Init1msTick(uint32_t HCLKFrequency)
  151. {
  152. /* Use frequency provided in argument */
  153. LL_InitTick(HCLKFrequency, 1000U);
  154. }
  155. /**
  156. * @brief This function provides accurate delay (in milliseconds) based
  157. * on SysTick counter flag
  158. * @note When a RTOS is used, it is recommended to avoid using blocking delay
  159. * and use rather osDelay service.
  160. * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
  161. * will configure Systick to 1ms
  162. * @param Delay specifies the delay time length, in milliseconds.
  163. * @retval None
  164. */
  165. void LL_mDelay(uint32_t Delay)
  166. {
  167. __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
  168. uint32_t tmpDelay = Delay;
  169. /* Add this code to indicate that local variable is not used */
  170. ((void)tmp);
  171. /* Add a period to guaranty minimum wait */
  172. if(tmpDelay < LL_MAX_DELAY)
  173. {
  174. tmpDelay++;
  175. }
  176. while (tmpDelay != 0U)
  177. {
  178. if((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
  179. {
  180. tmpDelay--;
  181. }
  182. }
  183. }
  184. /**
  185. * @}
  186. */
  187. /** @addtogroup UTILS_EF_SYSTEM
  188. * @brief System Configuration functions
  189. *
  190. @verbatim
  191. ===============================================================================
  192. ##### System Configuration functions #####
  193. ===============================================================================
  194. [..]
  195. System, AHB and APB buses clocks configuration
  196. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is
  197. 120000000 Hz for STM32L4Rx/STM32L4Sx devices and 80000000 Hz for others.
  198. @endverbatim
  199. @internal
  200. Depending on the device voltage range, the maximum frequency should be
  201. adapted accordingly:
  202. (++) Table 1. HCLK clock frequency for STM32L4+ Series devices
  203. (++) +--------------------------------------------------------+
  204. (++) | Latency | HCLK clock frequency (MHz) |
  205. (++) | |--------------------------------------|
  206. (++) | | voltage range 1 | voltage range 2 |
  207. (++) | | 1.2 V | 1.0 V |
  208. (++) |-----------------|-------------------|------------------|
  209. (++) |0WS(1 CPU cycles)| 0 < HCLK <= 20 | 0 < HCLK <= 8 |
  210. (++) |-----------------|-------------------|------------------|
  211. (++) |1WS(2 CPU cycles)| 20 < HCLK <= 40 | 8 < HCLK <= 16 |
  212. (++) |-----------------|-------------------|------------------|
  213. (++) |2WS(3 CPU cycles)| 40 < HCLK <= 60 | 16 < HCLK <= 26 |
  214. (++) |-----------------|-------------------|------------------|
  215. (++) |3WS(4 CPU cycles)| 60 < HCLK <= 80 | 16 < HCLK <= 26 |
  216. (++) |-----------------|-------------------|------------------|
  217. (++) |4WS(5 CPU cycles)| 80 < HCLK <= 100 | 16 < HCLK <= 26 |
  218. (++) |-----------------|-------------------|------------------|
  219. (++) |5WS(6 CPU cycles)| 100 < HCLK <= 120 | 16 < HCLK <= 26 |
  220. (++) +--------------------------------------------------------+
  221. (++) Table 2. HCLK clock frequency for STM32L4 Series devices
  222. (++) +-------------------------------------------------------+
  223. (++) | Latency | HCLK clock frequency (MHz) |
  224. (++) | |-------------------------------------|
  225. (++) | | voltage range 1 | voltage range 2 |
  226. (++) | | 1.2 V | 1.0 V |
  227. (++) |-----------------|------------------|------------------|
  228. (++) |0WS(1 CPU cycles)| 0 < HCLK <= 16 | 0 < HCLK <= 6 |
  229. (++) |-----------------|------------------|------------------|
  230. (++) |1WS(2 CPU cycles)| 16 < HCLK <= 32 | 6 < HCLK <= 12 |
  231. (++) |-----------------|------------------|------------------|
  232. (++) |2WS(3 CPU cycles)| 32 < HCLK <= 48 | 12 < HCLK <= 18 |
  233. (++) |-----------------|------------------|------------------|
  234. (++) |3WS(4 CPU cycles)| 48 < HCLK <= 64 | 18 < HCLK <= 26 |
  235. (++) |-----------------|------------------|------------------|
  236. (++) |4WS(5 CPU cycles)| 64 < HCLK <= 80 | 18 < HCLK <= 26 |
  237. (++) +-------------------------------------------------------+
  238. @endinternal
  239. * @{
  240. */
  241. /**
  242. * @brief This function sets directly SystemCoreClock CMSIS variable.
  243. * @note Variable can be calculated also through SystemCoreClockUpdate function.
  244. * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
  245. * @retval None
  246. */
  247. void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
  248. {
  249. /* HCLK clock frequency */
  250. SystemCoreClock = HCLKFrequency;
  251. }
  252. /**
  253. * @brief Update number of Flash wait states in line with new frequency and current
  254. voltage range.
  255. * @param HCLKFrequency HCLK frequency
  256. * @retval An ErrorStatus enumeration value:
  257. * - SUCCESS: Latency has been modified
  258. * - ERROR: Latency cannot be modified
  259. */
  260. ErrorStatus LL_SetFlashLatency(uint32_t HCLKFrequency)
  261. {
  262. ErrorStatus status = SUCCESS;
  263. uint32_t latency = LL_FLASH_LATENCY_0; /* default value 0WS */
  264. /* Frequency cannot be equal to 0 or greater than max clock */
  265. if ((HCLKFrequency == 0U) || (HCLKFrequency > UTILS_MAX_FREQUENCY_SCALE1))
  266. {
  267. status = ERROR;
  268. }
  269. else
  270. {
  271. if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1)
  272. {
  273. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  274. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  275. if(HCLKFrequency > UTILS_SCALE1_LATENCY5_FREQ)
  276. {
  277. /* 100 < HCLK <= 120 => 5WS (6 CPU cycles) */
  278. latency = LL_FLASH_LATENCY_5;
  279. }
  280. else if(HCLKFrequency > UTILS_SCALE1_LATENCY4_FREQ)
  281. {
  282. /* 80 < HCLK <= 100 => 4WS (5 CPU cycles) */
  283. latency = LL_FLASH_LATENCY_4;
  284. }
  285. else if(HCLKFrequency > UTILS_SCALE1_LATENCY3_FREQ)
  286. {
  287. /* 60 < HCLK <= 80 => 3WS (4 CPU cycles) */
  288. latency = LL_FLASH_LATENCY_3;
  289. }
  290. else if(HCLKFrequency > UTILS_SCALE1_LATENCY2_FREQ)
  291. {
  292. /* 40 < HCLK <= 20 => 2WS (3 CPU cycles) */
  293. latency = LL_FLASH_LATENCY_2;
  294. }
  295. else
  296. {
  297. if(HCLKFrequency > UTILS_SCALE1_LATENCY1_FREQ)
  298. {
  299. /* 20 < HCLK <= 40 => 1WS (2 CPU cycles) */
  300. latency = LL_FLASH_LATENCY_1;
  301. }
  302. /* else HCLKFrequency <= 10MHz default LL_FLASH_LATENCY_0 0WS */
  303. }
  304. #else
  305. if(HCLKFrequency > UTILS_SCALE1_LATENCY4_FREQ)
  306. {
  307. /* 64 < HCLK <= 80 => 4WS (5 CPU cycles) */
  308. latency = LL_FLASH_LATENCY_4;
  309. }
  310. else if(HCLKFrequency > UTILS_SCALE1_LATENCY3_FREQ)
  311. {
  312. /* 48 < HCLK <= 64 => 3WS (4 CPU cycles) */
  313. latency = LL_FLASH_LATENCY_3;
  314. }
  315. else if(HCLKFrequency > UTILS_SCALE1_LATENCY2_FREQ)
  316. {
  317. /* 32 < HCLK <= 48 => 2WS (3 CPU cycles) */
  318. latency = LL_FLASH_LATENCY_2;
  319. }
  320. else
  321. {
  322. if(HCLKFrequency > UTILS_SCALE1_LATENCY1_FREQ)
  323. {
  324. /* 16 < HCLK <= 32 => 1WS (2 CPU cycles) */
  325. latency = LL_FLASH_LATENCY_1;
  326. }
  327. /* else HCLKFrequency <= 16MHz default LL_FLASH_LATENCY_0 0WS */
  328. }
  329. #endif
  330. }
  331. else /* SCALE2 */
  332. {
  333. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  334. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  335. if(HCLKFrequency > UTILS_MAX_FREQUENCY_SCALE2)
  336. {
  337. /* Unexpected HCLK > 26 */
  338. status = ERROR;
  339. }
  340. else if(HCLKFrequency > UTILS_SCALE2_LATENCY2_FREQ)
  341. {
  342. /* 16 < HCLK <= 26 => 2WS (3 CPU cycles) */
  343. latency = LL_FLASH_LATENCY_2;
  344. }
  345. else
  346. {
  347. if(HCLKFrequency > UTILS_SCALE2_LATENCY1_FREQ)
  348. {
  349. /* 8 < HCLK <= 16 => 1WS (2 CPU cycles) */
  350. latency = LL_FLASH_LATENCY_1;
  351. }
  352. /* else HCLKFrequency <= 8MHz default LL_FLASH_LATENCY_0 0WS */
  353. }
  354. #else
  355. if(HCLKFrequency > UTILS_MAX_FREQUENCY_SCALE2)
  356. {
  357. /* Unexpected HCLK > 26 */
  358. status = ERROR;
  359. }
  360. else if(HCLKFrequency > UTILS_SCALE2_LATENCY3_FREQ)
  361. {
  362. /* 18 < HCLK <= 26 => 3WS (4 CPU cycles) */
  363. latency = LL_FLASH_LATENCY_3;
  364. }
  365. else if(HCLKFrequency > UTILS_SCALE2_LATENCY2_FREQ)
  366. {
  367. /* 12 < HCLK <= 18 => 2WS (3 CPU cycles) */
  368. latency = LL_FLASH_LATENCY_2;
  369. }
  370. else
  371. {
  372. if(HCLKFrequency > UTILS_SCALE2_LATENCY1_FREQ)
  373. {
  374. /* 6 < HCLK <= 12 => 1WS (2 CPU cycles) */
  375. latency = LL_FLASH_LATENCY_1;
  376. }
  377. /* else HCLKFrequency <= 6MHz default LL_FLASH_LATENCY_0 0WS */
  378. }
  379. #endif
  380. }
  381. LL_FLASH_SetLatency(latency);
  382. /* Check that the new number of wait states is taken into account to access the Flash
  383. memory by reading the FLASH_ACR register */
  384. if(LL_FLASH_GetLatency() != latency)
  385. {
  386. status = ERROR;
  387. }
  388. }
  389. return status;
  390. }
  391. /**
  392. * @brief This function configures system clock with MSI as clock source of the PLL
  393. * @note The application needs to ensure that PLL, PLLSAI1 and/or PLLSAI2 are disabled.
  394. * @note Function is based on the following formula:
  395. * - PLL output frequency = (((MSI frequency / PLLM) * PLLN) / PLLR)
  396. * - PLLM: ensure that the VCO input frequency ranges from 4 to 16 MHz (PLLVCO_input = MSI frequency / PLLM)
  397. * - PLLN: ensure that the VCO output frequency is between 64 and 344 MHz (PLLVCO_output = PLLVCO_input * PLLN)
  398. * - PLLR: ensure that max frequency at 120000000 Hz is reached (PLLVCO_output / PLLR)
  399. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  400. * the configuration information for the PLL.
  401. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  402. * the configuration information for the BUS prescalers.
  403. * @retval An ErrorStatus enumeration value:
  404. * - SUCCESS: Max frequency configuration done
  405. * - ERROR: Max frequency configuration not done
  406. */
  407. ErrorStatus LL_PLL_ConfigSystemClock_MSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  408. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  409. {
  410. ErrorStatus status = SUCCESS;
  411. uint32_t pllfreq, msi_range;
  412. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  413. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  414. uint32_t hpre = 0U; /* Set default value */
  415. #endif
  416. /* Check if one of the PLL is enabled */
  417. if(UTILS_PLL_IsBusy() == SUCCESS)
  418. {
  419. /* Get the current MSI range */
  420. if(LL_RCC_MSI_IsEnabledRangeSelect() != 0U)
  421. {
  422. msi_range = LL_RCC_MSI_GetRange();
  423. switch (msi_range)
  424. {
  425. case LL_RCC_MSIRANGE_0: /* MSI = 100 KHz */
  426. case LL_RCC_MSIRANGE_1: /* MSI = 200 KHz */
  427. case LL_RCC_MSIRANGE_2: /* MSI = 400 KHz */
  428. case LL_RCC_MSIRANGE_3: /* MSI = 800 KHz */
  429. case LL_RCC_MSIRANGE_4: /* MSI = 1 MHz */
  430. case LL_RCC_MSIRANGE_5: /* MSI = 2 MHz */
  431. /* PLLVCO input frequency is not in the range from 4 to 16 MHz*/
  432. status = ERROR;
  433. break;
  434. case LL_RCC_MSIRANGE_6: /* MSI = 4 MHz */
  435. case LL_RCC_MSIRANGE_7: /* MSI = 8 MHz */
  436. case LL_RCC_MSIRANGE_8: /* MSI = 16 MHz */
  437. case LL_RCC_MSIRANGE_9: /* MSI = 24 MHz */
  438. case LL_RCC_MSIRANGE_10: /* MSI = 32 MHz */
  439. case LL_RCC_MSIRANGE_11: /* MSI = 48 MHz */
  440. default:
  441. break;
  442. }
  443. }
  444. else
  445. {
  446. msi_range = LL_RCC_MSI_GetRangeAfterStandby();
  447. switch (msi_range)
  448. {
  449. case LL_RCC_MSISRANGE_4: /* MSI = 1 MHz */
  450. case LL_RCC_MSISRANGE_5: /* MSI = 2 MHz */
  451. /* PLLVCO input frequency is not in the range from 4 to 16 MHz*/
  452. status = ERROR;
  453. break;
  454. case LL_RCC_MSISRANGE_7: /* MSI = 8 MHz */
  455. case LL_RCC_MSISRANGE_6: /* MSI = 4 MHz */
  456. default:
  457. break;
  458. }
  459. }
  460. /* Main PLL configuration and activation */
  461. if(status != ERROR)
  462. {
  463. /* Calculate the new PLL output frequency */
  464. pllfreq = UTILS_GetPLLOutputFrequency(__LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_IsEnabledRangeSelect(), msi_range),
  465. UTILS_PLLInitStruct);
  466. /* Enable MSI if not enabled */
  467. if(LL_RCC_MSI_IsReady() != 1U)
  468. {
  469. LL_RCC_MSI_Enable();
  470. while ((LL_RCC_MSI_IsReady() != 1U))
  471. {
  472. /* Wait for MSI ready */
  473. }
  474. }
  475. /* Configure PLL */
  476. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_MSI, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  477. UTILS_PLLInitStruct->PLLR);
  478. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  479. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  480. /* Prevent undershoot at highest frequency by applying intermediate AHB prescaler 2 */
  481. if(pllfreq > 80000000U)
  482. {
  483. if(UTILS_ClkInitStruct->AHBCLKDivider == LL_RCC_SYSCLK_DIV_1)
  484. {
  485. UTILS_ClkInitStruct->AHBCLKDivider = LL_RCC_SYSCLK_DIV_2;
  486. hpre = LL_RCC_SYSCLK_DIV_2;
  487. }
  488. }
  489. #endif
  490. /* Enable PLL and switch system clock to PLL */
  491. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  492. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  493. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  494. /* Apply definitive AHB prescaler value if necessary */
  495. if((status == SUCCESS) && (hpre != LL_RCC_SYSCLK_DIV_1))
  496. {
  497. /* Set FLASH latency to highest latency */
  498. status = LL_SetFlashLatency(pllfreq);
  499. if(status == SUCCESS)
  500. {
  501. UTILS_ClkInitStruct->AHBCLKDivider = LL_RCC_SYSCLK_DIV_1;
  502. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  503. LL_SetSystemCoreClock(pllfreq);
  504. }
  505. }
  506. #endif
  507. }
  508. }
  509. else
  510. {
  511. /* Current PLL configuration cannot be modified */
  512. status = ERROR;
  513. }
  514. return status;
  515. }
  516. /**
  517. * @brief This function configures system clock at maximum frequency with HSI as clock source of the PLL
  518. * @note The application need to ensure that PLL, PLLSAI1 and/or PLLSAI2 are disabled.
  519. * @note Function is based on the following formula:
  520. * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLR)
  521. * - PLLM: ensure that the VCO input frequency ranges from 4 to 16 MHz (PLLVCO_input = HSI frequency / PLLM)
  522. * - PLLN: ensure that the VCO output frequency is between 64 and 344 MHz (PLLVCO_output = PLLVCO_input * PLLN)
  523. * - PLLR: ensure that max frequency at 120000000 Hz is reach (PLLVCO_output / PLLR)
  524. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  525. * the configuration information for the PLL.
  526. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  527. * the configuration information for the BUS prescalers.
  528. * @retval An ErrorStatus enumeration value:
  529. * - SUCCESS: Max frequency configuration done
  530. * - ERROR: Max frequency configuration not done
  531. */
  532. ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  533. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  534. {
  535. ErrorStatus status;
  536. uint32_t pllfreq;
  537. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  538. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  539. uint32_t hpre = LL_RCC_SYSCLK_DIV_1; /* Set default value */
  540. #endif
  541. /* Check if one of the PLL is enabled */
  542. if(UTILS_PLL_IsBusy() == SUCCESS)
  543. {
  544. /* Calculate the new PLL output frequency */
  545. pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct);
  546. /* Enable HSI if not enabled */
  547. if(LL_RCC_HSI_IsReady() != 1U)
  548. {
  549. LL_RCC_HSI_Enable();
  550. while (LL_RCC_HSI_IsReady() != 1U)
  551. {
  552. /* Wait for HSI ready */
  553. }
  554. }
  555. /* Configure PLL */
  556. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  557. UTILS_PLLInitStruct->PLLR);
  558. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  559. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  560. /* Prevent undershoot at highest frequency by applying intermediate AHB prescaler 2 */
  561. if(pllfreq > 80000000U)
  562. {
  563. if(UTILS_ClkInitStruct->AHBCLKDivider == LL_RCC_SYSCLK_DIV_1)
  564. {
  565. UTILS_ClkInitStruct->AHBCLKDivider = LL_RCC_SYSCLK_DIV_2;
  566. hpre = LL_RCC_SYSCLK_DIV_2;
  567. }
  568. }
  569. #endif
  570. /* Enable PLL and switch system clock to PLL */
  571. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  572. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  573. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  574. /* Apply definitive AHB prescaler value if necessary */
  575. if((status == SUCCESS) && (hpre != LL_RCC_SYSCLK_DIV_1))
  576. {
  577. /* Set FLASH latency to highest latency */
  578. status = LL_SetFlashLatency(pllfreq);
  579. if(status == SUCCESS)
  580. {
  581. UTILS_ClkInitStruct->AHBCLKDivider = LL_RCC_SYSCLK_DIV_1;
  582. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  583. LL_SetSystemCoreClock(pllfreq);
  584. }
  585. }
  586. #endif
  587. }
  588. else
  589. {
  590. /* Current PLL configuration cannot be modified */
  591. status = ERROR;
  592. }
  593. return status;
  594. }
  595. /**
  596. * @brief This function configures system clock with HSE as clock source of the PLL
  597. * @note The application need to ensure that PLL, PLLSAI1 and/or PLLSAI2 are disabled.
  598. * @note Function is based on the following formula:
  599. * - PLL output frequency = (((HSE frequency / PLLM) * PLLN) / PLLR)
  600. * - PLLM: ensure that the VCO input frequency ranges from 4 to 16 MHz (PLLVCO_input = HSE frequency / PLLM)
  601. * - PLLN: ensure that the VCO output frequency is between 64 and 344 MHz (PLLVCO_output = PLLVCO_input * PLLN)
  602. * - PLLR: ensure that max frequency at 120000000 Hz is reached (PLLVCO_output / PLLR)
  603. * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 48000000
  604. * @param HSEBypass This parameter can be one of the following values:
  605. * @arg @ref LL_UTILS_HSEBYPASS_ON
  606. * @arg @ref LL_UTILS_HSEBYPASS_OFF
  607. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  608. * the configuration information for the PLL.
  609. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  610. * the configuration information for the BUS prescalers.
  611. * @retval An ErrorStatus enumeration value:
  612. * - SUCCESS: Max frequency configuration done
  613. * - ERROR: Max frequency configuration not done
  614. */
  615. ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
  616. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  617. {
  618. ErrorStatus status;
  619. uint32_t pllfreq;
  620. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  621. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  622. uint32_t hpre = 0U; /* Set default value */
  623. #endif
  624. /* Check the parameters */
  625. assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
  626. assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
  627. /* Check if one of the PLL is enabled */
  628. if(UTILS_PLL_IsBusy() == SUCCESS)
  629. {
  630. /* Calculate the new PLL output frequency */
  631. pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
  632. /* Enable HSE if not enabled */
  633. if(LL_RCC_HSE_IsReady() != 1U)
  634. {
  635. /* Check if need to enable HSE bypass feature or not */
  636. if(HSEBypass == LL_UTILS_HSEBYPASS_ON)
  637. {
  638. LL_RCC_HSE_EnableBypass();
  639. }
  640. else
  641. {
  642. LL_RCC_HSE_DisableBypass();
  643. }
  644. /* Enable HSE */
  645. LL_RCC_HSE_Enable();
  646. while (LL_RCC_HSE_IsReady() != 1U)
  647. {
  648. /* Wait for HSE ready */
  649. }
  650. }
  651. /* Configure PLL */
  652. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  653. UTILS_PLLInitStruct->PLLR);
  654. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  655. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  656. /* Prevent undershoot at highest frequency by applying intermediate AHB prescaler 2 */
  657. if(pllfreq > 80000000U)
  658. {
  659. if(UTILS_ClkInitStruct->AHBCLKDivider == LL_RCC_SYSCLK_DIV_1)
  660. {
  661. UTILS_ClkInitStruct->AHBCLKDivider = LL_RCC_SYSCLK_DIV_2;
  662. hpre = LL_RCC_SYSCLK_DIV_2;
  663. }
  664. }
  665. #endif
  666. /* Enable PLL and switch system clock to PLL */
  667. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  668. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  669. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  670. /* Apply definitive AHB prescaler value if necessary */
  671. if((status == SUCCESS) && (hpre != LL_RCC_SYSCLK_DIV_1))
  672. {
  673. /* Set FLASH latency to highest latency */
  674. status = LL_SetFlashLatency(pllfreq);
  675. if(status == SUCCESS)
  676. {
  677. UTILS_ClkInitStruct->AHBCLKDivider = LL_RCC_SYSCLK_DIV_1;
  678. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  679. LL_SetSystemCoreClock(pllfreq);
  680. }
  681. }
  682. #endif
  683. }
  684. else
  685. {
  686. /* Current PLL configuration cannot be modified */
  687. status = ERROR;
  688. }
  689. return status;
  690. }
  691. /**
  692. * @}
  693. */
  694. /**
  695. * @}
  696. */
  697. /** @addtogroup UTILS_LL_Private_Functions
  698. * @{
  699. */
  700. /**
  701. * @brief Function to check that PLL can be modified
  702. * @param PLL_InputFrequency PLL input frequency (in Hz)
  703. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  704. * the configuration information for the PLL.
  705. * @retval PLL output frequency (in Hz)
  706. */
  707. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
  708. {
  709. uint32_t pllfreq;
  710. /* Check the parameters */
  711. assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
  712. assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
  713. assert_param(IS_LL_UTILS_PLLR_VALUE(UTILS_PLLInitStruct->PLLR));
  714. /* Check different PLL parameters according to RM */
  715. /* - PLLM: ensure that the VCO input frequency ranges from 4 to 16 MHz. */
  716. pllfreq = PLL_InputFrequency / (((UTILS_PLLInitStruct->PLLM >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  717. assert_param(IS_LL_UTILS_PLLVCO_INPUT(pllfreq));
  718. /* - PLLN: ensure that the VCO output frequency is between 64 and 344 MHz.*/
  719. pllfreq = pllfreq * (UTILS_PLLInitStruct->PLLN & (RCC_PLLCFGR_PLLN >> RCC_PLLCFGR_PLLN_Pos));
  720. assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(pllfreq));
  721. /* - PLLR: ensure that max frequency at 120000000 Hz is reached */
  722. pllfreq = pllfreq / (((UTILS_PLLInitStruct->PLLR >> RCC_PLLCFGR_PLLR_Pos) + 1U) * 2U);
  723. assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq));
  724. return pllfreq;
  725. }
  726. /**
  727. * @brief Function to check that PLL can be modified
  728. * @retval An ErrorStatus enumeration value:
  729. * - SUCCESS: PLL modification can be done
  730. * - ERROR: PLL is busy
  731. */
  732. static ErrorStatus UTILS_PLL_IsBusy(void)
  733. {
  734. ErrorStatus status = SUCCESS;
  735. /* Check if PLL is busy*/
  736. if(LL_RCC_PLL_IsReady() != 0U)
  737. {
  738. /* PLL configuration cannot be modified */
  739. status = ERROR;
  740. }
  741. #if defined(RCC_PLLSAI1_SUPPORT)
  742. /* Check if PLLSAI1 is busy*/
  743. if(LL_RCC_PLLSAI1_IsReady() != 0U)
  744. {
  745. /* PLLSAI1 configuration cannot be modified */
  746. status = ERROR;
  747. }
  748. #endif /*RCC_PLLSAI1_SUPPORT*/
  749. #if defined(RCC_PLLSAI2_SUPPORT)
  750. /* Check if PLLSAI2 is busy*/
  751. if(LL_RCC_PLLSAI2_IsReady() != 0U)
  752. {
  753. /* PLLSAI2 configuration cannot be modified */
  754. status = ERROR;
  755. }
  756. #endif /*RCC_PLLSAI2_SUPPORT*/
  757. return status;
  758. }
  759. /**
  760. * @brief Function to enable PLL and switch system clock to PLL
  761. * @param SYSCLK_Frequency SYSCLK frequency
  762. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  763. * the configuration information for the BUS prescalers.
  764. * @retval An ErrorStatus enumeration value:
  765. * - SUCCESS: No problem to switch system to PLL
  766. * - ERROR: Problem to switch system to PLL
  767. */
  768. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  769. {
  770. ErrorStatus status = SUCCESS;
  771. uint32_t hclk_frequency;
  772. assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
  773. assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
  774. assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct->APB2CLKDivider));
  775. /* Calculate HCLK frequency */
  776. hclk_frequency = __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider);
  777. /* Increasing the number of wait states because of higher CPU frequency */
  778. if(SystemCoreClock < hclk_frequency)
  779. {
  780. /* Set FLASH latency to highest latency */
  781. status = LL_SetFlashLatency(hclk_frequency);
  782. }
  783. /* Update system clock configuration */
  784. if(status == SUCCESS)
  785. {
  786. /* Enable PLL */
  787. LL_RCC_PLL_Enable();
  788. LL_RCC_PLL_EnableDomain_SYS();
  789. while (LL_RCC_PLL_IsReady() != 1U)
  790. {
  791. /* Wait for PLL ready */
  792. }
  793. /* Sysclk activation on the main PLL */
  794. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  795. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  796. while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  797. {
  798. /* Wait for system clock switch to PLL */
  799. }
  800. /* Set APB1 & APB2 prescaler*/
  801. LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
  802. LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct->APB2CLKDivider);
  803. }
  804. /* Decreasing the number of wait states because of lower CPU frequency */
  805. if(SystemCoreClock > hclk_frequency)
  806. {
  807. /* Set FLASH latency to lowest latency */
  808. status = LL_SetFlashLatency(hclk_frequency);
  809. }
  810. /* Update SystemCoreClock variable */
  811. if(status == SUCCESS)
  812. {
  813. LL_SetSystemCoreClock(hclk_frequency);
  814. }
  815. return status;
  816. }
  817. /**
  818. * @}
  819. */
  820. /**
  821. * @}
  822. */
  823. /**
  824. * @}
  825. */