stm32f3xx_ll_rcc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_ll_rcc.c
  4. * @author MCD Application Team
  5. * @brief RCC LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. #if defined(USE_FULL_LL_DRIVER)
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32f3xx_ll_rcc.h"
  38. #ifdef USE_FULL_ASSERT
  39. #include "stm32_assert.h"
  40. #else
  41. #define assert_param(expr) ((void)0U)
  42. #endif /* USE_FULL_ASSERT */
  43. /** @addtogroup STM32F3xx_LL_Driver
  44. * @{
  45. */
  46. #if defined(RCC)
  47. /** @defgroup RCC_LL RCC
  48. * @{
  49. */
  50. /* Private types -------------------------------------------------------------*/
  51. /* Private variables ---------------------------------------------------------*/
  52. /** @addtogroup RCC_LL_Private_Variables
  53. * @{
  54. */
  55. #if defined(RCC_CFGR2_ADC1PRES) || defined(RCC_CFGR2_ADCPRE12) || defined(RCC_CFGR2_ADCPRE34)
  56. const uint16_t aADCPrescTable[16] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U, 256U, 256U, 256U, 256U};
  57. #endif /* RCC_CFGR2_ADC1PRES || RCC_CFGR2_ADCPRE12 || RCC_CFGR2_ADCPRE34 */
  58. #if defined(RCC_CFGR_SDPRE)
  59. const uint8_t aSDADCPrescTable[16] = {2U, 4U, 6U, 8U, 10U, 12U, 14U, 16U, 20U, 24U, 28U, 32U, 36U, 40U, 44U, 48U};
  60. #endif /* RCC_CFGR_SDPRE */
  61. /**
  62. * @}
  63. */
  64. /* Private constants ---------------------------------------------------------*/
  65. /* Private macros ------------------------------------------------------------*/
  66. /** @addtogroup RCC_LL_Private_Macros
  67. * @{
  68. */
  69. #if defined(RCC_CFGR3_USART2SW) && defined(RCC_CFGR3_USART3SW)
  70. #define IS_LL_RCC_USART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USART1_CLKSOURCE) \
  71. || ((__VALUE__) == LL_RCC_USART2_CLKSOURCE) \
  72. || ((__VALUE__) == LL_RCC_USART3_CLKSOURCE))
  73. #elif defined(RCC_CFGR3_USART2SW) && !defined(RCC_CFGR3_USART3SW)
  74. #define IS_LL_RCC_USART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USART1_CLKSOURCE) \
  75. || ((__VALUE__) == LL_RCC_USART2_CLKSOURCE))
  76. #elif defined(RCC_CFGR3_USART3SW) && !defined(RCC_CFGR3_USART2SW)
  77. #define IS_LL_RCC_USART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USART1_CLKSOURCE) \
  78. || ((__VALUE__) == LL_RCC_USART3_CLKSOURCE))
  79. #else
  80. #define IS_LL_RCC_USART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USART1_CLKSOURCE))
  81. #endif /* RCC_CFGR3_USART2SW && RCC_CFGR3_USART3SW */
  82. #if defined(UART4) && defined(UART5)
  83. #define IS_LL_RCC_UART_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_UART4_CLKSOURCE) \
  84. || ((__VALUE__) == LL_RCC_UART5_CLKSOURCE))
  85. #elif defined(UART4)
  86. #define IS_LL_RCC_UART_INSTANCE(__VALUE__) ((__VALUE__) == LL_RCC_UART4_CLKSOURCE)
  87. #elif defined(UART5)
  88. #define IS_LL_RCC_UART_INSTANCE(__VALUE__) ((__VALUE__) == LL_RCC_UART5_CLKSOURCE)
  89. #endif /* UART4 && UART5*/
  90. #if defined(RCC_CFGR3_I2C2SW) && defined(RCC_CFGR3_I2C3SW)
  91. #define IS_LL_RCC_I2C_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_I2C1_CLKSOURCE) \
  92. || ((__VALUE__) == LL_RCC_I2C2_CLKSOURCE) \
  93. || ((__VALUE__) == LL_RCC_I2C3_CLKSOURCE))
  94. #elif defined(RCC_CFGR3_I2C2SW) && !defined(RCC_CFGR3_I2C3SW)
  95. #define IS_LL_RCC_I2C_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_I2C1_CLKSOURCE) \
  96. || ((__VALUE__) == LL_RCC_I2C2_CLKSOURCE))
  97. #elif defined(RCC_CFGR3_I2C3SW) && !defined(RCC_CFGR3_I2C2SW)
  98. #define IS_LL_RCC_I2C_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_I2C1_CLKSOURCE) \
  99. || ((__VALUE__) == LL_RCC_I2C3_CLKSOURCE))
  100. #else
  101. #define IS_LL_RCC_I2C_CLKSOURCE(__VALUE__) ((__VALUE__) == LL_RCC_I2C1_CLKSOURCE)
  102. #endif /* RCC_CFGR3_I2C2SW && RCC_CFGR3_I2C3SW */
  103. #define IS_LL_RCC_I2S_CLKSOURCE(__VALUE__) ((__VALUE__) == LL_RCC_I2S_CLKSOURCE)
  104. #if defined(USB)
  105. #define IS_LL_RCC_USB_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_USB_CLKSOURCE))
  106. #endif /* USB */
  107. #if defined(RCC_CFGR_ADCPRE)
  108. #define IS_LL_RCC_ADC_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_ADC_CLKSOURCE))
  109. #else
  110. #if defined(RCC_CFGR2_ADC1PRES)
  111. #define IS_LL_RCC_ADC_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_ADC1_CLKSOURCE))
  112. #elif defined(RCC_CFGR2_ADCPRE12) && defined(RCC_CFGR2_ADCPRE34)
  113. #define IS_LL_RCC_ADC_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_ADC12_CLKSOURCE) \
  114. || ((__VALUE__) == LL_RCC_ADC34_CLKSOURCE))
  115. #else /* RCC_CFGR2_ADCPRE12 */
  116. #define IS_LL_RCC_ADC_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_ADC12_CLKSOURCE))
  117. #endif /* RCC_CFGR2_ADC1PRES */
  118. #endif /* RCC_CFGR_ADCPRE */
  119. #if defined(RCC_CFGR_SDPRE)
  120. #define IS_LL_RCC_SDADC_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_SDADC_CLKSOURCE))
  121. #endif /* RCC_CFGR_SDPRE */
  122. #if defined(CEC)
  123. #define IS_LL_RCC_CEC_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_CEC_CLKSOURCE))
  124. #endif /* CEC */
  125. #if defined(RCC_CFGR3_TIMSW)
  126. #if defined(RCC_CFGR3_TIM8SW) && defined(RCC_CFGR3_TIM15SW) && defined(RCC_CFGR3_TIM16SW) \
  127. && defined(RCC_CFGR3_TIM17SW) && defined(RCC_CFGR3_TIM20SW) && defined(RCC_CFGR3_TIM2SW) \
  128. && defined(RCC_CFGR3_TIM34SW)
  129. #define IS_LL_RCC_TIM_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_TIM1_CLKSOURCE) \
  130. || ((__VALUE__) == LL_RCC_TIM2_CLKSOURCE) \
  131. || ((__VALUE__) == LL_RCC_TIM8_CLKSOURCE) \
  132. || ((__VALUE__) == LL_RCC_TIM15_CLKSOURCE) \
  133. || ((__VALUE__) == LL_RCC_TIM16_CLKSOURCE) \
  134. || ((__VALUE__) == LL_RCC_TIM17_CLKSOURCE) \
  135. || ((__VALUE__) == LL_RCC_TIM20_CLKSOURCE) \
  136. || ((__VALUE__) == LL_RCC_TIM34_CLKSOURCE))
  137. #elif !defined(RCC_CFGR3_TIM8SW) && defined(RCC_CFGR3_TIM15SW) && defined(RCC_CFGR3_TIM16SW) \
  138. && defined(RCC_CFGR3_TIM17SW) && !defined(RCC_CFGR3_TIM20SW) && defined(RCC_CFGR3_TIM2SW) \
  139. && defined(RCC_CFGR3_TIM34SW)
  140. #define IS_LL_RCC_TIM_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_TIM1_CLKSOURCE) \
  141. || ((__VALUE__) == LL_RCC_TIM2_CLKSOURCE) \
  142. || ((__VALUE__) == LL_RCC_TIM15_CLKSOURCE) \
  143. || ((__VALUE__) == LL_RCC_TIM16_CLKSOURCE) \
  144. || ((__VALUE__) == LL_RCC_TIM17_CLKSOURCE) \
  145. || ((__VALUE__) == LL_RCC_TIM34_CLKSOURCE))
  146. #elif defined(RCC_CFGR3_TIM8SW) && !defined(RCC_CFGR3_TIM15SW) && !defined(RCC_CFGR3_TIM16SW) \
  147. && !defined(RCC_CFGR3_TIM17SW) && !defined(RCC_CFGR3_TIM20SW) && !defined(RCC_CFGR3_TIM2SW) \
  148. && !defined(RCC_CFGR3_TIM34SW)
  149. #define IS_LL_RCC_TIM_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_TIM1_CLKSOURCE) \
  150. || ((__VALUE__) == LL_RCC_TIM8_CLKSOURCE))
  151. #elif !defined(RCC_CFGR3_TIM8SW) && defined(RCC_CFGR3_TIM15SW) && defined(RCC_CFGR3_TIM16SW) \
  152. && defined(RCC_CFGR3_TIM17SW) && !defined(RCC_CFGR3_TIM20SW) && !defined(RCC_CFGR3_TIM2SW) \
  153. && !defined(RCC_CFGR3_TIM34SW)
  154. #define IS_LL_RCC_TIM_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_TIM1_CLKSOURCE) \
  155. || ((__VALUE__) == LL_RCC_TIM15_CLKSOURCE) \
  156. || ((__VALUE__) == LL_RCC_TIM16_CLKSOURCE) \
  157. || ((__VALUE__) == LL_RCC_TIM17_CLKSOURCE))
  158. #elif !defined(RCC_CFGR3_TIM8SW) && !defined(RCC_CFGR3_TIM15SW) && !defined(RCC_CFGR3_TIM16SW) \
  159. && !defined(RCC_CFGR3_TIM17SW) && !defined(RCC_CFGR3_TIM20SW) && !defined(RCC_CFGR3_TIM2SW) \
  160. && !defined(RCC_CFGR3_TIM34SW)
  161. #define IS_LL_RCC_TIM_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_TIM1_CLKSOURCE))
  162. #else
  163. #error "Miss macro"
  164. #endif /* RCC_CFGR3_TIMxSW */
  165. #endif /* RCC_CFGR3_TIMSW */
  166. #if defined(HRTIM1)
  167. #define IS_LL_RCC_HRTIM_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_HRTIM1_CLKSOURCE))
  168. #endif /* HRTIM1 */
  169. /**
  170. * @}
  171. */
  172. /* Private function prototypes -----------------------------------------------*/
  173. /** @defgroup RCC_LL_Private_Functions RCC Private functions
  174. * @{
  175. */
  176. uint32_t RCC_GetSystemClockFreq(void);
  177. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency);
  178. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency);
  179. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency);
  180. uint32_t RCC_PLL_GetFreqDomain_SYS(void);
  181. /**
  182. * @}
  183. */
  184. /* Exported functions --------------------------------------------------------*/
  185. /** @addtogroup RCC_LL_Exported_Functions
  186. * @{
  187. */
  188. /** @addtogroup RCC_LL_EF_Init
  189. * @{
  190. */
  191. /**
  192. * @brief Reset the RCC clock configuration to the default reset state.
  193. * @note The default reset state of the clock configuration is given below:
  194. * - HSI ON and used as system clock source
  195. * - HSE and PLL OFF
  196. * - AHB, APB1 and APB2 prescaler set to 1.
  197. * - CSS, MCO OFF
  198. * - All interrupts disabled
  199. * @note This function doesn't modify the configuration of the
  200. * - Peripheral clocks
  201. * - LSI, LSE and RTC clocks
  202. * @retval An ErrorStatus enumeration value:
  203. * - SUCCESS: RCC registers are de-initialized
  204. * - ERROR: not applicable
  205. */
  206. ErrorStatus LL_RCC_DeInit(void)
  207. {
  208. uint32_t vl_mask = 0U;
  209. /* Set HSION bit */
  210. LL_RCC_HSI_Enable();
  211. /* Set HSITRIM bits to the reset value*/
  212. LL_RCC_HSI_SetCalibTrimming(0x10U);
  213. /* Reset SW, HPRE, PPRE and MCOSEL bits */
  214. vl_mask = 0xFFFFFFFFU;
  215. CLEAR_BIT(vl_mask, (RCC_CFGR_SW | RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2 | RCC_CFGR_MCOSEL));
  216. LL_RCC_WriteReg(CFGR, vl_mask);
  217. /* Reset HSEON, CSSON, PLLON bits */
  218. vl_mask = 0xFFFFFFFFU;
  219. CLEAR_BIT(vl_mask, (RCC_CR_PLLON | RCC_CR_CSSON | RCC_CR_HSEON));
  220. LL_RCC_WriteReg(CR, vl_mask);
  221. /* Reset HSEBYP bit */
  222. LL_RCC_HSE_DisableBypass();
  223. /* Reset CFGR register */
  224. LL_RCC_WriteReg(CFGR, 0x00000000U);
  225. /* Reset CFGR2 register */
  226. LL_RCC_WriteReg(CFGR2, 0x00000000U);
  227. /* Reset CFGR3 register */
  228. LL_RCC_WriteReg(CFGR3, 0x00000000U);
  229. /* Clear pending flags */
  230. vl_mask = (LL_RCC_CIR_LSIRDYC | LL_RCC_CIR_LSERDYC | LL_RCC_CIR_HSIRDYC | LL_RCC_CIR_HSERDYC | LL_RCC_CIR_PLLRDYC | LL_RCC_CIR_CSSC);
  231. SET_BIT(RCC->CIR, vl_mask);
  232. /* Disable all interrupts */
  233. LL_RCC_WriteReg(CIR, 0x00000000U);
  234. return SUCCESS;
  235. }
  236. /**
  237. * @}
  238. */
  239. /** @addtogroup RCC_LL_EF_Get_Freq
  240. * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks
  241. * and different peripheral clocks available on the device.
  242. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
  243. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  244. * @note If SYSCLK source is PLL, function returns values based on
  245. * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
  246. * @note (**) HSI_VALUE is a defined constant but the real value may vary
  247. * depending on the variations in voltage and temperature.
  248. * @note (***) HSE_VALUE is a defined constant, user has to ensure that
  249. * HSE_VALUE is same as the real frequency of the crystal used.
  250. * Otherwise, this function may have wrong result.
  251. * @note The result of this function could be incorrect when using fractional
  252. * value for HSE crystal.
  253. * @note This function can be used by the user application to compute the
  254. * baud-rate for the communication peripherals or configure other parameters.
  255. * @{
  256. */
  257. /**
  258. * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks
  259. * @note Each time SYSCLK, HCLK, PCLK1 and/or PCLK2 clock changes, this function
  260. * must be called to update structure fields. Otherwise, any
  261. * configuration based on this function will be incorrect.
  262. * @param RCC_Clocks pointer to a @ref LL_RCC_ClocksTypeDef structure which will hold the clocks frequencies
  263. * @retval None
  264. */
  265. void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks)
  266. {
  267. /* Get SYSCLK frequency */
  268. RCC_Clocks->SYSCLK_Frequency = RCC_GetSystemClockFreq();
  269. /* HCLK clock frequency */
  270. RCC_Clocks->HCLK_Frequency = RCC_GetHCLKClockFreq(RCC_Clocks->SYSCLK_Frequency);
  271. /* PCLK1 clock frequency */
  272. RCC_Clocks->PCLK1_Frequency = RCC_GetPCLK1ClockFreq(RCC_Clocks->HCLK_Frequency);
  273. /* PCLK2 clock frequency */
  274. RCC_Clocks->PCLK2_Frequency = RCC_GetPCLK2ClockFreq(RCC_Clocks->HCLK_Frequency);
  275. }
  276. /**
  277. * @brief Return USARTx clock frequency
  278. * @param USARTxSource This parameter can be one of the following values:
  279. * @arg @ref LL_RCC_USART1_CLKSOURCE
  280. * @arg @ref LL_RCC_USART2_CLKSOURCE (*)
  281. * @arg @ref LL_RCC_USART3_CLKSOURCE (*)
  282. *
  283. * (*) value not defined in all devices.
  284. * @retval USART clock frequency (in Hz)
  285. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI or LSE) is not ready
  286. */
  287. uint32_t LL_RCC_GetUSARTClockFreq(uint32_t USARTxSource)
  288. {
  289. uint32_t usart_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  290. /* Check parameter */
  291. assert_param(IS_LL_RCC_USART_CLKSOURCE(USARTxSource));
  292. #if defined(RCC_CFGR3_USART1SW)
  293. if (USARTxSource == LL_RCC_USART1_CLKSOURCE)
  294. {
  295. /* USART1CLK clock frequency */
  296. switch (LL_RCC_GetUSARTClockSource(USARTxSource))
  297. {
  298. case LL_RCC_USART1_CLKSOURCE_SYSCLK: /* USART1 Clock is System Clock */
  299. usart_frequency = RCC_GetSystemClockFreq();
  300. break;
  301. case LL_RCC_USART1_CLKSOURCE_HSI: /* USART1 Clock is HSI Osc. */
  302. if (LL_RCC_HSI_IsReady())
  303. {
  304. usart_frequency = HSI_VALUE;
  305. }
  306. break;
  307. case LL_RCC_USART1_CLKSOURCE_LSE: /* USART1 Clock is LSE Osc. */
  308. if (LL_RCC_LSE_IsReady())
  309. {
  310. usart_frequency = LSE_VALUE;
  311. }
  312. break;
  313. #if defined(RCC_CFGR3_USART1SW_PCLK1)
  314. case LL_RCC_USART1_CLKSOURCE_PCLK1: /* USART1 Clock is PCLK1 */
  315. default:
  316. usart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  317. #else
  318. case LL_RCC_USART1_CLKSOURCE_PCLK2: /* USART1 Clock is PCLK2 */
  319. default:
  320. usart_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  321. #endif /* RCC_CFGR3_USART1SW_PCLK1 */
  322. break;
  323. }
  324. }
  325. #endif /* RCC_CFGR3_USART1SW */
  326. #if defined(RCC_CFGR3_USART2SW)
  327. if (USARTxSource == LL_RCC_USART2_CLKSOURCE)
  328. {
  329. /* USART2CLK clock frequency */
  330. switch (LL_RCC_GetUSARTClockSource(USARTxSource))
  331. {
  332. case LL_RCC_USART2_CLKSOURCE_SYSCLK: /* USART2 Clock is System Clock */
  333. usart_frequency = RCC_GetSystemClockFreq();
  334. break;
  335. case LL_RCC_USART2_CLKSOURCE_HSI: /* USART2 Clock is HSI Osc. */
  336. if (LL_RCC_HSI_IsReady())
  337. {
  338. usart_frequency = HSI_VALUE;
  339. }
  340. break;
  341. case LL_RCC_USART2_CLKSOURCE_LSE: /* USART2 Clock is LSE Osc. */
  342. if (LL_RCC_LSE_IsReady())
  343. {
  344. usart_frequency = LSE_VALUE;
  345. }
  346. break;
  347. case LL_RCC_USART2_CLKSOURCE_PCLK1: /* USART2 Clock is PCLK1 */
  348. default:
  349. usart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  350. break;
  351. }
  352. }
  353. #endif /* RCC_CFGR3_USART2SW */
  354. #if defined(RCC_CFGR3_USART3SW)
  355. if (USARTxSource == LL_RCC_USART3_CLKSOURCE)
  356. {
  357. /* USART3CLK clock frequency */
  358. switch (LL_RCC_GetUSARTClockSource(USARTxSource))
  359. {
  360. case LL_RCC_USART3_CLKSOURCE_SYSCLK: /* USART3 Clock is System Clock */
  361. usart_frequency = RCC_GetSystemClockFreq();
  362. break;
  363. case LL_RCC_USART3_CLKSOURCE_HSI: /* USART3 Clock is HSI Osc. */
  364. if (LL_RCC_HSI_IsReady())
  365. {
  366. usart_frequency = HSI_VALUE;
  367. }
  368. break;
  369. case LL_RCC_USART3_CLKSOURCE_LSE: /* USART3 Clock is LSE Osc. */
  370. if (LL_RCC_LSE_IsReady())
  371. {
  372. usart_frequency = LSE_VALUE;
  373. }
  374. break;
  375. case LL_RCC_USART3_CLKSOURCE_PCLK1: /* USART3 Clock is PCLK1 */
  376. default:
  377. usart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  378. break;
  379. }
  380. }
  381. #endif /* RCC_CFGR3_USART3SW */
  382. return usart_frequency;
  383. }
  384. #if defined(UART4) || defined(UART5)
  385. /**
  386. * @brief Return UARTx clock frequency
  387. * @param UARTxSource This parameter can be one of the following values:
  388. * @arg @ref LL_RCC_UART4_CLKSOURCE
  389. * @arg @ref LL_RCC_UART5_CLKSOURCE
  390. * @retval UART clock frequency (in Hz)
  391. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI or LSE) is not ready
  392. */
  393. uint32_t LL_RCC_GetUARTClockFreq(uint32_t UARTxSource)
  394. {
  395. uint32_t uart_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  396. /* Check parameter */
  397. assert_param(IS_LL_RCC_UART_CLKSOURCE(UARTxSource));
  398. #if defined(UART4)
  399. if (UARTxSource == LL_RCC_UART4_CLKSOURCE)
  400. {
  401. /* UART4CLK clock frequency */
  402. switch (LL_RCC_GetUARTClockSource(UARTxSource))
  403. {
  404. case LL_RCC_UART4_CLKSOURCE_SYSCLK: /* UART4 Clock is System Clock */
  405. uart_frequency = RCC_GetSystemClockFreq();
  406. break;
  407. case LL_RCC_UART4_CLKSOURCE_HSI: /* UART4 Clock is HSI Osc. */
  408. if (LL_RCC_HSI_IsReady())
  409. {
  410. uart_frequency = HSI_VALUE;
  411. }
  412. break;
  413. case LL_RCC_UART4_CLKSOURCE_LSE: /* UART4 Clock is LSE Osc. */
  414. if (LL_RCC_LSE_IsReady())
  415. {
  416. uart_frequency = LSE_VALUE;
  417. }
  418. break;
  419. case LL_RCC_UART4_CLKSOURCE_PCLK1: /* UART4 Clock is PCLK1 */
  420. default:
  421. uart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  422. break;
  423. }
  424. }
  425. #endif /* UART4 */
  426. #if defined(UART5)
  427. if (UARTxSource == LL_RCC_UART5_CLKSOURCE)
  428. {
  429. /* UART5CLK clock frequency */
  430. switch (LL_RCC_GetUARTClockSource(UARTxSource))
  431. {
  432. case LL_RCC_UART5_CLKSOURCE_SYSCLK: /* UART5 Clock is System Clock */
  433. uart_frequency = RCC_GetSystemClockFreq();
  434. break;
  435. case LL_RCC_UART5_CLKSOURCE_HSI: /* UART5 Clock is HSI Osc. */
  436. if (LL_RCC_HSI_IsReady())
  437. {
  438. uart_frequency = HSI_VALUE;
  439. }
  440. break;
  441. case LL_RCC_UART5_CLKSOURCE_LSE: /* UART5 Clock is LSE Osc. */
  442. if (LL_RCC_LSE_IsReady())
  443. {
  444. uart_frequency = LSE_VALUE;
  445. }
  446. break;
  447. case LL_RCC_UART5_CLKSOURCE_PCLK1: /* UART5 Clock is PCLK1 */
  448. default:
  449. uart_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  450. break;
  451. }
  452. }
  453. #endif /* UART5 */
  454. return uart_frequency;
  455. }
  456. #endif /* UART4 || UART5 */
  457. /**
  458. * @brief Return I2Cx clock frequency
  459. * @param I2CxSource This parameter can be one of the following values:
  460. * @arg @ref LL_RCC_I2C1_CLKSOURCE
  461. * @arg @ref LL_RCC_I2C2_CLKSOURCE (*)
  462. * @arg @ref LL_RCC_I2C3_CLKSOURCE (*)
  463. *
  464. * (*) value not defined in all devices
  465. * @retval I2C clock frequency (in Hz)
  466. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that HSI oscillator is not ready
  467. */
  468. uint32_t LL_RCC_GetI2CClockFreq(uint32_t I2CxSource)
  469. {
  470. uint32_t i2c_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  471. /* Check parameter */
  472. assert_param(IS_LL_RCC_I2C_CLKSOURCE(I2CxSource));
  473. /* I2C1 CLK clock frequency */
  474. if (I2CxSource == LL_RCC_I2C1_CLKSOURCE)
  475. {
  476. switch (LL_RCC_GetI2CClockSource(I2CxSource))
  477. {
  478. case LL_RCC_I2C1_CLKSOURCE_SYSCLK: /* I2C1 Clock is System Clock */
  479. i2c_frequency = RCC_GetSystemClockFreq();
  480. break;
  481. case LL_RCC_I2C1_CLKSOURCE_HSI: /* I2C1 Clock is HSI Osc. */
  482. default:
  483. if (LL_RCC_HSI_IsReady())
  484. {
  485. i2c_frequency = HSI_VALUE;
  486. }
  487. break;
  488. }
  489. }
  490. #if defined(RCC_CFGR3_I2C2SW)
  491. /* I2C2 CLK clock frequency */
  492. if (I2CxSource == LL_RCC_I2C2_CLKSOURCE)
  493. {
  494. switch (LL_RCC_GetI2CClockSource(I2CxSource))
  495. {
  496. case LL_RCC_I2C2_CLKSOURCE_SYSCLK: /* I2C2 Clock is System Clock */
  497. i2c_frequency = RCC_GetSystemClockFreq();
  498. break;
  499. case LL_RCC_I2C2_CLKSOURCE_HSI: /* I2C2 Clock is HSI Osc. */
  500. default:
  501. if (LL_RCC_HSI_IsReady())
  502. {
  503. i2c_frequency = HSI_VALUE;
  504. }
  505. break;
  506. }
  507. }
  508. #endif /*RCC_CFGR3_I2C2SW*/
  509. #if defined(RCC_CFGR3_I2C3SW)
  510. /* I2C3 CLK clock frequency */
  511. if (I2CxSource == LL_RCC_I2C3_CLKSOURCE)
  512. {
  513. switch (LL_RCC_GetI2CClockSource(I2CxSource))
  514. {
  515. case LL_RCC_I2C3_CLKSOURCE_SYSCLK: /* I2C3 Clock is System Clock */
  516. i2c_frequency = RCC_GetSystemClockFreq();
  517. break;
  518. case LL_RCC_I2C3_CLKSOURCE_HSI: /* I2C3 Clock is HSI Osc. */
  519. default:
  520. if (LL_RCC_HSI_IsReady())
  521. {
  522. i2c_frequency = HSI_VALUE;
  523. }
  524. break;
  525. }
  526. }
  527. #endif /*RCC_CFGR3_I2C3SW*/
  528. return i2c_frequency;
  529. }
  530. #if defined(RCC_CFGR_I2SSRC)
  531. /**
  532. * @brief Return I2Sx clock frequency
  533. * @param I2SxSource This parameter can be one of the following values:
  534. * @arg @ref LL_RCC_I2S_CLKSOURCE
  535. * @retval I2S clock frequency (in Hz)
  536. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NA indicates that external clock is used */
  537. uint32_t LL_RCC_GetI2SClockFreq(uint32_t I2SxSource)
  538. {
  539. uint32_t i2s_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  540. /* Check parameter */
  541. assert_param(IS_LL_RCC_I2S_CLKSOURCE(I2SxSource));
  542. /* I2S1CLK clock frequency */
  543. switch (LL_RCC_GetI2SClockSource(I2SxSource))
  544. {
  545. case LL_RCC_I2S_CLKSOURCE_SYSCLK: /*!< System clock selected as I2S clock source */
  546. i2s_frequency = RCC_GetSystemClockFreq();
  547. break;
  548. case LL_RCC_I2S_CLKSOURCE_PIN: /*!< External clock selected as I2S clock source */
  549. default:
  550. i2s_frequency = LL_RCC_PERIPH_FREQUENCY_NA;
  551. break;
  552. }
  553. return i2s_frequency;
  554. }
  555. #endif /* RCC_CFGR_I2SSRC */
  556. #if defined(USB)
  557. /**
  558. * @brief Return USBx clock frequency
  559. * @param USBxSource This parameter can be one of the following values:
  560. * @arg @ref LL_RCC_USB_CLKSOURCE
  561. * @retval USB clock frequency (in Hz)
  562. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI48) or PLL is not ready
  563. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NA indicates that no clock source selected
  564. */
  565. uint32_t LL_RCC_GetUSBClockFreq(uint32_t USBxSource)
  566. {
  567. uint32_t usb_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  568. /* Check parameter */
  569. assert_param(IS_LL_RCC_USB_CLKSOURCE(USBxSource));
  570. /* USBCLK clock frequency */
  571. switch (LL_RCC_GetUSBClockSource(USBxSource))
  572. {
  573. case LL_RCC_USB_CLKSOURCE_PLL: /* PLL clock used as USB clock source */
  574. if (LL_RCC_PLL_IsReady())
  575. {
  576. usb_frequency = RCC_PLL_GetFreqDomain_SYS();
  577. }
  578. break;
  579. case LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5: /* PLL clock used as USB clock source */
  580. default:
  581. if (LL_RCC_PLL_IsReady())
  582. {
  583. usb_frequency = (RCC_PLL_GetFreqDomain_SYS() * 3U) / 2U;
  584. }
  585. break;
  586. }
  587. return usb_frequency;
  588. }
  589. #endif /* USB */
  590. #if defined(RCC_CFGR_ADCPRE) || defined(RCC_CFGR2_ADC1PRES) || defined(RCC_CFGR2_ADCPRE12) || defined(RCC_CFGR2_ADCPRE34)
  591. /**
  592. * @brief Return ADCx clock frequency
  593. * @param ADCxSource This parameter can be one of the following values:
  594. * @arg @ref LL_RCC_ADC_CLKSOURCE (*)
  595. * @arg @ref LL_RCC_ADC1_CLKSOURCE (*)
  596. * @arg @ref LL_RCC_ADC12_CLKSOURCE (*)
  597. * @arg @ref LL_RCC_ADC34_CLKSOURCE (*)
  598. *
  599. * (*) value not defined in all devices
  600. * @retval ADC clock frequency (in Hz)
  601. */
  602. uint32_t LL_RCC_GetADCClockFreq(uint32_t ADCxSource)
  603. {
  604. uint32_t adc_prescaler = 0U;
  605. uint32_t adc_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  606. /* Check parameter */
  607. assert_param(IS_LL_RCC_ADC_CLKSOURCE(ADCxSource));
  608. /* Get ADC prescaler */
  609. adc_prescaler = LL_RCC_GetADCClockSource(ADCxSource);
  610. #if defined(RCC_CFGR_ADCPRE)
  611. /* ADC frequency = PCLK2 frequency / ADC prescaler (2, 4, 6 or 8) */
  612. adc_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()))
  613. / (((adc_prescaler >> POSITION_VAL(ADCxSource)) + 1U) * 2U);
  614. #else
  615. if ((adc_prescaler & 0x0000FFFFU) == ((uint32_t)0x00000000U))
  616. {
  617. /* ADC frequency = HCLK frequency */
  618. adc_frequency = RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq());
  619. }
  620. else
  621. {
  622. /* ADC frequency = PCLK2 frequency / ADC prescaler (from 1 to 256) */
  623. adc_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()))
  624. / (aADCPrescTable[((adc_prescaler & 0x0000FFFFU) >> POSITION_VAL(ADCxSource)) & 0xFU]);
  625. }
  626. #endif /* RCC_CFGR_ADCPRE */
  627. return adc_frequency;
  628. }
  629. #endif /*RCC_CFGR_ADCPRE || RCC_CFGR2_ADC1PRES || RCC_CFGR2_ADCPRE12 || RCC_CFGR2_ADCPRE34 */
  630. #if defined(RCC_CFGR_SDPRE)
  631. /**
  632. * @brief Return SDADCx clock frequency
  633. * @param SDADCxSource This parameter can be one of the following values:
  634. * @arg @ref LL_RCC_SDADC_CLKSOURCE
  635. * @retval SDADC clock frequency (in Hz)
  636. */
  637. uint32_t LL_RCC_GetSDADCClockFreq(uint32_t SDADCxSource)
  638. {
  639. uint32_t sdadc_prescaler = 0U;
  640. uint32_t sdadc_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  641. /* Check parameter */
  642. assert_param(IS_LL_RCC_SDADC_CLKSOURCE(SDADCxSource));
  643. /* Get SDADC prescaler */
  644. sdadc_prescaler = LL_RCC_GetSDADCClockSource(SDADCxSource);
  645. /* SDADC frequency = SYSTEM frequency / SDADC prescaler (from 2 to 48) */
  646. sdadc_frequency = RCC_GetSystemClockFreq()
  647. / (aSDADCPrescTable[(sdadc_prescaler >> POSITION_VAL(SDADCxSource)) & 0xFU]);
  648. return sdadc_frequency;
  649. }
  650. #endif /*RCC_CFGR_SDPRE */
  651. #if defined(CEC)
  652. /**
  653. * @brief Return CECx clock frequency
  654. * @param CECxSource This parameter can be one of the following values:
  655. * @arg @ref LL_RCC_CEC_CLKSOURCE
  656. * @retval CEC clock frequency (in Hz)
  657. * @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillators (HSI or LSE) are not ready
  658. */
  659. uint32_t LL_RCC_GetCECClockFreq(uint32_t CECxSource)
  660. {
  661. uint32_t cec_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  662. /* Check parameter */
  663. assert_param(IS_LL_RCC_CEC_CLKSOURCE(CECxSource));
  664. /* CECCLK clock frequency */
  665. switch (LL_RCC_GetCECClockSource(CECxSource))
  666. {
  667. case LL_RCC_CEC_CLKSOURCE_HSI_DIV244: /* HSI / 244 clock used as CEC clock source */
  668. if (LL_RCC_HSI_IsReady())
  669. {
  670. cec_frequency = HSI_VALUE / 244U;
  671. }
  672. break;
  673. case LL_RCC_CEC_CLKSOURCE_LSE: /* LSE clock used as CEC clock source */
  674. default:
  675. if (LL_RCC_LSE_IsReady())
  676. {
  677. cec_frequency = LSE_VALUE;
  678. }
  679. break;
  680. }
  681. return cec_frequency;
  682. }
  683. #endif /* CEC */
  684. #if defined(RCC_CFGR3_TIMSW)
  685. /**
  686. * @brief Return TIMx clock frequency
  687. * @param TIMxSource This parameter can be one of the following values:
  688. * @arg @ref LL_RCC_TIM1_CLKSOURCE
  689. * @arg @ref LL_RCC_TIM8_CLKSOURCE (*)
  690. * @arg @ref LL_RCC_TIM15_CLKSOURCE (*)
  691. * @arg @ref LL_RCC_TIM16_CLKSOURCE (*)
  692. * @arg @ref LL_RCC_TIM17_CLKSOURCE (*)
  693. * @arg @ref LL_RCC_TIM20_CLKSOURCE (*)
  694. * @arg @ref LL_RCC_TIM2_CLKSOURCE (*)
  695. * @arg @ref LL_RCC_TIM34_CLKSOURCE (*)
  696. *
  697. * (*) value not defined in all devices
  698. * @retval TIM clock frequency (in Hz)
  699. */
  700. uint32_t LL_RCC_GetTIMClockFreq(uint32_t TIMxSource)
  701. {
  702. uint32_t tim_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  703. /* Check parameter */
  704. assert_param(IS_LL_RCC_TIM_CLKSOURCE(TIMxSource));
  705. if (TIMxSource == LL_RCC_TIM1_CLKSOURCE)
  706. {
  707. /* TIM1CLK clock frequency */
  708. if (LL_RCC_GetTIMClockSource(LL_RCC_TIM1_CLKSOURCE) == LL_RCC_TIM1_CLKSOURCE_PCLK2)
  709. {
  710. /* PCLK2 used as TIM1 clock source */
  711. tim_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  712. }
  713. else /* LL_RCC_TIM1_CLKSOURCE_PLL */
  714. {
  715. /* PLL clock used as TIM1 clock source */
  716. tim_frequency = RCC_PLL_GetFreqDomain_SYS();
  717. }
  718. }
  719. #if defined(RCC_CFGR3_TIM8SW)
  720. if (TIMxSource == LL_RCC_TIM8_CLKSOURCE)
  721. {
  722. /* TIM8CLK clock frequency */
  723. if (LL_RCC_GetTIMClockSource(LL_RCC_TIM8_CLKSOURCE) == LL_RCC_TIM8_CLKSOURCE_PCLK2)
  724. {
  725. /* PCLK2 used as TIM8 clock source */
  726. tim_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  727. }
  728. else /* LL_RCC_TIM8_CLKSOURCE_PLL */
  729. {
  730. /* PLL clock used as TIM8 clock source */
  731. tim_frequency = RCC_PLL_GetFreqDomain_SYS();
  732. }
  733. }
  734. #endif /*RCC_CFGR3_TIM8SW*/
  735. #if defined(RCC_CFGR3_TIM15SW)
  736. if (TIMxSource == LL_RCC_TIM15_CLKSOURCE)
  737. {
  738. /* TIM15CLK clock frequency */
  739. if (LL_RCC_GetTIMClockSource(LL_RCC_TIM15_CLKSOURCE) == LL_RCC_TIM15_CLKSOURCE_PCLK2)
  740. {
  741. /* PCLK2 used as TIM15 clock source */
  742. tim_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  743. }
  744. else /* LL_RCC_TIM15_CLKSOURCE_PLL */
  745. {
  746. /* PLL clock used as TIM15 clock source */
  747. tim_frequency = RCC_PLL_GetFreqDomain_SYS();
  748. }
  749. }
  750. #endif /*RCC_CFGR3_TIM15SW*/
  751. #if defined(RCC_CFGR3_TIM16SW)
  752. if (TIMxSource == LL_RCC_TIM16_CLKSOURCE)
  753. {
  754. /* TIM16CLK clock frequency */
  755. if (LL_RCC_GetTIMClockSource(LL_RCC_TIM16_CLKSOURCE) == LL_RCC_TIM16_CLKSOURCE_PCLK2)
  756. {
  757. /* PCLK2 used as TIM16 clock source */
  758. tim_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  759. }
  760. else /* LL_RCC_TIM16_CLKSOURCE_PLL */
  761. {
  762. /* PLL clock used as TIM16 clock source */
  763. tim_frequency = RCC_PLL_GetFreqDomain_SYS();
  764. }
  765. }
  766. #endif /*RCC_CFGR3_TIM16SW*/
  767. #if defined(RCC_CFGR3_TIM17SW)
  768. if (TIMxSource == LL_RCC_TIM17_CLKSOURCE)
  769. {
  770. /* TIM17CLK clock frequency */
  771. if (LL_RCC_GetTIMClockSource(LL_RCC_TIM17_CLKSOURCE) == LL_RCC_TIM17_CLKSOURCE_PCLK2)
  772. {
  773. /* PCLK2 used as TIM17 clock source */
  774. tim_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  775. }
  776. else /* LL_RCC_TIM17_CLKSOURCE_PLL */
  777. {
  778. /* PLL clock used as TIM17 clock source */
  779. tim_frequency = RCC_PLL_GetFreqDomain_SYS();
  780. }
  781. }
  782. #endif /*RCC_CFGR3_TIM17SW*/
  783. #if defined(RCC_CFGR3_TIM20SW)
  784. if (TIMxSource == LL_RCC_TIM20_CLKSOURCE)
  785. {
  786. /* TIM20CLK clock frequency */
  787. if (LL_RCC_GetTIMClockSource(LL_RCC_TIM20_CLKSOURCE) == LL_RCC_TIM20_CLKSOURCE_PCLK2)
  788. {
  789. /* PCLK2 used as TIM20 clock source */
  790. tim_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  791. }
  792. else /* LL_RCC_TIM20_CLKSOURCE_PLL */
  793. {
  794. /* PLL clock used as TIM20 clock source */
  795. tim_frequency = RCC_PLL_GetFreqDomain_SYS();
  796. }
  797. }
  798. #endif /*RCC_CFGR3_TIM20SW*/
  799. #if defined(RCC_CFGR3_TIM2SW)
  800. if (TIMxSource == LL_RCC_TIM2_CLKSOURCE)
  801. {
  802. /* TIM2CLK clock frequency */
  803. if (LL_RCC_GetTIMClockSource(LL_RCC_TIM2_CLKSOURCE) == LL_RCC_TIM2_CLKSOURCE_PCLK1)
  804. {
  805. /* PCLK1 used as TIM2 clock source */
  806. tim_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  807. }
  808. else /* LL_RCC_TIM2_CLKSOURCE_PLL */
  809. {
  810. /* PLL clock used as TIM2 clock source */
  811. tim_frequency = RCC_PLL_GetFreqDomain_SYS();
  812. }
  813. }
  814. #endif /*RCC_CFGR3_TIM2SW*/
  815. #if defined(RCC_CFGR3_TIM34SW)
  816. if (TIMxSource == LL_RCC_TIM34_CLKSOURCE)
  817. {
  818. /* TIM3/4 CLK clock frequency */
  819. if (LL_RCC_GetTIMClockSource(LL_RCC_TIM34_CLKSOURCE) == LL_RCC_TIM34_CLKSOURCE_PCLK1)
  820. {
  821. /* PCLK1 used as TIM3/4 clock source */
  822. tim_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  823. }
  824. else /* LL_RCC_TIM34_CLKSOURCE_PLL */
  825. {
  826. /* PLL clock used as TIM3/4 clock source */
  827. tim_frequency = RCC_PLL_GetFreqDomain_SYS();
  828. }
  829. }
  830. #endif /*RCC_CFGR3_TIM34SW*/
  831. return tim_frequency;
  832. }
  833. #endif /*RCC_CFGR3_TIMSW*/
  834. #if defined(HRTIM1)
  835. /**
  836. * @brief Return HRTIMx clock frequency
  837. * @param HRTIMxSource This parameter can be one of the following values:
  838. * @arg @ref LL_RCC_HRTIM1_CLKSOURCE
  839. * @retval HRTIM clock frequency (in Hz)
  840. */
  841. uint32_t LL_RCC_GetHRTIMClockFreq(uint32_t HRTIMxSource)
  842. {
  843. uint32_t hrtim_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  844. /* Check parameter */
  845. assert_param(IS_LL_RCC_HRTIM_CLKSOURCE(HRTIMxSource));
  846. /* HRTIM1CLK clock frequency */
  847. if (LL_RCC_GetHRTIMClockSource(LL_RCC_HRTIM1_CLKSOURCE) == LL_RCC_HRTIM1_CLKSOURCE_PCLK2)
  848. {
  849. /* PCLK2 used as HRTIM1 clock source */
  850. hrtim_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  851. }
  852. else /* LL_RCC_HRTIM1_CLKSOURCE_PLL */
  853. {
  854. /* PLL clock used as HRTIM1 clock source */
  855. hrtim_frequency = RCC_PLL_GetFreqDomain_SYS();
  856. }
  857. return hrtim_frequency;
  858. }
  859. #endif /* HRTIM1 */
  860. /**
  861. * @}
  862. */
  863. /**
  864. * @}
  865. */
  866. /** @addtogroup RCC_LL_Private_Functions
  867. * @{
  868. */
  869. /**
  870. * @brief Return SYSTEM clock frequency
  871. * @retval SYSTEM clock frequency (in Hz)
  872. */
  873. uint32_t RCC_GetSystemClockFreq(void)
  874. {
  875. uint32_t frequency = 0U;
  876. /* Get SYSCLK source -------------------------------------------------------*/
  877. switch (LL_RCC_GetSysClkSource())
  878. {
  879. case LL_RCC_SYS_CLKSOURCE_STATUS_HSI: /* HSI used as system clock source */
  880. frequency = HSI_VALUE;
  881. break;
  882. case LL_RCC_SYS_CLKSOURCE_STATUS_HSE: /* HSE used as system clock source */
  883. frequency = HSE_VALUE;
  884. break;
  885. case LL_RCC_SYS_CLKSOURCE_STATUS_PLL: /* PLL used as system clock source */
  886. frequency = RCC_PLL_GetFreqDomain_SYS();
  887. break;
  888. default:
  889. frequency = HSI_VALUE;
  890. break;
  891. }
  892. return frequency;
  893. }
  894. /**
  895. * @brief Return HCLK clock frequency
  896. * @param SYSCLK_Frequency SYSCLK clock frequency
  897. * @retval HCLK clock frequency (in Hz)
  898. */
  899. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency)
  900. {
  901. /* HCLK clock frequency */
  902. return __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, LL_RCC_GetAHBPrescaler());
  903. }
  904. /**
  905. * @brief Return PCLK1 clock frequency
  906. * @param HCLK_Frequency HCLK clock frequency
  907. * @retval PCLK1 clock frequency (in Hz)
  908. */
  909. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency)
  910. {
  911. /* PCLK1 clock frequency */
  912. return __LL_RCC_CALC_PCLK1_FREQ(HCLK_Frequency, LL_RCC_GetAPB1Prescaler());
  913. }
  914. /**
  915. * @brief Return PCLK2 clock frequency
  916. * @param HCLK_Frequency HCLK clock frequency
  917. * @retval PCLK2 clock frequency (in Hz)
  918. */
  919. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency)
  920. {
  921. /* PCLK2 clock frequency */
  922. return __LL_RCC_CALC_PCLK2_FREQ(HCLK_Frequency, LL_RCC_GetAPB2Prescaler());
  923. }
  924. /**
  925. * @brief Return PLL clock frequency used for system domain
  926. * @retval PLL clock frequency (in Hz)
  927. */
  928. uint32_t RCC_PLL_GetFreqDomain_SYS(void)
  929. {
  930. uint32_t pllinputfreq = 0U, pllsource = 0U;
  931. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL divider) * PLL Multiplicator */
  932. /* Get PLL source */
  933. pllsource = LL_RCC_PLL_GetMainSource();
  934. switch (pllsource)
  935. {
  936. #if defined(RCC_PLLSRC_PREDIV1_SUPPORT)
  937. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  938. pllinputfreq = HSI_VALUE;
  939. #else
  940. case LL_RCC_PLLSOURCE_HSI_DIV_2: /* HSI used as PLL clock source */
  941. pllinputfreq = HSI_VALUE / 2U;
  942. #endif /* RCC_PLLSRC_PREDIV1_SUPPORT */
  943. break;
  944. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  945. pllinputfreq = HSE_VALUE;
  946. break;
  947. default:
  948. #if defined(RCC_PLLSRC_PREDIV1_SUPPORT)
  949. pllinputfreq = HSI_VALUE;
  950. #else
  951. pllinputfreq = HSI_VALUE / 2U;
  952. #endif /* RCC_PLLSRC_PREDIV1_SUPPORT */
  953. break;
  954. }
  955. #if defined(RCC_PLLSRC_PREDIV1_SUPPORT)
  956. return __LL_RCC_CALC_PLLCLK_FREQ(pllinputfreq, LL_RCC_PLL_GetMultiplicator(), LL_RCC_PLL_GetPrediv());
  957. #else
  958. return __LL_RCC_CALC_PLLCLK_FREQ((pllinputfreq / (LL_RCC_PLL_GetPrediv() + 1U)), LL_RCC_PLL_GetMultiplicator());
  959. #endif /* RCC_PLLSRC_PREDIV1_SUPPORT */
  960. }
  961. /**
  962. * @}
  963. */
  964. /**
  965. * @}
  966. */
  967. #endif /* defined(RCC) */
  968. /**
  969. * @}
  970. */
  971. #endif /* USE_FULL_LL_DRIVER */
  972. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/