stm32l0xx_hal_rcc_ex.c 40 KB

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