stm32f3xx_ll_utils.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_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</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "stm32f3xx_ll_rcc.h"
  37. #include "stm32f3xx_ll_utils.h"
  38. #include "stm32f3xx_ll_system.h"
  39. #include "stm32f3xx_ll_pwr.h"
  40. #ifdef USE_FULL_ASSERT
  41. #include "stm32_assert.h"
  42. #else
  43. #define assert_param(expr) ((void)0U)
  44. #endif
  45. /** @addtogroup STM32F3xx_LL_Driver
  46. * @{
  47. */
  48. /** @addtogroup UTILS_LL
  49. * @{
  50. */
  51. /* Private types -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private constants ---------------------------------------------------------*/
  54. /** @addtogroup UTILS_LL_Private_Constants
  55. * @{
  56. */
  57. /* Defines used for PLL range */
  58. #define UTILS_PLL_OUTPUT_MAX 72000000U /*!< Frequency max for PLL output, in Hz */
  59. /* Defines used for HSE range */
  60. #define UTILS_HSE_FREQUENCY_MIN 4000000U /*!< Frequency min for HSE frequency, in Hz */
  61. #define UTILS_HSE_FREQUENCY_MAX 32000000U /*!< Frequency max for HSE frequency, in Hz */
  62. /* Defines used for FLASH latency according to SYSCLK Frequency */
  63. #define UTILS_LATENCY1_FREQ 24000000U /*!< SYSCLK frequency to set FLASH latency 1 */
  64. #define UTILS_LATENCY2_FREQ 48000000U /*!< SYSCLK frequency to set FLASH latency 2 */
  65. /**
  66. * @}
  67. */
  68. /* Private macros ------------------------------------------------------------*/
  69. /** @addtogroup UTILS_LL_Private_Macros
  70. * @{
  71. */
  72. #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
  73. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
  74. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
  75. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
  76. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
  77. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
  78. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
  79. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
  80. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
  81. #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
  82. || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
  83. || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
  84. || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
  85. || ((__VALUE__) == LL_RCC_APB1_DIV_16))
  86. #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
  87. || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
  88. || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
  89. || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
  90. || ((__VALUE__) == LL_RCC_APB2_DIV_16))
  91. #define IS_LL_UTILS_PLLMUL_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLL_MUL_2) \
  92. || ((__VALUE__) == LL_RCC_PLL_MUL_3) \
  93. || ((__VALUE__) == LL_RCC_PLL_MUL_4) \
  94. || ((__VALUE__) == LL_RCC_PLL_MUL_5) \
  95. || ((__VALUE__) == LL_RCC_PLL_MUL_6) \
  96. || ((__VALUE__) == LL_RCC_PLL_MUL_7) \
  97. || ((__VALUE__) == LL_RCC_PLL_MUL_8) \
  98. || ((__VALUE__) == LL_RCC_PLL_MUL_9) \
  99. || ((__VALUE__) == LL_RCC_PLL_MUL_10) \
  100. || ((__VALUE__) == LL_RCC_PLL_MUL_11) \
  101. || ((__VALUE__) == LL_RCC_PLL_MUL_12) \
  102. || ((__VALUE__) == LL_RCC_PLL_MUL_13) \
  103. || ((__VALUE__) == LL_RCC_PLL_MUL_14) \
  104. || ((__VALUE__) == LL_RCC_PLL_MUL_15) \
  105. || ((__VALUE__) == LL_RCC_PLL_MUL_16))
  106. #define IS_LL_UTILS_PREDIV_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PREDIV_DIV_1) || ((__VALUE__) == LL_RCC_PREDIV_DIV_2) || \
  107. ((__VALUE__) == LL_RCC_PREDIV_DIV_3) || ((__VALUE__) == LL_RCC_PREDIV_DIV_4) || \
  108. ((__VALUE__) == LL_RCC_PREDIV_DIV_5) || ((__VALUE__) == LL_RCC_PREDIV_DIV_6) || \
  109. ((__VALUE__) == LL_RCC_PREDIV_DIV_7) || ((__VALUE__) == LL_RCC_PREDIV_DIV_8) || \
  110. ((__VALUE__) == LL_RCC_PREDIV_DIV_9) || ((__VALUE__) == LL_RCC_PREDIV_DIV_10) || \
  111. ((__VALUE__) == LL_RCC_PREDIV_DIV_11) || ((__VALUE__) == LL_RCC_PREDIV_DIV_12) || \
  112. ((__VALUE__) == LL_RCC_PREDIV_DIV_13) || ((__VALUE__) == LL_RCC_PREDIV_DIV_14) || \
  113. ((__VALUE__) == LL_RCC_PREDIV_DIV_15) || ((__VALUE__) == LL_RCC_PREDIV_DIV_16))
  114. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((__VALUE__) <= UTILS_PLL_OUTPUT_MAX)
  115. #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
  116. || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
  117. #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
  118. /**
  119. * @}
  120. */
  121. /* Private function prototypes -----------------------------------------------*/
  122. /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
  123. * @{
  124. */
  125. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency,
  126. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
  127. #if defined(FLASH_ACR_LATENCY)
  128. static ErrorStatus UTILS_SetFlashLatency(uint32_t Frequency);
  129. #endif /* FLASH_ACR_LATENCY */
  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. /* Add this code to indicate that local variable is not used */
  169. ((void)tmp);
  170. /* Add a period to guaranty minimum wait */
  171. if (Delay < LL_MAX_DELAY)
  172. {
  173. Delay++;
  174. }
  175. while (Delay)
  176. {
  177. if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
  178. {
  179. Delay--;
  180. }
  181. }
  182. }
  183. /**
  184. * @}
  185. */
  186. /** @addtogroup UTILS_EF_SYSTEM
  187. * @brief System Configuration functions
  188. *
  189. @verbatim
  190. ===============================================================================
  191. ##### System Configuration functions #####
  192. ===============================================================================
  193. [..]
  194. System, AHB and APB buses clocks configuration
  195. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 72000000 Hz.
  196. @endverbatim
  197. @internal
  198. Depending on the SYSCLK frequency, the flash latency should be adapted accordingly:
  199. (++) +-----------------------------------------------+
  200. (++) | Latency | SYSCLK clock frequency (MHz) |
  201. (++) |---------------|-------------------------------|
  202. (++) |0WS(1CPU cycle)| 0 < SYSCLK <= 24 |
  203. (++) |---------------|-------------------------------|
  204. (++) |1WS(2CPU cycle)| 24 < SYSCLK <= 48 |
  205. (++) |---------------|-------------------------------|
  206. (++) |2WS(3CPU cycle)| 48 < SYSCLK <= 72 |
  207. (++) +-----------------------------------------------+
  208. @endinternal
  209. * @{
  210. */
  211. /**
  212. * @brief This function sets directly SystemCoreClock CMSIS variable.
  213. * @note Variable can be calculated also through SystemCoreClockUpdate function.
  214. * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
  215. * @retval None
  216. */
  217. void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
  218. {
  219. /* HCLK clock frequency */
  220. SystemCoreClock = HCLKFrequency;
  221. }
  222. /**
  223. * @brief This function configures system clock with HSI as clock source of the PLL
  224. * @note The application need to ensure that PLL is disabled.
  225. * @note Function is based on the following formula:
  226. * - PLL output frequency = ((HSI frequency / PREDIV) * PLLMUL)
  227. * - PREDIV: Set to 2 for few devices
  228. * - PLLMUL: The application software must set correctly the PLL multiplication factor to
  229. * not exceed 72MHz
  230. * @note FLASH latency can be modified through this function.
  231. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  232. * the configuration information for the PLL.
  233. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  234. * the configuration information for the BUS prescalers.
  235. * @retval An ErrorStatus enumeration value:
  236. * - SUCCESS: Max frequency configuration done
  237. * - ERROR: Max frequency configuration not done
  238. */
  239. ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  240. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  241. {
  242. ErrorStatus status = SUCCESS;
  243. uint32_t pllfreq = 0U;
  244. /* Check if one of the PLL is enabled */
  245. if (UTILS_PLL_IsBusy() == SUCCESS)
  246. {
  247. #if defined(RCC_PLLSRC_PREDIV1_SUPPORT)
  248. /* Check PREDIV value */
  249. assert_param(IS_LL_UTILS_PREDIV_VALUE(UTILS_PLLInitStruct->PLLDiv));
  250. #else
  251. /* Force PREDIV value to 2 */
  252. UTILS_PLLInitStruct->Prediv = LL_RCC_PREDIV_DIV_2;
  253. #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/
  254. /* Calculate the new PLL output frequency */
  255. pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct);
  256. /* Enable HSI if not enabled */
  257. if (LL_RCC_HSI_IsReady() != 1U)
  258. {
  259. LL_RCC_HSI_Enable();
  260. while (LL_RCC_HSI_IsReady() != 1U)
  261. {
  262. /* Wait for HSI ready */
  263. }
  264. }
  265. /* Configure PLL */
  266. #if defined(RCC_PLLSRC_PREDIV1_SUPPORT)
  267. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, UTILS_PLLInitStruct->PLLMul, UTILS_PLLInitStruct->PLLDiv);
  268. #else
  269. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, UTILS_PLLInitStruct->PLLMul);
  270. #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/
  271. /* Enable PLL and switch system clock to PLL */
  272. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  273. }
  274. else
  275. {
  276. /* Current PLL configuration cannot be modified */
  277. status = ERROR;
  278. }
  279. return status;
  280. }
  281. /**
  282. * @brief This function configures system clock with HSE as clock source of the PLL
  283. * @note The application need to ensure that PLL is disabled.
  284. * @note Function is based on the following formula:
  285. * - PLL output frequency = ((HSI frequency / PREDIV) * PLLMUL)
  286. * - PREDIV: Set to 2 for few devices
  287. * - PLLMUL: The application software must set correctly the PLL multiplication factor to
  288. * not exceed @ref UTILS_PLL_OUTPUT_MAX
  289. * @note FLASH latency can be modified through this function.
  290. * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 32000000
  291. * @param HSEBypass This parameter can be one of the following values:
  292. * @arg @ref LL_UTILS_HSEBYPASS_ON
  293. * @arg @ref LL_UTILS_HSEBYPASS_OFF
  294. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  295. * the configuration information for the PLL.
  296. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  297. * the configuration information for the BUS prescalers.
  298. * @retval An ErrorStatus enumeration value:
  299. * - SUCCESS: Max frequency configuration done
  300. * - ERROR: Max frequency configuration not done
  301. */
  302. ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
  303. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  304. {
  305. ErrorStatus status = SUCCESS;
  306. uint32_t pllfreq = 0U;
  307. /* Check the parameters */
  308. assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
  309. assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
  310. /* Check if one of the PLL is enabled */
  311. if (UTILS_PLL_IsBusy() == SUCCESS)
  312. {
  313. /* Check PREDIV value */
  314. #if defined(RCC_PLLSRC_PREDIV1_SUPPORT)
  315. assert_param(IS_LL_UTILS_PREDIV_VALUE(UTILS_PLLInitStruct->PLLDiv));
  316. #else
  317. assert_param(IS_LL_UTILS_PREDIV_VALUE(UTILS_PLLInitStruct->Prediv));
  318. #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/
  319. /* Calculate the new PLL output frequency */
  320. pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
  321. /* Enable HSE if not enabled */
  322. if (LL_RCC_HSE_IsReady() != 1U)
  323. {
  324. /* Check if need to enable HSE bypass feature or not */
  325. if (HSEBypass == LL_UTILS_HSEBYPASS_ON)
  326. {
  327. LL_RCC_HSE_EnableBypass();
  328. }
  329. else
  330. {
  331. LL_RCC_HSE_DisableBypass();
  332. }
  333. /* Enable HSE */
  334. LL_RCC_HSE_Enable();
  335. while (LL_RCC_HSE_IsReady() != 1U)
  336. {
  337. /* Wait for HSE ready */
  338. }
  339. }
  340. /* Configure PLL */
  341. #if defined(RCC_PLLSRC_PREDIV1_SUPPORT)
  342. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, UTILS_PLLInitStruct->PLLMul, UTILS_PLLInitStruct->PLLDiv);
  343. #else
  344. LL_RCC_PLL_ConfigDomain_SYS((RCC_CFGR_PLLSRC_HSE_PREDIV | UTILS_PLLInitStruct->Prediv), UTILS_PLLInitStruct->PLLMul);
  345. #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/
  346. /* Enable PLL and switch system clock to PLL */
  347. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  348. }
  349. else
  350. {
  351. /* Current PLL configuration cannot be modified */
  352. status = ERROR;
  353. }
  354. return status;
  355. }
  356. /**
  357. * @}
  358. */
  359. /**
  360. * @}
  361. */
  362. /** @addtogroup UTILS_LL_Private_Functions
  363. * @{
  364. */
  365. /**
  366. * @brief Update number of Flash wait states in line with new frequency and current
  367. voltage range.
  368. * @param Frequency SYSCLK frequency
  369. * @retval An ErrorStatus enumeration value:
  370. * - SUCCESS: Latency has been modified
  371. * - ERROR: Latency cannot be modified
  372. */
  373. #if defined(FLASH_ACR_LATENCY)
  374. static ErrorStatus UTILS_SetFlashLatency(uint32_t Frequency)
  375. {
  376. ErrorStatus status = SUCCESS;
  377. uint32_t latency = LL_FLASH_LATENCY_0; /* default value 0WS */
  378. /* Frequency cannot be equal to 0 */
  379. if (Frequency == 0U)
  380. {
  381. status = ERROR;
  382. }
  383. else
  384. {
  385. if (Frequency > UTILS_LATENCY2_FREQ)
  386. {
  387. /* 48 < SYSCLK <= 72 => 2WS (3 CPU cycles) */
  388. latency = LL_FLASH_LATENCY_2;
  389. }
  390. else
  391. {
  392. if (Frequency > UTILS_LATENCY1_FREQ)
  393. {
  394. /* 24 < SYSCLK <= 48 => 1WS (2 CPU cycles) */
  395. latency = LL_FLASH_LATENCY_1;
  396. }
  397. /* else SYSCLK < 24MHz default LL_FLASH_LATENCY_0 0WS */
  398. }
  399. LL_FLASH_SetLatency(latency);
  400. /* Check that the new number of wait states is taken into account to access the Flash
  401. memory by reading the FLASH_ACR register */
  402. if (LL_FLASH_GetLatency() != latency)
  403. {
  404. status = ERROR;
  405. }
  406. }
  407. return status;
  408. }
  409. #endif /* FLASH_ACR_LATENCY */
  410. /**
  411. * @brief Function to check that PLL can be modified
  412. * @param PLL_InputFrequency PLL input frequency (in Hz)
  413. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  414. * the configuration information for the PLL.
  415. * @retval PLL output frequency (in Hz)
  416. */
  417. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
  418. {
  419. uint32_t pllfreq = 0U;
  420. /* Check the parameters */
  421. assert_param(IS_LL_UTILS_PLLMUL_VALUE(UTILS_PLLInitStruct->PLLMul));
  422. /* Check different PLL parameters according to RM */
  423. /* The application software must set correctly the PLL multiplication factor to
  424. not exceed @ref UTILS_PLL_OUTPUT_MAX */
  425. #if defined(RCC_PLLSRC_PREDIV1_SUPPORT)
  426. pllfreq = __LL_RCC_CALC_PLLCLK_FREQ(PLL_InputFrequency, UTILS_PLLInitStruct->PLLMul, UTILS_PLLInitStruct->PLLDiv);
  427. #else
  428. pllfreq = __LL_RCC_CALC_PLLCLK_FREQ(PLL_InputFrequency / (UTILS_PLLInitStruct->Prediv + 1U), UTILS_PLLInitStruct->PLLMul);
  429. #endif /*RCC_PLLSRC_PREDIV1_SUPPORT*/
  430. assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq));
  431. return pllfreq;
  432. }
  433. /**
  434. * @brief Function to check that PLL can be modified
  435. * @retval An ErrorStatus enumeration value:
  436. * - SUCCESS: PLL modification can be done
  437. * - ERROR: PLL is busy
  438. */
  439. static ErrorStatus UTILS_PLL_IsBusy(void)
  440. {
  441. ErrorStatus status = SUCCESS;
  442. /* Check if PLL is busy*/
  443. if (LL_RCC_PLL_IsReady() != 0U)
  444. {
  445. /* PLL configuration cannot be modified */
  446. status = ERROR;
  447. }
  448. return status;
  449. }
  450. /**
  451. * @brief Function to enable PLL and switch system clock to PLL
  452. * @param SYSCLK_Frequency SYSCLK frequency
  453. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  454. * the configuration information for the BUS prescalers.
  455. * @retval An ErrorStatus enumeration value:
  456. * - SUCCESS: No problem to switch system to PLL
  457. * - ERROR: Problem to switch system to PLL
  458. */
  459. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  460. {
  461. ErrorStatus status = SUCCESS;
  462. uint32_t sysclk_frequency_current = 0U;
  463. assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
  464. assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
  465. assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct->APB2CLKDivider));
  466. /* Calculate current SYSCLK frequency */
  467. sysclk_frequency_current = (SystemCoreClock << AHBPrescTable[LL_RCC_GetAHBPrescaler() >> RCC_POSITION_HPRE]);
  468. /* Increasing the number of wait states because of higher CPU frequency */
  469. if (sysclk_frequency_current < SYSCLK_Frequency)
  470. {
  471. /* Set FLASH latency to highest latency */
  472. status = UTILS_SetFlashLatency(SYSCLK_Frequency);
  473. }
  474. /* Update system clock configuration */
  475. if (status == SUCCESS)
  476. {
  477. /* Enable PLL */
  478. LL_RCC_PLL_Enable();
  479. while (LL_RCC_PLL_IsReady() != 1U)
  480. {
  481. /* Wait for PLL ready */
  482. }
  483. /* Sysclk activation on the main PLL */
  484. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  485. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  486. while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  487. {
  488. /* Wait for system clock switch to PLL */
  489. }
  490. /* Set APB1 & APB2 prescaler*/
  491. LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
  492. LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct->APB2CLKDivider);
  493. }
  494. /* Decreasing the number of wait states because of lower CPU frequency */
  495. if (sysclk_frequency_current > SYSCLK_Frequency)
  496. {
  497. /* Set FLASH latency to lowest latency */
  498. status = UTILS_SetFlashLatency(SYSCLK_Frequency);
  499. }
  500. /* Update SystemCoreClock variable */
  501. if (status == SUCCESS)
  502. {
  503. LL_SetSystemCoreClock(__LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider));
  504. }
  505. return status;
  506. }
  507. /**
  508. * @}
  509. */
  510. /**
  511. * @}
  512. */
  513. /**
  514. * @}
  515. */
  516. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/