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.
 
 
 

699 lines
20 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_ll_rcc.c
  4. * @author MCD Application Team
  5. * @brief RCC LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright(c) 2016 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. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32l0xx_ll_rcc.h"
  22. #ifdef USE_FULL_ASSERT
  23. #include "stm32_assert.h"
  24. #else
  25. #define assert_param(expr) ((void)0U)
  26. #endif /* USE_FULL_ASSERT */
  27. /** @addtogroup STM32L0xx_LL_Driver
  28. * @{
  29. */
  30. #if defined(RCC)
  31. /** @defgroup RCC_LL RCC
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /* Private macros ------------------------------------------------------------*/
  38. /** @addtogroup RCC_LL_Private_Macros
  39. * @{
  40. */
  41. #if defined(RCC_CCIPR_USART1SEL) && defined(RCC_CCIPR_USART2SEL)
  42. #define IS_LL_RCC_USART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USART1_CLKSOURCE) \
  43. || ((__VALUE__) == LL_RCC_USART2_CLKSOURCE))
  44. #elif defined(RCC_CCIPR_USART1SEL) && !defined(RCC_CCIPR_USART2SEL)
  45. #define IS_LL_RCC_USART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USART1_CLKSOURCE))
  46. #else
  47. #define IS_LL_RCC_USART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USART2_CLKSOURCE))
  48. #endif /* RCC_CCIPR_USART1SEL && RCC_CCIPR_USART2SEL */
  49. #define IS_LL_RCC_LPUART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_LPUART1_CLKSOURCE))
  50. #if defined(RCC_CCIPR_I2C3SEL)
  51. #define IS_LL_RCC_I2C_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_I2C1_CLKSOURCE) \
  52. || ((__VALUE__) == LL_RCC_I2C3_CLKSOURCE))
  53. #else
  54. #define IS_LL_RCC_I2C_CLKSOURCE(__VALUE__) ((__VALUE__) == LL_RCC_I2C1_CLKSOURCE)
  55. #endif /* RCC_CCIPR_I2C3SEL */
  56. #define IS_LL_RCC_LPTIM_CLKSOURCE(__VALUE__) ((__VALUE__) == LL_RCC_LPTIM1_CLKSOURCE)
  57. #if defined(USB)
  58. #define IS_LL_RCC_USB_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USB_CLKSOURCE))
  59. #endif /* USB */
  60. /**
  61. * @}
  62. */
  63. /* Private function prototypes -----------------------------------------------*/
  64. /** @defgroup RCC_LL_Private_Functions RCC Private functions
  65. * @{
  66. */
  67. uint32_t RCC_GetSystemClockFreq(void);
  68. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency);
  69. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency);
  70. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency);
  71. uint32_t RCC_PLL_GetFreqDomain_SYS(void);
  72. /**
  73. * @}
  74. */
  75. /* Exported functions --------------------------------------------------------*/
  76. /** @addtogroup RCC_LL_Exported_Functions
  77. * @{
  78. */
  79. /** @addtogroup RCC_LL_EF_Init
  80. * @{
  81. */
  82. /**
  83. * @brief Reset the RCC clock configuration to the default reset state.
  84. * @note The default reset state of the clock configuration is given below:
  85. * - MSI ON and used as system clock source
  86. * - HSE, HSI and PLL OFF
  87. * - AHB, APB1 and APB2 prescaler set to 1.
  88. * - CSS, MCO OFF
  89. * - All interrupts disabled
  90. * @note This function doesn't modify the configuration of the
  91. * - Peripheral clocks
  92. * - LSI, LSE and RTC clocks
  93. * @retval An ErrorStatus enumeration value:
  94. * - SUCCESS: RCC registers are de-initialized
  95. * - ERROR: not applicable
  96. */
  97. ErrorStatus LL_RCC_DeInit(void)
  98. {
  99. uint32_t vl_mask;
  100. /* Set MSION bit */
  101. LL_RCC_MSI_Enable();
  102. /* Insure MSIRDY bit is set before writing default MSIRANGE value */
  103. while (LL_RCC_MSI_IsReady() == 0U)
  104. {
  105. __NOP();
  106. }
  107. /* Set MSIRANGE default value */
  108. LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_5);
  109. /* Set MSITRIM bits to the reset value*/
  110. LL_RCC_MSI_SetCalibTrimming(0U);
  111. /* Set HSITRIM bits to the reset value*/
  112. LL_RCC_HSI_SetCalibTrimming(0x10U);
  113. /* Reset SW, HPRE, PPRE1, PPRE2, MCOSEL and MCOPRE bits */
  114. vl_mask = 0xFFFFFFFFU;
  115. CLEAR_BIT(vl_mask, RCC_CFGR_SW | RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2 | \
  116. RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE);
  117. LL_RCC_WriteReg(CFGR, vl_mask);
  118. /* Reset HSI, HSE, PLL */
  119. vl_mask = LL_RCC_ReadReg(CR);
  120. #if defined(RCC_CR_HSIOUTEN)
  121. CLEAR_BIT(vl_mask, RCC_CR_HSION| RCC_CR_HSIKERON| RCC_CR_HSIDIVEN | RCC_CR_HSIOUTEN | \
  122. RCC_CR_HSEON | RCC_CR_PLLON);
  123. #else
  124. CLEAR_BIT(vl_mask, RCC_CR_HSION| RCC_CR_HSIKERON| RCC_CR_HSIDIVEN | \
  125. RCC_CR_HSEON | RCC_CR_PLLON);
  126. #endif
  127. LL_RCC_WriteReg(CR, vl_mask);
  128. /* Delay after an RCC peripheral clock */
  129. vl_mask = LL_RCC_ReadReg(CR);
  130. /* Reset HSEBYP bit */
  131. LL_RCC_HSE_DisableBypass();
  132. /* Set RCC_CR_RTCPRE to 0b00*/
  133. CLEAR_BIT(vl_mask, RCC_CR_RTCPRE);
  134. LL_RCC_WriteReg(CR, vl_mask);
  135. /* Insure PLL is disabled before to reset PLLSRC/PLLMUL/PLLDIV in CFGR register */
  136. while(LL_RCC_PLL_IsReady() != 0U) {};
  137. /* Reset CFGR register */
  138. LL_RCC_WriteReg(CFGR, 0x00000000U);
  139. #if defined(RCC_HSI48_SUPPORT)
  140. /* Reset CRRCR register to disable HSI48 */
  141. #if defined(RCC_CRRCR_HSI48DIV6OUTEN)
  142. CLEAR_BIT(RCC->CRRCR, (RCC_CRRCR_HSI48ON | RCC_CRRCR_HSI48DIV6OUTEN));
  143. #else
  144. CLEAR_BIT(RCC->CRRCR, RCC_CRRCR_HSI48ON);
  145. #endif
  146. #endif /*RCC_HSI48_SUPPORT*/
  147. /* Disable all interrupts */
  148. LL_RCC_WriteReg(CIER, 0x00000000U);
  149. /* Disable all interrupt flags */
  150. LL_RCC_WriteReg(CICR, 0xFFFFFFFFU);
  151. /* Clear reset flags */
  152. LL_RCC_ClearResetFlags();
  153. return SUCCESS;
  154. }
  155. /**
  156. * @}
  157. */
  158. /** @addtogroup RCC_LL_EF_Get_Freq
  159. * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks
  160. * and different peripheral clocks available on the device.
  161. * @note If SYSCLK source is MSI, function returns values based on MSI clock(*)
  162. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
  163. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  164. * @note If SYSCLK source is PLL, function returns values based on
  165. * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
  166. * @note (*) MSI clock depends on the selected MSI range but the real value
  167. * may vary depending on the variations in voltage and temperature.
  168. * @note (**) HSI_VALUE is a defined constant but the real value may vary
  169. * depending on the variations in voltage and temperature.
  170. * @note (***) HSE_VALUE is a defined constant, user has to ensure that
  171. * HSE_VALUE is same as the real frequency of the crystal used.
  172. * Otherwise, this function may have wrong result.
  173. * @note The result of this function could be incorrect when using fractional
  174. * value for HSE crystal.
  175. * @note This function can be used by the user application to compute the
  176. * baud-rate for the communication peripherals or configure other parameters.
  177. * @{
  178. */
  179. /**
  180. * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks
  181. * @note Each time SYSCLK, HCLK, PCLK1 and/or PCLK2 clock changes, this function
  182. * must be called to update structure fields. Otherwise, any
  183. * configuration based on this function will be incorrect.
  184. * @param RCC_Clocks pointer to a @ref LL_RCC_ClocksTypeDef structure which will hold the clocks frequencies
  185. * @retval None
  186. */
  187. void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks)
  188. {
  189. /* Get SYSCLK frequency */
  190. RCC_Clocks->SYSCLK_Frequency = RCC_GetSystemClockFreq();
  191. /* HCLK clock frequency */
  192. RCC_Clocks->HCLK_Frequency = RCC_GetHCLKClockFreq(RCC_Clocks->SYSCLK_Frequency);
  193. /* PCLK1 clock frequency */
  194. RCC_Clocks->PCLK1_Frequency = RCC_GetPCLK1ClockFreq(RCC_Clocks->HCLK_Frequency);
  195. /* PCLK2 clock frequency */
  196. RCC_Clocks->PCLK2_Frequency = RCC_GetPCLK2ClockFreq(RCC_Clocks->HCLK_Frequency);
  197. }
  198. /**
  199. * @brief Return USARTx clock frequency
  200. * @param USARTxSource This parameter can be one of the following values:
  201. * @arg @ref LL_RCC_USART1_CLKSOURCE
  202. * @arg @ref LL_RCC_USART2_CLKSOURCE (*)
  203. *
  204. * (*) value not defined in all devices.
  205. * @retval USART clock frequency (in Hz)
  206. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI or LSE) is not ready
  207. */
  208. uint32_t LL_RCC_GetUSARTClockFreq(uint32_t USARTxSource)
  209. {
  210. uint32_t usart_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  211. /* Check parameter */
  212. assert_param(IS_LL_RCC_USART_CLKSOURCE(USARTxSource));
  213. #if defined(RCC_CCIPR_USART1SEL)
  214. if (USARTxSource == LL_RCC_USART1_CLKSOURCE)
  215. {
  216. /* USART1CLK clock frequency */
  217. switch (LL_RCC_GetUSARTClockSource(USARTxSource))
  218. {
  219. case LL_RCC_USART1_CLKSOURCE_SYSCLK: /* USART1 Clock is System Clock */
  220. usart_frequency = RCC_GetSystemClockFreq();
  221. break;
  222. case LL_RCC_USART1_CLKSOURCE_HSI: /* USART1 Clock is HSI Osc. */
  223. if (LL_RCC_HSI_IsReady() != 0U)
  224. {
  225. if (LL_RCC_IsActiveFlag_HSIDIV() != 0U)
  226. {
  227. usart_frequency = (HSI_VALUE >> 2U);
  228. }
  229. else
  230. {
  231. usart_frequency = HSI_VALUE;
  232. }
  233. }
  234. break;
  235. case LL_RCC_USART1_CLKSOURCE_LSE: /* USART1 Clock is LSE Osc. */
  236. if (LL_RCC_LSE_IsReady() != 0U)
  237. {
  238. usart_frequency = LSE_VALUE;
  239. }
  240. break;
  241. case LL_RCC_USART1_CLKSOURCE_PCLK2: /* USART1 Clock is PCLK2 */
  242. default:
  243. usart_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  244. break;
  245. }
  246. }
  247. #endif /* RCC_CCIPR_USART1SEL */
  248. #if defined(RCC_CCIPR_USART2SEL)
  249. if (USARTxSource == LL_RCC_USART2_CLKSOURCE)
  250. {
  251. /* USART2CLK clock frequency */
  252. switch (LL_RCC_GetUSARTClockSource(USARTxSource))
  253. {
  254. case LL_RCC_USART2_CLKSOURCE_SYSCLK: /* USART2 Clock is System Clock */
  255. usart_frequency = RCC_GetSystemClockFreq();
  256. break;
  257. case LL_RCC_USART2_CLKSOURCE_HSI: /* USART2 Clock is HSI Osc. */
  258. if (LL_RCC_HSI_IsReady() != 0U)
  259. {
  260. if (LL_RCC_IsActiveFlag_HSIDIV() != 0U)
  261. {
  262. usart_frequency = (HSI_VALUE >> 2U);
  263. }
  264. else
  265. {
  266. usart_frequency = HSI_VALUE;
  267. }
  268. }
  269. break;
  270. case LL_RCC_USART2_CLKSOURCE_LSE: /* USART2 Clock is LSE Osc. */
  271. if (LL_RCC_LSE_IsReady() != 0U)
  272. {
  273. usart_frequency = LSE_VALUE;
  274. }
  275. break;
  276. case LL_RCC_USART2_CLKSOURCE_PCLK1: /* USART2 Clock is PCLK1 */
  277. default:
  278. usart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  279. break;
  280. }
  281. }
  282. #endif /* RCC_CCIPR_USART2SEL */
  283. return usart_frequency;
  284. }
  285. /**
  286. * @brief Return I2Cx clock frequency
  287. * @param I2CxSource This parameter can be one of the following values:
  288. * @arg @ref LL_RCC_I2C1_CLKSOURCE
  289. * @arg @ref LL_RCC_I2C3_CLKSOURCE (*)
  290. *
  291. * (*) value not defined in all devices
  292. * @retval I2C clock frequency (in Hz)
  293. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that HSI oscillator is not ready
  294. */
  295. uint32_t LL_RCC_GetI2CClockFreq(uint32_t I2CxSource)
  296. {
  297. uint32_t i2c_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  298. /* Check parameter */
  299. assert_param(IS_LL_RCC_I2C_CLKSOURCE(I2CxSource));
  300. /* I2C1 CLK clock frequency */
  301. if (I2CxSource == LL_RCC_I2C1_CLKSOURCE)
  302. {
  303. switch (LL_RCC_GetI2CClockSource(I2CxSource))
  304. {
  305. case LL_RCC_I2C1_CLKSOURCE_SYSCLK: /* I2C1 Clock is System Clock */
  306. i2c_frequency = RCC_GetSystemClockFreq();
  307. break;
  308. case LL_RCC_I2C1_CLKSOURCE_HSI: /* I2C1 Clock is HSI Osc. */
  309. if (LL_RCC_HSI_IsReady() != 0U)
  310. {
  311. if (LL_RCC_IsActiveFlag_HSIDIV() != 0U)
  312. {
  313. i2c_frequency = (HSI_VALUE >> 2U);
  314. }
  315. else
  316. {
  317. i2c_frequency = HSI_VALUE;
  318. }
  319. }
  320. break;
  321. case LL_RCC_I2C1_CLKSOURCE_PCLK1: /* I2C1 Clock is PCLK1 */
  322. default:
  323. i2c_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  324. break;
  325. }
  326. }
  327. #if defined(RCC_CCIPR_I2C3SEL)
  328. /* I2C3 CLK clock frequency */
  329. if (I2CxSource == LL_RCC_I2C3_CLKSOURCE)
  330. {
  331. switch (LL_RCC_GetI2CClockSource(I2CxSource))
  332. {
  333. case LL_RCC_I2C3_CLKSOURCE_SYSCLK: /* I2C3 Clock is System Clock */
  334. i2c_frequency = RCC_GetSystemClockFreq();
  335. break;
  336. case LL_RCC_I2C3_CLKSOURCE_HSI: /* I2C3 Clock is HSI Osc. */
  337. if (LL_RCC_HSI_IsReady() != 0U)
  338. {
  339. if (LL_RCC_IsActiveFlag_HSIDIV() != 0U)
  340. {
  341. i2c_frequency = (HSI_VALUE >> 2U);
  342. }
  343. else
  344. {
  345. i2c_frequency = HSI_VALUE;
  346. }
  347. }
  348. break;
  349. case LL_RCC_I2C3_CLKSOURCE_PCLK1: /* I2C3 Clock is PCLK1 */
  350. default:
  351. i2c_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  352. break;
  353. }
  354. }
  355. #endif /*RCC_CCIPR_I2C3SEL*/
  356. return i2c_frequency;
  357. }
  358. /**
  359. * @brief Return LPUARTx clock frequency
  360. * @param LPUARTxSource This parameter can be one of the following values:
  361. * @arg @ref LL_RCC_LPUART1_CLKSOURCE
  362. * @retval LPUART clock frequency (in Hz)
  363. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI or LSE) is not ready
  364. */
  365. uint32_t LL_RCC_GetLPUARTClockFreq(uint32_t LPUARTxSource)
  366. {
  367. uint32_t lpuart_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  368. /* Check parameter */
  369. assert_param(IS_LL_RCC_LPUART_CLKSOURCE(LPUARTxSource));
  370. /* LPUART1CLK clock frequency */
  371. switch (LL_RCC_GetLPUARTClockSource(LPUARTxSource))
  372. {
  373. case LL_RCC_LPUART1_CLKSOURCE_SYSCLK: /* LPUART1 Clock is System Clock */
  374. lpuart_frequency = RCC_GetSystemClockFreq();
  375. break;
  376. case LL_RCC_LPUART1_CLKSOURCE_HSI: /* LPUART1 Clock is HSI Osc. */
  377. if (LL_RCC_HSI_IsReady() != 0U)
  378. {
  379. if (LL_RCC_IsActiveFlag_HSIDIV() != 0U)
  380. {
  381. lpuart_frequency = (HSI_VALUE >> 2U);
  382. }
  383. else
  384. {
  385. lpuart_frequency = HSI_VALUE;
  386. }
  387. }
  388. break;
  389. case LL_RCC_LPUART1_CLKSOURCE_LSE: /* LPUART1 Clock is LSE Osc. */
  390. if (LL_RCC_LSE_IsReady() != 0U)
  391. {
  392. lpuart_frequency = LSE_VALUE;
  393. }
  394. break;
  395. case LL_RCC_LPUART1_CLKSOURCE_PCLK1: /* LPUART1 Clock is PCLK1 */
  396. default:
  397. lpuart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  398. break;
  399. }
  400. return lpuart_frequency;
  401. }
  402. /**
  403. * @brief Return LPTIMx clock frequency
  404. * @param LPTIMxSource This parameter can be one of the following values:
  405. * @arg @ref LL_RCC_LPTIM1_CLKSOURCE
  406. * @retval LPTIM clock frequency (in Hz)
  407. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI or LSE) is not ready
  408. */
  409. uint32_t LL_RCC_GetLPTIMClockFreq(uint32_t LPTIMxSource)
  410. {
  411. uint32_t lptim_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  412. /* Check parameter */
  413. assert_param(IS_LL_RCC_LPTIM_CLKSOURCE(LPTIMxSource));
  414. if (LPTIMxSource == LL_RCC_LPTIM1_CLKSOURCE)
  415. {
  416. /* LPTIM1CLK clock frequency */
  417. switch (LL_RCC_GetLPTIMClockSource(LPTIMxSource))
  418. {
  419. case LL_RCC_LPTIM1_CLKSOURCE_LSI: /* LPTIM1 Clock is LSI Osc. */
  420. if (LL_RCC_LSI_IsReady() != 0U)
  421. {
  422. lptim_frequency = LSI_VALUE;
  423. }
  424. break;
  425. case LL_RCC_LPTIM1_CLKSOURCE_HSI: /* LPTIM1 Clock is HSI Osc. */
  426. if (LL_RCC_HSI_IsReady() != 0U)
  427. {
  428. if (LL_RCC_IsActiveFlag_HSIDIV() != 0U)
  429. {
  430. lptim_frequency = (HSI_VALUE >> 2U);
  431. }
  432. else
  433. {
  434. lptim_frequency = HSI_VALUE;
  435. }
  436. }
  437. break;
  438. case LL_RCC_LPTIM1_CLKSOURCE_LSE: /* LPTIM1 Clock is LSE Osc. */
  439. if (LL_RCC_LSE_IsReady() != 0U)
  440. {
  441. lptim_frequency = LSE_VALUE;
  442. }
  443. break;
  444. case LL_RCC_LPTIM1_CLKSOURCE_PCLK1: /* LPTIM1 Clock is PCLK1 */
  445. default:
  446. lptim_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  447. break;
  448. }
  449. }
  450. return lptim_frequency;
  451. }
  452. #if defined(USB)
  453. /**
  454. * @brief Return USBx clock frequency
  455. * @param USBxSource This parameter can be one of the following values:
  456. * @arg @ref LL_RCC_USB_CLKSOURCE
  457. * @retval USB clock frequency (in Hz)
  458. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI48) or PLL is not ready
  459. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NA indicates that no clock source selected
  460. */
  461. uint32_t LL_RCC_GetUSBClockFreq(uint32_t USBxSource)
  462. {
  463. uint32_t usb_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  464. /* Check parameter */
  465. assert_param(IS_LL_RCC_USB_CLKSOURCE(USBxSource));
  466. /* USBCLK clock frequency */
  467. switch (LL_RCC_GetUSBClockSource(USBxSource))
  468. {
  469. case LL_RCC_USB_CLKSOURCE_PLL: /* PLL clock used as USB clock source */
  470. if (LL_RCC_PLL_IsReady() != 0U)
  471. {
  472. usb_frequency = RCC_PLL_GetFreqDomain_SYS();
  473. }
  474. break;
  475. case LL_RCC_USB_CLKSOURCE_HSI48: /* HSI48 clock used as USB clock source */
  476. default:
  477. if (LL_RCC_HSI48_IsReady() != 0U)
  478. {
  479. usb_frequency = HSI48_VALUE;
  480. }
  481. break;
  482. }
  483. return usb_frequency;
  484. }
  485. #endif /* USB */
  486. /**
  487. * @}
  488. */
  489. /**
  490. * @}
  491. */
  492. /** @addtogroup RCC_LL_Private_Functions
  493. * @{
  494. */
  495. /**
  496. * @brief Return SYSTEM clock frequency
  497. * @retval SYSTEM clock frequency (in Hz)
  498. */
  499. uint32_t RCC_GetSystemClockFreq(void)
  500. {
  501. uint32_t frequency;
  502. /* Get SYSCLK source -------------------------------------------------------*/
  503. switch (LL_RCC_GetSysClkSource())
  504. {
  505. case LL_RCC_SYS_CLKSOURCE_STATUS_MSI: /* MSI used as system clock source */
  506. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  507. break;
  508. case LL_RCC_SYS_CLKSOURCE_STATUS_HSI: /* HSI used as system clock source */
  509. if (LL_RCC_IsActiveFlag_HSIDIV() != 0U)
  510. {
  511. frequency = (HSI_VALUE >> 2U);
  512. }
  513. else
  514. {
  515. frequency = HSI_VALUE;
  516. }
  517. break;
  518. case LL_RCC_SYS_CLKSOURCE_STATUS_HSE: /* HSE used as system clock source */
  519. frequency = HSE_VALUE;
  520. break;
  521. case LL_RCC_SYS_CLKSOURCE_STATUS_PLL: /* PLL used as system clock source */
  522. frequency = RCC_PLL_GetFreqDomain_SYS();
  523. break;
  524. default:
  525. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  526. break;
  527. }
  528. return frequency;
  529. }
  530. /**
  531. * @brief Return HCLK clock frequency
  532. * @param SYSCLK_Frequency SYSCLK clock frequency
  533. * @retval HCLK clock frequency (in Hz)
  534. */
  535. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency)
  536. {
  537. /* HCLK clock frequency */
  538. return __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, LL_RCC_GetAHBPrescaler());
  539. }
  540. /**
  541. * @brief Return PCLK1 clock frequency
  542. * @param HCLK_Frequency HCLK clock frequency
  543. * @retval PCLK1 clock frequency (in Hz)
  544. */
  545. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency)
  546. {
  547. /* PCLK1 clock frequency */
  548. return __LL_RCC_CALC_PCLK1_FREQ(HCLK_Frequency, LL_RCC_GetAPB1Prescaler());
  549. }
  550. /**
  551. * @brief Return PCLK2 clock frequency
  552. * @param HCLK_Frequency HCLK clock frequency
  553. * @retval PCLK2 clock frequency (in Hz)
  554. */
  555. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency)
  556. {
  557. /* PCLK2 clock frequency */
  558. return __LL_RCC_CALC_PCLK2_FREQ(HCLK_Frequency, LL_RCC_GetAPB2Prescaler());
  559. }
  560. /**
  561. * @brief Return PLL clock frequency used for system domain
  562. * @retval PLL clock frequency (in Hz)
  563. */
  564. uint32_t RCC_PLL_GetFreqDomain_SYS(void)
  565. {
  566. uint32_t pllinputfreq, pllsource;
  567. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL divider) * PLL Multiplicator */
  568. /* Get PLL source */
  569. pllsource = LL_RCC_PLL_GetMainSource();
  570. switch (pllsource)
  571. {
  572. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  573. if (LL_RCC_IsActiveFlag_HSIDIV() != 0U)
  574. {
  575. pllinputfreq = (HSI_VALUE >> 2U);
  576. }
  577. else
  578. {
  579. pllinputfreq = HSI_VALUE;
  580. }
  581. break;
  582. default: /* HSE used as PLL clock source */
  583. pllinputfreq = HSE_VALUE;
  584. break;
  585. }
  586. return __LL_RCC_CALC_PLLCLK_FREQ(pllinputfreq, LL_RCC_PLL_GetMultiplicator(), LL_RCC_PLL_GetDivider());
  587. }
  588. /**
  589. * @}
  590. */
  591. /**
  592. * @}
  593. */
  594. #endif /* defined(RCC) */
  595. /**
  596. * @}
  597. */
  598. #endif /* USE_FULL_LL_DRIVER */
  599. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/