stm32g0xx_hal_rcc_ex.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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. * Copyright (c) 2018 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file in
  19. * the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32g0xx_hal.h"
  25. /** @addtogroup STM32G0xx_HAL_Driver
  26. * @{
  27. */
  28. /** @defgroup RCCEx RCCEx
  29. * @brief RCC Extended HAL module driver
  30. * @{
  31. */
  32. #ifdef HAL_RCC_MODULE_ENABLED
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private defines -----------------------------------------------------------*/
  35. /** @defgroup RCCEx_Private_Constants RCCEx Private Constants
  36. * @{
  37. */
  38. #define PLL_TIMEOUT_VALUE 100U /* 100 ms (minimum Tick + 1) */
  39. #define LSCO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  40. #define LSCO_GPIO_PORT GPIOA
  41. #define LSCO_PIN GPIO_PIN_2
  42. /**
  43. * @}
  44. */
  45. /* Private macros ------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private function prototypes -----------------------------------------------*/
  48. /* Exported functions --------------------------------------------------------*/
  49. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  50. * @{
  51. */
  52. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  53. * @brief Extended Peripheral Control functions
  54. *
  55. @verbatim
  56. ===============================================================================
  57. ##### Extended Peripheral Control functions #####
  58. ===============================================================================
  59. [..]
  60. This subsection provides a set of functions allowing to control the RCC Clocks
  61. frequencies.
  62. [..]
  63. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  64. select the RTC clock source; in this case the Backup domain will be reset in
  65. order to modify the RTC Clock source, as consequence RTC registers (including
  66. the backup registers) and RCC_BDCR register are set to their reset values.
  67. @endverbatim
  68. * @{
  69. */
  70. /**
  71. * @brief Initialize the RCC extended peripherals clocks according to the specified
  72. * parameters in the @ref RCC_PeriphCLKInitTypeDef.
  73. * @param PeriphClkInit pointer to a @ref RCC_PeriphCLKInitTypeDef structure that
  74. * contains a field PeriphClockSelection which can be a combination of the following values:
  75. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  76. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  77. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  78. * @arg @ref RCC_PERIPHCLK_I2C2 I2C2 peripheral clock (2)
  79. * @arg @ref RCC_PERIPHCLK_I2S1 I2S1 peripheral clock
  80. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock (1)
  81. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  82. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock (1)
  83. * @arg @ref RCC_PERIPHCLK_LPTIM1 LPTIM1 peripheral clock (1)
  84. * @arg @ref RCC_PERIPHCLK_LPTIM2 LPTIM2 peripheral clock (1)
  85. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock (1)
  86. * @arg @ref RCC_PERIPHCLK_LPUART2 LPUART2 peripheral clock (1)
  87. * @arg @ref RCC_PERIPHCLK_RNG RNG peripheral clock (1)
  88. * @arg @ref RCC_PERIPHCLK_TIM1 TIM1 peripheral clock (1)(2)
  89. * @arg @ref RCC_PERIPHCLK_TIM15 TIM15 peripheral clock (1)(2)
  90. * @arg @ref RCC_PERIPHCLK_USART2 USART2 peripheral clock (2)
  91. * @arg @ref RCC_PERIPHCLK_USART3 USART3 peripheral clock (2)
  92. * @arg @ref RCC_PERIPHCLK_FDCAN FDCAN peripheral clock (1)
  93. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (1)
  94. *
  95. * @note (1) Peripherals are not available on all devices
  96. * @note (2) Peripherals clock selection is not available on all devices
  97. * @note Care must be taken when @ref HAL_RCCEx_PeriphCLKConfig() is used to select
  98. * the RTC clock source: in this case the access to Backup domain is enabled.
  99. *
  100. * @retval HAL status
  101. */
  102. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  103. {
  104. uint32_t tmpregister;
  105. uint32_t tickstart;
  106. HAL_StatusTypeDef ret = HAL_OK; /* Intermediate status */
  107. HAL_StatusTypeDef status = HAL_OK; /* Final status */
  108. /* Check the parameters */
  109. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  110. /*-------------------------- RTC clock source configuration ----------------------*/
  111. if ((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC)
  112. {
  113. FlagStatus pwrclkchanged = RESET;
  114. /* Check for RTC Parameters used to output RTCCLK */
  115. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  116. /* Enable Power Clock */
  117. if (__HAL_RCC_PWR_IS_CLK_DISABLED())
  118. {
  119. __HAL_RCC_PWR_CLK_ENABLE();
  120. pwrclkchanged = SET;
  121. }
  122. /* Enable write access to Backup domain */
  123. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  124. /* Wait for Backup domain Write protection disable */
  125. tickstart = HAL_GetTick();
  126. while ((PWR->CR1 & PWR_CR1_DBP) == 0U)
  127. {
  128. if ((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  129. {
  130. ret = HAL_TIMEOUT;
  131. break;
  132. }
  133. }
  134. if (ret == HAL_OK)
  135. {
  136. /* Reset the Backup domain only if the RTC Clock source selection is modified from default */
  137. tmpregister = READ_BIT(RCC->BDCR, RCC_BDCR_RTCSEL);
  138. /* Reset the Backup domain only if the RTC Clock source selection is modified */
  139. if ((tmpregister != RCC_RTCCLKSOURCE_NONE) && (tmpregister != PeriphClkInit->RTCClockSelection))
  140. {
  141. /* Store the content of BDCR register before the reset of Backup Domain */
  142. tmpregister = READ_BIT(RCC->BDCR, ~(RCC_BDCR_RTCSEL));
  143. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  144. __HAL_RCC_BACKUPRESET_FORCE();
  145. __HAL_RCC_BACKUPRESET_RELEASE();
  146. /* Restore the Content of BDCR register */
  147. RCC->BDCR = tmpregister;
  148. }
  149. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  150. if (HAL_IS_BIT_SET(tmpregister, RCC_BDCR_LSEON))
  151. {
  152. /* Get Start Tick*/
  153. tickstart = HAL_GetTick();
  154. /* Wait till LSE is ready */
  155. while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  156. {
  157. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  158. {
  159. ret = HAL_TIMEOUT;
  160. break;
  161. }
  162. }
  163. }
  164. if (ret == HAL_OK)
  165. {
  166. /* Apply new RTC clock source selection */
  167. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  168. }
  169. else
  170. {
  171. /* set overall return value */
  172. status = ret;
  173. }
  174. }
  175. else
  176. {
  177. /* set overall return value */
  178. status = ret;
  179. }
  180. /* Restore clock configuration if changed */
  181. if (pwrclkchanged == SET)
  182. {
  183. __HAL_RCC_PWR_CLK_DISABLE();
  184. }
  185. }
  186. /*-------------------------- USART1 clock source configuration -------------------*/
  187. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART1) == RCC_PERIPHCLK_USART1)
  188. {
  189. /* Check the parameters */
  190. assert_param(IS_RCC_USART1CLKSOURCE(PeriphClkInit->Usart1ClockSelection));
  191. /* Configure the USART1 clock source */
  192. __HAL_RCC_USART1_CONFIG(PeriphClkInit->Usart1ClockSelection);
  193. }
  194. #if defined(RCC_CCIPR_USART2SEL)
  195. /*-------------------------- USART2 clock source configuration -------------------*/
  196. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART2) == RCC_PERIPHCLK_USART2)
  197. {
  198. /* Check the parameters */
  199. assert_param(IS_RCC_USART2CLKSOURCE(PeriphClkInit->Usart2ClockSelection));
  200. /* Configure the USART2 clock source */
  201. __HAL_RCC_USART2_CONFIG(PeriphClkInit->Usart2ClockSelection);
  202. }
  203. #endif /* RCC_CCIPR_USART2SEL */
  204. #if defined(RCC_CCIPR_USART3SEL)
  205. /*-------------------------- USART3 clock source configuration -------------------*/
  206. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART3) == RCC_PERIPHCLK_USART3)
  207. {
  208. /* Check the parameters */
  209. assert_param(IS_RCC_USART3CLKSOURCE(PeriphClkInit->Usart3ClockSelection));
  210. /* Configure the USART3 clock source */
  211. __HAL_RCC_USART3_CONFIG(PeriphClkInit->Usart3ClockSelection);
  212. }
  213. #endif /* RCC_CCIPR_USART3SEL */
  214. #if defined(LPUART1)
  215. /*-------------------------- LPUART1 clock source configuration ------------------*/
  216. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPUART1) == RCC_PERIPHCLK_LPUART1)
  217. {
  218. /* Check the parameters */
  219. assert_param(IS_RCC_LPUART1CLKSOURCE(PeriphClkInit->Lpuart1ClockSelection));
  220. /* Configure the LPUART1 clock source */
  221. __HAL_RCC_LPUART1_CONFIG(PeriphClkInit->Lpuart1ClockSelection);
  222. }
  223. #endif /* LPUART1 */
  224. #if defined(LPUART2)
  225. /*-------------------------- LPUART2 clock source configuration ------------------*/
  226. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPUART2) == RCC_PERIPHCLK_LPUART2)
  227. {
  228. /* Check the parameters */
  229. assert_param(IS_RCC_LPUART2CLKSOURCE(PeriphClkInit->Lpuart2ClockSelection));
  230. /* Configure the LPUART clock source */
  231. __HAL_RCC_LPUART2_CONFIG(PeriphClkInit->Lpuart2ClockSelection);
  232. }
  233. #endif /* LPUART2 */
  234. #if defined(RCC_CCIPR_LPTIM1SEL)
  235. /*-------------------------- LPTIM1 clock source configuration -------------------*/
  236. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == (RCC_PERIPHCLK_LPTIM1))
  237. {
  238. assert_param(IS_RCC_LPTIM1CLKSOURCE(PeriphClkInit->Lptim1ClockSelection));
  239. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->Lptim1ClockSelection);
  240. }
  241. #endif /* RCC_CCIPR_LPTIM1SEL */
  242. #if defined(RCC_CCIPR_LPTIM2SEL)
  243. /*-------------------------- LPTIM2 clock source configuration -------------------*/
  244. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM2) == (RCC_PERIPHCLK_LPTIM2))
  245. {
  246. assert_param(IS_RCC_LPTIM2CLKSOURCE(PeriphClkInit->Lptim2ClockSelection));
  247. __HAL_RCC_LPTIM2_CONFIG(PeriphClkInit->Lptim2ClockSelection);
  248. }
  249. #endif /* RCC_CCIPR_LPTIM2SEL */
  250. /*-------------------------- I2C1 clock source configuration ---------------------*/
  251. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C1) == RCC_PERIPHCLK_I2C1)
  252. {
  253. /* Check the parameters */
  254. assert_param(IS_RCC_I2C1CLKSOURCE(PeriphClkInit->I2c1ClockSelection));
  255. /* Configure the I2C1 clock source */
  256. __HAL_RCC_I2C1_CONFIG(PeriphClkInit->I2c1ClockSelection);
  257. }
  258. #if defined(RCC_CCIPR_I2C2SEL)
  259. /*-------------------------- I2C2 clock source configuration ---------------------*/
  260. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C2) == RCC_PERIPHCLK_I2C2)
  261. {
  262. /* Check the parameters */
  263. assert_param(IS_RCC_I2C2CLKSOURCE(PeriphClkInit->I2c2ClockSelection));
  264. /* Configure the I2C2 clock source */
  265. __HAL_RCC_I2C2_CONFIG(PeriphClkInit->I2c2ClockSelection);
  266. }
  267. #endif /* (RCC_CCIPR_I2C2SEL */
  268. #if defined(RNG)
  269. /*-------------------------- RNG clock source configuration ----------------------*/
  270. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RNG) == (RCC_PERIPHCLK_RNG))
  271. {
  272. assert_param(IS_RCC_RNGCLKSOURCE(PeriphClkInit->RngClockSelection));
  273. __HAL_RCC_RNG_CONFIG(PeriphClkInit->RngClockSelection);
  274. if (PeriphClkInit->RngClockSelection == RCC_RNGCLKSOURCE_PLL)
  275. {
  276. /* Enable PLLQCLK output */
  277. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLLQCLK);
  278. }
  279. }
  280. #endif /* RNG */
  281. /*-------------------------- ADC clock source configuration ----------------------*/
  282. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC) == RCC_PERIPHCLK_ADC)
  283. {
  284. /* Check the parameters */
  285. assert_param(IS_RCC_ADCCLKSOURCE(PeriphClkInit->AdcClockSelection));
  286. /* Configure the ADC interface clock source */
  287. __HAL_RCC_ADC_CONFIG(PeriphClkInit->AdcClockSelection);
  288. if (PeriphClkInit->AdcClockSelection == RCC_ADCCLKSOURCE_PLLADC)
  289. {
  290. /* Enable PLLPCLK output */
  291. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLLPCLK);
  292. }
  293. }
  294. #if defined(CEC)
  295. /*-------------------------- CEC clock source configuration ---------------------*/
  296. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CEC) == RCC_PERIPHCLK_CEC)
  297. {
  298. /* Check the parameters */
  299. assert_param(IS_RCC_CECCLKSOURCE(PeriphClkInit->CecClockSelection));
  300. /* Configure the CEC clock source */
  301. __HAL_RCC_CEC_CONFIG(PeriphClkInit->CecClockSelection);
  302. }
  303. #endif /* CEC */
  304. #if defined(RCC_CCIPR_TIM1SEL)
  305. /*-------------------------- TIM1 clock source configuration ---------------------*/
  306. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM1) == RCC_PERIPHCLK_TIM1)
  307. {
  308. /* Check the parameters */
  309. assert_param(IS_RCC_TIM1CLKSOURCE(PeriphClkInit->Tim1ClockSelection));
  310. /* Configure the TIM1 clock source */
  311. __HAL_RCC_TIM1_CONFIG(PeriphClkInit->Tim1ClockSelection);
  312. if (PeriphClkInit->Tim1ClockSelection == RCC_TIM1CLKSOURCE_PLL)
  313. {
  314. /* Enable PLLQCLK output */
  315. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLLQCLK);
  316. }
  317. }
  318. #endif /* RCC_CCIPR_TIM1SEL */
  319. #if defined(RCC_CCIPR_TIM15SEL)
  320. /*-------------------------- TIM15 clock source configuration ---------------------*/
  321. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM15) == RCC_PERIPHCLK_TIM15)
  322. {
  323. /* Check the parameters */
  324. assert_param(IS_RCC_TIM15CLKSOURCE(PeriphClkInit->Tim15ClockSelection));
  325. /* Configure the TIM15 clock source */
  326. __HAL_RCC_TIM15_CONFIG(PeriphClkInit->Tim15ClockSelection);
  327. if (PeriphClkInit->Tim15ClockSelection == RCC_TIM15CLKSOURCE_PLL)
  328. {
  329. /* Enable PLLQCLK output */
  330. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLLQCLK);
  331. }
  332. }
  333. #endif /* RCC_CCIPR_TIM15SEL */
  334. /*-------------------------- I2S1 clock source configuration ---------------------*/
  335. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S1) == RCC_PERIPHCLK_I2S1)
  336. {
  337. /* Check the parameters */
  338. assert_param(IS_RCC_I2S1CLKSOURCE(PeriphClkInit->I2s1ClockSelection));
  339. /* Configure the I2S1 clock source */
  340. __HAL_RCC_I2S1_CONFIG(PeriphClkInit->I2s1ClockSelection);
  341. if (PeriphClkInit->I2s1ClockSelection == RCC_I2S1CLKSOURCE_PLL)
  342. {
  343. /* Enable PLLPCLK output */
  344. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLLPCLK);
  345. }
  346. }
  347. #if defined(RCC_CCIPR2_I2S2SEL)
  348. /*-------------------------- I2S2 clock source configuration ---------------------*/
  349. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S2) == RCC_PERIPHCLK_I2S2)
  350. {
  351. /* Check the parameters */
  352. assert_param(IS_RCC_I2S2CLKSOURCE(PeriphClkInit->I2s2ClockSelection));
  353. /* Configure the I2S2 clock source */
  354. __HAL_RCC_I2S2_CONFIG(PeriphClkInit->I2s2ClockSelection);
  355. if (PeriphClkInit->I2s2ClockSelection == RCC_I2S2CLKSOURCE_PLL)
  356. {
  357. /* Enable PLLPCLK output */
  358. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLLPCLK);
  359. }
  360. }
  361. #endif /* RCC_CCIPR2_I2S2SEL */
  362. #if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
  363. /*-------------------------- USB clock source configuration ---------------------*/
  364. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == RCC_PERIPHCLK_USB)
  365. {
  366. /* Check the parameters */
  367. assert_param(IS_RCC_USBCLKSOURCE(PeriphClkInit->UsbClockSelection));
  368. /* Configure the USB clock source */
  369. __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
  370. if (PeriphClkInit->UsbClockSelection == RCC_USBCLKSOURCE_PLL)
  371. {
  372. /* Enable PLLQCLK output */
  373. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLLQCLK);
  374. }
  375. }
  376. #endif /* STM32G0C1xx || STM32G0B1xx || STM32G0B0xx */
  377. #if defined(FDCAN1) || defined(FDCAN2)
  378. /*-------------------------- FDCAN clock source configuration ---------------------*/
  379. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_FDCAN) == RCC_PERIPHCLK_FDCAN)
  380. {
  381. /* Check the parameters */
  382. assert_param(IS_RCC_FDCANCLKSOURCE(PeriphClkInit->FdcanClockSelection));
  383. /* Configure the FDCAN clock source */
  384. __HAL_RCC_FDCAN_CONFIG(PeriphClkInit->FdcanClockSelection);
  385. if (PeriphClkInit->FdcanClockSelection == RCC_FDCANCLKSOURCE_PLL)
  386. {
  387. /* Enable PLLQCLK output */
  388. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLLQCLK);
  389. }
  390. }
  391. #endif /* FDCAN1 || FDCAN2 */
  392. return status;
  393. }
  394. /**
  395. * @brief Get the RCC_ClkInitStruct according to the internal RCC configuration registers.
  396. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  397. * returns the configuration information for the Extended Peripherals
  398. * clocks: I2C1, I2S1, USART1, RTC, ADC,
  399. * LPTIM1 (1), LPTIM2 (1), TIM1 (2), TIM15 (1)(2), USART2 (2), LPUART1 (1), CEC (1) and RNG (1)
  400. * @note (1) Peripheral is not available on all devices
  401. * @note (2) Peripheral clock selection is not available on all devices
  402. * @retval None
  403. */
  404. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  405. {
  406. /* Set all possible values for the extended clock type parameter------------*/
  407. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2S1 | \
  408. RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_RTC ;
  409. #if defined(RCC_CCIPR_LPTIM1SEL) && defined(RCC_CCIPR_LPTIM2SEL)
  410. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_LPTIM1;
  411. #endif /* RCC_CCIPR_LPTIM1SEL && RCC_CCIPR_LPTIM2SEL */
  412. #if defined(RCC_CCIPR_RNGSEL)
  413. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_RNG;
  414. #endif /* RCC_CCIPR_RNGSEL */
  415. #if defined(RCC_CCIPR_LPUART1SEL)
  416. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_LPUART1;
  417. #endif /* RCC_CCIPR_LPUART1SEL */
  418. #if defined(RCC_CCIPR_LPUART2SEL)
  419. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_LPUART2;
  420. #endif /* RCC_CCIPR_LPUART2SEL */
  421. #if defined(RCC_CCIPR_CECSEL)
  422. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_CEC;
  423. #endif /* RCC_CCIPR_CECSEL */
  424. #if defined(RCC_CCIPR_TIM1SEL)
  425. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM1;
  426. #endif /* RCC_CCIPR_TIM1SEL */
  427. #if defined(RCC_CCIPR_TIM15SEL)
  428. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM15;
  429. #endif /* RCC_CCIPR_TIM15SEL */
  430. #if defined(RCC_CCIPR_USART2SEL)
  431. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USART2;
  432. #endif /* RCC_CCIPR_USART2SEL */
  433. #if defined(RCC_CCIPR_USART3SEL)
  434. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USART3;
  435. #endif /* RCC_CCIPR_USART3SEL */
  436. #if defined(RCC_CCIPR_I2C2SEL)
  437. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2C2;
  438. #endif /* RCC_CCIPR_I2C2SEL */
  439. #if defined(RCC_CCIPR2_I2S2SEL)
  440. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S2;
  441. #endif /* RCC_CCIPR2_I2S2SEL */
  442. #if defined(RCC_CCIPR2_USBSEL)
  443. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USB;
  444. #endif /* RCC_CCIPR2_USBSEL */
  445. #if defined(RCC_CCIPR2_FDCANSEL)
  446. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_FDCAN;
  447. #endif /* RCC_CCIPR_FDCANSEL */
  448. /* Get the USART1 clock source ---------------------------------------------*/
  449. PeriphClkInit->Usart1ClockSelection = __HAL_RCC_GET_USART1_SOURCE();
  450. #if defined(RCC_CCIPR_USART2SEL)
  451. /* Get the USART2 clock source ---------------------------------------------*/
  452. PeriphClkInit->Usart2ClockSelection = __HAL_RCC_GET_USART2_SOURCE();
  453. #endif /* RCC_CCIPR_USART2SEL */
  454. #if defined(RCC_CCIPR_USART3SEL)
  455. /* Get the USART3 clock source ---------------------------------------------*/
  456. PeriphClkInit->Usart3ClockSelection = __HAL_RCC_GET_USART3_SOURCE();
  457. #endif /* RCC_CCIPR_USART3SEL */
  458. #if defined(RCC_CCIPR_LPUART1SEL)
  459. /* Get the LPUART1 clock source --------------------------------------------*/
  460. PeriphClkInit->Lpuart1ClockSelection = __HAL_RCC_GET_LPUART1_SOURCE();
  461. #endif /* RCC_CCIPR_LPUART1SEL */
  462. #if defined(RCC_CCIPR_LPUART2SEL)
  463. /* Get the LPUART2 clock source --------------------------------------------*/
  464. PeriphClkInit->Lpuart2ClockSelection = __HAL_RCC_GET_LPUART2_SOURCE();
  465. #endif /* RCC_CCIPR_LPUART2SEL */
  466. /* Get the I2C1 clock source -----------------------------------------------*/
  467. PeriphClkInit->I2c1ClockSelection = __HAL_RCC_GET_I2C1_SOURCE();
  468. #if defined(RCC_CCIPR_I2C2SEL)
  469. /* Get the I2C2 clock source -----------------------------------------------*/
  470. PeriphClkInit->I2c2ClockSelection = __HAL_RCC_GET_I2C2_SOURCE();
  471. #endif /* RCC_CCIPR_I2C2SEL */
  472. #if defined(RCC_CCIPR_LPTIM1SEL)
  473. /* Get the LPTIM1 clock source ---------------------------------------------*/
  474. PeriphClkInit->Lptim1ClockSelection = __HAL_RCC_GET_LPTIM1_SOURCE();
  475. #endif /* RCC_CCIPR_LPTIM1SEL */
  476. #if defined(RCC_CCIPR_LPTIM2SEL)
  477. /* Get the LPTIM2 clock source ---------------------------------------------*/
  478. PeriphClkInit->Lptim2ClockSelection = __HAL_RCC_GET_LPTIM2_SOURCE();
  479. #endif /* RCC_CCIPR_LPTIM2SEL */
  480. #if defined(RCC_CCIPR_TIM1SEL)
  481. /* Get the TIM1 clock source ---------------------------------------------*/
  482. PeriphClkInit->Tim1ClockSelection = __HAL_RCC_GET_TIM1_SOURCE();
  483. #endif /* RCC_CCIPR_TIM1SEL */
  484. #if defined(RCC_CCIPR_TIM15SEL)
  485. /* Get the TIM15 clock source ---------------------------------------------*/
  486. PeriphClkInit->Tim15ClockSelection = __HAL_RCC_GET_TIM15_SOURCE();
  487. #endif /* RCC_CCIPR_TIM15SEL */
  488. /* Get the RTC clock source ------------------------------------------------*/
  489. PeriphClkInit->RTCClockSelection = __HAL_RCC_GET_RTC_SOURCE();
  490. #if defined(RCC_CCIPR_RNGSEL)
  491. /* Get the RNG clock source ------------------------------------------------*/
  492. PeriphClkInit->RngClockSelection = __HAL_RCC_GET_RNG_SOURCE();
  493. #endif /* RCC_CCIPR_RNGSEL */
  494. /* Get the ADC clock source -----------------------------------------------*/
  495. PeriphClkInit->AdcClockSelection = __HAL_RCC_GET_ADC_SOURCE();
  496. #if defined(RCC_CCIPR_CECSEL)
  497. /* Get the CEC clock source -----------------------------------------------*/
  498. PeriphClkInit->CecClockSelection = __HAL_RCC_GET_CEC_SOURCE();
  499. #endif /* RCC_CCIPR_CECSEL */
  500. #if defined(RCC_CCIPR2_USBSEL)
  501. /* Get the USB clock source -----------------------------------------------*/
  502. PeriphClkInit->UsbClockSelection = __HAL_RCC_GET_USB_SOURCE();
  503. #endif /* RCC_CCIPR2_USBSEL */
  504. #if defined(RCC_CCIPR2_FDCANSEL)
  505. /* Get the FDCAN clock source -----------------------------------------------*/
  506. PeriphClkInit->FdcanClockSelection = __HAL_RCC_GET_FDCAN_SOURCE();
  507. #endif /* RCC_CCIPR2_FDCANSEL */
  508. /* Get the I2S1 clock source -----------------------------------------------*/
  509. PeriphClkInit->I2s1ClockSelection = __HAL_RCC_GET_I2S1_SOURCE();
  510. #if defined(RCC_CCIPR2_I2S2SEL)
  511. /* Get the I2S2 clock source -----------------------------------------------*/
  512. PeriphClkInit->I2s2ClockSelection = __HAL_RCC_GET_I2S2_SOURCE();
  513. #endif /* RCC_CCIPR2_I2S2SEL */
  514. }
  515. /**
  516. * @brief Return the peripheral clock frequency for peripherals with clock source from PLL
  517. * @note Return 0 if peripheral clock identifier not managed by this API
  518. * @param PeriphClk Peripheral clock identifier
  519. * This parameter can be one of the following values:
  520. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  521. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  522. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  523. * @arg @ref RCC_PERIPHCLK_I2C2 I2C2 peripheral clock (1)
  524. * @arg @ref RCC_PERIPHCLK_I2S1 I2S1 peripheral clock
  525. * @arg @ref RCC_PERIPHCLK_I2S2 I2S2 peripheral clock (1)
  526. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  527. * @arg @ref RCC_PERIPHCLK_USART2 USART2 peripheral clock (1)(2)
  528. * @arg @ref RCC_PERIPHCLK_USART3 USART3 peripheral clock (1)
  529. * @arg @ref RCC_PERIPHCLK_RNG RNG peripheral clock (1)
  530. * @arg @ref RCC_PERIPHCLK_TIM15 TIM15 peripheral clock (1)(2)
  531. * @arg @ref RCC_PERIPHCLK_TIM1 TIM1 peripheral clock (1)(2)
  532. * @arg @ref RCC_PERIPHCLK_LPTIM1 LPTIM1 peripheral clock (1)
  533. * @arg @ref RCC_PERIPHCLK_LPTIM2 LPTIM2 peripheral clock (1)
  534. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock(1)
  535. * @arg @ref RCC_PERIPHCLK_LPUART2 LPUART2 peripheral clock(1)
  536. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock (1)
  537. * @arg @ref RCC_PERIPHCLK_FDCAN FDCAN peripheral clock (1)
  538. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (1)
  539. * @note (1) Peripheral not available on all devices
  540. * @note (2) Peripheral Clock configuration not available on all devices
  541. * @retval Frequency in Hz
  542. */
  543. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  544. {
  545. uint32_t frequency = 0U;
  546. uint32_t srcclk;
  547. uint32_t pllvco;
  548. uint32_t plln;
  549. #if defined(RCC_CCIPR_RNGSEL)
  550. uint32_t rngclk;
  551. uint32_t rngdiv;
  552. #endif /* RCC_CCIPR_RNGSEL */
  553. /* Check the parameters */
  554. assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
  555. if (PeriphClk == RCC_PERIPHCLK_RTC)
  556. {
  557. /* Get the current RTC source */
  558. srcclk = __HAL_RCC_GET_RTC_SOURCE();
  559. /* Check if LSE is ready and if RTC clock selection is LSE */
  560. if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (srcclk == RCC_RTCCLKSOURCE_LSE))
  561. {
  562. frequency = LSE_VALUE;
  563. }
  564. /* Check if LSI is ready and if RTC clock selection is LSI */
  565. else if ((HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)) && (srcclk == RCC_RTCCLKSOURCE_LSI))
  566. {
  567. frequency = LSI_VALUE;
  568. }
  569. /* Check if HSE is ready and if RTC clock selection is HSI_DIV32*/
  570. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY)) && (srcclk == RCC_RTCCLKSOURCE_HSE_DIV32))
  571. {
  572. frequency = HSE_VALUE / 32U;
  573. }
  574. /* Clock not enabled for RTC*/
  575. else
  576. {
  577. /* Nothing to do as frequency already initialized to 0U */
  578. }
  579. }
  580. else
  581. {
  582. /* Other external peripheral clock source than RTC */
  583. /* Compute PLL clock input */
  584. if (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI) /* HSI ? */
  585. {
  586. pllvco = HSI_VALUE;
  587. }
  588. else if (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE) /* HSE ? */
  589. {
  590. pllvco = HSE_VALUE;
  591. }
  592. else /* No source */
  593. {
  594. pllvco = 0U;
  595. }
  596. /* f(PLL Source) / PLLM */
  597. pllvco = (pllvco / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  598. switch (PeriphClk)
  599. {
  600. #if defined(RCC_CCIPR_RNGSEL)
  601. case RCC_PERIPHCLK_RNG:
  602. srcclk = READ_BIT(RCC->CCIPR, RCC_CCIPR_RNGSEL);
  603. if (srcclk == RCC_RNGCLKSOURCE_HSI_DIV8) /* HSI_DIV8 ? */
  604. {
  605. rngclk = HSI_VALUE / 8U;
  606. }
  607. else if (srcclk == RCC_RNGCLKSOURCE_PLL) /* PLL ? */
  608. {
  609. /* f(PLLQ) = f(VCO input) * PLLN / PLLQ */
  610. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  611. rngclk = (pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U);
  612. }
  613. else if (srcclk == RCC_RNGCLKSOURCE_SYSCLK) /* SYSCLK ? */
  614. {
  615. rngclk = HAL_RCC_GetSysClockFreq();
  616. }
  617. else /* No clock source */
  618. {
  619. rngclk = 0U;
  620. }
  621. rngdiv = (1UL << ((READ_BIT(RCC->CCIPR, RCC_CCIPR_RNGDIV)) >> RCC_CCIPR_RNGDIV_Pos));
  622. frequency = (rngclk / rngdiv);
  623. break;
  624. #endif /* RCC_CCIPR_RNGSEL */
  625. case RCC_PERIPHCLK_USART1:
  626. /* Get the current USART1 source */
  627. srcclk = __HAL_RCC_GET_USART1_SOURCE();
  628. if (srcclk == RCC_USART1CLKSOURCE_PCLK1) /* PCLK1 ? */
  629. {
  630. frequency = HAL_RCC_GetPCLK1Freq();
  631. }
  632. else if (srcclk == RCC_USART1CLKSOURCE_SYSCLK) /* SYSCLK ? */
  633. {
  634. frequency = HAL_RCC_GetSysClockFreq();
  635. }
  636. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_USART1CLKSOURCE_HSI))
  637. {
  638. frequency = HSI_VALUE;
  639. }
  640. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (srcclk == RCC_USART1CLKSOURCE_LSE))
  641. {
  642. frequency = LSE_VALUE;
  643. }
  644. /* Clock not enabled for USART1 */
  645. else
  646. {
  647. /* Nothing to do as frequency already initialized to 0U */
  648. }
  649. break;
  650. #if defined(RCC_CCIPR_USART2SEL)
  651. case RCC_PERIPHCLK_USART2:
  652. /* Get the current USART2 source */
  653. srcclk = __HAL_RCC_GET_USART2_SOURCE();
  654. if (srcclk == RCC_USART2CLKSOURCE_PCLK1)
  655. {
  656. frequency = HAL_RCC_GetPCLK1Freq();
  657. }
  658. else if (srcclk == RCC_USART2CLKSOURCE_SYSCLK)
  659. {
  660. frequency = HAL_RCC_GetSysClockFreq();
  661. }
  662. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_USART2CLKSOURCE_HSI))
  663. {
  664. frequency = HSI_VALUE;
  665. }
  666. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (srcclk == RCC_USART2CLKSOURCE_LSE))
  667. {
  668. frequency = LSE_VALUE;
  669. }
  670. /* Clock not enabled for USART2 */
  671. else
  672. {
  673. /* Nothing to do as frequency already initialized to 0U */
  674. }
  675. break;
  676. #endif /* RCC_CCIPR_USART2SEL */
  677. #if defined(RCC_CCIPR_USART3SEL)
  678. case RCC_PERIPHCLK_USART3:
  679. /* Get the current USART3 source */
  680. srcclk = __HAL_RCC_GET_USART3_SOURCE();
  681. if (srcclk == RCC_USART3CLKSOURCE_PCLK1)
  682. {
  683. frequency = HAL_RCC_GetPCLK1Freq();
  684. }
  685. else if (srcclk == RCC_USART3CLKSOURCE_SYSCLK)
  686. {
  687. frequency = HAL_RCC_GetSysClockFreq();
  688. }
  689. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_USART3CLKSOURCE_HSI))
  690. {
  691. frequency = HSI_VALUE;
  692. }
  693. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (srcclk == RCC_USART3CLKSOURCE_LSE))
  694. {
  695. frequency = LSE_VALUE;
  696. }
  697. /* Clock not enabled for USART3 */
  698. else
  699. {
  700. /* Nothing to do as frequency already initialized to 0U */
  701. }
  702. break;
  703. #endif /* RCC_CCIPR_USART3SEL */
  704. #if defined(RCC_CCIPR_CECSEL)
  705. case RCC_PERIPHCLK_CEC:
  706. /* Get the current CEC source */
  707. srcclk = __HAL_RCC_GET_CEC_SOURCE();
  708. if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_CECCLKSOURCE_HSI_DIV488))
  709. {
  710. frequency = (HSI_VALUE / 488U);
  711. }
  712. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (srcclk == RCC_CECCLKSOURCE_LSE))
  713. {
  714. frequency = LSE_VALUE;
  715. }
  716. /* Clock not enabled for CEC */
  717. else
  718. {
  719. /* Nothing to do as frequency already initialized to 0U */
  720. }
  721. break;
  722. #endif /* RCC_CCIPR_CECSEL */
  723. #if defined(RCC_CCIPR_LPUART1SEL)
  724. case RCC_PERIPHCLK_LPUART1:
  725. /* Get the current LPUART1 source */
  726. srcclk = __HAL_RCC_GET_LPUART1_SOURCE();
  727. if (srcclk == RCC_LPUART1CLKSOURCE_PCLK1)
  728. {
  729. frequency = HAL_RCC_GetPCLK1Freq();
  730. }
  731. else if (srcclk == RCC_LPUART1CLKSOURCE_SYSCLK)
  732. {
  733. frequency = HAL_RCC_GetSysClockFreq();
  734. }
  735. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_LPUART1CLKSOURCE_HSI))
  736. {
  737. frequency = HSI_VALUE;
  738. }
  739. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (srcclk == RCC_LPUART1CLKSOURCE_LSE))
  740. {
  741. frequency = LSE_VALUE;
  742. }
  743. /* Clock not enabled for LPUART1 */
  744. else
  745. {
  746. /* Nothing to do as frequency already initialized to 0U */
  747. }
  748. break;
  749. #endif /* RCC_CCIPR_LPUART1SEL */
  750. #if defined(RCC_CCIPR_LPUART2SEL)
  751. case RCC_PERIPHCLK_LPUART2:
  752. /* Get the current LPUART2 source */
  753. srcclk = __HAL_RCC_GET_LPUART2_SOURCE();
  754. if (srcclk == RCC_LPUART2CLKSOURCE_PCLK1)
  755. {
  756. frequency = HAL_RCC_GetPCLK1Freq();
  757. }
  758. else if (srcclk == RCC_LPUART2CLKSOURCE_SYSCLK)
  759. {
  760. frequency = HAL_RCC_GetSysClockFreq();
  761. }
  762. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_LPUART2CLKSOURCE_HSI))
  763. {
  764. frequency = HSI_VALUE;
  765. }
  766. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (srcclk == RCC_LPUART2CLKSOURCE_LSE))
  767. {
  768. frequency = LSE_VALUE;
  769. }
  770. /* Clock not enabled for LPUART2 */
  771. else
  772. {
  773. /* Nothing to do as frequency already initialized to 0U */
  774. }
  775. break;
  776. #endif /* RCC_CCIPR_LPUART2SEL */
  777. case RCC_PERIPHCLK_ADC:
  778. srcclk = __HAL_RCC_GET_ADC_SOURCE();
  779. if (srcclk == RCC_ADCCLKSOURCE_SYSCLK)
  780. {
  781. frequency = HAL_RCC_GetSysClockFreq();
  782. }
  783. else if (srcclk == RCC_ADCCLKSOURCE_HSI)
  784. {
  785. frequency = HSI_VALUE;
  786. }
  787. else if (srcclk == RCC_ADCCLKSOURCE_PLLADC)
  788. {
  789. if (__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLLPCLK) != 0U)
  790. {
  791. /* f(PLLP) = f(VCO input) * PLLN / PLLP */
  792. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  793. frequency = (pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) >> RCC_PLLCFGR_PLLP_Pos) + 1U);
  794. }
  795. }
  796. /* Clock not enabled for ADC */
  797. else
  798. {
  799. /* Nothing to do as frequency already initialized to 0U */
  800. }
  801. break;
  802. case RCC_PERIPHCLK_I2C1:
  803. /* Get the current I2C1 source */
  804. srcclk = __HAL_RCC_GET_I2C1_SOURCE();
  805. if (srcclk == RCC_I2C1CLKSOURCE_PCLK1)
  806. {
  807. frequency = HAL_RCC_GetPCLK1Freq();
  808. }
  809. else if (srcclk == RCC_I2C1CLKSOURCE_SYSCLK)
  810. {
  811. frequency = HAL_RCC_GetSysClockFreq();
  812. }
  813. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_I2C1CLKSOURCE_HSI))
  814. {
  815. frequency = HSI_VALUE;
  816. }
  817. /* Clock not enabled for I2C1 */
  818. else
  819. {
  820. /* Nothing to do as frequency already initialized to 0U */
  821. }
  822. break;
  823. #if defined(RCC_CCIPR_I2C2SEL)
  824. case RCC_PERIPHCLK_I2C2:
  825. /* Get the current I2C2 source */
  826. srcclk = __HAL_RCC_GET_I2C2_SOURCE();
  827. if (srcclk == RCC_I2C2CLKSOURCE_PCLK1)
  828. {
  829. frequency = HAL_RCC_GetPCLK1Freq();
  830. }
  831. else if (srcclk == RCC_I2C2CLKSOURCE_SYSCLK)
  832. {
  833. frequency = HAL_RCC_GetSysClockFreq();
  834. }
  835. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_I2C2CLKSOURCE_HSI))
  836. {
  837. frequency = HSI_VALUE;
  838. }
  839. /* Clock not enabled for I2C2 */
  840. else
  841. {
  842. /* Nothing to do as frequency already initialized to 0U */
  843. }
  844. break;
  845. #endif /* RCC_CCIPR_I2C2SEL */
  846. case RCC_PERIPHCLK_I2S1:
  847. /* Get the current I2S1 source */
  848. srcclk = __HAL_RCC_GET_I2S1_SOURCE();
  849. if (srcclk == RCC_I2S1CLKSOURCE_PLL)
  850. {
  851. if (__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLLPCLK) != 0U)
  852. {
  853. /* f(PLLP) = f(VCO input) * PLLN / PLLP */
  854. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  855. frequency = (pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) >> RCC_PLLCFGR_PLLP_Pos) + 1U);
  856. }
  857. }
  858. else if (srcclk == RCC_I2S1CLKSOURCE_SYSCLK)
  859. {
  860. frequency = HAL_RCC_GetSysClockFreq();
  861. }
  862. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_I2S1CLKSOURCE_HSI))
  863. {
  864. frequency = HSI_VALUE;
  865. }
  866. else if (srcclk == RCC_I2S1CLKSOURCE_EXT)
  867. {
  868. /* External clock used.*/
  869. frequency = EXTERNAL_I2S1_CLOCK_VALUE;
  870. }
  871. /* Clock not enabled for I2S1 */
  872. else
  873. {
  874. /* Nothing to do as frequency already initialized to 0U */
  875. }
  876. break;
  877. #if defined(RCC_CCIPR2_I2S2SEL)
  878. case RCC_PERIPHCLK_I2S2:
  879. /* Get the current I2S2 source */
  880. srcclk = __HAL_RCC_GET_I2S2_SOURCE();
  881. if (srcclk == RCC_I2S2CLKSOURCE_PLL)
  882. {
  883. if (__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLLPCLK) != 0U)
  884. {
  885. /* f(PLLP) = f(VCO input) * PLLN / PLLP */
  886. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  887. frequency = (pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) >> RCC_PLLCFGR_PLLP_Pos) + 1U);
  888. }
  889. }
  890. else if (srcclk == RCC_I2S2CLKSOURCE_SYSCLK)
  891. {
  892. frequency = HAL_RCC_GetSysClockFreq();
  893. }
  894. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_I2S2CLKSOURCE_HSI))
  895. {
  896. frequency = HSI_VALUE;
  897. }
  898. else if (srcclk == RCC_I2S2CLKSOURCE_EXT)
  899. {
  900. /* External clock used.*/
  901. frequency = EXTERNAL_I2S2_CLOCK_VALUE;
  902. }
  903. /* Clock not enabled for I2S2 */
  904. else
  905. {
  906. /* Nothing to do as frequency already initialized to 0U */
  907. }
  908. break;
  909. #endif /* RCC_CCIPR2_I2S2SEL */
  910. #if defined(RCC_CCIPR_LPTIM1SEL)
  911. case RCC_PERIPHCLK_LPTIM1:
  912. /* Get the current LPTIM1 source */
  913. srcclk = __HAL_RCC_GET_LPTIM1_SOURCE();
  914. if (srcclk == RCC_LPTIM1CLKSOURCE_PCLK1)
  915. {
  916. frequency = HAL_RCC_GetPCLK1Freq();
  917. }
  918. else if ((HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)) && (srcclk == RCC_LPTIM1CLKSOURCE_LSI))
  919. {
  920. frequency = LSI_VALUE;
  921. }
  922. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_LPTIM1CLKSOURCE_HSI))
  923. {
  924. frequency = HSI_VALUE;
  925. }
  926. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (srcclk == RCC_LPTIM1CLKSOURCE_LSE))
  927. {
  928. frequency = LSE_VALUE;
  929. }
  930. /* Clock not enabled for LPTIM1 */
  931. else
  932. {
  933. /* Nothing to do as frequency already initialized to 0U */
  934. }
  935. break;
  936. #endif /* RCC_CCIPR_LPTIM1SEL */
  937. #if defined(RCC_CCIPR_LPTIM2SEL)
  938. case RCC_PERIPHCLK_LPTIM2:
  939. /* Get the current LPTIM2 source */
  940. srcclk = __HAL_RCC_GET_LPTIM2_SOURCE();
  941. if (srcclk == RCC_LPTIM2CLKSOURCE_PCLK1)
  942. {
  943. frequency = HAL_RCC_GetPCLK1Freq();
  944. }
  945. else if ((HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)) && (srcclk == RCC_LPTIM2CLKSOURCE_LSI))
  946. {
  947. frequency = LSI_VALUE;
  948. }
  949. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)) && (srcclk == RCC_LPTIM2CLKSOURCE_HSI))
  950. {
  951. frequency = HSI_VALUE;
  952. }
  953. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (srcclk == RCC_LPTIM2CLKSOURCE_LSE))
  954. {
  955. frequency = LSE_VALUE;
  956. }
  957. /* Clock not enabled for LPTIM2 */
  958. else
  959. {
  960. /* Nothing to do as frequency already initialized to 0U */
  961. }
  962. break;
  963. #endif /* RCC_CCIPR_LPTIM2SEL */
  964. #if defined(RCC_CCIPR_TIM1SEL)
  965. case RCC_PERIPHCLK_TIM1:
  966. srcclk = READ_BIT(RCC->CCIPR, RCC_CCIPR_TIM1SEL);
  967. if (srcclk == RCC_TIM1CLKSOURCE_PLL) /* PLL ? */
  968. {
  969. if (__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLLQCLK) != 0U)
  970. {
  971. /* f(PLLQ) = f(VCO input) * PLLN / PLLQ */
  972. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  973. frequency = (pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U);
  974. }
  975. }
  976. else if (srcclk == RCC_TIM1CLKSOURCE_PCLK1) /* PCLK1 ? */
  977. {
  978. frequency = HAL_RCC_GetPCLK1Freq();
  979. }
  980. else /* No clock source */
  981. {
  982. /* Nothing to do as frequency already initialized to 0U */
  983. }
  984. break;
  985. #endif /* RCC_CCIPR_TIM1SEL */
  986. #if defined(RCC_CCIPR_TIM15SEL)
  987. case RCC_PERIPHCLK_TIM15:
  988. srcclk = READ_BIT(RCC->CCIPR, RCC_CCIPR_TIM15SEL);
  989. if (srcclk == RCC_TIM15CLKSOURCE_PLL) /* PLL ? */
  990. {
  991. if (__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLLQCLK) != 0U)
  992. {
  993. /* f(PLLQ) = f(VCO input) * PLLN / PLLQ */
  994. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  995. frequency = (pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U);
  996. }
  997. }
  998. else if (srcclk == RCC_TIM15CLKSOURCE_PCLK1) /* PCLK1 ? */
  999. {
  1000. frequency = HAL_RCC_GetPCLK1Freq();
  1001. }
  1002. else /* No clock source */
  1003. {
  1004. /* Nothing to do as frequency already initialized to 0U */
  1005. }
  1006. break;
  1007. #endif /* RCC_CCIPR_TIM15SEL */
  1008. #if defined(RCC_CCIPR2_USBSEL)
  1009. case RCC_PERIPHCLK_USB:
  1010. srcclk = READ_BIT(RCC->CCIPR2, RCC_CCIPR2_USBSEL);
  1011. if (srcclk == RCC_USBCLKSOURCE_PLL) /* PLL ? */
  1012. {
  1013. if (__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLLQCLK) != 0U)
  1014. {
  1015. /* f(PLLQ) = f(VCO input) * PLLN / PLLQ */
  1016. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  1017. frequency = (pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U);
  1018. }
  1019. }
  1020. #if defined(RCC_HSI48_SUPPORT)
  1021. else if (srcclk == RCC_USBCLKSOURCE_HSI48) /* HSI48 ? */
  1022. {
  1023. if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSI48RDY)) && (srcclk == RCC_USBCLKSOURCE_HSI48))
  1024. {
  1025. frequency = HSI48_VALUE;
  1026. }
  1027. }
  1028. #endif /* RCC_HSI48_SUPPORT */
  1029. else if (srcclk == RCC_USBCLKSOURCE_HSE)
  1030. {
  1031. if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY)) && (srcclk == RCC_USBCLKSOURCE_HSE))
  1032. {
  1033. frequency = HSE_VALUE;
  1034. }
  1035. }
  1036. else /* No clock source */
  1037. {
  1038. /* Nothing to do as frequency already initialized to 0U */
  1039. }
  1040. break;
  1041. #endif /* RCC_CCIPR2_USBSEL */
  1042. #if defined(RCC_CCIPR2_FDCANSEL)
  1043. case RCC_PERIPHCLK_FDCAN:
  1044. srcclk = READ_BIT(RCC->CCIPR2, RCC_CCIPR2_FDCANSEL);
  1045. if (srcclk == RCC_FDCANCLKSOURCE_PLL) /* PLL ? */
  1046. {
  1047. if (__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLLQCLK) != 0U)
  1048. {
  1049. /* f(PLLQ) = f(VCO input) * PLLN / PLLQ */
  1050. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  1051. frequency = (pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U);
  1052. }
  1053. }
  1054. else if (srcclk == RCC_FDCANCLKSOURCE_PCLK1) /* PCLK1 ? */
  1055. {
  1056. frequency = HAL_RCC_GetPCLK1Freq();
  1057. }
  1058. else if ((HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY)) && (srcclk == RCC_FDCANCLKSOURCE_HSE))
  1059. {
  1060. frequency = HSE_VALUE;
  1061. }
  1062. else /* No clock source */
  1063. {
  1064. /* Nothing to do as frequency already initialized to 0U */
  1065. }
  1066. break;
  1067. #endif /* RCC_CCIPR2_FDCANSEL */
  1068. default:
  1069. break;
  1070. }
  1071. }
  1072. return (frequency);
  1073. }
  1074. /**
  1075. * @}
  1076. */
  1077. /** @defgroup RCCEx_Exported_Functions_Group2 Extended Clock management functions
  1078. * @brief Extended Clock management functions
  1079. *
  1080. @verbatim
  1081. ===============================================================================
  1082. ##### Extended clock management functions #####
  1083. ===============================================================================
  1084. [..]
  1085. This subsection provides a set of functions allowing to control the
  1086. activation or deactivation of LSE CSS, Low speed clock output and
  1087. clock after wake-up from STOP mode.
  1088. @endverbatim
  1089. * @{
  1090. */
  1091. /**
  1092. * @brief Select the Low Speed clock source to output on LSCO pin (PA2).
  1093. * @param LSCOSource specifies the Low Speed clock source to output.
  1094. * This parameter can be one of the following values:
  1095. * @arg @ref RCC_LSCOSOURCE_LSI LSI clock selected as LSCO source
  1096. * @arg @ref RCC_LSCOSOURCE_LSE LSE clock selected as LSCO source
  1097. * @retval None
  1098. */
  1099. void HAL_RCCEx_EnableLSCO(uint32_t LSCOSource)
  1100. {
  1101. GPIO_InitTypeDef GPIO_InitStruct;
  1102. FlagStatus pwrclkchanged = RESET;
  1103. FlagStatus backupchanged = RESET;
  1104. /* Check the parameters */
  1105. assert_param(IS_RCC_LSCOSOURCE(LSCOSource));
  1106. /* LSCO Pin Clock Enable */
  1107. LSCO_CLK_ENABLE();
  1108. /* Configure the LSCO pin in analog mode */
  1109. GPIO_InitStruct.Pin = LSCO_PIN;
  1110. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  1111. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1112. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1113. HAL_GPIO_Init(LSCO_GPIO_PORT, &GPIO_InitStruct);
  1114. /* Update LSCOSEL clock source in Backup Domain control register */
  1115. if (__HAL_RCC_PWR_IS_CLK_DISABLED())
  1116. {
  1117. __HAL_RCC_PWR_CLK_ENABLE();
  1118. pwrclkchanged = SET;
  1119. }
  1120. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  1121. {
  1122. HAL_PWR_EnableBkUpAccess();
  1123. backupchanged = SET;
  1124. }
  1125. MODIFY_REG(RCC->BDCR, RCC_BDCR_LSCOSEL | RCC_BDCR_LSCOEN, LSCOSource | RCC_BDCR_LSCOEN);
  1126. if (backupchanged == SET)
  1127. {
  1128. HAL_PWR_DisableBkUpAccess();
  1129. }
  1130. if (pwrclkchanged == SET)
  1131. {
  1132. __HAL_RCC_PWR_CLK_DISABLE();
  1133. }
  1134. }
  1135. /**
  1136. * @brief Disable the Low Speed clock output.
  1137. * @retval None
  1138. */
  1139. void HAL_RCCEx_DisableLSCO(void)
  1140. {
  1141. FlagStatus pwrclkchanged = RESET;
  1142. FlagStatus backupchanged = RESET;
  1143. /* Update LSCOEN bit in Backup Domain control register */
  1144. if (__HAL_RCC_PWR_IS_CLK_DISABLED())
  1145. {
  1146. __HAL_RCC_PWR_CLK_ENABLE();
  1147. pwrclkchanged = SET;
  1148. }
  1149. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  1150. {
  1151. /* Enable access to the backup domain */
  1152. HAL_PWR_EnableBkUpAccess();
  1153. backupchanged = SET;
  1154. }
  1155. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSCOEN);
  1156. /* Restore previous configuration */
  1157. if (backupchanged == SET)
  1158. {
  1159. /* Disable access to the backup domain */
  1160. HAL_PWR_DisableBkUpAccess();
  1161. }
  1162. if (pwrclkchanged == SET)
  1163. {
  1164. __HAL_RCC_PWR_CLK_DISABLE();
  1165. }
  1166. }
  1167. /**
  1168. * @}
  1169. */
  1170. #if defined(CRS)
  1171. /** @defgroup RCCEx_Exported_Functions_Group3 Extended Clock Recovery System Control functions
  1172. * @brief Extended Clock Recovery System Control functions
  1173. *
  1174. @verbatim
  1175. ===============================================================================
  1176. ##### Extended Clock Recovery System Control functions #####
  1177. ===============================================================================
  1178. [..]
  1179. For devices with Clock Recovery System feature (CRS), RCC Extension HAL driver can be used as follows:
  1180. (#) In System clock config, HSI48 needs to be enabled
  1181. (#) Enable CRS clock in IP MSP init which will use CRS functions
  1182. (#) Call CRS functions as follows:
  1183. (##) Prepare synchronization configuration necessary for HSI48 calibration
  1184. (+++) Default values can be set for frequency Error Measurement (reload and error limit)
  1185. and also HSI48 oscillator smooth trimming.
  1186. (+++) Macro __HAL_RCC_CRS_RELOADVALUE_CALCULATE can be also used to calculate
  1187. directly reload value with target and synchronization frequencies values
  1188. (##) Call function HAL_RCCEx_CRSConfig which
  1189. (+++) Resets CRS registers to their default values.
  1190. (+++) Configures CRS registers with synchronization configuration
  1191. (+++) Enables automatic calibration and frequency error counter feature
  1192. Note: When using USB LPM (Link Power Management) and the device is in Sleep mode, the
  1193. periodic USB SOF will not be generated by the host. No SYNC signal will therefore be
  1194. provided to the CRS to calibrate the HSI48 on the run. To guarantee the required clock
  1195. precision after waking up from Sleep mode, the LSE or reference clock on the GPIOs
  1196. should be used as SYNC signal.
  1197. (##) A polling function is provided to wait for complete synchronization
  1198. (+++) Call function HAL_RCCEx_CRSWaitSynchronization()
  1199. (+++) According to CRS status, user can decide to adjust again the calibration or continue
  1200. application if synchronization is OK
  1201. (#) User can retrieve information related to synchronization in calling function
  1202. HAL_RCCEx_CRSGetSynchronizationInfo()
  1203. (#) Regarding synchronization status and synchronization information, user can try a new calibration
  1204. in changing synchronization configuration and call again HAL_RCCEx_CRSConfig.
  1205. Note: When the SYNC event is detected during the downcounting phase (before reaching the zero value),
  1206. it means that the actual frequency is lower than the target (and so, that the TRIM value should be
  1207. incremented), while when it is detected during the upcounting phase it means that the actual frequency
  1208. is higher (and that the TRIM value should be decremented).
  1209. (#) In interrupt mode, user can resort to the available macros (__HAL_RCC_CRS_XXX_IT). Interrupts will go
  1210. through CRS Handler (CRS_IRQn/CRS_IRQHandler)
  1211. (++) Call function HAL_RCCEx_CRSConfig()
  1212. (++) Enable CRS_IRQn (thanks to NVIC functions)
  1213. (++) Enable CRS interrupt (__HAL_RCC_CRS_ENABLE_IT)
  1214. (++) Implement CRS status management in the following user callbacks called from
  1215. HAL_RCCEx_CRS_IRQHandler():
  1216. (+++) HAL_RCCEx_CRS_SyncOkCallback()
  1217. (+++) HAL_RCCEx_CRS_SyncWarnCallback()
  1218. (+++) HAL_RCCEx_CRS_ExpectedSyncCallback()
  1219. (+++) HAL_RCCEx_CRS_ErrorCallback()
  1220. (#) To force a SYNC EVENT, user can use the function HAL_RCCEx_CRSSoftwareSynchronizationGenerate().
  1221. This function can be called before calling HAL_RCCEx_CRSConfig (for instance in Systick handler)
  1222. @endverbatim
  1223. * @{
  1224. */
  1225. /**
  1226. * @brief Start automatic synchronization for polling mode
  1227. * @param pInit Pointer on RCC_CRSInitTypeDef structure
  1228. * @retval None
  1229. */
  1230. void HAL_RCCEx_CRSConfig(RCC_CRSInitTypeDef *pInit)
  1231. {
  1232. uint32_t value; /* no init needed */
  1233. /* Check the parameters */
  1234. assert_param(IS_RCC_CRS_SYNC_DIV(pInit->Prescaler));
  1235. assert_param(IS_RCC_CRS_SYNC_SOURCE(pInit->Source));
  1236. assert_param(IS_RCC_CRS_SYNC_POLARITY(pInit->Polarity));
  1237. assert_param(IS_RCC_CRS_RELOADVALUE(pInit->ReloadValue));
  1238. assert_param(IS_RCC_CRS_ERRORLIMIT(pInit->ErrorLimitValue));
  1239. assert_param(IS_RCC_CRS_HSI48CALIBRATION(pInit->HSI48CalibrationValue));
  1240. /* CONFIGURATION */
  1241. /* Before configuration, reset CRS registers to their default values*/
  1242. __HAL_RCC_CRS_FORCE_RESET();
  1243. __HAL_RCC_CRS_RELEASE_RESET();
  1244. /* Set the SYNCDIV[2:0] bits according to Prescaler value */
  1245. /* Set the SYNCSRC[1:0] bits according to Source value */
  1246. /* Set the SYNCSPOL bit according to Polarity value */
  1247. value = (pInit->Prescaler | pInit->Source | pInit->Polarity);
  1248. /* Set the RELOAD[15:0] bits according to ReloadValue value */
  1249. value |= pInit->ReloadValue;
  1250. /* Set the FELIM[7:0] bits according to ErrorLimitValue value */
  1251. value |= (pInit->ErrorLimitValue << CRS_CFGR_FELIM_Pos);
  1252. WRITE_REG(CRS->CFGR, value);
  1253. /* Adjust HSI48 oscillator smooth trimming */
  1254. /* Set the TRIM[6:0] bits according to RCC_CRS_HSI48CalibrationValue value */
  1255. MODIFY_REG(CRS->CR, CRS_CR_TRIM, (pInit->HSI48CalibrationValue << CRS_CR_TRIM_Pos));
  1256. /* START AUTOMATIC SYNCHRONIZATION*/
  1257. /* Enable Automatic trimming & Frequency error counter */
  1258. SET_BIT(CRS->CR, CRS_CR_AUTOTRIMEN | CRS_CR_CEN);
  1259. }
  1260. /**
  1261. * @brief Generate the software synchronization event
  1262. * @retval None
  1263. */
  1264. void HAL_RCCEx_CRSSoftwareSynchronizationGenerate(void)
  1265. {
  1266. SET_BIT(CRS->CR, CRS_CR_SWSYNC);
  1267. }
  1268. /**
  1269. * @brief Return synchronization info
  1270. * @param pSynchroInfo Pointer on RCC_CRSSynchroInfoTypeDef structure
  1271. * @retval None
  1272. */
  1273. void HAL_RCCEx_CRSGetSynchronizationInfo(RCC_CRSSynchroInfoTypeDef *pSynchroInfo)
  1274. {
  1275. /* Check the parameter */
  1276. assert_param(pSynchroInfo != (void *)NULL);
  1277. /* Get the reload value */
  1278. pSynchroInfo->ReloadValue = (READ_BIT(CRS->CFGR, CRS_CFGR_RELOAD));
  1279. /* Get HSI48 oscillator smooth trimming */
  1280. pSynchroInfo->HSI48CalibrationValue = (READ_BIT(CRS->CR, CRS_CR_TRIM) >> CRS_CR_TRIM_Pos);
  1281. /* Get Frequency error capture */
  1282. pSynchroInfo->FreqErrorCapture = (READ_BIT(CRS->ISR, CRS_ISR_FECAP) >> CRS_ISR_FECAP_Pos);
  1283. /* Get Frequency error direction */
  1284. pSynchroInfo->FreqErrorDirection = (READ_BIT(CRS->ISR, CRS_ISR_FEDIR));
  1285. }
  1286. /**
  1287. * @brief Wait for CRS Synchronization status.
  1288. * @param Timeout Duration of the timeout
  1289. * @note Timeout is based on the maximum time to receive a SYNC event based on synchronization
  1290. * frequency.
  1291. * @note If Timeout set to HAL_MAX_DELAY, HAL_TIMEOUT will be never returned.
  1292. * @retval Combination of Synchronization status
  1293. * This parameter can be a combination of the following values:
  1294. * @arg @ref RCC_CRS_TIMEOUT
  1295. * @arg @ref RCC_CRS_SYNCOK
  1296. * @arg @ref RCC_CRS_SYNCWARN
  1297. * @arg @ref RCC_CRS_SYNCERR
  1298. * @arg @ref RCC_CRS_SYNCMISS
  1299. * @arg @ref RCC_CRS_TRIMOVF
  1300. */
  1301. uint32_t HAL_RCCEx_CRSWaitSynchronization(uint32_t Timeout)
  1302. {
  1303. uint32_t crsstatus = RCC_CRS_NONE;
  1304. uint32_t tickstart;
  1305. /* Get timeout */
  1306. tickstart = HAL_GetTick();
  1307. /* Wait for CRS flag or timeout detection */
  1308. do
  1309. {
  1310. if (Timeout != HAL_MAX_DELAY)
  1311. {
  1312. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1313. {
  1314. crsstatus = RCC_CRS_TIMEOUT;
  1315. }
  1316. }
  1317. /* Check CRS SYNCOK flag */
  1318. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCOK))
  1319. {
  1320. /* CRS SYNC event OK */
  1321. crsstatus |= RCC_CRS_SYNCOK;
  1322. /* Clear CRS SYNC event OK bit */
  1323. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCOK);
  1324. }
  1325. /* Check CRS SYNCWARN flag */
  1326. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCWARN))
  1327. {
  1328. /* CRS SYNC warning */
  1329. crsstatus |= RCC_CRS_SYNCWARN;
  1330. /* Clear CRS SYNCWARN bit */
  1331. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCWARN);
  1332. }
  1333. /* Check CRS TRIM overflow flag */
  1334. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_TRIMOVF))
  1335. {
  1336. /* CRS SYNC Error */
  1337. crsstatus |= RCC_CRS_TRIMOVF;
  1338. /* Clear CRS Error bit */
  1339. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_TRIMOVF);
  1340. }
  1341. /* Check CRS Error flag */
  1342. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCERR))
  1343. {
  1344. /* CRS SYNC Error */
  1345. crsstatus |= RCC_CRS_SYNCERR;
  1346. /* Clear CRS Error bit */
  1347. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCERR);
  1348. }
  1349. /* Check CRS SYNC Missed flag */
  1350. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCMISS))
  1351. {
  1352. /* CRS SYNC Missed */
  1353. crsstatus |= RCC_CRS_SYNCMISS;
  1354. /* Clear CRS SYNC Missed bit */
  1355. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCMISS);
  1356. }
  1357. /* Check CRS Expected SYNC flag */
  1358. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_ESYNC))
  1359. {
  1360. /* frequency error counter reached a zero value */
  1361. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_ESYNC);
  1362. }
  1363. } while (RCC_CRS_NONE == crsstatus);
  1364. return crsstatus;
  1365. }
  1366. /**
  1367. * @brief Handle the Clock Recovery System interrupt request.
  1368. * @retval None
  1369. */
  1370. void HAL_RCCEx_CRS_IRQHandler(void)
  1371. {
  1372. uint32_t crserror = RCC_CRS_NONE;
  1373. /* Get current IT flags and IT sources values */
  1374. uint32_t itflags = READ_REG(CRS->ISR);
  1375. uint32_t itsources = READ_REG(CRS->CR);
  1376. /* Check CRS SYNCOK flag */
  1377. if (((itflags & RCC_CRS_FLAG_SYNCOK) != 0U) && ((itsources & RCC_CRS_IT_SYNCOK) != 0U))
  1378. {
  1379. /* Clear CRS SYNC event OK flag */
  1380. WRITE_REG(CRS->ICR, CRS_ICR_SYNCOKC);
  1381. /* user callback */
  1382. HAL_RCCEx_CRS_SyncOkCallback();
  1383. }
  1384. /* Check CRS SYNCWARN flag */
  1385. else if (((itflags & RCC_CRS_FLAG_SYNCWARN) != 0U) && ((itsources & RCC_CRS_IT_SYNCWARN) != 0U))
  1386. {
  1387. /* Clear CRS SYNCWARN flag */
  1388. WRITE_REG(CRS->ICR, CRS_ICR_SYNCWARNC);
  1389. /* user callback */
  1390. HAL_RCCEx_CRS_SyncWarnCallback();
  1391. }
  1392. /* Check CRS Expected SYNC flag */
  1393. else if (((itflags & RCC_CRS_FLAG_ESYNC) != 0U) && ((itsources & RCC_CRS_IT_ESYNC) != 0U))
  1394. {
  1395. /* frequency error counter reached a zero value */
  1396. WRITE_REG(CRS->ICR, CRS_ICR_ESYNCC);
  1397. /* user callback */
  1398. HAL_RCCEx_CRS_ExpectedSyncCallback();
  1399. }
  1400. /* Check CRS Error flags */
  1401. else
  1402. {
  1403. if (((itflags & RCC_CRS_FLAG_ERR) != 0U) && ((itsources & RCC_CRS_IT_ERR) != 0U))
  1404. {
  1405. if ((itflags & RCC_CRS_FLAG_SYNCERR) != 0U)
  1406. {
  1407. crserror |= RCC_CRS_SYNCERR;
  1408. }
  1409. if ((itflags & RCC_CRS_FLAG_SYNCMISS) != 0U)
  1410. {
  1411. crserror |= RCC_CRS_SYNCMISS;
  1412. }
  1413. if ((itflags & RCC_CRS_FLAG_TRIMOVF) != 0U)
  1414. {
  1415. crserror |= RCC_CRS_TRIMOVF;
  1416. }
  1417. /* Clear CRS Error flags */
  1418. WRITE_REG(CRS->ICR, CRS_ICR_ERRC);
  1419. /* user error callback */
  1420. HAL_RCCEx_CRS_ErrorCallback(crserror);
  1421. }
  1422. }
  1423. }
  1424. /**
  1425. * @brief RCCEx Clock Recovery System SYNCOK interrupt callback.
  1426. * @retval none
  1427. */
  1428. __weak void HAL_RCCEx_CRS_SyncOkCallback(void)
  1429. {
  1430. /* NOTE : This function should not be modified, when the callback is needed,
  1431. the @ref HAL_RCCEx_CRS_SyncOkCallback should be implemented in the user file
  1432. */
  1433. }
  1434. /**
  1435. * @brief RCCEx Clock Recovery System SYNCWARN interrupt callback.
  1436. * @retval none
  1437. */
  1438. __weak void HAL_RCCEx_CRS_SyncWarnCallback(void)
  1439. {
  1440. /* NOTE : This function should not be modified, when the callback is needed,
  1441. the @ref HAL_RCCEx_CRS_SyncWarnCallback should be implemented in the user file
  1442. */
  1443. }
  1444. /**
  1445. * @brief RCCEx Clock Recovery System Expected SYNC interrupt callback.
  1446. * @retval none
  1447. */
  1448. __weak void HAL_RCCEx_CRS_ExpectedSyncCallback(void)
  1449. {
  1450. /* NOTE : This function should not be modified, when the callback is needed,
  1451. the @ref HAL_RCCEx_CRS_ExpectedSyncCallback should be implemented in the user file
  1452. */
  1453. }
  1454. /**
  1455. * @brief RCCEx Clock Recovery System Error interrupt callback.
  1456. * @param Error Combination of Error status.
  1457. * This parameter can be a combination of the following values:
  1458. * @arg @ref RCC_CRS_SYNCERR
  1459. * @arg @ref RCC_CRS_SYNCMISS
  1460. * @arg @ref RCC_CRS_TRIMOVF
  1461. * @retval none
  1462. */
  1463. __weak void HAL_RCCEx_CRS_ErrorCallback(uint32_t Error)
  1464. {
  1465. /* Prevent unused argument(s) compilation warning */
  1466. UNUSED(Error);
  1467. /* NOTE : This function should not be modified, when the callback is needed,
  1468. the @ref HAL_RCCEx_CRS_ErrorCallback should be implemented in the user file
  1469. */
  1470. }
  1471. /**
  1472. * @}
  1473. */
  1474. #endif /* CRS */
  1475. /**
  1476. * @}
  1477. */
  1478. /**
  1479. * @}
  1480. */
  1481. /**
  1482. * @}
  1483. */
  1484. #endif /* HAL_RCC_MODULE_ENABLED */
  1485. /**
  1486. * @}
  1487. */
  1488. /**
  1489. * @}
  1490. */