stm32f0xx_ll_utils.c 24 KB

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