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.
 
 
 

1216 lines
39 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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 extension peripheral:
  8. * + Extended Peripheral Control functions
  9. * + Extended Clock Recovery System Control functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * <h2><center>&copy; Copyright(c) 2016 STMicroelectronics.
  15. * All rights reserved.</center></h2>
  16. *
  17. * This software component is licensed by ST under BSD 3-Clause license,
  18. * the "License"; You may not use this file except in compliance with the
  19. * License. You may obtain a copy of the License at:
  20. * opensource.org/licenses/BSD-3-Clause
  21. *
  22. ******************************************************************************
  23. */
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32l0xx_hal.h"
  26. /** @addtogroup STM32L0xx_HAL_Driver
  27. * @{
  28. */
  29. #ifdef HAL_RCC_MODULE_ENABLED
  30. /** @defgroup RCCEx RCCEx
  31. * @brief RCC Extension HAL module driver
  32. * @{
  33. */
  34. /* Private typedef -----------------------------------------------------------*/
  35. /* Private define ------------------------------------------------------------*/
  36. /** @defgroup RCCEx_Private_Constants RCCEx Private Constants
  37. * @{
  38. */
  39. #if defined(USB)
  40. extern const uint8_t PLLMulTable[];
  41. #endif /* USB */
  42. /**
  43. * @}
  44. */
  45. /* Private macro -------------------------------------------------------------*/
  46. /** @defgroup RCCEx_Private_Macros RCCEx Private Macros
  47. * @{
  48. */
  49. /**
  50. * @}
  51. */
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private function prototypes -----------------------------------------------*/
  54. /* Private functions ---------------------------------------------------------*/
  55. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  56. * @{
  57. */
  58. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  59. * @brief Extended Peripheral Control functions
  60. *
  61. @verbatim
  62. ===============================================================================
  63. ##### Extended Peripheral Control functions #####
  64. ===============================================================================
  65. [..]
  66. This subsection provides a set of functions allowing to control the RCC Clocks
  67. frequencies.
  68. [..]
  69. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  70. select the RTC clock source; in this case the Backup domain will be reset in
  71. order to modify the RTC Clock source, as consequence RTC registers (including
  72. the backup registers) are set to their reset values.
  73. @endverbatim
  74. * @{
  75. */
  76. /**
  77. * @brief Initializes the RCC extended peripherals clocks according to the specified
  78. * parameters in the RCC_PeriphCLKInitTypeDef.
  79. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  80. * contains the configuration information for the Extended Peripherals clocks(USART1,USART2, LPUART1,
  81. * I2C1, I2C3, RTC, USB/RNG and LPTIM1 clocks).
  82. * @retval HAL status
  83. * @note If HAL_ERROR returned, first switch-OFF HSE clock oscillator with @ref HAL_RCC_OscConfig()
  84. * to possibly update HSE divider.
  85. */
  86. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  87. {
  88. uint32_t tickstart;
  89. uint32_t temp_reg;
  90. /* Check the parameters */
  91. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  92. /*------------------------------- RTC/LCD Configuration ------------------------*/
  93. if ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC)
  94. #if defined(LCD)
  95. || (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LCD) == RCC_PERIPHCLK_LCD)
  96. #endif /* LCD */
  97. )
  98. {
  99. /* check for RTC Parameters used to output RTCCLK */
  100. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC)
  101. {
  102. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  103. }
  104. #if defined(LCD)
  105. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LCD) == RCC_PERIPHCLK_LCD)
  106. {
  107. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->LCDClockSelection));
  108. }
  109. #endif /* LCD */
  110. FlagStatus pwrclkchanged = RESET;
  111. /* As soon as function is called to change RTC clock source, activation of the
  112. power domain is done. */
  113. /* Requires to enable write access to Backup Domain of necessary */
  114. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  115. {
  116. __HAL_RCC_PWR_CLK_ENABLE();
  117. pwrclkchanged = SET;
  118. }
  119. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  120. {
  121. /* Enable write access to Backup domain */
  122. SET_BIT(PWR->CR, PWR_CR_DBP);
  123. /* Wait for Backup domain Write protection disable */
  124. tickstart = HAL_GetTick();
  125. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  126. {
  127. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  128. {
  129. return HAL_TIMEOUT;
  130. }
  131. }
  132. }
  133. /* Check if user wants to change HSE RTC prescaler whereas HSE is enabled */
  134. temp_reg = (RCC->CR & RCC_CR_RTCPRE);
  135. if ((temp_reg != (PeriphClkInit->RTCClockSelection & RCC_CR_RTCPRE))
  136. #if defined (LCD)
  137. || (temp_reg != (PeriphClkInit->LCDClockSelection & RCC_CR_RTCPRE))
  138. #endif /* LCD */
  139. )
  140. { /* Check HSE State */
  141. if ((PeriphClkInit->RTCClockSelection & RCC_CSR_RTCSEL) == RCC_CSR_RTCSEL_HSE)
  142. {
  143. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY))
  144. {
  145. /* To update HSE divider, first switch-OFF HSE clock oscillator*/
  146. return HAL_ERROR;
  147. }
  148. }
  149. }
  150. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  151. temp_reg = (RCC->CSR & RCC_CSR_RTCSEL);
  152. if((temp_reg != 0x00000000U) && (((temp_reg != (PeriphClkInit->RTCClockSelection & RCC_CSR_RTCSEL)) \
  153. && (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC))
  154. #if defined(LCD)
  155. || ((temp_reg != (PeriphClkInit->LCDClockSelection & RCC_CSR_RTCSEL)) \
  156. && (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LCD) == RCC_PERIPHCLK_LCD))
  157. #endif /* LCD */
  158. ))
  159. {
  160. /* Store the content of CSR register before the reset of Backup Domain */
  161. temp_reg = (RCC->CSR & ~(RCC_CSR_RTCSEL));
  162. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  163. __HAL_RCC_BACKUPRESET_FORCE();
  164. __HAL_RCC_BACKUPRESET_RELEASE();
  165. /* Restore the Content of CSR register */
  166. RCC->CSR = temp_reg;
  167. /* Wait for LSERDY if LSE was enabled */
  168. if (HAL_IS_BIT_SET(temp_reg, RCC_CSR_LSEON))
  169. {
  170. /* Get Start Tick */
  171. tickstart = HAL_GetTick();
  172. /* Wait till LSE is ready */
  173. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == 0U)
  174. {
  175. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  176. {
  177. return HAL_TIMEOUT;
  178. }
  179. }
  180. }
  181. }
  182. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  183. /* Require to disable power clock if necessary */
  184. if(pwrclkchanged == SET)
  185. {
  186. __HAL_RCC_PWR_CLK_DISABLE();
  187. }
  188. }
  189. #if defined (RCC_CCIPR_USART1SEL)
  190. /*------------------------------- USART1 Configuration ------------------------*/
  191. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART1) == RCC_PERIPHCLK_USART1)
  192. {
  193. /* Check the parameters */
  194. assert_param(IS_RCC_USART1CLKSOURCE(PeriphClkInit->Usart1ClockSelection));
  195. /* Configure the USART1 clock source */
  196. __HAL_RCC_USART1_CONFIG(PeriphClkInit->Usart1ClockSelection);
  197. }
  198. #endif /* RCC_CCIPR_USART1SEL */
  199. /*----------------------------- USART2 Configuration --------------------------*/
  200. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART2) == RCC_PERIPHCLK_USART2)
  201. {
  202. /* Check the parameters */
  203. assert_param(IS_RCC_USART2CLKSOURCE(PeriphClkInit->Usart2ClockSelection));
  204. /* Configure the USART2 clock source */
  205. __HAL_RCC_USART2_CONFIG(PeriphClkInit->Usart2ClockSelection);
  206. }
  207. /*------------------------------ LPUART1 Configuration ------------------------*/
  208. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPUART1) == RCC_PERIPHCLK_LPUART1)
  209. {
  210. /* Check the parameters */
  211. assert_param(IS_RCC_LPUART1CLKSOURCE(PeriphClkInit->Lpuart1ClockSelection));
  212. /* Configure the LPUAR1 clock source */
  213. __HAL_RCC_LPUART1_CONFIG(PeriphClkInit->Lpuart1ClockSelection);
  214. }
  215. /*------------------------------ I2C1 Configuration ------------------------*/
  216. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C1) == RCC_PERIPHCLK_I2C1)
  217. {
  218. /* Check the parameters */
  219. assert_param(IS_RCC_I2C1CLKSOURCE(PeriphClkInit->I2c1ClockSelection));
  220. /* Configure the I2C1 clock source */
  221. __HAL_RCC_I2C1_CONFIG(PeriphClkInit->I2c1ClockSelection);
  222. }
  223. #if defined (RCC_CCIPR_I2C3SEL)
  224. /*------------------------------ I2C3 Configuration ------------------------*/
  225. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C3) == RCC_PERIPHCLK_I2C3)
  226. {
  227. /* Check the parameters */
  228. assert_param(IS_RCC_I2C3CLKSOURCE(PeriphClkInit->I2c3ClockSelection));
  229. /* Configure the I2C3 clock source */
  230. __HAL_RCC_I2C3_CONFIG(PeriphClkInit->I2c3ClockSelection);
  231. }
  232. #endif /* RCC_CCIPR_I2C3SEL */
  233. #if defined(USB)
  234. /*---------------------------- USB and RNG configuration --------------------*/
  235. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == (RCC_PERIPHCLK_USB))
  236. {
  237. assert_param(IS_RCC_USBCLKSOURCE(PeriphClkInit->UsbClockSelection));
  238. __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
  239. }
  240. #endif /* USB */
  241. /*---------------------------- LPTIM1 configuration ------------------------*/
  242. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == (RCC_PERIPHCLK_LPTIM1))
  243. {
  244. assert_param(IS_RCC_LPTIMCLK(PeriphClkInit->LptimClockSelection));
  245. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->LptimClockSelection);
  246. }
  247. return HAL_OK;
  248. }
  249. /**
  250. * @brief Get the PeriphClkInit according to the internal RCC configuration registers.
  251. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  252. * returns the configuration information for the Extended Peripherals clocks(USART1,USART2, LPUART1,
  253. * I2C1, I2C3, RTC, USB/RNG and LPTIM1 clocks).
  254. * @retval None
  255. */
  256. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  257. {
  258. uint32_t srcclk;
  259. /* Set all possible values for the extended clock type parameter -----------*/
  260. /* Common part first */
  261. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_LPUART1 | \
  262. RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_RTC | \
  263. RCC_PERIPHCLK_LPTIM1;
  264. #if defined(RCC_CCIPR_USART1SEL)
  265. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USART1;
  266. #endif /* RCC_CCIPR_USART1SEL */
  267. #if defined(RCC_CCIPR_I2C3SEL)
  268. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2C3;
  269. #endif /* RCC_CCIPR_I2C3SEL */
  270. #if defined(USB)
  271. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USB;
  272. #endif /* USB */
  273. #if defined(LCD)
  274. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_LCD;
  275. #endif /* LCD */
  276. /* Get the RTC/LCD configuration -----------------------------------------------*/
  277. srcclk = __HAL_RCC_GET_RTC_SOURCE();
  278. if (srcclk != RCC_RTCCLKSOURCE_HSE_DIV2)
  279. {
  280. /* Source clock is LSE or LSI*/
  281. PeriphClkInit->RTCClockSelection = srcclk;
  282. }
  283. else
  284. {
  285. /* Source clock is HSE. Need to get the prescaler value*/
  286. PeriphClkInit->RTCClockSelection = srcclk | (READ_BIT(RCC->CR, RCC_CR_RTCPRE));
  287. }
  288. #if defined(LCD)
  289. PeriphClkInit->LCDClockSelection = PeriphClkInit->RTCClockSelection;
  290. #endif /* LCD */
  291. #if defined(RCC_CCIPR_USART1SEL)
  292. /* Get the USART1 configuration --------------------------------------------*/
  293. PeriphClkInit->Usart1ClockSelection = __HAL_RCC_GET_USART1_SOURCE();
  294. #endif /* RCC_CCIPR_USART1SEL */
  295. /* Get the USART2 clock source ---------------------------------------------*/
  296. PeriphClkInit->Usart2ClockSelection = __HAL_RCC_GET_USART2_SOURCE();
  297. /* Get the LPUART1 clock source ---------------------------------------------*/
  298. PeriphClkInit->Lpuart1ClockSelection = __HAL_RCC_GET_LPUART1_SOURCE();
  299. /* Get the I2C1 clock source -----------------------------------------------*/
  300. PeriphClkInit->I2c1ClockSelection = __HAL_RCC_GET_I2C1_SOURCE();
  301. #if defined(RCC_CCIPR_I2C3SEL)
  302. /* Get the I2C3 clock source -----------------------------------------------*/
  303. PeriphClkInit->I2c3ClockSelection = __HAL_RCC_GET_I2C3_SOURCE();
  304. #endif /* RCC_CCIPR_I2C3SEL */
  305. /* Get the LPTIM1 clock source -----------------------------------------------*/
  306. PeriphClkInit->LptimClockSelection = __HAL_RCC_GET_LPTIM1_SOURCE();
  307. /* Get the RTC clock source -----------------------------------------------*/
  308. PeriphClkInit->RTCClockSelection = __HAL_RCC_GET_RTC_SOURCE();
  309. #if defined(USB)
  310. /* Get the USB/RNG clock source -----------------------------------------------*/
  311. PeriphClkInit->UsbClockSelection = __HAL_RCC_GET_USB_SOURCE();
  312. #endif /* USB */
  313. }
  314. /**
  315. * @brief Return the peripheral clock frequency
  316. * @note Return 0 if peripheral clock is unknown
  317. * @param PeriphClk Peripheral clock identifier
  318. * This parameter can be one of the following values:
  319. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  320. * @arg @ref RCC_PERIPHCLK_LCD LCD peripheral clock (*)
  321. * @arg @ref RCC_PERIPHCLK_USB USB or RNG peripheral clock (*)
  322. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock (*)
  323. * @arg @ref RCC_PERIPHCLK_USART2 USART2 peripheral clock
  324. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock
  325. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  326. * @arg @ref RCC_PERIPHCLK_I2C2 I2C2 peripheral clock (*)
  327. * @arg @ref RCC_PERIPHCLK_I2C3 I2C3 peripheral clock (*)
  328. * @note (*) means that this peripheral is not present on all the devices
  329. * @retval Frequency in Hz (0: means that no available frequency for the peripheral)
  330. */
  331. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  332. {
  333. uint32_t frequency = 0U;
  334. uint32_t temp_reg, clkprediv, srcclk; /* no init needed */
  335. #if defined(USB)
  336. uint32_t pllmul, plldiv, pllvco; /* no init needed */
  337. #endif /* USB */
  338. /* Check the parameters */
  339. assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
  340. switch (PeriphClk)
  341. {
  342. case RCC_PERIPHCLK_RTC:
  343. #if defined(LCD)
  344. case RCC_PERIPHCLK_LCD:
  345. #endif /* LCD */
  346. {
  347. /* Get RCC CSR configuration ------------------------------------------------------*/
  348. temp_reg = RCC->CSR;
  349. /* Get the current RTC source */
  350. srcclk = __HAL_RCC_GET_RTC_SOURCE();
  351. /* Check if LSE is ready if RTC clock selection is LSE */
  352. if ((srcclk == RCC_RTCCLKSOURCE_LSE) && (HAL_IS_BIT_SET(temp_reg, RCC_CSR_LSERDY)))
  353. {
  354. frequency = LSE_VALUE;
  355. }
  356. /* Check if LSI is ready if RTC clock selection is LSI */
  357. else if (srcclk == RCC_RTCCLKSOURCE_LSI)
  358. {
  359. if (HAL_IS_BIT_SET(temp_reg, RCC_CSR_LSIRDY))
  360. {
  361. frequency = LSI_VALUE;
  362. }
  363. }
  364. /* Check if HSE is ready and if RTC clock selection is HSE */
  365. else if (srcclk == RCC_RTCCLKSOURCE_HSE_DIVX)
  366. {
  367. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY))
  368. {
  369. /* Get the current HSE clock divider */
  370. clkprediv = __HAL_RCC_GET_RTC_HSE_PRESCALER();
  371. switch (clkprediv)
  372. {
  373. case RCC_RTC_HSE_DIV_16: /* HSE DIV16 has been selected */
  374. {
  375. frequency = HSE_VALUE / 16U;
  376. break;
  377. }
  378. case RCC_RTC_HSE_DIV_8: /* HSE DIV8 has been selected */
  379. {
  380. frequency = HSE_VALUE / 8U;
  381. break;
  382. }
  383. case RCC_RTC_HSE_DIV_4: /* HSE DIV4 has been selected */
  384. {
  385. frequency = HSE_VALUE / 4U;
  386. break;
  387. }
  388. default: /* HSE DIV2 has been selected */
  389. {
  390. frequency = HSE_VALUE / 2U;
  391. break;
  392. }
  393. }
  394. }
  395. }
  396. /* Clock not enabled for RTC */
  397. else
  398. {
  399. frequency = 0U;
  400. }
  401. break;
  402. }
  403. #if defined(USB)
  404. case RCC_PERIPHCLK_USB:
  405. {
  406. /* Get the current USB source */
  407. srcclk = __HAL_RCC_GET_USB_SOURCE();
  408. if (srcclk == RCC_USBCLKSOURCE_PLL)
  409. {
  410. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLRDY))
  411. {
  412. /* Get PLL clock source and multiplication factor ----------------------*/
  413. pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
  414. plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
  415. pllmul = PLLMulTable[(pllmul >> RCC_CFGR_PLLMUL_Pos)];
  416. plldiv = (plldiv >> RCC_CFGR_PLLDIV_Pos) + 1U;
  417. /* Compute PLL clock input */
  418. if(__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI)
  419. {
  420. if (READ_BIT(RCC->CR, RCC_CR_HSIDIVF) != 0U)
  421. {
  422. pllvco = (HSI_VALUE >> 2U);
  423. }
  424. else
  425. {
  426. pllvco = HSI_VALUE;
  427. }
  428. }
  429. else /* HSE source */
  430. {
  431. pllvco = HSE_VALUE;
  432. }
  433. /* pllvco * pllmul / plldiv */
  434. pllvco = (pllvco * pllmul);
  435. frequency = (pllvco/ plldiv);
  436. }
  437. }
  438. else if (srcclk == RCC_USBCLKSOURCE_HSI48)
  439. {
  440. if (HAL_IS_BIT_SET(RCC->CRRCR, RCC_CRRCR_HSI48RDY))
  441. {
  442. frequency = HSI48_VALUE;
  443. }
  444. }
  445. else /* RCC_USBCLKSOURCE_NONE */
  446. {
  447. frequency = 0U;
  448. }
  449. break;
  450. }
  451. #endif /* USB */
  452. #if defined(RCC_CCIPR_USART1SEL)
  453. case RCC_PERIPHCLK_USART1:
  454. {
  455. /* Get the current USART1 source */
  456. srcclk = __HAL_RCC_GET_USART1_SOURCE();
  457. /* Check if USART1 clock selection is PCLK2 */
  458. if (srcclk == RCC_USART1CLKSOURCE_PCLK2)
  459. {
  460. frequency = HAL_RCC_GetPCLK2Freq();
  461. }
  462. /* Check if HSI is ready and if USART1 clock selection is HSI */
  463. else if (srcclk == RCC_USART1CLKSOURCE_HSI)
  464. {
  465. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  466. {
  467. if (READ_BIT(RCC->CR, RCC_CR_HSIDIVF) != 0U)
  468. {
  469. frequency = (HSI_VALUE >> 2U);
  470. }
  471. else
  472. {
  473. frequency = HSI_VALUE;
  474. }
  475. }
  476. }
  477. /* Check if USART1 clock selection is SYSCLK */
  478. else if (srcclk == RCC_USART1CLKSOURCE_SYSCLK)
  479. {
  480. frequency = HAL_RCC_GetSysClockFreq();
  481. }
  482. /* Check if LSE is ready and if USART1 clock selection is LSE */
  483. else if (srcclk == RCC_USART1CLKSOURCE_LSE)
  484. {
  485. if (HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSERDY))
  486. {
  487. frequency = LSE_VALUE;
  488. }
  489. }
  490. /* Clock not enabled for USART1*/
  491. else
  492. {
  493. frequency = 0U;
  494. }
  495. break;
  496. }
  497. #endif /* RCC_CCIPR_USART1SEL */
  498. case RCC_PERIPHCLK_USART2:
  499. {
  500. /* Get the current USART2 source */
  501. srcclk = __HAL_RCC_GET_USART2_SOURCE();
  502. /* Check if USART2 clock selection is PCLK1 */
  503. if (srcclk == RCC_USART2CLKSOURCE_PCLK1)
  504. {
  505. frequency = HAL_RCC_GetPCLK1Freq();
  506. }
  507. /* Check if HSI is ready and if USART2 clock selection is HSI */
  508. else if (srcclk == RCC_USART2CLKSOURCE_HSI)
  509. {
  510. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  511. {
  512. if (READ_BIT(RCC->CR, RCC_CR_HSIDIVF) != 0U)
  513. {
  514. frequency = (HSI_VALUE >> 2U);
  515. }
  516. else
  517. {
  518. frequency = HSI_VALUE;
  519. }
  520. }
  521. }
  522. /* Check if USART2 clock selection is SYSCLK */
  523. else if (srcclk == RCC_USART2CLKSOURCE_SYSCLK)
  524. {
  525. frequency = HAL_RCC_GetSysClockFreq();
  526. }
  527. /* Check if LSE is ready and if USART2 clock selection is LSE */
  528. else if (srcclk == RCC_USART2CLKSOURCE_LSE)
  529. {
  530. if (HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSERDY))
  531. {
  532. frequency = LSE_VALUE;
  533. }
  534. }
  535. /* Clock not enabled for USART2*/
  536. else
  537. {
  538. frequency = 0U;
  539. }
  540. break;
  541. }
  542. case RCC_PERIPHCLK_LPUART1:
  543. {
  544. /* Get the current LPUART1 source */
  545. srcclk = __HAL_RCC_GET_LPUART1_SOURCE();
  546. /* Check if LPUART1 clock selection is PCLK1 */
  547. if (srcclk == RCC_LPUART1CLKSOURCE_PCLK1)
  548. {
  549. frequency = HAL_RCC_GetPCLK1Freq();
  550. }
  551. /* Check if HSI is ready and if LPUART1 clock selection is HSI */
  552. else if (srcclk == RCC_LPUART1CLKSOURCE_HSI)
  553. {
  554. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  555. {
  556. if (READ_BIT(RCC->CR, RCC_CR_HSIDIVF) != 0U)
  557. {
  558. frequency = (HSI_VALUE >> 2U);
  559. }
  560. else
  561. {
  562. frequency = HSI_VALUE;
  563. }
  564. }
  565. }
  566. /* Check if LPUART1 clock selection is SYSCLK */
  567. else if (srcclk == RCC_LPUART1CLKSOURCE_SYSCLK)
  568. {
  569. frequency = HAL_RCC_GetSysClockFreq();
  570. }
  571. /* Check if LSE is ready and if LPUART1 clock selection is LSE */
  572. else if (srcclk == RCC_LPUART1CLKSOURCE_LSE)
  573. {
  574. if (HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSERDY))
  575. {
  576. frequency = LSE_VALUE;
  577. }
  578. }
  579. /* Clock not enabled for LPUART1*/
  580. else
  581. {
  582. frequency = 0U;
  583. }
  584. break;
  585. }
  586. case RCC_PERIPHCLK_I2C1:
  587. {
  588. /* Get the current I2C1 source */
  589. srcclk = __HAL_RCC_GET_I2C1_SOURCE();
  590. /* Check if I2C1 clock selection is PCLK1 */
  591. if (srcclk == RCC_I2C1CLKSOURCE_PCLK1)
  592. {
  593. frequency = HAL_RCC_GetPCLK1Freq();
  594. }
  595. /* Check if HSI is ready and if I2C1 clock selection is HSI */
  596. else if (srcclk == RCC_I2C1CLKSOURCE_HSI)
  597. {
  598. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  599. {
  600. if (READ_BIT(RCC->CR, RCC_CR_HSIDIVF) != 0U)
  601. {
  602. frequency = (HSI_VALUE >> 2U);
  603. }
  604. else
  605. {
  606. frequency = HSI_VALUE;
  607. }
  608. }
  609. }
  610. /* Check if I2C1 clock selection is SYSCLK */
  611. else if (srcclk == RCC_I2C1CLKSOURCE_SYSCLK)
  612. {
  613. frequency = HAL_RCC_GetSysClockFreq();
  614. }
  615. /* Clock not enabled for I2C1*/
  616. else
  617. {
  618. frequency = 0U;
  619. }
  620. break;
  621. }
  622. #if defined(I2C2)
  623. case RCC_PERIPHCLK_I2C2:
  624. {
  625. /* Check if I2C2 on APB1 clock enabled*/
  626. if (READ_BIT(RCC->APB1ENR, (RCC_APB1ENR_I2C2EN))==RCC_APB1ENR_I2C2EN)
  627. {
  628. frequency = HAL_RCC_GetPCLK1Freq();
  629. }
  630. else
  631. {
  632. frequency = 0U;
  633. }
  634. break;
  635. }
  636. #endif /* I2C2 */
  637. #if defined(RCC_CCIPR_I2C3SEL)
  638. case RCC_PERIPHCLK_I2C3:
  639. {
  640. /* Get the current I2C3 source */
  641. srcclk = __HAL_RCC_GET_I2C3_SOURCE();
  642. /* Check if I2C3 clock selection is PCLK1 */
  643. if (srcclk == RCC_I2C3CLKSOURCE_PCLK1)
  644. {
  645. frequency = HAL_RCC_GetPCLK1Freq();
  646. }
  647. /* Check if HSI is ready and if I2C3 clock selection is HSI */
  648. else if (srcclk == RCC_I2C3CLKSOURCE_HSI)
  649. {
  650. if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  651. {
  652. if (READ_BIT(RCC->CR, RCC_CR_HSIDIVF) != 0U)
  653. {
  654. frequency = (HSI_VALUE >> 2U);
  655. }
  656. else
  657. {
  658. frequency = HSI_VALUE;
  659. }
  660. }
  661. }
  662. /* Check if I2C3 clock selection is SYSCLK */
  663. else if (srcclk == RCC_I2C3CLKSOURCE_SYSCLK)
  664. {
  665. frequency = HAL_RCC_GetSysClockFreq();
  666. }
  667. /* Clock not enabled for I2C3*/
  668. else
  669. {
  670. frequency = 0U;
  671. }
  672. break;
  673. }
  674. #endif /* RCC_CCIPR_I2C3SEL */
  675. default:
  676. {
  677. break;
  678. }
  679. }
  680. return(frequency);
  681. }
  682. /**
  683. * @brief Enables the LSE Clock Security System.
  684. * @retval None
  685. */
  686. void HAL_RCCEx_EnableLSECSS(void)
  687. {
  688. SET_BIT(RCC->CSR, RCC_CSR_LSECSSON) ;
  689. }
  690. /**
  691. * @brief Disables the LSE Clock Security System.
  692. * @note Once enabled this bit cannot be disabled, except after an LSE failure detection
  693. * (LSECSSD=1). In that case the software MUST disable the LSECSSON bit.
  694. * Reset by power on reset and RTC software reset (RTCRST bit).
  695. * @retval None
  696. */
  697. void HAL_RCCEx_DisableLSECSS(void)
  698. {
  699. /* Disable LSE CSS */
  700. CLEAR_BIT(RCC->CSR, RCC_CSR_LSECSSON) ;
  701. /* Disable LSE CSS IT */
  702. __HAL_RCC_DISABLE_IT(RCC_IT_LSECSS);
  703. }
  704. /**
  705. * @brief Enable the LSE Clock Security System IT & corresponding EXTI line.
  706. * @note LSE Clock Security System IT is mapped on RTC EXTI line 19
  707. * @retval None
  708. */
  709. void HAL_RCCEx_EnableLSECSS_IT(void)
  710. {
  711. /* Enable LSE CSS */
  712. SET_BIT(RCC->CSR, RCC_CSR_LSECSSON) ;
  713. /* Enable LSE CSS IT */
  714. __HAL_RCC_ENABLE_IT(RCC_IT_LSECSS);
  715. /* Enable IT on EXTI Line 19 */
  716. __HAL_RCC_LSECSS_EXTI_ENABLE_IT();
  717. __HAL_RCC_LSECSS_EXTI_ENABLE_RISING_EDGE();
  718. }
  719. /**
  720. * @brief Handle the RCC LSE Clock Security System interrupt request.
  721. * @retval None
  722. */
  723. void HAL_RCCEx_LSECSS_IRQHandler(void)
  724. {
  725. /* Check RCC LSE CSSF flag */
  726. if(__HAL_RCC_GET_IT(RCC_IT_LSECSS))
  727. {
  728. /* RCC LSE Clock Security System interrupt user callback */
  729. HAL_RCCEx_LSECSS_Callback();
  730. /* Clear RCC LSE CSS pending bit */
  731. __HAL_RCC_CLEAR_IT(RCC_IT_LSECSS);
  732. }
  733. }
  734. /**
  735. * @brief RCCEx LSE Clock Security System interrupt callback.
  736. * @retval none
  737. */
  738. __weak void HAL_RCCEx_LSECSS_Callback(void)
  739. {
  740. /* NOTE : This function should not be modified, when the callback is needed,
  741. the @ref HAL_RCCEx_LSECSS_Callback should be implemented in the user file
  742. */
  743. }
  744. #if defined(SYSCFG_CFGR3_ENREF_HSI48)
  745. /**
  746. * @brief Enables Vrefint for the HSI48.
  747. * @note This is functional only if the LOCK is not set
  748. * @retval None
  749. */
  750. void HAL_RCCEx_EnableHSI48_VREFINT(void)
  751. {
  752. /* Enable the Buffer for the ADC by setting SYSCFG_CFGR3_ENREF_HSI48 bit in SYSCFG_CFGR3 register */
  753. SET_BIT (SYSCFG->CFGR3, SYSCFG_CFGR3_ENREF_HSI48);
  754. }
  755. /**
  756. * @brief Disables the Vrefint for the HSI48.
  757. * @note This is functional only if the LOCK is not set
  758. * @retval None
  759. */
  760. void HAL_RCCEx_DisableHSI48_VREFINT(void)
  761. {
  762. /* Disable the Vrefint by resetting SYSCFG_CFGR3_ENREF_HSI48 bit in SYSCFG_CFGR3 register */
  763. CLEAR_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENREF_HSI48);
  764. }
  765. #endif /* SYSCFG_CFGR3_ENREF_HSI48 */
  766. /**
  767. * @}
  768. */
  769. #if defined (CRS)
  770. /** @defgroup RCCEx_Exported_Functions_Group3 Extended Clock Recovery System Control functions
  771. * @brief Extended Clock Recovery System Control functions
  772. *
  773. @verbatim
  774. ===============================================================================
  775. ##### Extended Clock Recovery System Control functions #####
  776. ===============================================================================
  777. [..]
  778. For devices with Clock Recovery System feature (CRS), RCC Extention HAL driver can be used as follows:
  779. (#) In System clock config, HSI48 needs to be enabled
  780. (#) Enable CRS clock in IP MSP init which will use CRS functions
  781. (#) Call CRS functions as follows:
  782. (##) Prepare synchronization configuration necessary for HSI48 calibration
  783. (+++) Default values can be set for frequency Error Measurement (reload and error limit)
  784. and also HSI48 oscillator smooth trimming.
  785. (+++) Macro @ref __HAL_RCC_CRS_RELOADVALUE_CALCULATE can be also used to calculate
  786. directly reload value with target and synchronization frequencies values
  787. (##) Call function @ref HAL_RCCEx_CRSConfig which
  788. (+++) Reset CRS registers to their default values.
  789. (+++) Configure CRS registers with synchronization configuration
  790. (+++) Enable automatic calibration and frequency error counter feature
  791. Note: When using USB LPM (Link Power Management) and the device is in Sleep mode, the
  792. periodic USB SOF will not be generated by the host. No SYNC signal will therefore be
  793. provided to the CRS to calibrate the HSI48 on the run. To guarantee the required clock
  794. precision after waking up from Sleep mode, the LSE or reference clock on the GPIOs
  795. should be used as SYNC signal.
  796. (##) A polling function is provided to wait for complete synchronization
  797. (+++) Call function @ref HAL_RCCEx_CRSWaitSynchronization()
  798. (+++) According to CRS status, user can decide to adjust again the calibration or continue
  799. application if synchronization is OK
  800. (#) User can retrieve information related to synchronization in calling function
  801. @ref HAL_RCCEx_CRSGetSynchronizationInfo()
  802. (#) Regarding synchronization status and synchronization information, user can try a new calibration
  803. in changing synchronization configuration and call again HAL_RCCEx_CRSConfig.
  804. Note: When the SYNC event is detected during the downcounting phase (before reaching the zero value),
  805. it means that the actual frequency is lower than the target (and so, that the TRIM value should be
  806. incremented), while when it is detected during the upcounting phase it means that the actual frequency
  807. is higher (and that the TRIM value should be decremented).
  808. (#) In interrupt mode, user can resort to the available macros (__HAL_RCC_CRS_XXX_IT). Interrupts will go
  809. through CRS Handler (RCC_IRQn/RCC_IRQHandler)
  810. (++) Call function @ref HAL_RCCEx_CRSConfig()
  811. (++) Enable RCC_IRQn (thanks to NVIC functions)
  812. (++) Enable CRS interrupt (@ref __HAL_RCC_CRS_ENABLE_IT)
  813. (++) Implement CRS status management in the following user callbacks called from
  814. HAL_RCCEx_CRS_IRQHandler():
  815. (+++) @ref HAL_RCCEx_CRS_SyncOkCallback()
  816. (+++) @ref HAL_RCCEx_CRS_SyncWarnCallback()
  817. (+++) @ref HAL_RCCEx_CRS_ExpectedSyncCallback()
  818. (+++) @ref HAL_RCCEx_CRS_ErrorCallback()
  819. (#) To force a SYNC EVENT, user can use the function @ref HAL_RCCEx_CRSSoftwareSynchronizationGenerate().
  820. This function can be called before calling @ref HAL_RCCEx_CRSConfig (for instance in Systick handler)
  821. @endverbatim
  822. * @{
  823. */
  824. /**
  825. * @brief Start automatic synchronization for polling mode
  826. * @param pInit Pointer on RCC_CRSInitTypeDef structure
  827. * @retval None
  828. */
  829. void HAL_RCCEx_CRSConfig(RCC_CRSInitTypeDef *pInit)
  830. {
  831. uint32_t value;
  832. /* Check the parameters */
  833. assert_param(IS_RCC_CRS_SYNC_DIV(pInit->Prescaler));
  834. assert_param(IS_RCC_CRS_SYNC_SOURCE(pInit->Source));
  835. assert_param(IS_RCC_CRS_SYNC_POLARITY(pInit->Polarity));
  836. assert_param(IS_RCC_CRS_RELOADVALUE(pInit->ReloadValue));
  837. assert_param(IS_RCC_CRS_ERRORLIMIT(pInit->ErrorLimitValue));
  838. assert_param(IS_RCC_CRS_HSI48CALIBRATION(pInit->HSI48CalibrationValue));
  839. /* CONFIGURATION */
  840. /* Before configuration, reset CRS registers to their default values*/
  841. __HAL_RCC_CRS_FORCE_RESET();
  842. __HAL_RCC_CRS_RELEASE_RESET();
  843. /* Set the SYNCDIV[2:0] bits according to Prescaler value */
  844. /* Set the SYNCSRC[1:0] bits according to Source value */
  845. /* Set the SYNCSPOL bit according to Polarity value */
  846. value = (pInit->Prescaler | pInit->Source | pInit->Polarity);
  847. /* Set the RELOAD[15:0] bits according to ReloadValue value */
  848. value |= pInit->ReloadValue;
  849. /* Set the FELIM[7:0] bits according to ErrorLimitValue value */
  850. value |= (pInit->ErrorLimitValue << CRS_CFGR_FELIM_Pos);
  851. WRITE_REG(CRS->CFGR, value);
  852. /* Adjust HSI48 oscillator smooth trimming */
  853. /* Set the TRIM[5:0] bits according to RCC_CRS_HSI48CalibrationValue value */
  854. MODIFY_REG(CRS->CR, CRS_CR_TRIM, (pInit->HSI48CalibrationValue << CRS_CR_TRIM_Pos));
  855. /* START AUTOMATIC SYNCHRONIZATION*/
  856. /* Enable Automatic trimming & Frequency error counter */
  857. SET_BIT(CRS->CR, CRS_CR_AUTOTRIMEN | CRS_CR_CEN);
  858. }
  859. /**
  860. * @brief Generate the software synchronization event
  861. * @retval None
  862. */
  863. void HAL_RCCEx_CRSSoftwareSynchronizationGenerate(void)
  864. {
  865. SET_BIT(CRS->CR, CRS_CR_SWSYNC);
  866. }
  867. /**
  868. * @brief Return synchronization info
  869. * @param pSynchroInfo Pointer on RCC_CRSSynchroInfoTypeDef structure
  870. * @retval None
  871. */
  872. void HAL_RCCEx_CRSGetSynchronizationInfo(RCC_CRSSynchroInfoTypeDef *pSynchroInfo)
  873. {
  874. /* Check the parameter */
  875. assert_param(pSynchroInfo != (void *)NULL);
  876. /* Get the reload value */
  877. pSynchroInfo->ReloadValue = (uint32_t)(READ_BIT(CRS->CFGR, CRS_CFGR_RELOAD));
  878. /* Get HSI48 oscillator smooth trimming */
  879. pSynchroInfo->HSI48CalibrationValue = (uint32_t)(READ_BIT(CRS->CR, CRS_CR_TRIM) >> CRS_CR_TRIM_Pos);
  880. /* Get Frequency error capture */
  881. pSynchroInfo->FreqErrorCapture = (uint32_t)(READ_BIT(CRS->ISR, CRS_ISR_FECAP) >> CRS_ISR_FECAP_Pos);
  882. /* Get Frequency error direction */
  883. pSynchroInfo->FreqErrorDirection = (uint32_t)(READ_BIT(CRS->ISR, CRS_ISR_FEDIR));
  884. }
  885. /**
  886. * @brief Wait for CRS Synchronization status.
  887. * @param Timeout Duration of the timeout
  888. * @note Timeout is based on the maximum time to receive a SYNC event based on synchronization
  889. * frequency.
  890. * @note If Timeout set to HAL_MAX_DELAY, HAL_TIMEOUT will be never returned.
  891. * @retval Combination of Synchronization status
  892. * This parameter can be a combination of the following values:
  893. * @arg @ref RCC_CRS_TIMEOUT
  894. * @arg @ref RCC_CRS_SYNCOK
  895. * @arg @ref RCC_CRS_SYNCWARN
  896. * @arg @ref RCC_CRS_SYNCERR
  897. * @arg @ref RCC_CRS_SYNCMISS
  898. * @arg @ref RCC_CRS_TRIMOVF
  899. */
  900. uint32_t HAL_RCCEx_CRSWaitSynchronization(uint32_t Timeout)
  901. {
  902. uint32_t crsstatus = RCC_CRS_NONE;
  903. uint32_t tickstart;
  904. /* Get timeout */
  905. tickstart = HAL_GetTick();
  906. /* Wait for CRS flag or timeout detection */
  907. do
  908. {
  909. if(Timeout != HAL_MAX_DELAY)
  910. {
  911. if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  912. {
  913. crsstatus = RCC_CRS_TIMEOUT;
  914. }
  915. }
  916. /* Check CRS SYNCOK flag */
  917. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCOK))
  918. {
  919. /* CRS SYNC event OK */
  920. crsstatus |= RCC_CRS_SYNCOK;
  921. /* Clear CRS SYNC event OK bit */
  922. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCOK);
  923. }
  924. /* Check CRS SYNCWARN flag */
  925. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCWARN))
  926. {
  927. /* CRS SYNC warning */
  928. crsstatus |= RCC_CRS_SYNCWARN;
  929. /* Clear CRS SYNCWARN bit */
  930. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCWARN);
  931. }
  932. /* Check CRS TRIM overflow flag */
  933. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_TRIMOVF))
  934. {
  935. /* CRS SYNC Error */
  936. crsstatus |= RCC_CRS_TRIMOVF;
  937. /* Clear CRS Error bit */
  938. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_TRIMOVF);
  939. }
  940. /* Check CRS Error flag */
  941. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCERR))
  942. {
  943. /* CRS SYNC Error */
  944. crsstatus |= RCC_CRS_SYNCERR;
  945. /* Clear CRS Error bit */
  946. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCERR);
  947. }
  948. /* Check CRS SYNC Missed flag */
  949. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCMISS))
  950. {
  951. /* CRS SYNC Missed */
  952. crsstatus |= RCC_CRS_SYNCMISS;
  953. /* Clear CRS SYNC Missed bit */
  954. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCMISS);
  955. }
  956. /* Check CRS Expected SYNC flag */
  957. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_ESYNC))
  958. {
  959. /* frequency error counter reached a zero value */
  960. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_ESYNC);
  961. }
  962. } while(RCC_CRS_NONE == crsstatus);
  963. return crsstatus;
  964. }
  965. /**
  966. * @brief Handle the Clock Recovery System interrupt request.
  967. * @retval None
  968. */
  969. void HAL_RCCEx_CRS_IRQHandler(void)
  970. {
  971. uint32_t crserror = RCC_CRS_NONE;
  972. /* Get current IT flags and IT sources values */
  973. uint32_t itflags = READ_REG(CRS->ISR);
  974. uint32_t itsources = READ_REG(CRS->CR);
  975. /* Check CRS SYNCOK flag */
  976. if(((itflags & RCC_CRS_FLAG_SYNCOK) != 0U) && ((itsources & RCC_CRS_IT_SYNCOK) != 0U))
  977. {
  978. /* Clear CRS SYNC event OK flag */
  979. WRITE_REG(CRS->ICR, CRS_ICR_SYNCOKC);
  980. /* user callback */
  981. HAL_RCCEx_CRS_SyncOkCallback();
  982. }
  983. /* Check CRS SYNCWARN flag */
  984. else if(((itflags & RCC_CRS_FLAG_SYNCWARN) != 0U) && ((itsources & RCC_CRS_IT_SYNCWARN) != 0U))
  985. {
  986. /* Clear CRS SYNCWARN flag */
  987. WRITE_REG(CRS->ICR, CRS_ICR_SYNCWARNC);
  988. /* user callback */
  989. HAL_RCCEx_CRS_SyncWarnCallback();
  990. }
  991. /* Check CRS Expected SYNC flag */
  992. else if(((itflags & RCC_CRS_FLAG_ESYNC) != 0U) && ((itsources & RCC_CRS_IT_ESYNC) != 0U))
  993. {
  994. /* frequency error counter reached a zero value */
  995. WRITE_REG(CRS->ICR, CRS_ICR_ESYNCC);
  996. /* user callback */
  997. HAL_RCCEx_CRS_ExpectedSyncCallback();
  998. }
  999. /* Check CRS Error flags */
  1000. else
  1001. {
  1002. if(((itflags & RCC_CRS_FLAG_ERR) != 0U) && ((itsources & RCC_CRS_IT_ERR) != 0U))
  1003. {
  1004. if((itflags & RCC_CRS_FLAG_SYNCERR) != 0U)
  1005. {
  1006. crserror |= RCC_CRS_SYNCERR;
  1007. }
  1008. if((itflags & RCC_CRS_FLAG_SYNCMISS) != 0U)
  1009. {
  1010. crserror |= RCC_CRS_SYNCMISS;
  1011. }
  1012. if((itflags & RCC_CRS_FLAG_TRIMOVF) != 0U)
  1013. {
  1014. crserror |= RCC_CRS_TRIMOVF;
  1015. }
  1016. /* Clear CRS Error flags */
  1017. WRITE_REG(CRS->ICR, CRS_ICR_ERRC);
  1018. /* user error callback */
  1019. HAL_RCCEx_CRS_ErrorCallback(crserror);
  1020. }
  1021. }
  1022. }
  1023. /**
  1024. * @brief RCCEx Clock Recovery System SYNCOK interrupt callback.
  1025. * @retval none
  1026. */
  1027. __weak void HAL_RCCEx_CRS_SyncOkCallback(void)
  1028. {
  1029. /* NOTE : This function should not be modified, when the callback is needed,
  1030. the @ref HAL_RCCEx_CRS_SyncOkCallback should be implemented in the user file
  1031. */
  1032. }
  1033. /**
  1034. * @brief RCCEx Clock Recovery System SYNCWARN interrupt callback.
  1035. * @retval none
  1036. */
  1037. __weak void HAL_RCCEx_CRS_SyncWarnCallback(void)
  1038. {
  1039. /* NOTE : This function should not be modified, when the callback is needed,
  1040. the @ref HAL_RCCEx_CRS_SyncWarnCallback should be implemented in the user file
  1041. */
  1042. }
  1043. /**
  1044. * @brief RCCEx Clock Recovery System Expected SYNC interrupt callback.
  1045. * @retval none
  1046. */
  1047. __weak void HAL_RCCEx_CRS_ExpectedSyncCallback(void)
  1048. {
  1049. /* NOTE : This function should not be modified, when the callback is needed,
  1050. the @ref HAL_RCCEx_CRS_ExpectedSyncCallback should be implemented in the user file
  1051. */
  1052. }
  1053. /**
  1054. * @brief RCCEx Clock Recovery System Error interrupt callback.
  1055. * @param Error Combination of Error status.
  1056. * This parameter can be a combination of the following values:
  1057. * @arg @ref RCC_CRS_SYNCERR
  1058. * @arg @ref RCC_CRS_SYNCMISS
  1059. * @arg @ref RCC_CRS_TRIMOVF
  1060. * @retval none
  1061. */
  1062. __weak void HAL_RCCEx_CRS_ErrorCallback(uint32_t Error)
  1063. {
  1064. /* Prevent unused argument(s) compilation warning */
  1065. UNUSED(Error);
  1066. /* NOTE : This function should not be modified, when the callback is needed,
  1067. the @ref HAL_RCCEx_CRS_ErrorCallback should be implemented in the user file
  1068. */
  1069. }
  1070. /**
  1071. * @}
  1072. */
  1073. #endif /* CRS */
  1074. /**
  1075. * @}
  1076. */
  1077. /**
  1078. * @}
  1079. */
  1080. /**
  1081. * @}
  1082. */
  1083. #endif /* HAL_RCC_MODULE_ENABLED */
  1084. /**
  1085. * @}
  1086. */
  1087. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/