You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

581 lines
22 KiB

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