stm32l4xx_hal_rcc_ex.c 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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) 2017 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 "stm32l4xx_hal.h"
  25. /** @addtogroup STM32L4xx_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 PLLSAI1_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  39. #define PLLSAI2_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  40. #define PLL_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  41. #define DIVIDER_P_UPDATE 0U
  42. #define DIVIDER_Q_UPDATE 1U
  43. #define DIVIDER_R_UPDATE 2U
  44. #define __LSCO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  45. #define LSCO_GPIO_PORT GPIOA
  46. #define LSCO_PIN GPIO_PIN_2
  47. /**
  48. * @}
  49. */
  50. /* Private macros ------------------------------------------------------------*/
  51. /* Private variables ---------------------------------------------------------*/
  52. /* Private function prototypes -----------------------------------------------*/
  53. /** @defgroup RCCEx_Private_Functions RCCEx Private Functions
  54. * @{
  55. */
  56. #if defined(RCC_PLLSAI1_SUPPORT)
  57. static HAL_StatusTypeDef RCCEx_PLLSAI1_Config(RCC_PLLSAI1InitTypeDef *PllSai1, uint32_t Divider);
  58. #endif /* RCC_PLLSAI1_SUPPORT */
  59. #if defined(RCC_PLLSAI2_SUPPORT)
  60. static HAL_StatusTypeDef RCCEx_PLLSAI2_Config(RCC_PLLSAI2InitTypeDef *PllSai2, uint32_t Divider);
  61. #endif /* RCC_PLLSAI2_SUPPORT */
  62. #if defined(SAI1)
  63. static uint32_t RCCEx_GetSAIxPeriphCLKFreq(uint32_t PeriphClk, uint32_t InputFrequency);
  64. #endif /* SAI1 */
  65. /**
  66. * @}
  67. */
  68. /* Exported functions --------------------------------------------------------*/
  69. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  70. * @{
  71. */
  72. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  73. * @brief Extended Peripheral Control functions
  74. *
  75. @verbatim
  76. ===============================================================================
  77. ##### Extended Peripheral Control functions #####
  78. ===============================================================================
  79. [..]
  80. This subsection provides a set of functions allowing to control the RCC Clocks
  81. frequencies.
  82. [..]
  83. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  84. select the RTC clock source; in this case the Backup domain will be reset in
  85. order to modify the RTC Clock source, as consequence RTC registers (including
  86. the backup registers) are set to their reset values.
  87. @endverbatim
  88. * @{
  89. */
  90. /**
  91. * @brief Initialize the RCC extended peripherals clocks according to the specified
  92. * parameters in the RCC_PeriphCLKInitTypeDef.
  93. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  94. * contains a field PeriphClockSelection which can be a combination of the following values:
  95. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  96. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  97. @if STM32L462xx
  98. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM1)
  99. @endif
  100. @if STM32L486xx
  101. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM1)
  102. @endif
  103. @if STM32L4A6xx
  104. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM1)
  105. @endif
  106. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  107. * @arg @ref RCC_PERIPHCLK_I2C2 I2C2 peripheral clock
  108. * @arg @ref RCC_PERIPHCLK_I2C3 I2C3 peripheral clock
  109. @if STM32L462xx
  110. * @arg @ref RCC_PERIPHCLK_I2C4 I2C4 peripheral clock (only for devices with I2C4)
  111. @endif
  112. @if STM32L4A6xx
  113. * @arg @ref RCC_PERIPHCLK_I2C4 I2C4 peripheral clock (only for devices with I2C4)
  114. @endif
  115. @if STM32L4S9xx
  116. * @arg @ref RCC_PERIPHCLK_I2C4 I2C4 peripheral clock (only for devices with I2C4)
  117. @endif
  118. * @arg @ref RCC_PERIPHCLK_LPTIM1 LPTIM1 peripheral clock
  119. * @arg @ref RCC_PERIPHCLK_LPTIM2 LPTIM2 peripheral clock
  120. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock
  121. * @arg @ref RCC_PERIPHCLK_RNG RNG peripheral clock
  122. * @arg @ref RCC_PERIPHCLK_SAI1 SAI1 peripheral clock (only for devices with SAI1)
  123. @if STM32L486xx
  124. * @arg @ref RCC_PERIPHCLK_SAI2 SAI2 peripheral clock (only for devices with SAI2)
  125. @endif
  126. @if STM32L4A6xx
  127. * @arg @ref RCC_PERIPHCLK_SAI2 SAI2 peripheral clock (only for devices with SAI2)
  128. @endif
  129. @if STM32L4S9xx
  130. * @arg @ref RCC_PERIPHCLK_SAI2 SAI2 peripheral clock (only for devices with SAI2)
  131. @endif
  132. * @arg @ref RCC_PERIPHCLK_SDMMC1 SDMMC1 peripheral clock
  133. @if STM32L443xx
  134. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  135. @endif
  136. @if STM32L486xx
  137. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  138. @endif
  139. @if STM32L4A6xx
  140. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  141. @endif
  142. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  143. * @arg @ref RCC_PERIPHCLK_USART2 USART1 peripheral clock
  144. * @arg @ref RCC_PERIPHCLK_USART3 USART1 peripheral clock
  145. @if STM32L462xx
  146. * @arg @ref RCC_PERIPHCLK_UART4 USART1 peripheral clock (only for devices with UART4)
  147. @endif
  148. @if STM32L486xx
  149. * @arg @ref RCC_PERIPHCLK_UART4 USART1 peripheral clock (only for devices with UART4)
  150. * @arg @ref RCC_PERIPHCLK_UART5 USART1 peripheral clock (only for devices with UART5)
  151. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (only for devices with USB)
  152. @endif
  153. @if STM32L4A6xx
  154. * @arg @ref RCC_PERIPHCLK_UART4 USART1 peripheral clock (only for devices with UART4)
  155. * @arg @ref RCC_PERIPHCLK_UART5 USART1 peripheral clock (only for devices with UART5)
  156. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (only for devices with USB)
  157. @endif
  158. @if STM32L4S9xx
  159. * @arg @ref RCC_PERIPHCLK_UART4 USART1 peripheral clock (only for devices with UART4)
  160. * @arg @ref RCC_PERIPHCLK_UART5 USART1 peripheral clock (only for devices with UART5)
  161. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (only for devices with USB)
  162. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral kernel clock (only for devices with DFSDM1)
  163. * @arg @ref RCC_PERIPHCLK_DFSDM1AUDIO DFSDM1 peripheral audio clock (only for devices with DFSDM1)
  164. * @arg @ref RCC_PERIPHCLK_LTDC LTDC peripheral clock (only for devices with LTDC)
  165. * @arg @ref RCC_PERIPHCLK_DSI DSI peripheral clock (only for devices with DSI)
  166. * @arg @ref RCC_PERIPHCLK_OSPI OctoSPI peripheral clock (only for devices with OctoSPI)
  167. @endif
  168. *
  169. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  170. * the RTC clock source: in this case the access to Backup domain is enabled.
  171. *
  172. * @retval HAL status
  173. */
  174. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  175. {
  176. uint32_t tmpregister, tickstart; /* no init needed */
  177. HAL_StatusTypeDef ret = HAL_OK; /* Intermediate status */
  178. HAL_StatusTypeDef status = HAL_OK; /* Final status */
  179. /* Check the parameters */
  180. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  181. #if defined(SAI1)
  182. /*-------------------------- SAI1 clock source configuration ---------------------*/
  183. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == RCC_PERIPHCLK_SAI1))
  184. {
  185. /* Check the parameters */
  186. assert_param(IS_RCC_SAI1CLK(PeriphClkInit->Sai1ClockSelection));
  187. switch(PeriphClkInit->Sai1ClockSelection)
  188. {
  189. case RCC_SAI1CLKSOURCE_PLL: /* PLL is used as clock source for SAI1*/
  190. /* Enable SAI Clock output generated from System PLL . */
  191. #if defined(RCC_PLLSAI2_SUPPORT)
  192. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SAI3CLK);
  193. #else
  194. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SAI2CLK);
  195. #endif /* RCC_PLLSAI2_SUPPORT */
  196. /* SAI1 clock source config set later after clock selection check */
  197. break;
  198. case RCC_SAI1CLKSOURCE_PLLSAI1: /* PLLSAI1 is used as clock source for SAI1*/
  199. /* PLLSAI1 input clock, parameters M, N & P configuration and clock output (PLLSAI1ClockOut) */
  200. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_P_UPDATE);
  201. /* SAI1 clock source config set later after clock selection check */
  202. break;
  203. #if defined(RCC_PLLSAI2_SUPPORT)
  204. case RCC_SAI1CLKSOURCE_PLLSAI2: /* PLLSAI2 is used as clock source for SAI1*/
  205. /* PLLSAI2 input clock, parameters M, N & P configuration clock output (PLLSAI2ClockOut) */
  206. ret = RCCEx_PLLSAI2_Config(&(PeriphClkInit->PLLSAI2), DIVIDER_P_UPDATE);
  207. /* SAI1 clock source config set later after clock selection check */
  208. break;
  209. #endif /* RCC_PLLSAI2_SUPPORT */
  210. case RCC_SAI1CLKSOURCE_PIN: /* External clock is used as source of SAI1 clock*/
  211. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  212. case RCC_SAI1CLKSOURCE_HSI: /* HSI is used as source of SAI1 clock*/
  213. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  214. /* SAI1 clock source config set later after clock selection check */
  215. break;
  216. default:
  217. ret = HAL_ERROR;
  218. break;
  219. }
  220. if(ret == HAL_OK)
  221. {
  222. /* Set the source of SAI1 clock*/
  223. __HAL_RCC_SAI1_CONFIG(PeriphClkInit->Sai1ClockSelection);
  224. }
  225. else
  226. {
  227. /* set overall return value */
  228. status = ret;
  229. }
  230. }
  231. #endif /* SAI1 */
  232. #if defined(SAI2)
  233. /*-------------------------- SAI2 clock source configuration ---------------------*/
  234. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == RCC_PERIPHCLK_SAI2))
  235. {
  236. /* Check the parameters */
  237. assert_param(IS_RCC_SAI2CLK(PeriphClkInit->Sai2ClockSelection));
  238. switch(PeriphClkInit->Sai2ClockSelection)
  239. {
  240. case RCC_SAI2CLKSOURCE_PLL: /* PLL is used as clock source for SAI2*/
  241. /* Enable SAI Clock output generated from System PLL . */
  242. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SAI3CLK);
  243. /* SAI2 clock source config set later after clock selection check */
  244. break;
  245. case RCC_SAI2CLKSOURCE_PLLSAI1: /* PLLSAI1 is used as clock source for SAI2*/
  246. /* PLLSAI1 input clock, parameters M, N & P configuration and clock output (PLLSAI1ClockOut) */
  247. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_P_UPDATE);
  248. /* SAI2 clock source config set later after clock selection check */
  249. break;
  250. case RCC_SAI2CLKSOURCE_PLLSAI2: /* PLLSAI2 is used as clock source for SAI2*/
  251. /* PLLSAI2 input clock, parameters M, N & P configuration and clock output (PLLSAI2ClockOut) */
  252. ret = RCCEx_PLLSAI2_Config(&(PeriphClkInit->PLLSAI2), DIVIDER_P_UPDATE);
  253. /* SAI2 clock source config set later after clock selection check */
  254. break;
  255. case RCC_SAI2CLKSOURCE_PIN: /* External clock is used as source of SAI2 clock*/
  256. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  257. case RCC_SAI2CLKSOURCE_HSI: /* HSI is used as source of SAI2 clock*/
  258. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  259. /* SAI2 clock source config set later after clock selection check */
  260. break;
  261. default:
  262. ret = HAL_ERROR;
  263. break;
  264. }
  265. if(ret == HAL_OK)
  266. {
  267. /* Set the source of SAI2 clock*/
  268. __HAL_RCC_SAI2_CONFIG(PeriphClkInit->Sai2ClockSelection);
  269. }
  270. else
  271. {
  272. /* set overall return value */
  273. status = ret;
  274. }
  275. }
  276. #endif /* SAI2 */
  277. /*-------------------------- RTC clock source configuration ----------------------*/
  278. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC)
  279. {
  280. FlagStatus pwrclkchanged = RESET;
  281. /* Check for RTC Parameters used to output RTCCLK */
  282. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  283. /* Enable Power Clock */
  284. if(__HAL_RCC_PWR_IS_CLK_DISABLED() != 0U)
  285. {
  286. __HAL_RCC_PWR_CLK_ENABLE();
  287. pwrclkchanged = SET;
  288. }
  289. /* Enable write access to Backup domain */
  290. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  291. /* Wait for Backup domain Write protection disable */
  292. tickstart = HAL_GetTick();
  293. while(READ_BIT(PWR->CR1, PWR_CR1_DBP) == 0U)
  294. {
  295. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  296. {
  297. ret = HAL_TIMEOUT;
  298. break;
  299. }
  300. }
  301. if(ret == HAL_OK)
  302. {
  303. /* Reset the Backup domain only if the RTC Clock source selection is modified from default */
  304. tmpregister = READ_BIT(RCC->BDCR, RCC_BDCR_RTCSEL);
  305. if((tmpregister != RCC_RTCCLKSOURCE_NONE) && (tmpregister != PeriphClkInit->RTCClockSelection))
  306. {
  307. /* Store the content of BDCR register before the reset of Backup Domain */
  308. tmpregister = READ_BIT(RCC->BDCR, ~(RCC_BDCR_RTCSEL));
  309. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  310. __HAL_RCC_BACKUPRESET_FORCE();
  311. __HAL_RCC_BACKUPRESET_RELEASE();
  312. /* Restore the Content of BDCR register */
  313. RCC->BDCR = tmpregister;
  314. }
  315. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  316. if (HAL_IS_BIT_SET(tmpregister, RCC_BDCR_LSEON))
  317. {
  318. /* Get Start Tick*/
  319. tickstart = HAL_GetTick();
  320. /* Wait till LSE is ready */
  321. while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  322. {
  323. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  324. {
  325. ret = HAL_TIMEOUT;
  326. break;
  327. }
  328. }
  329. }
  330. if(ret == HAL_OK)
  331. {
  332. /* Apply new RTC clock source selection */
  333. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  334. }
  335. else
  336. {
  337. /* set overall return value */
  338. status = ret;
  339. }
  340. }
  341. else
  342. {
  343. /* set overall return value */
  344. status = ret;
  345. }
  346. /* Restore clock configuration if changed */
  347. if(pwrclkchanged == SET)
  348. {
  349. __HAL_RCC_PWR_CLK_DISABLE();
  350. }
  351. }
  352. /*-------------------------- USART1 clock source configuration -------------------*/
  353. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART1) == RCC_PERIPHCLK_USART1)
  354. {
  355. /* Check the parameters */
  356. assert_param(IS_RCC_USART1CLKSOURCE(PeriphClkInit->Usart1ClockSelection));
  357. /* Configure the USART1 clock source */
  358. __HAL_RCC_USART1_CONFIG(PeriphClkInit->Usart1ClockSelection);
  359. }
  360. /*-------------------------- USART2 clock source configuration -------------------*/
  361. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART2) == RCC_PERIPHCLK_USART2)
  362. {
  363. /* Check the parameters */
  364. assert_param(IS_RCC_USART2CLKSOURCE(PeriphClkInit->Usart2ClockSelection));
  365. /* Configure the USART2 clock source */
  366. __HAL_RCC_USART2_CONFIG(PeriphClkInit->Usart2ClockSelection);
  367. }
  368. #if defined(USART3)
  369. /*-------------------------- USART3 clock source configuration -------------------*/
  370. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART3) == RCC_PERIPHCLK_USART3)
  371. {
  372. /* Check the parameters */
  373. assert_param(IS_RCC_USART3CLKSOURCE(PeriphClkInit->Usart3ClockSelection));
  374. /* Configure the USART3 clock source */
  375. __HAL_RCC_USART3_CONFIG(PeriphClkInit->Usart3ClockSelection);
  376. }
  377. #endif /* USART3 */
  378. #if defined(UART4)
  379. /*-------------------------- UART4 clock source configuration --------------------*/
  380. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_UART4) == RCC_PERIPHCLK_UART4)
  381. {
  382. /* Check the parameters */
  383. assert_param(IS_RCC_UART4CLKSOURCE(PeriphClkInit->Uart4ClockSelection));
  384. /* Configure the UART4 clock source */
  385. __HAL_RCC_UART4_CONFIG(PeriphClkInit->Uart4ClockSelection);
  386. }
  387. #endif /* UART4 */
  388. #if defined(UART5)
  389. /*-------------------------- UART5 clock source configuration --------------------*/
  390. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_UART5) == RCC_PERIPHCLK_UART5)
  391. {
  392. /* Check the parameters */
  393. assert_param(IS_RCC_UART5CLKSOURCE(PeriphClkInit->Uart5ClockSelection));
  394. /* Configure the UART5 clock source */
  395. __HAL_RCC_UART5_CONFIG(PeriphClkInit->Uart5ClockSelection);
  396. }
  397. #endif /* UART5 */
  398. /*-------------------------- LPUART1 clock source configuration ------------------*/
  399. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPUART1) == RCC_PERIPHCLK_LPUART1)
  400. {
  401. /* Check the parameters */
  402. assert_param(IS_RCC_LPUART1CLKSOURCE(PeriphClkInit->Lpuart1ClockSelection));
  403. /* Configure the LPUART1 clock source */
  404. __HAL_RCC_LPUART1_CONFIG(PeriphClkInit->Lpuart1ClockSelection);
  405. }
  406. /*-------------------------- LPTIM1 clock source configuration -------------------*/
  407. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == (RCC_PERIPHCLK_LPTIM1))
  408. {
  409. assert_param(IS_RCC_LPTIM1CLK(PeriphClkInit->Lptim1ClockSelection));
  410. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->Lptim1ClockSelection);
  411. }
  412. /*-------------------------- LPTIM2 clock source configuration -------------------*/
  413. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM2) == (RCC_PERIPHCLK_LPTIM2))
  414. {
  415. assert_param(IS_RCC_LPTIM2CLK(PeriphClkInit->Lptim2ClockSelection));
  416. __HAL_RCC_LPTIM2_CONFIG(PeriphClkInit->Lptim2ClockSelection);
  417. }
  418. /*-------------------------- I2C1 clock source configuration ---------------------*/
  419. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C1) == RCC_PERIPHCLK_I2C1)
  420. {
  421. /* Check the parameters */
  422. assert_param(IS_RCC_I2C1CLKSOURCE(PeriphClkInit->I2c1ClockSelection));
  423. /* Configure the I2C1 clock source */
  424. __HAL_RCC_I2C1_CONFIG(PeriphClkInit->I2c1ClockSelection);
  425. }
  426. #if defined(I2C2)
  427. /*-------------------------- I2C2 clock source configuration ---------------------*/
  428. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C2) == RCC_PERIPHCLK_I2C2)
  429. {
  430. /* Check the parameters */
  431. assert_param(IS_RCC_I2C2CLKSOURCE(PeriphClkInit->I2c2ClockSelection));
  432. /* Configure the I2C2 clock source */
  433. __HAL_RCC_I2C2_CONFIG(PeriphClkInit->I2c2ClockSelection);
  434. }
  435. #endif /* I2C2 */
  436. /*-------------------------- I2C3 clock source configuration ---------------------*/
  437. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C3) == RCC_PERIPHCLK_I2C3)
  438. {
  439. /* Check the parameters */
  440. assert_param(IS_RCC_I2C3CLKSOURCE(PeriphClkInit->I2c3ClockSelection));
  441. /* Configure the I2C3 clock source */
  442. __HAL_RCC_I2C3_CONFIG(PeriphClkInit->I2c3ClockSelection);
  443. }
  444. #if defined(I2C4)
  445. /*-------------------------- I2C4 clock source configuration ---------------------*/
  446. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C4) == RCC_PERIPHCLK_I2C4)
  447. {
  448. /* Check the parameters */
  449. assert_param(IS_RCC_I2C4CLKSOURCE(PeriphClkInit->I2c4ClockSelection));
  450. /* Configure the I2C4 clock source */
  451. __HAL_RCC_I2C4_CONFIG(PeriphClkInit->I2c4ClockSelection);
  452. }
  453. #endif /* I2C4 */
  454. #if defined(USB_OTG_FS) || defined(USB)
  455. /*-------------------------- USB clock source configuration ----------------------*/
  456. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == (RCC_PERIPHCLK_USB))
  457. {
  458. assert_param(IS_RCC_USBCLKSOURCE(PeriphClkInit->UsbClockSelection));
  459. __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
  460. if(PeriphClkInit->UsbClockSelection == RCC_USBCLKSOURCE_PLL)
  461. {
  462. /* Enable PLL48M1CLK output clock */
  463. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_48M1CLK);
  464. }
  465. else
  466. {
  467. #if defined(RCC_PLLSAI1_SUPPORT)
  468. if(PeriphClkInit->UsbClockSelection == RCC_USBCLKSOURCE_PLLSAI1)
  469. {
  470. /* PLLSAI1 input clock, parameters M, N & Q configuration and clock output (PLLSAI1ClockOut) */
  471. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_Q_UPDATE);
  472. if(ret != HAL_OK)
  473. {
  474. /* set overall return value */
  475. status = ret;
  476. }
  477. }
  478. #endif /* RCC_PLLSAI1_SUPPORT */
  479. }
  480. }
  481. #endif /* USB_OTG_FS || USB */
  482. #if defined(SDMMC1)
  483. /*-------------------------- SDMMC1 clock source configuration -------------------*/
  484. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDMMC1) == (RCC_PERIPHCLK_SDMMC1))
  485. {
  486. assert_param(IS_RCC_SDMMC1CLKSOURCE(PeriphClkInit->Sdmmc1ClockSelection));
  487. __HAL_RCC_SDMMC1_CONFIG(PeriphClkInit->Sdmmc1ClockSelection);
  488. if(PeriphClkInit->Sdmmc1ClockSelection == RCC_SDMMC1CLKSOURCE_PLL) /* PLL "Q" ? */
  489. {
  490. /* Enable PLL48M1CLK output clock */
  491. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_48M1CLK);
  492. }
  493. #if defined(RCC_CCIPR2_SDMMCSEL)
  494. else if(PeriphClkInit->Sdmmc1ClockSelection == RCC_SDMMC1CLKSOURCE_PLLP) /* PLL "P" ? */
  495. {
  496. /* Enable PLLSAI3CLK output */
  497. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SAI3CLK);
  498. }
  499. #endif
  500. else if(PeriphClkInit->Sdmmc1ClockSelection == RCC_SDMMC1CLKSOURCE_PLLSAI1)
  501. {
  502. /* PLLSAI1 input clock, parameters M, N & Q configuration and clock output (PLLSAI1ClockOut) */
  503. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_Q_UPDATE);
  504. if(ret != HAL_OK)
  505. {
  506. /* set overall return value */
  507. status = ret;
  508. }
  509. }
  510. else
  511. {
  512. /* nothing to do */
  513. }
  514. }
  515. #endif /* SDMMC1 */
  516. /*-------------------------- RNG clock source configuration ----------------------*/
  517. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RNG) == (RCC_PERIPHCLK_RNG))
  518. {
  519. assert_param(IS_RCC_RNGCLKSOURCE(PeriphClkInit->RngClockSelection));
  520. __HAL_RCC_RNG_CONFIG(PeriphClkInit->RngClockSelection);
  521. if(PeriphClkInit->RngClockSelection == RCC_RNGCLKSOURCE_PLL)
  522. {
  523. /* Enable PLL48M1CLK output clock */
  524. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_48M1CLK);
  525. }
  526. #if defined(RCC_PLLSAI1_SUPPORT)
  527. else if(PeriphClkInit->RngClockSelection == RCC_RNGCLKSOURCE_PLLSAI1)
  528. {
  529. /* PLLSAI1 input clock, parameters M, N & Q configuration and clock output (PLLSAI1ClockOut) */
  530. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_Q_UPDATE);
  531. if(ret != HAL_OK)
  532. {
  533. /* set overall return value */
  534. status = ret;
  535. }
  536. }
  537. #endif /* RCC_PLLSAI1_SUPPORT */
  538. else
  539. {
  540. /* nothing to do */
  541. }
  542. }
  543. /*-------------------------- ADC clock source configuration ----------------------*/
  544. #if !defined(STM32L412xx) && !defined(STM32L422xx)
  545. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC) == RCC_PERIPHCLK_ADC)
  546. {
  547. /* Check the parameters */
  548. assert_param(IS_RCC_ADCCLKSOURCE(PeriphClkInit->AdcClockSelection));
  549. /* Configure the ADC interface clock source */
  550. __HAL_RCC_ADC_CONFIG(PeriphClkInit->AdcClockSelection);
  551. #if defined(RCC_PLLSAI1_SUPPORT)
  552. if(PeriphClkInit->AdcClockSelection == RCC_ADCCLKSOURCE_PLLSAI1)
  553. {
  554. /* PLLSAI1 input clock, parameters M, N & R configuration and clock output (PLLSAI1ClockOut) */
  555. ret = RCCEx_PLLSAI1_Config(&(PeriphClkInit->PLLSAI1), DIVIDER_R_UPDATE);
  556. if(ret != HAL_OK)
  557. {
  558. /* set overall return value */
  559. status = ret;
  560. }
  561. }
  562. #endif /* RCC_PLLSAI1_SUPPORT */
  563. #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L496xx) || defined(STM32L4A6xx)
  564. else if(PeriphClkInit->AdcClockSelection == RCC_ADCCLKSOURCE_PLLSAI2)
  565. {
  566. /* PLLSAI2 input clock, parameters M, N & R configuration and clock output (PLLSAI2ClockOut) */
  567. ret = RCCEx_PLLSAI2_Config(&(PeriphClkInit->PLLSAI2), DIVIDER_R_UPDATE);
  568. if(ret != HAL_OK)
  569. {
  570. /* set overall return value */
  571. status = ret;
  572. }
  573. }
  574. #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || STM32L496xx || STM32L4A6xx */
  575. }
  576. #endif /* !STM32L412xx && !STM32L422xx */
  577. #if defined(SWPMI1)
  578. /*-------------------------- SWPMI1 clock source configuration -------------------*/
  579. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SWPMI1) == RCC_PERIPHCLK_SWPMI1)
  580. {
  581. /* Check the parameters */
  582. assert_param(IS_RCC_SWPMI1CLKSOURCE(PeriphClkInit->Swpmi1ClockSelection));
  583. /* Configure the SWPMI1 clock source */
  584. __HAL_RCC_SWPMI1_CONFIG(PeriphClkInit->Swpmi1ClockSelection);
  585. }
  586. #endif /* SWPMI1 */
  587. #if defined(DFSDM1_Filter0)
  588. /*-------------------------- DFSDM1 clock source configuration -------------------*/
  589. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM1) == RCC_PERIPHCLK_DFSDM1)
  590. {
  591. /* Check the parameters */
  592. assert_param(IS_RCC_DFSDM1CLKSOURCE(PeriphClkInit->Dfsdm1ClockSelection));
  593. /* Configure the DFSDM1 interface clock source */
  594. __HAL_RCC_DFSDM1_CONFIG(PeriphClkInit->Dfsdm1ClockSelection);
  595. }
  596. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  597. /*-------------------------- DFSDM1 audio clock source configuration -------------*/
  598. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM1AUDIO) == RCC_PERIPHCLK_DFSDM1AUDIO)
  599. {
  600. /* Check the parameters */
  601. assert_param(IS_RCC_DFSDM1AUDIOCLKSOURCE(PeriphClkInit->Dfsdm1AudioClockSelection));
  602. /* Configure the DFSDM1 interface audio clock source */
  603. __HAL_RCC_DFSDM1AUDIO_CONFIG(PeriphClkInit->Dfsdm1AudioClockSelection);
  604. }
  605. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  606. #endif /* DFSDM1_Filter0 */
  607. #if defined(LTDC)
  608. /*-------------------------- LTDC clock source configuration --------------------*/
  609. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == RCC_PERIPHCLK_LTDC)
  610. {
  611. /* Check the parameters */
  612. assert_param(IS_RCC_LTDCCLKSOURCE(PeriphClkInit->LtdcClockSelection));
  613. /* Disable the PLLSAI2 */
  614. __HAL_RCC_PLLSAI2_DISABLE();
  615. /* Get Start Tick*/
  616. tickstart = HAL_GetTick();
  617. /* Wait till PLLSAI2 is ready */
  618. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) != 0U)
  619. {
  620. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  621. {
  622. ret = HAL_TIMEOUT;
  623. break;
  624. }
  625. }
  626. if(ret == HAL_OK)
  627. {
  628. /* Configure the LTDC clock source */
  629. __HAL_RCC_LTDC_CONFIG(PeriphClkInit->LtdcClockSelection);
  630. /* PLLSAI2 input clock, parameters M, N & R configuration and clock output (PLLSAI2ClockOut) */
  631. ret = RCCEx_PLLSAI2_Config(&(PeriphClkInit->PLLSAI2), DIVIDER_R_UPDATE);
  632. }
  633. if(ret != HAL_OK)
  634. {
  635. /* set overall return value */
  636. status = ret;
  637. }
  638. }
  639. #endif /* LTDC */
  640. #if defined(DSI)
  641. /*-------------------------- DSI clock source configuration ---------------------*/
  642. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DSI) == RCC_PERIPHCLK_DSI)
  643. {
  644. /* Check the parameters */
  645. assert_param(IS_RCC_DSICLKSOURCE(PeriphClkInit->DsiClockSelection));
  646. /* Configure the DSI clock source */
  647. __HAL_RCC_DSI_CONFIG(PeriphClkInit->DsiClockSelection);
  648. if(PeriphClkInit->DsiClockSelection == RCC_DSICLKSOURCE_PLLSAI2)
  649. {
  650. /* PLLSAI2 input clock, parameters M, N & Q configuration and clock output (PLLSAI2ClockOut) */
  651. ret = RCCEx_PLLSAI2_Config(&(PeriphClkInit->PLLSAI2), DIVIDER_Q_UPDATE);
  652. if(ret != HAL_OK)
  653. {
  654. /* set overall return value */
  655. status = ret;
  656. }
  657. }
  658. }
  659. #endif /* DSI */
  660. #if defined(OCTOSPI1) || defined(OCTOSPI2)
  661. /*-------------------------- OctoSPIx clock source configuration ----------------*/
  662. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_OSPI) == RCC_PERIPHCLK_OSPI)
  663. {
  664. /* Check the parameters */
  665. assert_param(IS_RCC_OSPICLKSOURCE(PeriphClkInit->OspiClockSelection));
  666. /* Configure the OctoSPI clock source */
  667. __HAL_RCC_OSPI_CONFIG(PeriphClkInit->OspiClockSelection);
  668. if(PeriphClkInit->OspiClockSelection == RCC_OSPICLKSOURCE_PLL)
  669. {
  670. /* Enable PLL48M1CLK output */
  671. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_48M1CLK);
  672. }
  673. }
  674. #endif /* OCTOSPI1 || OCTOSPI2 */
  675. return status;
  676. }
  677. /**
  678. * @brief Get the RCC_ClkInitStruct according to the internal RCC configuration registers.
  679. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  680. * returns the configuration information for the Extended Peripherals
  681. * clocks(SAI1, SAI2, LPTIM1, LPTIM2, I2C1, I2C2, I2C3, I2C4, LPUART1,
  682. * USART1, USART2, USART3, UART4, UART5, RTC, ADCx, DFSDMx, SWPMI1, USB, SDMMC1 and RNG).
  683. * @retval None
  684. */
  685. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  686. {
  687. /* Set all possible values for the extended clock type parameter------------*/
  688. #if defined(STM32L412xx) || defined(STM32L422xx)
  689. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | \
  690. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | \
  691. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_USB | \
  692. RCC_PERIPHCLK_RNG | \
  693. RCC_PERIPHCLK_RTC ;
  694. #elif defined(STM32L431xx)
  695. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | \
  696. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | \
  697. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | \
  698. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | \
  699. RCC_PERIPHCLK_RTC ;
  700. #elif defined(STM32L432xx) || defined(STM32L442xx)
  701. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | \
  702. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C3 | \
  703. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_USB | \
  704. RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | \
  705. RCC_PERIPHCLK_RTC ;
  706. #elif defined(STM32L433xx) || defined(STM32L443xx)
  707. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | \
  708. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | \
  709. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_USB | \
  710. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | \
  711. RCC_PERIPHCLK_RTC ;
  712. #elif defined(STM32L451xx)
  713. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | \
  714. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | RCC_PERIPHCLK_I2C4 | \
  715. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | \
  716. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_DFSDM1 | \
  717. RCC_PERIPHCLK_RTC ;
  718. #elif defined(STM32L452xx) || defined(STM32L462xx)
  719. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | \
  720. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | RCC_PERIPHCLK_I2C4 | \
  721. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_USB | \
  722. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_DFSDM1 | \
  723. RCC_PERIPHCLK_RTC ;
  724. #elif defined(STM32L471xx)
  725. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | RCC_PERIPHCLK_UART5 | \
  726. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | \
  727. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 | \
  728. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | RCC_PERIPHCLK_DFSDM1 | \
  729. RCC_PERIPHCLK_RTC ;
  730. #elif defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
  731. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | RCC_PERIPHCLK_UART5 | \
  732. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | \
  733. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 | RCC_PERIPHCLK_USB | \
  734. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | RCC_PERIPHCLK_DFSDM1 | \
  735. RCC_PERIPHCLK_RTC ;
  736. #elif defined(STM32L496xx) || defined(STM32L4A6xx)
  737. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | RCC_PERIPHCLK_UART5 | \
  738. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | RCC_PERIPHCLK_I2C4 | \
  739. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 | RCC_PERIPHCLK_USB | \
  740. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_SWPMI1 | RCC_PERIPHCLK_DFSDM1 | \
  741. RCC_PERIPHCLK_RTC ;
  742. #elif defined(STM32L4R5xx) || defined(STM32L4S5xx)
  743. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | RCC_PERIPHCLK_UART5 | \
  744. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | RCC_PERIPHCLK_I2C4 | \
  745. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 | RCC_PERIPHCLK_USB | \
  746. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_DFSDM1 | \
  747. RCC_PERIPHCLK_DFSDM1AUDIO | RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_OSPI;
  748. #elif defined(STM32L4R7xx) || defined(STM32L4S7xx) || defined(STM32L4Q5xx)
  749. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | RCC_PERIPHCLK_UART5 | \
  750. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | RCC_PERIPHCLK_I2C4 | \
  751. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 | RCC_PERIPHCLK_USB | \
  752. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_DFSDM1 | \
  753. RCC_PERIPHCLK_DFSDM1AUDIO | RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_OSPI | RCC_PERIPHCLK_LTDC;
  754. #elif defined(STM32L4R9xx) || defined(STM32L4S9xx)
  755. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4 | RCC_PERIPHCLK_UART5 | \
  756. RCC_PERIPHCLK_LPUART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_I2C2 | RCC_PERIPHCLK_I2C3 | RCC_PERIPHCLK_I2C4 | \
  757. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 | RCC_PERIPHCLK_USB | \
  758. RCC_PERIPHCLK_SDMMC1 | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_DFSDM1 | \
  759. RCC_PERIPHCLK_DFSDM1AUDIO | RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_OSPI | RCC_PERIPHCLK_LTDC | RCC_PERIPHCLK_DSI;
  760. #endif /* STM32L431xx */
  761. #if defined(RCC_PLLSAI1_SUPPORT)
  762. /* Get the PLLSAI1 Clock configuration -----------------------------------------------*/
  763. PeriphClkInit->PLLSAI1.PLLSAI1Source = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC) >> RCC_PLLCFGR_PLLSRC_Pos;
  764. #if defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  765. PeriphClkInit->PLLSAI1.PLLSAI1M = (READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1M) >> RCC_PLLSAI1CFGR_PLLSAI1M_Pos) + 1U;
  766. #else
  767. PeriphClkInit->PLLSAI1.PLLSAI1M = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U;
  768. #endif /* RCC_PLLSAI1M_DIV_1_16_SUPPORT */
  769. PeriphClkInit->PLLSAI1.PLLSAI1N = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos;
  770. PeriphClkInit->PLLSAI1.PLLSAI1P = ((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1P) >> RCC_PLLSAI1CFGR_PLLSAI1P_Pos) << 4U) + 7U;
  771. PeriphClkInit->PLLSAI1.PLLSAI1Q = ((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1Q) >> RCC_PLLSAI1CFGR_PLLSAI1Q_Pos) + 1U) * 2U;
  772. PeriphClkInit->PLLSAI1.PLLSAI1R = ((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1R) >> RCC_PLLSAI1CFGR_PLLSAI1R_Pos) + 1U) * 2U;
  773. #endif /* RCC_PLLSAI1_SUPPORT */
  774. #if defined(RCC_PLLSAI2_SUPPORT)
  775. /* Get the PLLSAI2 Clock configuration -----------------------------------------------*/
  776. PeriphClkInit->PLLSAI2.PLLSAI2Source = PeriphClkInit->PLLSAI1.PLLSAI1Source;
  777. #if defined(RCC_PLLSAI2M_DIV_1_16_SUPPORT)
  778. PeriphClkInit->PLLSAI2.PLLSAI2M = (READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2M) >> RCC_PLLSAI2CFGR_PLLSAI2M_Pos) + 1U;
  779. #else
  780. PeriphClkInit->PLLSAI2.PLLSAI2M = PeriphClkInit->PLLSAI1.PLLSAI1M;
  781. #endif /* RCC_PLLSAI2M_DIV_1_16_SUPPORT */
  782. PeriphClkInit->PLLSAI2.PLLSAI2N = READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N) >> RCC_PLLSAI2CFGR_PLLSAI2N_Pos;
  783. PeriphClkInit->PLLSAI2.PLLSAI2P = ((READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2P) >> RCC_PLLSAI2CFGR_PLLSAI2P_Pos) << 4U) + 7U;
  784. #if defined(RCC_PLLSAI2Q_DIV_SUPPORT)
  785. PeriphClkInit->PLLSAI2.PLLSAI2Q = ((READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2Q) >> RCC_PLLSAI2CFGR_PLLSAI2Q_Pos) + 1U) * 2U;
  786. #endif /* RCC_PLLSAI2Q_DIV_SUPPORT */
  787. PeriphClkInit->PLLSAI2.PLLSAI2R = ((READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2R)>> RCC_PLLSAI2CFGR_PLLSAI2R_Pos) + 1U) * 2U;
  788. #endif /* RCC_PLLSAI2_SUPPORT */
  789. /* Get the USART1 clock source ---------------------------------------------*/
  790. PeriphClkInit->Usart1ClockSelection = __HAL_RCC_GET_USART1_SOURCE();
  791. /* Get the USART2 clock source ---------------------------------------------*/
  792. PeriphClkInit->Usart2ClockSelection = __HAL_RCC_GET_USART2_SOURCE();
  793. #if defined(USART3)
  794. /* Get the USART3 clock source ---------------------------------------------*/
  795. PeriphClkInit->Usart3ClockSelection = __HAL_RCC_GET_USART3_SOURCE();
  796. #endif /* USART3 */
  797. #if defined(UART4)
  798. /* Get the UART4 clock source ----------------------------------------------*/
  799. PeriphClkInit->Uart4ClockSelection = __HAL_RCC_GET_UART4_SOURCE();
  800. #endif /* UART4 */
  801. #if defined(UART5)
  802. /* Get the UART5 clock source ----------------------------------------------*/
  803. PeriphClkInit->Uart5ClockSelection = __HAL_RCC_GET_UART5_SOURCE();
  804. #endif /* UART5 */
  805. /* Get the LPUART1 clock source --------------------------------------------*/
  806. PeriphClkInit->Lpuart1ClockSelection = __HAL_RCC_GET_LPUART1_SOURCE();
  807. /* Get the I2C1 clock source -----------------------------------------------*/
  808. PeriphClkInit->I2c1ClockSelection = __HAL_RCC_GET_I2C1_SOURCE();
  809. #if defined(I2C2)
  810. /* Get the I2C2 clock source ----------------------------------------------*/
  811. PeriphClkInit->I2c2ClockSelection = __HAL_RCC_GET_I2C2_SOURCE();
  812. #endif /* I2C2 */
  813. /* Get the I2C3 clock source -----------------------------------------------*/
  814. PeriphClkInit->I2c3ClockSelection = __HAL_RCC_GET_I2C3_SOURCE();
  815. #if defined(I2C4)
  816. /* Get the I2C4 clock source -----------------------------------------------*/
  817. PeriphClkInit->I2c4ClockSelection = __HAL_RCC_GET_I2C4_SOURCE();
  818. #endif /* I2C4 */
  819. /* Get the LPTIM1 clock source ---------------------------------------------*/
  820. PeriphClkInit->Lptim1ClockSelection = __HAL_RCC_GET_LPTIM1_SOURCE();
  821. /* Get the LPTIM2 clock source ---------------------------------------------*/
  822. PeriphClkInit->Lptim2ClockSelection = __HAL_RCC_GET_LPTIM2_SOURCE();
  823. #if defined(SAI1)
  824. /* Get the SAI1 clock source -----------------------------------------------*/
  825. PeriphClkInit->Sai1ClockSelection = __HAL_RCC_GET_SAI1_SOURCE();
  826. #endif /* SAI1 */
  827. #if defined(SAI2)
  828. /* Get the SAI2 clock source -----------------------------------------------*/
  829. PeriphClkInit->Sai2ClockSelection = __HAL_RCC_GET_SAI2_SOURCE();
  830. #endif /* SAI2 */
  831. /* Get the RTC clock source ------------------------------------------------*/
  832. PeriphClkInit->RTCClockSelection = __HAL_RCC_GET_RTC_SOURCE();
  833. #if defined(USB_OTG_FS) || defined(USB)
  834. /* Get the USB clock source ------------------------------------------------*/
  835. PeriphClkInit->UsbClockSelection = __HAL_RCC_GET_USB_SOURCE();
  836. #endif /* USB_OTG_FS || USB */
  837. #if defined(SDMMC1)
  838. /* Get the SDMMC1 clock source ---------------------------------------------*/
  839. PeriphClkInit->Sdmmc1ClockSelection = __HAL_RCC_GET_SDMMC1_SOURCE();
  840. #endif /* SDMMC1 */
  841. /* Get the RNG clock source ------------------------------------------------*/
  842. PeriphClkInit->RngClockSelection = __HAL_RCC_GET_RNG_SOURCE();
  843. #if !defined(STM32L412xx) && !defined(STM32L422xx)
  844. /* Get the ADC clock source ------------------------------------------------*/
  845. PeriphClkInit->AdcClockSelection = __HAL_RCC_GET_ADC_SOURCE();
  846. #endif /* !STM32L412xx && !STM32L422xx */
  847. #if defined(SWPMI1)
  848. /* Get the SWPMI1 clock source ---------------------------------------------*/
  849. PeriphClkInit->Swpmi1ClockSelection = __HAL_RCC_GET_SWPMI1_SOURCE();
  850. #endif /* SWPMI1 */
  851. #if defined(DFSDM1_Filter0)
  852. /* Get the DFSDM1 clock source ---------------------------------------------*/
  853. PeriphClkInit->Dfsdm1ClockSelection = __HAL_RCC_GET_DFSDM1_SOURCE();
  854. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  855. /* Get the DFSDM1 audio clock source ---------------------------------------*/
  856. PeriphClkInit->Dfsdm1AudioClockSelection = __HAL_RCC_GET_DFSDM1AUDIO_SOURCE();
  857. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  858. #endif /* DFSDM1_Filter0 */
  859. #if defined(LTDC)
  860. /* Get the LTDC clock source -----------------------------------------------*/
  861. PeriphClkInit->LtdcClockSelection = __HAL_RCC_GET_LTDC_SOURCE();
  862. #endif /* LTDC */
  863. #if defined(DSI)
  864. /* Get the DSI clock source ------------------------------------------------*/
  865. PeriphClkInit->DsiClockSelection = __HAL_RCC_GET_DSI_SOURCE();
  866. #endif /* DSI */
  867. #if defined(OCTOSPI1) || defined(OCTOSPI2)
  868. /* Get the OctoSPIclock source --------------------------------------------*/
  869. PeriphClkInit->OspiClockSelection = __HAL_RCC_GET_OSPI_SOURCE();
  870. #endif /* OCTOSPI1 || OCTOSPI2 */
  871. }
  872. /**
  873. * @brief Return the peripheral clock frequency for peripherals with clock source from PLLSAIs
  874. * @note Return 0 if peripheral clock identifier not managed by this API
  875. * @param PeriphClk Peripheral clock identifier
  876. * This parameter can be one of the following values:
  877. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  878. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  879. @if STM32L462xx
  880. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM)
  881. @endif
  882. @if STM32L486xx
  883. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM)
  884. @endif
  885. @if STM32L4A6xx
  886. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral clock (only for devices with DFSDM)
  887. @endif
  888. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  889. * @arg @ref RCC_PERIPHCLK_I2C2 I2C2 peripheral clock
  890. * @arg @ref RCC_PERIPHCLK_I2C3 I2C3 peripheral clock
  891. @if STM32L462xx
  892. * @arg @ref RCC_PERIPHCLK_I2C4 I2C4 peripheral clock (only for devices with I2C4)
  893. @endif
  894. @if STM32L4A6xx
  895. * @arg @ref RCC_PERIPHCLK_I2C4 I2C4 peripheral clock (only for devices with I2C4)
  896. @endif
  897. @if STM32L4S9xx
  898. * @arg @ref RCC_PERIPHCLK_I2C4 I2C4 peripheral clock (only for devices with I2C4)
  899. @endif
  900. * @arg @ref RCC_PERIPHCLK_LPTIM1 LPTIM1 peripheral clock
  901. * @arg @ref RCC_PERIPHCLK_LPTIM2 LPTIM2 peripheral clock
  902. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock
  903. * @arg @ref RCC_PERIPHCLK_RNG RNG peripheral clock
  904. * @arg @ref RCC_PERIPHCLK_SAI1 SAI1 peripheral clock (only for devices with SAI1)
  905. @if STM32L486xx
  906. * @arg @ref RCC_PERIPHCLK_SAI2 SAI2 peripheral clock (only for devices with SAI2)
  907. @endif
  908. @if STM32L4A6xx
  909. * @arg @ref RCC_PERIPHCLK_SAI2 SAI2 peripheral clock (only for devices with SAI2)
  910. @endif
  911. @if STM32L4S9xx
  912. * @arg @ref RCC_PERIPHCLK_SAI2 SAI2 peripheral clock (only for devices with SAI2)
  913. @endif
  914. * @arg @ref RCC_PERIPHCLK_SDMMC1 SDMMC1 peripheral clock
  915. @if STM32L443xx
  916. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  917. @endif
  918. @if STM32L486xx
  919. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  920. @endif
  921. @if STM32L4A6xx
  922. * @arg @ref RCC_PERIPHCLK_SWPMI1 SWPMI1 peripheral clock (only for devices with SWPMI1)
  923. @endif
  924. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  925. * @arg @ref RCC_PERIPHCLK_USART2 USART1 peripheral clock
  926. * @arg @ref RCC_PERIPHCLK_USART3 USART1 peripheral clock
  927. @if STM32L462xx
  928. * @arg @ref RCC_PERIPHCLK_UART4 UART4 peripheral clock (only for devices with UART4)
  929. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (only for devices with USB)
  930. @endif
  931. @if STM32L486xx
  932. * @arg @ref RCC_PERIPHCLK_UART4 UART4 peripheral clock (only for devices with UART4)
  933. * @arg @ref RCC_PERIPHCLK_UART5 UART5 peripheral clock (only for devices with UART5)
  934. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (only for devices with USB)
  935. @endif
  936. @if STM32L4A6xx
  937. * @arg @ref RCC_PERIPHCLK_UART4 UART4 peripheral clock (only for devices with UART4)
  938. * @arg @ref RCC_PERIPHCLK_UART5 UART5 peripheral clock (only for devices with UART5)
  939. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (only for devices with USB)
  940. @endif
  941. @if STM32L4S9xx
  942. * @arg @ref RCC_PERIPHCLK_UART4 USART1 peripheral clock (only for devices with UART4)
  943. * @arg @ref RCC_PERIPHCLK_UART5 USART1 peripheral clock (only for devices with UART5)
  944. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock (only for devices with USB)
  945. * @arg @ref RCC_PERIPHCLK_DFSDM1 DFSDM1 peripheral kernel clock (only for devices with DFSDM1)
  946. * @arg @ref RCC_PERIPHCLK_DFSDM1AUDIO DFSDM1 peripheral audio clock (only for devices with DFSDM1)
  947. * @arg @ref RCC_PERIPHCLK_LTDC LTDC peripheral clock (only for devices with LTDC)
  948. * @arg @ref RCC_PERIPHCLK_DSI DSI peripheral clock (only for devices with DSI)
  949. * @arg @ref RCC_PERIPHCLK_OSPI OctoSPI peripheral clock (only for devices with OctoSPI)
  950. @endif
  951. * @retval Frequency in Hz
  952. */
  953. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  954. {
  955. uint32_t frequency = 0U;
  956. uint32_t srcclk, pll_oscsource, pllvco, plln; /* no init needed */
  957. #if defined(SDMMC1) && defined(RCC_CCIPR2_SDMMCSEL)
  958. uint32_t pllp; /* no init needed */
  959. #endif
  960. /* Check the parameters */
  961. assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
  962. if(PeriphClk == RCC_PERIPHCLK_RTC)
  963. {
  964. /* Get the current RTC source */
  965. srcclk = __HAL_RCC_GET_RTC_SOURCE();
  966. switch(srcclk)
  967. {
  968. case RCC_RTCCLKSOURCE_LSE:
  969. /* Check if LSE is ready */
  970. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY))
  971. {
  972. frequency = LSE_VALUE;
  973. }
  974. break;
  975. case RCC_RTCCLKSOURCE_LSI:
  976. /* Check if LSI is ready */
  977. if(HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY))
  978. {
  979. #if defined(RCC_CSR_LSIPREDIV)
  980. if(HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIPREDIV))
  981. {
  982. frequency = LSI_VALUE/128U;
  983. }
  984. else
  985. #endif /* RCC_CSR_LSIPREDIV */
  986. {
  987. frequency = LSI_VALUE;
  988. }
  989. }
  990. break;
  991. case RCC_RTCCLKSOURCE_HSE_DIV32:
  992. /* Check if HSE is ready */
  993. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY))
  994. {
  995. frequency = HSE_VALUE / 32U;
  996. }
  997. break;
  998. default:
  999. /* No clock source, frequency default init at 0 */
  1000. break;
  1001. }
  1002. }
  1003. else
  1004. {
  1005. /* Other external peripheral clock source than RTC */
  1006. pll_oscsource = __HAL_RCC_GET_PLL_OSCSOURCE();
  1007. /* Compute PLL clock input */
  1008. switch(pll_oscsource)
  1009. {
  1010. case RCC_PLLSOURCE_MSI: /* MSI ? */
  1011. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_MSIRDY))
  1012. {
  1013. /*MSI frequency range in HZ*/
  1014. pllvco = MSIRangeTable[(__HAL_RCC_GET_MSI_RANGE() >> 4U)];
  1015. }
  1016. else
  1017. {
  1018. pllvco = 0U;
  1019. }
  1020. break;
  1021. case RCC_PLLSOURCE_HSI: /* HSI ? */
  1022. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1023. {
  1024. pllvco = HSI_VALUE;
  1025. }
  1026. else
  1027. {
  1028. pllvco = 0U;
  1029. }
  1030. break;
  1031. case RCC_PLLSOURCE_HSE: /* HSE ? */
  1032. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY))
  1033. {
  1034. pllvco = HSE_VALUE;
  1035. }
  1036. else
  1037. {
  1038. pllvco = 0U;
  1039. }
  1040. break;
  1041. default:
  1042. /* No source */
  1043. pllvco = 0U;
  1044. break;
  1045. }
  1046. switch(PeriphClk)
  1047. {
  1048. #if defined(SAI1)
  1049. case RCC_PERIPHCLK_SAI1:
  1050. frequency = RCCEx_GetSAIxPeriphCLKFreq(RCC_PERIPHCLK_SAI1, pllvco);
  1051. break;
  1052. #endif
  1053. #if defined(SAI2)
  1054. case RCC_PERIPHCLK_SAI2:
  1055. frequency = RCCEx_GetSAIxPeriphCLKFreq(RCC_PERIPHCLK_SAI2, pllvco);
  1056. break;
  1057. #endif
  1058. #if defined(USB_OTG_FS) || defined(USB)
  1059. case RCC_PERIPHCLK_USB:
  1060. #endif /* USB_OTG_FS || USB */
  1061. case RCC_PERIPHCLK_RNG:
  1062. #if defined(SDMMC1) && !defined(RCC_CCIPR2_SDMMCSEL)
  1063. case RCC_PERIPHCLK_SDMMC1:
  1064. #endif /* SDMMC1 && !RCC_CCIPR2_SDMMCSEL */
  1065. {
  1066. srcclk = READ_BIT(RCC->CCIPR, RCC_CCIPR_CLK48SEL);
  1067. switch(srcclk)
  1068. {
  1069. case RCC_CCIPR_CLK48SEL: /* MSI ? */
  1070. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_MSIRDY))
  1071. {
  1072. /*MSI frequency range in HZ*/
  1073. frequency = MSIRangeTable[(__HAL_RCC_GET_MSI_RANGE() >> 4U)];
  1074. }
  1075. break;
  1076. case RCC_CCIPR_CLK48SEL_1: /* PLL ? */
  1077. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLRDY))
  1078. {
  1079. if(HAL_IS_BIT_SET(RCC->PLLCFGR, RCC_PLLCFGR_PLLQEN))
  1080. {
  1081. /* f(PLL Source) * PLLN / PLLM */
  1082. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  1083. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  1084. /* f(PLL48M1CLK) = f(VCO input) / PLLQ */
  1085. frequency = (pllvco / (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U) << 1U));
  1086. }
  1087. }
  1088. break;
  1089. #if defined(RCC_PLLSAI1_SUPPORT)
  1090. case RCC_CCIPR_CLK48SEL_0: /* PLLSAI1 ? */
  1091. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLSAI1RDY))
  1092. {
  1093. if(HAL_IS_BIT_SET(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1QEN))
  1094. {
  1095. plln = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos;
  1096. #if defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  1097. /* PLLSAI1M exists: apply PLLSAI1M divider for PLLSAI1 output computation */
  1098. /* f(PLLSAI1 Source) * PLLSAI1N / PLLSAI1M */
  1099. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1M) >> RCC_PLLSAI1CFGR_PLLSAI1M_Pos) + 1U));
  1100. #else
  1101. /* f(PLL Source) * PLLSAI1N / PLLM */
  1102. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  1103. #endif
  1104. /* f(PLL48M2CLK) = f(VCOSAI1 input) / PLLSAI1Q */
  1105. frequency = (pllvco / (((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1Q) >> RCC_PLLSAI1CFGR_PLLSAI1Q_Pos) + 1U) << 1U));
  1106. }
  1107. }
  1108. break;
  1109. #endif /* RCC_PLLSAI1_SUPPORT */
  1110. #if defined(RCC_HSI48_SUPPORT)
  1111. case 0U:
  1112. if(HAL_IS_BIT_SET(RCC->CRRCR, RCC_CRRCR_HSI48RDY)) /* HSI48 ? */
  1113. {
  1114. frequency = HSI48_VALUE;
  1115. }
  1116. break;
  1117. #endif /* RCC_HSI48_SUPPORT */
  1118. default:
  1119. /* No clock source, frequency default init at 0 */
  1120. break;
  1121. } /* switch(srcclk) */
  1122. break;
  1123. }
  1124. #if defined(SDMMC1) && defined(RCC_CCIPR2_SDMMCSEL)
  1125. case RCC_PERIPHCLK_SDMMC1:
  1126. if(HAL_IS_BIT_SET(RCC->CCIPR2, RCC_CCIPR2_SDMMCSEL)) /* PLL "P" ? */
  1127. {
  1128. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLRDY))
  1129. {
  1130. if(HAL_IS_BIT_SET(RCC->PLLCFGR, RCC_PLLCFGR_PLLPEN))
  1131. {
  1132. /* f(PLL Source) * PLLN / PLLM */
  1133. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  1134. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  1135. /* f(PLLSAI3CLK) = f(VCO input) / PLLP */
  1136. pllp = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos;
  1137. if(pllp == 0U)
  1138. {
  1139. if(READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) != 0U)
  1140. {
  1141. pllp = 17U;
  1142. }
  1143. else
  1144. {
  1145. pllp = 7U;
  1146. }
  1147. }
  1148. frequency = (pllvco / pllp);
  1149. }
  1150. }
  1151. }
  1152. else /* 48MHz from PLL "Q" or MSI or PLLSAI1Q or HSI48 */
  1153. {
  1154. srcclk = READ_BIT(RCC->CCIPR, RCC_CCIPR_CLK48SEL);
  1155. switch(srcclk)
  1156. {
  1157. case RCC_CCIPR_CLK48SEL: /* MSI ? */
  1158. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_MSIRDY))
  1159. {
  1160. /*MSI frequency range in HZ*/
  1161. frequency = MSIRangeTable[(__HAL_RCC_GET_MSI_RANGE() >> 4U)];
  1162. }
  1163. break;
  1164. case RCC_CCIPR_CLK48SEL_1: /* PLL "Q" ? */
  1165. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLRDY))
  1166. {
  1167. if(HAL_IS_BIT_SET(RCC->PLLCFGR, RCC_PLLCFGR_PLLQEN))
  1168. {
  1169. /* f(PLL Source) * PLLN / PLLM */
  1170. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  1171. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  1172. /* f(PLL48M1CLK) = f(VCO input) / PLLQ */
  1173. frequency = (pllvco / (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U) << 1U));
  1174. }
  1175. }
  1176. break;
  1177. case RCC_CCIPR_CLK48SEL_0: /* PLLSAI1 ? */
  1178. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLSAI1RDY))
  1179. {
  1180. if(HAL_IS_BIT_SET(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1QEN))
  1181. {
  1182. /* f(PLLSAI1 Source) * PLLSAI1N / PLLSAI1M */
  1183. plln = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos;
  1184. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1M) >> RCC_PLLSAI1CFGR_PLLSAI1M_Pos) + 1U));
  1185. /* f(PLL48M2CLK) = f(VCOSAI1 input) / PLLSAI1Q */
  1186. frequency = (pllvco / (((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1Q) >> RCC_PLLSAI1CFGR_PLLSAI1Q_Pos) + 1U) << 1U));
  1187. }
  1188. }
  1189. break;
  1190. case 0U:
  1191. if(HAL_IS_BIT_SET(RCC->CRRCR, RCC_CRRCR_HSI48RDY)) /* HSI48 ? */
  1192. {
  1193. frequency = HSI48_VALUE;
  1194. }
  1195. break;
  1196. default:
  1197. /* No clock source, frequency default init at 0 */
  1198. break;
  1199. } /* switch(srcclk) */
  1200. }
  1201. break;
  1202. #endif /* SDMMC1 && RCC_CCIPR2_SDMMCSEL */
  1203. case RCC_PERIPHCLK_USART1:
  1204. {
  1205. /* Get the current USART1 source */
  1206. srcclk = __HAL_RCC_GET_USART1_SOURCE();
  1207. switch(srcclk)
  1208. {
  1209. case RCC_USART1CLKSOURCE_PCLK2:
  1210. frequency = HAL_RCC_GetPCLK2Freq();
  1211. break;
  1212. case RCC_USART1CLKSOURCE_SYSCLK:
  1213. frequency = HAL_RCC_GetSysClockFreq();
  1214. break;
  1215. case RCC_USART1CLKSOURCE_HSI:
  1216. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1217. {
  1218. frequency = HSI_VALUE;
  1219. }
  1220. break;
  1221. case RCC_USART1CLKSOURCE_LSE:
  1222. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY))
  1223. {
  1224. frequency = LSE_VALUE;
  1225. }
  1226. break;
  1227. default:
  1228. /* No clock source, frequency default init at 0 */
  1229. break;
  1230. }
  1231. break;
  1232. }
  1233. case RCC_PERIPHCLK_USART2:
  1234. {
  1235. /* Get the current USART2 source */
  1236. srcclk = __HAL_RCC_GET_USART2_SOURCE();
  1237. switch(srcclk)
  1238. {
  1239. case RCC_USART2CLKSOURCE_PCLK1:
  1240. frequency = HAL_RCC_GetPCLK1Freq();
  1241. break;
  1242. case RCC_USART2CLKSOURCE_SYSCLK:
  1243. frequency = HAL_RCC_GetSysClockFreq();
  1244. break;
  1245. case RCC_USART2CLKSOURCE_HSI:
  1246. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1247. {
  1248. frequency = HSI_VALUE;
  1249. }
  1250. break;
  1251. case RCC_USART2CLKSOURCE_LSE:
  1252. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY))
  1253. {
  1254. frequency = LSE_VALUE;
  1255. }
  1256. break;
  1257. default:
  1258. /* No clock source, frequency default init at 0 */
  1259. break;
  1260. }
  1261. break;
  1262. }
  1263. #if defined(USART3)
  1264. case RCC_PERIPHCLK_USART3:
  1265. {
  1266. /* Get the current USART3 source */
  1267. srcclk = __HAL_RCC_GET_USART3_SOURCE();
  1268. switch(srcclk)
  1269. {
  1270. case RCC_USART3CLKSOURCE_PCLK1:
  1271. frequency = HAL_RCC_GetPCLK1Freq();
  1272. break;
  1273. case RCC_USART3CLKSOURCE_SYSCLK:
  1274. frequency = HAL_RCC_GetSysClockFreq();
  1275. break;
  1276. case RCC_USART3CLKSOURCE_HSI:
  1277. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1278. {
  1279. frequency = HSI_VALUE;
  1280. }
  1281. break;
  1282. case RCC_USART3CLKSOURCE_LSE:
  1283. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY))
  1284. {
  1285. frequency = LSE_VALUE;
  1286. }
  1287. break;
  1288. default:
  1289. /* No clock source, frequency default init at 0 */
  1290. break;
  1291. }
  1292. break;
  1293. }
  1294. #endif /* USART3 */
  1295. #if defined(UART4)
  1296. case RCC_PERIPHCLK_UART4:
  1297. {
  1298. /* Get the current UART4 source */
  1299. srcclk = __HAL_RCC_GET_UART4_SOURCE();
  1300. switch(srcclk)
  1301. {
  1302. case RCC_UART4CLKSOURCE_PCLK1:
  1303. frequency = HAL_RCC_GetPCLK1Freq();
  1304. break;
  1305. case RCC_UART4CLKSOURCE_SYSCLK:
  1306. frequency = HAL_RCC_GetSysClockFreq();
  1307. break;
  1308. case RCC_UART4CLKSOURCE_HSI:
  1309. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1310. {
  1311. frequency = HSI_VALUE;
  1312. }
  1313. break;
  1314. case RCC_UART4CLKSOURCE_LSE:
  1315. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY))
  1316. {
  1317. frequency = LSE_VALUE;
  1318. }
  1319. break;
  1320. default:
  1321. /* No clock source, frequency default init at 0 */
  1322. break;
  1323. }
  1324. break;
  1325. }
  1326. #endif /* UART4 */
  1327. #if defined(UART5)
  1328. case RCC_PERIPHCLK_UART5:
  1329. {
  1330. /* Get the current UART5 source */
  1331. srcclk = __HAL_RCC_GET_UART5_SOURCE();
  1332. switch(srcclk)
  1333. {
  1334. case RCC_UART5CLKSOURCE_PCLK1:
  1335. frequency = HAL_RCC_GetPCLK1Freq();
  1336. break;
  1337. case RCC_UART5CLKSOURCE_SYSCLK:
  1338. frequency = HAL_RCC_GetSysClockFreq();
  1339. break;
  1340. case RCC_UART5CLKSOURCE_HSI:
  1341. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1342. {
  1343. frequency = HSI_VALUE;
  1344. }
  1345. break;
  1346. case RCC_UART5CLKSOURCE_LSE:
  1347. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY))
  1348. {
  1349. frequency = LSE_VALUE;
  1350. }
  1351. break;
  1352. default:
  1353. /* No clock source, frequency default init at 0 */
  1354. break;
  1355. }
  1356. break;
  1357. }
  1358. #endif /* UART5 */
  1359. case RCC_PERIPHCLK_LPUART1:
  1360. {
  1361. /* Get the current LPUART1 source */
  1362. srcclk = __HAL_RCC_GET_LPUART1_SOURCE();
  1363. switch(srcclk)
  1364. {
  1365. case RCC_LPUART1CLKSOURCE_PCLK1:
  1366. frequency = HAL_RCC_GetPCLK1Freq();
  1367. break;
  1368. case RCC_LPUART1CLKSOURCE_SYSCLK:
  1369. frequency = HAL_RCC_GetSysClockFreq();
  1370. break;
  1371. case RCC_LPUART1CLKSOURCE_HSI:
  1372. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1373. {
  1374. frequency = HSI_VALUE;
  1375. }
  1376. break;
  1377. case RCC_LPUART1CLKSOURCE_LSE:
  1378. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY))
  1379. {
  1380. frequency = LSE_VALUE;
  1381. }
  1382. break;
  1383. default:
  1384. /* No clock source, frequency default init at 0 */
  1385. break;
  1386. }
  1387. break;
  1388. }
  1389. case RCC_PERIPHCLK_ADC:
  1390. {
  1391. srcclk = __HAL_RCC_GET_ADC_SOURCE();
  1392. switch(srcclk)
  1393. {
  1394. case RCC_ADCCLKSOURCE_SYSCLK:
  1395. frequency = HAL_RCC_GetSysClockFreq();
  1396. break;
  1397. #if defined(RCC_PLLSAI1_SUPPORT)
  1398. case RCC_ADCCLKSOURCE_PLLSAI1:
  1399. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLSAI1RDY) && (__HAL_RCC_GET_PLLSAI1CLKOUT_CONFIG(RCC_PLLSAI1_ADC1CLK) != 0U))
  1400. {
  1401. plln = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos;
  1402. #if defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  1403. /* PLLSAI1M exists: apply PLLSAI1M divider for PLLSAI1 output computation */
  1404. /* f(PLLSAI1 Source) * PLLSAI1N / PLLSAI1M */
  1405. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1M) >> RCC_PLLSAI1CFGR_PLLSAI1M_Pos) + 1U));
  1406. #else
  1407. /* f(PLL Source) * PLLSAI1N / PLLM */
  1408. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  1409. #endif
  1410. /* f(PLLADC1CLK) = f(VCOSAI1 input) / PLLSAI1R */
  1411. frequency = (pllvco / (((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1R) >> RCC_PLLSAI1CFGR_PLLSAI1R_Pos) + 1U) << 1U));
  1412. }
  1413. break;
  1414. #endif /* RCC_PLLSAI1_SUPPORT */
  1415. #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L496xx) || defined(STM32L4A6xx)
  1416. case RCC_ADCCLKSOURCE_PLLSAI2:
  1417. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLSAI2RDY) && (__HAL_RCC_GET_PLLSAI2CLKOUT_CONFIG(RCC_PLLSAI2_ADC2CLK) != 0U))
  1418. {
  1419. plln = READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N) >> RCC_PLLSAI2CFGR_PLLSAI2N_Pos;
  1420. #if defined(RCC_PLLSAI2M_DIV_1_16_SUPPORT)
  1421. /* PLLSAI2M exists: apply PLLSAI2M divider for PLLSAI2 output computation */
  1422. /* f(PLLSAI2 Source) * PLLSAI2N / PLLSAI2M */
  1423. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2M) >> RCC_PLLSAI2CFGR_PLLSAI2M_Pos) + 1U));
  1424. #else
  1425. /* f(PLL Source) * PLLSAI2N / PLLM */
  1426. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  1427. #endif
  1428. /* f(PLLADC2CLK) = f(VCOSAI2 input) / PLLSAI2R */
  1429. frequency = (pllvco / (((READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2R) >> RCC_PLLSAI2CFGR_PLLSAI2R_Pos) + 1U) << 1U));
  1430. }
  1431. break;
  1432. #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || STM32L496xx || STM32L4A6xx */
  1433. default:
  1434. /* No clock source, frequency default init at 0 */
  1435. break;
  1436. }
  1437. break;
  1438. }
  1439. #if defined(DFSDM1_Filter0)
  1440. case RCC_PERIPHCLK_DFSDM1:
  1441. {
  1442. /* Get the current DFSDM1 source */
  1443. srcclk = __HAL_RCC_GET_DFSDM1_SOURCE();
  1444. if(srcclk == RCC_DFSDM1CLKSOURCE_PCLK2)
  1445. {
  1446. frequency = HAL_RCC_GetPCLK2Freq();
  1447. }
  1448. else
  1449. {
  1450. frequency = HAL_RCC_GetSysClockFreq();
  1451. }
  1452. break;
  1453. }
  1454. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1455. case RCC_PERIPHCLK_DFSDM1AUDIO:
  1456. {
  1457. /* Get the current DFSDM1 audio source */
  1458. srcclk = __HAL_RCC_GET_DFSDM1AUDIO_SOURCE();
  1459. switch(srcclk)
  1460. {
  1461. case RCC_DFSDM1AUDIOCLKSOURCE_SAI1:
  1462. frequency = RCCEx_GetSAIxPeriphCLKFreq(RCC_PERIPHCLK_SAI1, pllvco);
  1463. break;
  1464. case RCC_DFSDM1AUDIOCLKSOURCE_MSI:
  1465. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_MSIRDY))
  1466. {
  1467. /*MSI frequency range in HZ*/
  1468. frequency = MSIRangeTable[(__HAL_RCC_GET_MSI_RANGE() >> 4U)];
  1469. }
  1470. break;
  1471. case RCC_DFSDM1AUDIOCLKSOURCE_HSI:
  1472. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1473. {
  1474. frequency = HSI_VALUE;
  1475. }
  1476. break;
  1477. default:
  1478. /* No clock source, frequency default init at 0 */
  1479. break;
  1480. }
  1481. break;
  1482. }
  1483. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1484. #endif /* DFSDM1_Filter0 */
  1485. case RCC_PERIPHCLK_I2C1:
  1486. {
  1487. /* Get the current I2C1 source */
  1488. srcclk = __HAL_RCC_GET_I2C1_SOURCE();
  1489. switch(srcclk)
  1490. {
  1491. case RCC_I2C1CLKSOURCE_PCLK1:
  1492. frequency = HAL_RCC_GetPCLK1Freq();
  1493. break;
  1494. case RCC_I2C1CLKSOURCE_SYSCLK:
  1495. frequency = HAL_RCC_GetSysClockFreq();
  1496. break;
  1497. case RCC_I2C1CLKSOURCE_HSI:
  1498. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1499. {
  1500. frequency = HSI_VALUE;
  1501. }
  1502. break;
  1503. default:
  1504. /* No clock source, frequency default init at 0 */
  1505. break;
  1506. }
  1507. break;
  1508. }
  1509. #if defined(I2C2)
  1510. case RCC_PERIPHCLK_I2C2:
  1511. {
  1512. /* Get the current I2C2 source */
  1513. srcclk = __HAL_RCC_GET_I2C2_SOURCE();
  1514. switch(srcclk)
  1515. {
  1516. case RCC_I2C2CLKSOURCE_PCLK1:
  1517. frequency = HAL_RCC_GetPCLK1Freq();
  1518. break;
  1519. case RCC_I2C2CLKSOURCE_SYSCLK:
  1520. frequency = HAL_RCC_GetSysClockFreq();
  1521. break;
  1522. case RCC_I2C2CLKSOURCE_HSI:
  1523. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1524. {
  1525. frequency = HSI_VALUE;
  1526. }
  1527. break;
  1528. default:
  1529. /* No clock source, frequency default init at 0 */
  1530. break;
  1531. }
  1532. break;
  1533. }
  1534. #endif /* I2C2 */
  1535. case RCC_PERIPHCLK_I2C3:
  1536. {
  1537. /* Get the current I2C3 source */
  1538. srcclk = __HAL_RCC_GET_I2C3_SOURCE();
  1539. switch(srcclk)
  1540. {
  1541. case RCC_I2C3CLKSOURCE_PCLK1:
  1542. frequency = HAL_RCC_GetPCLK1Freq();
  1543. break;
  1544. case RCC_I2C3CLKSOURCE_SYSCLK:
  1545. frequency = HAL_RCC_GetSysClockFreq();
  1546. break;
  1547. case RCC_I2C3CLKSOURCE_HSI:
  1548. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1549. {
  1550. frequency = HSI_VALUE;
  1551. }
  1552. break;
  1553. default:
  1554. /* No clock source, frequency default init at 0 */
  1555. break;
  1556. }
  1557. break;
  1558. }
  1559. #if defined(I2C4)
  1560. case RCC_PERIPHCLK_I2C4:
  1561. {
  1562. /* Get the current I2C4 source */
  1563. srcclk = __HAL_RCC_GET_I2C4_SOURCE();
  1564. switch(srcclk)
  1565. {
  1566. case RCC_I2C4CLKSOURCE_PCLK1:
  1567. frequency = HAL_RCC_GetPCLK1Freq();
  1568. break;
  1569. case RCC_I2C4CLKSOURCE_SYSCLK:
  1570. frequency = HAL_RCC_GetSysClockFreq();
  1571. break;
  1572. case RCC_I2C4CLKSOURCE_HSI:
  1573. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1574. {
  1575. frequency = HSI_VALUE;
  1576. }
  1577. break;
  1578. default:
  1579. /* No clock source, frequency default init at 0 */
  1580. break;
  1581. }
  1582. break;
  1583. }
  1584. #endif /* I2C4 */
  1585. case RCC_PERIPHCLK_LPTIM1:
  1586. {
  1587. /* Get the current LPTIM1 source */
  1588. srcclk = __HAL_RCC_GET_LPTIM1_SOURCE();
  1589. switch(srcclk)
  1590. {
  1591. case RCC_LPTIM1CLKSOURCE_PCLK1:
  1592. frequency = HAL_RCC_GetPCLK1Freq();
  1593. break;
  1594. case RCC_LPTIM1CLKSOURCE_LSI:
  1595. if(HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY))
  1596. {
  1597. #if defined(RCC_CSR_LSIPREDIV)
  1598. if(HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIPREDIV))
  1599. {
  1600. frequency = LSI_VALUE/128U;
  1601. }
  1602. else
  1603. #endif /* RCC_CSR_LSIPREDIV */
  1604. {
  1605. frequency = LSI_VALUE;
  1606. }
  1607. }
  1608. break;
  1609. case RCC_LPTIM1CLKSOURCE_HSI:
  1610. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1611. {
  1612. frequency = HSI_VALUE;
  1613. }
  1614. break;
  1615. case RCC_LPTIM1CLKSOURCE_LSE:
  1616. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY))
  1617. {
  1618. frequency = LSE_VALUE;
  1619. }
  1620. break;
  1621. default:
  1622. /* No clock source, frequency default init at 0 */
  1623. break;
  1624. }
  1625. break;
  1626. }
  1627. case RCC_PERIPHCLK_LPTIM2:
  1628. {
  1629. /* Get the current LPTIM2 source */
  1630. srcclk = __HAL_RCC_GET_LPTIM2_SOURCE();
  1631. switch(srcclk)
  1632. {
  1633. case RCC_LPTIM2CLKSOURCE_PCLK1:
  1634. frequency = HAL_RCC_GetPCLK1Freq();
  1635. break;
  1636. case RCC_LPTIM2CLKSOURCE_LSI:
  1637. if(HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY))
  1638. {
  1639. #if defined(RCC_CSR_LSIPREDIV)
  1640. if(HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIPREDIV))
  1641. {
  1642. frequency = LSI_VALUE/128U;
  1643. }
  1644. else
  1645. #endif /* RCC_CSR_LSIPREDIV */
  1646. {
  1647. frequency = LSI_VALUE;
  1648. }
  1649. }
  1650. break;
  1651. case RCC_LPTIM2CLKSOURCE_HSI:
  1652. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1653. {
  1654. frequency = HSI_VALUE;
  1655. }
  1656. break;
  1657. case RCC_LPTIM2CLKSOURCE_LSE:
  1658. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY))
  1659. {
  1660. frequency = LSE_VALUE;
  1661. }
  1662. break;
  1663. default:
  1664. /* No clock source, frequency default init at 0 */
  1665. break;
  1666. }
  1667. break;
  1668. }
  1669. #if defined(SWPMI1)
  1670. case RCC_PERIPHCLK_SWPMI1:
  1671. {
  1672. /* Get the current SWPMI1 source */
  1673. srcclk = __HAL_RCC_GET_SWPMI1_SOURCE();
  1674. switch(srcclk)
  1675. {
  1676. case RCC_SWPMI1CLKSOURCE_PCLK1:
  1677. frequency = HAL_RCC_GetPCLK1Freq();
  1678. break;
  1679. case RCC_SWPMI1CLKSOURCE_HSI:
  1680. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  1681. {
  1682. frequency = HSI_VALUE;
  1683. }
  1684. break;
  1685. default:
  1686. /* No clock source, frequency default init at 0 */
  1687. break;
  1688. }
  1689. break;
  1690. }
  1691. #endif /* SWPMI1 */
  1692. #if defined(OCTOSPI1) || defined(OCTOSPI2)
  1693. case RCC_PERIPHCLK_OSPI:
  1694. {
  1695. /* Get the current OctoSPI clock source */
  1696. srcclk = __HAL_RCC_GET_OSPI_SOURCE();
  1697. switch(srcclk)
  1698. {
  1699. case RCC_OSPICLKSOURCE_SYSCLK:
  1700. frequency = HAL_RCC_GetSysClockFreq();
  1701. break;
  1702. case RCC_OSPICLKSOURCE_MSI:
  1703. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_MSIRDY))
  1704. {
  1705. /*MSI frequency range in HZ*/
  1706. frequency = MSIRangeTable[(__HAL_RCC_GET_MSI_RANGE() >> 4U)];
  1707. }
  1708. break;
  1709. case RCC_OSPICLKSOURCE_PLL:
  1710. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLRDY))
  1711. {
  1712. if(HAL_IS_BIT_SET(RCC->PLLCFGR, RCC_PLLCFGR_PLLQEN))
  1713. {
  1714. /* f(PLL Source) * PLLN / PLLM */
  1715. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  1716. pllvco = ((pllvco * plln) / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  1717. /* f(PLL48M1CLK) = f(VCO input) / PLLQ */
  1718. frequency = (pllvco / (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U) << 1U));
  1719. }
  1720. }
  1721. break;
  1722. default:
  1723. /* No clock source, frequency default init at 0 */
  1724. break;
  1725. }
  1726. break;
  1727. }
  1728. #endif /* OCTOSPI1 || OCTOSPI2 */
  1729. default:
  1730. break;
  1731. }
  1732. }
  1733. return(frequency);
  1734. }
  1735. /**
  1736. * @}
  1737. */
  1738. /** @defgroup RCCEx_Exported_Functions_Group2 Extended Clock management functions
  1739. * @brief Extended Clock management functions
  1740. *
  1741. @verbatim
  1742. ===============================================================================
  1743. ##### Extended clock management functions #####
  1744. ===============================================================================
  1745. [..]
  1746. This subsection provides a set of functions allowing to control the
  1747. activation or deactivation of MSI PLL-mode, PLLSAI1, PLLSAI2, LSE CSS,
  1748. Low speed clock output and clock after wake-up from STOP mode.
  1749. @endverbatim
  1750. * @{
  1751. */
  1752. #if defined(RCC_PLLSAI1_SUPPORT)
  1753. /**
  1754. * @brief Enable PLLSAI1.
  1755. * @param PLLSAI1Init pointer to an RCC_PLLSAI1InitTypeDef structure that
  1756. * contains the configuration information for the PLLSAI1
  1757. * @retval HAL status
  1758. */
  1759. HAL_StatusTypeDef HAL_RCCEx_EnablePLLSAI1(RCC_PLLSAI1InitTypeDef *PLLSAI1Init)
  1760. {
  1761. uint32_t tickstart;
  1762. HAL_StatusTypeDef status = HAL_OK;
  1763. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  1764. assert_param(IS_RCC_PLLSAI1SOURCE(PLLSAI1Init->PLLSAI1Source));
  1765. assert_param(IS_RCC_PLLSAI1M_VALUE(PLLSAI1Init->PLLSAI1M));
  1766. assert_param(IS_RCC_PLLSAI1N_VALUE(PLLSAI1Init->PLLSAI1N));
  1767. assert_param(IS_RCC_PLLSAI1P_VALUE(PLLSAI1Init->PLLSAI1P));
  1768. assert_param(IS_RCC_PLLSAI1Q_VALUE(PLLSAI1Init->PLLSAI1Q));
  1769. assert_param(IS_RCC_PLLSAI1R_VALUE(PLLSAI1Init->PLLSAI1R));
  1770. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PLLSAI1Init->PLLSAI1ClockOut));
  1771. /* Disable the PLLSAI1 */
  1772. __HAL_RCC_PLLSAI1_DISABLE();
  1773. /* Get Start Tick*/
  1774. tickstart = HAL_GetTick();
  1775. /* Wait till PLLSAI1 is ready to be updated */
  1776. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) != 0U)
  1777. {
  1778. if((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1779. {
  1780. status = HAL_TIMEOUT;
  1781. break;
  1782. }
  1783. }
  1784. if(status == HAL_OK)
  1785. {
  1786. #if defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  1787. /* Configure the PLLSAI1 Multiplication factor N */
  1788. /* Configure the PLLSAI1 Division factors M, P, Q and R */
  1789. __HAL_RCC_PLLSAI1_CONFIG(PLLSAI1Init->PLLSAI1M, PLLSAI1Init->PLLSAI1N, PLLSAI1Init->PLLSAI1P, PLLSAI1Init->PLLSAI1Q, PLLSAI1Init->PLLSAI1R);
  1790. #else
  1791. /* Configure the PLLSAI1 Multiplication factor N */
  1792. /* Configure the PLLSAI1 Division factors P, Q and R */
  1793. __HAL_RCC_PLLSAI1_CONFIG(PLLSAI1Init->PLLSAI1N, PLLSAI1Init->PLLSAI1P, PLLSAI1Init->PLLSAI1Q, PLLSAI1Init->PLLSAI1R);
  1794. #endif /* RCC_PLLSAI1M_DIV_1_16_SUPPORT */
  1795. /* Configure the PLLSAI1 Clock output(s) */
  1796. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PLLSAI1Init->PLLSAI1ClockOut);
  1797. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  1798. __HAL_RCC_PLLSAI1_ENABLE();
  1799. /* Get Start Tick*/
  1800. tickstart = HAL_GetTick();
  1801. /* Wait till PLLSAI1 is ready */
  1802. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) == 0U)
  1803. {
  1804. if((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1805. {
  1806. status = HAL_TIMEOUT;
  1807. break;
  1808. }
  1809. }
  1810. }
  1811. return status;
  1812. }
  1813. /**
  1814. * @brief Disable PLLSAI1.
  1815. * @retval HAL status
  1816. */
  1817. HAL_StatusTypeDef HAL_RCCEx_DisablePLLSAI1(void)
  1818. {
  1819. uint32_t tickstart;
  1820. HAL_StatusTypeDef status = HAL_OK;
  1821. /* Disable the PLLSAI1 */
  1822. __HAL_RCC_PLLSAI1_DISABLE();
  1823. /* Get Start Tick*/
  1824. tickstart = HAL_GetTick();
  1825. /* Wait till PLLSAI1 is ready */
  1826. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) != 0U)
  1827. {
  1828. if((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1829. {
  1830. status = HAL_TIMEOUT;
  1831. break;
  1832. }
  1833. }
  1834. /* Disable the PLLSAI1 Clock outputs */
  1835. __HAL_RCC_PLLSAI1CLKOUT_DISABLE(RCC_PLLSAI1CFGR_PLLSAI1PEN|RCC_PLLSAI1CFGR_PLLSAI1QEN|RCC_PLLSAI1CFGR_PLLSAI1REN);
  1836. /* Reset PLL source to save power if no PLLs on */
  1837. #if defined(RCC_PLLSAI2_SUPPORT)
  1838. if(READ_BIT(RCC->CR, (RCC_CR_PLLRDY | RCC_CR_PLLSAI2RDY)) == 0U)
  1839. {
  1840. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
  1841. }
  1842. #else
  1843. if(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  1844. {
  1845. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
  1846. }
  1847. #endif /* RCC_PLLSAI2_SUPPORT */
  1848. return status;
  1849. }
  1850. #endif /* RCC_PLLSAI1_SUPPORT */
  1851. #if defined(RCC_PLLSAI2_SUPPORT)
  1852. /**
  1853. * @brief Enable PLLSAI2.
  1854. * @param PLLSAI2Init pointer to an RCC_PLLSAI2InitTypeDef structure that
  1855. * contains the configuration information for the PLLSAI2
  1856. * @retval HAL status
  1857. */
  1858. HAL_StatusTypeDef HAL_RCCEx_EnablePLLSAI2(RCC_PLLSAI2InitTypeDef *PLLSAI2Init)
  1859. {
  1860. uint32_t tickstart;
  1861. HAL_StatusTypeDef status = HAL_OK;
  1862. /* check for PLLSAI2 Parameters used to output PLLSAI2CLK */
  1863. assert_param(IS_RCC_PLLSAI2SOURCE(PLLSAI2Init->PLLSAI2Source));
  1864. assert_param(IS_RCC_PLLSAI2M_VALUE(PLLSAI2Init->PLLSAI2M));
  1865. assert_param(IS_RCC_PLLSAI2N_VALUE(PLLSAI2Init->PLLSAI2N));
  1866. assert_param(IS_RCC_PLLSAI2P_VALUE(PLLSAI2Init->PLLSAI2P));
  1867. #if defined(RCC_PLLSAI2Q_DIV_SUPPORT)
  1868. assert_param(IS_RCC_PLLSAI2Q_VALUE(PLLSAI2Init->PLLSAI2Q));
  1869. #endif /* RCC_PLLSAI2Q_DIV_SUPPORT */
  1870. assert_param(IS_RCC_PLLSAI2R_VALUE(PLLSAI2Init->PLLSAI2R));
  1871. assert_param(IS_RCC_PLLSAI2CLOCKOUT_VALUE(PLLSAI2Init->PLLSAI2ClockOut));
  1872. /* Disable the PLLSAI2 */
  1873. __HAL_RCC_PLLSAI2_DISABLE();
  1874. /* Get Start Tick*/
  1875. tickstart = HAL_GetTick();
  1876. /* Wait till PLLSAI2 is ready to be updated */
  1877. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) != 0U)
  1878. {
  1879. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  1880. {
  1881. status = HAL_TIMEOUT;
  1882. break;
  1883. }
  1884. }
  1885. if(status == HAL_OK)
  1886. {
  1887. #if defined(RCC_PLLSAI2M_DIV_1_16_SUPPORT) && defined(RCC_PLLSAI2Q_DIV_SUPPORT)
  1888. /* Configure the PLLSAI2 Multiplication factor N */
  1889. /* Configure the PLLSAI2 Division factors M, P, Q and R */
  1890. __HAL_RCC_PLLSAI2_CONFIG(PLLSAI2Init->PLLSAI2M, PLLSAI2Init->PLLSAI2N, PLLSAI2Init->PLLSAI2P, PLLSAI2Init->PLLSAI2Q, PLLSAI2Init->PLLSAI2R);
  1891. #elif defined(RCC_PLLSAI2M_DIV_1_16_SUPPORT)
  1892. /* Configure the PLLSAI2 Multiplication factor N */
  1893. /* Configure the PLLSAI2 Division factors M, P and R */
  1894. __HAL_RCC_PLLSAI2_CONFIG(PLLSAI2Init->PLLSAI2M, PLLSAI2Init->PLLSAI2N, PLLSAI2Init->PLLSAI2P, PLLSAI2Init->PLLSAI2R);
  1895. #elif defined(RCC_PLLSAI2Q_DIV_SUPPORT)
  1896. /* Configure the PLLSAI2 Multiplication factor N */
  1897. /* Configure the PLLSAI2 Division factors P, Q and R */
  1898. __HAL_RCC_PLLSAI2_CONFIG(PLLSAI2Init->PLLSAI2N, PLLSAI2Init->PLLSAI2P, PLLSAI2Init->PLLSAI2Q, PLLSAI2Init->PLLSAI2R);
  1899. #else
  1900. /* Configure the PLLSAI2 Multiplication factor N */
  1901. /* Configure the PLLSAI2 Division factors P and R */
  1902. __HAL_RCC_PLLSAI2_CONFIG(PLLSAI2Init->PLLSAI2N, PLLSAI2Init->PLLSAI2P, PLLSAI2Init->PLLSAI2R);
  1903. #endif /* RCC_PLLSAI2M_DIV_1_16_SUPPORT && RCC_PLLSAI2Q_DIV_SUPPORT */
  1904. /* Configure the PLLSAI2 Clock output(s) */
  1905. __HAL_RCC_PLLSAI2CLKOUT_ENABLE(PLLSAI2Init->PLLSAI2ClockOut);
  1906. /* Enable the PLLSAI2 again by setting PLLSAI2ON to 1*/
  1907. __HAL_RCC_PLLSAI2_ENABLE();
  1908. /* Get Start Tick*/
  1909. tickstart = HAL_GetTick();
  1910. /* Wait till PLLSAI2 is ready */
  1911. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) == 0U)
  1912. {
  1913. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  1914. {
  1915. status = HAL_TIMEOUT;
  1916. break;
  1917. }
  1918. }
  1919. }
  1920. return status;
  1921. }
  1922. /**
  1923. * @brief Disable PLLISAI2.
  1924. * @retval HAL status
  1925. */
  1926. HAL_StatusTypeDef HAL_RCCEx_DisablePLLSAI2(void)
  1927. {
  1928. uint32_t tickstart;
  1929. HAL_StatusTypeDef status = HAL_OK;
  1930. /* Disable the PLLSAI2 */
  1931. __HAL_RCC_PLLSAI2_DISABLE();
  1932. /* Get Start Tick*/
  1933. tickstart = HAL_GetTick();
  1934. /* Wait till PLLSAI2 is ready */
  1935. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) != 0U)
  1936. {
  1937. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  1938. {
  1939. status = HAL_TIMEOUT;
  1940. break;
  1941. }
  1942. }
  1943. /* Disable the PLLSAI2 Clock outputs */
  1944. #if defined(RCC_PLLSAI2Q_DIV_SUPPORT)
  1945. __HAL_RCC_PLLSAI2CLKOUT_DISABLE(RCC_PLLSAI2CFGR_PLLSAI2PEN|RCC_PLLSAI2CFGR_PLLSAI2QEN|RCC_PLLSAI2CFGR_PLLSAI2REN);
  1946. #else
  1947. __HAL_RCC_PLLSAI2CLKOUT_DISABLE(RCC_PLLSAI2CFGR_PLLSAI2PEN|RCC_PLLSAI2CFGR_PLLSAI2REN);
  1948. #endif /* RCC_PLLSAI2M_DIV_1_16_SUPPORT && RCC_PLLSAI2Q_DIV_SUPPORT */
  1949. /* Reset PLL source to save power if no PLLs on */
  1950. if(READ_BIT(RCC->CR, (RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY)) == 0U)
  1951. {
  1952. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
  1953. }
  1954. return status;
  1955. }
  1956. #endif /* RCC_PLLSAI2_SUPPORT */
  1957. /**
  1958. * @brief Configure the oscillator clock source for wakeup from Stop and CSS backup clock.
  1959. * @param WakeUpClk Wakeup clock
  1960. * This parameter can be one of the following values:
  1961. * @arg @ref RCC_STOP_WAKEUPCLOCK_MSI MSI oscillator selection
  1962. * @arg @ref RCC_STOP_WAKEUPCLOCK_HSI HSI oscillator selection
  1963. * @note This function shall not be called after the Clock Security System on HSE has been
  1964. * enabled.
  1965. * @retval None
  1966. */
  1967. void HAL_RCCEx_WakeUpStopCLKConfig(uint32_t WakeUpClk)
  1968. {
  1969. assert_param(IS_RCC_STOP_WAKEUPCLOCK(WakeUpClk));
  1970. __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(WakeUpClk);
  1971. }
  1972. /**
  1973. * @brief Configure the MSI range after standby mode.
  1974. * @note After Standby its frequency can be selected between 4 possible values (1, 2, 4 or 8 MHz).
  1975. * @param MSIRange MSI range
  1976. * This parameter can be one of the following values:
  1977. * @arg @ref RCC_MSIRANGE_4 Range 4 around 1 MHz
  1978. * @arg @ref RCC_MSIRANGE_5 Range 5 around 2 MHz
  1979. * @arg @ref RCC_MSIRANGE_6 Range 6 around 4 MHz (reset value)
  1980. * @arg @ref RCC_MSIRANGE_7 Range 7 around 8 MHz
  1981. * @retval None
  1982. */
  1983. void HAL_RCCEx_StandbyMSIRangeConfig(uint32_t MSIRange)
  1984. {
  1985. assert_param(IS_RCC_MSI_STANDBY_CLOCK_RANGE(MSIRange));
  1986. __HAL_RCC_MSI_STANDBY_RANGE_CONFIG(MSIRange);
  1987. }
  1988. /**
  1989. * @brief Enable the LSE Clock Security System.
  1990. * @note Prior to enable the LSE Clock Security System, LSE oscillator is to be enabled
  1991. * with HAL_RCC_OscConfig() and the LSE oscillator clock is to be selected as RTC
  1992. * clock with HAL_RCCEx_PeriphCLKConfig().
  1993. * @retval None
  1994. */
  1995. void HAL_RCCEx_EnableLSECSS(void)
  1996. {
  1997. SET_BIT(RCC->BDCR, RCC_BDCR_LSECSSON);
  1998. }
  1999. /**
  2000. * @brief Disable the LSE Clock Security System.
  2001. * @note LSE Clock Security System can only be disabled after a LSE failure detection.
  2002. * @retval None
  2003. */
  2004. void HAL_RCCEx_DisableLSECSS(void)
  2005. {
  2006. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  2007. /* Disable LSE CSS IT if any */
  2008. __HAL_RCC_DISABLE_IT(RCC_IT_LSECSS);
  2009. }
  2010. /**
  2011. * @brief Enable the LSE Clock Security System Interrupt & corresponding EXTI line.
  2012. * @note LSE Clock Security System Interrupt is mapped on RTC EXTI line 19
  2013. * @retval None
  2014. */
  2015. void HAL_RCCEx_EnableLSECSS_IT(void)
  2016. {
  2017. /* Enable LSE CSS */
  2018. SET_BIT(RCC->BDCR, RCC_BDCR_LSECSSON) ;
  2019. /* Enable LSE CSS IT */
  2020. __HAL_RCC_ENABLE_IT(RCC_IT_LSECSS);
  2021. /* Enable IT on EXTI Line 19 */
  2022. __HAL_RCC_LSECSS_EXTI_ENABLE_IT();
  2023. __HAL_RCC_LSECSS_EXTI_ENABLE_RISING_EDGE();
  2024. }
  2025. /**
  2026. * @brief Handle the RCC LSE Clock Security System interrupt request.
  2027. * @retval None
  2028. */
  2029. void HAL_RCCEx_LSECSS_IRQHandler(void)
  2030. {
  2031. /* Check RCC LSE CSSF flag */
  2032. if(__HAL_RCC_GET_IT(RCC_IT_LSECSS))
  2033. {
  2034. /* RCC LSE Clock Security System interrupt user callback */
  2035. HAL_RCCEx_LSECSS_Callback();
  2036. /* Clear RCC LSE CSS pending bit */
  2037. __HAL_RCC_CLEAR_IT(RCC_IT_LSECSS);
  2038. }
  2039. }
  2040. /**
  2041. * @brief RCCEx LSE Clock Security System interrupt callback.
  2042. * @retval none
  2043. */
  2044. __weak void HAL_RCCEx_LSECSS_Callback(void)
  2045. {
  2046. /* NOTE : This function should not be modified, when the callback is needed,
  2047. the @ref HAL_RCCEx_LSECSS_Callback should be implemented in the user file
  2048. */
  2049. }
  2050. /**
  2051. * @brief Select the Low Speed clock source to output on LSCO pin (PA2).
  2052. * @param LSCOSource specifies the Low Speed clock source to output.
  2053. * This parameter can be one of the following values:
  2054. * @arg @ref RCC_LSCOSOURCE_LSI LSI clock selected as LSCO source
  2055. * @arg @ref RCC_LSCOSOURCE_LSE LSE clock selected as LSCO source
  2056. * @retval None
  2057. */
  2058. void HAL_RCCEx_EnableLSCO(uint32_t LSCOSource)
  2059. {
  2060. GPIO_InitTypeDef GPIO_InitStruct;
  2061. FlagStatus pwrclkchanged = RESET;
  2062. FlagStatus backupchanged = RESET;
  2063. /* Check the parameters */
  2064. assert_param(IS_RCC_LSCOSOURCE(LSCOSource));
  2065. /* LSCO Pin Clock Enable */
  2066. __LSCO_CLK_ENABLE();
  2067. /* Configure the LSCO pin in analog mode */
  2068. GPIO_InitStruct.Pin = LSCO_PIN;
  2069. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  2070. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  2071. GPIO_InitStruct.Pull = GPIO_NOPULL;
  2072. HAL_GPIO_Init(LSCO_GPIO_PORT, &GPIO_InitStruct);
  2073. /* Update LSCOSEL clock source in Backup Domain control register */
  2074. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  2075. {
  2076. __HAL_RCC_PWR_CLK_ENABLE();
  2077. pwrclkchanged = SET;
  2078. }
  2079. if(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  2080. {
  2081. HAL_PWR_EnableBkUpAccess();
  2082. backupchanged = SET;
  2083. }
  2084. MODIFY_REG(RCC->BDCR, RCC_BDCR_LSCOSEL | RCC_BDCR_LSCOEN, LSCOSource | RCC_BDCR_LSCOEN);
  2085. if(backupchanged == SET)
  2086. {
  2087. HAL_PWR_DisableBkUpAccess();
  2088. }
  2089. if(pwrclkchanged == SET)
  2090. {
  2091. __HAL_RCC_PWR_CLK_DISABLE();
  2092. }
  2093. }
  2094. /**
  2095. * @brief Disable the Low Speed clock output.
  2096. * @retval None
  2097. */
  2098. void HAL_RCCEx_DisableLSCO(void)
  2099. {
  2100. FlagStatus pwrclkchanged = RESET;
  2101. FlagStatus backupchanged = RESET;
  2102. /* Update LSCOEN bit in Backup Domain control register */
  2103. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  2104. {
  2105. __HAL_RCC_PWR_CLK_ENABLE();
  2106. pwrclkchanged = SET;
  2107. }
  2108. if(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  2109. {
  2110. /* Enable access to the backup domain */
  2111. HAL_PWR_EnableBkUpAccess();
  2112. backupchanged = SET;
  2113. }
  2114. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSCOEN);
  2115. /* Restore previous configuration */
  2116. if(backupchanged == SET)
  2117. {
  2118. /* Disable access to the backup domain */
  2119. HAL_PWR_DisableBkUpAccess();
  2120. }
  2121. if(pwrclkchanged == SET)
  2122. {
  2123. __HAL_RCC_PWR_CLK_DISABLE();
  2124. }
  2125. }
  2126. /**
  2127. * @brief Enable the PLL-mode of the MSI.
  2128. * @note Prior to enable the PLL-mode of the MSI for automatic hardware
  2129. * calibration LSE oscillator is to be enabled with HAL_RCC_OscConfig().
  2130. * @retval None
  2131. */
  2132. void HAL_RCCEx_EnableMSIPLLMode(void)
  2133. {
  2134. SET_BIT(RCC->CR, RCC_CR_MSIPLLEN) ;
  2135. }
  2136. /**
  2137. * @brief Disable the PLL-mode of the MSI.
  2138. * @note PLL-mode of the MSI is automatically reset when LSE oscillator is disabled.
  2139. * @retval None
  2140. */
  2141. void HAL_RCCEx_DisableMSIPLLMode(void)
  2142. {
  2143. CLEAR_BIT(RCC->CR, RCC_CR_MSIPLLEN) ;
  2144. }
  2145. #if defined (OCTOSPI1) && defined (OCTOSPI2)
  2146. /**
  2147. * @brief Configure OCTOSPI instances DQS delays.
  2148. * @param Delay1 OCTOSPI1 DQS delay
  2149. * @param Delay2 OCTOSPI2 DQS delay
  2150. * @note Delay parameters stand for unitary delays from 0 to 15. Actual delay is Delay1 or Delay2 + 1.
  2151. * @retval None
  2152. */
  2153. void HAL_RCCEx_OCTOSPIDelayConfig(uint32_t Delay1, uint32_t Delay2)
  2154. {
  2155. assert_param(IS_RCC_OCTOSPIDELAY(Delay1));
  2156. assert_param(IS_RCC_OCTOSPIDELAY(Delay2));
  2157. MODIFY_REG(RCC->DLYCFGR, RCC_DLYCFGR_OCTOSPI1_DLY|RCC_DLYCFGR_OCTOSPI2_DLY, (Delay1 | (Delay2 << RCC_DLYCFGR_OCTOSPI2_DLY_Pos))) ;
  2158. }
  2159. #endif /* OCTOSPI1 && OCTOSPI2 */
  2160. /**
  2161. * @}
  2162. */
  2163. #if defined(CRS)
  2164. /** @defgroup RCCEx_Exported_Functions_Group3 Extended Clock Recovery System Control functions
  2165. * @brief Extended Clock Recovery System Control functions
  2166. *
  2167. @verbatim
  2168. ===============================================================================
  2169. ##### Extended Clock Recovery System Control functions #####
  2170. ===============================================================================
  2171. [..]
  2172. For devices with Clock Recovery System feature (CRS), RCC Extension HAL driver can be used as follows:
  2173. (#) In System clock config, HSI48 needs to be enabled
  2174. (#) Enable CRS clock in IP MSP init which will use CRS functions
  2175. (#) Call CRS functions as follows:
  2176. (##) Prepare synchronization configuration necessary for HSI48 calibration
  2177. (+++) Default values can be set for frequency Error Measurement (reload and error limit)
  2178. and also HSI48 oscillator smooth trimming.
  2179. (+++) Macro __HAL_RCC_CRS_RELOADVALUE_CALCULATE can be also used to calculate
  2180. directly reload value with target and sychronization frequencies values
  2181. (##) Call function HAL_RCCEx_CRSConfig which
  2182. (+++) Resets CRS registers to their default values.
  2183. (+++) Configures CRS registers with synchronization configuration
  2184. (+++) Enables automatic calibration and frequency error counter feature
  2185. Note: When using USB LPM (Link Power Management) and the device is in Sleep mode, the
  2186. periodic USB SOF will not be generated by the host. No SYNC signal will therefore be
  2187. provided to the CRS to calibrate the HSI48 on the run. To guarantee the required clock
  2188. precision after waking up from Sleep mode, the LSE or reference clock on the GPIOs
  2189. should be used as SYNC signal.
  2190. (##) A polling function is provided to wait for complete synchronization
  2191. (+++) Call function HAL_RCCEx_CRSWaitSynchronization()
  2192. (+++) According to CRS status, user can decide to adjust again the calibration or continue
  2193. application if synchronization is OK
  2194. (#) User can retrieve information related to synchronization in calling function
  2195. HAL_RCCEx_CRSGetSynchronizationInfo()
  2196. (#) Regarding synchronization status and synchronization information, user can try a new calibration
  2197. in changing synchronization configuration and call again HAL_RCCEx_CRSConfig.
  2198. Note: When the SYNC event is detected during the downcounting phase (before reaching the zero value),
  2199. it means that the actual frequency is lower than the target (and so, that the TRIM value should be
  2200. incremented), while when it is detected during the upcounting phase it means that the actual frequency
  2201. is higher (and that the TRIM value should be decremented).
  2202. (#) In interrupt mode, user can resort to the available macros (__HAL_RCC_CRS_XXX_IT). Interrupts will go
  2203. through CRS Handler (CRS_IRQn/CRS_IRQHandler)
  2204. (++) Call function HAL_RCCEx_CRSConfig()
  2205. (++) Enable CRS_IRQn (thanks to NVIC functions)
  2206. (++) Enable CRS interrupt (__HAL_RCC_CRS_ENABLE_IT)
  2207. (++) Implement CRS status management in the following user callbacks called from
  2208. HAL_RCCEx_CRS_IRQHandler():
  2209. (+++) HAL_RCCEx_CRS_SyncOkCallback()
  2210. (+++) HAL_RCCEx_CRS_SyncWarnCallback()
  2211. (+++) HAL_RCCEx_CRS_ExpectedSyncCallback()
  2212. (+++) HAL_RCCEx_CRS_ErrorCallback()
  2213. (#) To force a SYNC EVENT, user can use the function HAL_RCCEx_CRSSoftwareSynchronizationGenerate().
  2214. This function can be called before calling HAL_RCCEx_CRSConfig (for instance in Systick handler)
  2215. @endverbatim
  2216. * @{
  2217. */
  2218. /**
  2219. * @brief Start automatic synchronization for polling mode
  2220. * @param pInit Pointer on RCC_CRSInitTypeDef structure
  2221. * @retval None
  2222. */
  2223. void HAL_RCCEx_CRSConfig(RCC_CRSInitTypeDef *pInit)
  2224. {
  2225. uint32_t value; /* no init needed */
  2226. /* Check the parameters */
  2227. assert_param(IS_RCC_CRS_SYNC_DIV(pInit->Prescaler));
  2228. assert_param(IS_RCC_CRS_SYNC_SOURCE(pInit->Source));
  2229. assert_param(IS_RCC_CRS_SYNC_POLARITY(pInit->Polarity));
  2230. assert_param(IS_RCC_CRS_RELOADVALUE(pInit->ReloadValue));
  2231. assert_param(IS_RCC_CRS_ERRORLIMIT(pInit->ErrorLimitValue));
  2232. assert_param(IS_RCC_CRS_HSI48CALIBRATION(pInit->HSI48CalibrationValue));
  2233. /* CONFIGURATION */
  2234. /* Before configuration, reset CRS registers to their default values*/
  2235. __HAL_RCC_CRS_FORCE_RESET();
  2236. __HAL_RCC_CRS_RELEASE_RESET();
  2237. /* Set the SYNCDIV[2:0] bits according to Prescaler value */
  2238. /* Set the SYNCSRC[1:0] bits according to Source value */
  2239. /* Set the SYNCSPOL bit according to Polarity value */
  2240. value = (pInit->Prescaler | pInit->Source | pInit->Polarity);
  2241. /* Set the RELOAD[15:0] bits according to ReloadValue value */
  2242. value |= pInit->ReloadValue;
  2243. /* Set the FELIM[7:0] bits according to ErrorLimitValue value */
  2244. value |= (pInit->ErrorLimitValue << CRS_CFGR_FELIM_Pos);
  2245. WRITE_REG(CRS->CFGR, value);
  2246. /* Adjust HSI48 oscillator smooth trimming */
  2247. /* Set the TRIM[6:0] bits for STM32L412xx/L422xx or TRIM[5:0] bits otherwise
  2248. according to RCC_CRS_HSI48CalibrationValue value */
  2249. MODIFY_REG(CRS->CR, CRS_CR_TRIM, (pInit->HSI48CalibrationValue << CRS_CR_TRIM_Pos));
  2250. /* START AUTOMATIC SYNCHRONIZATION*/
  2251. /* Enable Automatic trimming & Frequency error counter */
  2252. SET_BIT(CRS->CR, CRS_CR_AUTOTRIMEN | CRS_CR_CEN);
  2253. }
  2254. /**
  2255. * @brief Generate the software synchronization event
  2256. * @retval None
  2257. */
  2258. void HAL_RCCEx_CRSSoftwareSynchronizationGenerate(void)
  2259. {
  2260. SET_BIT(CRS->CR, CRS_CR_SWSYNC);
  2261. }
  2262. /**
  2263. * @brief Return synchronization info
  2264. * @param pSynchroInfo Pointer on RCC_CRSSynchroInfoTypeDef structure
  2265. * @retval None
  2266. */
  2267. void HAL_RCCEx_CRSGetSynchronizationInfo(RCC_CRSSynchroInfoTypeDef *pSynchroInfo)
  2268. {
  2269. /* Check the parameter */
  2270. assert_param(pSynchroInfo != (void *)NULL);
  2271. /* Get the reload value */
  2272. pSynchroInfo->ReloadValue = (READ_BIT(CRS->CFGR, CRS_CFGR_RELOAD));
  2273. /* Get HSI48 oscillator smooth trimming */
  2274. pSynchroInfo->HSI48CalibrationValue = (READ_BIT(CRS->CR, CRS_CR_TRIM) >> CRS_CR_TRIM_Pos);
  2275. /* Get Frequency error capture */
  2276. pSynchroInfo->FreqErrorCapture = (READ_BIT(CRS->ISR, CRS_ISR_FECAP) >> CRS_ISR_FECAP_Pos);
  2277. /* Get Frequency error direction */
  2278. pSynchroInfo->FreqErrorDirection = (READ_BIT(CRS->ISR, CRS_ISR_FEDIR));
  2279. }
  2280. /**
  2281. * @brief Wait for CRS Synchronization status.
  2282. * @param Timeout Duration of the timeout
  2283. * @note Timeout is based on the maximum time to receive a SYNC event based on synchronization
  2284. * frequency.
  2285. * @note If Timeout set to HAL_MAX_DELAY, HAL_TIMEOUT will be never returned.
  2286. * @retval Combination of Synchronization status
  2287. * This parameter can be a combination of the following values:
  2288. * @arg @ref RCC_CRS_TIMEOUT
  2289. * @arg @ref RCC_CRS_SYNCOK
  2290. * @arg @ref RCC_CRS_SYNCWARN
  2291. * @arg @ref RCC_CRS_SYNCERR
  2292. * @arg @ref RCC_CRS_SYNCMISS
  2293. * @arg @ref RCC_CRS_TRIMOVF
  2294. */
  2295. uint32_t HAL_RCCEx_CRSWaitSynchronization(uint32_t Timeout)
  2296. {
  2297. uint32_t crsstatus = RCC_CRS_NONE;
  2298. uint32_t tickstart;
  2299. /* Get timeout */
  2300. tickstart = HAL_GetTick();
  2301. /* Wait for CRS flag or timeout detection */
  2302. do
  2303. {
  2304. if(Timeout != HAL_MAX_DELAY)
  2305. {
  2306. if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  2307. {
  2308. crsstatus = RCC_CRS_TIMEOUT;
  2309. }
  2310. }
  2311. /* Check CRS SYNCOK flag */
  2312. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCOK))
  2313. {
  2314. /* CRS SYNC event OK */
  2315. crsstatus |= RCC_CRS_SYNCOK;
  2316. /* Clear CRS SYNC event OK bit */
  2317. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCOK);
  2318. }
  2319. /* Check CRS SYNCWARN flag */
  2320. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCWARN))
  2321. {
  2322. /* CRS SYNC warning */
  2323. crsstatus |= RCC_CRS_SYNCWARN;
  2324. /* Clear CRS SYNCWARN bit */
  2325. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCWARN);
  2326. }
  2327. /* Check CRS TRIM overflow flag */
  2328. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_TRIMOVF))
  2329. {
  2330. /* CRS SYNC Error */
  2331. crsstatus |= RCC_CRS_TRIMOVF;
  2332. /* Clear CRS Error bit */
  2333. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_TRIMOVF);
  2334. }
  2335. /* Check CRS Error flag */
  2336. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCERR))
  2337. {
  2338. /* CRS SYNC Error */
  2339. crsstatus |= RCC_CRS_SYNCERR;
  2340. /* Clear CRS Error bit */
  2341. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCERR);
  2342. }
  2343. /* Check CRS SYNC Missed flag */
  2344. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCMISS))
  2345. {
  2346. /* CRS SYNC Missed */
  2347. crsstatus |= RCC_CRS_SYNCMISS;
  2348. /* Clear CRS SYNC Missed bit */
  2349. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCMISS);
  2350. }
  2351. /* Check CRS Expected SYNC flag */
  2352. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_ESYNC))
  2353. {
  2354. /* frequency error counter reached a zero value */
  2355. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_ESYNC);
  2356. }
  2357. } while(RCC_CRS_NONE == crsstatus);
  2358. return crsstatus;
  2359. }
  2360. /**
  2361. * @brief Handle the Clock Recovery System interrupt request.
  2362. * @retval None
  2363. */
  2364. void HAL_RCCEx_CRS_IRQHandler(void)
  2365. {
  2366. uint32_t crserror = RCC_CRS_NONE;
  2367. /* Get current IT flags and IT sources values */
  2368. uint32_t itflags = READ_REG(CRS->ISR);
  2369. uint32_t itsources = READ_REG(CRS->CR);
  2370. /* Check CRS SYNCOK flag */
  2371. if(((itflags & RCC_CRS_FLAG_SYNCOK) != 0U) && ((itsources & RCC_CRS_IT_SYNCOK) != 0U))
  2372. {
  2373. /* Clear CRS SYNC event OK flag */
  2374. WRITE_REG(CRS->ICR, CRS_ICR_SYNCOKC);
  2375. /* user callback */
  2376. HAL_RCCEx_CRS_SyncOkCallback();
  2377. }
  2378. /* Check CRS SYNCWARN flag */
  2379. else if(((itflags & RCC_CRS_FLAG_SYNCWARN) != 0U) && ((itsources & RCC_CRS_IT_SYNCWARN) != 0U))
  2380. {
  2381. /* Clear CRS SYNCWARN flag */
  2382. WRITE_REG(CRS->ICR, CRS_ICR_SYNCWARNC);
  2383. /* user callback */
  2384. HAL_RCCEx_CRS_SyncWarnCallback();
  2385. }
  2386. /* Check CRS Expected SYNC flag */
  2387. else if(((itflags & RCC_CRS_FLAG_ESYNC) != 0U) && ((itsources & RCC_CRS_IT_ESYNC) != 0U))
  2388. {
  2389. /* frequency error counter reached a zero value */
  2390. WRITE_REG(CRS->ICR, CRS_ICR_ESYNCC);
  2391. /* user callback */
  2392. HAL_RCCEx_CRS_ExpectedSyncCallback();
  2393. }
  2394. /* Check CRS Error flags */
  2395. else
  2396. {
  2397. if(((itflags & RCC_CRS_FLAG_ERR) != 0U) && ((itsources & RCC_CRS_IT_ERR) != 0U))
  2398. {
  2399. if((itflags & RCC_CRS_FLAG_SYNCERR) != 0U)
  2400. {
  2401. crserror |= RCC_CRS_SYNCERR;
  2402. }
  2403. if((itflags & RCC_CRS_FLAG_SYNCMISS) != 0U)
  2404. {
  2405. crserror |= RCC_CRS_SYNCMISS;
  2406. }
  2407. if((itflags & RCC_CRS_FLAG_TRIMOVF) != 0U)
  2408. {
  2409. crserror |= RCC_CRS_TRIMOVF;
  2410. }
  2411. /* Clear CRS Error flags */
  2412. WRITE_REG(CRS->ICR, CRS_ICR_ERRC);
  2413. /* user error callback */
  2414. HAL_RCCEx_CRS_ErrorCallback(crserror);
  2415. }
  2416. }
  2417. }
  2418. /**
  2419. * @brief RCCEx Clock Recovery System SYNCOK interrupt callback.
  2420. * @retval none
  2421. */
  2422. __weak void HAL_RCCEx_CRS_SyncOkCallback(void)
  2423. {
  2424. /* NOTE : This function should not be modified, when the callback is needed,
  2425. the @ref HAL_RCCEx_CRS_SyncOkCallback should be implemented in the user file
  2426. */
  2427. }
  2428. /**
  2429. * @brief RCCEx Clock Recovery System SYNCWARN interrupt callback.
  2430. * @retval none
  2431. */
  2432. __weak void HAL_RCCEx_CRS_SyncWarnCallback(void)
  2433. {
  2434. /* NOTE : This function should not be modified, when the callback is needed,
  2435. the @ref HAL_RCCEx_CRS_SyncWarnCallback should be implemented in the user file
  2436. */
  2437. }
  2438. /**
  2439. * @brief RCCEx Clock Recovery System Expected SYNC interrupt callback.
  2440. * @retval none
  2441. */
  2442. __weak void HAL_RCCEx_CRS_ExpectedSyncCallback(void)
  2443. {
  2444. /* NOTE : This function should not be modified, when the callback is needed,
  2445. the @ref HAL_RCCEx_CRS_ExpectedSyncCallback should be implemented in the user file
  2446. */
  2447. }
  2448. /**
  2449. * @brief RCCEx Clock Recovery System Error interrupt callback.
  2450. * @param Error Combination of Error status.
  2451. * This parameter can be a combination of the following values:
  2452. * @arg @ref RCC_CRS_SYNCERR
  2453. * @arg @ref RCC_CRS_SYNCMISS
  2454. * @arg @ref RCC_CRS_TRIMOVF
  2455. * @retval none
  2456. */
  2457. __weak void HAL_RCCEx_CRS_ErrorCallback(uint32_t Error)
  2458. {
  2459. /* Prevent unused argument(s) compilation warning */
  2460. UNUSED(Error);
  2461. /* NOTE : This function should not be modified, when the callback is needed,
  2462. the @ref HAL_RCCEx_CRS_ErrorCallback should be implemented in the user file
  2463. */
  2464. }
  2465. /**
  2466. * @}
  2467. */
  2468. #endif /* CRS */
  2469. /**
  2470. * @}
  2471. */
  2472. /** @addtogroup RCCEx_Private_Functions
  2473. * @{
  2474. */
  2475. #if defined(RCC_PLLSAI1_SUPPORT)
  2476. /**
  2477. * @brief Configure the parameters N & P & optionally M of PLLSAI1 and enable PLLSAI1 output clock(s).
  2478. * @param PllSai1 pointer to an RCC_PLLSAI1InitTypeDef structure that
  2479. * contains the configuration parameters N & P & optionally M as well as PLLSAI1 output clock(s)
  2480. * @param Divider divider parameter to be updated
  2481. *
  2482. * @note PLLSAI1 is temporary disable to apply new parameters
  2483. *
  2484. * @retval HAL status
  2485. */
  2486. static HAL_StatusTypeDef RCCEx_PLLSAI1_Config(RCC_PLLSAI1InitTypeDef *PllSai1, uint32_t Divider)
  2487. {
  2488. uint32_t tickstart;
  2489. HAL_StatusTypeDef status = HAL_OK;
  2490. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  2491. /* P, Q and R dividers are verified in each specific divider case below */
  2492. assert_param(IS_RCC_PLLSAI1SOURCE(PllSai1->PLLSAI1Source));
  2493. assert_param(IS_RCC_PLLSAI1M_VALUE(PllSai1->PLLSAI1M));
  2494. assert_param(IS_RCC_PLLSAI1N_VALUE(PllSai1->PLLSAI1N));
  2495. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PllSai1->PLLSAI1ClockOut));
  2496. /* Check that PLLSAI1 clock source and divider M can be applied */
  2497. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_NONE)
  2498. {
  2499. /* PLL clock source and divider M already set, check that no request for change */
  2500. if((__HAL_RCC_GET_PLL_OSCSOURCE() != PllSai1->PLLSAI1Source)
  2501. ||
  2502. (PllSai1->PLLSAI1Source == RCC_PLLSOURCE_NONE)
  2503. #if !defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  2504. ||
  2505. (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U) != PllSai1->PLLSAI1M)
  2506. #endif
  2507. )
  2508. {
  2509. status = HAL_ERROR;
  2510. }
  2511. }
  2512. else
  2513. {
  2514. /* Check PLLSAI1 clock source availability */
  2515. switch(PllSai1->PLLSAI1Source)
  2516. {
  2517. case RCC_PLLSOURCE_MSI:
  2518. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_MSIRDY))
  2519. {
  2520. status = HAL_ERROR;
  2521. }
  2522. break;
  2523. case RCC_PLLSOURCE_HSI:
  2524. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSIRDY))
  2525. {
  2526. status = HAL_ERROR;
  2527. }
  2528. break;
  2529. case RCC_PLLSOURCE_HSE:
  2530. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSERDY))
  2531. {
  2532. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSEBYP))
  2533. {
  2534. status = HAL_ERROR;
  2535. }
  2536. }
  2537. break;
  2538. default:
  2539. status = HAL_ERROR;
  2540. break;
  2541. }
  2542. if(status == HAL_OK)
  2543. {
  2544. #if defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  2545. /* Set PLLSAI1 clock source */
  2546. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, PllSai1->PLLSAI1Source);
  2547. #else
  2548. /* Set PLLSAI1 clock source and divider M */
  2549. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLM, PllSai1->PLLSAI1Source | (PllSai1->PLLSAI1M - 1U) << RCC_PLLCFGR_PLLM_Pos);
  2550. #endif
  2551. }
  2552. }
  2553. if(status == HAL_OK)
  2554. {
  2555. /* Disable the PLLSAI1 */
  2556. __HAL_RCC_PLLSAI1_DISABLE();
  2557. /* Get Start Tick*/
  2558. tickstart = HAL_GetTick();
  2559. /* Wait till PLLSAI1 is ready to be updated */
  2560. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) != 0U)
  2561. {
  2562. if((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  2563. {
  2564. status = HAL_TIMEOUT;
  2565. break;
  2566. }
  2567. }
  2568. if(status == HAL_OK)
  2569. {
  2570. if(Divider == DIVIDER_P_UPDATE)
  2571. {
  2572. assert_param(IS_RCC_PLLSAI1P_VALUE(PllSai1->PLLSAI1P));
  2573. #if defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  2574. /* Configure the PLLSAI1 Division factor M, P and Multiplication factor N*/
  2575. #if defined(RCC_PLLSAI1P_DIV_2_31_SUPPORT)
  2576. MODIFY_REG(RCC->PLLSAI1CFGR,
  2577. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1PDIV | RCC_PLLSAI1CFGR_PLLSAI1M,
  2578. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2579. (PllSai1->PLLSAI1P << RCC_PLLSAI1CFGR_PLLSAI1PDIV_Pos) |
  2580. ((PllSai1->PLLSAI1M - 1U) << RCC_PLLSAI1CFGR_PLLSAI1M_Pos));
  2581. #else
  2582. MODIFY_REG(RCC->PLLSAI1CFGR,
  2583. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1P | RCC_PLLSAI1CFGR_PLLSAI1M,
  2584. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2585. ((PllSai1->PLLSAI1P >> 4U) << RCC_PLLSAI1CFGR_PLLSAI1P_Pos) |
  2586. ((PllSai1->PLLSAI1M - 1U) << RCC_PLLSAI1CFGR_PLLSAI1M_Pos));
  2587. #endif /* RCC_PLLSAI1P_DIV_2_31_SUPPORT */
  2588. #else
  2589. /* Configure the PLLSAI1 Division factor P and Multiplication factor N*/
  2590. #if defined(RCC_PLLSAI1P_DIV_2_31_SUPPORT)
  2591. MODIFY_REG(RCC->PLLSAI1CFGR,
  2592. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1PDIV,
  2593. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2594. (PllSai1->PLLSAI1P << RCC_PLLSAI1CFGR_PLLSAI1PDIV_Pos));
  2595. #else
  2596. MODIFY_REG(RCC->PLLSAI1CFGR,
  2597. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1P,
  2598. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2599. ((PllSai1->PLLSAI1P >> 4U) << RCC_PLLSAI1CFGR_PLLSAI1P_Pos));
  2600. #endif /* RCC_PLLSAI1P_DIV_2_31_SUPPORT */
  2601. #endif /* RCC_PLLSAI1M_DIV_1_16_SUPPORT */
  2602. }
  2603. else if(Divider == DIVIDER_Q_UPDATE)
  2604. {
  2605. assert_param(IS_RCC_PLLSAI1Q_VALUE(PllSai1->PLLSAI1Q));
  2606. #if defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  2607. /* Configure the PLLSAI1 Division factor M, Q and Multiplication factor N*/
  2608. MODIFY_REG(RCC->PLLSAI1CFGR,
  2609. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1Q | RCC_PLLSAI1CFGR_PLLSAI1M,
  2610. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2611. (((PllSai1->PLLSAI1Q >> 1U) - 1U) << RCC_PLLSAI1CFGR_PLLSAI1Q_Pos) |
  2612. ((PllSai1->PLLSAI1M - 1U) << RCC_PLLSAI1CFGR_PLLSAI1M_Pos));
  2613. #else
  2614. /* Configure the PLLSAI1 Division factor Q and Multiplication factor N*/
  2615. MODIFY_REG(RCC->PLLSAI1CFGR,
  2616. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1Q,
  2617. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2618. (((PllSai1->PLLSAI1Q >> 1U) - 1U) << RCC_PLLSAI1CFGR_PLLSAI1Q_Pos));
  2619. #endif /* RCC_PLLSAI1M_DIV_1_16_SUPPORT */
  2620. }
  2621. else
  2622. {
  2623. assert_param(IS_RCC_PLLSAI1R_VALUE(PllSai1->PLLSAI1R));
  2624. #if defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  2625. /* Configure the PLLSAI1 Division factor M, R and Multiplication factor N*/
  2626. MODIFY_REG(RCC->PLLSAI1CFGR,
  2627. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1R | RCC_PLLSAI1CFGR_PLLSAI1M,
  2628. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2629. (((PllSai1->PLLSAI1R >> 1U) - 1U) << RCC_PLLSAI1CFGR_PLLSAI1R_Pos) |
  2630. ((PllSai1->PLLSAI1M - 1U) << RCC_PLLSAI1CFGR_PLLSAI1M_Pos));
  2631. #else
  2632. /* Configure the PLLSAI1 Division factor R and Multiplication factor N*/
  2633. MODIFY_REG(RCC->PLLSAI1CFGR,
  2634. RCC_PLLSAI1CFGR_PLLSAI1N | RCC_PLLSAI1CFGR_PLLSAI1R,
  2635. (PllSai1->PLLSAI1N << RCC_PLLSAI1CFGR_PLLSAI1N_Pos) |
  2636. (((PllSai1->PLLSAI1R >> 1U) - 1U) << RCC_PLLSAI1CFGR_PLLSAI1R_Pos));
  2637. #endif /* RCC_PLLSAI1M_DIV_1_16_SUPPORT */
  2638. }
  2639. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  2640. __HAL_RCC_PLLSAI1_ENABLE();
  2641. /* Get Start Tick*/
  2642. tickstart = HAL_GetTick();
  2643. /* Wait till PLLSAI1 is ready */
  2644. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) == 0U)
  2645. {
  2646. if((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  2647. {
  2648. status = HAL_TIMEOUT;
  2649. break;
  2650. }
  2651. }
  2652. if(status == HAL_OK)
  2653. {
  2654. /* Configure the PLLSAI1 Clock output(s) */
  2655. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PllSai1->PLLSAI1ClockOut);
  2656. }
  2657. }
  2658. }
  2659. return status;
  2660. }
  2661. #endif /* RCC_PLLSAI1_SUPPORT */
  2662. #if defined(RCC_PLLSAI2_SUPPORT)
  2663. /**
  2664. * @brief Configure the parameters N & P & optionally M of PLLSAI2 and enable PLLSAI2 output clock(s).
  2665. * @param PllSai2 pointer to an RCC_PLLSAI2InitTypeDef structure that
  2666. * contains the configuration parameters N & P & optionally M as well as PLLSAI2 output clock(s)
  2667. * @param Divider divider parameter to be updated
  2668. *
  2669. * @note PLLSAI2 is temporary disable to apply new parameters
  2670. *
  2671. * @retval HAL status
  2672. */
  2673. static HAL_StatusTypeDef RCCEx_PLLSAI2_Config(RCC_PLLSAI2InitTypeDef *PllSai2, uint32_t Divider)
  2674. {
  2675. uint32_t tickstart;
  2676. HAL_StatusTypeDef status = HAL_OK;
  2677. /* check for PLLSAI2 Parameters used to output PLLSAI2CLK */
  2678. /* P, Q and R dividers are verified in each specific divider case below */
  2679. assert_param(IS_RCC_PLLSAI2SOURCE(PllSai2->PLLSAI2Source));
  2680. assert_param(IS_RCC_PLLSAI2M_VALUE(PllSai2->PLLSAI2M));
  2681. assert_param(IS_RCC_PLLSAI2N_VALUE(PllSai2->PLLSAI2N));
  2682. assert_param(IS_RCC_PLLSAI2CLOCKOUT_VALUE(PllSai2->PLLSAI2ClockOut));
  2683. /* Check that PLLSAI2 clock source and divider M can be applied */
  2684. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_NONE)
  2685. {
  2686. /* PLL clock source and divider M already set, check that no request for change */
  2687. if((__HAL_RCC_GET_PLL_OSCSOURCE() != PllSai2->PLLSAI2Source)
  2688. ||
  2689. (PllSai2->PLLSAI2Source == RCC_PLLSOURCE_NONE)
  2690. #if !defined(RCC_PLLSAI2M_DIV_1_16_SUPPORT)
  2691. ||
  2692. (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U) != PllSai2->PLLSAI2M)
  2693. #endif
  2694. )
  2695. {
  2696. status = HAL_ERROR;
  2697. }
  2698. }
  2699. else
  2700. {
  2701. /* Check PLLSAI2 clock source availability */
  2702. switch(PllSai2->PLLSAI2Source)
  2703. {
  2704. case RCC_PLLSOURCE_MSI:
  2705. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_MSIRDY))
  2706. {
  2707. status = HAL_ERROR;
  2708. }
  2709. break;
  2710. case RCC_PLLSOURCE_HSI:
  2711. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSIRDY))
  2712. {
  2713. status = HAL_ERROR;
  2714. }
  2715. break;
  2716. case RCC_PLLSOURCE_HSE:
  2717. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSERDY))
  2718. {
  2719. if(HAL_IS_BIT_CLR(RCC->CR, RCC_CR_HSEBYP))
  2720. {
  2721. status = HAL_ERROR;
  2722. }
  2723. }
  2724. break;
  2725. default:
  2726. status = HAL_ERROR;
  2727. break;
  2728. }
  2729. if(status == HAL_OK)
  2730. {
  2731. #if defined(RCC_PLLSAI2M_DIV_1_16_SUPPORT)
  2732. /* Set PLLSAI2 clock source */
  2733. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, PllSai2->PLLSAI2Source);
  2734. #else
  2735. /* Set PLLSAI2 clock source and divider M */
  2736. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLM, PllSai2->PLLSAI2Source | (PllSai2->PLLSAI2M - 1U) << RCC_PLLCFGR_PLLM_Pos);
  2737. #endif
  2738. }
  2739. }
  2740. if(status == HAL_OK)
  2741. {
  2742. /* Disable the PLLSAI2 */
  2743. __HAL_RCC_PLLSAI2_DISABLE();
  2744. /* Get Start Tick*/
  2745. tickstart = HAL_GetTick();
  2746. /* Wait till PLLSAI2 is ready to be updated */
  2747. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) != 0U)
  2748. {
  2749. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  2750. {
  2751. status = HAL_TIMEOUT;
  2752. break;
  2753. }
  2754. }
  2755. if(status == HAL_OK)
  2756. {
  2757. if(Divider == DIVIDER_P_UPDATE)
  2758. {
  2759. assert_param(IS_RCC_PLLSAI2P_VALUE(PllSai2->PLLSAI2P));
  2760. #if defined(RCC_PLLSAI2M_DIV_1_16_SUPPORT)
  2761. /* Configure the PLLSAI2 Division factor M, P and Multiplication factor N*/
  2762. #if defined(RCC_PLLSAI2P_DIV_2_31_SUPPORT)
  2763. MODIFY_REG(RCC->PLLSAI2CFGR,
  2764. RCC_PLLSAI2CFGR_PLLSAI2N | RCC_PLLSAI2CFGR_PLLSAI2PDIV | RCC_PLLSAI2CFGR_PLLSAI2M,
  2765. (PllSai2->PLLSAI2N << RCC_PLLSAI2CFGR_PLLSAI2N_Pos) |
  2766. (PllSai2->PLLSAI2P << RCC_PLLSAI2CFGR_PLLSAI2PDIV_Pos) |
  2767. ((PllSai2->PLLSAI2M - 1U) << RCC_PLLSAI2CFGR_PLLSAI2M_Pos));
  2768. #else
  2769. MODIFY_REG(RCC->PLLSAI2CFGR,
  2770. RCC_PLLSAI2CFGR_PLLSAI2N | RCC_PLLSAI2CFGR_PLLSAI2P | RCC_PLLSAI2CFGR_PLLSAI2M,
  2771. (PllSai2->PLLSAI2N << RCC_PLLSAI2CFGR_PLLSAI2N_Pos) |
  2772. ((PllSai2->PLLSAI2P >> 4U) << RCC_PLLSAI2CFGR_PLLSAI2P_Pos) |
  2773. ((PllSai2->PLLSAI2M - 1U) << RCC_PLLSAI2CFGR_PLLSAI2M_Pos));
  2774. #endif /* RCC_PLLSAI2P_DIV_2_31_SUPPORT */
  2775. #else
  2776. /* Configure the PLLSAI2 Division factor P and Multiplication factor N*/
  2777. #if defined(RCC_PLLSAI2P_DIV_2_31_SUPPORT)
  2778. MODIFY_REG(RCC->PLLSAI2CFGR,
  2779. RCC_PLLSAI2CFGR_PLLSAI2N | RCC_PLLSAI2CFGR_PLLSAI2PDIV,
  2780. (PllSai2->PLLSAI2N << RCC_PLLSAI2CFGR_PLLSAI2N_Pos) |
  2781. (PllSai2->PLLSAI2P << RCC_PLLSAI2CFGR_PLLSAI2PDIV_Pos));
  2782. #else
  2783. MODIFY_REG(RCC->PLLSAI2CFGR,
  2784. RCC_PLLSAI2CFGR_PLLSAI2N | RCC_PLLSAI2CFGR_PLLSAI2P,
  2785. (PllSai2->PLLSAI2N << RCC_PLLSAI2CFGR_PLLSAI2N_Pos) |
  2786. ((PllSai2->PLLSAI2P >> 4U) << RCC_PLLSAI2CFGR_PLLSAI2P_Pos));
  2787. #endif /* RCC_PLLSAI2P_DIV_2_31_SUPPORT */
  2788. #endif /* RCC_PLLSAI2M_DIV_1_16_SUPPORT */
  2789. }
  2790. #if defined(RCC_PLLSAI2Q_DIV_SUPPORT)
  2791. else if(Divider == DIVIDER_Q_UPDATE)
  2792. {
  2793. assert_param(IS_RCC_PLLSAI2Q_VALUE(PllSai2->PLLSAI2Q));
  2794. #if defined(RCC_PLLSAI2M_DIV_1_16_SUPPORT)
  2795. /* Configure the PLLSAI2 Division factor M, Q and Multiplication factor N*/
  2796. MODIFY_REG(RCC->PLLSAI2CFGR,
  2797. RCC_PLLSAI2CFGR_PLLSAI2N | RCC_PLLSAI2CFGR_PLLSAI2Q | RCC_PLLSAI2CFGR_PLLSAI2M,
  2798. (PllSai2->PLLSAI2N << RCC_PLLSAI2CFGR_PLLSAI2N_Pos) |
  2799. (((PllSai2->PLLSAI2Q >> 1U) - 1U) << RCC_PLLSAI2CFGR_PLLSAI2Q_Pos) |
  2800. ((PllSai2->PLLSAI2M - 1U) << RCC_PLLSAI2CFGR_PLLSAI2M_Pos));
  2801. #else
  2802. /* Configure the PLLSAI2 Division factor Q and Multiplication factor N*/
  2803. MODIFY_REG(RCC->PLLSAI2CFGR,
  2804. RCC_PLLSAI2CFGR_PLLSAI2N | RCC_PLLSAI2CFGR_PLLSAI2Q,
  2805. (PllSai2->PLLSAI2N << RCC_PLLSAI2CFGR_PLLSAI2N_Pos) |
  2806. (((PllSai2->PLLSAI2Q >> 1U) - 1U) << RCC_PLLSAI2CFGR_PLLSAI2Q_Pos));
  2807. #endif /* RCC_PLLSAI2M_DIV_1_16_SUPPORT */
  2808. }
  2809. #endif /* RCC_PLLSAI2Q_DIV_SUPPORT */
  2810. else
  2811. {
  2812. assert_param(IS_RCC_PLLSAI2R_VALUE(PllSai2->PLLSAI2R));
  2813. #if defined(RCC_PLLSAI2M_DIV_1_16_SUPPORT)
  2814. /* Configure the PLLSAI2 Division factor M, R and Multiplication factor N*/
  2815. MODIFY_REG(RCC->PLLSAI2CFGR,
  2816. RCC_PLLSAI2CFGR_PLLSAI2N | RCC_PLLSAI2CFGR_PLLSAI2R | RCC_PLLSAI2CFGR_PLLSAI2M,
  2817. (PllSai2->PLLSAI2N << RCC_PLLSAI2CFGR_PLLSAI2N_Pos) |
  2818. (((PllSai2->PLLSAI2R >> 1U) - 1U) << RCC_PLLSAI2CFGR_PLLSAI2R_Pos) |
  2819. ((PllSai2->PLLSAI2M - 1U) << RCC_PLLSAI2CFGR_PLLSAI2M_Pos));
  2820. #else
  2821. /* Configure the PLLSAI2 Division factor R and Multiplication factor N*/
  2822. MODIFY_REG(RCC->PLLSAI2CFGR,
  2823. RCC_PLLSAI2CFGR_PLLSAI2N | RCC_PLLSAI2CFGR_PLLSAI2R,
  2824. (PllSai2->PLLSAI2N << RCC_PLLSAI2CFGR_PLLSAI2N_Pos) |
  2825. (((PllSai2->PLLSAI2R >> 1U) - 1U) << RCC_PLLSAI2CFGR_PLLSAI2R_Pos));
  2826. #endif /* RCC_PLLSAI2M_DIV_1_16_SUPPORT */
  2827. }
  2828. /* Enable the PLLSAI2 again by setting PLLSAI2ON to 1*/
  2829. __HAL_RCC_PLLSAI2_ENABLE();
  2830. /* Get Start Tick*/
  2831. tickstart = HAL_GetTick();
  2832. /* Wait till PLLSAI2 is ready */
  2833. while(READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) == 0U)
  2834. {
  2835. if((HAL_GetTick() - tickstart) > PLLSAI2_TIMEOUT_VALUE)
  2836. {
  2837. status = HAL_TIMEOUT;
  2838. break;
  2839. }
  2840. }
  2841. if(status == HAL_OK)
  2842. {
  2843. /* Configure the PLLSAI2 Clock output(s) */
  2844. __HAL_RCC_PLLSAI2CLKOUT_ENABLE(PllSai2->PLLSAI2ClockOut);
  2845. }
  2846. }
  2847. }
  2848. return status;
  2849. }
  2850. #endif /* RCC_PLLSAI2_SUPPORT */
  2851. #if defined(SAI1)
  2852. static uint32_t RCCEx_GetSAIxPeriphCLKFreq(uint32_t PeriphClk, uint32_t InputFrequency)
  2853. {
  2854. uint32_t frequency = 0U;
  2855. uint32_t srcclk = 0U;
  2856. uint32_t pllvco, plln; /* no init needed */
  2857. #if defined(RCC_PLLP_SUPPORT)
  2858. uint32_t pllp = 0U;
  2859. #endif /* RCC_PLLP_SUPPORT */
  2860. /* Handle SAIs */
  2861. if(PeriphClk == RCC_PERIPHCLK_SAI1)
  2862. {
  2863. srcclk = __HAL_RCC_GET_SAI1_SOURCE();
  2864. if(srcclk == RCC_SAI1CLKSOURCE_PIN)
  2865. {
  2866. frequency = EXTERNAL_SAI1_CLOCK_VALUE;
  2867. }
  2868. /* Else, PLL clock output to check below */
  2869. }
  2870. #if defined(SAI2)
  2871. else
  2872. {
  2873. if(PeriphClk == RCC_PERIPHCLK_SAI2)
  2874. {
  2875. srcclk = __HAL_RCC_GET_SAI2_SOURCE();
  2876. if(srcclk == RCC_SAI2CLKSOURCE_PIN)
  2877. {
  2878. frequency = EXTERNAL_SAI2_CLOCK_VALUE;
  2879. }
  2880. /* Else, PLL clock output to check below */
  2881. }
  2882. }
  2883. #endif /* SAI2 */
  2884. if(frequency == 0U)
  2885. {
  2886. pllvco = InputFrequency;
  2887. #if defined(SAI2)
  2888. if((srcclk == RCC_SAI1CLKSOURCE_PLL) || (srcclk == RCC_SAI2CLKSOURCE_PLL))
  2889. {
  2890. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLRDY) && (__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLL_SAI3CLK) != 0U))
  2891. {
  2892. /* f(PLL Source) / PLLM */
  2893. pllvco = (pllvco / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  2894. /* f(PLLSAI3CLK) = f(VCO input) * PLLN / PLLP */
  2895. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  2896. #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
  2897. pllp = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos;
  2898. #endif
  2899. if(pllp == 0U)
  2900. {
  2901. if(READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) != 0U)
  2902. {
  2903. pllp = 17U;
  2904. }
  2905. else
  2906. {
  2907. pllp = 7U;
  2908. }
  2909. }
  2910. frequency = (pllvco * plln) / pllp;
  2911. }
  2912. }
  2913. else if(srcclk == 0U) /* RCC_SAI1CLKSOURCE_PLLSAI1 || RCC_SAI2CLKSOURCE_PLLSAI1 */
  2914. {
  2915. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLSAI1RDY) && (__HAL_RCC_GET_PLLSAI1CLKOUT_CONFIG(RCC_PLLSAI1_SAI1CLK) != 0U))
  2916. {
  2917. #if defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  2918. /* PLLSAI1M exists: apply PLLSAI1M divider for PLLSAI1 output computation */
  2919. /* f(PLLSAI1 Source) / PLLSAI1M */
  2920. pllvco = (pllvco / ((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1M) >> RCC_PLLSAI1CFGR_PLLSAI1M_Pos) + 1U));
  2921. #else
  2922. /* f(PLL Source) / PLLM */
  2923. pllvco = (pllvco / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  2924. #endif
  2925. /* f(PLLSAI1CLK) = f(VCOSAI1 input) * PLLSAI1N / PLLSAI1P */
  2926. plln = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos;
  2927. #if defined(RCC_PLLSAI1P_DIV_2_31_SUPPORT)
  2928. pllp = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1PDIV) >> RCC_PLLSAI1CFGR_PLLSAI1PDIV_Pos;
  2929. #endif
  2930. if(pllp == 0U)
  2931. {
  2932. if(READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1P) != 0U)
  2933. {
  2934. pllp = 17U;
  2935. }
  2936. else
  2937. {
  2938. pllp = 7U;
  2939. }
  2940. }
  2941. frequency = (pllvco * plln) / pllp;
  2942. }
  2943. }
  2944. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2945. else if((srcclk == RCC_SAI1CLKSOURCE_HSI) || (srcclk == RCC_SAI2CLKSOURCE_HSI))
  2946. {
  2947. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  2948. {
  2949. frequency = HSI_VALUE;
  2950. }
  2951. }
  2952. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2953. #else
  2954. if(srcclk == RCC_SAI1CLKSOURCE_PLL)
  2955. {
  2956. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLRDY) && (__HAL_RCC_GET_PLLCLKOUT_CONFIG(RCC_PLL_SAI2CLK) != 0U))
  2957. {
  2958. /* f(PLL Source) / PLLM */
  2959. pllvco = (pllvco / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  2960. /* f(PLLSAI2CLK) = f(VCO input) * PLLN / PLLP */
  2961. plln = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  2962. #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
  2963. pllp = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos;
  2964. #endif
  2965. if(pllp == 0U)
  2966. {
  2967. if(READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) != 0U)
  2968. {
  2969. pllp = 17U;
  2970. }
  2971. else
  2972. {
  2973. pllp = 7U;
  2974. }
  2975. }
  2976. frequency = (pllvco * plln) / pllp;
  2977. }
  2978. else if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  2979. {
  2980. /* HSI automatically selected as clock source if PLLs not enabled */
  2981. frequency = HSI_VALUE;
  2982. }
  2983. else
  2984. {
  2985. /* No clock source, frequency default init at 0 */
  2986. }
  2987. }
  2988. else if(srcclk == RCC_SAI1CLKSOURCE_PLLSAI1)
  2989. {
  2990. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLSAI1RDY) && (__HAL_RCC_GET_PLLSAI1CLKOUT_CONFIG(RCC_PLLSAI1_SAI1CLK) != 0U))
  2991. {
  2992. #if defined(RCC_PLLSAI1M_DIV_1_16_SUPPORT)
  2993. /* PLLSAI1M exists: apply PLLSAI1M divider for PLLSAI1 output computation */
  2994. /* f(PLLSAI1 Source) / PLLSAI1M */
  2995. pllvco = (pllvco / ((READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1M) >> RCC_PLLSAI1CFGR_PLLSAI1M_Pos) + 1U));
  2996. #else
  2997. /* f(PLL Source) / PLLM */
  2998. pllvco = (pllvco / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  2999. #endif
  3000. /* f(PLLSAI1CLK) = f(VCOSAI1 input) * PLLSAI1N / PLLSAI1P */
  3001. plln = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N) >> RCC_PLLSAI1CFGR_PLLSAI1N_Pos;
  3002. #if defined(RCC_PLLSAI1P_DIV_2_31_SUPPORT)
  3003. pllp = READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1PDIV) >> RCC_PLLSAI1CFGR_PLLSAI1PDIV_Pos;
  3004. #endif
  3005. if(pllp == 0U)
  3006. {
  3007. if(READ_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1P) != 0U)
  3008. {
  3009. pllp = 17U;
  3010. }
  3011. else
  3012. {
  3013. pllp = 7U;
  3014. }
  3015. }
  3016. frequency = (pllvco * plln) / pllp;
  3017. }
  3018. else if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY))
  3019. {
  3020. /* HSI automatically selected as clock source if PLLs not enabled */
  3021. frequency = HSI_VALUE;
  3022. }
  3023. else
  3024. {
  3025. /* No clock source, frequency default init at 0 */
  3026. }
  3027. }
  3028. #endif /* SAI2 */
  3029. #if defined(RCC_PLLSAI2_SUPPORT)
  3030. else if((srcclk == RCC_SAI1CLKSOURCE_PLLSAI2) || (srcclk == RCC_SAI2CLKSOURCE_PLLSAI2))
  3031. {
  3032. if(HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLSAI2RDY) && (__HAL_RCC_GET_PLLSAI2CLKOUT_CONFIG(RCC_PLLSAI2_SAI2CLK) != 0U))
  3033. {
  3034. #if defined(RCC_PLLSAI2M_DIV_1_16_SUPPORT)
  3035. /* PLLSAI2M exists: apply PLLSAI2M divider for PLLSAI2 output computation */
  3036. /* f(PLLSAI2 Source) / PLLSAI2M */
  3037. pllvco = (pllvco / ((READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2M) >> RCC_PLLSAI2CFGR_PLLSAI2M_Pos) + 1U));
  3038. #else
  3039. /* f(PLL Source) / PLLM */
  3040. pllvco = (pllvco / ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U));
  3041. #endif
  3042. /* f(PLLSAI2CLK) = f(VCOSAI2 input) * PLLSAI2N / PLLSAI2P */
  3043. plln = READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N) >> RCC_PLLSAI2CFGR_PLLSAI2N_Pos;
  3044. #if defined(RCC_PLLSAI2P_DIV_2_31_SUPPORT)
  3045. pllp = READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2PDIV) >> RCC_PLLSAI2CFGR_PLLSAI2PDIV_Pos;
  3046. #endif
  3047. if(pllp == 0U)
  3048. {
  3049. if(READ_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2P) != 0U)
  3050. {
  3051. pllp = 17U;
  3052. }
  3053. else
  3054. {
  3055. pllp = 7U;
  3056. }
  3057. }
  3058. frequency = (pllvco * plln) / pllp;
  3059. }
  3060. }
  3061. #endif /* RCC_PLLSAI2_SUPPORT */
  3062. else
  3063. {
  3064. /* No clock source, frequency default init at 0 */
  3065. }
  3066. }
  3067. return frequency;
  3068. }
  3069. #endif /* SAI1 */
  3070. /**
  3071. * @}
  3072. */
  3073. /**
  3074. * @}
  3075. */
  3076. #endif /* HAL_RCC_MODULE_ENABLED */
  3077. /**
  3078. * @}
  3079. */
  3080. /**
  3081. * @}
  3082. */