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.

stm32f1xx_ll_utils.c 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_ll_utils.c
  4. * @author MCD Application Team
  5. * @brief UTILS LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2016 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 "stm32f1xx_ll_rcc.h"
  21. #include "stm32f1xx_ll_utils.h"
  22. #include "stm32f1xx_ll_system.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif
  28. /** @addtogroup STM32F1xx_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. /* Defines used for PLL range */
  41. #define UTILS_PLL_OUTPUT_MAX RCC_MAX_FREQUENCY /*!< Frequency max for PLL output, in Hz */
  42. #define UTILS_PLL2_OUTPUT_MAX RCC_MAX_FREQUENCY /*!< Frequency max for PLL2 output, in Hz */
  43. /* Defines used for HSE range */
  44. #define UTILS_HSE_FREQUENCY_MIN RCC_HSE_MIN /*!< Frequency min for HSE frequency, in Hz */
  45. #define UTILS_HSE_FREQUENCY_MAX RCC_HSE_MAX /*!< Frequency max for HSE frequency, in Hz */
  46. /* Defines used for FLASH latency according to HCLK Frequency */
  47. #if defined(FLASH_ACR_LATENCY)
  48. #define UTILS_LATENCY1_FREQ 24000000U /*!< SYSCLK frequency to set FLASH latency 1 */
  49. #define UTILS_LATENCY2_FREQ 48000000U /*!< SYSCLK frequency to set FLASH latency 2 */
  50. #else
  51. /*!< No Latency Configuration in this device */
  52. #endif
  53. /**
  54. * @}
  55. */
  56. /* Private macros ------------------------------------------------------------*/
  57. /** @addtogroup UTILS_LL_Private_Macros
  58. * @{
  59. */
  60. #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
  61. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
  62. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
  63. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
  64. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
  65. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
  66. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
  67. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
  68. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
  69. #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
  70. || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
  71. || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
  72. || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
  73. || ((__VALUE__) == LL_RCC_APB1_DIV_16))
  74. #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
  75. || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
  76. || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
  77. || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
  78. || ((__VALUE__) == LL_RCC_APB2_DIV_16))
  79. #if defined(RCC_CFGR_PLLMULL6_5)
  80. #define IS_LL_UTILS_PLLMUL_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLL_MUL_4) \
  81. || ((__VALUE__) == LL_RCC_PLL_MUL_5) \
  82. || ((__VALUE__) == LL_RCC_PLL_MUL_6) \
  83. || ((__VALUE__) == LL_RCC_PLL_MUL_7) \
  84. || ((__VALUE__) == LL_RCC_PLL_MUL_8) \
  85. || ((__VALUE__) == LL_RCC_PLL_MUL_9) \
  86. || ((__VALUE__) == LL_RCC_PLL_MUL_6_5))
  87. #else
  88. #define IS_LL_UTILS_PLLMUL_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLL_MUL_2) \
  89. || ((__VALUE__) == LL_RCC_PLL_MUL_3) \
  90. || ((__VALUE__) == LL_RCC_PLL_MUL_4) \
  91. || ((__VALUE__) == LL_RCC_PLL_MUL_5) \
  92. || ((__VALUE__) == LL_RCC_PLL_MUL_6) \
  93. || ((__VALUE__) == LL_RCC_PLL_MUL_7) \
  94. || ((__VALUE__) == LL_RCC_PLL_MUL_8) \
  95. || ((__VALUE__) == LL_RCC_PLL_MUL_9) \
  96. || ((__VALUE__) == LL_RCC_PLL_MUL_10) \
  97. || ((__VALUE__) == LL_RCC_PLL_MUL_11) \
  98. || ((__VALUE__) == LL_RCC_PLL_MUL_12) \
  99. || ((__VALUE__) == LL_RCC_PLL_MUL_13) \
  100. || ((__VALUE__) == LL_RCC_PLL_MUL_14) \
  101. || ((__VALUE__) == LL_RCC_PLL_MUL_15) \
  102. || ((__VALUE__) == LL_RCC_PLL_MUL_16))
  103. #endif /* RCC_CFGR_PLLMULL6_5 */
  104. #if defined(RCC_CFGR2_PREDIV1)
  105. #define IS_LL_UTILS_PREDIV_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PREDIV_DIV_1) || ((__VALUE__) == LL_RCC_PREDIV_DIV_2) || \
  106. ((__VALUE__) == LL_RCC_PREDIV_DIV_3) || ((__VALUE__) == LL_RCC_PREDIV_DIV_4) || \
  107. ((__VALUE__) == LL_RCC_PREDIV_DIV_5) || ((__VALUE__) == LL_RCC_PREDIV_DIV_6) || \
  108. ((__VALUE__) == LL_RCC_PREDIV_DIV_7) || ((__VALUE__) == LL_RCC_PREDIV_DIV_8) || \
  109. ((__VALUE__) == LL_RCC_PREDIV_DIV_9) || ((__VALUE__) == LL_RCC_PREDIV_DIV_10) || \
  110. ((__VALUE__) == LL_RCC_PREDIV_DIV_11) || ((__VALUE__) == LL_RCC_PREDIV_DIV_12) || \
  111. ((__VALUE__) == LL_RCC_PREDIV_DIV_13) || ((__VALUE__) == LL_RCC_PREDIV_DIV_14) || \
  112. ((__VALUE__) == LL_RCC_PREDIV_DIV_15) || ((__VALUE__) == LL_RCC_PREDIV_DIV_16))
  113. #else
  114. #define IS_LL_UTILS_PREDIV_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PREDIV_DIV_1) || ((__VALUE__) == LL_RCC_PREDIV_DIV_2))
  115. #endif /*RCC_PREDIV1_DIV_2_16_SUPPORT*/
  116. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((__VALUE__) <= UTILS_PLL_OUTPUT_MAX)
  117. #if defined(RCC_PLL2_SUPPORT)
  118. #define IS_LL_UTILS_PLL2MUL_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLL2_MUL_8) \
  119. || ((__VALUE__) == LL_RCC_PLL2_MUL_9) \
  120. || ((__VALUE__) == LL_RCC_PLL2_MUL_10) \
  121. || ((__VALUE__) == LL_RCC_PLL2_MUL_11) \
  122. || ((__VALUE__) == LL_RCC_PLL2_MUL_12) \
  123. || ((__VALUE__) == LL_RCC_PLL2_MUL_13) \
  124. || ((__VALUE__) == LL_RCC_PLL2_MUL_14) \
  125. || ((__VALUE__) == LL_RCC_PLL2_MUL_16) \
  126. || ((__VALUE__) == LL_RCC_PLL2_MUL_20))
  127. #define IS_LL_UTILS_PREDIV2_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_1) || ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_2) || \
  128. ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_3) || ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_4) || \
  129. ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_5) || ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_6) || \
  130. ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_7) || ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_8) || \
  131. ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_9) || ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_10) || \
  132. ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_11) || ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_12) || \
  133. ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_13) || ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_14) || \
  134. ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_15) || ((__VALUE__) == LL_RCC_HSE_PREDIV2_DIV_16))
  135. #define IS_LL_UTILS_PLL2_FREQUENCY(__VALUE__) ((__VALUE__) <= UTILS_PLL2_OUTPUT_MAX)
  136. #endif /* RCC_PLL2_SUPPORT */
  137. #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
  138. || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
  139. #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
  140. /**
  141. * @}
  142. */
  143. /* Private function prototypes -----------------------------------------------*/
  144. /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
  145. * @{
  146. */
  147. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency,
  148. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
  149. static ErrorStatus UTILS_PLL_HSE_ConfigSystemClock(uint32_t PLL_InputFrequency, uint32_t HSEBypass,
  150. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  151. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
  152. #if defined(RCC_PLL2_SUPPORT)
  153. static uint32_t UTILS_GetPLL2OutputFrequency(uint32_t PLL2_InputFrequency,
  154. LL_UTILS_PLLInitTypeDef *UTILS_PLL2InitStruct);
  155. #endif /* RCC_PLL2_SUPPORT */
  156. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
  157. static ErrorStatus UTILS_PLL_IsBusy(void);
  158. /**
  159. * @}
  160. */
  161. /* Exported functions --------------------------------------------------------*/
  162. /** @addtogroup UTILS_LL_Exported_Functions
  163. * @{
  164. */
  165. /** @addtogroup UTILS_LL_EF_DELAY
  166. * @{
  167. */
  168. /**
  169. * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
  170. * @note When a RTOS is used, it is recommended to avoid changing the Systick
  171. * configuration by calling this function, for a delay use rather osDelay RTOS service.
  172. * @param HCLKFrequency HCLK frequency in Hz
  173. * @note HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq
  174. * @retval None
  175. */
  176. void LL_Init1msTick(uint32_t HCLKFrequency)
  177. {
  178. /* Use frequency provided in argument */
  179. LL_InitTick(HCLKFrequency, 1000U);
  180. }
  181. /**
  182. * @brief This function provides accurate delay (in milliseconds) based
  183. * on SysTick counter flag
  184. * @note When a RTOS is used, it is recommended to avoid using blocking delay
  185. * and use rather osDelay service.
  186. * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
  187. * will configure Systick to 1ms
  188. * @param Delay specifies the delay time length, in milliseconds.
  189. * @retval None
  190. */
  191. void LL_mDelay(uint32_t Delay)
  192. {
  193. __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
  194. /* Add this code to indicate that local variable is not used */
  195. ((void)tmp);
  196. /* Add a period to guaranty minimum wait */
  197. if (Delay < LL_MAX_DELAY)
  198. {
  199. Delay++;
  200. }
  201. while (Delay)
  202. {
  203. if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
  204. {
  205. Delay--;
  206. }
  207. }
  208. }
  209. /**
  210. * @}
  211. */
  212. /** @addtogroup UTILS_EF_SYSTEM
  213. * @brief System Configuration functions
  214. *
  215. @verbatim
  216. ===============================================================================
  217. ##### System Configuration functions #####
  218. ===============================================================================
  219. [..]
  220. System, AHB and APB buses clocks configuration
  221. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is RCC_MAX_FREQUENCY Hz.
  222. @endverbatim
  223. @internal
  224. Depending on the SYSCLK frequency, the flash latency should be adapted accordingly:
  225. (++) +-----------------------------------------------+
  226. (++) | Latency | SYSCLK clock frequency (MHz) |
  227. (++) |---------------|-------------------------------|
  228. (++) |0WS(1CPU cycle)| 0 < SYSCLK <= 24 |
  229. (++) |---------------|-------------------------------|
  230. (++) |1WS(2CPU cycle)| 24 < SYSCLK <= 48 |
  231. (++) |---------------|-------------------------------|
  232. (++) |2WS(3CPU cycle)| 48 < SYSCLK <= 72 |
  233. (++) +-----------------------------------------------+
  234. @endinternal
  235. * @{
  236. */
  237. /**
  238. * @brief This function sets directly SystemCoreClock CMSIS variable.
  239. * @note Variable can be calculated also through SystemCoreClockUpdate function.
  240. * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
  241. * @retval None
  242. */
  243. void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
  244. {
  245. /* HCLK clock frequency */
  246. SystemCoreClock = HCLKFrequency;
  247. }
  248. /**
  249. * @brief Update number of Flash wait states in line with new frequency and current
  250. voltage range.
  251. * @param Frequency SYSCLK frequency
  252. * @retval An ErrorStatus enumeration value:
  253. * - SUCCESS: Latency has been modified
  254. * - ERROR: Latency cannot be modified
  255. */
  256. #if defined(FLASH_ACR_LATENCY)
  257. ErrorStatus LL_SetFlashLatency(uint32_t Frequency)
  258. {
  259. uint32_t timeout;
  260. uint32_t getlatency;
  261. uint32_t latency = LL_FLASH_LATENCY_0; /* default value 0WS */
  262. ErrorStatus status = SUCCESS;
  263. /* Frequency cannot be equal to 0 */
  264. if (Frequency == 0U)
  265. {
  266. status = ERROR;
  267. }
  268. else
  269. {
  270. if (Frequency > UTILS_LATENCY2_FREQ)
  271. {
  272. /* 48 < SYSCLK <= 72 => 2WS (3 CPU cycles) */
  273. latency = LL_FLASH_LATENCY_2;
  274. }
  275. else
  276. {
  277. if (Frequency > UTILS_LATENCY1_FREQ)
  278. {
  279. /* 24 < SYSCLK <= 48 => 1WS (2 CPU cycles) */
  280. latency = LL_FLASH_LATENCY_1;
  281. }
  282. else
  283. {
  284. /* else SYSCLK < 24MHz default LL_FLASH_LATENCY_0 0WS */
  285. latency = LL_FLASH_LATENCY_0;
  286. }
  287. }
  288. if (status != ERROR)
  289. {
  290. LL_FLASH_SetLatency(latency);
  291. /* Check that the new number of wait states is taken into account to access the Flash
  292. memory by reading the FLASH_ACR register */
  293. timeout = 2;
  294. do
  295. {
  296. /* Wait for Flash latency to be updated */
  297. getlatency = LL_FLASH_GetLatency();
  298. timeout--;
  299. }
  300. while ((getlatency != latency) && (timeout > 0));
  301. if (getlatency != latency)
  302. {
  303. status = ERROR;
  304. }
  305. else
  306. {
  307. status = SUCCESS;
  308. }
  309. }
  310. }
  311. return status;
  312. }
  313. #endif /* FLASH_ACR_LATENCY */
  314. /**
  315. * @brief This function configures system clock with HSI as clock source of the PLL
  316. * @note The application need to ensure that PLL is disabled.
  317. * @note Function is based on the following formula:
  318. * - PLL output frequency = ((HSI frequency / PREDIV) * PLLMUL)
  319. * - PREDIV: Set to 2 for few devices
  320. * - PLLMUL: The application software must set correctly the PLL multiplication factor to
  321. * not exceed 72MHz
  322. * @note FLASH latency can be modified through this function.
  323. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  324. * the configuration information for the PLL.
  325. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  326. * the configuration information for the BUS prescalers.
  327. * @retval An ErrorStatus enumeration value:
  328. * - SUCCESS: Max frequency configuration done
  329. * - ERROR: Max frequency configuration not done
  330. */
  331. ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  332. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  333. {
  334. ErrorStatus status = SUCCESS;
  335. uint32_t pllfreq = 0U;
  336. /* Check if one of the PLL is enabled */
  337. if (UTILS_PLL_IsBusy() == SUCCESS)
  338. {
  339. #if defined(RCC_PLLSRC_PREDIV1_SUPPORT)
  340. /* Check PREDIV value */
  341. assert_param(IS_LL_UTILS_PREDIV_VALUE(UTILS_PLLInitStruct->PLLDiv));
  342. #else
  343. /* Force PREDIV value to 2 */
  344. UTILS_PLLInitStruct->Prediv = LL_RCC_PREDIV_DIV_2;
  345. #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/
  346. /* Calculate the new PLL output frequency */
  347. pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct);
  348. /* Enable HSI if not enabled */
  349. if (LL_RCC_HSI_IsReady() != 1U)
  350. {
  351. LL_RCC_HSI_Enable();
  352. while (LL_RCC_HSI_IsReady() != 1U)
  353. {
  354. /* Wait for HSI ready */
  355. }
  356. }
  357. /* Configure PLL */
  358. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, UTILS_PLLInitStruct->PLLMul);
  359. /* Enable PLL and switch system clock to PLL */
  360. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  361. }
  362. else
  363. {
  364. /* Current PLL configuration cannot be modified */
  365. status = ERROR;
  366. }
  367. return status;
  368. }
  369. /**
  370. * @brief This function configures system clock with HSE as clock source of the PLL
  371. * @note The application need to ensure that PLL is disabled.
  372. * @note Function is based on the following formula:
  373. * - PLL output frequency = ((HSI frequency / PREDIV) * PLLMUL)
  374. * - PREDIV: Set to 2 for few devices
  375. * - PLLMUL: The application software must set correctly the PLL multiplication factor to
  376. * not exceed @ref UTILS_PLL_OUTPUT_MAX
  377. * @note FLASH latency can be modified through this function.
  378. * @param HSEFrequency Value between Min_Data = RCC_HSE_MIN and Max_Data = RCC_HSE_MAX
  379. * @param HSEBypass This parameter can be one of the following values:
  380. * @arg @ref LL_UTILS_HSEBYPASS_ON
  381. * @arg @ref LL_UTILS_HSEBYPASS_OFF
  382. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  383. * the configuration information for the PLL.
  384. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  385. * the configuration information for the BUS prescalers.
  386. * @retval An ErrorStatus enumeration value:
  387. * - SUCCESS: Max frequency configuration done
  388. * - ERROR: Max frequency configuration not done
  389. */
  390. ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
  391. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  392. {
  393. ErrorStatus status = SUCCESS;
  394. uint32_t pllfrequency = 0U;
  395. /* Check the parameters */
  396. assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
  397. assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
  398. assert_param(IS_LL_UTILS_PREDIV_VALUE(UTILS_PLLInitStruct->Prediv));
  399. /* Calculate the new PLL output frequency */
  400. pllfrequency = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
  401. /* Enable HSE if not enabled */
  402. status = UTILS_PLL_HSE_ConfigSystemClock(HSEFrequency, HSEBypass, UTILS_PLLInitStruct, UTILS_ClkInitStruct);
  403. /* Check if HSE is not enabled*/
  404. if (status == SUCCESS)
  405. {
  406. /* Configure PLL */
  407. LL_RCC_PLL_ConfigDomain_SYS((LL_RCC_PLLSOURCE_HSE | UTILS_PLLInitStruct->Prediv), UTILS_PLLInitStruct->PLLMul);
  408. /* Enable PLL and switch system clock to PLL */
  409. status = UTILS_EnablePLLAndSwitchSystem(pllfrequency, UTILS_ClkInitStruct);
  410. }
  411. return status;
  412. }
  413. #if defined(RCC_PLL2_SUPPORT)
  414. /**
  415. * @brief This function configures system clock with HSE as clock source of the PLL, via PLL2
  416. * @note The application need to ensure that PLL and PLL2 are disabled.
  417. * @note Function is based on the following formula:
  418. * - PLL output frequency = ((((HSE frequency / PREDIV2) * PLL2MUL) / PREDIV) * PLLMUL)
  419. * - PREDIV, PLLMUL, PREDIV2, PLL2MUL: The application software must set correctly the
  420. * PLL multiplication factor to not exceed @ref UTILS_PLL_OUTPUT_MAX
  421. * @note FLASH latency can be modified through this function.
  422. * @param HSEFrequency Value between Min_Data = RCC_HSE_MIN and Max_Data = RCC_HSE_MAX
  423. * @param HSEBypass This parameter can be one of the following values:
  424. * @arg @ref LL_UTILS_HSEBYPASS_ON
  425. * @arg @ref LL_UTILS_HSEBYPASS_OFF
  426. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  427. * the configuration information for the PLL.
  428. * @param UTILS_PLL2InitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  429. * the configuration information for the PLL2.
  430. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  431. * the configuration information for the BUS prescalers.
  432. * @retval An ErrorStatus enumeration value:
  433. * - SUCCESS: Max frequency configuration done
  434. * - ERROR: Max frequency configuration not done
  435. */
  436. ErrorStatus LL_PLL_ConfigSystemClock_PLL2(uint32_t HSEFrequency, uint32_t HSEBypass,
  437. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  438. LL_UTILS_PLLInitTypeDef *UTILS_PLL2InitStruct,
  439. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  440. {
  441. ErrorStatus status = SUCCESS;
  442. uint32_t pllfrequency = 0U;
  443. /* Check the parameters */
  444. assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
  445. assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
  446. assert_param(IS_LL_UTILS_PREDIV_VALUE(UTILS_PLLInitStruct->Prediv));
  447. assert_param(IS_LL_UTILS_PREDIV2_VALUE(UTILS_PLL2InitStruct->Prediv));
  448. /* Calculate the new PLL output frequency */
  449. pllfrequency = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
  450. /* Enable HSE if not enabled */
  451. status = UTILS_PLL_HSE_ConfigSystemClock(HSEFrequency, HSEBypass, UTILS_PLLInitStruct, UTILS_ClkInitStruct);
  452. /* Check if HSE is not enabled*/
  453. if (status == SUCCESS)
  454. {
  455. /* Configure PLL */
  456. LL_RCC_PLL_ConfigDomain_PLL2(UTILS_PLL2InitStruct->Prediv, UTILS_PLL2InitStruct->PLLMul);
  457. LL_RCC_PLL_ConfigDomain_SYS((LL_RCC_PLLSOURCE_PLL2 | UTILS_PLLInitStruct->Prediv), UTILS_PLLInitStruct->PLLMul);
  458. /* Calculate the new PLL output frequency */
  459. pllfrequency = UTILS_GetPLL2OutputFrequency(pllfrequency, UTILS_PLL2InitStruct);
  460. /* Enable PLL and switch system clock to PLL */
  461. status = UTILS_EnablePLLAndSwitchSystem(pllfrequency, UTILS_ClkInitStruct);
  462. }
  463. return status;
  464. }
  465. #endif /* RCC_PLL2_SUPPORT */
  466. /**
  467. * @}
  468. */
  469. /**
  470. * @}
  471. */
  472. /** @addtogroup UTILS_LL_Private_Functions
  473. * @{
  474. */
  475. /**
  476. * @brief Function to check that PLL can be modified
  477. * @param PLL_InputFrequency PLL input frequency (in Hz)
  478. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  479. * the configuration information for the PLL.
  480. * @retval PLL output frequency (in Hz)
  481. */
  482. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
  483. {
  484. uint32_t pllfreq = 0U;
  485. /* Check the parameters */
  486. assert_param(IS_LL_UTILS_PLLMUL_VALUE(UTILS_PLLInitStruct->PLLMul));
  487. /* Check different PLL parameters according to RM */
  488. #if defined (RCC_CFGR2_PREDIV1)
  489. pllfreq = __LL_RCC_CALC_PLLCLK_FREQ(PLL_InputFrequency / (UTILS_PLLInitStruct->Prediv + 1U), UTILS_PLLInitStruct->PLLMul);
  490. #else
  491. pllfreq = __LL_RCC_CALC_PLLCLK_FREQ(PLL_InputFrequency / ((UTILS_PLLInitStruct->Prediv >> RCC_CFGR_PLLXTPRE_Pos) + 1U), UTILS_PLLInitStruct->PLLMul);
  492. #endif /*RCC_CFGR2_PREDIV1SRC*/
  493. assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq));
  494. return pllfreq;
  495. }
  496. /**
  497. * @brief This function enable the HSE when it is used by PLL or PLL2
  498. * @note The application need to ensure that PLL is disabled.
  499. * @param HSEFrequency Value between Min_Data = RCC_HSE_MIN and Max_Data = RCC_HSE_MAX
  500. * @param HSEBypass This parameter can be one of the following values:
  501. * @arg @ref LL_UTILS_HSEBYPASS_ON
  502. * @arg @ref LL_UTILS_HSEBYPASS_OFF
  503. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  504. * the configuration information for the PLL.
  505. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  506. * the configuration information for the BUS prescalers.
  507. * @retval An ErrorStatus enumeration value:
  508. * - SUCCESS: HSE configuration done
  509. * - ERROR: HSE configuration not done
  510. */
  511. static ErrorStatus UTILS_PLL_HSE_ConfigSystemClock(uint32_t PLL_InputFrequency, uint32_t HSEBypass,
  512. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  513. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  514. {
  515. ErrorStatus status = SUCCESS;
  516. /* Check if one of the PLL is enabled */
  517. if (UTILS_PLL_IsBusy() == SUCCESS)
  518. {
  519. /* Enable HSE if not enabled */
  520. if (LL_RCC_HSE_IsReady() != 1U)
  521. {
  522. /* Check if need to enable HSE bypass feature or not */
  523. if (HSEBypass == LL_UTILS_HSEBYPASS_ON)
  524. {
  525. LL_RCC_HSE_EnableBypass();
  526. }
  527. else
  528. {
  529. LL_RCC_HSE_DisableBypass();
  530. }
  531. /* Enable HSE */
  532. LL_RCC_HSE_Enable();
  533. while (LL_RCC_HSE_IsReady() != 1U)
  534. {
  535. /* Wait for HSE ready */
  536. }
  537. }
  538. }
  539. else
  540. {
  541. /* Current PLL configuration cannot be modified */
  542. status = ERROR;
  543. }
  544. return status;
  545. }
  546. #if defined(RCC_PLL2_SUPPORT)
  547. /**
  548. * @brief Function to check that PLL2 can be modified
  549. * @param PLL2_InputFrequency PLL2 input frequency (in Hz)
  550. * @param UTILS_PLL2InitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  551. * the configuration information for the PLL.
  552. * @retval PLL2 output frequency (in Hz)
  553. */
  554. static uint32_t UTILS_GetPLL2OutputFrequency(uint32_t PLL2_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLL2InitStruct)
  555. {
  556. uint32_t pll2freq = 0U;
  557. /* Check the parameters */
  558. assert_param(IS_LL_UTILS_PLL2MUL_VALUE(UTILS_PLL2InitStruct->PLLMul));
  559. assert_param(IS_LL_UTILS_PREDIV2_VALUE(UTILS_PLL2InitStruct->Prediv));
  560. /* Check different PLL2 parameters according to RM */
  561. pll2freq = __LL_RCC_CALC_PLL2CLK_FREQ(PLL2_InputFrequency, UTILS_PLL2InitStruct->PLLMul, UTILS_PLL2InitStruct->Prediv);
  562. assert_param(IS_LL_UTILS_PLL2_FREQUENCY(pll2freq));
  563. return pll2freq;
  564. }
  565. #endif /* RCC_PLL2_SUPPORT */
  566. /**
  567. * @brief Function to check that PLL can be modified
  568. * @retval An ErrorStatus enumeration value:
  569. * - SUCCESS: PLL modification can be done
  570. * - ERROR: PLL is busy
  571. */
  572. static ErrorStatus UTILS_PLL_IsBusy(void)
  573. {
  574. ErrorStatus status = SUCCESS;
  575. /* Check if PLL is busy*/
  576. if (LL_RCC_PLL_IsReady() != 0U)
  577. {
  578. /* PLL configuration cannot be modified */
  579. status = ERROR;
  580. }
  581. #if defined(RCC_PLL2_SUPPORT)
  582. /* Check if PLL2 is busy*/
  583. if (LL_RCC_PLL2_IsReady() != 0U)
  584. {
  585. /* PLL2 configuration cannot be modified */
  586. status = ERROR;
  587. }
  588. #endif /* RCC_PLL2_SUPPORT */
  589. #if defined(RCC_PLLI2S_SUPPORT)
  590. /* Check if PLLI2S is busy*/
  591. if (LL_RCC_PLLI2S_IsReady() != 0U)
  592. {
  593. /* PLLI2S configuration cannot be modified */
  594. status = ERROR;
  595. }
  596. #endif /* RCC_PLLI2S_SUPPORT */
  597. return status;
  598. }
  599. /**
  600. * @brief Function to enable PLL and switch system clock to PLL
  601. * @param SYSCLK_Frequency SYSCLK frequency
  602. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  603. * the configuration information for the BUS prescalers.
  604. * @retval An ErrorStatus enumeration value:
  605. * - SUCCESS: No problem to switch system to PLL
  606. * - ERROR: Problem to switch system to PLL
  607. */
  608. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  609. {
  610. ErrorStatus status = SUCCESS;
  611. #if defined(FLASH_ACR_LATENCY)
  612. uint32_t sysclk_frequency_current = 0U;
  613. #endif /* FLASH_ACR_LATENCY */
  614. assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
  615. assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
  616. assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct->APB2CLKDivider));
  617. #if defined(FLASH_ACR_LATENCY)
  618. /* Calculate current SYSCLK frequency */
  619. sysclk_frequency_current = (SystemCoreClock << AHBPrescTable[LL_RCC_GetAHBPrescaler() >> RCC_CFGR_HPRE_Pos]);
  620. #endif /* FLASH_ACR_LATENCY */
  621. /* Increasing the number of wait states because of higher CPU frequency */
  622. #if defined (FLASH_ACR_LATENCY)
  623. if (sysclk_frequency_current < SYSCLK_Frequency)
  624. {
  625. /* Set FLASH latency to highest latency */
  626. status = LL_SetFlashLatency(SYSCLK_Frequency);
  627. }
  628. #endif /* FLASH_ACR_LATENCY */
  629. /* Update system clock configuration */
  630. if (status == SUCCESS)
  631. {
  632. #if defined(RCC_PLL2_SUPPORT)
  633. if (LL_RCC_PLL_GetMainSource() != LL_RCC_PLLSOURCE_HSI_DIV_2)
  634. {
  635. /* Enable PLL2 */
  636. LL_RCC_PLL2_Enable();
  637. while (LL_RCC_PLL2_IsReady() != 1U)
  638. {
  639. /* Wait for PLL2 ready */
  640. }
  641. }
  642. #endif /* RCC_PLL2_SUPPORT */
  643. /* Enable PLL */
  644. LL_RCC_PLL_Enable();
  645. while (LL_RCC_PLL_IsReady() != 1U)
  646. {
  647. /* Wait for PLL ready */
  648. }
  649. /* Sysclk activation on the main PLL */
  650. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  651. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  652. while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  653. {
  654. /* Wait for system clock switch to PLL */
  655. }
  656. /* Set APB1 & APB2 prescaler*/
  657. LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
  658. LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct->APB2CLKDivider);
  659. }
  660. /* Decreasing the number of wait states because of lower CPU frequency */
  661. #if defined (FLASH_ACR_LATENCY)
  662. if (sysclk_frequency_current > SYSCLK_Frequency)
  663. {
  664. /* Set FLASH latency to lowest latency */
  665. status = LL_SetFlashLatency(SYSCLK_Frequency);
  666. }
  667. #endif /* FLASH_ACR_LATENCY */
  668. /* Update SystemCoreClock variable */
  669. if (status == SUCCESS)
  670. {
  671. LL_SetSystemCoreClock(__LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider));
  672. }
  673. return status;
  674. }
  675. /**
  676. * @}
  677. */
  678. /**
  679. * @}
  680. */
  681. /**
  682. * @}
  683. */
  684. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/