stm32g0xx_ll_rcc.c 41 KB

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