stm32g4xx_hal_rcc_ex.c 62 KB

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