训练营PLSR题目
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

750 行
31 KiB

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