You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2352 lines
72 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_rcc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended RCC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities RCC extended peripheral:
  8. * + Extended Peripheral Control functions
  9. * + Extended Clock management functions
  10. * + Extended Clock Recovery System Control functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  16. * All rights reserved.</center></h2>
  17. *
  18. * This software component is licensed by ST under BSD 3-Clause license,
  19. * the "License"; You may not use this file except in compliance with the
  20. * License. You may obtain a copy of the License at:
  21. * opensource.org/licenses/BSD-3-Clause
  22. *
  23. ******************************************************************************
  24. */
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32wbxx_hal.h"
  27. /** @addtogroup STM32WBxx_HAL_Driver
  28. * @{
  29. */
  30. /** @defgroup RCCEx RCCEx
  31. * @brief RCC Extended HAL module driver
  32. * @{
  33. */
  34. #ifdef HAL_RCC_MODULE_ENABLED
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private defines -----------------------------------------------------------*/
  37. /** @defgroup RCCEx_Private_Constants RCCEx Private Constants
  38. * @{
  39. */
  40. #if defined(SAI1)
  41. #define PLLSAI1_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  42. #endif
  43. #define PLL_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  44. #define CLOCKSMPS_TIMEOUT_VALUE (5000U) /* 5 s */
  45. #define __LSCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  46. #define LSCO1_GPIO_PORT GPIOA
  47. #define LSCO1_PIN GPIO_PIN_2
  48. #define __LSCO2_CLK_ENABLE() __HAL_RCC_GPIOH_CLK_ENABLE()
  49. #define LSCO2_GPIO_PORT GPIOH
  50. #define LSCO2_PIN GPIO_PIN_3
  51. #if defined(RCC_LSCO3_SUPPORT)
  52. #define __LSCO3_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  53. #define LSCO3_GPIO_PORT GPIOC
  54. #define LSCO3_PIN GPIO_PIN_12
  55. #endif
  56. #define LSI2_TIMEOUT_VALUE (3U) /* to be adjusted with DS */
  57. /**
  58. * @}
  59. */
  60. /* Private macros ------------------------------------------------------------*/
  61. /* Private variables ---------------------------------------------------------*/
  62. /* Private function prototypes -----------------------------------------------*/
  63. /** @defgroup RCCEx_Private_Functions RCCEx Private Functions
  64. * @{
  65. */
  66. #if defined(SAI1)
  67. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNP(RCC_PLLSAI1InitTypeDef *PLLSAI1);
  68. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNQ(RCC_PLLSAI1InitTypeDef *PLLSAI1);
  69. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNR(RCC_PLLSAI1InitTypeDef *PLLSAI1);
  70. #endif
  71. static uint32_t RCC_PLL_GetFreqDomain_P(void);
  72. static uint32_t RCC_PLL_GetFreqDomain_Q(void);
  73. #if defined(SAI1)
  74. static uint32_t RCC_PLLSAI1_GetFreqDomain_R(void);
  75. static uint32_t RCC_PLLSAI1_GetFreqDomain_P(void);
  76. static uint32_t RCC_PLLSAI1_GetFreqDomain_Q(void);
  77. #endif
  78. /**
  79. * @}
  80. */
  81. /* Exported functions --------------------------------------------------------*/
  82. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  83. * @{
  84. */
  85. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  86. * @brief Extended Peripheral Control functions
  87. *
  88. @verbatim
  89. ===============================================================================
  90. ##### Extended Peripheral Control functions #####
  91. ===============================================================================
  92. [..]
  93. This subsection provides a set of functions allowing to control the RCC Clocks
  94. frequencies.
  95. [..]
  96. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  97. select the RTC clock source; in this case the Backup domain will be reset in
  98. order to modify the RTC Clock source, as consequence RTC registers (including
  99. the backup registers) and RCC_BDCR register are set to their reset values.
  100. @endverbatim
  101. * @{
  102. */
  103. /**
  104. * @brief Initialize the RCC extended peripherals clocks according to the specified
  105. * parameters in the @ref RCC_PeriphCLKInitTypeDef.
  106. * @param PeriphClkInit pointer to a @ref RCC_PeriphCLKInitTypeDef structure that
  107. * contains a field PeriphClockSelection which can be a combination of the following values:
  108. *
  109. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  110. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock
  111. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  112. * @arg @ref RCC_PERIPHCLK_I2C3 I2C3 peripheral clock
  113. * @arg @ref RCC_PERIPHCLK_LPTIM1 LPTIM1 peripheral clock
  114. * @arg @ref RCC_PERIPHCLK_LPTIM2 LPTIM2 peripheral clock
  115. * @arg @ref RCC_PERIPHCLK_SAI1 SAI1 peripheral clock
  116. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  117. * @arg @ref RCC_PERIPHCLK_RNG RNG peripheral clock
  118. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  119. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  120. * @arg @ref RCC_PERIPHCLK_RFWAKEUP RFWKP peripheral clock
  121. * @arg @ref RCC_PERIPHCLK_SMPS SMPS peripheral clock
  122. *
  123. *
  124. * @note Care must be taken when @ref HAL_RCCEx_PeriphCLKConfig() is used to select
  125. * the RTC clock source: in this case the access to Backup domain is enabled.
  126. *
  127. * @retval HAL status
  128. */
  129. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  130. {
  131. uint32_t tickstart;
  132. HAL_StatusTypeDef ret = HAL_OK; /* Intermediate status */
  133. HAL_StatusTypeDef status = HAL_OK; /* Final status */
  134. /* Check the parameters */
  135. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  136. #if defined(SAI1)
  137. /*-------------------------- SAI1 clock source configuration ---------------------*/
  138. if ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == RCC_PERIPHCLK_SAI1))
  139. {
  140. /* Check the parameters */
  141. assert_param(IS_RCC_SAI1CLK(PeriphClkInit->Sai1ClockSelection));
  142. switch (PeriphClkInit->Sai1ClockSelection)
  143. {
  144. case RCC_SAI1CLKSOURCE_PLL: /* PLL is used as clock source for SAI1 */
  145. /* Enable SAI1 Clock output generated form System PLL . */
  146. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SAI1CLK);
  147. /* SAI1 clock source config set later after clock selection check */
  148. break;
  149. #if defined(SAI1)
  150. case RCC_SAI1CLKSOURCE_PLLSAI1: /* PLLSAI1 is used as clock source for SAI1 */
  151. /* PLLSAI1 parameters N & P configuration and clock output (PLLSAI1ClockOut) */
  152. ret = RCCEx_PLLSAI1_ConfigNP(&(PeriphClkInit->PLLSAI1));
  153. /* SAI1 clock source config set later after clock selection check */
  154. break;
  155. #endif
  156. case RCC_SAI1CLKSOURCE_PIN: /* External clock is used as source of SAI1 clock*/
  157. /* SAI1 clock source config set later after clock selection check */
  158. break;
  159. case RCC_SAI1CLKSOURCE_HSI:
  160. break;
  161. default:
  162. ret = HAL_ERROR;
  163. break;
  164. }
  165. if (ret == HAL_OK)
  166. {
  167. /* Set the source of SAI1 clock*/
  168. __HAL_RCC_SAI1_CONFIG(PeriphClkInit->Sai1ClockSelection);
  169. }
  170. else
  171. {
  172. /* set overall return value */
  173. status = ret;
  174. }
  175. }
  176. #endif
  177. /*-------------------------- RTC clock source configuration ----------------------*/
  178. if ((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC)
  179. {
  180. uint32_t rtcclocksource = LL_RCC_GetRTCClockSource();
  181. /* Check for RTC Parameters used to output RTCCLK */
  182. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  183. /* Configure the clock source only if a different source is expected */
  184. if (rtcclocksource != PeriphClkInit->RTCClockSelection)
  185. {
  186. /* Enable write access to Backup domain */
  187. HAL_PWR_EnableBkUpAccess();
  188. /* If a clock source is not yet selected */
  189. if (rtcclocksource == RCC_RTCCLKSOURCE_NONE)
  190. {
  191. /* Directly set the configuration of the clock source selection */
  192. LL_RCC_SetRTCClockSource(PeriphClkInit->RTCClockSelection);
  193. }
  194. else /* A clock source is already selected */
  195. {
  196. /* Store the content of BDCR register before the reset of Backup Domain */
  197. uint32_t bdcr = LL_RCC_ReadReg(BDCR);
  198. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  199. LL_RCC_ForceBackupDomainReset();
  200. LL_RCC_ReleaseBackupDomainReset();
  201. /* Set the value of the clock source selection */
  202. MODIFY_REG(bdcr, RCC_BDCR_RTCSEL, PeriphClkInit->RTCClockSelection);
  203. /* Restore the content of BDCR register */
  204. LL_RCC_WriteReg(BDCR, bdcr);
  205. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  206. if (LL_RCC_LSE_IsEnabled() == 1U)
  207. {
  208. /* Get Start Tick*/
  209. tickstart = HAL_GetTick();
  210. /* Wait till LSE is ready */
  211. while (LL_RCC_LSE_IsReady() != 1U)
  212. {
  213. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  214. {
  215. ret = HAL_TIMEOUT;
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. /* set overall return value */
  222. status = ret;
  223. }
  224. else
  225. {
  226. /* set overall return value */
  227. status = ret;
  228. }
  229. }
  230. /*-------------------------- USART1 clock source configuration -------------------*/
  231. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART1) == RCC_PERIPHCLK_USART1)
  232. {
  233. /* Check the parameters */
  234. assert_param(IS_RCC_USART1CLKSOURCE(PeriphClkInit->Usart1ClockSelection));
  235. /* Configure the USART1 clock source */
  236. __HAL_RCC_USART1_CONFIG(PeriphClkInit->Usart1ClockSelection);
  237. }
  238. #if defined(LPUART1)
  239. /*-------------------------- LPUART1 clock source configuration ------------------*/
  240. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPUART1) == RCC_PERIPHCLK_LPUART1)
  241. {
  242. /* Check the parameters */
  243. assert_param(IS_RCC_LPUART1CLKSOURCE(PeriphClkInit->Lpuart1ClockSelection));
  244. /* Configure the LPUAR1 clock source */
  245. __HAL_RCC_LPUART1_CONFIG(PeriphClkInit->Lpuart1ClockSelection);
  246. }
  247. #endif
  248. /*-------------------------- LPTIM1 clock source configuration -------------------*/
  249. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == (RCC_PERIPHCLK_LPTIM1))
  250. {
  251. assert_param(IS_RCC_LPTIM1CLK(PeriphClkInit->Lptim1ClockSelection));
  252. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->Lptim1ClockSelection);
  253. }
  254. /*-------------------------- LPTIM2 clock source configuration -------------------*/
  255. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM2) == (RCC_PERIPHCLK_LPTIM2))
  256. {
  257. assert_param(IS_RCC_LPTIM2CLK(PeriphClkInit->Lptim2ClockSelection));
  258. __HAL_RCC_LPTIM2_CONFIG(PeriphClkInit->Lptim2ClockSelection);
  259. }
  260. /*-------------------------- I2C1 clock source configuration ---------------------*/
  261. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C1) == RCC_PERIPHCLK_I2C1)
  262. {
  263. /* Check the parameters */
  264. assert_param(IS_RCC_I2C1CLKSOURCE(PeriphClkInit->I2c1ClockSelection));
  265. /* Configure the I2C1 clock source */
  266. __HAL_RCC_I2C1_CONFIG(PeriphClkInit->I2c1ClockSelection);
  267. }
  268. #if defined(I2C3)
  269. /*-------------------------- I2C3 clock source configuration ---------------------*/
  270. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C3) == RCC_PERIPHCLK_I2C3)
  271. {
  272. /* Check the parameters */
  273. assert_param(IS_RCC_I2C3CLKSOURCE(PeriphClkInit->I2c3ClockSelection));
  274. /* Configure the I2C3 clock source */
  275. __HAL_RCC_I2C3_CONFIG(PeriphClkInit->I2c3ClockSelection);
  276. }
  277. #endif
  278. #if defined(USB)
  279. /*-------------------------- USB clock source configuration ----------------------*/
  280. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == (RCC_PERIPHCLK_USB))
  281. {
  282. assert_param(IS_RCC_USBCLKSOURCE(PeriphClkInit->UsbClockSelection));
  283. __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
  284. if (PeriphClkInit->UsbClockSelection == RCC_USBCLKSOURCE_PLL)
  285. {
  286. /* Enable PLLQ output */
  287. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_USBCLK);
  288. }
  289. #if defined(SAI1)
  290. if (PeriphClkInit->UsbClockSelection == RCC_USBCLKSOURCE_PLLSAI1)
  291. {
  292. /* PLLSAI1 parameters N & Q configuration and clock output (PLLSAI1ClockOut) */
  293. ret = RCCEx_PLLSAI1_ConfigNQ(&(PeriphClkInit->PLLSAI1));
  294. if (ret != HAL_OK)
  295. {
  296. /* set overall return value */
  297. status = ret;
  298. }
  299. }
  300. #endif
  301. }
  302. #endif
  303. /*-------------------------- RNG clock source configuration ----------------------*/
  304. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RNG) == (RCC_PERIPHCLK_RNG))
  305. {
  306. /* Check the parameters */
  307. assert_param(IS_RCC_RNGCLKSOURCE(PeriphClkInit->RngClockSelection));
  308. /* Configure the RNG clock source */
  309. __HAL_RCC_RNG_CONFIG(PeriphClkInit->RngClockSelection);
  310. if (PeriphClkInit->RngClockSelection == RCC_RNGCLKSOURCE_PLL)
  311. {
  312. /* Enable PLLQ output */
  313. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_RNGCLK);
  314. }
  315. }
  316. /*-------------------------- ADC clock source configuration ----------------------*/
  317. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC) == RCC_PERIPHCLK_ADC)
  318. {
  319. /* Check the parameters */
  320. assert_param(IS_RCC_ADCCLKSOURCE(PeriphClkInit->AdcClockSelection));
  321. /* Configure the ADC interface clock source */
  322. __HAL_RCC_ADC_CONFIG(PeriphClkInit->AdcClockSelection);
  323. if (PeriphClkInit->AdcClockSelection == RCC_ADCCLKSOURCE_PLL)
  324. {
  325. /* Enable RCC_PLL_RNGCLK output */
  326. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_ADCCLK);
  327. }
  328. #if defined(SAI1)
  329. if (PeriphClkInit->AdcClockSelection == RCC_ADCCLKSOURCE_PLLSAI1)
  330. {
  331. /* PLLSAI1 parameters N & R configuration and clock output (PLLSAI1ClockOut) */
  332. ret = RCCEx_PLLSAI1_ConfigNR(&(PeriphClkInit->PLLSAI1));
  333. if (ret != HAL_OK)
  334. {
  335. /* set overall return value */
  336. status = ret;
  337. }
  338. }
  339. #endif
  340. }
  341. /*-------------------------- RFWKP clock source configuration ----------------------*/
  342. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RFWAKEUP) == RCC_PERIPHCLK_RFWAKEUP)
  343. {
  344. /* Check the parameters */
  345. assert_param(IS_RCC_RFWKPCLKSOURCE(PeriphClkInit->RFWakeUpClockSelection));
  346. /* Configure the RFWKP interface clock source */
  347. __HAL_RCC_RFWAKEUP_CONFIG(PeriphClkInit->RFWakeUpClockSelection);
  348. }
  349. #if defined(RCC_SMPS_SUPPORT)
  350. /*-------------------------- SMPS clock source configuration ----------------------*/
  351. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SMPS) == RCC_PERIPHCLK_SMPS)
  352. {
  353. /* Check the parameters */
  354. assert_param(IS_RCC_SMPSCLKDIV(PeriphClkInit->SmpsDivSelection));
  355. assert_param(IS_RCC_SMPSCLKSOURCE(PeriphClkInit->SmpsClockSelection));
  356. /* Configure the SMPS interface clock division factor */
  357. __HAL_RCC_SMPS_DIV_CONFIG(PeriphClkInit->SmpsDivSelection);
  358. /* Configure the SMPS interface clock source */
  359. __HAL_RCC_SMPS_CONFIG(PeriphClkInit->SmpsClockSelection);
  360. }
  361. #endif
  362. return status;
  363. }
  364. /**
  365. * @brief Get the RCC_ClkInitStruct according to the internal RCC configuration registers.
  366. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  367. * returns the configuration information for the Extended Peripherals
  368. * clocks(SAI1, LPTIM1, LPTIM2, I2C1, I2C3, LPUART1,
  369. * USART1, RTC, ADCx, USB, RNG, RFWKP, SMPS).
  370. * @retval None
  371. */
  372. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  373. {
  374. /* Set all possible values for the extended clock type parameter------------*/
  375. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_I2C1 | \
  376. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | \
  377. RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | \
  378. RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_RFWAKEUP;
  379. #if defined(LPUART1)
  380. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_LPUART1;
  381. #endif
  382. #if defined(I2C3)
  383. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2C3;
  384. #endif
  385. #if defined(SAI1)
  386. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_SAI1;
  387. #endif
  388. #if defined(USB)
  389. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USB;
  390. #endif
  391. #if defined(RCC_SMPS_SUPPORT)
  392. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_SMPS;
  393. #endif
  394. #if defined(SAI1)
  395. /* Get the PLLSAI1 Clock configuration -----------------------------------------------*/
  396. PeriphClkInit->PLLSAI1.PLLN = LL_RCC_PLLSAI1_GetN();
  397. PeriphClkInit->PLLSAI1.PLLP = LL_RCC_PLLSAI1_GetP();
  398. PeriphClkInit->PLLSAI1.PLLR = LL_RCC_PLLSAI1_GetR();
  399. PeriphClkInit->PLLSAI1.PLLQ = LL_RCC_PLLSAI1_GetQ();
  400. #endif
  401. /* Get the USART1 clock source ---------------------------------------------*/
  402. PeriphClkInit->Usart1ClockSelection = __HAL_RCC_GET_USART1_SOURCE();
  403. #if defined(LPUART1)
  404. /* Get the LPUART1 clock source --------------------------------------------*/
  405. PeriphClkInit->Lpuart1ClockSelection = __HAL_RCC_GET_LPUART1_SOURCE();
  406. #endif
  407. /* Get the I2C1 clock source -----------------------------------------------*/
  408. PeriphClkInit->I2c1ClockSelection = __HAL_RCC_GET_I2C1_SOURCE();
  409. #if defined(I2C3)
  410. /* Get the I2C3 clock source -----------------------------------------------*/
  411. PeriphClkInit->I2c3ClockSelection = __HAL_RCC_GET_I2C3_SOURCE();
  412. #endif
  413. /* Get the LPTIM1 clock source ---------------------------------------------*/
  414. PeriphClkInit->Lptim1ClockSelection = __HAL_RCC_GET_LPTIM1_SOURCE();
  415. /* Get the LPTIM2 clock source ---------------------------------------------*/
  416. PeriphClkInit->Lptim2ClockSelection = __HAL_RCC_GET_LPTIM2_SOURCE();
  417. #if defined(SAI1)
  418. /* Get the SAI1 clock source -----------------------------------------------*/
  419. PeriphClkInit->Sai1ClockSelection = __HAL_RCC_GET_SAI1_SOURCE();
  420. #endif
  421. /* Get the RTC clock source ------------------------------------------------*/
  422. PeriphClkInit->RTCClockSelection = __HAL_RCC_GET_RTC_SOURCE();
  423. #if defined(USB)
  424. /* Get the USB clock source ------------------------------------------------*/
  425. PeriphClkInit->UsbClockSelection = __HAL_RCC_GET_USB_SOURCE();
  426. #endif
  427. /* Get the RNG clock source ------------------------------------------------*/
  428. PeriphClkInit->RngClockSelection = HAL_RCCEx_GetRngCLKSource();
  429. /* Get the ADC clock source ------------------------------------------------*/
  430. PeriphClkInit->AdcClockSelection = __HAL_RCC_GET_ADC_SOURCE();
  431. /* Get the RFWKP clock source ----------------------------------------------*/
  432. PeriphClkInit->RFWakeUpClockSelection = __HAL_RCC_GET_RFWAKEUP_SOURCE();
  433. #if defined(RCC_SMPS_SUPPORT)
  434. /* Get the SMPS clock division factor --------------------------------------*/
  435. PeriphClkInit->SmpsDivSelection = __HAL_RCC_GET_SMPS_DIV();
  436. /* Get the SMPS clock source -----------------------------------------------*/
  437. PeriphClkInit->SmpsClockSelection = __HAL_RCC_GET_SMPS_SOURCE();
  438. #endif
  439. }
  440. /**
  441. * @brief Return the peripheral clock frequency for peripherals with clock source
  442. * @note Return 0 if peripheral clock identifier not managed by this API
  443. * @param PeriphClk Peripheral clock identifier
  444. * This parameter can be one of the following values:
  445. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  446. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  447. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  448. * @arg @ref RCC_PERIPHCLK_I2C3 I2C3 peripheral clock
  449. * @arg @ref RCC_PERIPHCLK_LPTIM1 LPTIM1 peripheral clock
  450. * @arg @ref RCC_PERIPHCLK_LPTIM2 LPTIM2 peripheral clock
  451. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock
  452. * @arg @ref RCC_PERIPHCLK_RNG RNG peripheral clock
  453. * @arg @ref RCC_PERIPHCLK_SAI1 SAI1 peripheral clock
  454. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  455. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  456. * @arg @ref RCC_PERIPHCLK_RFWAKEUP RFWKP peripheral clock
  457. * @arg @ref RCC_PERIPHCLK_SMPS SMPS peripheral clock
  458. * @retval Frequency in Hz
  459. */
  460. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  461. {
  462. uint32_t frequency = 0U;
  463. #if defined(RCC_SMPS_SUPPORT)
  464. uint32_t smps_prescaler_index = ((LL_RCC_GetSMPSPrescaler()) >> RCC_SMPSCR_SMPSDIV_Pos);
  465. #endif
  466. /* Check the parameters */
  467. assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
  468. if (PeriphClk == RCC_PERIPHCLK_RTC)
  469. {
  470. uint32_t rtcClockSource = LL_RCC_GetRTCClockSource();
  471. if (rtcClockSource == LL_RCC_RTC_CLKSOURCE_LSE) /* LSE clock used as RTC clock source */
  472. {
  473. if (LL_RCC_LSE_IsReady() == 1U)
  474. {
  475. frequency = LSE_VALUE;
  476. }
  477. else
  478. {
  479. /* Nothing to do as frequency already initialized to 0U */
  480. }
  481. }
  482. else if (rtcClockSource == LL_RCC_RTC_CLKSOURCE_LSI) /* LSI clock used as RTC clock source */
  483. {
  484. const uint32_t temp_lsi1ready = LL_RCC_LSI1_IsReady();
  485. const uint32_t temp_lsi2ready = LL_RCC_LSI2_IsReady();
  486. if ((temp_lsi1ready == 1U) || (temp_lsi2ready == 1U))
  487. {
  488. frequency = LSI_VALUE;
  489. }
  490. else
  491. {
  492. /* Nothing to do as frequency already initialized to 0U */
  493. }
  494. }
  495. else if (rtcClockSource == LL_RCC_RTC_CLKSOURCE_HSE_DIV32) /* HSE clock used as RTC clock source */
  496. {
  497. frequency = HSE_VALUE / 32U;
  498. }
  499. else /* No clock used as RTC clock source */
  500. {
  501. /* Nothing to do as frequency already initialized to 0U */
  502. }
  503. }
  504. #if defined(SAI1)
  505. else if (PeriphClk == RCC_PERIPHCLK_SAI1)
  506. {
  507. switch (LL_RCC_GetSAIClockSource(LL_RCC_SAI1_CLKSOURCE))
  508. {
  509. case LL_RCC_SAI1_CLKSOURCE_HSI: /* HSI clock used as SAI1 clock source */
  510. if (LL_RCC_HSI_IsReady() == 1U)
  511. {
  512. frequency = HSI_VALUE;
  513. }
  514. else
  515. {
  516. /* Nothing to do as frequency already initialized to 0U */
  517. }
  518. break;
  519. #if defined(SAI1)
  520. case LL_RCC_SAI1_CLKSOURCE_PLLSAI1: /* PLLSAI1 clock used as SAI1 clock source */
  521. if (LL_RCC_PLLSAI1_IsReady() == 1U)
  522. {
  523. frequency = RCC_PLLSAI1_GetFreqDomain_P();
  524. }
  525. else
  526. {
  527. /* Nothing to do as frequency already initialized to 0U */
  528. }
  529. break;
  530. #endif
  531. case LL_RCC_SAI1_CLKSOURCE_PLL: /* PLL clock used as SAI1 clock source */
  532. if (LL_RCC_PLL_IsReady() == 1U)
  533. {
  534. frequency = RCC_PLL_GetFreqDomain_P();
  535. }
  536. else
  537. {
  538. /* Nothing to do as frequency already initialized to 0U */
  539. }
  540. break;
  541. default: /* External input clock used as SAI1 clock source */
  542. frequency = EXTERNAL_SAI1_CLOCK_VALUE;
  543. break;
  544. }
  545. }
  546. #endif
  547. else if (PeriphClk == RCC_PERIPHCLK_RNG)
  548. {
  549. uint32_t rngClockSource = HAL_RCCEx_GetRngCLKSource();
  550. if (rngClockSource == RCC_RNGCLKSOURCE_LSI) /* LSI clock used as RNG clock source */
  551. {
  552. const uint32_t temp_lsi1ready = LL_RCC_LSI1_IsReady();
  553. const uint32_t temp_lsi2ready = LL_RCC_LSI2_IsReady();
  554. if ((temp_lsi1ready == 1U) || (temp_lsi2ready == 1U))
  555. {
  556. frequency = LSI_VALUE;
  557. }
  558. else
  559. {
  560. /* Nothing to do as frequency already initialized to 0U */
  561. }
  562. }
  563. else if (rngClockSource == RCC_RNGCLKSOURCE_LSE) /* LSE clock used as RNG clock source */
  564. {
  565. if (LL_RCC_LSE_IsReady() == 1U)
  566. {
  567. frequency = LSE_VALUE;
  568. }
  569. else
  570. {
  571. /* Nothing to do as frequency already initialized to 0U */
  572. }
  573. }
  574. else if (rngClockSource == RCC_RNGCLKSOURCE_PLL) /* PLL clock divided by 3 used as RNG clock source */
  575. {
  576. if (LL_RCC_PLL_IsReady() == 1U)
  577. {
  578. frequency = (RCC_PLL_GetFreqDomain_Q() / 3U);
  579. }
  580. else
  581. {
  582. /* Nothing to do as frequency already initialized to 0U */
  583. }
  584. }
  585. else if (rngClockSource == RCC_RNGCLKSOURCE_MSI) /* MSI clock divided by 3 used as RNG clock source */
  586. {
  587. if (LL_RCC_MSI_IsReady() == 1U)
  588. {
  589. frequency = (__LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange()) / 3U);
  590. }
  591. else
  592. {
  593. /* Nothing to do as frequency already initialized to 0U */
  594. }
  595. }
  596. else /* HSI48 clock divided by 3 used as RNG clock source */
  597. {
  598. #if defined(RCC_HSI48_SUPPORT)
  599. if (LL_RCC_HSI48_IsReady() == 1U)
  600. {
  601. frequency = HSI48_VALUE / 3U;
  602. }
  603. else
  604. {
  605. /* Nothing to do as frequency already initialized to 0U */
  606. }
  607. #else
  608. /* Nothing to do as frequency already initialized to 0U */
  609. #endif
  610. }
  611. }
  612. #if defined(USB)
  613. else if (PeriphClk == RCC_PERIPHCLK_USB)
  614. {
  615. switch (LL_RCC_GetUSBClockSource(LL_RCC_USB_CLKSOURCE))
  616. {
  617. #if defined(SAI1)
  618. case LL_RCC_USB_CLKSOURCE_PLLSAI1: /* PLLSAI1 clock used as USB clock source */
  619. if (LL_RCC_PLLSAI1_IsReady() == 1U)
  620. {
  621. frequency = RCC_PLLSAI1_GetFreqDomain_Q();
  622. }
  623. else
  624. {
  625. /* Nothing to do as frequency already initialized to 0U */
  626. }
  627. break;
  628. #endif
  629. case LL_RCC_USB_CLKSOURCE_PLL: /* PLL clock used as USB clock source */
  630. if (LL_RCC_PLL_IsReady() == 1U)
  631. {
  632. frequency = RCC_PLL_GetFreqDomain_Q();
  633. }
  634. else
  635. {
  636. /* Nothing to do as frequency already initialized to 0U */
  637. }
  638. break;
  639. case LL_RCC_USB_CLKSOURCE_MSI: /* MSI clock used as USB clock source */
  640. if (LL_RCC_MSI_IsReady() == 1U)
  641. {
  642. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  643. }
  644. else
  645. {
  646. /* Nothing to do as frequency already initialized to 0U */
  647. }
  648. break;
  649. default: /* HSI48 clock used as USB clock source */
  650. if (LL_RCC_HSI48_IsReady() == 1U)
  651. {
  652. frequency = HSI48_VALUE;
  653. }
  654. else
  655. {
  656. /* Nothing to do as frequency already initialized to 0U */
  657. }
  658. break;
  659. }
  660. }
  661. #endif
  662. else if (PeriphClk == RCC_PERIPHCLK_USART1)
  663. {
  664. switch (LL_RCC_GetUSARTClockSource(LL_RCC_USART1_CLKSOURCE))
  665. {
  666. case LL_RCC_USART1_CLKSOURCE_SYSCLK: /* USART1 Clock is System Clock */
  667. frequency = HAL_RCC_GetSysClockFreq();
  668. break;
  669. case LL_RCC_USART1_CLKSOURCE_HSI: /* USART1 Clock is HSI Osc. */
  670. if (LL_RCC_HSI_IsReady() == 1U)
  671. {
  672. frequency = HSI_VALUE;
  673. }
  674. else
  675. {
  676. /* Nothing to do as frequency already initialized to 0U */
  677. }
  678. break;
  679. case LL_RCC_USART1_CLKSOURCE_LSE: /* USART1 Clock is LSE Osc. */
  680. if (LL_RCC_LSE_IsReady() == 1U)
  681. {
  682. frequency = LSE_VALUE;
  683. }
  684. else
  685. {
  686. /* Nothing to do as frequency already initialized to 0U */
  687. }
  688. break;
  689. default: /* USART1 Clock is PCLK2 */
  690. frequency = __LL_RCC_CALC_PCLK2_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(), \
  691. LL_RCC_GetAHBPrescaler()), LL_RCC_GetAPB2Prescaler());
  692. break;
  693. }
  694. }
  695. #if defined(LPUART1)
  696. else if (PeriphClk == RCC_PERIPHCLK_LPUART1)
  697. {
  698. switch (LL_RCC_GetLPUARTClockSource(LL_RCC_LPUART1_CLKSOURCE))
  699. {
  700. case LL_RCC_LPUART1_CLKSOURCE_SYSCLK: /* LPUART1 Clock is System Clock */
  701. frequency = HAL_RCC_GetSysClockFreq();
  702. break;
  703. case LL_RCC_LPUART1_CLKSOURCE_HSI: /* LPUART1 Clock is HSI Osc. */
  704. if (LL_RCC_HSI_IsReady() == 1U)
  705. {
  706. frequency = HSI_VALUE;
  707. }
  708. else
  709. {
  710. /* Nothing to do as frequency already initialized to 0U */
  711. }
  712. break;
  713. case LL_RCC_LPUART1_CLKSOURCE_LSE: /* LPUART1 Clock is LSE Osc. */
  714. if (LL_RCC_LSE_IsReady() == 1U)
  715. {
  716. frequency = LSE_VALUE;
  717. }
  718. else
  719. {
  720. /* Nothing to do as frequency already initialized to 0U */
  721. }
  722. break;
  723. default: /* LPUART1 Clock is PCLK1 */
  724. frequency = __LL_RCC_CALC_PCLK1_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(), \
  725. LL_RCC_GetAHBPrescaler()), LL_RCC_GetAPB1Prescaler());
  726. break;
  727. }
  728. }
  729. #endif
  730. else if (PeriphClk == RCC_PERIPHCLK_ADC)
  731. {
  732. switch (LL_RCC_GetADCClockSource(LL_RCC_ADC_CLKSOURCE))
  733. {
  734. #if defined(STM32WB55xx) || defined (STM32WB5Mxx) || defined(STM32WB35xx)
  735. case LL_RCC_ADC_CLKSOURCE_PLLSAI1: /* PLLSAI1 clock used as ADC clock source */
  736. if (LL_RCC_PLLSAI1_IsReady() == 1U)
  737. {
  738. frequency = RCC_PLLSAI1_GetFreqDomain_R();
  739. }
  740. else
  741. {
  742. /* Nothing to do as frequency already initialized to 0U */
  743. }
  744. break;
  745. #endif
  746. case LL_RCC_ADC_CLKSOURCE_SYSCLK: /* SYSCLK clock used as ADC clock source */
  747. frequency = HAL_RCC_GetSysClockFreq();
  748. break;
  749. case LL_RCC_ADC_CLKSOURCE_PLL: /* PLL clock used as ADC clock source */
  750. if (LL_RCC_PLL_IsReady() == 1U)
  751. {
  752. frequency = RCC_PLL_GetFreqDomain_P();
  753. }
  754. else
  755. {
  756. /* Nothing to do as frequency already initialized to 0U */
  757. }
  758. break;
  759. default: /* No clock used as ADC clock source */
  760. break;
  761. }
  762. }
  763. else if (PeriphClk == RCC_PERIPHCLK_I2C1)
  764. {
  765. switch (LL_RCC_GetI2CClockSource(LL_RCC_I2C1_CLKSOURCE))
  766. {
  767. case LL_RCC_I2C1_CLKSOURCE_SYSCLK: /* I2C1 Clock is System Clock */
  768. frequency = HAL_RCC_GetSysClockFreq();
  769. break;
  770. case LL_RCC_I2C1_CLKSOURCE_HSI: /* I2C1 Clock is HSI Osc. */
  771. if (LL_RCC_HSI_IsReady() == 1U)
  772. {
  773. frequency = HSI_VALUE;
  774. }
  775. else
  776. {
  777. /* Nothing to do as frequency already initialized to 0U */
  778. }
  779. break;
  780. default: /* I2C1 Clock is PCLK1 */
  781. frequency = __LL_RCC_CALC_PCLK1_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(), \
  782. LL_RCC_GetAHBPrescaler()), LL_RCC_GetAPB1Prescaler());
  783. break;
  784. }
  785. }
  786. #if defined(I2C3)
  787. else if (PeriphClk == RCC_PERIPHCLK_I2C3)
  788. {
  789. switch (LL_RCC_GetI2CClockSource(LL_RCC_I2C3_CLKSOURCE))
  790. {
  791. case LL_RCC_I2C3_CLKSOURCE_SYSCLK: /* I2C3 Clock is System Clock */
  792. frequency = HAL_RCC_GetSysClockFreq();
  793. break;
  794. case LL_RCC_I2C3_CLKSOURCE_HSI: /* I2C3 Clock is HSI Osc. */
  795. if (LL_RCC_HSI_IsReady() == 1U)
  796. {
  797. frequency = HSI_VALUE;
  798. }
  799. else
  800. {
  801. /* Nothing to do as frequency already initialized to 0U */
  802. }
  803. break;
  804. default: /* I2C3 Clock is PCLK1 */
  805. frequency = __LL_RCC_CALC_PCLK1_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(), \
  806. LL_RCC_GetAHBPrescaler()), LL_RCC_GetAPB1Prescaler());
  807. break;
  808. }
  809. }
  810. #endif
  811. else if (PeriphClk == RCC_PERIPHCLK_LPTIM1)
  812. {
  813. uint32_t lptimClockSource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
  814. if (lptimClockSource == LL_RCC_LPTIM1_CLKSOURCE_LSI) /* LPTIM1 Clock is LSI Osc. */
  815. {
  816. const uint32_t temp_lsi1ready = LL_RCC_LSI1_IsReady();
  817. const uint32_t temp_lsi2ready = LL_RCC_LSI2_IsReady();
  818. if ((temp_lsi1ready == 1U) || (temp_lsi2ready == 1U))
  819. {
  820. frequency = LSI_VALUE;
  821. }
  822. else
  823. {
  824. /* Nothing to do as frequency already initialized to 0U */
  825. }
  826. }
  827. else if (lptimClockSource == LL_RCC_LPTIM1_CLKSOURCE_HSI) /* LPTIM1 Clock is HSI Osc. */
  828. {
  829. if (LL_RCC_HSI_IsReady() == 1U)
  830. {
  831. frequency = HSI_VALUE;
  832. }
  833. else
  834. {
  835. /* Nothing to do as frequency already initialized to 0U */
  836. }
  837. }
  838. else if (lptimClockSource == LL_RCC_LPTIM1_CLKSOURCE_LSE) /* LPTIM1 Clock is LSE Osc. */
  839. {
  840. if (LL_RCC_LSE_IsReady() == 1U)
  841. {
  842. frequency = LSE_VALUE;
  843. }
  844. else
  845. {
  846. /* Nothing to do as frequency already initialized to 0U */
  847. }
  848. }
  849. else /* LPTIM1 Clock is PCLK1 */
  850. {
  851. frequency = __LL_RCC_CALC_PCLK1_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(), LL_RCC_GetAHBPrescaler()), LL_RCC_GetAPB1Prescaler());
  852. }
  853. }
  854. else if (PeriphClk == RCC_PERIPHCLK_LPTIM2)
  855. {
  856. uint32_t lptimClockSource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE);
  857. if (lptimClockSource == LL_RCC_LPTIM2_CLKSOURCE_LSI) /* LPTIM2 Clock is LSI Osc. */
  858. {
  859. const uint32_t temp_lsi1ready = LL_RCC_LSI1_IsReady();
  860. const uint32_t temp_lsi2ready = LL_RCC_LSI2_IsReady();
  861. if ((temp_lsi1ready == 1U) || (temp_lsi2ready == 1U))
  862. {
  863. frequency = LSI_VALUE;
  864. }
  865. else
  866. {
  867. /* Nothing to do as frequency already initialized to 0U */
  868. }
  869. }
  870. else if (lptimClockSource == LL_RCC_LPTIM2_CLKSOURCE_HSI) /* LPTIM2 Clock is HSI Osc. */
  871. {
  872. if (LL_RCC_HSI_IsReady() == 1U)
  873. {
  874. frequency = HSI_VALUE;
  875. }
  876. else
  877. {
  878. /* Nothing to do as frequency already initialized to 0U */
  879. }
  880. }
  881. else if (lptimClockSource == LL_RCC_LPTIM2_CLKSOURCE_LSE) /* LPTIM2 Clock is LSE Osc. */
  882. {
  883. if (LL_RCC_LSE_IsReady() == 1U)
  884. {
  885. frequency = LSE_VALUE;
  886. }
  887. else
  888. {
  889. /* Nothing to do as frequency already initialized to 0U */
  890. }
  891. }
  892. else /* LPTIM2 Clock is PCLK1 */
  893. {
  894. frequency = __LL_RCC_CALC_PCLK1_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(), LL_RCC_GetAHBPrescaler()), LL_RCC_GetAPB1Prescaler());
  895. }
  896. }
  897. else if (PeriphClk == RCC_PERIPHCLK_RFWAKEUP)
  898. {
  899. uint32_t rfwkpClockSource = LL_RCC_GetRFWKPClockSource();
  900. if (rfwkpClockSource == LL_RCC_RFWKP_CLKSOURCE_LSE) /* LSE clock used as RF Wakeup clock source */
  901. {
  902. if (LL_RCC_LSE_IsReady() == 1U)
  903. {
  904. frequency = LSE_VALUE;
  905. }
  906. else
  907. {
  908. /* Nothing to do as frequency already initialized to 0U */
  909. }
  910. }
  911. else if (rfwkpClockSource == LL_RCC_RFWKP_CLKSOURCE_HSE_DIV1024) /* HSE clock used as RF Wakeup clock source */
  912. {
  913. frequency = HSE_VALUE / 1024U;
  914. }
  915. else /* No clock used as RF Wakeup clock source */
  916. {
  917. /* Nothing to do as frequency already initialized to 0U */
  918. }
  919. }
  920. #if defined(RCC_SMPS_SUPPORT)
  921. else if (PeriphClk == RCC_PERIPHCLK_SMPS)
  922. {
  923. uint32_t smpsClockSource = LL_RCC_GetSMPSClockSource();
  924. if (smpsClockSource == LL_RCC_SMPS_CLKSOURCE_STATUS_HSI) /* SMPS Clock source is HSI Osc. */
  925. {
  926. if (LL_RCC_HSI_IsReady() == 1U)
  927. {
  928. frequency = HSI_VALUE / SmpsPrescalerTable[smps_prescaler_index][0];
  929. frequency = frequency >> 1U; /* Systematic Div by 2 */
  930. }
  931. else
  932. {
  933. /* Nothing to do as frequency already initialized to 0U */
  934. }
  935. }
  936. else if (smpsClockSource == LL_RCC_SMPS_CLKSOURCE_STATUS_HSE) /* SMPS Clock source is HSE Osc. */
  937. {
  938. if (LL_RCC_HSE_IsReady() == 1U)
  939. {
  940. frequency = HSE_VALUE / SmpsPrescalerTable[smps_prescaler_index][5];
  941. frequency = frequency >> 1U; /* Systematic Div by 2 */
  942. }
  943. else
  944. {
  945. /* Nothing to do as frequency already initialized to 0U */
  946. }
  947. }
  948. else if (smpsClockSource == LL_RCC_SMPS_CLKSOURCE_STATUS_MSI) /* SMPS Clock source is MSI Osc. */
  949. {
  950. switch (LL_RCC_MSI_GetRange())
  951. {
  952. case LL_RCC_MSIRANGE_8:
  953. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSIRANGE_8) / SmpsPrescalerTable[smps_prescaler_index][4];
  954. break;
  955. case LL_RCC_MSIRANGE_9:
  956. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSIRANGE_9) / SmpsPrescalerTable[smps_prescaler_index][3];
  957. break;
  958. case LL_RCC_MSIRANGE_10:
  959. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSIRANGE_10) / SmpsPrescalerTable[smps_prescaler_index][2];
  960. break;
  961. case LL_RCC_MSIRANGE_11:
  962. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSIRANGE_11) / SmpsPrescalerTable[smps_prescaler_index][1];
  963. break;
  964. default:
  965. break;
  966. }
  967. frequency = frequency >> 1U; /* Systematic Div by 2 */
  968. }
  969. else /* SMPS has no Clock */
  970. {
  971. /* Nothing to do as frequency already initialized to 0U */
  972. }
  973. }
  974. #endif
  975. return (frequency);
  976. }
  977. /**
  978. * @brief Return the RNG clock source
  979. * @retval The RNG clock source can be one of the following values:
  980. * @arg @ref RCC_RNGCLKSOURCE_HSI48 HSI48 clock divided by 3 selected as RNG clock
  981. * @arg @ref RCC_RNGCLKSOURCE_PLL PLL "Q" clock divided by 3 selected as RNG clock
  982. * @arg @ref RCC_RNGCLKSOURCE_MSI MSI clock divided by 3 selected as RNG clock
  983. * @arg @ref RCC_RNGCLKSOURCE_LSI LSI clock selected as RNG clock
  984. * @arg @ref RCC_RNGCLKSOURCE_LSE LSE clock selected as RNG clock
  985. */
  986. uint32_t HAL_RCCEx_GetRngCLKSource(void)
  987. {
  988. uint32_t rng_clock_source = LL_RCC_GetRNGClockSource(LL_RCC_RNG_CLKSOURCE);
  989. uint32_t clk48_clock_source;
  990. /* RNG clock source originates from 48 MHz RC oscillator */
  991. if (rng_clock_source == RCC_RNGCLKSOURCE_CLK48)
  992. {
  993. clk48_clock_source = LL_RCC_GetCLK48ClockSource(LL_RCC_CLK48_CLKSOURCE);
  994. rng_clock_source = (CLK48_MASK | clk48_clock_source);
  995. }
  996. return rng_clock_source;
  997. }
  998. /**
  999. * @}
  1000. */
  1001. /** @defgroup RCCEx_Exported_Functions_Group2 Extended Clock management functions
  1002. * @brief Extended Clock management functions
  1003. *
  1004. @verbatim
  1005. ===============================================================================
  1006. ##### Extended clock management functions #####
  1007. ===============================================================================
  1008. [..]
  1009. This subsection provides a set of functions allowing to control the
  1010. activation or deactivation of MSI PLL-mode, PLLSAI1, PLLSAI12, LSE CSS,
  1011. Low speed clock output and clock after wake-up from STOP mode.
  1012. @endverbatim
  1013. * @{
  1014. */
  1015. #if defined(SAI1)
  1016. /**
  1017. * @brief Enable PLLSAI1.
  1018. * @param PLLSAI1Init pointer to an RCC_PLLSAI1InitTypeDef structure that
  1019. * contains the configuration information for the PLLSAI1
  1020. * @retval HAL status
  1021. */
  1022. HAL_StatusTypeDef HAL_RCCEx_EnablePLLSAI1(RCC_PLLSAI1InitTypeDef *PLLSAI1Init)
  1023. {
  1024. uint32_t tickstart;
  1025. HAL_StatusTypeDef status = HAL_OK;
  1026. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  1027. assert_param(IS_RCC_PLLN_VALUE(PLLSAI1Init->PLLN));
  1028. assert_param(IS_RCC_PLLP_VALUE(PLLSAI1Init->PLLP));
  1029. assert_param(IS_RCC_PLLQ_VALUE(PLLSAI1Init->PLLQ));
  1030. assert_param(IS_RCC_PLLR_VALUE(PLLSAI1Init->PLLR));
  1031. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PLLSAI1Init->PLLSAI1ClockOut));
  1032. /* Disable the PLLSAI1 */
  1033. __HAL_RCC_PLLSAI1_DISABLE();
  1034. /* Get Start Tick*/
  1035. tickstart = HAL_GetTick();
  1036. /* Wait till PLLSAI1 is ready to be updated */
  1037. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  1038. {
  1039. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1040. {
  1041. status = HAL_TIMEOUT;
  1042. break;
  1043. }
  1044. }
  1045. if (status == HAL_OK)
  1046. {
  1047. /* Configure the PLLSAI1 Multiplication factor N */
  1048. /* Configure the PLLSAI1 Division factors P, Q and R */
  1049. __HAL_RCC_PLLSAI1_CONFIG(PLLSAI1Init->PLLN, PLLSAI1Init->PLLP, PLLSAI1Init->PLLQ, PLLSAI1Init->PLLR);
  1050. /* Configure the PLLSAI1 Clock output(s) */
  1051. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PLLSAI1Init->PLLSAI1ClockOut);
  1052. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  1053. __HAL_RCC_PLLSAI1_ENABLE();
  1054. /* Get Start Tick*/
  1055. tickstart = HAL_GetTick();
  1056. /* Wait till PLLSAI1 is ready */
  1057. while (LL_RCC_PLLSAI1_IsReady() != 1U)
  1058. {
  1059. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1060. {
  1061. status = HAL_TIMEOUT;
  1062. break;
  1063. }
  1064. }
  1065. }
  1066. return status;
  1067. }
  1068. /**
  1069. * @brief Disable PLLSAI1.
  1070. * @retval HAL status
  1071. */
  1072. HAL_StatusTypeDef HAL_RCCEx_DisablePLLSAI1(void)
  1073. {
  1074. uint32_t tickstart;
  1075. HAL_StatusTypeDef status = HAL_OK;
  1076. /* Disable the PLLSAI1 */
  1077. __HAL_RCC_PLLSAI1_DISABLE();
  1078. /* Get Start Tick*/
  1079. tickstart = HAL_GetTick();
  1080. /* Wait till PLLSAI1 is ready */
  1081. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  1082. {
  1083. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1084. {
  1085. status = HAL_TIMEOUT;
  1086. break;
  1087. }
  1088. }
  1089. /* Disable the PLLSAI1 Clock outputs */
  1090. __HAL_RCC_PLLSAI1CLKOUT_DISABLE(RCC_PLLSAI1_SAI1CLK | RCC_PLLSAI1_USBCLK | RCC_PLLSAI1_ADCCLK);
  1091. return status;
  1092. }
  1093. #endif
  1094. /***********************************************************************************************/
  1095. /**
  1096. * @brief Configure the oscillator clock source for wakeup from Stop and CSS backup clock.
  1097. * @param WakeUpClk Wakeup clock
  1098. * This parameter can be one of the following values:
  1099. * @arg @ref RCC_STOP_WAKEUPCLOCK_MSI MSI oscillator selection
  1100. * @arg @ref RCC_STOP_WAKEUPCLOCK_HSI HSI oscillator selection
  1101. * @note This function shall not be called after the Clock Security System on HSE has been
  1102. * enabled.
  1103. * @retval None
  1104. */
  1105. void HAL_RCCEx_WakeUpStopCLKConfig(uint32_t WakeUpClk)
  1106. {
  1107. assert_param(IS_RCC_STOP_WAKEUPCLOCK(WakeUpClk));
  1108. __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(WakeUpClk);
  1109. }
  1110. /**
  1111. * @brief Enable the LSE Clock Security System.
  1112. * @note Prior to enable the LSE Clock Security System, LSE oscillator is to be enabled
  1113. * with HAL_RCC_OscConfig() and the LSE oscillator clock is to be selected as RTC
  1114. * clock with HAL_RCCEx_PeriphCLKConfig().
  1115. * @retval None
  1116. */
  1117. void HAL_RCCEx_EnableLSECSS(void)
  1118. {
  1119. LL_RCC_LSE_EnableCSS();
  1120. }
  1121. /**
  1122. * @brief Disable the LSE Clock Security System.
  1123. * @note LSE Clock Security System can only be disabled after a LSE failure detection.
  1124. * @retval None
  1125. */
  1126. void HAL_RCCEx_DisableLSECSS(void)
  1127. {
  1128. LL_RCC_LSE_DisableCSS();
  1129. /* Disable LSE CSS IT if any */
  1130. __HAL_RCC_DISABLE_IT(RCC_IT_LSECSS);
  1131. }
  1132. /**
  1133. * @brief Enable the LSE Clock Security System Interrupt & corresponding EXTI line.
  1134. * @note LSE Clock Security System Interrupt is mapped on RTC EXTI line 18
  1135. * @retval None
  1136. */
  1137. void HAL_RCCEx_EnableLSECSS_IT(void)
  1138. {
  1139. /* Enable LSE CSS */
  1140. LL_RCC_LSE_EnableCSS();
  1141. /* Enable LSE CSS IT */
  1142. __HAL_RCC_ENABLE_IT(RCC_IT_LSECSS);
  1143. /* Enable IT on EXTI Line 18 */
  1144. __HAL_RCC_LSECSS_EXTI_ENABLE_IT();
  1145. __HAL_RCC_LSECSS_EXTI_ENABLE_RISING_EDGE();
  1146. }
  1147. /**
  1148. * @brief Handle the RCC LSE Clock Security System interrupt request.
  1149. * @retval None
  1150. */
  1151. void HAL_RCCEx_LSECSS_IRQHandler(void)
  1152. {
  1153. /* Check RCC LSE CSSF flag */
  1154. if (__HAL_RCC_GET_IT(RCC_IT_LSECSS))
  1155. {
  1156. /* RCC LSE Clock Security System interrupt user callback */
  1157. HAL_RCCEx_LSECSS_Callback();
  1158. /* Clear RCC LSE CSS pending bit */
  1159. __HAL_RCC_CLEAR_IT(RCC_IT_LSECSS);
  1160. }
  1161. }
  1162. /**
  1163. * @brief RCCEx LSE Clock Security System interrupt callback.
  1164. * @retval none
  1165. */
  1166. __weak void HAL_RCCEx_LSECSS_Callback(void)
  1167. {
  1168. /* NOTE : This function should not be modified, when the callback is needed,
  1169. the HAL_RCCEx_LSECSS_Callback should be implemented in the user file
  1170. */
  1171. }
  1172. /**
  1173. * @brief Select the clock source to output on LSCO1 pin(PA2) or LSC02 pin (PH3) or LSCO3 pin (PC12).
  1174. * @note PA2, PH3 or PC12 should be configured in alternate function mode.
  1175. * @param RCC_LSCOx specifies the output direction for the clock source.
  1176. * @arg @ref RCC_LSCO1 Clock source to output on LSCO1 pin(PA2)
  1177. * @arg @ref RCC_LSCO2 Clock source to output on LSCO2 pin(PH3)
  1178. * @arg @ref RCC_LSCO3 Clock source to output on LSCO3 pin(PC12)
  1179. * @param RCC_LSCOSource specifies the clock source to output.
  1180. * This parameter can be one of the following values:
  1181. * @arg @ref RCC_LSCOSOURCE_LSI LSI clock selected as LSCO source
  1182. * @arg @ref RCC_LSCOSOURCE_LSE LSE clock selected as LSCO source
  1183. * @retval None
  1184. * @note LSCO should be disable with @ref HAL_RCCEx_DisableLSCO
  1185. */
  1186. void HAL_RCCEx_LSCOConfig(uint32_t RCC_LSCOx, uint32_t RCC_LSCOSource)
  1187. {
  1188. GPIO_InitTypeDef GPIO_InitStruct;
  1189. FlagStatus backupchanged;
  1190. /* Check the parameters */
  1191. assert_param(IS_RCC_LSCO(RCC_LSCOx));
  1192. assert_param(IS_RCC_LSCOSOURCE(RCC_LSCOSource));
  1193. /* Common GPIO init parameters */
  1194. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1195. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1196. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1197. /* RCC_LSCO1 */
  1198. if (RCC_LSCOx == RCC_LSCO1)
  1199. {
  1200. /* LSCO1 Clock Enable */
  1201. __LSCO1_CLK_ENABLE();
  1202. /* Configue the LSCO1 pin in alternate function mode */
  1203. GPIO_InitStruct.Pin = LSCO1_PIN;
  1204. GPIO_InitStruct.Alternate = GPIO_AF0_LSCO;
  1205. HAL_GPIO_Init(LSCO1_GPIO_PORT, &GPIO_InitStruct);
  1206. }
  1207. else if (RCC_LSCOx == RCC_LSCO2)
  1208. {
  1209. /* LSCO2 Clock Enable */
  1210. __LSCO2_CLK_ENABLE();
  1211. /* Configue the LSCO2 pin in alternate function mode */
  1212. GPIO_InitStruct.Pin = LSCO2_PIN;
  1213. GPIO_InitStruct.Alternate = GPIO_AF0_LSCO;
  1214. HAL_GPIO_Init(LSCO2_GPIO_PORT, &GPIO_InitStruct);
  1215. }
  1216. #if defined(RCC_LSCO3_SUPPORT)
  1217. else if (RCC_LSCOx == RCC_LSCO3)
  1218. {
  1219. /* LSCO3 Clock Enable */
  1220. __LSCO3_CLK_ENABLE();
  1221. /* Configue the LSCO3 pin in alternate function mode */
  1222. GPIO_InitStruct.Pin = LSCO3_PIN;
  1223. GPIO_InitStruct.Alternate = GPIO_AF6_LSCO;
  1224. HAL_GPIO_Init(LSCO3_GPIO_PORT, &GPIO_InitStruct);
  1225. }
  1226. #endif
  1227. else
  1228. {
  1229. ;
  1230. }
  1231. /* Update LSCOSEL clock source in Backup Domain control register */
  1232. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  1233. {
  1234. HAL_PWR_EnableBkUpAccess();
  1235. backupchanged = SET;
  1236. }
  1237. else
  1238. {
  1239. backupchanged = RESET;
  1240. }
  1241. MODIFY_REG(RCC->BDCR, RCC_BDCR_LSCOSEL | RCC_BDCR_LSCOEN, RCC_LSCOSource | RCC_BDCR_LSCOEN);
  1242. if (backupchanged == SET)
  1243. {
  1244. HAL_PWR_DisableBkUpAccess();
  1245. }
  1246. }
  1247. /**
  1248. * @brief Select the Low Speed clock source to output on LSCO pin (PA2).
  1249. * @param LSCOSource specifies the Low Speed clock source to output.
  1250. * This parameter can be one of the following values:
  1251. * @arg @ref RCC_LSCOSOURCE_LSI LSI clock selected as LSCO source
  1252. * @arg @ref RCC_LSCOSOURCE_LSE LSE clock selected as LSCO source
  1253. * @retval None
  1254. */
  1255. void HAL_RCCEx_EnableLSCO(uint32_t LSCOSource)
  1256. {
  1257. GPIO_InitTypeDef GPIO_InitStruct;
  1258. FlagStatus backupchanged;
  1259. /* Check the parameters */
  1260. assert_param(IS_RCC_LSCOSOURCE(LSCOSource));
  1261. /* LSCO Pin Clock Enable */
  1262. __LSCO1_CLK_ENABLE();
  1263. /* Configure the LSCO pin in analog mode */
  1264. GPIO_InitStruct.Pin = LSCO1_PIN;
  1265. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1266. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1267. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1268. GPIO_InitStruct.Alternate = GPIO_AF0_LSCO;
  1269. HAL_GPIO_Init(LSCO1_GPIO_PORT, &GPIO_InitStruct);
  1270. /* Update LSCOSEL clock source in Backup Domain control register */
  1271. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  1272. {
  1273. HAL_PWR_EnableBkUpAccess();
  1274. backupchanged = SET;
  1275. }
  1276. else
  1277. {
  1278. backupchanged = RESET;
  1279. }
  1280. MODIFY_REG(RCC->BDCR, RCC_BDCR_LSCOSEL | RCC_BDCR_LSCOEN, LSCOSource | RCC_BDCR_LSCOEN);
  1281. if (backupchanged == SET)
  1282. {
  1283. HAL_PWR_DisableBkUpAccess();
  1284. }
  1285. }
  1286. /**
  1287. * @brief Disable the Low Speed clock output.
  1288. * @retval None
  1289. */
  1290. void HAL_RCCEx_DisableLSCO(void)
  1291. {
  1292. FlagStatus backupchanged;
  1293. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  1294. {
  1295. /* Enable access to the backup domain */
  1296. HAL_PWR_EnableBkUpAccess();
  1297. backupchanged = SET;
  1298. }
  1299. else
  1300. {
  1301. backupchanged = RESET;
  1302. }
  1303. LL_RCC_LSCO_Disable();
  1304. /* Restore previous configuration */
  1305. if (backupchanged == SET)
  1306. {
  1307. /* Disable access to the backup domain */
  1308. HAL_PWR_DisableBkUpAccess();
  1309. }
  1310. }
  1311. /**
  1312. * @brief Enable the PLL-mode of the MSI.
  1313. * @note Prior to enable the PLL-mode of the MSI for automatic hardware
  1314. * calibration LSE oscillator is to be enabled with @ref HAL_RCC_OscConfig().
  1315. * @retval None
  1316. */
  1317. void HAL_RCCEx_EnableMSIPLLMode(void)
  1318. {
  1319. LL_RCC_MSI_EnablePLLMode() ;
  1320. }
  1321. /**
  1322. * @brief Disable the PLL-mode of the MSI.
  1323. * @note PLL-mode of the MSI is automatically reset when LSE oscillator is disabled.
  1324. * @retval None
  1325. */
  1326. void HAL_RCCEx_DisableMSIPLLMode(void)
  1327. {
  1328. LL_RCC_MSI_DisablePLLMode() ;
  1329. }
  1330. /**
  1331. * @brief Set trimming value
  1332. * @param OscillatorType Specifies the oscillator to be trimmed
  1333. * This parameter can be one of the following values:
  1334. * @arg @ref RCC_OSCILLATORTYPE_LSI2 LSI2 oscillator selected.
  1335. * When disabling and re-enabling the LSI2 there is no need for re-trimming
  1336. * Trimming is only needed once after a NRST reset.
  1337. * Trimming values comes from factory trimmed flash location (0x1FFF7548).
  1338. * @note The LSI2 oscillator must be disabled before calling this trimming function through @ref HAL_RCC_OscConfig
  1339. * @retval HAL status
  1340. */
  1341. HAL_StatusTypeDef HAL_RCCEx_TrimOsc(uint32_t OscillatorType)
  1342. {
  1343. #define FTLSI2TRIM (0xFUL)
  1344. HAL_StatusTypeDef status = HAL_OK;
  1345. assert_param(IS_RCC_TRIMOSC(OscillatorType));
  1346. if (OscillatorType == RCC_OSCILLATORTYPE_LSI2)
  1347. {
  1348. if (LL_RCC_LSI2_IsReady() == 1U)
  1349. {
  1350. status = HAL_ERROR;
  1351. }
  1352. else
  1353. {
  1354. /* Copy the LSI2 trimming information from the factory trimmed Flash location */
  1355. uint32_t factoryTrimming = ((*(uint32_t *)(0x1FFF7548)) & FTLSI2TRIM);
  1356. LL_RCC_LSI2_SetTrimming(factoryTrimming);
  1357. }
  1358. }
  1359. else
  1360. {
  1361. status = HAL_ERROR;
  1362. }
  1363. return status;
  1364. }
  1365. /**
  1366. * @}
  1367. */
  1368. #if defined(CRS)
  1369. /** @defgroup RCCEx_Exported_Functions_Group3 Extended Clock Recovery System Control functions
  1370. * @brief Extended Clock Recovery System Control functions
  1371. *
  1372. @verbatim
  1373. ===============================================================================
  1374. ##### Extended Clock Recovery System Control functions #####
  1375. ===============================================================================
  1376. [..]
  1377. For devices with Clock Recovery System feature (CRS), RCC Extention HAL driver can be used as follows:
  1378. (#) In System clock config, HSI48 needs to be enabled
  1379. (#) Enable CRS clock in IP MSP init which will use CRS functions
  1380. (#) Call CRS functions as follows:
  1381. (##) Prepare synchronization configuration necessary for HSI48 calibration
  1382. (+++) Default values can be set for frequency Error Measurement (reload and error limit)
  1383. and also HSI48 oscillator smooth trimming.
  1384. (+++) Macro __HAL_RCC_CRS_RELOADVALUE_CALCULATE can be also used to calculate
  1385. directly reload value with target and sychronization frequencies values
  1386. (##) Call function HAL_RCCEx_CRSConfig which
  1387. (+++) Resets CRS registers to their default values.
  1388. (+++) Configures CRS registers with synchronization configuration
  1389. (+++) Enables automatic calibration and frequency error counter feature
  1390. Note: When using USB LPM (Link Power Management) and the device is in Sleep mode, the
  1391. periodic USB SOF will not be generated by the host. No SYNC signal will therefore be
  1392. provided to the CRS to calibrate the HSI48 on the run. To guarantee the required clock
  1393. precision after waking up from Sleep mode, the LSE or reference clock on the GPIOs
  1394. should be used as SYNC signal.
  1395. (##) A polling function is provided to wait for complete synchronization
  1396. (+++) Call function HAL_RCCEx_CRSWaitSynchronization()
  1397. (+++) According to CRS status, user can decide to adjust again the calibration or continue
  1398. application if synchronization is OK
  1399. (#) User can retrieve information related to synchronization in calling function
  1400. HAL_RCCEx_CRSGetSynchronizationInfo()
  1401. (#) Regarding synchronization status and synchronization information, user can try a new calibration
  1402. in changing synchronization configuration and call again HAL_RCCEx_CRSConfig.
  1403. Note: When the SYNC event is detected during the downcounting phase (before reaching the zero value),
  1404. it means that the actual frequency is lower than the target (and so, that the TRIM value should be
  1405. incremented), while when it is detected during the upcounting phase it means that the actual frequency
  1406. is higher (and that the TRIM value should be decremented).
  1407. (#) In interrupt mode, user can resort to the available macros (__HAL_RCC_CRS_XXX_IT). Interrupts will go
  1408. through CRS Handler (CRS_IRQn/CRS_IRQHandler)
  1409. (++) Call function HAL_RCCEx_CRSConfig()
  1410. (++) Enable CRS_IRQn (thanks to NVIC functions)
  1411. (++) Enable CRS interrupt (__HAL_RCC_CRS_ENABLE_IT)
  1412. (++) Implement CRS status management in the following user callbacks called from
  1413. HAL_RCCEx_CRS_IRQHandler():
  1414. (+++) HAL_RCCEx_CRS_SyncOkCallback()
  1415. (+++) HAL_RCCEx_CRS_SyncWarnCallback()
  1416. (+++) HAL_RCCEx_CRS_ExpectedSyncCallback()
  1417. (+++) HAL_RCCEx_CRS_ErrorCallback()
  1418. (#) To force a SYNC EVENT, user can use the function HAL_RCCEx_CRSSoftwareSynchronizationGenerate().
  1419. This function can be called before calling HAL_RCCEx_CRSConfig (for instance in Systick handler)
  1420. @endverbatim
  1421. * @{
  1422. */
  1423. /**
  1424. * @brief Start automatic synchronization for polling mode
  1425. * @param pInit Pointer on RCC_CRSInitTypeDef structure
  1426. * @retval None
  1427. */
  1428. void HAL_RCCEx_CRSConfig(RCC_CRSInitTypeDef *pInit)
  1429. {
  1430. uint32_t value;
  1431. /* Check the parameters */
  1432. assert_param(IS_RCC_CRS_SYNC_DIV(pInit->Prescaler));
  1433. assert_param(IS_RCC_CRS_SYNC_SOURCE(pInit->Source));
  1434. assert_param(IS_RCC_CRS_SYNC_POLARITY(pInit->Polarity));
  1435. assert_param(IS_RCC_CRS_RELOADVALUE(pInit->ReloadValue));
  1436. assert_param(IS_RCC_CRS_ERRORLIMIT(pInit->ErrorLimitValue));
  1437. assert_param(IS_RCC_CRS_HSI48CALIBRATION(pInit->HSI48CalibrationValue));
  1438. /* CONFIGURATION */
  1439. /* Before configuration, reset CRS registers to their default values*/
  1440. __HAL_RCC_CRS_FORCE_RESET();
  1441. __HAL_RCC_CRS_RELEASE_RESET();
  1442. /* Set the SYNCDIV[2:0] bits according to Prescaler value */
  1443. /* Set the SYNCSRC[1:0] bits according to Source value */
  1444. /* Set the SYNCSPOL bit according to Polarity value */
  1445. value = (pInit->Prescaler | pInit->Source | pInit->Polarity);
  1446. /* Set the RELOAD[15:0] bits according to ReloadValue value */
  1447. value |= pInit->ReloadValue;
  1448. /* Set the FELIM[7:0] bits according to ErrorLimitValue value */
  1449. value |= (pInit->ErrorLimitValue << CRS_CFGR_FELIM_Pos);
  1450. WRITE_REG(CRS->CFGR, value);
  1451. /* Adjust HSI48 oscillator smooth trimming */
  1452. /* Set the TRIM[5:0] bits according to RCC_CRS_HSI48CalibrationValue value */
  1453. MODIFY_REG(CRS->CR, CRS_CR_TRIM, (pInit->HSI48CalibrationValue << CRS_CR_TRIM_Pos));
  1454. /* START AUTOMATIC SYNCHRONIZATION*/
  1455. /* Enable Automatic trimming & Frequency error counter */
  1456. SET_BIT(CRS->CR, CRS_CR_AUTOTRIMEN | CRS_CR_CEN);
  1457. }
  1458. /**
  1459. * @brief Generate the software synchronization event
  1460. * @retval None
  1461. */
  1462. void HAL_RCCEx_CRSSoftwareSynchronizationGenerate(void)
  1463. {
  1464. LL_CRS_GenerateEvent_SWSYNC();
  1465. }
  1466. /**
  1467. * @brief Return synchronization info
  1468. * @param pSynchroInfo Pointer on @ref RCC_CRSSynchroInfoTypeDef structure
  1469. * @retval None
  1470. */
  1471. void HAL_RCCEx_CRSGetSynchronizationInfo(RCC_CRSSynchroInfoTypeDef *pSynchroInfo)
  1472. {
  1473. /* Check the parameter */
  1474. assert_param(pSynchroInfo != (void *)NULL);
  1475. /* Get the reload value */
  1476. pSynchroInfo->ReloadValue = LL_CRS_GetReloadCounter();
  1477. /* Get HSI48 oscillator smooth trimming */
  1478. pSynchroInfo->HSI48CalibrationValue = LL_CRS_GetHSI48SmoothTrimming();
  1479. /* Get Frequency error capture */
  1480. pSynchroInfo->FreqErrorCapture = LL_CRS_GetFreqErrorCapture();
  1481. /* Get Frequency error direction */
  1482. pSynchroInfo->FreqErrorDirection = LL_CRS_GetFreqErrorDirection();
  1483. }
  1484. /**
  1485. * @brief Wait for CRS Synchronization status.
  1486. * @param Timeout Duration of the timeout
  1487. * @note Timeout is based on the maximum time to receive a SYNC event based on synchronization
  1488. * frequency.
  1489. * @note If Timeout set to HAL_MAX_DELAY, HAL_TIMEOUT will be never returned.
  1490. * @retval Combination of Synchronization status
  1491. * This parameter can be a combination of the following values:
  1492. * @arg @ref RCC_CRS_TIMEOUT
  1493. * @arg @ref RCC_CRS_SYNCOK
  1494. * @arg @ref RCC_CRS_SYNCWARN
  1495. * @arg @ref RCC_CRS_SYNCERR
  1496. * @arg @ref RCC_CRS_SYNCMISS
  1497. * @arg @ref RCC_CRS_TRIMOVF
  1498. */
  1499. uint32_t HAL_RCCEx_CRSWaitSynchronization(uint32_t Timeout)
  1500. {
  1501. uint32_t crsstatus = RCC_CRS_NONE;
  1502. uint32_t tickstart;
  1503. /* Get timeout */
  1504. tickstart = HAL_GetTick();
  1505. /* Wait for CRS flag or timeout detection */
  1506. do
  1507. {
  1508. if (Timeout != HAL_MAX_DELAY)
  1509. {
  1510. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1511. {
  1512. crsstatus = RCC_CRS_TIMEOUT;
  1513. }
  1514. }
  1515. /* Check CRS SYNCOK flag */
  1516. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCOK))
  1517. {
  1518. /* CRS SYNC event OK */
  1519. crsstatus |= RCC_CRS_SYNCOK;
  1520. /* Clear CRS SYNC event OK bit */
  1521. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCOK);
  1522. }
  1523. /* Check CRS SYNCWARN flag */
  1524. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCWARN))
  1525. {
  1526. /* CRS SYNC warning */
  1527. crsstatus |= RCC_CRS_SYNCWARN;
  1528. /* Clear CRS SYNCWARN bit */
  1529. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCWARN);
  1530. }
  1531. /* Check CRS TRIM overflow flag */
  1532. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_TRIMOVF))
  1533. {
  1534. /* CRS SYNC Error */
  1535. crsstatus |= RCC_CRS_TRIMOVF;
  1536. /* Clear CRS Error bit */
  1537. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_TRIMOVF);
  1538. }
  1539. /* Check CRS Error flag */
  1540. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCERR))
  1541. {
  1542. /* CRS SYNC Error */
  1543. crsstatus |= RCC_CRS_SYNCERR;
  1544. /* Clear CRS Error bit */
  1545. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCERR);
  1546. }
  1547. /* Check CRS SYNC Missed flag */
  1548. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCMISS))
  1549. {
  1550. /* CRS SYNC Missed */
  1551. crsstatus |= RCC_CRS_SYNCMISS;
  1552. /* Clear CRS SYNC Missed bit */
  1553. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCMISS);
  1554. }
  1555. /* Check CRS Expected SYNC flag */
  1556. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_ESYNC))
  1557. {
  1558. /* frequency error counter reached a zero value */
  1559. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_ESYNC);
  1560. }
  1561. }
  1562. while (RCC_CRS_NONE == crsstatus);
  1563. return crsstatus;
  1564. }
  1565. /**
  1566. * @brief Handle the Clock Recovery System interrupt request.
  1567. * @retval None
  1568. */
  1569. void HAL_RCCEx_CRS_IRQHandler(void)
  1570. {
  1571. uint32_t crserror = RCC_CRS_NONE;
  1572. /* Get current IT flags and IT sources values */
  1573. uint32_t itflags = READ_REG(CRS->ISR);
  1574. uint32_t itsources = READ_REG(CRS->CR);
  1575. /* Check CRS SYNCOK flag */
  1576. if (((itflags & RCC_CRS_FLAG_SYNCOK) != 0U) && ((itsources & RCC_CRS_IT_SYNCOK) != 0U))
  1577. {
  1578. /* Clear CRS SYNC event OK flag */
  1579. LL_CRS_ClearFlag_SYNCOK();
  1580. /* user callback */
  1581. HAL_RCCEx_CRS_SyncOkCallback();
  1582. }
  1583. /* Check CRS SYNCWARN flag */
  1584. else if (((itflags & RCC_CRS_FLAG_SYNCWARN) != 0U) && ((itsources & RCC_CRS_IT_SYNCWARN) != 0U))
  1585. {
  1586. /* Clear CRS SYNCWARN flag */
  1587. LL_CRS_ClearFlag_SYNCWARN();
  1588. /* user callback */
  1589. HAL_RCCEx_CRS_SyncWarnCallback();
  1590. }
  1591. /* Check CRS Expected SYNC flag */
  1592. else if (((itflags & RCC_CRS_FLAG_ESYNC) != 0U) && ((itsources & RCC_CRS_IT_ESYNC) != 0U))
  1593. {
  1594. /* frequency error counter reached a zero value */
  1595. LL_CRS_ClearFlag_ESYNC();
  1596. /* user callback */
  1597. HAL_RCCEx_CRS_ExpectedSyncCallback();
  1598. }
  1599. /* Check CRS Error flags */
  1600. else
  1601. {
  1602. if (((itflags & RCC_CRS_FLAG_ERR) != 0U) && ((itsources & RCC_CRS_IT_ERR) != 0U))
  1603. {
  1604. if ((itflags & RCC_CRS_FLAG_SYNCERR) != 0U)
  1605. {
  1606. crserror |= RCC_CRS_SYNCERR;
  1607. }
  1608. if ((itflags & RCC_CRS_FLAG_SYNCMISS) != 0U)
  1609. {
  1610. crserror |= RCC_CRS_SYNCMISS;
  1611. }
  1612. if ((itflags & RCC_CRS_FLAG_TRIMOVF) != 0U)
  1613. {
  1614. crserror |= RCC_CRS_TRIMOVF;
  1615. }
  1616. /* Clear CRS Error flags */
  1617. LL_CRS_ClearFlag_ERR();
  1618. /* user error callback */
  1619. HAL_RCCEx_CRS_ErrorCallback(crserror);
  1620. }
  1621. }
  1622. }
  1623. /**
  1624. * @brief RCCEx Clock Recovery System SYNCOK interrupt callback.
  1625. * @retval none
  1626. */
  1627. __weak void HAL_RCCEx_CRS_SyncOkCallback(void)
  1628. {
  1629. /* NOTE : This function should not be modified, when the callback is needed,
  1630. the @ref HAL_RCCEx_CRS_SyncOkCallback should be implemented in the user file
  1631. */
  1632. }
  1633. /**
  1634. * @brief RCCEx Clock Recovery System SYNCWARN interrupt callback.
  1635. * @retval none
  1636. */
  1637. __weak void HAL_RCCEx_CRS_SyncWarnCallback(void)
  1638. {
  1639. /* NOTE : This function should not be modified, when the callback is needed,
  1640. the HAL_RCCEx_CRS_SyncWarnCallback should be implemented in the user file
  1641. */
  1642. }
  1643. /**
  1644. * @brief RCCEx Clock Recovery System Expected SYNC interrupt callback.
  1645. * @retval none
  1646. */
  1647. __weak void HAL_RCCEx_CRS_ExpectedSyncCallback(void)
  1648. {
  1649. /* NOTE : This function should not be modified, when the callback is needed,
  1650. the HAL_RCCEx_CRS_ExpectedSyncCallback should be implemented in the user file
  1651. */
  1652. }
  1653. /**
  1654. * @brief RCCEx Clock Recovery System Error interrupt callback.
  1655. * @param Error Combination of Error status.
  1656. * This parameter can be a combination of the following values:
  1657. * @arg @ref RCC_CRS_SYNCERR
  1658. * @arg @ref RCC_CRS_SYNCMISS
  1659. * @arg @ref RCC_CRS_TRIMOVF
  1660. * @retval none
  1661. */
  1662. __weak void HAL_RCCEx_CRS_ErrorCallback(uint32_t Error)
  1663. {
  1664. /* Prevent unused argument(s) compilation warning */
  1665. UNUSED(Error);
  1666. /* NOTE : This function should not be modified, when the callback is needed,
  1667. the @ref HAL_RCCEx_CRS_ErrorCallback should be implemented in the user file
  1668. */
  1669. }
  1670. /**
  1671. * @}
  1672. */
  1673. #endif
  1674. /**
  1675. * @}
  1676. */
  1677. /** @addtogroup RCCEx_Private_Functions
  1678. * @{
  1679. */
  1680. #if defined(SAI1)
  1681. /**
  1682. * @brief Configure the parameters N & P of PLLSAI1 and enable PLLSAI1 output clock(s).
  1683. * @param PLLSAI1 pointer to an RCC_PLLSAI1InitTypeDef structure that
  1684. * contains the configuration parameters N & P as well as PLLSAI1 output clock(s)
  1685. *
  1686. * @note PLLSAI1 is temporary disable to apply new parameters
  1687. *
  1688. * @retval HAL status
  1689. */
  1690. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNP(RCC_PLLSAI1InitTypeDef *PLLSAI1)
  1691. {
  1692. uint32_t tickstart;
  1693. HAL_StatusTypeDef status = HAL_OK;
  1694. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  1695. assert_param(IS_RCC_PLLN_VALUE(PLLSAI1->PLLN));
  1696. assert_param(IS_RCC_PLLP_VALUE(PLLSAI1->PLLP));
  1697. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PLLSAI1->PLLSAI1ClockOut));
  1698. /* Disable the PLLSAI1 */
  1699. __HAL_RCC_PLLSAI1_DISABLE();
  1700. /* Get Start Tick*/
  1701. tickstart = HAL_GetTick();
  1702. /* Wait till PLLSAI1 is ready to be updated */
  1703. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  1704. {
  1705. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1706. {
  1707. status = HAL_TIMEOUT;
  1708. break;
  1709. }
  1710. }
  1711. if (status == HAL_OK)
  1712. {
  1713. /* Configure the PLLSAI1 Multiplication factor N */
  1714. __HAL_RCC_PLLSAI1_MULN_CONFIG(PLLSAI1->PLLN);
  1715. /* Configure the PLLSAI1 Division factor P */
  1716. __HAL_RCC_PLLSAI1_DIVP_CONFIG(PLLSAI1->PLLP);
  1717. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  1718. __HAL_RCC_PLLSAI1_ENABLE();
  1719. /* Get Start Tick*/
  1720. tickstart = HAL_GetTick();
  1721. /* Wait till PLLSAI1 is ready */
  1722. while (LL_RCC_PLLSAI1_IsReady() != 1U)
  1723. {
  1724. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1725. {
  1726. status = HAL_TIMEOUT;
  1727. break;
  1728. }
  1729. }
  1730. if (status == HAL_OK)
  1731. {
  1732. /* Configure the PLLSAI1 Clock output(s) */
  1733. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PLLSAI1->PLLSAI1ClockOut);
  1734. }
  1735. }
  1736. return status;
  1737. }
  1738. /**
  1739. * @brief Configure the parameters N & Q of PLLSAI1 and enable PLLSAI1 output clock(s).
  1740. * @param PLLSAI1 pointer to an RCC_PLLSAI1InitTypeDef structure that
  1741. * contains the configuration parameters N & Q as well as PLLSAI1 output clock(s)
  1742. *
  1743. * @note PLLSAI1 is temporary disable to apply new parameters
  1744. *
  1745. * @retval HAL status
  1746. */
  1747. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNQ(RCC_PLLSAI1InitTypeDef *PLLSAI1)
  1748. {
  1749. uint32_t tickstart;
  1750. HAL_StatusTypeDef status = HAL_OK;
  1751. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  1752. assert_param(IS_RCC_PLLN_VALUE(PLLSAI1->PLLN));
  1753. assert_param(IS_RCC_PLLQ_VALUE(PLLSAI1->PLLQ));
  1754. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PLLSAI1->PLLSAI1ClockOut));
  1755. /* Disable the PLLSAI1 */
  1756. __HAL_RCC_PLLSAI1_DISABLE();
  1757. /* Get Start Tick*/
  1758. tickstart = HAL_GetTick();
  1759. /* Wait till PLLSAI1 is ready to be updated */
  1760. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  1761. {
  1762. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1763. {
  1764. status = HAL_TIMEOUT;
  1765. break;
  1766. }
  1767. }
  1768. if (status == HAL_OK)
  1769. {
  1770. /* Configure the PLLSAI1 Multiplication factor N */
  1771. __HAL_RCC_PLLSAI1_MULN_CONFIG(PLLSAI1->PLLN);
  1772. /* Configure the PLLSAI1 Division factor Q */
  1773. __HAL_RCC_PLLSAI1_DIVQ_CONFIG(PLLSAI1->PLLQ);
  1774. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  1775. __HAL_RCC_PLLSAI1_ENABLE();
  1776. /* Get Start Tick*/
  1777. tickstart = HAL_GetTick();
  1778. /* Wait till PLLSAI1 is ready */
  1779. while (LL_RCC_PLLSAI1_IsReady() != 1U)
  1780. {
  1781. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1782. {
  1783. status = HAL_TIMEOUT;
  1784. break;
  1785. }
  1786. }
  1787. if (status == HAL_OK)
  1788. {
  1789. /* Configure the PLLSAI1 Clock output(s) */
  1790. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PLLSAI1->PLLSAI1ClockOut);
  1791. }
  1792. }
  1793. return status;
  1794. }
  1795. /**
  1796. * @brief Configure the parameters N & R of PLLSAI1 and enable PLLSAI1 output clock(s).
  1797. * @param PLLSAI1 pointer to an RCC_PLLSAI1InitTypeDef structure that
  1798. * contains the configuration parameters N & R as well as PLLSAI1 output clock(s)
  1799. *
  1800. * @note PLLSAI1 is temporary disable to apply new parameters
  1801. *
  1802. * @retval HAL status
  1803. */
  1804. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNR(RCC_PLLSAI1InitTypeDef *PLLSAI1)
  1805. {
  1806. uint32_t tickstart;
  1807. HAL_StatusTypeDef status = HAL_OK;
  1808. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  1809. assert_param(IS_RCC_PLLN_VALUE(PLLSAI1->PLLN));
  1810. assert_param(IS_RCC_PLLR_VALUE(PLLSAI1->PLLR));
  1811. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PLLSAI1->PLLSAI1ClockOut));
  1812. /* Disable the PLLSAI1 */
  1813. __HAL_RCC_PLLSAI1_DISABLE();
  1814. /* Get Start Tick*/
  1815. tickstart = HAL_GetTick();
  1816. /* Wait till PLLSAI1 is ready to be updated */
  1817. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  1818. {
  1819. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1820. {
  1821. status = HAL_TIMEOUT;
  1822. break;
  1823. }
  1824. }
  1825. if (status == HAL_OK)
  1826. {
  1827. /* Configure the PLLSAI1 Multiplication factor N */
  1828. __HAL_RCC_PLLSAI1_MULN_CONFIG(PLLSAI1->PLLN);
  1829. /* Configure the PLLSAI1 Division factor R */
  1830. __HAL_RCC_PLLSAI1_DIVR_CONFIG(PLLSAI1->PLLR);
  1831. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  1832. __HAL_RCC_PLLSAI1_ENABLE();
  1833. /* Get Start Tick*/
  1834. tickstart = HAL_GetTick();
  1835. /* Wait till PLLSAI1 is ready */
  1836. while (LL_RCC_PLLSAI1_IsReady() != 1U)
  1837. {
  1838. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1839. {
  1840. status = HAL_TIMEOUT;
  1841. break;
  1842. }
  1843. }
  1844. if (status == HAL_OK)
  1845. {
  1846. /* Configure the PLLSAI1 Clock output(s) */
  1847. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PLLSAI1->PLLSAI1ClockOut);
  1848. }
  1849. }
  1850. return status;
  1851. }
  1852. #endif
  1853. /**
  1854. * @brief Return PLL clock (PLLPCLK) frequency used for SAI domain
  1855. * @retval PLLPCLK clock frequency (in Hz)
  1856. */
  1857. static uint32_t RCC_PLL_GetFreqDomain_P(void)
  1858. {
  1859. uint32_t pllinputfreq, pllsource;
  1860. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI Value / PLLM) * PLLN
  1861. SAI Domain clock = PLL_VCO / PLLP
  1862. */
  1863. pllsource = LL_RCC_PLL_GetMainSource();
  1864. switch (pllsource)
  1865. {
  1866. case LL_RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1867. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1868. break;
  1869. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1870. pllinputfreq = HSI_VALUE;
  1871. break;
  1872. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1873. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  1874. {
  1875. pllinputfreq = HSE_VALUE / 2U;
  1876. }
  1877. else
  1878. {
  1879. pllinputfreq = HSE_VALUE;
  1880. }
  1881. break;
  1882. default:
  1883. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1884. break;
  1885. }
  1886. return __LL_RCC_CALC_PLLCLK_ADC_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1887. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetP());
  1888. }
  1889. /**
  1890. * @brief Return PLL clock (PLLQCLK) frequency used for 48 MHz domain
  1891. * @retval PLLQCLK clock frequency (in Hz)
  1892. */
  1893. static uint32_t RCC_PLL_GetFreqDomain_Q(void)
  1894. {
  1895. uint32_t pllinputfreq, pllsource;
  1896. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI Value/ PLLM) * PLLN
  1897. 48M Domain clock = PLL_VCO / PLLQ
  1898. */
  1899. pllsource = LL_RCC_PLL_GetMainSource();
  1900. switch (pllsource)
  1901. {
  1902. case LL_RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1903. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1904. break;
  1905. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1906. pllinputfreq = HSI_VALUE;
  1907. break;
  1908. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1909. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  1910. {
  1911. pllinputfreq = HSE_VALUE / 2U;
  1912. }
  1913. else
  1914. {
  1915. pllinputfreq = HSE_VALUE;
  1916. }
  1917. break;
  1918. default:
  1919. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1920. break;
  1921. }
  1922. return __LL_RCC_CALC_PLLCLK_48M_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1923. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetQ());
  1924. }
  1925. #if defined(SAI1)
  1926. /**
  1927. * @brief Return PLLSAI1 clock (PLLSAI1RCLK) frequency used for ADC domain
  1928. * @retval PLLSAI1RCLK clock frequency (in Hz)
  1929. */
  1930. static uint32_t RCC_PLLSAI1_GetFreqDomain_R(void)
  1931. {
  1932. uint32_t pllinputfreq, pllsource;
  1933. /* PLLSAI1_VCO = (HSE_VALUE or HSI_VALUE or MSI Value/ PLLM) * PLLSAI1N */
  1934. /* 48M Domain clock = PLLSAI1_VCO / PLLSAI1R */
  1935. pllsource = LL_RCC_PLL_GetMainSource();
  1936. switch (pllsource)
  1937. {
  1938. case LL_RCC_PLLSOURCE_MSI: /* MSI used as PLLSAI1 clock source */
  1939. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1940. break;
  1941. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLLSAI1 clock source */
  1942. pllinputfreq = HSI_VALUE;
  1943. break;
  1944. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLLSAI1 clock source */
  1945. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  1946. {
  1947. pllinputfreq = HSE_VALUE / 2U;
  1948. }
  1949. else
  1950. {
  1951. pllinputfreq = HSE_VALUE;
  1952. }
  1953. break;
  1954. default:
  1955. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1956. break;
  1957. }
  1958. return __LL_RCC_CALC_PLLSAI1_ADC_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1959. LL_RCC_PLLSAI1_GetN(), LL_RCC_PLLSAI1_GetR());
  1960. }
  1961. /**
  1962. * @brief Return PLLSAI1 clock (PLLSAI1PCLK) frequency used for SAI domain
  1963. * @retval PLLSAI1PCLK clock frequency (in Hz)
  1964. */
  1965. static uint32_t RCC_PLLSAI1_GetFreqDomain_P(void)
  1966. {
  1967. uint32_t pllinputfreq, pllsource;
  1968. /* PLLSAI1_VCO = (HSE_VALUE or HSI_VALUE or MSI Value/ PLLM) * PLLSAI1N */
  1969. /* SAI Domain clock = PLLSAI1_VCO / PLLSAI1P */
  1970. pllsource = LL_RCC_PLL_GetMainSource();
  1971. switch (pllsource)
  1972. {
  1973. case LL_RCC_PLLSOURCE_MSI: /* MSI used as PLLSAI1 clock source */
  1974. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1975. break;
  1976. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLLSAI1 clock source */
  1977. pllinputfreq = HSI_VALUE;
  1978. break;
  1979. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLLSAI1 clock source */
  1980. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  1981. {
  1982. pllinputfreq = HSE_VALUE / 2U;
  1983. }
  1984. else
  1985. {
  1986. pllinputfreq = HSE_VALUE;
  1987. }
  1988. break;
  1989. default:
  1990. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1991. break;
  1992. }
  1993. return __LL_RCC_CALC_PLLSAI1_SAI_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1994. LL_RCC_PLLSAI1_GetN(), LL_RCC_PLLSAI1_GetP());
  1995. }
  1996. /**
  1997. * @brief Return PLLSAI1 clock (PLLSAI1QCLK) frequency used for 48Mhz domain
  1998. * @retval PLLSAI1QCLK clock frequency (in Hz)
  1999. */
  2000. static uint32_t RCC_PLLSAI1_GetFreqDomain_Q(void)
  2001. {
  2002. uint32_t pllinputfreq, pllsource;
  2003. /* PLLSAI1_VCO = (HSE_VALUE or HSI_VALUE or MSI Value/ PLLM) * PLLSAI1N */
  2004. /* 48M Domain clock = PLLSAI1_VCO / PLLSAI1Q */
  2005. pllsource = LL_RCC_PLL_GetMainSource();
  2006. switch (pllsource)
  2007. {
  2008. case LL_RCC_PLLSOURCE_MSI: /* MSI used as PLLSAI1 clock source */
  2009. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  2010. break;
  2011. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLLSAI1 clock source */
  2012. pllinputfreq = HSI_VALUE;
  2013. break;
  2014. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLLSAI1 clock source */
  2015. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  2016. {
  2017. pllinputfreq = HSE_VALUE / 2U;
  2018. }
  2019. else
  2020. {
  2021. pllinputfreq = HSE_VALUE;
  2022. }
  2023. break;
  2024. default:
  2025. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  2026. break;
  2027. }
  2028. return __LL_RCC_CALC_PLLSAI1_48M_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  2029. LL_RCC_PLLSAI1_GetN(), LL_RCC_PLLSAI1_GetQ());
  2030. }
  2031. #endif
  2032. /**
  2033. * @}
  2034. */
  2035. #endif /* HAL_RCC_MODULE_ENABLED */
  2036. /**
  2037. * @}
  2038. */
  2039. /**
  2040. * @}
  2041. */
  2042. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/