stm32g0xx_ll_rcc.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_ll_rcc.c
  4. * @author MCD Application Team
  5. * @brief RCC LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2018 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file in
  13. * the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. ******************************************************************************
  16. */
  17. #if defined(USE_FULL_LL_DRIVER)
  18. /* Includes ------------------------------------------------------------------*/
  19. #include "stm32g0xx_ll_rcc.h"
  20. #ifdef USE_FULL_ASSERT
  21. #include "stm32_assert.h"
  22. #else
  23. #define assert_param(expr) ((void)0U)
  24. #endif /* USE_FULL_ASSERT */
  25. /** @addtogroup STM32G0xx_LL_Driver
  26. * @{
  27. */
  28. #if defined(RCC)
  29. /** @addtogroup RCC_LL
  30. * @{
  31. */
  32. /* Private types -------------------------------------------------------------*/
  33. /* Private variables ---------------------------------------------------------*/
  34. /* Private constants ---------------------------------------------------------*/
  35. /* Private macros ------------------------------------------------------------*/
  36. /** @addtogroup RCC_LL_Private_Macros
  37. * @{
  38. */
  39. #if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
  40. #define IS_LL_RCC_USART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USART1_CLKSOURCE) \
  41. || ((__VALUE__) == LL_RCC_USART2_CLKSOURCE) \
  42. || ((__VALUE__) == LL_RCC_USART3_CLKSOURCE))
  43. #elif defined(STM32G081xx) || defined(STM32G071xx) || defined(STM32G070xx)
  44. #define IS_LL_RCC_USART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USART1_CLKSOURCE) \
  45. || ((__VALUE__) == LL_RCC_USART2_CLKSOURCE))
  46. #else
  47. #define IS_LL_RCC_USART_CLKSOURCE(__VALUE__) ((__VALUE__) == LL_RCC_USART1_CLKSOURCE)
  48. #endif /* STM32G0C1xx || STM32G0B1xx || STM32G0B0xx */
  49. #if defined(LPUART1) && defined(LPUART2)
  50. #define IS_LL_RCC_LPUART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_LPUART1_CLKSOURCE) \
  51. || ((__VALUE__) == LL_RCC_LPUART2_CLKSOURCE))
  52. #elif defined(LPUART1)
  53. #define IS_LL_RCC_LPUART_CLKSOURCE(__VALUE__) ((__VALUE__) == LL_RCC_LPUART1_CLKSOURCE)
  54. #endif /* LPUART1 && LPUART2 */
  55. #if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
  56. #define IS_LL_RCC_I2C_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_I2C1_CLKSOURCE) \
  57. || ((__VALUE__) == LL_RCC_I2C2_CLKSOURCE))
  58. #else
  59. #define IS_LL_RCC_I2C_CLKSOURCE(__VALUE__) ((__VALUE__) == LL_RCC_I2C1_CLKSOURCE)
  60. #endif /* STM32G0C1xx || STM32G0B1xx || STM32G0B0xx */
  61. #if defined(LPTIM1) || defined(LPTIM2)
  62. #define IS_LL_RCC_LPTIM_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_LPTIM1_CLKSOURCE) \
  63. || ((__VALUE__) == LL_RCC_LPTIM2_CLKSOURCE))
  64. #endif /* LPTIM1 || LPTIM2 */
  65. #if defined(RNG)
  66. #define IS_LL_RCC_RNG_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_RNG_CLKSOURCE))
  67. #endif /* RNG */
  68. #define IS_LL_RCC_ADC_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_ADC_CLKSOURCE))
  69. #if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
  70. #define IS_LL_RCC_I2S_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_I2S1_CLKSOURCE) \
  71. || ((__VALUE__) == LL_RCC_I2S2_CLKSOURCE))
  72. #else
  73. #define IS_LL_RCC_I2S_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_I2S1_CLKSOURCE))
  74. #endif /* STM32G0C1xx || STM32G0B1xx || STM32G0B0xx */
  75. #if defined(CEC)
  76. #define IS_LL_RCC_CEC_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_CEC_CLKSOURCE))
  77. #endif /* CEC */
  78. #if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
  79. #define IS_LL_RCC_USB_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USB_CLKSOURCE))
  80. #endif /* STM32G0C1xx || STM32G0B1xx || STM32G0B0xx */
  81. #if defined(FDCAN1) || defined(FDCAN2)
  82. #define IS_LL_RCC_FDCAN_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_FDCAN_CLKSOURCE))
  83. #endif /* FDCAN1 || FDCAN2 */
  84. #if defined(RCC_CCIPR_TIM1SEL) && defined(RCC_CCIPR_TIM15SEL)
  85. #define IS_LL_RCC_TIM_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_TIM1_CLKSOURCE) \
  86. || ((__VALUE__) == LL_RCC_TIM15_CLKSOURCE))
  87. #elif defined(RCC_CCIPR_TIM1SEL)
  88. #define IS_LL_RCC_TIM_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_TIM1_CLKSOURCE))
  89. #endif /* RCC_CCIPR_TIM1SEL */
  90. /**
  91. * @}
  92. */
  93. /* Private function prototypes -----------------------------------------------*/
  94. /** @defgroup RCC_LL_Private_Functions RCC Private functions
  95. * @{
  96. */
  97. static uint32_t RCC_GetSystemClockFreq(void);
  98. static uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency);
  99. static uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency);
  100. static uint32_t RCC_PLL_GetFreqDomain_SYS(void);
  101. static uint32_t RCC_PLL_GetFreqDomain_ADC(void);
  102. static uint32_t RCC_PLL_GetFreqDomain_I2S1(void);
  103. #if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
  104. static uint32_t RCC_PLL_GetFreqDomain_I2S2(void);
  105. static uint32_t RCC_PLL_GetFreqDomain_USB(void);
  106. #endif /* STM32G0C1xx || STM32G0B1xx || STM32G0B0xx */
  107. #if defined(FDCAN1) || defined(FDCAN2)
  108. static uint32_t RCC_PLL_GetFreqDomain_FDCAN(void);
  109. #endif /* FDCAN1 || FDCAN2 */
  110. #if defined(RNG)
  111. static uint32_t RCC_PLL_GetFreqDomain_RNG(void);
  112. #endif /* RNG */
  113. #if defined(RCC_PLLQ_SUPPORT) && defined(RCC_CCIPR_TIM1SEL)
  114. static uint32_t RCC_PLL_GetFreqDomain_TIM1(void);
  115. #endif /* RCC_PLLQ_SUPPORT && RCC_CCIPR_TIM1SEL */
  116. #if defined(RCC_CCIPR_TIM15SEL)
  117. static uint32_t RCC_PLL_GetFreqDomain_TIM15(void);
  118. #endif /* RCC_CCIPR_TIM15SEL */
  119. /**
  120. * @}
  121. */
  122. /* Exported functions --------------------------------------------------------*/
  123. /** @addtogroup RCC_LL_Exported_Functions
  124. * @{
  125. */
  126. /** @addtogroup RCC_LL_EF_Init
  127. * @{
  128. */
  129. /**
  130. * @brief Reset the RCC clock configuration to the default reset state.
  131. * @note The default reset state of the clock configuration is given below:
  132. * - HSI ON and used as system clock source
  133. * - HSE and PLL OFF
  134. * - AHB and APB1 prescaler set to 1.
  135. * - CSS, MCO OFF
  136. * - All interrupts disabled
  137. * @note This function does not modify the configuration of the
  138. * - Peripheral clocks
  139. * - LSI, LSE and RTC clocks
  140. * @retval An ErrorStatus enumeration value:
  141. * - SUCCESS: RCC registers are de-initialized
  142. * - ERROR: not applicable
  143. */
  144. ErrorStatus LL_RCC_DeInit(void)
  145. {
  146. /* Set HSION bit and wait for HSI READY bit */
  147. LL_RCC_HSI_Enable();
  148. while (LL_RCC_HSI_IsReady() != 1U)
  149. {}
  150. /* Set HSITRIM bits to reset value*/
  151. LL_RCC_HSI_SetCalibTrimming(0x40U);
  152. /* Reset CFGR register */
  153. LL_RCC_WriteReg(CFGR, 0x00000000U);
  154. /* Reset whole CR register but HSI in 2 steps in case HSEBYP is set */
  155. LL_RCC_WriteReg(CR, RCC_CR_HSION);
  156. while (LL_RCC_HSE_IsReady() != 0U)
  157. {}
  158. LL_RCC_WriteReg(CR, RCC_CR_HSION);
  159. /* Wait for PLL READY bit to be reset */
  160. while (LL_RCC_PLL_IsReady() != 0U)
  161. {}
  162. /* Reset PLLCFGR register */
  163. LL_RCC_WriteReg(PLLCFGR, 16U << RCC_PLLCFGR_PLLN_Pos);
  164. /* Disable all interrupts */
  165. LL_RCC_WriteReg(CIER, 0x00000000U);
  166. /* Clear all interrupts flags */
  167. LL_RCC_WriteReg(CICR, 0xFFFFFFFFU);
  168. return SUCCESS;
  169. }
  170. /**
  171. * @}
  172. */
  173. /** @addtogroup RCC_LL_EF_Get_Freq
  174. * @brief Return the frequencies of different on chip clocks; System, AHB and APB1 buses clocks
  175. * and different peripheral clocks available on the device.
  176. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE divided by HSI division factor(**)
  177. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  178. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(***)
  179. * or HSI_VALUE(**) multiplied/divided by the PLL factors.
  180. * @note (**) HSI_VALUE is a constant defined in this file (default value
  181. * 16 MHz) but the real value may vary depending on the variations
  182. * in voltage and temperature.
  183. * @note (***) HSE_VALUE is a constant defined in this file (default value
  184. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  185. * frequency of the crystal used. Otherwise, this function may
  186. * have wrong result.
  187. * @note The result of this function could be incorrect when using fractional
  188. * value for HSE crystal.
  189. * @note This function can be used by the user application to compute the
  190. * baud-rate for the communication peripherals or configure other parameters.
  191. * @{
  192. */
  193. /**
  194. * @brief Return the frequencies of different on chip clocks; System, AHB and APB1 buses clocks
  195. * @note Each time SYSCLK, HCLK and/or PCLK1 clock changes, this function
  196. * must be called to update structure fields. Otherwise, any
  197. * configuration based on this function will be incorrect.
  198. * @param RCC_Clocks pointer to a @ref LL_RCC_ClocksTypeDef structure which will hold the clocks frequencies
  199. * @retval None
  200. */
  201. void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks)
  202. {
  203. /* Get SYSCLK frequency */
  204. RCC_Clocks->SYSCLK_Frequency = RCC_GetSystemClockFreq();
  205. /* HCLK clock frequency */
  206. RCC_Clocks->HCLK_Frequency = RCC_GetHCLKClockFreq(RCC_Clocks->SYSCLK_Frequency);
  207. /* PCLK1 clock frequency */
  208. RCC_Clocks->PCLK1_Frequency = RCC_GetPCLK1ClockFreq(RCC_Clocks->HCLK_Frequency);
  209. }
  210. /**
  211. * @brief Return USARTx clock frequency
  212. * @param USARTxSource This parameter can be one of the following values:
  213. * @arg @ref LL_RCC_USART1_CLKSOURCE
  214. * @arg @ref LL_RCC_USART2_CLKSOURCE
  215. * @arg @ref LL_RCC_USART3_CLKSOURCE
  216. * @retval USART clock frequency (in Hz)
  217. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI or LSE) is not ready
  218. */
  219. uint32_t LL_RCC_GetUSARTClockFreq(uint32_t USARTxSource)
  220. {
  221. uint32_t usart_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  222. /* Check parameter */
  223. assert_param(IS_LL_RCC_USART_CLKSOURCE(USARTxSource));
  224. if (USARTxSource == LL_RCC_USART1_CLKSOURCE)
  225. {
  226. /* USART1CLK clock frequency */
  227. switch (LL_RCC_GetUSARTClockSource(USARTxSource))
  228. {
  229. case LL_RCC_USART1_CLKSOURCE_SYSCLK: /* USART1 Clock is System Clock */
  230. usart_frequency = RCC_GetSystemClockFreq();
  231. break;
  232. case LL_RCC_USART1_CLKSOURCE_HSI: /* USART1 Clock is HSI Osc. */
  233. if (LL_RCC_HSI_IsReady() == 1U)
  234. {
  235. usart_frequency = HSI_VALUE;
  236. }
  237. break;
  238. case LL_RCC_USART1_CLKSOURCE_LSE: /* USART1 Clock is LSE Osc. */
  239. if (LL_RCC_LSE_IsReady() == 1U)
  240. {
  241. usart_frequency = LSE_VALUE;
  242. }
  243. break;
  244. case LL_RCC_USART1_CLKSOURCE_PCLK1: /* USART1 Clock is PCLK1 */
  245. default:
  246. usart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  247. break;
  248. }
  249. }
  250. #if defined(RCC_CCIPR_USART2SEL)
  251. else if (USARTxSource == LL_RCC_USART2_CLKSOURCE)
  252. {
  253. /* USART2CLK clock frequency */
  254. switch (LL_RCC_GetUSARTClockSource(USARTxSource))
  255. {
  256. case LL_RCC_USART2_CLKSOURCE_SYSCLK: /* USART2 Clock is System Clock */
  257. usart_frequency = RCC_GetSystemClockFreq();
  258. break;
  259. case LL_RCC_USART2_CLKSOURCE_HSI: /* USART2 Clock is HSI Osc. */
  260. if (LL_RCC_HSI_IsReady() == 1U)
  261. {
  262. usart_frequency = HSI_VALUE;
  263. }
  264. break;
  265. case LL_RCC_USART2_CLKSOURCE_LSE: /* USART2 Clock is LSE Osc. */
  266. if (LL_RCC_LSE_IsReady() == 1U)
  267. {
  268. usart_frequency = LSE_VALUE;
  269. }
  270. break;
  271. case LL_RCC_USART2_CLKSOURCE_PCLK1: /* USART2 Clock is PCLK1 */
  272. default:
  273. usart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  274. break;
  275. }
  276. }
  277. #endif /* RCC_CCIPR_USART2SEL */
  278. #if defined(RCC_CCIPR_USART3SEL)
  279. else if (USARTxSource == LL_RCC_USART3_CLKSOURCE)
  280. {
  281. /* USART3CLK clock frequency */
  282. switch (LL_RCC_GetUSARTClockSource(USARTxSource))
  283. {
  284. case LL_RCC_USART3_CLKSOURCE_SYSCLK: /* USART3 Clock is System Clock */
  285. usart_frequency = RCC_GetSystemClockFreq();
  286. break;
  287. case LL_RCC_USART3_CLKSOURCE_HSI: /* USART3 Clock is HSI Osc. */
  288. if (LL_RCC_HSI_IsReady() == 1U)
  289. {
  290. usart_frequency = HSI_VALUE;
  291. }
  292. break;
  293. case LL_RCC_USART3_CLKSOURCE_LSE: /* USART3 Clock is LSE Osc. */
  294. if (LL_RCC_LSE_IsReady() == 1U)
  295. {
  296. usart_frequency = LSE_VALUE;
  297. }
  298. break;
  299. case LL_RCC_USART3_CLKSOURCE_PCLK1: /* USART3 Clock is PCLK1 */
  300. default:
  301. usart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  302. break;
  303. }
  304. }
  305. #endif /* RCC_CCIPR_USART3SEL */
  306. else
  307. {
  308. /* nothing to do */
  309. }
  310. return usart_frequency;
  311. }
  312. /**
  313. * @brief Return I2Cx clock frequency
  314. * @param I2CxSource This parameter can be one of the following values:
  315. * @arg @ref LL_RCC_I2C1_CLKSOURCE
  316. * @retval I2C clock frequency (in Hz)
  317. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that HSI oscillator is not ready
  318. */
  319. uint32_t LL_RCC_GetI2CClockFreq(uint32_t I2CxSource)
  320. {
  321. uint32_t i2c_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  322. /* Check parameter */
  323. assert_param(IS_LL_RCC_I2C_CLKSOURCE(I2CxSource));
  324. if (I2CxSource == LL_RCC_I2C1_CLKSOURCE)
  325. {
  326. /* I2C1 CLK clock frequency */
  327. switch (LL_RCC_GetI2CClockSource(I2CxSource))
  328. {
  329. case LL_RCC_I2C1_CLKSOURCE_SYSCLK: /* I2C1 Clock is System Clock */
  330. i2c_frequency = RCC_GetSystemClockFreq();
  331. break;
  332. case LL_RCC_I2C1_CLKSOURCE_HSI: /* I2C1 Clock is HSI Osc. */
  333. if (LL_RCC_HSI_IsReady() == 1U)
  334. {
  335. i2c_frequency = HSI_VALUE;
  336. }
  337. break;
  338. case LL_RCC_I2C1_CLKSOURCE_PCLK1: /* I2C1 Clock is PCLK1 */
  339. default:
  340. i2c_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  341. break;
  342. }
  343. }
  344. #if defined(RCC_CCIPR_I2C2SEL)
  345. else if (I2CxSource == LL_RCC_I2C2_CLKSOURCE)
  346. {
  347. /* I2C2 CLK clock frequency */
  348. switch (LL_RCC_GetI2CClockSource(I2CxSource))
  349. {
  350. case LL_RCC_I2C2_CLKSOURCE_SYSCLK: /* I2C2 Clock is System Clock */
  351. i2c_frequency = RCC_GetSystemClockFreq();
  352. break;
  353. case LL_RCC_I2C2_CLKSOURCE_HSI: /* I2C2 Clock is HSI Osc. */
  354. if (LL_RCC_HSI_IsReady() == 1U)
  355. {
  356. i2c_frequency = HSI_VALUE;
  357. }
  358. break;
  359. case LL_RCC_I2C2_CLKSOURCE_PCLK1: /* I2C2 Clock is PCLK1 */
  360. default:
  361. i2c_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  362. break;
  363. }
  364. }
  365. #endif /* RCC_CCIPR_I2C2SEL */
  366. else
  367. {
  368. /* nothing to do */
  369. }
  370. return i2c_frequency;
  371. }
  372. /**
  373. * @brief Return I2Sx clock frequency
  374. * @param I2SxSource This parameter can be one of the following values:
  375. * @arg @ref LL_RCC_I2S1_CLKSOURCE
  376. * @retval I2S clock frequency (in Hz)
  377. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator is not ready
  378. */
  379. uint32_t LL_RCC_GetI2SClockFreq(uint32_t I2SxSource)
  380. {
  381. uint32_t i2s_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  382. /* Check parameter */
  383. assert_param(IS_LL_RCC_I2S_CLKSOURCE(I2SxSource));
  384. if (I2SxSource == LL_RCC_I2S1_CLKSOURCE)
  385. {
  386. /* I2S1 CLK clock frequency */
  387. switch (LL_RCC_GetI2SClockSource(I2SxSource))
  388. {
  389. case LL_RCC_I2S1_CLKSOURCE_HSI: /* I2S1 Clock is HSI */
  390. i2s_frequency = HSI_VALUE;
  391. break;
  392. case LL_RCC_I2S1_CLKSOURCE_PLL: /* I2S1 Clock is PLL"P" */
  393. if (LL_RCC_PLL_IsReady() == 1U)
  394. {
  395. if (LL_RCC_PLL_IsEnabledDomain_I2S1() == 1U)
  396. {
  397. i2s_frequency = RCC_PLL_GetFreqDomain_I2S1();
  398. }
  399. }
  400. break;
  401. case LL_RCC_I2S1_CLKSOURCE_PIN: /* I2S1 Clock is External clock */
  402. i2s_frequency = EXTERNAL_CLOCK_VALUE;
  403. break;
  404. case LL_RCC_I2S1_CLKSOURCE_SYSCLK: /* I2S1 Clock is System Clock */
  405. default:
  406. i2s_frequency = RCC_GetSystemClockFreq();
  407. break;
  408. }
  409. }
  410. #if defined(RCC_CCIPR2_I2S2SEL)
  411. else if (I2SxSource == LL_RCC_I2S2_CLKSOURCE)
  412. {
  413. /* I2S2 CLK clock frequency */
  414. switch (LL_RCC_GetI2SClockSource(I2SxSource))
  415. {
  416. case LL_RCC_I2S2_CLKSOURCE_HSI: /* I2S2 Clock is HSI */
  417. i2s_frequency = HSI_VALUE;
  418. break;
  419. case LL_RCC_I2S2_CLKSOURCE_PLL: /* I2S2 Clock is PLL"P" */
  420. if (LL_RCC_PLL_IsReady() == 1U)
  421. {
  422. if (LL_RCC_PLL_IsEnabledDomain_I2S2() == 1U)
  423. {
  424. i2s_frequency = RCC_PLL_GetFreqDomain_I2S2();
  425. }
  426. }
  427. break;
  428. case LL_RCC_I2S2_CLKSOURCE_PIN: /* I2S2 Clock is External clock */
  429. i2s_frequency = EXTERNAL_CLOCK_VALUE;
  430. break;
  431. case LL_RCC_I2S2_CLKSOURCE_SYSCLK: /* I2S2 Clock is System Clock */
  432. default:
  433. i2s_frequency = RCC_GetSystemClockFreq();
  434. break;
  435. }
  436. }
  437. #endif /* RCC_CCIPR2_I2S2SEL */
  438. else
  439. {
  440. }
  441. return i2s_frequency;
  442. }
  443. #if defined(LPUART1) || defined(LPUART2)
  444. /**
  445. * @brief Return LPUARTx clock frequency
  446. * @param LPUARTxSource This parameter can be one of the following values:
  447. * @arg @ref LL_RCC_LPUART1_CLKSOURCE
  448. * @arg @ref LL_RCC_LPUART2_CLKSOURCE (*)
  449. * @retval LPUART clock frequency (in Hz)
  450. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI or LSE) is not ready
  451. * (*) feature not available on all devices
  452. */
  453. uint32_t LL_RCC_GetLPUARTClockFreq(uint32_t LPUARTxSource)
  454. {
  455. uint32_t lpuart_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  456. /* Check parameter */
  457. assert_param(IS_LL_RCC_LPUART_CLKSOURCE(LPUARTxSource));
  458. if (LPUARTxSource == LL_RCC_LPUART1_CLKSOURCE)
  459. {
  460. /* LPUART1CLK clock frequency */
  461. switch (LL_RCC_GetLPUARTClockSource(LPUARTxSource))
  462. {
  463. case LL_RCC_LPUART1_CLKSOURCE_SYSCLK: /* LPUART1 Clock is System Clock */
  464. lpuart_frequency = RCC_GetSystemClockFreq();
  465. break;
  466. case LL_RCC_LPUART1_CLKSOURCE_HSI: /* LPUART1 Clock is HSI Osc. */
  467. if (LL_RCC_HSI_IsReady() == 1U)
  468. {
  469. lpuart_frequency = HSI_VALUE;
  470. }
  471. break;
  472. case LL_RCC_LPUART1_CLKSOURCE_LSE: /* LPUART1 Clock is LSE Osc. */
  473. if (LL_RCC_LSE_IsReady() == 1U)
  474. {
  475. lpuart_frequency = LSE_VALUE;
  476. }
  477. break;
  478. case LL_RCC_LPUART1_CLKSOURCE_PCLK1: /* LPUART1 Clock is PCLK1 */
  479. default:
  480. lpuart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  481. break;
  482. }
  483. }
  484. #if defined(LPUART2)
  485. else if (LPUARTxSource == LL_RCC_LPUART2_CLKSOURCE)
  486. {
  487. /* LPUART2CLK clock frequency */
  488. switch (LL_RCC_GetLPUARTClockSource(LPUARTxSource))
  489. {
  490. case LL_RCC_LPUART2_CLKSOURCE_SYSCLK: /* LPUART2 Clock is System Clock */
  491. lpuart_frequency = RCC_GetSystemClockFreq();
  492. break;
  493. case LL_RCC_LPUART2_CLKSOURCE_HSI: /* LPUART2 Clock is HSI Osc. */
  494. if (LL_RCC_HSI_IsReady() == 1U)
  495. {
  496. lpuart_frequency = HSI_VALUE;
  497. }
  498. break;
  499. case LL_RCC_LPUART2_CLKSOURCE_LSE: /* LPUART2 Clock is LSE Osc. */
  500. if (LL_RCC_LSE_IsReady() == 1U)
  501. {
  502. lpuart_frequency = LSE_VALUE;
  503. }
  504. break;
  505. case LL_RCC_LPUART2_CLKSOURCE_PCLK1: /* LPUART2 Clock is PCLK1 */
  506. default:
  507. lpuart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  508. break;
  509. }
  510. }
  511. #endif /* LPUART2 */
  512. else
  513. {
  514. /*nothing to do*/
  515. }
  516. return lpuart_frequency;
  517. }
  518. #endif /* LPUART1 */
  519. #if defined(LPTIM1) && defined(LPTIM2)
  520. /**
  521. * @brief Return LPTIMx clock frequency
  522. * @param LPTIMxSource This parameter can be one of the following values:
  523. * @arg @ref LL_RCC_LPTIM1_CLKSOURCE
  524. * @arg @ref LL_RCC_LPTIM2_CLKSOURCE
  525. * @retval LPTIM clock frequency (in Hz)
  526. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI, LSI or LSE) is not ready
  527. */
  528. uint32_t LL_RCC_GetLPTIMClockFreq(uint32_t LPTIMxSource)
  529. {
  530. uint32_t lptim_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  531. /* Check parameter */
  532. assert_param(IS_LL_RCC_LPTIM_CLKSOURCE(LPTIMxSource));
  533. if (LPTIMxSource == LL_RCC_LPTIM1_CLKSOURCE)
  534. {
  535. /* LPTIM1CLK clock frequency */
  536. switch (LL_RCC_GetLPTIMClockSource(LPTIMxSource))
  537. {
  538. case LL_RCC_LPTIM1_CLKSOURCE_LSI: /* LPTIM1 Clock is LSI Osc. */
  539. if (LL_RCC_LSI_IsReady() == 1U)
  540. {
  541. lptim_frequency = LSI_VALUE;
  542. }
  543. break;
  544. case LL_RCC_LPTIM1_CLKSOURCE_HSI: /* LPTIM1 Clock is HSI Osc. */
  545. if (LL_RCC_HSI_IsReady() == 1U)
  546. {
  547. lptim_frequency = HSI_VALUE;
  548. }
  549. break;
  550. case LL_RCC_LPTIM1_CLKSOURCE_LSE: /* LPTIM1 Clock is LSE Osc. */
  551. if (LL_RCC_LSE_IsReady() == 1U)
  552. {
  553. lptim_frequency = LSE_VALUE;
  554. }
  555. break;
  556. case LL_RCC_LPTIM1_CLKSOURCE_PCLK1: /* LPTIM1 Clock is PCLK1 */
  557. default:
  558. lptim_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  559. break;
  560. }
  561. }
  562. else
  563. {
  564. /* LPTIM2CLK clock frequency */
  565. switch (LL_RCC_GetLPTIMClockSource(LPTIMxSource))
  566. {
  567. case LL_RCC_LPTIM2_CLKSOURCE_LSI: /* LPTIM2 Clock is LSI Osc. */
  568. if (LL_RCC_LSI_IsReady() == 1U)
  569. {
  570. lptim_frequency = LSI_VALUE;
  571. }
  572. break;
  573. case LL_RCC_LPTIM2_CLKSOURCE_HSI: /* LPTIM2 Clock is HSI Osc. */
  574. if (LL_RCC_HSI_IsReady() == 1U)
  575. {
  576. lptim_frequency = HSI_VALUE;
  577. }
  578. break;
  579. case LL_RCC_LPTIM2_CLKSOURCE_LSE: /* LPTIM2 Clock is LSE Osc. */
  580. if (LL_RCC_LSE_IsReady() == 1U)
  581. {
  582. lptim_frequency = LSE_VALUE;
  583. }
  584. break;
  585. case LL_RCC_LPTIM2_CLKSOURCE_PCLK1: /* LPTIM2 Clock is PCLK1 */
  586. default:
  587. lptim_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  588. break;
  589. }
  590. }
  591. return lptim_frequency;
  592. }
  593. #endif /* LPTIM1 && LPTIM2 */
  594. #if defined(RCC_CCIPR_TIM1SEL) || defined(RCC_CCIPR_TIM15SEL)
  595. /**
  596. * @brief Return TIMx clock frequency
  597. * @param TIMxSource This parameter can be one of the following values:
  598. * @arg @ref LL_RCC_TIM1_CLKSOURCE
  599. * @if defined(STM32G081xx)
  600. * @arg @ref LL_RCC_TIM15_CLKSOURCE
  601. * @endif
  602. * @retval TIMx clock frequency (in Hz)
  603. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI or LSE) is not ready
  604. */
  605. uint32_t LL_RCC_GetTIMClockFreq(uint32_t TIMxSource)
  606. {
  607. uint32_t tim_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  608. /* Check parameter */
  609. assert_param(IS_LL_RCC_TIM_CLKSOURCE(TIMxSource));
  610. if (TIMxSource == LL_RCC_TIM1_CLKSOURCE)
  611. {
  612. /* TIM1CLK clock frequency */
  613. switch (LL_RCC_GetTIMClockSource(TIMxSource))
  614. {
  615. case LL_RCC_TIM1_CLKSOURCE_PLL: /* TIM1 Clock is PLLQ */
  616. if (LL_RCC_PLL_IsReady() == 1U)
  617. {
  618. if (LL_RCC_PLL_IsEnabledDomain_TIM1() == 1U)
  619. {
  620. tim_frequency = RCC_PLL_GetFreqDomain_TIM1();
  621. }
  622. }
  623. break;
  624. case LL_RCC_TIM1_CLKSOURCE_PCLK1: /* TIM1 Clock is PCLK1 */
  625. default:
  626. tim_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  627. break;
  628. }
  629. }
  630. #if defined(TIM15)
  631. else
  632. {
  633. if (TIMxSource == LL_RCC_TIM15_CLKSOURCE)
  634. {
  635. /* TIM15CLK clock frequency */
  636. switch (LL_RCC_GetTIMClockSource(TIMxSource))
  637. {
  638. case LL_RCC_TIM15_CLKSOURCE_PLL: /* TIM1 Clock is PLLQ */
  639. if (LL_RCC_PLL_IsReady() == 1U)
  640. {
  641. if (LL_RCC_PLL_IsEnabledDomain_TIM15() == 1U)
  642. {
  643. tim_frequency = RCC_PLL_GetFreqDomain_TIM15();
  644. }
  645. }
  646. break;
  647. case LL_RCC_TIM15_CLKSOURCE_PCLK1: /* TIM15 Clock is PCLK1 */
  648. default:
  649. tim_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  650. break;
  651. }
  652. }
  653. }
  654. #endif /* TIM15 */
  655. return tim_frequency;
  656. }
  657. #endif /* RCC_CCIPR_TIM1SEL && RCC_CCIPR_TIM15SEL */
  658. #if defined(RNG)
  659. /**
  660. * @brief Return RNGx clock frequency
  661. * @param RNGxSource This parameter can be one of the following values:
  662. * @arg @ref LL_RCC_RNG_CLKSOURCE
  663. * @retval RNG clock frequency (in Hz)
  664. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI) or PLL is not ready
  665. * - @ref LL_RCC_PERIPH_FREQUENCY_NA indicates that no clock source selected
  666. */
  667. uint32_t LL_RCC_GetRNGClockFreq(uint32_t RNGxSource)
  668. {
  669. uint32_t rng_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  670. uint32_t rngdiv;
  671. /* Check parameter */
  672. assert_param(IS_LL_RCC_RNG_CLKSOURCE(RNGxSource));
  673. /* RNGCLK clock frequency */
  674. switch (LL_RCC_GetRNGClockSource(RNGxSource))
  675. {
  676. case LL_RCC_RNG_CLKSOURCE_PLL: /* PLL clock used as RNG clock source */
  677. if (LL_RCC_PLL_IsReady() == 1U)
  678. {
  679. if (LL_RCC_PLL_IsEnabledDomain_RNG() == 1U)
  680. {
  681. rng_frequency = RCC_PLL_GetFreqDomain_RNG();
  682. rngdiv = (1UL << ((READ_BIT(RCC->CCIPR, RCC_CCIPR_RNGDIV)) >> RCC_CCIPR_RNGDIV_Pos));
  683. rng_frequency = (rng_frequency / rngdiv);
  684. }
  685. }
  686. break;
  687. case LL_RCC_RNG_CLKSOURCE_HSI_DIV8: /* HSI clock divided by 8 used as RNG clock source */
  688. rng_frequency = HSI_VALUE / 8U;
  689. rngdiv = (1UL << ((READ_BIT(RCC->CCIPR, RCC_CCIPR_RNGDIV)) >> RCC_CCIPR_RNGDIV_Pos));
  690. rng_frequency = (rng_frequency / rngdiv);
  691. break;
  692. case LL_RCC_RNG_CLKSOURCE_SYSCLK: /* SYSCLK clock used as RNG clock source */
  693. rng_frequency = RCC_GetSystemClockFreq();
  694. rngdiv = (1UL << ((READ_BIT(RCC->CCIPR, RCC_CCIPR_RNGDIV)) >> RCC_CCIPR_RNGDIV_Pos));
  695. rng_frequency = (rng_frequency / rngdiv);
  696. break;
  697. case LL_RCC_RNG_CLKSOURCE_NONE: /* No clock used as RNG clock source */
  698. default:
  699. rng_frequency = LL_RCC_PERIPH_FREQUENCY_NA;
  700. break;
  701. }
  702. return rng_frequency;
  703. }
  704. #endif /* RNG */
  705. #if defined(CEC)
  706. /**
  707. * @brief Return CEC clock frequency
  708. * @param CECxSource This parameter can be one of the following values:
  709. * @arg @ref LL_RCC_CEC_CLKSOURCE
  710. * @retval CEC clock frequency (in Hz)
  711. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI or LSE) is not ready
  712. */
  713. uint32_t LL_RCC_GetCECClockFreq(uint32_t CECxSource)
  714. {
  715. uint32_t cec_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  716. /* Check parameter */
  717. assert_param(IS_LL_RCC_CEC_CLKSOURCE(CECxSource));
  718. /* CECCLK clock frequency */
  719. switch (LL_RCC_GetCECClockSource(CECxSource))
  720. {
  721. case LL_RCC_CEC_CLKSOURCE_LSE: /* CEC Clock is LSE Osc. */
  722. if (LL_RCC_LSE_IsReady() == 1U)
  723. {
  724. cec_frequency = LSE_VALUE;
  725. }
  726. break;
  727. case LL_RCC_CEC_CLKSOURCE_HSI_DIV488: /* CEC Clock is HSI Osc. */
  728. default:
  729. if (LL_RCC_HSI_IsReady() == 1U)
  730. {
  731. cec_frequency = (HSI_VALUE / 488U);
  732. }
  733. break;
  734. }
  735. return cec_frequency;
  736. }
  737. #endif /* CEC */
  738. #if defined(FDCAN1) || defined(FDCAN2)
  739. /**
  740. * @brief Return FDCANx clock frequency
  741. * @param FDCANxSource This parameter can be one of the following values:
  742. * @arg @ref LL_RCC_FDCAN_CLKSOURCE
  743. * @retval FDCANx clock frequency (in Hz)
  744. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI, LSI or LSE) is not ready
  745. */
  746. uint32_t LL_RCC_GetFDCANClockFreq(uint32_t FDCANxSource)
  747. {
  748. uint32_t fdcan_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  749. /* Check parameter */
  750. assert_param(IS_LL_RCC_FDCAN_CLKSOURCE(FDCANxSource));
  751. /* FDCANCLK clock frequency */
  752. switch (LL_RCC_GetFDCANClockSource(FDCANxSource))
  753. {
  754. case LL_RCC_FDCAN_CLKSOURCE_PLL: /* FDCAN Clock is PLL "Q" Osc. */
  755. if (LL_RCC_PLL_IsReady() == 1U)
  756. {
  757. if (LL_RCC_PLL_IsEnabledDomain_FDCAN() == 1U)
  758. {
  759. fdcan_frequency = RCC_PLL_GetFreqDomain_FDCAN();
  760. }
  761. }
  762. break;
  763. case LL_RCC_FDCAN_CLKSOURCE_HSE: /* FDCAN Clock is HSE Osc. */
  764. if (LL_RCC_HSE_IsReady() == 1U)
  765. {
  766. fdcan_frequency = HSE_VALUE;
  767. }
  768. break;
  769. case LL_RCC_FDCAN_CLKSOURCE_PCLK1: /* FDCAN Clock is PCLK1 */
  770. default:
  771. fdcan_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  772. break;
  773. }
  774. return fdcan_frequency;
  775. }
  776. #endif /* FDCAN1 || FDCAN2 */
  777. /**
  778. * @brief Return ADCx clock frequency
  779. * @param ADCxSource This parameter can be one of the following values:
  780. * @arg @ref LL_RCC_ADC_CLKSOURCE
  781. * @retval ADC clock frequency (in Hz)
  782. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI) or PLL is not ready
  783. * - @ref LL_RCC_PERIPH_FREQUENCY_NA indicates that no clock source selected
  784. */
  785. uint32_t LL_RCC_GetADCClockFreq(uint32_t ADCxSource)
  786. {
  787. uint32_t adc_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  788. /* Check parameter */
  789. assert_param(IS_LL_RCC_ADC_CLKSOURCE(ADCxSource));
  790. /* ADCCLK clock frequency */
  791. switch (LL_RCC_GetADCClockSource(ADCxSource))
  792. {
  793. case LL_RCC_ADC_CLKSOURCE_SYSCLK: /* SYSCLK clock used as ADC clock source */
  794. adc_frequency = RCC_GetSystemClockFreq();
  795. break;
  796. case LL_RCC_ADC_CLKSOURCE_HSI : /* HSI clock used as ADC clock source */
  797. adc_frequency = HSI_VALUE;
  798. break;
  799. case LL_RCC_ADC_CLKSOURCE_PLL: /* PLLP clock used as ADC clock source */
  800. if (LL_RCC_PLL_IsReady() == 1U)
  801. {
  802. if (LL_RCC_PLL_IsEnabledDomain_ADC() == 1U)
  803. {
  804. adc_frequency = RCC_PLL_GetFreqDomain_ADC();
  805. }
  806. }
  807. break;
  808. default:
  809. adc_frequency = LL_RCC_PERIPH_FREQUENCY_NA;
  810. break;
  811. }
  812. return adc_frequency;
  813. }
  814. /**
  815. * @brief Return RTC clock frequency
  816. * @retval RTC clock frequency (in Hz)
  817. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillators (LSI, LSE or HSE) are not ready
  818. * - @ref LL_RCC_PERIPH_FREQUENCY_NA indicates that no clock source selected
  819. */
  820. uint32_t LL_RCC_GetRTCClockFreq(void)
  821. {
  822. uint32_t rtc_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  823. /* RTCCLK clock frequency */
  824. switch (LL_RCC_GetRTCClockSource())
  825. {
  826. case LL_RCC_RTC_CLKSOURCE_LSE: /* LSE clock used as RTC clock source */
  827. if (LL_RCC_LSE_IsReady() == 1U)
  828. {
  829. rtc_frequency = LSE_VALUE;
  830. }
  831. break;
  832. case LL_RCC_RTC_CLKSOURCE_LSI: /* LSI clock used as RTC clock source */
  833. if (LL_RCC_LSI_IsReady() == 1U)
  834. {
  835. rtc_frequency = LSI_VALUE;
  836. }
  837. break;
  838. case LL_RCC_RTC_CLKSOURCE_HSE_DIV32: /* HSE clock used as ADC clock source */
  839. rtc_frequency = HSE_VALUE / 32U;
  840. break;
  841. case LL_RCC_RTC_CLKSOURCE_NONE: /* No clock used as RTC clock source */
  842. default:
  843. rtc_frequency = LL_RCC_PERIPH_FREQUENCY_NA;
  844. break;
  845. }
  846. return rtc_frequency;
  847. }
  848. #if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
  849. /**
  850. * @brief Return USBx clock frequency
  851. * @param USBxSource This parameter can be one of the following values:
  852. * @arg @ref LL_RCC_USB_CLKSOURCE
  853. * @retval USB clock frequency (in Hz)
  854. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI48) or PLL is not ready
  855. * - @ref LL_RCC_PERIPH_FREQUENCY_NA indicates that no clock source selected
  856. */
  857. uint32_t LL_RCC_GetUSBClockFreq(uint32_t USBxSource)
  858. {
  859. uint32_t usb_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  860. /* Check parameter */
  861. assert_param(IS_LL_RCC_USB_CLKSOURCE(USBxSource));
  862. /* USBCLK clock frequency */
  863. switch (LL_RCC_GetUSBClockSource(USBxSource))
  864. {
  865. #if defined(RCC_HSI48_SUPPORT)
  866. case LL_RCC_USB_CLKSOURCE_HSI48: /* HSI48 used as USB clock source */
  867. if (LL_RCC_HSI48_IsReady() != 0U)
  868. {
  869. usb_frequency = HSI48_VALUE;
  870. }
  871. break;
  872. #endif /* RCC_HSI48_SUPPORT */
  873. case LL_RCC_USB_CLKSOURCE_HSE: /* HSE used as USB clock source */
  874. if (LL_RCC_HSE_IsReady() != 0U)
  875. {
  876. usb_frequency = HSE_VALUE;
  877. }
  878. break;
  879. case LL_RCC_USB_CLKSOURCE_PLL: /* PLL clock used as USB clock source */
  880. if (LL_RCC_PLL_IsReady() != 0U)
  881. {
  882. if (LL_RCC_PLL_IsEnabledDomain_USB() != 0U)
  883. {
  884. usb_frequency = RCC_PLL_GetFreqDomain_USB();
  885. }
  886. }
  887. break;
  888. default:
  889. usb_frequency = LL_RCC_PERIPH_FREQUENCY_NA;
  890. break;
  891. }
  892. return usb_frequency;
  893. }
  894. #endif /* STM32G0C1xx || STM32G0B1xx || STM32G0B0xx) */
  895. /**
  896. * @}
  897. */
  898. /**
  899. * @}
  900. */
  901. /** @addtogroup RCC_LL_Private_Functions
  902. * @{
  903. */
  904. /**
  905. * @brief Return SYSTEM clock frequency
  906. * @retval SYSTEM clock frequency (in Hz)
  907. */
  908. static uint32_t RCC_GetSystemClockFreq(void)
  909. {
  910. uint32_t frequency;
  911. uint32_t hsidiv;
  912. /* Get SYSCLK source -------------------------------------------------------*/
  913. switch (LL_RCC_GetSysClkSource())
  914. {
  915. case LL_RCC_SYS_CLKSOURCE_STATUS_HSE: /* HSE used as system clock source */
  916. frequency = HSE_VALUE;
  917. break;
  918. case LL_RCC_SYS_CLKSOURCE_STATUS_PLL: /* PLL used as system clock source */
  919. frequency = RCC_PLL_GetFreqDomain_SYS();
  920. break;
  921. case LL_RCC_SYS_CLKSOURCE_STATUS_HSI: /* HSI used as system clock source */
  922. default:
  923. hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos));
  924. frequency = (HSI_VALUE / hsidiv);
  925. break;
  926. }
  927. return frequency;
  928. }
  929. /**
  930. * @brief Return HCLK clock frequency
  931. * @param SYSCLK_Frequency SYSCLK clock frequency
  932. * @retval HCLK clock frequency (in Hz)
  933. */
  934. static uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency)
  935. {
  936. /* HCLK clock frequency */
  937. return __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, LL_RCC_GetAHBPrescaler());
  938. }
  939. /**
  940. * @brief Return PCLK1 clock frequency
  941. * @param HCLK_Frequency HCLK clock frequency
  942. * @retval PCLK1 clock frequency (in Hz)
  943. */
  944. static uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency)
  945. {
  946. /* PCLK1 clock frequency */
  947. return __LL_RCC_CALC_PCLK1_FREQ(HCLK_Frequency, LL_RCC_GetAPB1Prescaler());
  948. }
  949. /**
  950. * @brief Return PLL clock frequency used for system domain
  951. * @retval PLL clock frequency (in Hz)
  952. */
  953. static uint32_t RCC_PLL_GetFreqDomain_SYS(void)
  954. {
  955. uint32_t pllinputfreq;
  956. uint32_t pllsource;
  957. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  958. SYSCLK = PLL_VCO / PLLR
  959. */
  960. pllsource = LL_RCC_PLL_GetMainSource();
  961. switch (pllsource)
  962. {
  963. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  964. pllinputfreq = HSI_VALUE;
  965. break;
  966. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  967. pllinputfreq = HSE_VALUE;
  968. break;
  969. default:
  970. pllinputfreq = HSI_VALUE;
  971. break;
  972. }
  973. return __LL_RCC_CALC_PLLCLK_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  974. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetR());
  975. }
  976. /**
  977. * @brief Return PLL clock frequency used for ADC domain
  978. * @retval PLL clock frequency (in Hz)
  979. */
  980. static uint32_t RCC_PLL_GetFreqDomain_ADC(void)
  981. {
  982. uint32_t pllinputfreq;
  983. uint32_t pllsource;
  984. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  985. ADC Domain clock = PLL_VCO / PLLP
  986. */
  987. pllsource = LL_RCC_PLL_GetMainSource();
  988. switch (pllsource)
  989. {
  990. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  991. pllinputfreq = HSE_VALUE;
  992. break;
  993. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  994. default:
  995. pllinputfreq = HSI_VALUE;
  996. break;
  997. }
  998. return __LL_RCC_CALC_PLLCLK_ADC_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  999. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetP());
  1000. }
  1001. #if defined(FDCAN1) || defined(FDCAN2)
  1002. /**
  1003. * @brief Return PLL clock frequency used for FDCAN domain
  1004. * @retval PLL clock frequency (in Hz)
  1005. */
  1006. static uint32_t RCC_PLL_GetFreqDomain_FDCAN(void)
  1007. {
  1008. uint32_t pllinputfreq;
  1009. uint32_t pllsource;
  1010. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM ) * PLLN
  1011. FDCAN Domain clock = PLL_VCO / PLLQ
  1012. */
  1013. pllsource = LL_RCC_PLL_GetMainSource();
  1014. switch (pllsource)
  1015. {
  1016. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1017. pllinputfreq = HSE_VALUE;
  1018. break;
  1019. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1020. default:
  1021. pllinputfreq = HSI_VALUE;
  1022. break;
  1023. }
  1024. return __LL_RCC_CALC_PLLCLK_FDCAN_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1025. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetQ());
  1026. }
  1027. #endif /* FDCAN1 || FDCAN2 */
  1028. /**
  1029. * @brief Return PLL clock frequency used for I2S1 domain
  1030. * @retval PLL clock frequency (in Hz)
  1031. */
  1032. static uint32_t RCC_PLL_GetFreqDomain_I2S1(void)
  1033. {
  1034. uint32_t pllinputfreq;
  1035. uint32_t pllsource;
  1036. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  1037. I2S1 Domain clock = PLL_VCO / PLLP
  1038. */
  1039. pllsource = LL_RCC_PLL_GetMainSource();
  1040. switch (pllsource)
  1041. {
  1042. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1043. pllinputfreq = HSE_VALUE;
  1044. break;
  1045. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1046. default:
  1047. pllinputfreq = HSI_VALUE;
  1048. break;
  1049. }
  1050. return __LL_RCC_CALC_PLLCLK_I2S1_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1051. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetP());
  1052. }
  1053. #if defined(RCC_CCIPR2_I2S2SEL)
  1054. /**
  1055. * @brief Return PLL clock frequency used for I2S2 domain
  1056. * @retval PLL clock frequency (in Hz)
  1057. */
  1058. static uint32_t RCC_PLL_GetFreqDomain_I2S2(void)
  1059. {
  1060. uint32_t pllinputfreq;
  1061. uint32_t pllsource;
  1062. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  1063. I2S2 Domain clock = PLL_VCO / PLLP
  1064. */
  1065. pllsource = LL_RCC_PLL_GetMainSource();
  1066. switch (pllsource)
  1067. {
  1068. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1069. pllinputfreq = HSE_VALUE;
  1070. break;
  1071. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1072. default:
  1073. pllinputfreq = HSI_VALUE;
  1074. break;
  1075. }
  1076. return __LL_RCC_CALC_PLLCLK_I2S2_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1077. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetP());
  1078. }
  1079. #endif /* RCC_CCIPR2_I2S2SEL */
  1080. #if defined(RNG)
  1081. /**
  1082. * @brief Return PLL clock frequency used for RNG domain
  1083. * @retval PLL clock frequency (in Hz)
  1084. */
  1085. static uint32_t RCC_PLL_GetFreqDomain_RNG(void)
  1086. {
  1087. uint32_t pllinputfreq;
  1088. uint32_t pllsource;
  1089. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM ) * PLLN
  1090. RNG Domain clock = PLL_VCO / PLLQ
  1091. */
  1092. pllsource = LL_RCC_PLL_GetMainSource();
  1093. switch (pllsource)
  1094. {
  1095. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1096. pllinputfreq = HSE_VALUE;
  1097. break;
  1098. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1099. default:
  1100. pllinputfreq = HSI_VALUE;
  1101. break;
  1102. }
  1103. return __LL_RCC_CALC_PLLCLK_RNG_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1104. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetQ());
  1105. }
  1106. #endif /* RNG */
  1107. #if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
  1108. /**
  1109. * @brief Return PLL clock frequency used for USB domain
  1110. * @retval PLL clock frequency (in Hz)
  1111. */
  1112. static uint32_t RCC_PLL_GetFreqDomain_USB(void)
  1113. {
  1114. uint32_t pllinputfreq;
  1115. uint32_t pllsource;
  1116. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM ) * PLLN
  1117. RNG Domain clock = PLL_VCO / PLLQ
  1118. */
  1119. pllsource = LL_RCC_PLL_GetMainSource();
  1120. switch (pllsource)
  1121. {
  1122. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1123. pllinputfreq = HSE_VALUE;
  1124. break;
  1125. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1126. default:
  1127. pllinputfreq = HSI_VALUE;
  1128. break;
  1129. }
  1130. return __LL_RCC_CALC_PLLCLK_USB_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1131. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetQ());
  1132. }
  1133. #endif /* STM32G0C1xx || STM32G0B1xx || STM32G0B0xx */
  1134. #if defined(RCC_PLLQ_SUPPORT) && defined(RCC_CCIPR_TIM1SEL)
  1135. /**
  1136. * @brief Return PLL clock frequency used for TIM1 domain
  1137. * @retval PLL clock frequency (in Hz)
  1138. */
  1139. static uint32_t RCC_PLL_GetFreqDomain_TIM1(void)
  1140. {
  1141. uint32_t pllinputfreq;
  1142. uint32_t pllsource;
  1143. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM ) * PLLN
  1144. TIM1 Domain clock = PLL_VCO / PLLQ
  1145. */
  1146. pllsource = LL_RCC_PLL_GetMainSource();
  1147. switch (pllsource)
  1148. {
  1149. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1150. pllinputfreq = HSE_VALUE;
  1151. break;
  1152. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1153. default:
  1154. pllinputfreq = HSI_VALUE;
  1155. break;
  1156. }
  1157. return __LL_RCC_CALC_PLLCLK_TIM1_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1158. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetQ());
  1159. }
  1160. #endif /* RCC_PLLQ_SUPPORT */
  1161. #if defined(RCC_CCIPR_TIM15SEL)
  1162. /**
  1163. * @brief Return PLL clock frequency used for TIM15 domain
  1164. * @retval PLL clock frequency (in Hz)
  1165. */
  1166. static uint32_t RCC_PLL_GetFreqDomain_TIM15(void)
  1167. {
  1168. uint32_t pllinputfreq;
  1169. uint32_t pllsource;
  1170. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM ) * PLLN
  1171. TIM15 Domain clock = PLL_VCO / PLLQ
  1172. */
  1173. pllsource = LL_RCC_PLL_GetMainSource();
  1174. switch (pllsource)
  1175. {
  1176. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1177. pllinputfreq = HSE_VALUE;
  1178. break;
  1179. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1180. default:
  1181. pllinputfreq = HSI_VALUE;
  1182. break;
  1183. }
  1184. return __LL_RCC_CALC_PLLCLK_TIM15_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1185. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetQ());
  1186. }
  1187. #endif /* RCC_CCIPR_TIM15SEL */
  1188. /**
  1189. * @}
  1190. */
  1191. /**
  1192. * @}
  1193. */
  1194. #endif /* RCC */
  1195. /**
  1196. * @}
  1197. */
  1198. #endif /* USE_FULL_LL_DRIVER */