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.
 
 
 

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