stm32l1xx_ll_utils.c 24 KB

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