stm32f4xx_ll_utils.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_utils.c
  4. * @author MCD Application Team
  5. * @brief UTILS LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f4xx_ll_utils.h"
  21. #include "stm32f4xx_ll_rcc.h"
  22. #include "stm32f4xx_ll_system.h"
  23. #include "stm32f4xx_ll_pwr.h"
  24. #ifdef USE_FULL_ASSERT
  25. #include "stm32_assert.h"
  26. #else
  27. #define assert_param(expr) ((void)0U)
  28. #endif /* USE_FULL_ASSERT */
  29. /** @addtogroup STM32F4xx_LL_Driver
  30. * @{
  31. */
  32. /** @addtogroup UTILS_LL
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /** @addtogroup UTILS_LL_Private_Constants
  39. * @{
  40. */
  41. #if defined(RCC_MAX_FREQUENCY_SCALE1)
  42. #define UTILS_MAX_FREQUENCY_SCALE1 RCC_MAX_FREQUENCY /*!< Maximum frequency for system clock at power scale1, in Hz */
  43. #endif /*RCC_MAX_FREQUENCY_SCALE1 */
  44. #define UTILS_MAX_FREQUENCY_SCALE2 RCC_MAX_FREQUENCY_SCALE2 /*!< Maximum frequency for system clock at power scale2, in Hz */
  45. #if defined(RCC_MAX_FREQUENCY_SCALE3)
  46. #define UTILS_MAX_FREQUENCY_SCALE3 RCC_MAX_FREQUENCY_SCALE3 /*!< Maximum frequency for system clock at power scale3, in Hz */
  47. #endif /* MAX_FREQUENCY_SCALE3 */
  48. /* Defines used for PLL range */
  49. #define UTILS_PLLVCO_INPUT_MIN RCC_PLLVCO_INPUT_MIN /*!< Frequency min for PLLVCO input, in Hz */
  50. #define UTILS_PLLVCO_INPUT_MAX RCC_PLLVCO_INPUT_MAX /*!< Frequency max for PLLVCO input, in Hz */
  51. #define UTILS_PLLVCO_OUTPUT_MIN RCC_PLLVCO_OUTPUT_MIN /*!< Frequency min for PLLVCO output, in Hz */
  52. #define UTILS_PLLVCO_OUTPUT_MAX RCC_PLLVCO_OUTPUT_MAX /*!< Frequency max for PLLVCO output, in Hz */
  53. /* Defines used for HSE range */
  54. #define UTILS_HSE_FREQUENCY_MIN 4000000U /*!< Frequency min for HSE frequency, in Hz */
  55. #define UTILS_HSE_FREQUENCY_MAX 26000000U /*!< Frequency max for HSE frequency, in Hz */
  56. /* Defines used for FLASH latency according to HCLK Frequency */
  57. #if defined(FLASH_SCALE1_LATENCY1_FREQ)
  58. #define UTILS_SCALE1_LATENCY1_FREQ FLASH_SCALE1_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
  59. #endif
  60. #if defined(FLASH_SCALE1_LATENCY2_FREQ)
  61. #define UTILS_SCALE1_LATENCY2_FREQ FLASH_SCALE1_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
  62. #endif
  63. #if defined(FLASH_SCALE1_LATENCY3_FREQ)
  64. #define UTILS_SCALE1_LATENCY3_FREQ FLASH_SCALE1_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
  65. #endif
  66. #if defined(FLASH_SCALE1_LATENCY4_FREQ)
  67. #define UTILS_SCALE1_LATENCY4_FREQ FLASH_SCALE1_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
  68. #endif
  69. #if defined(FLASH_SCALE1_LATENCY5_FREQ)
  70. #define UTILS_SCALE1_LATENCY5_FREQ FLASH_SCALE1_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 1 */
  71. #endif
  72. #define UTILS_SCALE2_LATENCY1_FREQ FLASH_SCALE2_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
  73. #define UTILS_SCALE2_LATENCY2_FREQ FLASH_SCALE2_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
  74. #if defined(FLASH_SCALE2_LATENCY3_FREQ)
  75. #define UTILS_SCALE2_LATENCY3_FREQ FLASH_SCALE2_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
  76. #endif
  77. #if defined(FLASH_SCALE2_LATENCY4_FREQ)
  78. #define UTILS_SCALE2_LATENCY4_FREQ FLASH_SCALE2_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 2 */
  79. #endif
  80. #if defined(FLASH_SCALE2_LATENCY5_FREQ)
  81. #define UTILS_SCALE2_LATENCY5_FREQ FLASH_SCALE2_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 2 */
  82. #endif
  83. #if defined(FLASH_SCALE3_LATENCY1_FREQ)
  84. #define UTILS_SCALE3_LATENCY1_FREQ FLASH_SCALE3_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 3 */
  85. #endif
  86. #if defined(FLASH_SCALE3_LATENCY2_FREQ)
  87. #define UTILS_SCALE3_LATENCY2_FREQ FLASH_SCALE3_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 3 */
  88. #endif
  89. #if defined(FLASH_SCALE3_LATENCY3_FREQ)
  90. #define UTILS_SCALE3_LATENCY3_FREQ FLASH_SCALE3_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 3 in power scale 3 */
  91. #endif
  92. #if defined(FLASH_SCALE3_LATENCY4_FREQ)
  93. #define UTILS_SCALE3_LATENCY4_FREQ FLASH_SCALE3_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 3 */
  94. #endif
  95. #if defined(FLASH_SCALE3_LATENCY5_FREQ)
  96. #define UTILS_SCALE3_LATENCY5_FREQ FLASH_SCALE3_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 3 */
  97. #endif
  98. /**
  99. * @}
  100. */
  101. /* Private macros ------------------------------------------------------------*/
  102. /** @addtogroup UTILS_LL_Private_Macros
  103. * @{
  104. */
  105. #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
  106. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
  107. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
  108. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
  109. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
  110. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
  111. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
  112. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
  113. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
  114. #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
  115. || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
  116. || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
  117. || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
  118. || ((__VALUE__) == LL_RCC_APB1_DIV_16))
  119. #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
  120. || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
  121. || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
  122. || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
  123. || ((__VALUE__) == LL_RCC_APB2_DIV_16))
  124. #define IS_LL_UTILS_PLLM_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLM_DIV_2) \
  125. || ((__VALUE__) == LL_RCC_PLLM_DIV_3) \
  126. || ((__VALUE__) == LL_RCC_PLLM_DIV_4) \
  127. || ((__VALUE__) == LL_RCC_PLLM_DIV_5) \
  128. || ((__VALUE__) == LL_RCC_PLLM_DIV_6) \
  129. || ((__VALUE__) == LL_RCC_PLLM_DIV_7) \
  130. || ((__VALUE__) == LL_RCC_PLLM_DIV_8) \
  131. || ((__VALUE__) == LL_RCC_PLLM_DIV_9) \
  132. || ((__VALUE__) == LL_RCC_PLLM_DIV_10) \
  133. || ((__VALUE__) == LL_RCC_PLLM_DIV_11) \
  134. || ((__VALUE__) == LL_RCC_PLLM_DIV_12) \
  135. || ((__VALUE__) == LL_RCC_PLLM_DIV_13) \
  136. || ((__VALUE__) == LL_RCC_PLLM_DIV_14) \
  137. || ((__VALUE__) == LL_RCC_PLLM_DIV_15) \
  138. || ((__VALUE__) == LL_RCC_PLLM_DIV_16) \
  139. || ((__VALUE__) == LL_RCC_PLLM_DIV_17) \
  140. || ((__VALUE__) == LL_RCC_PLLM_DIV_18) \
  141. || ((__VALUE__) == LL_RCC_PLLM_DIV_19) \
  142. || ((__VALUE__) == LL_RCC_PLLM_DIV_20) \
  143. || ((__VALUE__) == LL_RCC_PLLM_DIV_21) \
  144. || ((__VALUE__) == LL_RCC_PLLM_DIV_22) \
  145. || ((__VALUE__) == LL_RCC_PLLM_DIV_23) \
  146. || ((__VALUE__) == LL_RCC_PLLM_DIV_24) \
  147. || ((__VALUE__) == LL_RCC_PLLM_DIV_25) \
  148. || ((__VALUE__) == LL_RCC_PLLM_DIV_26) \
  149. || ((__VALUE__) == LL_RCC_PLLM_DIV_27) \
  150. || ((__VALUE__) == LL_RCC_PLLM_DIV_28) \
  151. || ((__VALUE__) == LL_RCC_PLLM_DIV_29) \
  152. || ((__VALUE__) == LL_RCC_PLLM_DIV_30) \
  153. || ((__VALUE__) == LL_RCC_PLLM_DIV_31) \
  154. || ((__VALUE__) == LL_RCC_PLLM_DIV_32) \
  155. || ((__VALUE__) == LL_RCC_PLLM_DIV_33) \
  156. || ((__VALUE__) == LL_RCC_PLLM_DIV_34) \
  157. || ((__VALUE__) == LL_RCC_PLLM_DIV_35) \
  158. || ((__VALUE__) == LL_RCC_PLLM_DIV_36) \
  159. || ((__VALUE__) == LL_RCC_PLLM_DIV_37) \
  160. || ((__VALUE__) == LL_RCC_PLLM_DIV_38) \
  161. || ((__VALUE__) == LL_RCC_PLLM_DIV_39) \
  162. || ((__VALUE__) == LL_RCC_PLLM_DIV_40) \
  163. || ((__VALUE__) == LL_RCC_PLLM_DIV_41) \
  164. || ((__VALUE__) == LL_RCC_PLLM_DIV_42) \
  165. || ((__VALUE__) == LL_RCC_PLLM_DIV_43) \
  166. || ((__VALUE__) == LL_RCC_PLLM_DIV_44) \
  167. || ((__VALUE__) == LL_RCC_PLLM_DIV_45) \
  168. || ((__VALUE__) == LL_RCC_PLLM_DIV_46) \
  169. || ((__VALUE__) == LL_RCC_PLLM_DIV_47) \
  170. || ((__VALUE__) == LL_RCC_PLLM_DIV_48) \
  171. || ((__VALUE__) == LL_RCC_PLLM_DIV_49) \
  172. || ((__VALUE__) == LL_RCC_PLLM_DIV_50) \
  173. || ((__VALUE__) == LL_RCC_PLLM_DIV_51) \
  174. || ((__VALUE__) == LL_RCC_PLLM_DIV_52) \
  175. || ((__VALUE__) == LL_RCC_PLLM_DIV_53) \
  176. || ((__VALUE__) == LL_RCC_PLLM_DIV_54) \
  177. || ((__VALUE__) == LL_RCC_PLLM_DIV_55) \
  178. || ((__VALUE__) == LL_RCC_PLLM_DIV_56) \
  179. || ((__VALUE__) == LL_RCC_PLLM_DIV_57) \
  180. || ((__VALUE__) == LL_RCC_PLLM_DIV_58) \
  181. || ((__VALUE__) == LL_RCC_PLLM_DIV_59) \
  182. || ((__VALUE__) == LL_RCC_PLLM_DIV_60) \
  183. || ((__VALUE__) == LL_RCC_PLLM_DIV_61) \
  184. || ((__VALUE__) == LL_RCC_PLLM_DIV_62) \
  185. || ((__VALUE__) == LL_RCC_PLLM_DIV_63))
  186. #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((RCC_PLLN_MIN_VALUE <= (__VALUE__)) && ((__VALUE__) <= RCC_PLLN_MAX_VALUE))
  187. #define IS_LL_UTILS_PLLP_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLP_DIV_2) \
  188. || ((__VALUE__) == LL_RCC_PLLP_DIV_4) \
  189. || ((__VALUE__) == LL_RCC_PLLP_DIV_6) \
  190. || ((__VALUE__) == LL_RCC_PLLP_DIV_8))
  191. #define IS_LL_UTILS_PLLVCO_INPUT(__VALUE__) ((UTILS_PLLVCO_INPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX))
  192. #define IS_LL_UTILS_PLLVCO_OUTPUT(__VALUE__) ((UTILS_PLLVCO_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_OUTPUT_MAX))
  193. #if !defined(RCC_MAX_FREQUENCY_SCALE1)
  194. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
  195. ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
  196. #elif defined(RCC_MAX_FREQUENCY_SCALE3)
  197. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
  198. (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
  199. ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
  200. #else
  201. #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
  202. ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2))
  203. #endif /* RCC_MAX_FREQUENCY_SCALE1*/
  204. #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
  205. || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
  206. #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
  207. /**
  208. * @}
  209. */
  210. /* Private function prototypes -----------------------------------------------*/
  211. /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
  212. * @{
  213. */
  214. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency,
  215. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
  216. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
  217. static ErrorStatus UTILS_PLL_IsBusy(void);
  218. /**
  219. * @}
  220. */
  221. /* Exported functions --------------------------------------------------------*/
  222. /** @addtogroup UTILS_LL_Exported_Functions
  223. * @{
  224. */
  225. /** @addtogroup UTILS_LL_EF_DELAY
  226. * @{
  227. */
  228. /**
  229. * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
  230. * @note When a RTOS is used, it is recommended to avoid changing the Systick
  231. * configuration by calling this function, for a delay use rather osDelay RTOS service.
  232. * @param HCLKFrequency HCLK frequency in Hz
  233. * @note HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq
  234. * @retval None
  235. */
  236. void LL_Init1msTick(uint32_t HCLKFrequency)
  237. {
  238. /* Use frequency provided in argument */
  239. LL_InitTick(HCLKFrequency, 1000U);
  240. }
  241. /**
  242. * @brief This function provides accurate delay (in milliseconds) based
  243. * on SysTick counter flag
  244. * @note When a RTOS is used, it is recommended to avoid using blocking delay
  245. * and use rather osDelay service.
  246. * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
  247. * will configure Systick to 1ms
  248. * @param Delay specifies the delay time length, in milliseconds.
  249. * @retval None
  250. */
  251. void LL_mDelay(uint32_t Delay)
  252. {
  253. __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
  254. /* Add this code to indicate that local variable is not used */
  255. ((void)tmp);
  256. /* Add a period to guaranty minimum wait */
  257. if(Delay < LL_MAX_DELAY)
  258. {
  259. Delay++;
  260. }
  261. while (Delay)
  262. {
  263. if((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
  264. {
  265. Delay--;
  266. }
  267. }
  268. }
  269. /**
  270. * @}
  271. */
  272. /** @addtogroup UTILS_EF_SYSTEM
  273. * @brief System Configuration functions
  274. *
  275. @verbatim
  276. ===============================================================================
  277. ##### System Configuration functions #####
  278. ===============================================================================
  279. [..]
  280. System, AHB and APB buses clocks configuration
  281. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 180000000 Hz.
  282. @endverbatim
  283. @internal
  284. Depending on the device voltage range, the maximum frequency should be
  285. adapted accordingly to the Refenece manual.
  286. @endinternal
  287. * @{
  288. */
  289. /**
  290. * @brief This function sets directly SystemCoreClock CMSIS variable.
  291. * @note Variable can be calculated also through SystemCoreClockUpdate function.
  292. * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
  293. * @retval None
  294. */
  295. void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
  296. {
  297. /* HCLK clock frequency */
  298. SystemCoreClock = HCLKFrequency;
  299. }
  300. /**
  301. * @brief Update number of Flash wait states in line with new frequency and current
  302. voltage range.
  303. * @note This Function support ONLY devices with supply voltage (voltage range) between 2.7V and 3.6V
  304. * @param HCLK_Frequency HCLK frequency
  305. * @retval An ErrorStatus enumeration value:
  306. * - SUCCESS: Latency has been modified
  307. * - ERROR: Latency cannot be modified
  308. */
  309. ErrorStatus LL_SetFlashLatency(uint32_t HCLK_Frequency)
  310. {
  311. uint32_t timeout;
  312. uint32_t getlatency;
  313. uint32_t latency = LL_FLASH_LATENCY_0; /* default value 0WS */
  314. ErrorStatus status = SUCCESS;
  315. /* Frequency cannot be equal to 0 */
  316. if(HCLK_Frequency == 0U)
  317. {
  318. status = ERROR;
  319. }
  320. else
  321. {
  322. if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1)
  323. {
  324. #if defined (UTILS_SCALE1_LATENCY5_FREQ)
  325. if((HCLK_Frequency > UTILS_SCALE1_LATENCY5_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  326. {
  327. latency = LL_FLASH_LATENCY_5;
  328. }
  329. #endif /*UTILS_SCALE1_LATENCY5_FREQ */
  330. #if defined (UTILS_SCALE1_LATENCY4_FREQ)
  331. if((HCLK_Frequency > UTILS_SCALE1_LATENCY4_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  332. {
  333. latency = LL_FLASH_LATENCY_4;
  334. }
  335. #endif /* UTILS_SCALE1_LATENCY4_FREQ */
  336. #if defined (UTILS_SCALE1_LATENCY3_FREQ)
  337. if((HCLK_Frequency > UTILS_SCALE1_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  338. {
  339. latency = LL_FLASH_LATENCY_3;
  340. }
  341. #endif /* UTILS_SCALE1_LATENCY3_FREQ */
  342. #if defined (UTILS_SCALE1_LATENCY2_FREQ)
  343. if((HCLK_Frequency > UTILS_SCALE1_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  344. {
  345. latency = LL_FLASH_LATENCY_2;
  346. }
  347. else
  348. {
  349. if((HCLK_Frequency > UTILS_SCALE1_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  350. {
  351. latency = LL_FLASH_LATENCY_1;
  352. }
  353. }
  354. #endif /* UTILS_SCALE1_LATENCY2_FREQ */
  355. }
  356. if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2)
  357. {
  358. #if defined (UTILS_SCALE2_LATENCY5_FREQ)
  359. if((HCLK_Frequency > UTILS_SCALE2_LATENCY5_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  360. {
  361. latency = LL_FLASH_LATENCY_5;
  362. }
  363. #endif /*UTILS_SCALE1_LATENCY5_FREQ */
  364. #if defined (UTILS_SCALE2_LATENCY4_FREQ)
  365. if((HCLK_Frequency > UTILS_SCALE2_LATENCY4_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  366. {
  367. latency = LL_FLASH_LATENCY_4;
  368. }
  369. #endif /*UTILS_SCALE1_LATENCY4_FREQ */
  370. #if defined (UTILS_SCALE2_LATENCY3_FREQ)
  371. if((HCLK_Frequency > UTILS_SCALE2_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  372. {
  373. latency = LL_FLASH_LATENCY_3;
  374. }
  375. #endif /*UTILS_SCALE1_LATENCY3_FREQ */
  376. if((HCLK_Frequency > UTILS_SCALE2_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  377. {
  378. latency = LL_FLASH_LATENCY_2;
  379. }
  380. else
  381. {
  382. if((HCLK_Frequency > UTILS_SCALE2_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  383. {
  384. latency = LL_FLASH_LATENCY_1;
  385. }
  386. }
  387. }
  388. #if defined (LL_PWR_REGU_VOLTAGE_SCALE3)
  389. if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE3)
  390. {
  391. #if defined (UTILS_SCALE3_LATENCY3_FREQ)
  392. if((HCLK_Frequency > UTILS_SCALE3_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  393. {
  394. latency = LL_FLASH_LATENCY_3;
  395. }
  396. #endif /*UTILS_SCALE1_LATENCY3_FREQ */
  397. #if defined (UTILS_SCALE3_LATENCY2_FREQ)
  398. if((HCLK_Frequency > UTILS_SCALE3_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  399. {
  400. latency = LL_FLASH_LATENCY_2;
  401. }
  402. else
  403. {
  404. if((HCLK_Frequency > UTILS_SCALE3_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
  405. {
  406. latency = LL_FLASH_LATENCY_1;
  407. }
  408. }
  409. }
  410. #endif /*UTILS_SCALE1_LATENCY2_FREQ */
  411. #endif /* LL_PWR_REGU_VOLTAGE_SCALE3 */
  412. LL_FLASH_SetLatency(latency);
  413. /* Check that the new number of wait states is taken into account to access the Flash
  414. memory by reading the FLASH_ACR register */
  415. timeout = 2;
  416. do
  417. {
  418. /* Wait for Flash latency to be updated */
  419. getlatency = LL_FLASH_GetLatency();
  420. timeout--;
  421. } while ((getlatency != latency) && (timeout > 0));
  422. if(getlatency != latency)
  423. {
  424. status = ERROR;
  425. }
  426. else
  427. {
  428. status = SUCCESS;
  429. }
  430. }
  431. return status;
  432. }
  433. /**
  434. * @brief This function configures system clock at maximum frequency with HSI as clock source of the PLL
  435. * @note The application need to ensure that PLL is disabled.
  436. * @note Function is based on the following formula:
  437. * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
  438. * - PLLM: ensure that the VCO input frequency ranges from @ref RCC_PLLVCO_INPUT_MIN to @ref RCC_PLLVCO_INPUT_MAX (PLLVCO_input = HSI frequency / PLLM)
  439. * - PLLN: ensure that the VCO output frequency is between @ref RCC_PLLVCO_OUTPUT_MIN and @ref RCC_PLLVCO_OUTPUT_MAX (PLLVCO_output = PLLVCO_input * PLLN)
  440. * - PLLP: ensure that max frequency at 180000000 Hz is reach (PLLVCO_output / PLLP)
  441. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  442. * the configuration information for the PLL.
  443. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  444. * the configuration information for the BUS prescalers.
  445. * @retval An ErrorStatus enumeration value:
  446. * - SUCCESS: Max frequency configuration done
  447. * - ERROR: Max frequency configuration not done
  448. */
  449. ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
  450. LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  451. {
  452. ErrorStatus status = SUCCESS;
  453. uint32_t pllfreq = 0U;
  454. /* Check if one of the PLL is enabled */
  455. if(UTILS_PLL_IsBusy() == SUCCESS)
  456. {
  457. /* Calculate the new PLL output frequency */
  458. pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct);
  459. /* Enable HSI if not enabled */
  460. if(LL_RCC_HSI_IsReady() != 1U)
  461. {
  462. LL_RCC_HSI_Enable();
  463. while (LL_RCC_HSI_IsReady() != 1U)
  464. {
  465. /* Wait for HSI ready */
  466. }
  467. }
  468. /* Configure PLL */
  469. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  470. UTILS_PLLInitStruct->PLLP);
  471. /* Enable PLL and switch system clock to PLL */
  472. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  473. }
  474. else
  475. {
  476. /* Current PLL configuration cannot be modified */
  477. status = ERROR;
  478. }
  479. return status;
  480. }
  481. /**
  482. * @brief This function configures system clock with HSE as clock source of the PLL
  483. * @note The application need to ensure that PLL is disabled.
  484. * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
  485. * - PLLM: ensure that the VCO input frequency ranges from @ref RCC_PLLVCO_INPUT_MIN to @ref RCC_PLLVCO_INPUT_MAX (PLLVCO_input = HSI frequency / PLLM)
  486. * - PLLN: ensure that the VCO output frequency is between @ref RCC_PLLVCO_OUTPUT_MIN and @ref RCC_PLLVCO_OUTPUT_MAX (PLLVCO_output = PLLVCO_input * PLLN)
  487. * - PLLP: ensure that max frequency at 180000000 Hz is reach (PLLVCO_output / PLLP)
  488. * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 26000000
  489. * @param HSEBypass This parameter can be one of the following values:
  490. * @arg @ref LL_UTILS_HSEBYPASS_ON
  491. * @arg @ref LL_UTILS_HSEBYPASS_OFF
  492. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  493. * the configuration information for the PLL.
  494. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  495. * the configuration information for the BUS prescalers.
  496. * @retval An ErrorStatus enumeration value:
  497. * - SUCCESS: Max frequency configuration done
  498. * - ERROR: Max frequency configuration not done
  499. */
  500. ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
  501. LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  502. {
  503. ErrorStatus status = SUCCESS;
  504. uint32_t pllfreq = 0U;
  505. /* Check the parameters */
  506. assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
  507. assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
  508. /* Check if one of the PLL is enabled */
  509. if(UTILS_PLL_IsBusy() == SUCCESS)
  510. {
  511. /* Calculate the new PLL output frequency */
  512. pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
  513. /* Enable HSE if not enabled */
  514. if(LL_RCC_HSE_IsReady() != 1U)
  515. {
  516. /* Check if need to enable HSE bypass feature or not */
  517. if(HSEBypass == LL_UTILS_HSEBYPASS_ON)
  518. {
  519. LL_RCC_HSE_EnableBypass();
  520. }
  521. else
  522. {
  523. LL_RCC_HSE_DisableBypass();
  524. }
  525. /* Enable HSE */
  526. LL_RCC_HSE_Enable();
  527. while (LL_RCC_HSE_IsReady() != 1U)
  528. {
  529. /* Wait for HSE ready */
  530. }
  531. }
  532. /* Configure PLL */
  533. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
  534. UTILS_PLLInitStruct->PLLP);
  535. /* Enable PLL and switch system clock to PLL */
  536. status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
  537. }
  538. else
  539. {
  540. /* Current PLL configuration cannot be modified */
  541. status = ERROR;
  542. }
  543. return status;
  544. }
  545. /**
  546. * @}
  547. */
  548. /**
  549. * @}
  550. */
  551. /** @addtogroup UTILS_LL_Private_Functions
  552. * @{
  553. */
  554. /**
  555. * @brief Function to check that PLL can be modified
  556. * @param PLL_InputFrequency PLL input frequency (in Hz)
  557. * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
  558. * the configuration information for the PLL.
  559. * @retval PLL output frequency (in Hz)
  560. */
  561. static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
  562. {
  563. uint32_t pllfreq = 0U;
  564. /* Check the parameters */
  565. assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
  566. assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
  567. assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct->PLLP));
  568. /* Check different PLL parameters according to RM */
  569. /* - PLLM: ensure that the VCO input frequency ranges from @ref UTILS_PLLVCO_INPUT_MIN to @ref UTILS_PLLVCO_INPUT_MAX MHz. */
  570. pllfreq = PLL_InputFrequency / (UTILS_PLLInitStruct->PLLM & (RCC_PLLCFGR_PLLM >> RCC_PLLCFGR_PLLM_Pos));
  571. assert_param(IS_LL_UTILS_PLLVCO_INPUT(pllfreq));
  572. /* - PLLN: ensure that the VCO output frequency is between @ref UTILS_PLLVCO_OUTPUT_MIN and @ref UTILS_PLLVCO_OUTPUT_MAX .*/
  573. pllfreq = pllfreq * (UTILS_PLLInitStruct->PLLN & (RCC_PLLCFGR_PLLN >> RCC_PLLCFGR_PLLN_Pos));
  574. assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(pllfreq));
  575. /* - PLLP: ensure that max frequency at @ref RCC_MAX_FREQUENCY Hz is reached */
  576. pllfreq = pllfreq / (((UTILS_PLLInitStruct->PLLP >> RCC_PLLCFGR_PLLP_Pos) + 1) * 2);
  577. assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq));
  578. return pllfreq;
  579. }
  580. /**
  581. * @brief Function to check that PLL can be modified
  582. * @retval An ErrorStatus enumeration value:
  583. * - SUCCESS: PLL modification can be done
  584. * - ERROR: PLL is busy
  585. */
  586. static ErrorStatus UTILS_PLL_IsBusy(void)
  587. {
  588. ErrorStatus status = SUCCESS;
  589. /* Check if PLL is busy*/
  590. if(LL_RCC_PLL_IsReady() != 0U)
  591. {
  592. /* PLL configuration cannot be modified */
  593. status = ERROR;
  594. }
  595. #if defined(RCC_PLLSAI_SUPPORT)
  596. /* Check if PLLSAI is busy*/
  597. if(LL_RCC_PLLSAI_IsReady() != 0U)
  598. {
  599. /* PLLSAI1 configuration cannot be modified */
  600. status = ERROR;
  601. }
  602. #endif /*RCC_PLLSAI_SUPPORT*/
  603. #if defined(RCC_PLLI2S_SUPPORT)
  604. /* Check if PLLI2S is busy*/
  605. if(LL_RCC_PLLI2S_IsReady() != 0U)
  606. {
  607. /* PLLI2S configuration cannot be modified */
  608. status = ERROR;
  609. }
  610. #endif /*RCC_PLLI2S_SUPPORT*/
  611. return status;
  612. }
  613. /**
  614. * @brief Function to enable PLL and switch system clock to PLL
  615. * @param SYSCLK_Frequency SYSCLK frequency
  616. * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
  617. * the configuration information for the BUS prescalers.
  618. * @retval An ErrorStatus enumeration value:
  619. * - SUCCESS: No problem to switch system to PLL
  620. * - ERROR: Problem to switch system to PLL
  621. */
  622. static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
  623. {
  624. ErrorStatus status = SUCCESS;
  625. uint32_t hclk_frequency = 0U;
  626. assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
  627. assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
  628. assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct->APB2CLKDivider));
  629. /* Calculate HCLK frequency */
  630. hclk_frequency = __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider);
  631. /* Increasing the number of wait states because of higher CPU frequency */
  632. if(SystemCoreClock < hclk_frequency)
  633. {
  634. /* Set FLASH latency to highest latency */
  635. status = LL_SetFlashLatency(hclk_frequency);
  636. }
  637. /* Update system clock configuration */
  638. if(status == SUCCESS)
  639. {
  640. /* Enable PLL */
  641. LL_RCC_PLL_Enable();
  642. while (LL_RCC_PLL_IsReady() != 1U)
  643. {
  644. /* Wait for PLL ready */
  645. }
  646. /* Sysclk activation on the main PLL */
  647. LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
  648. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  649. while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  650. {
  651. /* Wait for system clock switch to PLL */
  652. }
  653. /* Set APB1 & APB2 prescaler*/
  654. LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
  655. LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct->APB2CLKDivider);
  656. }
  657. /* Decreasing the number of wait states because of lower CPU frequency */
  658. if(SystemCoreClock > hclk_frequency)
  659. {
  660. /* Set FLASH latency to lowest latency */
  661. status = LL_SetFlashLatency(hclk_frequency);
  662. }
  663. /* Update SystemCoreClock variable */
  664. if(status == SUCCESS)
  665. {
  666. LL_SetSystemCoreClock(hclk_frequency);
  667. }
  668. return status;
  669. }
  670. /**
  671. * @}
  672. */
  673. /**
  674. * @}
  675. */
  676. /**
  677. * @}
  678. */
  679. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/