stm32l4xx_ll_utils.c 28 KB

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