You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

759 lines
32 KiB

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