You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f4xx_hal_rcc_ex.c 150KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_rcc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extension RCC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities RCC extension peripheral:
  8. * + Extended Peripheral Control functions
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  14. * All rights reserved.</center></h2>
  15. *
  16. * This software component is licensed by ST under BSD 3-Clause license,
  17. * the "License"; You may not use this file except in compliance with the
  18. * License. You may obtain a copy of the License at:
  19. * opensource.org/licenses/BSD-3-Clause
  20. *
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32f4xx_hal.h"
  25. /** @addtogroup STM32F4xx_HAL_Driver
  26. * @{
  27. */
  28. /** @defgroup RCCEx RCCEx
  29. * @brief RCCEx HAL module driver
  30. * @{
  31. */
  32. #ifdef HAL_RCC_MODULE_ENABLED
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. /** @addtogroup RCCEx_Private_Constants
  36. * @{
  37. */
  38. /**
  39. * @}
  40. */
  41. /* Private macro -------------------------------------------------------------*/
  42. /* Private variables ---------------------------------------------------------*/
  43. /* Private function prototypes -----------------------------------------------*/
  44. /* Private functions ---------------------------------------------------------*/
  45. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  46. * @{
  47. */
  48. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  49. * @brief Extended Peripheral Control functions
  50. *
  51. @verbatim
  52. ===============================================================================
  53. ##### Extended Peripheral Control functions #####
  54. ===============================================================================
  55. [..]
  56. This subsection provides a set of functions allowing to control the RCC Clocks
  57. frequencies.
  58. [..]
  59. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  60. select the RTC clock source; in this case the Backup domain will be reset in
  61. order to modify the RTC Clock source, as consequence RTC registers (including
  62. the backup registers) and RCC_BDCR register are set to their reset values.
  63. @endverbatim
  64. * @{
  65. */
  66. #if defined(STM32F446xx)
  67. /**
  68. * @brief Initializes the RCC extended peripherals clocks according to the specified
  69. * parameters in the RCC_PeriphCLKInitTypeDef.
  70. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  71. * contains the configuration information for the Extended Peripherals
  72. * clocks(I2S, SAI, LTDC RTC and TIM).
  73. *
  74. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  75. * the RTC clock source; in this case the Backup domain will be reset in
  76. * order to modify the RTC Clock source, as consequence RTC registers (including
  77. * the backup registers) and RCC_BDCR register are set to their reset values.
  78. *
  79. * @retval HAL status
  80. */
  81. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  82. {
  83. uint32_t tickstart = 0U;
  84. uint32_t tmpreg1 = 0U;
  85. uint32_t plli2sp = 0U;
  86. uint32_t plli2sq = 0U;
  87. uint32_t plli2sr = 0U;
  88. uint32_t pllsaip = 0U;
  89. uint32_t pllsaiq = 0U;
  90. uint32_t plli2sused = 0U;
  91. uint32_t pllsaiused = 0U;
  92. /* Check the peripheral clock selection parameters */
  93. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  94. /*------------------------ I2S APB1 configuration --------------------------*/
  95. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == (RCC_PERIPHCLK_I2S_APB1))
  96. {
  97. /* Check the parameters */
  98. assert_param(IS_RCC_I2SAPB1CLKSOURCE(PeriphClkInit->I2sApb1ClockSelection));
  99. /* Configure I2S Clock source */
  100. __HAL_RCC_I2S_APB1_CONFIG(PeriphClkInit->I2sApb1ClockSelection);
  101. /* Enable the PLLI2S when it's used as clock source for I2S */
  102. if(PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)
  103. {
  104. plli2sused = 1U;
  105. }
  106. }
  107. /*--------------------------------------------------------------------------*/
  108. /*---------------------------- I2S APB2 configuration ----------------------*/
  109. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == (RCC_PERIPHCLK_I2S_APB2))
  110. {
  111. /* Check the parameters */
  112. assert_param(IS_RCC_I2SAPB2CLKSOURCE(PeriphClkInit->I2sApb2ClockSelection));
  113. /* Configure I2S Clock source */
  114. __HAL_RCC_I2S_APB2_CONFIG(PeriphClkInit->I2sApb2ClockSelection);
  115. /* Enable the PLLI2S when it's used as clock source for I2S */
  116. if(PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)
  117. {
  118. plli2sused = 1U;
  119. }
  120. }
  121. /*--------------------------------------------------------------------------*/
  122. /*--------------------------- SAI1 configuration ---------------------------*/
  123. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == (RCC_PERIPHCLK_SAI1))
  124. {
  125. /* Check the parameters */
  126. assert_param(IS_RCC_SAI1CLKSOURCE(PeriphClkInit->Sai1ClockSelection));
  127. /* Configure SAI1 Clock source */
  128. __HAL_RCC_SAI1_CONFIG(PeriphClkInit->Sai1ClockSelection);
  129. /* Enable the PLLI2S when it's used as clock source for SAI */
  130. if(PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLI2S)
  131. {
  132. plli2sused = 1U;
  133. }
  134. /* Enable the PLLSAI when it's used as clock source for SAI */
  135. if(PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLSAI)
  136. {
  137. pllsaiused = 1U;
  138. }
  139. }
  140. /*--------------------------------------------------------------------------*/
  141. /*-------------------------- SAI2 configuration ----------------------------*/
  142. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == (RCC_PERIPHCLK_SAI2))
  143. {
  144. /* Check the parameters */
  145. assert_param(IS_RCC_SAI2CLKSOURCE(PeriphClkInit->Sai2ClockSelection));
  146. /* Configure SAI2 Clock source */
  147. __HAL_RCC_SAI2_CONFIG(PeriphClkInit->Sai2ClockSelection);
  148. /* Enable the PLLI2S when it's used as clock source for SAI */
  149. if(PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLI2S)
  150. {
  151. plli2sused = 1U;
  152. }
  153. /* Enable the PLLSAI when it's used as clock source for SAI */
  154. if(PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLSAI)
  155. {
  156. pllsaiused = 1U;
  157. }
  158. }
  159. /*--------------------------------------------------------------------------*/
  160. /*----------------------------- RTC configuration --------------------------*/
  161. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  162. {
  163. /* Check for RTC Parameters used to output RTCCLK */
  164. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  165. /* Enable Power Clock*/
  166. __HAL_RCC_PWR_CLK_ENABLE();
  167. /* Enable write access to Backup domain */
  168. PWR->CR |= PWR_CR_DBP;
  169. /* Get tick */
  170. tickstart = HAL_GetTick();
  171. while((PWR->CR & PWR_CR_DBP) == RESET)
  172. {
  173. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  174. {
  175. return HAL_TIMEOUT;
  176. }
  177. }
  178. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  179. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  180. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  181. {
  182. /* Store the content of BDCR register before the reset of Backup Domain */
  183. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  184. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  185. __HAL_RCC_BACKUPRESET_FORCE();
  186. __HAL_RCC_BACKUPRESET_RELEASE();
  187. /* Restore the Content of BDCR register */
  188. RCC->BDCR = tmpreg1;
  189. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  190. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  191. {
  192. /* Get tick */
  193. tickstart = HAL_GetTick();
  194. /* Wait till LSE is ready */
  195. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  196. {
  197. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  198. {
  199. return HAL_TIMEOUT;
  200. }
  201. }
  202. }
  203. }
  204. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  205. }
  206. /*--------------------------------------------------------------------------*/
  207. /*---------------------------- TIM configuration ---------------------------*/
  208. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  209. {
  210. /* Configure Timer Prescaler */
  211. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  212. }
  213. /*--------------------------------------------------------------------------*/
  214. /*---------------------------- FMPI2C1 Configuration -----------------------*/
  215. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_FMPI2C1) == RCC_PERIPHCLK_FMPI2C1)
  216. {
  217. /* Check the parameters */
  218. assert_param(IS_RCC_FMPI2C1CLKSOURCE(PeriphClkInit->Fmpi2c1ClockSelection));
  219. /* Configure the FMPI2C1 clock source */
  220. __HAL_RCC_FMPI2C1_CONFIG(PeriphClkInit->Fmpi2c1ClockSelection);
  221. }
  222. /*--------------------------------------------------------------------------*/
  223. /*------------------------------ CEC Configuration -------------------------*/
  224. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CEC) == RCC_PERIPHCLK_CEC)
  225. {
  226. /* Check the parameters */
  227. assert_param(IS_RCC_CECCLKSOURCE(PeriphClkInit->CecClockSelection));
  228. /* Configure the CEC clock source */
  229. __HAL_RCC_CEC_CONFIG(PeriphClkInit->CecClockSelection);
  230. }
  231. /*--------------------------------------------------------------------------*/
  232. /*----------------------------- CLK48 Configuration ------------------------*/
  233. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48)
  234. {
  235. /* Check the parameters */
  236. assert_param(IS_RCC_CLK48CLKSOURCE(PeriphClkInit->Clk48ClockSelection));
  237. /* Configure the CLK48 clock source */
  238. __HAL_RCC_CLK48_CONFIG(PeriphClkInit->Clk48ClockSelection);
  239. /* Enable the PLLSAI when it's used as clock source for CLK48 */
  240. if(PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP)
  241. {
  242. pllsaiused = 1U;
  243. }
  244. }
  245. /*--------------------------------------------------------------------------*/
  246. /*----------------------------- SDIO Configuration -------------------------*/
  247. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO)
  248. {
  249. /* Check the parameters */
  250. assert_param(IS_RCC_SDIOCLKSOURCE(PeriphClkInit->SdioClockSelection));
  251. /* Configure the SDIO clock source */
  252. __HAL_RCC_SDIO_CONFIG(PeriphClkInit->SdioClockSelection);
  253. }
  254. /*--------------------------------------------------------------------------*/
  255. /*------------------------------ SPDIFRX Configuration ---------------------*/
  256. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SPDIFRX) == RCC_PERIPHCLK_SPDIFRX)
  257. {
  258. /* Check the parameters */
  259. assert_param(IS_RCC_SPDIFRXCLKSOURCE(PeriphClkInit->SpdifClockSelection));
  260. /* Configure the SPDIFRX clock source */
  261. __HAL_RCC_SPDIFRX_CONFIG(PeriphClkInit->SpdifClockSelection);
  262. /* Enable the PLLI2S when it's used as clock source for SPDIFRX */
  263. if(PeriphClkInit->SpdifClockSelection == RCC_SPDIFRXCLKSOURCE_PLLI2SP)
  264. {
  265. plli2sused = 1U;
  266. }
  267. }
  268. /*--------------------------------------------------------------------------*/
  269. /*---------------------------- PLLI2S Configuration ------------------------*/
  270. /* PLLI2S is configured when a peripheral will use it as source clock : SAI1, SAI2, I2S on APB1,
  271. I2S on APB2 or SPDIFRX */
  272. if((plli2sused == 1U) || (PeriphClkInit->PeriphClockSelection == RCC_PERIPHCLK_PLLI2S))
  273. {
  274. /* Disable the PLLI2S */
  275. __HAL_RCC_PLLI2S_DISABLE();
  276. /* Get tick */
  277. tickstart = HAL_GetTick();
  278. /* Wait till PLLI2S is disabled */
  279. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  280. {
  281. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  282. {
  283. /* return in case of Timeout detected */
  284. return HAL_TIMEOUT;
  285. }
  286. }
  287. /* check for common PLLI2S Parameters */
  288. assert_param(IS_RCC_PLLI2SM_VALUE(PeriphClkInit->PLLI2S.PLLI2SM));
  289. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  290. /*------ In Case of PLLI2S is selected as source clock for I2S -----------*/
  291. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == RCC_PERIPHCLK_I2S_APB1) && (PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)) ||
  292. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == RCC_PERIPHCLK_I2S_APB2) && (PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)))
  293. {
  294. /* check for Parameters */
  295. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  296. /* Read PLLI2SP/PLLI2SQ value from PLLI2SCFGR register (this value is not needed for I2S configuration) */
  297. plli2sp = ((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> RCC_PLLI2SCFGR_PLLI2SP_Pos) + 1U) << 1U);
  298. plli2sq = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  299. /* Configure the PLLI2S division factors */
  300. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  301. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  302. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , plli2sp, plli2sq, PeriphClkInit->PLLI2S.PLLI2SR);
  303. }
  304. /*------- In Case of PLLI2S is selected as source clock for SAI ----------*/
  305. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == RCC_PERIPHCLK_SAI1) && (PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLI2S)) ||
  306. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == RCC_PERIPHCLK_SAI2) && (PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLI2S)))
  307. {
  308. /* Check for PLLI2S Parameters */
  309. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  310. /* Check for PLLI2S/DIVQ parameters */
  311. assert_param(IS_RCC_PLLI2S_DIVQ_VALUE(PeriphClkInit->PLLI2SDivQ));
  312. /* Read PLLI2SP/PLLI2SR value from PLLI2SCFGR register (this value is not needed for SAI configuration) */
  313. plli2sp = ((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> RCC_PLLI2SCFGR_PLLI2SP_Pos) + 1U) << 1U);
  314. plli2sr = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  315. /* Configure the PLLI2S division factors */
  316. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  317. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  318. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  319. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , plli2sp, PeriphClkInit->PLLI2S.PLLI2SQ, plli2sr);
  320. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  321. __HAL_RCC_PLLI2S_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLI2SDivQ);
  322. }
  323. /*------ In Case of PLLI2S is selected as source clock for SPDIFRX -------*/
  324. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SPDIFRX) == RCC_PERIPHCLK_SPDIFRX) && (PeriphClkInit->SpdifClockSelection == RCC_SPDIFRXCLKSOURCE_PLLI2SP))
  325. {
  326. /* check for Parameters */
  327. assert_param(IS_RCC_PLLI2SP_VALUE(PeriphClkInit->PLLI2S.PLLI2SP));
  328. /* Read PLLI2SR value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  329. plli2sq = ((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> RCC_PLLI2SCFGR_PLLI2SP_Pos) + 1U) << 1U);
  330. plli2sr = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  331. /* Configure the PLLI2S division factors */
  332. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  333. /* SPDIFRXCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SP */
  334. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SP, plli2sq, plli2sr);
  335. }
  336. /*----------------- In Case of PLLI2S is just selected -----------------*/
  337. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S)
  338. {
  339. /* Check for Parameters */
  340. assert_param(IS_RCC_PLLI2SP_VALUE(PeriphClkInit->PLLI2S.PLLI2SP));
  341. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  342. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  343. /* Configure the PLLI2S division factors */
  344. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  345. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SP, PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  346. }
  347. /* Enable the PLLI2S */
  348. __HAL_RCC_PLLI2S_ENABLE();
  349. /* Get tick */
  350. tickstart = HAL_GetTick();
  351. /* Wait till PLLI2S is ready */
  352. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  353. {
  354. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  355. {
  356. /* return in case of Timeout detected */
  357. return HAL_TIMEOUT;
  358. }
  359. }
  360. }
  361. /*--------------------------------------------------------------------------*/
  362. /*----------------------------- PLLSAI Configuration -----------------------*/
  363. /* PLLSAI is configured when a peripheral will use it as source clock : SAI1, SAI2, CLK48 or SDIO */
  364. if(pllsaiused == 1U)
  365. {
  366. /* Disable PLLSAI Clock */
  367. __HAL_RCC_PLLSAI_DISABLE();
  368. /* Get tick */
  369. tickstart = HAL_GetTick();
  370. /* Wait till PLLSAI is disabled */
  371. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  372. {
  373. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  374. {
  375. /* return in case of Timeout detected */
  376. return HAL_TIMEOUT;
  377. }
  378. }
  379. /* Check the PLLSAI division factors */
  380. assert_param(IS_RCC_PLLSAIM_VALUE(PeriphClkInit->PLLSAI.PLLSAIM));
  381. assert_param(IS_RCC_PLLSAIN_VALUE(PeriphClkInit->PLLSAI.PLLSAIN));
  382. /*------ In Case of PLLSAI is selected as source clock for SAI -----------*/
  383. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == RCC_PERIPHCLK_SAI1) && (PeriphClkInit->Sai1ClockSelection == RCC_SAI1CLKSOURCE_PLLSAI)) ||
  384. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI2) == RCC_PERIPHCLK_SAI2) && (PeriphClkInit->Sai2ClockSelection == RCC_SAI2CLKSOURCE_PLLSAI)))
  385. {
  386. /* check for PLLSAIQ Parameter */
  387. assert_param(IS_RCC_PLLSAIQ_VALUE(PeriphClkInit->PLLSAI.PLLSAIQ));
  388. /* check for PLLSAI/DIVQ Parameter */
  389. assert_param(IS_RCC_PLLSAI_DIVQ_VALUE(PeriphClkInit->PLLSAIDivQ));
  390. /* Read PLLSAIP value from PLLSAICFGR register (this value is not needed for SAI configuration) */
  391. pllsaip = ((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> RCC_PLLSAICFGR_PLLSAIP_Pos) + 1U) << 1U);
  392. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  393. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  394. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  395. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIM, PeriphClkInit->PLLSAI.PLLSAIN , pllsaip, PeriphClkInit->PLLSAI.PLLSAIQ, 0U);
  396. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  397. __HAL_RCC_PLLSAI_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLSAIDivQ);
  398. }
  399. /*------ In Case of PLLSAI is selected as source clock for CLK48 ---------*/
  400. /* In Case of PLLI2S is selected as source clock for CLK48 */
  401. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48) && (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP))
  402. {
  403. /* check for Parameters */
  404. assert_param(IS_RCC_PLLSAIP_VALUE(PeriphClkInit->PLLSAI.PLLSAIP));
  405. /* Read PLLSAIQ value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  406. pllsaiq = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  407. /* Configure the PLLSAI division factors */
  408. /* PLLSAI_VCO = f(VCO clock) = f(PLLSAI clock input) * (PLLI2SN/PLLSAIM) */
  409. /* 48CLK = f(PLLSAI clock output) = f(VCO clock) / PLLSAIP */
  410. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIM, PeriphClkInit->PLLSAI.PLLSAIN , PeriphClkInit->PLLSAI.PLLSAIP, pllsaiq, 0U);
  411. }
  412. /* Enable PLLSAI Clock */
  413. __HAL_RCC_PLLSAI_ENABLE();
  414. /* Get tick */
  415. tickstart = HAL_GetTick();
  416. /* Wait till PLLSAI is ready */
  417. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  418. {
  419. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  420. {
  421. /* return in case of Timeout detected */
  422. return HAL_TIMEOUT;
  423. }
  424. }
  425. }
  426. return HAL_OK;
  427. }
  428. /**
  429. * @brief Get the RCC_PeriphCLKInitTypeDef according to the internal
  430. * RCC configuration registers.
  431. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  432. * will be configured.
  433. * @retval None
  434. */
  435. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  436. {
  437. uint32_t tempreg;
  438. /* Set all possible values for the extended clock type parameter------------*/
  439. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_I2S_APB2 |\
  440. RCC_PERIPHCLK_SAI1 | RCC_PERIPHCLK_SAI2 |\
  441. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  442. RCC_PERIPHCLK_CEC | RCC_PERIPHCLK_FMPI2C1 |\
  443. RCC_PERIPHCLK_CLK48 | RCC_PERIPHCLK_SDIO |\
  444. RCC_PERIPHCLK_SPDIFRX;
  445. /* Get the PLLI2S Clock configuration --------------------------------------*/
  446. PeriphClkInit->PLLI2S.PLLI2SM = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM) >> RCC_PLLI2SCFGR_PLLI2SM_Pos);
  447. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> RCC_PLLI2SCFGR_PLLI2SN_Pos);
  448. PeriphClkInit->PLLI2S.PLLI2SP = (uint32_t)((((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SP) >> RCC_PLLI2SCFGR_PLLI2SP_Pos) + 1U) << 1U);
  449. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  450. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  451. /* Get the PLLSAI Clock configuration --------------------------------------*/
  452. PeriphClkInit->PLLSAI.PLLSAIM = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIM) >> RCC_PLLSAICFGR_PLLSAIM_Pos);
  453. PeriphClkInit->PLLSAI.PLLSAIN = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> RCC_PLLSAICFGR_PLLSAIN_Pos);
  454. PeriphClkInit->PLLSAI.PLLSAIP = (uint32_t)((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> RCC_PLLSAICFGR_PLLSAIP_Pos) + 1U) << 1U);
  455. PeriphClkInit->PLLSAI.PLLSAIQ = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  456. /* Get the PLLSAI/PLLI2S division factors ----------------------------------*/
  457. PeriphClkInit->PLLI2SDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) >> RCC_DCKCFGR_PLLI2SDIVQ_Pos);
  458. PeriphClkInit->PLLSAIDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> RCC_DCKCFGR_PLLSAIDIVQ_Pos);
  459. /* Get the SAI1 clock configuration ----------------------------------------*/
  460. PeriphClkInit->Sai1ClockSelection = __HAL_RCC_GET_SAI1_SOURCE();
  461. /* Get the SAI2 clock configuration ----------------------------------------*/
  462. PeriphClkInit->Sai2ClockSelection = __HAL_RCC_GET_SAI2_SOURCE();
  463. /* Get the I2S APB1 clock configuration ------------------------------------*/
  464. PeriphClkInit->I2sApb1ClockSelection = __HAL_RCC_GET_I2S_APB1_SOURCE();
  465. /* Get the I2S APB2 clock configuration ------------------------------------*/
  466. PeriphClkInit->I2sApb2ClockSelection = __HAL_RCC_GET_I2S_APB2_SOURCE();
  467. /* Get the RTC Clock configuration -----------------------------------------*/
  468. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  469. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  470. /* Get the CEC clock configuration -----------------------------------------*/
  471. PeriphClkInit->CecClockSelection = __HAL_RCC_GET_CEC_SOURCE();
  472. /* Get the FMPI2C1 clock configuration -------------------------------------*/
  473. PeriphClkInit->Fmpi2c1ClockSelection = __HAL_RCC_GET_FMPI2C1_SOURCE();
  474. /* Get the CLK48 clock configuration ----------------------------------------*/
  475. PeriphClkInit->Clk48ClockSelection = __HAL_RCC_GET_CLK48_SOURCE();
  476. /* Get the SDIO clock configuration ----------------------------------------*/
  477. PeriphClkInit->SdioClockSelection = __HAL_RCC_GET_SDIO_SOURCE();
  478. /* Get the SPDIFRX clock configuration -------------------------------------*/
  479. PeriphClkInit->SpdifClockSelection = __HAL_RCC_GET_SPDIFRX_SOURCE();
  480. /* Get the TIM Prescaler configuration -------------------------------------*/
  481. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  482. {
  483. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  484. }
  485. else
  486. {
  487. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  488. }
  489. }
  490. /**
  491. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  492. * @note Return 0 if peripheral clock identifier not managed by this API
  493. * @param PeriphClk Peripheral clock identifier
  494. * This parameter can be one of the following values:
  495. * @arg RCC_PERIPHCLK_SAI1: SAI1 peripheral clock
  496. * @arg RCC_PERIPHCLK_SAI2: SAI2 peripheral clock
  497. * @arg RCC_PERIPHCLK_I2S_APB1: I2S APB1 peripheral clock
  498. * @arg RCC_PERIPHCLK_I2S_APB2: I2S APB2 peripheral clock
  499. * @retval Frequency in KHz
  500. */
  501. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  502. {
  503. uint32_t tmpreg1 = 0U;
  504. /* This variable used to store the SAI clock frequency (value in Hz) */
  505. uint32_t frequency = 0U;
  506. /* This variable used to store the VCO Input (value in Hz) */
  507. uint32_t vcoinput = 0U;
  508. /* This variable used to store the SAI clock source */
  509. uint32_t saiclocksource = 0U;
  510. uint32_t srcclk = 0U;
  511. /* This variable used to store the VCO Output (value in Hz) */
  512. uint32_t vcooutput = 0U;
  513. switch (PeriphClk)
  514. {
  515. case RCC_PERIPHCLK_SAI1:
  516. case RCC_PERIPHCLK_SAI2:
  517. {
  518. saiclocksource = RCC->DCKCFGR;
  519. saiclocksource &= (RCC_DCKCFGR_SAI1SRC | RCC_DCKCFGR_SAI2SRC);
  520. switch (saiclocksource)
  521. {
  522. case 0U: /* PLLSAI is the clock source for SAI*/
  523. {
  524. /* Configure the PLLSAI division factor */
  525. /* PLLSAI_VCO Input = PLL_SOURCE/PLLSAIM */
  526. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  527. {
  528. /* In Case the PLL Source is HSI (Internal Clock) */
  529. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIM));
  530. }
  531. else
  532. {
  533. /* In Case the PLL Source is HSE (External Clock) */
  534. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIM)));
  535. }
  536. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  537. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  538. tmpreg1 = (RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> 24U;
  539. frequency = (vcoinput * ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> 6U))/(tmpreg1);
  540. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  541. tmpreg1 = (((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> 8U) + 1U);
  542. frequency = frequency/(tmpreg1);
  543. break;
  544. }
  545. case RCC_DCKCFGR_SAI1SRC_0: /* PLLI2S is the clock source for SAI*/
  546. case RCC_DCKCFGR_SAI2SRC_0: /* PLLI2S is the clock source for SAI*/
  547. {
  548. /* Configure the PLLI2S division factor */
  549. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  550. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  551. {
  552. /* In Case the PLL Source is HSI (Internal Clock) */
  553. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  554. }
  555. else
  556. {
  557. /* In Case the PLL Source is HSE (External Clock) */
  558. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM)));
  559. }
  560. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  561. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  562. tmpreg1 = (RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> 24U;
  563. frequency = (vcoinput * ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U))/(tmpreg1);
  564. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  565. tmpreg1 = ((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) + 1U);
  566. frequency = frequency/(tmpreg1);
  567. break;
  568. }
  569. case RCC_DCKCFGR_SAI1SRC_1: /* PLLR is the clock source for SAI*/
  570. case RCC_DCKCFGR_SAI2SRC_1: /* PLLR is the clock source for SAI*/
  571. {
  572. /* Configure the PLLI2S division factor */
  573. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  574. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  575. {
  576. /* In Case the PLL Source is HSI (Internal Clock) */
  577. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  578. }
  579. else
  580. {
  581. /* In Case the PLL Source is HSE (External Clock) */
  582. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM)));
  583. }
  584. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  585. /* SAI_CLK_x = PLL_VCO Output/PLLR */
  586. tmpreg1 = (RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U;
  587. frequency = (vcoinput * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U))/(tmpreg1);
  588. break;
  589. }
  590. case RCC_DCKCFGR_SAI1SRC: /* External clock is the clock source for SAI*/
  591. {
  592. frequency = EXTERNAL_CLOCK_VALUE;
  593. break;
  594. }
  595. case RCC_DCKCFGR_SAI2SRC: /* PLLSRC(HSE or HSI) is the clock source for SAI*/
  596. {
  597. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  598. {
  599. /* In Case the PLL Source is HSI (Internal Clock) */
  600. frequency = (uint32_t)(HSI_VALUE);
  601. }
  602. else
  603. {
  604. /* In Case the PLL Source is HSE (External Clock) */
  605. frequency = (uint32_t)(HSE_VALUE);
  606. }
  607. break;
  608. }
  609. default :
  610. {
  611. break;
  612. }
  613. }
  614. break;
  615. }
  616. case RCC_PERIPHCLK_I2S_APB1:
  617. {
  618. /* Get the current I2S source */
  619. srcclk = __HAL_RCC_GET_I2S_APB1_SOURCE();
  620. switch (srcclk)
  621. {
  622. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  623. case RCC_I2SAPB1CLKSOURCE_EXT:
  624. {
  625. /* Set the I2S clock to the external clock value */
  626. frequency = EXTERNAL_CLOCK_VALUE;
  627. break;
  628. }
  629. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  630. case RCC_I2SAPB1CLKSOURCE_PLLI2S:
  631. {
  632. /* Configure the PLLI2S division factor */
  633. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  634. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  635. {
  636. /* Get the I2S source clock value */
  637. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  638. }
  639. else
  640. {
  641. /* Get the I2S source clock value */
  642. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  643. }
  644. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  645. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  646. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  647. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  648. break;
  649. }
  650. /* Check if I2S clock selection is PLL VCO Output divided by PLLR used as I2S clock */
  651. case RCC_I2SAPB1CLKSOURCE_PLLR:
  652. {
  653. /* Configure the PLL division factor R */
  654. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  655. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  656. {
  657. /* Get the I2S source clock value */
  658. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  659. }
  660. else
  661. {
  662. /* Get the I2S source clock value */
  663. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  664. }
  665. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  666. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U) & (RCC_PLLCFGR_PLLN >> 6U)));
  667. /* I2S_CLK = PLL_VCO Output/PLLR */
  668. frequency = (uint32_t)(vcooutput /(((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U) & (RCC_PLLCFGR_PLLR >> 28U)));
  669. break;
  670. }
  671. /* Check if I2S clock selection is HSI or HSE depending from PLL source Clock */
  672. case RCC_I2SAPB1CLKSOURCE_PLLSRC:
  673. {
  674. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  675. {
  676. frequency = HSE_VALUE;
  677. }
  678. else
  679. {
  680. frequency = HSI_VALUE;
  681. }
  682. break;
  683. }
  684. /* Clock not enabled for I2S*/
  685. default:
  686. {
  687. frequency = 0U;
  688. break;
  689. }
  690. }
  691. break;
  692. }
  693. case RCC_PERIPHCLK_I2S_APB2:
  694. {
  695. /* Get the current I2S source */
  696. srcclk = __HAL_RCC_GET_I2S_APB2_SOURCE();
  697. switch (srcclk)
  698. {
  699. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  700. case RCC_I2SAPB2CLKSOURCE_EXT:
  701. {
  702. /* Set the I2S clock to the external clock value */
  703. frequency = EXTERNAL_CLOCK_VALUE;
  704. break;
  705. }
  706. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  707. case RCC_I2SAPB2CLKSOURCE_PLLI2S:
  708. {
  709. /* Configure the PLLI2S division factor */
  710. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  711. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  712. {
  713. /* Get the I2S source clock value */
  714. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  715. }
  716. else
  717. {
  718. /* Get the I2S source clock value */
  719. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  720. }
  721. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  722. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  723. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  724. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  725. break;
  726. }
  727. /* Check if I2S clock selection is PLL VCO Output divided by PLLR used as I2S clock */
  728. case RCC_I2SAPB2CLKSOURCE_PLLR:
  729. {
  730. /* Configure the PLL division factor R */
  731. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  732. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  733. {
  734. /* Get the I2S source clock value */
  735. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  736. }
  737. else
  738. {
  739. /* Get the I2S source clock value */
  740. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  741. }
  742. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  743. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U) & (RCC_PLLCFGR_PLLN >> 6U)));
  744. /* I2S_CLK = PLL_VCO Output/PLLR */
  745. frequency = (uint32_t)(vcooutput /(((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U) & (RCC_PLLCFGR_PLLR >> 28U)));
  746. break;
  747. }
  748. /* Check if I2S clock selection is HSI or HSE depending from PLL source Clock */
  749. case RCC_I2SAPB2CLKSOURCE_PLLSRC:
  750. {
  751. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  752. {
  753. frequency = HSE_VALUE;
  754. }
  755. else
  756. {
  757. frequency = HSI_VALUE;
  758. }
  759. break;
  760. }
  761. /* Clock not enabled for I2S*/
  762. default:
  763. {
  764. frequency = 0U;
  765. break;
  766. }
  767. }
  768. break;
  769. }
  770. }
  771. return frequency;
  772. }
  773. #endif /* STM32F446xx */
  774. #if defined(STM32F469xx) || defined(STM32F479xx)
  775. /**
  776. * @brief Initializes the RCC extended peripherals clocks according to the specified
  777. * parameters in the RCC_PeriphCLKInitTypeDef.
  778. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  779. * contains the configuration information for the Extended Peripherals
  780. * clocks(I2S, SAI, LTDC, RTC and TIM).
  781. *
  782. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  783. * the RTC clock source; in this case the Backup domain will be reset in
  784. * order to modify the RTC Clock source, as consequence RTC registers (including
  785. * the backup registers) and RCC_BDCR register are set to their reset values.
  786. *
  787. * @retval HAL status
  788. */
  789. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  790. {
  791. uint32_t tickstart = 0U;
  792. uint32_t tmpreg1 = 0U;
  793. uint32_t pllsaip = 0U;
  794. uint32_t pllsaiq = 0U;
  795. uint32_t pllsair = 0U;
  796. /* Check the parameters */
  797. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  798. /*--------------------------- CLK48 Configuration --------------------------*/
  799. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48)
  800. {
  801. /* Check the parameters */
  802. assert_param(IS_RCC_CLK48CLKSOURCE(PeriphClkInit->Clk48ClockSelection));
  803. /* Configure the CLK48 clock source */
  804. __HAL_RCC_CLK48_CONFIG(PeriphClkInit->Clk48ClockSelection);
  805. }
  806. /*--------------------------------------------------------------------------*/
  807. /*------------------------------ SDIO Configuration ------------------------*/
  808. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO)
  809. {
  810. /* Check the parameters */
  811. assert_param(IS_RCC_SDIOCLKSOURCE(PeriphClkInit->SdioClockSelection));
  812. /* Configure the SDIO clock source */
  813. __HAL_RCC_SDIO_CONFIG(PeriphClkInit->SdioClockSelection);
  814. }
  815. /*--------------------------------------------------------------------------*/
  816. /*----------------------- SAI/I2S Configuration (PLLI2S) -------------------*/
  817. /*------------------- Common configuration SAI/I2S -------------------------*/
  818. /* In Case of SAI or I2S Clock Configuration through PLLI2S, PLLI2SN division
  819. factor is common parameters for both peripherals */
  820. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S) ||
  821. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == RCC_PERIPHCLK_SAI_PLLI2S) ||
  822. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S))
  823. {
  824. /* check for Parameters */
  825. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  826. /* Disable the PLLI2S */
  827. __HAL_RCC_PLLI2S_DISABLE();
  828. /* Get tick */
  829. tickstart = HAL_GetTick();
  830. /* Wait till PLLI2S is disabled */
  831. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  832. {
  833. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  834. {
  835. /* return in case of Timeout detected */
  836. return HAL_TIMEOUT;
  837. }
  838. }
  839. /*---------------------- I2S configuration -------------------------------*/
  840. /* In Case of I2S Clock Configuration through PLLI2S, PLLI2SR must be added
  841. only for I2S configuration */
  842. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == (RCC_PERIPHCLK_I2S))
  843. {
  844. /* check for Parameters */
  845. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  846. /* Configure the PLLI2S division factors */
  847. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) x (PLLI2SN/PLLM) */
  848. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  849. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SR);
  850. }
  851. /*---------------------------- SAI configuration -------------------------*/
  852. /* In Case of SAI Clock Configuration through PLLI2S, PLLI2SQ and PLLI2S_DIVQ must
  853. be added only for SAI configuration */
  854. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == (RCC_PERIPHCLK_SAI_PLLI2S))
  855. {
  856. /* Check the PLLI2S division factors */
  857. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  858. assert_param(IS_RCC_PLLI2S_DIVQ_VALUE(PeriphClkInit->PLLI2SDivQ));
  859. /* Read PLLI2SR value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  860. tmpreg1 = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  861. /* Configure the PLLI2S division factors */
  862. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  863. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  864. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  865. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ , tmpreg1);
  866. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  867. __HAL_RCC_PLLI2S_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLI2SDivQ);
  868. }
  869. /*----------------- In Case of PLLI2S is just selected -----------------*/
  870. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S)
  871. {
  872. /* Check for Parameters */
  873. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  874. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  875. /* Configure the PLLI2S multiplication and division factors */
  876. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN, PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  877. }
  878. /* Enable the PLLI2S */
  879. __HAL_RCC_PLLI2S_ENABLE();
  880. /* Get tick */
  881. tickstart = HAL_GetTick();
  882. /* Wait till PLLI2S is ready */
  883. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  884. {
  885. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  886. {
  887. /* return in case of Timeout detected */
  888. return HAL_TIMEOUT;
  889. }
  890. }
  891. }
  892. /*--------------------------------------------------------------------------*/
  893. /*----------------------- SAI/LTDC Configuration (PLLSAI) ------------------*/
  894. /*----------------------- Common configuration SAI/LTDC --------------------*/
  895. /* In Case of SAI, LTDC or CLK48 Clock Configuration through PLLSAI, PLLSAIN division
  896. factor is common parameters for these peripherals */
  897. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == RCC_PERIPHCLK_SAI_PLLSAI) ||
  898. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == RCC_PERIPHCLK_LTDC) ||
  899. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48) &&
  900. (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP)))
  901. {
  902. /* Check the PLLSAI division factors */
  903. assert_param(IS_RCC_PLLSAIN_VALUE(PeriphClkInit->PLLSAI.PLLSAIN));
  904. /* Disable PLLSAI Clock */
  905. __HAL_RCC_PLLSAI_DISABLE();
  906. /* Get tick */
  907. tickstart = HAL_GetTick();
  908. /* Wait till PLLSAI is disabled */
  909. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  910. {
  911. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  912. {
  913. /* return in case of Timeout detected */
  914. return HAL_TIMEOUT;
  915. }
  916. }
  917. /*---------------------------- SAI configuration -------------------------*/
  918. /* In Case of SAI Clock Configuration through PLLSAI, PLLSAIQ and PLLSAI_DIVQ must
  919. be added only for SAI configuration */
  920. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == (RCC_PERIPHCLK_SAI_PLLSAI))
  921. {
  922. assert_param(IS_RCC_PLLSAIQ_VALUE(PeriphClkInit->PLLSAI.PLLSAIQ));
  923. assert_param(IS_RCC_PLLSAI_DIVQ_VALUE(PeriphClkInit->PLLSAIDivQ));
  924. /* Read PLLSAIP value from PLLSAICFGR register (this value is not needed for SAI configuration) */
  925. pllsaip = ((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> RCC_PLLSAICFGR_PLLSAIP_Pos) + 1U) << 1U);
  926. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  927. pllsair = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> RCC_PLLSAICFGR_PLLSAIR_Pos);
  928. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  929. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  930. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  931. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN, pllsaip, PeriphClkInit->PLLSAI.PLLSAIQ, pllsair);
  932. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  933. __HAL_RCC_PLLSAI_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLSAIDivQ);
  934. }
  935. /*---------------------------- LTDC configuration ------------------------*/
  936. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == (RCC_PERIPHCLK_LTDC))
  937. {
  938. assert_param(IS_RCC_PLLSAIR_VALUE(PeriphClkInit->PLLSAI.PLLSAIR));
  939. assert_param(IS_RCC_PLLSAI_DIVR_VALUE(PeriphClkInit->PLLSAIDivR));
  940. /* Read PLLSAIP value from PLLSAICFGR register (this value is not needed for SAI configuration) */
  941. pllsaip = ((((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIP) >> RCC_PLLSAICFGR_PLLSAIP_Pos) + 1U) << 1U);
  942. /* Read PLLSAIQ value from PLLSAICFGR register (this value is not need for SAI configuration) */
  943. pllsaiq = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  944. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  945. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  946. /* LTDC_CLK(first level) = PLLSAI_VCO Output/PLLSAIR */
  947. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN, pllsaip, pllsaiq, PeriphClkInit->PLLSAI.PLLSAIR);
  948. /* LTDC_CLK = LTDC_CLK(first level)/PLLSAIDIVR */
  949. __HAL_RCC_PLLSAI_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLSAIDivR);
  950. }
  951. /*---------------------------- CLK48 configuration ------------------------*/
  952. /* Configure the PLLSAI when it is used as clock source for CLK48 */
  953. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == (RCC_PERIPHCLK_CLK48)) &&
  954. (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLSAIP))
  955. {
  956. assert_param(IS_RCC_PLLSAIP_VALUE(PeriphClkInit->PLLSAI.PLLSAIP));
  957. /* Read PLLSAIQ value from PLLSAICFGR register (this value is not need for SAI configuration) */
  958. pllsaiq = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  959. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  960. pllsair = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> RCC_PLLSAICFGR_PLLSAIR_Pos);
  961. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  962. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  963. /* CLK48_CLK(first level) = PLLSAI_VCO Output/PLLSAIP */
  964. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN, PeriphClkInit->PLLSAI.PLLSAIP, pllsaiq, pllsair);
  965. }
  966. /* Enable PLLSAI Clock */
  967. __HAL_RCC_PLLSAI_ENABLE();
  968. /* Get tick */
  969. tickstart = HAL_GetTick();
  970. /* Wait till PLLSAI is ready */
  971. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  972. {
  973. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  974. {
  975. /* return in case of Timeout detected */
  976. return HAL_TIMEOUT;
  977. }
  978. }
  979. }
  980. /*--------------------------------------------------------------------------*/
  981. /*---------------------------- RTC configuration ---------------------------*/
  982. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  983. {
  984. /* Check for RTC Parameters used to output RTCCLK */
  985. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  986. /* Enable Power Clock*/
  987. __HAL_RCC_PWR_CLK_ENABLE();
  988. /* Enable write access to Backup domain */
  989. PWR->CR |= PWR_CR_DBP;
  990. /* Get tick */
  991. tickstart = HAL_GetTick();
  992. while((PWR->CR & PWR_CR_DBP) == RESET)
  993. {
  994. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  995. {
  996. return HAL_TIMEOUT;
  997. }
  998. }
  999. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  1000. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  1001. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  1002. {
  1003. /* Store the content of BDCR register before the reset of Backup Domain */
  1004. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  1005. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  1006. __HAL_RCC_BACKUPRESET_FORCE();
  1007. __HAL_RCC_BACKUPRESET_RELEASE();
  1008. /* Restore the Content of BDCR register */
  1009. RCC->BDCR = tmpreg1;
  1010. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  1011. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  1012. {
  1013. /* Get tick */
  1014. tickstart = HAL_GetTick();
  1015. /* Wait till LSE is ready */
  1016. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  1017. {
  1018. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  1019. {
  1020. return HAL_TIMEOUT;
  1021. }
  1022. }
  1023. }
  1024. }
  1025. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  1026. }
  1027. /*--------------------------------------------------------------------------*/
  1028. /*---------------------------- TIM configuration ---------------------------*/
  1029. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  1030. {
  1031. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  1032. }
  1033. return HAL_OK;
  1034. }
  1035. /**
  1036. * @brief Configures the RCC_PeriphCLKInitTypeDef according to the internal
  1037. * RCC configuration registers.
  1038. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1039. * will be configured.
  1040. * @retval None
  1041. */
  1042. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1043. {
  1044. uint32_t tempreg;
  1045. /* Set all possible values for the extended clock type parameter------------*/
  1046. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S | RCC_PERIPHCLK_SAI_PLLSAI |\
  1047. RCC_PERIPHCLK_SAI_PLLI2S | RCC_PERIPHCLK_LTDC |\
  1048. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  1049. RCC_PERIPHCLK_CLK48 | RCC_PERIPHCLK_SDIO;
  1050. /* Get the PLLI2S Clock configuration --------------------------------------*/
  1051. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> RCC_PLLI2SCFGR_PLLI2SN_Pos);
  1052. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  1053. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  1054. /* Get the PLLSAI Clock configuration --------------------------------------*/
  1055. PeriphClkInit->PLLSAI.PLLSAIN = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> RCC_PLLSAICFGR_PLLSAIN_Pos);
  1056. PeriphClkInit->PLLSAI.PLLSAIR = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> RCC_PLLSAICFGR_PLLSAIR_Pos);
  1057. PeriphClkInit->PLLSAI.PLLSAIQ = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  1058. /* Get the PLLSAI/PLLI2S division factors ----------------------------------*/
  1059. PeriphClkInit->PLLI2SDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) >> RCC_DCKCFGR_PLLI2SDIVQ_Pos);
  1060. PeriphClkInit->PLLSAIDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> RCC_DCKCFGR_PLLSAIDIVQ_Pos);
  1061. PeriphClkInit->PLLSAIDivR = (uint32_t)(RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVR);
  1062. /* Get the RTC Clock configuration -----------------------------------------*/
  1063. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  1064. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  1065. /* Get the CLK48 clock configuration -------------------------------------*/
  1066. PeriphClkInit->Clk48ClockSelection = __HAL_RCC_GET_CLK48_SOURCE();
  1067. /* Get the SDIO clock configuration ----------------------------------------*/
  1068. PeriphClkInit->SdioClockSelection = __HAL_RCC_GET_SDIO_SOURCE();
  1069. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  1070. {
  1071. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1072. }
  1073. else
  1074. {
  1075. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1076. }
  1077. }
  1078. /**
  1079. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  1080. * @note Return 0 if peripheral clock identifier not managed by this API
  1081. * @param PeriphClk Peripheral clock identifier
  1082. * This parameter can be one of the following values:
  1083. * @arg RCC_PERIPHCLK_I2S: I2S peripheral clock
  1084. * @retval Frequency in KHz
  1085. */
  1086. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  1087. {
  1088. /* This variable used to store the I2S clock frequency (value in Hz) */
  1089. uint32_t frequency = 0U;
  1090. /* This variable used to store the VCO Input (value in Hz) */
  1091. uint32_t vcoinput = 0U;
  1092. uint32_t srcclk = 0U;
  1093. /* This variable used to store the VCO Output (value in Hz) */
  1094. uint32_t vcooutput = 0U;
  1095. switch (PeriphClk)
  1096. {
  1097. case RCC_PERIPHCLK_I2S:
  1098. {
  1099. /* Get the current I2S source */
  1100. srcclk = __HAL_RCC_GET_I2S_SOURCE();
  1101. switch (srcclk)
  1102. {
  1103. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  1104. case RCC_I2SCLKSOURCE_EXT:
  1105. {
  1106. /* Set the I2S clock to the external clock value */
  1107. frequency = EXTERNAL_CLOCK_VALUE;
  1108. break;
  1109. }
  1110. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  1111. case RCC_I2SCLKSOURCE_PLLI2S:
  1112. {
  1113. /* Configure the PLLI2S division factor */
  1114. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  1115. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1116. {
  1117. /* Get the I2S source clock value */
  1118. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1119. }
  1120. else
  1121. {
  1122. /* Get the I2S source clock value */
  1123. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1124. }
  1125. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  1126. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  1127. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  1128. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  1129. break;
  1130. }
  1131. /* Clock not enabled for I2S*/
  1132. default:
  1133. {
  1134. frequency = 0U;
  1135. break;
  1136. }
  1137. }
  1138. break;
  1139. }
  1140. }
  1141. return frequency;
  1142. }
  1143. #endif /* STM32F469xx || STM32F479xx */
  1144. #if defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  1145. /**
  1146. * @brief Initializes the RCC extended peripherals clocks according to the specified
  1147. * parameters in the RCC_PeriphCLKInitTypeDef.
  1148. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1149. * contains the configuration information for the Extended Peripherals
  1150. * clocks(I2S, LTDC RTC and TIM).
  1151. *
  1152. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  1153. * the RTC clock source; in this case the Backup domain will be reset in
  1154. * order to modify the RTC Clock source, as consequence RTC registers (including
  1155. * the backup registers) and RCC_BDCR register are set to their reset values.
  1156. *
  1157. * @retval HAL status
  1158. */
  1159. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1160. {
  1161. uint32_t tickstart = 0U;
  1162. uint32_t tmpreg1 = 0U;
  1163. #if defined(STM32F413xx) || defined(STM32F423xx)
  1164. uint32_t plli2sq = 0U;
  1165. #endif /* STM32F413xx || STM32F423xx */
  1166. uint32_t plli2sused = 0U;
  1167. /* Check the peripheral clock selection parameters */
  1168. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  1169. /*----------------------------------- I2S APB1 configuration ---------------*/
  1170. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == (RCC_PERIPHCLK_I2S_APB1))
  1171. {
  1172. /* Check the parameters */
  1173. assert_param(IS_RCC_I2SAPB1CLKSOURCE(PeriphClkInit->I2sApb1ClockSelection));
  1174. /* Configure I2S Clock source */
  1175. __HAL_RCC_I2S_APB1_CONFIG(PeriphClkInit->I2sApb1ClockSelection);
  1176. /* Enable the PLLI2S when it's used as clock source for I2S */
  1177. if(PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)
  1178. {
  1179. plli2sused = 1U;
  1180. }
  1181. }
  1182. /*--------------------------------------------------------------------------*/
  1183. /*----------------------------------- I2S APB2 configuration ---------------*/
  1184. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == (RCC_PERIPHCLK_I2S_APB2))
  1185. {
  1186. /* Check the parameters */
  1187. assert_param(IS_RCC_I2SAPB2CLKSOURCE(PeriphClkInit->I2sApb2ClockSelection));
  1188. /* Configure I2S Clock source */
  1189. __HAL_RCC_I2S_APB2_CONFIG(PeriphClkInit->I2sApb2ClockSelection);
  1190. /* Enable the PLLI2S when it's used as clock source for I2S */
  1191. if(PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)
  1192. {
  1193. plli2sused = 1U;
  1194. }
  1195. }
  1196. /*--------------------------------------------------------------------------*/
  1197. #if defined(STM32F413xx) || defined(STM32F423xx)
  1198. /*----------------------- SAI1 Block A configuration -----------------------*/
  1199. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAIA) == (RCC_PERIPHCLK_SAIA))
  1200. {
  1201. /* Check the parameters */
  1202. assert_param(IS_RCC_SAIACLKSOURCE(PeriphClkInit->SaiAClockSelection));
  1203. /* Configure SAI1 Clock source */
  1204. __HAL_RCC_SAI_BLOCKACLKSOURCE_CONFIG(PeriphClkInit->SaiAClockSelection);
  1205. /* Enable the PLLI2S when it's used as clock source for SAI */
  1206. if(PeriphClkInit->SaiAClockSelection == RCC_SAIACLKSOURCE_PLLI2SR)
  1207. {
  1208. plli2sused = 1U;
  1209. }
  1210. /* Enable the PLLSAI when it's used as clock source for SAI */
  1211. if(PeriphClkInit->SaiAClockSelection == RCC_SAIACLKSOURCE_PLLR)
  1212. {
  1213. /* Check for PLL/DIVR parameters */
  1214. assert_param(IS_RCC_PLL_DIVR_VALUE(PeriphClkInit->PLLDivR));
  1215. /* SAI_CLK_x = SAI_CLK(first level)/PLLDIVR */
  1216. __HAL_RCC_PLL_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLDivR);
  1217. }
  1218. }
  1219. /*--------------------------------------------------------------------------*/
  1220. /*---------------------- SAI1 Block B configuration ------------------------*/
  1221. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAIB) == (RCC_PERIPHCLK_SAIB))
  1222. {
  1223. /* Check the parameters */
  1224. assert_param(IS_RCC_SAIBCLKSOURCE(PeriphClkInit->SaiBClockSelection));
  1225. /* Configure SAI1 Clock source */
  1226. __HAL_RCC_SAI_BLOCKBCLKSOURCE_CONFIG(PeriphClkInit->SaiBClockSelection);
  1227. /* Enable the PLLI2S when it's used as clock source for SAI */
  1228. if(PeriphClkInit->SaiBClockSelection == RCC_SAIBCLKSOURCE_PLLI2SR)
  1229. {
  1230. plli2sused = 1U;
  1231. }
  1232. /* Enable the PLLSAI when it's used as clock source for SAI */
  1233. if(PeriphClkInit->SaiBClockSelection == RCC_SAIBCLKSOURCE_PLLR)
  1234. {
  1235. /* Check for PLL/DIVR parameters */
  1236. assert_param(IS_RCC_PLL_DIVR_VALUE(PeriphClkInit->PLLDivR));
  1237. /* SAI_CLK_x = SAI_CLK(first level)/PLLDIVR */
  1238. __HAL_RCC_PLL_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLDivR);
  1239. }
  1240. }
  1241. /*--------------------------------------------------------------------------*/
  1242. #endif /* STM32F413xx || STM32F423xx */
  1243. /*------------------------------------ RTC configuration -------------------*/
  1244. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  1245. {
  1246. /* Check for RTC Parameters used to output RTCCLK */
  1247. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  1248. /* Enable Power Clock*/
  1249. __HAL_RCC_PWR_CLK_ENABLE();
  1250. /* Enable write access to Backup domain */
  1251. PWR->CR |= PWR_CR_DBP;
  1252. /* Get tick */
  1253. tickstart = HAL_GetTick();
  1254. while((PWR->CR & PWR_CR_DBP) == RESET)
  1255. {
  1256. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  1257. {
  1258. return HAL_TIMEOUT;
  1259. }
  1260. }
  1261. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  1262. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  1263. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  1264. {
  1265. /* Store the content of BDCR register before the reset of Backup Domain */
  1266. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  1267. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  1268. __HAL_RCC_BACKUPRESET_FORCE();
  1269. __HAL_RCC_BACKUPRESET_RELEASE();
  1270. /* Restore the Content of BDCR register */
  1271. RCC->BDCR = tmpreg1;
  1272. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  1273. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  1274. {
  1275. /* Get tick */
  1276. tickstart = HAL_GetTick();
  1277. /* Wait till LSE is ready */
  1278. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  1279. {
  1280. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  1281. {
  1282. return HAL_TIMEOUT;
  1283. }
  1284. }
  1285. }
  1286. }
  1287. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  1288. }
  1289. /*--------------------------------------------------------------------------*/
  1290. /*------------------------------------ TIM configuration -------------------*/
  1291. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  1292. {
  1293. /* Configure Timer Prescaler */
  1294. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  1295. }
  1296. /*--------------------------------------------------------------------------*/
  1297. /*------------------------------------- FMPI2C1 Configuration --------------*/
  1298. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_FMPI2C1) == RCC_PERIPHCLK_FMPI2C1)
  1299. {
  1300. /* Check the parameters */
  1301. assert_param(IS_RCC_FMPI2C1CLKSOURCE(PeriphClkInit->Fmpi2c1ClockSelection));
  1302. /* Configure the FMPI2C1 clock source */
  1303. __HAL_RCC_FMPI2C1_CONFIG(PeriphClkInit->Fmpi2c1ClockSelection);
  1304. }
  1305. /*--------------------------------------------------------------------------*/
  1306. /*------------------------------------- CLK48 Configuration ----------------*/
  1307. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48)
  1308. {
  1309. /* Check the parameters */
  1310. assert_param(IS_RCC_CLK48CLKSOURCE(PeriphClkInit->Clk48ClockSelection));
  1311. /* Configure the SDIO clock source */
  1312. __HAL_RCC_CLK48_CONFIG(PeriphClkInit->Clk48ClockSelection);
  1313. /* Enable the PLLI2S when it's used as clock source for CLK48 */
  1314. if(PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLI2SQ)
  1315. {
  1316. plli2sused = 1U;
  1317. }
  1318. }
  1319. /*--------------------------------------------------------------------------*/
  1320. /*------------------------------------- SDIO Configuration -----------------*/
  1321. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO)
  1322. {
  1323. /* Check the parameters */
  1324. assert_param(IS_RCC_SDIOCLKSOURCE(PeriphClkInit->SdioClockSelection));
  1325. /* Configure the SDIO clock source */
  1326. __HAL_RCC_SDIO_CONFIG(PeriphClkInit->SdioClockSelection);
  1327. }
  1328. /*--------------------------------------------------------------------------*/
  1329. /*-------------------------------------- PLLI2S Configuration --------------*/
  1330. /* PLLI2S is configured when a peripheral will use it as source clock : I2S on APB1 or
  1331. I2S on APB2*/
  1332. if((plli2sused == 1U) || (PeriphClkInit->PeriphClockSelection == RCC_PERIPHCLK_PLLI2S))
  1333. {
  1334. /* Disable the PLLI2S */
  1335. __HAL_RCC_PLLI2S_DISABLE();
  1336. /* Get tick */
  1337. tickstart = HAL_GetTick();
  1338. /* Wait till PLLI2S is disabled */
  1339. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  1340. {
  1341. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1342. {
  1343. /* return in case of Timeout detected */
  1344. return HAL_TIMEOUT;
  1345. }
  1346. }
  1347. /* check for common PLLI2S Parameters */
  1348. assert_param(IS_RCC_PLLI2SCLKSOURCE(PeriphClkInit->PLLI2SSelection));
  1349. assert_param(IS_RCC_PLLI2SM_VALUE(PeriphClkInit->PLLI2S.PLLI2SM));
  1350. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  1351. /*-------------------- Set the PLL I2S clock -----------------------------*/
  1352. __HAL_RCC_PLL_I2S_CONFIG(PeriphClkInit->PLLI2SSelection);
  1353. /*------- In Case of PLLI2S is selected as source clock for I2S ----------*/
  1354. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB1) == RCC_PERIPHCLK_I2S_APB1) && (PeriphClkInit->I2sApb1ClockSelection == RCC_I2SAPB1CLKSOURCE_PLLI2S)) ||
  1355. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S_APB2) == RCC_PERIPHCLK_I2S_APB2) && (PeriphClkInit->I2sApb2ClockSelection == RCC_I2SAPB2CLKSOURCE_PLLI2S)) ||
  1356. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CLK48) == RCC_PERIPHCLK_CLK48) && (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLI2SQ)) ||
  1357. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDIO) == RCC_PERIPHCLK_SDIO) && (PeriphClkInit->SdioClockSelection == RCC_SDIOCLKSOURCE_CLK48) && (PeriphClkInit->Clk48ClockSelection == RCC_CLK48CLKSOURCE_PLLI2SQ)))
  1358. {
  1359. /* check for Parameters */
  1360. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1361. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  1362. /* Configure the PLLI2S division factors */
  1363. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM)*/
  1364. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  1365. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  1366. }
  1367. #if defined(STM32F413xx) || defined(STM32F423xx)
  1368. /*------- In Case of PLLI2S is selected as source clock for SAI ----------*/
  1369. if(((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAIA) == RCC_PERIPHCLK_SAIA) && (PeriphClkInit->SaiAClockSelection == RCC_SAIACLKSOURCE_PLLI2SR)) ||
  1370. ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAIB) == RCC_PERIPHCLK_SAIB) && (PeriphClkInit->SaiBClockSelection == RCC_SAIBCLKSOURCE_PLLI2SR)))
  1371. {
  1372. /* Check for PLLI2S Parameters */
  1373. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1374. /* Check for PLLI2S/DIVR parameters */
  1375. assert_param(IS_RCC_PLLI2S_DIVR_VALUE(PeriphClkInit->PLLI2SDivR));
  1376. /* Read PLLI2SQ value from PLLI2SCFGR register (this value is not needed for SAI configuration) */
  1377. plli2sq = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  1378. /* Configure the PLLI2S division factors */
  1379. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  1380. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  1381. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  1382. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN, plli2sq, PeriphClkInit->PLLI2S.PLLI2SR);
  1383. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVR */
  1384. __HAL_RCC_PLLI2S_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLI2SDivR);
  1385. }
  1386. #endif /* STM32F413xx || STM32F423xx */
  1387. /*----------------- In Case of PLLI2S is just selected ------------------*/
  1388. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S)
  1389. {
  1390. /* Check for Parameters */
  1391. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1392. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  1393. /* Configure the PLLI2S division factors */
  1394. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM)*/
  1395. /* SPDIFRXCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SP */
  1396. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  1397. }
  1398. /* Enable the PLLI2S */
  1399. __HAL_RCC_PLLI2S_ENABLE();
  1400. /* Get tick */
  1401. tickstart = HAL_GetTick();
  1402. /* Wait till PLLI2S is ready */
  1403. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  1404. {
  1405. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1406. {
  1407. /* return in case of Timeout detected */
  1408. return HAL_TIMEOUT;
  1409. }
  1410. }
  1411. }
  1412. /*--------------------------------------------------------------------------*/
  1413. /*-------------------- DFSDM1 clock source configuration -------------------*/
  1414. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM1) == RCC_PERIPHCLK_DFSDM1)
  1415. {
  1416. /* Check the parameters */
  1417. assert_param(IS_RCC_DFSDM1CLKSOURCE(PeriphClkInit->Dfsdm1ClockSelection));
  1418. /* Configure the DFSDM1 interface clock source */
  1419. __HAL_RCC_DFSDM1_CONFIG(PeriphClkInit->Dfsdm1ClockSelection);
  1420. }
  1421. /*--------------------------------------------------------------------------*/
  1422. /*-------------------- DFSDM1 Audio clock source configuration -------------*/
  1423. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM1_AUDIO) == RCC_PERIPHCLK_DFSDM1_AUDIO)
  1424. {
  1425. /* Check the parameters */
  1426. assert_param(IS_RCC_DFSDM1AUDIOCLKSOURCE(PeriphClkInit->Dfsdm1AudioClockSelection));
  1427. /* Configure the DFSDM1 Audio interface clock source */
  1428. __HAL_RCC_DFSDM1AUDIO_CONFIG(PeriphClkInit->Dfsdm1AudioClockSelection);
  1429. }
  1430. /*--------------------------------------------------------------------------*/
  1431. #if defined(STM32F413xx) || defined(STM32F423xx)
  1432. /*-------------------- DFSDM2 clock source configuration -------------------*/
  1433. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM2) == RCC_PERIPHCLK_DFSDM2)
  1434. {
  1435. /* Check the parameters */
  1436. assert_param(IS_RCC_DFSDM2CLKSOURCE(PeriphClkInit->Dfsdm2ClockSelection));
  1437. /* Configure the DFSDM1 interface clock source */
  1438. __HAL_RCC_DFSDM2_CONFIG(PeriphClkInit->Dfsdm2ClockSelection);
  1439. }
  1440. /*--------------------------------------------------------------------------*/
  1441. /*-------------------- DFSDM2 Audio clock source configuration -------------*/
  1442. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_DFSDM2_AUDIO) == RCC_PERIPHCLK_DFSDM2_AUDIO)
  1443. {
  1444. /* Check the parameters */
  1445. assert_param(IS_RCC_DFSDM2AUDIOCLKSOURCE(PeriphClkInit->Dfsdm2AudioClockSelection));
  1446. /* Configure the DFSDM1 Audio interface clock source */
  1447. __HAL_RCC_DFSDM2AUDIO_CONFIG(PeriphClkInit->Dfsdm2AudioClockSelection);
  1448. }
  1449. /*--------------------------------------------------------------------------*/
  1450. /*---------------------------- LPTIM1 Configuration ------------------------*/
  1451. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == RCC_PERIPHCLK_LPTIM1)
  1452. {
  1453. /* Check the parameters */
  1454. assert_param(IS_RCC_LPTIM1CLKSOURCE(PeriphClkInit->Lptim1ClockSelection));
  1455. /* Configure the LPTIM1 clock source */
  1456. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->Lptim1ClockSelection);
  1457. }
  1458. /*--------------------------------------------------------------------------*/
  1459. #endif /* STM32F413xx || STM32F423xx */
  1460. return HAL_OK;
  1461. }
  1462. /**
  1463. * @brief Get the RCC_PeriphCLKInitTypeDef according to the internal
  1464. * RCC configuration registers.
  1465. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1466. * will be configured.
  1467. * @retval None
  1468. */
  1469. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1470. {
  1471. uint32_t tempreg;
  1472. /* Set all possible values for the extended clock type parameter------------*/
  1473. #if defined(STM32F413xx) || defined(STM32F423xx)
  1474. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_I2S_APB2 |\
  1475. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  1476. RCC_PERIPHCLK_FMPI2C1 | RCC_PERIPHCLK_CLK48 |\
  1477. RCC_PERIPHCLK_SDIO | RCC_PERIPHCLK_DFSDM1 |\
  1478. RCC_PERIPHCLK_DFSDM1_AUDIO | RCC_PERIPHCLK_DFSDM2 |\
  1479. RCC_PERIPHCLK_DFSDM2_AUDIO | RCC_PERIPHCLK_LPTIM1 |\
  1480. RCC_PERIPHCLK_SAIA | RCC_PERIPHCLK_SAIB;
  1481. #else /* STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx */
  1482. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S_APB1 | RCC_PERIPHCLK_I2S_APB2 |\
  1483. RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC |\
  1484. RCC_PERIPHCLK_FMPI2C1 | RCC_PERIPHCLK_CLK48 |\
  1485. RCC_PERIPHCLK_SDIO | RCC_PERIPHCLK_DFSDM1 |\
  1486. RCC_PERIPHCLK_DFSDM1_AUDIO;
  1487. #endif /* STM32F413xx || STM32F423xx */
  1488. /* Get the PLLI2S Clock configuration --------------------------------------*/
  1489. PeriphClkInit->PLLI2S.PLLI2SM = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM) >> RCC_PLLI2SCFGR_PLLI2SM_Pos);
  1490. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> RCC_PLLI2SCFGR_PLLI2SN_Pos);
  1491. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  1492. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  1493. #if defined(STM32F413xx) || defined(STM32F423xx)
  1494. /* Get the PLL/PLLI2S division factors -------------------------------------*/
  1495. PeriphClkInit->PLLI2SDivR = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVR) >> RCC_DCKCFGR_PLLI2SDIVR_Pos);
  1496. PeriphClkInit->PLLDivR = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLDIVR) >> RCC_DCKCFGR_PLLDIVR_Pos);
  1497. #endif /* STM32F413xx || STM32F423xx */
  1498. /* Get the I2S APB1 clock configuration ------------------------------------*/
  1499. PeriphClkInit->I2sApb1ClockSelection = __HAL_RCC_GET_I2S_APB1_SOURCE();
  1500. /* Get the I2S APB2 clock configuration ------------------------------------*/
  1501. PeriphClkInit->I2sApb2ClockSelection = __HAL_RCC_GET_I2S_APB2_SOURCE();
  1502. /* Get the RTC Clock configuration -----------------------------------------*/
  1503. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  1504. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  1505. /* Get the FMPI2C1 clock configuration -------------------------------------*/
  1506. PeriphClkInit->Fmpi2c1ClockSelection = __HAL_RCC_GET_FMPI2C1_SOURCE();
  1507. /* Get the CLK48 clock configuration ---------------------------------------*/
  1508. PeriphClkInit->Clk48ClockSelection = __HAL_RCC_GET_CLK48_SOURCE();
  1509. /* Get the SDIO clock configuration ----------------------------------------*/
  1510. PeriphClkInit->SdioClockSelection = __HAL_RCC_GET_SDIO_SOURCE();
  1511. /* Get the DFSDM1 clock configuration --------------------------------------*/
  1512. PeriphClkInit->Dfsdm1ClockSelection = __HAL_RCC_GET_DFSDM1_SOURCE();
  1513. /* Get the DFSDM1 Audio clock configuration --------------------------------*/
  1514. PeriphClkInit->Dfsdm1AudioClockSelection = __HAL_RCC_GET_DFSDM1AUDIO_SOURCE();
  1515. #if defined(STM32F413xx) || defined(STM32F423xx)
  1516. /* Get the DFSDM2 clock configuration --------------------------------------*/
  1517. PeriphClkInit->Dfsdm2ClockSelection = __HAL_RCC_GET_DFSDM2_SOURCE();
  1518. /* Get the DFSDM2 Audio clock configuration --------------------------------*/
  1519. PeriphClkInit->Dfsdm2AudioClockSelection = __HAL_RCC_GET_DFSDM2AUDIO_SOURCE();
  1520. /* Get the LPTIM1 clock configuration --------------------------------------*/
  1521. PeriphClkInit->Lptim1ClockSelection = __HAL_RCC_GET_LPTIM1_SOURCE();
  1522. /* Get the SAI1 Block Aclock configuration ---------------------------------*/
  1523. PeriphClkInit->SaiAClockSelection = __HAL_RCC_GET_SAI_BLOCKA_SOURCE();
  1524. /* Get the SAI1 Block B clock configuration --------------------------------*/
  1525. PeriphClkInit->SaiBClockSelection = __HAL_RCC_GET_SAI_BLOCKB_SOURCE();
  1526. #endif /* STM32F413xx || STM32F423xx */
  1527. /* Get the TIM Prescaler configuration -------------------------------------*/
  1528. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  1529. {
  1530. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1531. }
  1532. else
  1533. {
  1534. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1535. }
  1536. }
  1537. /**
  1538. * @brief Return the peripheral clock frequency for a given peripheral(I2S..)
  1539. * @note Return 0 if peripheral clock identifier not managed by this API
  1540. * @param PeriphClk Peripheral clock identifier
  1541. * This parameter can be one of the following values:
  1542. * @arg RCC_PERIPHCLK_I2S_APB1: I2S APB1 peripheral clock
  1543. * @arg RCC_PERIPHCLK_I2S_APB2: I2S APB2 peripheral clock
  1544. * @retval Frequency in KHz
  1545. */
  1546. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  1547. {
  1548. /* This variable used to store the I2S clock frequency (value in Hz) */
  1549. uint32_t frequency = 0U;
  1550. /* This variable used to store the VCO Input (value in Hz) */
  1551. uint32_t vcoinput = 0U;
  1552. uint32_t srcclk = 0U;
  1553. /* This variable used to store the VCO Output (value in Hz) */
  1554. uint32_t vcooutput = 0U;
  1555. switch (PeriphClk)
  1556. {
  1557. case RCC_PERIPHCLK_I2S_APB1:
  1558. {
  1559. /* Get the current I2S source */
  1560. srcclk = __HAL_RCC_GET_I2S_APB1_SOURCE();
  1561. switch (srcclk)
  1562. {
  1563. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  1564. case RCC_I2SAPB1CLKSOURCE_EXT:
  1565. {
  1566. /* Set the I2S clock to the external clock value */
  1567. frequency = EXTERNAL_CLOCK_VALUE;
  1568. break;
  1569. }
  1570. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  1571. case RCC_I2SAPB1CLKSOURCE_PLLI2S:
  1572. {
  1573. if((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SSRC) == RCC_PLLI2SCFGR_PLLI2SSRC)
  1574. {
  1575. /* Get the I2S source clock value */
  1576. vcoinput = (uint32_t)(EXTERNAL_CLOCK_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1577. }
  1578. else
  1579. {
  1580. /* Configure the PLLI2S division factor */
  1581. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  1582. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1583. {
  1584. /* Get the I2S source clock value */
  1585. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1586. }
  1587. else
  1588. {
  1589. /* Get the I2S source clock value */
  1590. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1591. }
  1592. }
  1593. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  1594. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  1595. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  1596. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  1597. break;
  1598. }
  1599. /* Check if I2S clock selection is PLL VCO Output divided by PLLR used as I2S clock */
  1600. case RCC_I2SAPB1CLKSOURCE_PLLR:
  1601. {
  1602. /* Configure the PLL division factor R */
  1603. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  1604. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1605. {
  1606. /* Get the I2S source clock value */
  1607. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1608. }
  1609. else
  1610. {
  1611. /* Get the I2S source clock value */
  1612. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1613. }
  1614. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  1615. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U) & (RCC_PLLCFGR_PLLN >> 6U)));
  1616. /* I2S_CLK = PLL_VCO Output/PLLR */
  1617. frequency = (uint32_t)(vcooutput /(((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U) & (RCC_PLLCFGR_PLLR >> 28U)));
  1618. break;
  1619. }
  1620. /* Check if I2S clock selection is HSI or HSE depending from PLL source Clock */
  1621. case RCC_I2SAPB1CLKSOURCE_PLLSRC:
  1622. {
  1623. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1624. {
  1625. frequency = HSE_VALUE;
  1626. }
  1627. else
  1628. {
  1629. frequency = HSI_VALUE;
  1630. }
  1631. break;
  1632. }
  1633. /* Clock not enabled for I2S*/
  1634. default:
  1635. {
  1636. frequency = 0U;
  1637. break;
  1638. }
  1639. }
  1640. break;
  1641. }
  1642. case RCC_PERIPHCLK_I2S_APB2:
  1643. {
  1644. /* Get the current I2S source */
  1645. srcclk = __HAL_RCC_GET_I2S_APB2_SOURCE();
  1646. switch (srcclk)
  1647. {
  1648. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  1649. case RCC_I2SAPB2CLKSOURCE_EXT:
  1650. {
  1651. /* Set the I2S clock to the external clock value */
  1652. frequency = EXTERNAL_CLOCK_VALUE;
  1653. break;
  1654. }
  1655. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  1656. case RCC_I2SAPB2CLKSOURCE_PLLI2S:
  1657. {
  1658. if((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SSRC) == RCC_PLLI2SCFGR_PLLI2SSRC)
  1659. {
  1660. /* Get the I2S source clock value */
  1661. vcoinput = (uint32_t)(EXTERNAL_CLOCK_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1662. }
  1663. else
  1664. {
  1665. /* Configure the PLLI2S division factor */
  1666. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  1667. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1668. {
  1669. /* Get the I2S source clock value */
  1670. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1671. }
  1672. else
  1673. {
  1674. /* Get the I2S source clock value */
  1675. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  1676. }
  1677. }
  1678. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  1679. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  1680. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  1681. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  1682. break;
  1683. }
  1684. /* Check if I2S clock selection is PLL VCO Output divided by PLLR used as I2S clock */
  1685. case RCC_I2SAPB2CLKSOURCE_PLLR:
  1686. {
  1687. /* Configure the PLL division factor R */
  1688. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  1689. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1690. {
  1691. /* Get the I2S source clock value */
  1692. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1693. }
  1694. else
  1695. {
  1696. /* Get the I2S source clock value */
  1697. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1698. }
  1699. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  1700. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U) & (RCC_PLLCFGR_PLLN >> 6U)));
  1701. /* I2S_CLK = PLL_VCO Output/PLLR */
  1702. frequency = (uint32_t)(vcooutput /(((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U) & (RCC_PLLCFGR_PLLR >> 28U)));
  1703. break;
  1704. }
  1705. /* Check if I2S clock selection is HSI or HSE depending from PLL source Clock */
  1706. case RCC_I2SAPB2CLKSOURCE_PLLSRC:
  1707. {
  1708. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1709. {
  1710. frequency = HSE_VALUE;
  1711. }
  1712. else
  1713. {
  1714. frequency = HSI_VALUE;
  1715. }
  1716. break;
  1717. }
  1718. /* Clock not enabled for I2S*/
  1719. default:
  1720. {
  1721. frequency = 0U;
  1722. break;
  1723. }
  1724. }
  1725. break;
  1726. }
  1727. }
  1728. return frequency;
  1729. }
  1730. #endif /* STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  1731. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx)
  1732. /**
  1733. * @brief Initializes the RCC extended peripherals clocks according to the specified parameters in the
  1734. * RCC_PeriphCLKInitTypeDef.
  1735. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1736. * contains the configuration information for the Extended Peripherals clocks(I2S and RTC clocks).
  1737. *
  1738. * @note A caution to be taken when HAL_RCCEx_PeriphCLKConfig() is used to select RTC clock selection, in this case
  1739. * the Reset of Backup domain will be applied in order to modify the RTC Clock source as consequence all backup
  1740. * domain (RTC and RCC_BDCR register expect BKPSRAM) will be reset
  1741. *
  1742. * @retval HAL status
  1743. */
  1744. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1745. {
  1746. uint32_t tickstart = 0U;
  1747. uint32_t tmpreg1 = 0U;
  1748. /* Check the parameters */
  1749. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  1750. /*---------------------------- RTC configuration ---------------------------*/
  1751. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  1752. {
  1753. /* Check for RTC Parameters used to output RTCCLK */
  1754. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  1755. /* Enable Power Clock*/
  1756. __HAL_RCC_PWR_CLK_ENABLE();
  1757. /* Enable write access to Backup domain */
  1758. PWR->CR |= PWR_CR_DBP;
  1759. /* Get tick */
  1760. tickstart = HAL_GetTick();
  1761. while((PWR->CR & PWR_CR_DBP) == RESET)
  1762. {
  1763. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  1764. {
  1765. return HAL_TIMEOUT;
  1766. }
  1767. }
  1768. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  1769. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  1770. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  1771. {
  1772. /* Store the content of BDCR register before the reset of Backup Domain */
  1773. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  1774. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  1775. __HAL_RCC_BACKUPRESET_FORCE();
  1776. __HAL_RCC_BACKUPRESET_RELEASE();
  1777. /* Restore the Content of BDCR register */
  1778. RCC->BDCR = tmpreg1;
  1779. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  1780. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  1781. {
  1782. /* Get tick */
  1783. tickstart = HAL_GetTick();
  1784. /* Wait till LSE is ready */
  1785. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  1786. {
  1787. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  1788. {
  1789. return HAL_TIMEOUT;
  1790. }
  1791. }
  1792. }
  1793. }
  1794. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  1795. }
  1796. /*--------------------------------------------------------------------------*/
  1797. /*---------------------------- TIM configuration ---------------------------*/
  1798. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  1799. {
  1800. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  1801. }
  1802. /*--------------------------------------------------------------------------*/
  1803. /*---------------------------- FMPI2C1 Configuration -----------------------*/
  1804. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_FMPI2C1) == RCC_PERIPHCLK_FMPI2C1)
  1805. {
  1806. /* Check the parameters */
  1807. assert_param(IS_RCC_FMPI2C1CLKSOURCE(PeriphClkInit->Fmpi2c1ClockSelection));
  1808. /* Configure the FMPI2C1 clock source */
  1809. __HAL_RCC_FMPI2C1_CONFIG(PeriphClkInit->Fmpi2c1ClockSelection);
  1810. }
  1811. /*--------------------------------------------------------------------------*/
  1812. /*---------------------------- LPTIM1 Configuration ------------------------*/
  1813. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == RCC_PERIPHCLK_LPTIM1)
  1814. {
  1815. /* Check the parameters */
  1816. assert_param(IS_RCC_LPTIM1CLKSOURCE(PeriphClkInit->Lptim1ClockSelection));
  1817. /* Configure the LPTIM1 clock source */
  1818. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->Lptim1ClockSelection);
  1819. }
  1820. /*---------------------------- I2S Configuration ---------------------------*/
  1821. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S)
  1822. {
  1823. /* Check the parameters */
  1824. assert_param(IS_RCC_I2SAPBCLKSOURCE(PeriphClkInit->I2SClockSelection));
  1825. /* Configure the I2S clock source */
  1826. __HAL_RCC_I2S_CONFIG(PeriphClkInit->I2SClockSelection);
  1827. }
  1828. return HAL_OK;
  1829. }
  1830. /**
  1831. * @brief Configures the RCC_OscInitStruct according to the internal
  1832. * RCC configuration registers.
  1833. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1834. * will be configured.
  1835. * @retval None
  1836. */
  1837. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1838. {
  1839. uint32_t tempreg;
  1840. /* Set all possible values for the extended clock type parameter------------*/
  1841. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_FMPI2C1 | RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC;
  1842. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  1843. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  1844. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  1845. {
  1846. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  1847. }
  1848. else
  1849. {
  1850. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  1851. }
  1852. /* Get the FMPI2C1 clock configuration -------------------------------------*/
  1853. PeriphClkInit->Fmpi2c1ClockSelection = __HAL_RCC_GET_FMPI2C1_SOURCE();
  1854. /* Get the I2S clock configuration -----------------------------------------*/
  1855. PeriphClkInit->I2SClockSelection = __HAL_RCC_GET_I2S_SOURCE();
  1856. }
  1857. /**
  1858. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  1859. * @note Return 0 if peripheral clock identifier not managed by this API
  1860. * @param PeriphClk Peripheral clock identifier
  1861. * This parameter can be one of the following values:
  1862. * @arg RCC_PERIPHCLK_I2S: I2S peripheral clock
  1863. * @retval Frequency in KHz
  1864. */
  1865. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  1866. {
  1867. /* This variable used to store the I2S clock frequency (value in Hz) */
  1868. uint32_t frequency = 0U;
  1869. /* This variable used to store the VCO Input (value in Hz) */
  1870. uint32_t vcoinput = 0U;
  1871. uint32_t srcclk = 0U;
  1872. /* This variable used to store the VCO Output (value in Hz) */
  1873. uint32_t vcooutput = 0U;
  1874. switch (PeriphClk)
  1875. {
  1876. case RCC_PERIPHCLK_I2S:
  1877. {
  1878. /* Get the current I2S source */
  1879. srcclk = __HAL_RCC_GET_I2S_SOURCE();
  1880. switch (srcclk)
  1881. {
  1882. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  1883. case RCC_I2SAPBCLKSOURCE_EXT:
  1884. {
  1885. /* Set the I2S clock to the external clock value */
  1886. frequency = EXTERNAL_CLOCK_VALUE;
  1887. break;
  1888. }
  1889. /* Check if I2S clock selection is PLL VCO Output divided by PLLR used as I2S clock */
  1890. case RCC_I2SAPBCLKSOURCE_PLLR:
  1891. {
  1892. /* Configure the PLL division factor R */
  1893. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  1894. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1895. {
  1896. /* Get the I2S source clock value */
  1897. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1898. }
  1899. else
  1900. {
  1901. /* Get the I2S source clock value */
  1902. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  1903. }
  1904. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  1905. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U) & (RCC_PLLCFGR_PLLN >> 6U)));
  1906. /* I2S_CLK = PLL_VCO Output/PLLR */
  1907. frequency = (uint32_t)(vcooutput /(((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U) & (RCC_PLLCFGR_PLLR >> 28U)));
  1908. break;
  1909. }
  1910. /* Check if I2S clock selection is HSI or HSE depending from PLL source Clock */
  1911. case RCC_I2SAPBCLKSOURCE_PLLSRC:
  1912. {
  1913. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  1914. {
  1915. frequency = HSE_VALUE;
  1916. }
  1917. else
  1918. {
  1919. frequency = HSI_VALUE;
  1920. }
  1921. break;
  1922. }
  1923. /* Clock not enabled for I2S*/
  1924. default:
  1925. {
  1926. frequency = 0U;
  1927. break;
  1928. }
  1929. }
  1930. break;
  1931. }
  1932. }
  1933. return frequency;
  1934. }
  1935. #endif /* STM32F410Tx || STM32F410Cx || STM32F410Rx */
  1936. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  1937. /**
  1938. * @brief Initializes the RCC extended peripherals clocks according to the specified
  1939. * parameters in the RCC_PeriphCLKInitTypeDef.
  1940. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  1941. * contains the configuration information for the Extended Peripherals
  1942. * clocks(I2S, SAI, LTDC RTC and TIM).
  1943. *
  1944. * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  1945. * the RTC clock source; in this case the Backup domain will be reset in
  1946. * order to modify the RTC Clock source, as consequence RTC registers (including
  1947. * the backup registers) and RCC_BDCR register are set to their reset values.
  1948. *
  1949. * @retval HAL status
  1950. */
  1951. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  1952. {
  1953. uint32_t tickstart = 0U;
  1954. uint32_t tmpreg1 = 0U;
  1955. /* Check the parameters */
  1956. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  1957. /*----------------------- SAI/I2S Configuration (PLLI2S) -------------------*/
  1958. /*----------------------- Common configuration SAI/I2S ---------------------*/
  1959. /* In Case of SAI or I2S Clock Configuration through PLLI2S, PLLI2SN division
  1960. factor is common parameters for both peripherals */
  1961. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S) ||
  1962. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == RCC_PERIPHCLK_SAI_PLLI2S) ||
  1963. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S))
  1964. {
  1965. /* check for Parameters */
  1966. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  1967. /* Disable the PLLI2S */
  1968. __HAL_RCC_PLLI2S_DISABLE();
  1969. /* Get tick */
  1970. tickstart = HAL_GetTick();
  1971. /* Wait till PLLI2S is disabled */
  1972. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  1973. {
  1974. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  1975. {
  1976. /* return in case of Timeout detected */
  1977. return HAL_TIMEOUT;
  1978. }
  1979. }
  1980. /*---------------------------- I2S configuration -------------------------*/
  1981. /* In Case of I2S Clock Configuration through PLLI2S, PLLI2SR must be added
  1982. only for I2S configuration */
  1983. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == (RCC_PERIPHCLK_I2S))
  1984. {
  1985. /* check for Parameters */
  1986. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  1987. /* Configure the PLLI2S division factors */
  1988. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLM) */
  1989. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  1990. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SR);
  1991. }
  1992. /*---------------------------- SAI configuration -------------------------*/
  1993. /* In Case of SAI Clock Configuration through PLLI2S, PLLI2SQ and PLLI2S_DIVQ must
  1994. be added only for SAI configuration */
  1995. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLI2S) == (RCC_PERIPHCLK_SAI_PLLI2S))
  1996. {
  1997. /* Check the PLLI2S division factors */
  1998. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  1999. assert_param(IS_RCC_PLLI2S_DIVQ_VALUE(PeriphClkInit->PLLI2SDivQ));
  2000. /* Read PLLI2SR value from PLLI2SCFGR register (this value is not need for SAI configuration) */
  2001. tmpreg1 = ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  2002. /* Configure the PLLI2S division factors */
  2003. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  2004. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  2005. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  2006. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SQ , tmpreg1);
  2007. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  2008. __HAL_RCC_PLLI2S_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLI2SDivQ);
  2009. }
  2010. /*----------------- In Case of PLLI2S is just selected -----------------*/
  2011. if((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S)
  2012. {
  2013. /* Check for Parameters */
  2014. assert_param(IS_RCC_PLLI2SQ_VALUE(PeriphClkInit->PLLI2S.PLLI2SQ));
  2015. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  2016. /* Configure the PLLI2S multiplication and division factors */
  2017. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN, PeriphClkInit->PLLI2S.PLLI2SQ, PeriphClkInit->PLLI2S.PLLI2SR);
  2018. }
  2019. /* Enable the PLLI2S */
  2020. __HAL_RCC_PLLI2S_ENABLE();
  2021. /* Get tick */
  2022. tickstart = HAL_GetTick();
  2023. /* Wait till PLLI2S is ready */
  2024. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  2025. {
  2026. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  2027. {
  2028. /* return in case of Timeout detected */
  2029. return HAL_TIMEOUT;
  2030. }
  2031. }
  2032. }
  2033. /*--------------------------------------------------------------------------*/
  2034. /*----------------------- SAI/LTDC Configuration (PLLSAI) ------------------*/
  2035. /*----------------------- Common configuration SAI/LTDC --------------------*/
  2036. /* In Case of SAI or LTDC Clock Configuration through PLLSAI, PLLSAIN division
  2037. factor is common parameters for both peripherals */
  2038. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == RCC_PERIPHCLK_SAI_PLLSAI) ||
  2039. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == RCC_PERIPHCLK_LTDC))
  2040. {
  2041. /* Check the PLLSAI division factors */
  2042. assert_param(IS_RCC_PLLSAIN_VALUE(PeriphClkInit->PLLSAI.PLLSAIN));
  2043. /* Disable PLLSAI Clock */
  2044. __HAL_RCC_PLLSAI_DISABLE();
  2045. /* Get tick */
  2046. tickstart = HAL_GetTick();
  2047. /* Wait till PLLSAI is disabled */
  2048. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  2049. {
  2050. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  2051. {
  2052. /* return in case of Timeout detected */
  2053. return HAL_TIMEOUT;
  2054. }
  2055. }
  2056. /*---------------------------- SAI configuration -------------------------*/
  2057. /* In Case of SAI Clock Configuration through PLLSAI, PLLSAIQ and PLLSAI_DIVQ must
  2058. be added only for SAI configuration */
  2059. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI_PLLSAI) == (RCC_PERIPHCLK_SAI_PLLSAI))
  2060. {
  2061. assert_param(IS_RCC_PLLSAIQ_VALUE(PeriphClkInit->PLLSAI.PLLSAIQ));
  2062. assert_param(IS_RCC_PLLSAI_DIVQ_VALUE(PeriphClkInit->PLLSAIDivQ));
  2063. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  2064. tmpreg1 = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> RCC_PLLSAICFGR_PLLSAIR_Pos);
  2065. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  2066. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  2067. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  2068. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN , PeriphClkInit->PLLSAI.PLLSAIQ, tmpreg1);
  2069. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  2070. __HAL_RCC_PLLSAI_PLLSAICLKDIVQ_CONFIG(PeriphClkInit->PLLSAIDivQ);
  2071. }
  2072. /*---------------------------- LTDC configuration ------------------------*/
  2073. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LTDC) == (RCC_PERIPHCLK_LTDC))
  2074. {
  2075. assert_param(IS_RCC_PLLSAIR_VALUE(PeriphClkInit->PLLSAI.PLLSAIR));
  2076. assert_param(IS_RCC_PLLSAI_DIVR_VALUE(PeriphClkInit->PLLSAIDivR));
  2077. /* Read PLLSAIR value from PLLSAICFGR register (this value is not need for SAI configuration) */
  2078. tmpreg1 = ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  2079. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  2080. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  2081. /* LTDC_CLK(first level) = PLLSAI_VCO Output/PLLSAIR */
  2082. __HAL_RCC_PLLSAI_CONFIG(PeriphClkInit->PLLSAI.PLLSAIN , tmpreg1, PeriphClkInit->PLLSAI.PLLSAIR);
  2083. /* LTDC_CLK = LTDC_CLK(first level)/PLLSAIDIVR */
  2084. __HAL_RCC_PLLSAI_PLLSAICLKDIVR_CONFIG(PeriphClkInit->PLLSAIDivR);
  2085. }
  2086. /* Enable PLLSAI Clock */
  2087. __HAL_RCC_PLLSAI_ENABLE();
  2088. /* Get tick */
  2089. tickstart = HAL_GetTick();
  2090. /* Wait till PLLSAI is ready */
  2091. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  2092. {
  2093. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  2094. {
  2095. /* return in case of Timeout detected */
  2096. return HAL_TIMEOUT;
  2097. }
  2098. }
  2099. }
  2100. /*--------------------------------------------------------------------------*/
  2101. /*---------------------------- RTC configuration ---------------------------*/
  2102. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  2103. {
  2104. /* Check for RTC Parameters used to output RTCCLK */
  2105. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  2106. /* Enable Power Clock*/
  2107. __HAL_RCC_PWR_CLK_ENABLE();
  2108. /* Enable write access to Backup domain */
  2109. PWR->CR |= PWR_CR_DBP;
  2110. /* Get tick */
  2111. tickstart = HAL_GetTick();
  2112. while((PWR->CR & PWR_CR_DBP) == RESET)
  2113. {
  2114. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  2115. {
  2116. return HAL_TIMEOUT;
  2117. }
  2118. }
  2119. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  2120. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  2121. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  2122. {
  2123. /* Store the content of BDCR register before the reset of Backup Domain */
  2124. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  2125. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  2126. __HAL_RCC_BACKUPRESET_FORCE();
  2127. __HAL_RCC_BACKUPRESET_RELEASE();
  2128. /* Restore the Content of BDCR register */
  2129. RCC->BDCR = tmpreg1;
  2130. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  2131. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  2132. {
  2133. /* Get tick */
  2134. tickstart = HAL_GetTick();
  2135. /* Wait till LSE is ready */
  2136. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  2137. {
  2138. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  2139. {
  2140. return HAL_TIMEOUT;
  2141. }
  2142. }
  2143. }
  2144. }
  2145. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  2146. }
  2147. /*--------------------------------------------------------------------------*/
  2148. /*---------------------------- TIM configuration ---------------------------*/
  2149. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  2150. {
  2151. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  2152. }
  2153. return HAL_OK;
  2154. }
  2155. /**
  2156. * @brief Configures the PeriphClkInit according to the internal
  2157. * RCC configuration registers.
  2158. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  2159. * will be configured.
  2160. * @retval None
  2161. */
  2162. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  2163. {
  2164. uint32_t tempreg;
  2165. /* Set all possible values for the extended clock type parameter------------*/
  2166. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S | RCC_PERIPHCLK_SAI_PLLSAI | RCC_PERIPHCLK_SAI_PLLI2S | RCC_PERIPHCLK_LTDC | RCC_PERIPHCLK_TIM | RCC_PERIPHCLK_RTC;
  2167. /* Get the PLLI2S Clock configuration -----------------------------------------------*/
  2168. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> RCC_PLLI2SCFGR_PLLI2SN_Pos);
  2169. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  2170. PeriphClkInit->PLLI2S.PLLI2SQ = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> RCC_PLLI2SCFGR_PLLI2SQ_Pos);
  2171. /* Get the PLLSAI Clock configuration -----------------------------------------------*/
  2172. PeriphClkInit->PLLSAI.PLLSAIN = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> RCC_PLLSAICFGR_PLLSAIN_Pos);
  2173. PeriphClkInit->PLLSAI.PLLSAIR = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIR) >> RCC_PLLSAICFGR_PLLSAIR_Pos);
  2174. PeriphClkInit->PLLSAI.PLLSAIQ = (uint32_t)((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> RCC_PLLSAICFGR_PLLSAIQ_Pos);
  2175. /* Get the PLLSAI/PLLI2S division factors -----------------------------------------------*/
  2176. PeriphClkInit->PLLI2SDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) >> RCC_DCKCFGR_PLLI2SDIVQ_Pos);
  2177. PeriphClkInit->PLLSAIDivQ = (uint32_t)((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> RCC_DCKCFGR_PLLSAIDIVQ_Pos);
  2178. PeriphClkInit->PLLSAIDivR = (uint32_t)(RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVR);
  2179. /* Get the RTC Clock configuration -----------------------------------------------*/
  2180. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  2181. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  2182. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  2183. {
  2184. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  2185. }
  2186. else
  2187. {
  2188. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  2189. }
  2190. }
  2191. /**
  2192. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  2193. * @note Return 0 if peripheral clock identifier not managed by this API
  2194. * @param PeriphClk Peripheral clock identifier
  2195. * This parameter can be one of the following values:
  2196. * @arg RCC_PERIPHCLK_I2S: I2S peripheral clock
  2197. * @retval Frequency in KHz
  2198. */
  2199. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  2200. {
  2201. /* This variable used to store the I2S clock frequency (value in Hz) */
  2202. uint32_t frequency = 0U;
  2203. /* This variable used to store the VCO Input (value in Hz) */
  2204. uint32_t vcoinput = 0U;
  2205. uint32_t srcclk = 0U;
  2206. /* This variable used to store the VCO Output (value in Hz) */
  2207. uint32_t vcooutput = 0U;
  2208. switch (PeriphClk)
  2209. {
  2210. case RCC_PERIPHCLK_I2S:
  2211. {
  2212. /* Get the current I2S source */
  2213. srcclk = __HAL_RCC_GET_I2S_SOURCE();
  2214. switch (srcclk)
  2215. {
  2216. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  2217. case RCC_I2SCLKSOURCE_EXT:
  2218. {
  2219. /* Set the I2S clock to the external clock value */
  2220. frequency = EXTERNAL_CLOCK_VALUE;
  2221. break;
  2222. }
  2223. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  2224. case RCC_I2SCLKSOURCE_PLLI2S:
  2225. {
  2226. /* Configure the PLLI2S division factor */
  2227. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  2228. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  2229. {
  2230. /* Get the I2S source clock value */
  2231. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  2232. }
  2233. else
  2234. {
  2235. /* Get the I2S source clock value */
  2236. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  2237. }
  2238. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  2239. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  2240. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  2241. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  2242. break;
  2243. }
  2244. /* Clock not enabled for I2S*/
  2245. default:
  2246. {
  2247. frequency = 0U;
  2248. break;
  2249. }
  2250. }
  2251. break;
  2252. }
  2253. }
  2254. return frequency;
  2255. }
  2256. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  2257. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx) ||\
  2258. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE)
  2259. /**
  2260. * @brief Initializes the RCC extended peripherals clocks according to the specified parameters in the
  2261. * RCC_PeriphCLKInitTypeDef.
  2262. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  2263. * contains the configuration information for the Extended Peripherals clocks(I2S and RTC clocks).
  2264. *
  2265. * @note A caution to be taken when HAL_RCCEx_PeriphCLKConfig() is used to select RTC clock selection, in this case
  2266. * the Reset of Backup domain will be applied in order to modify the RTC Clock source as consequence all backup
  2267. * domain (RTC and RCC_BDCR register expect BKPSRAM) will be reset
  2268. *
  2269. * @retval HAL status
  2270. */
  2271. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  2272. {
  2273. uint32_t tickstart = 0U;
  2274. uint32_t tmpreg1 = 0U;
  2275. /* Check the parameters */
  2276. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  2277. /*---------------------------- I2S configuration ---------------------------*/
  2278. if((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S) ||
  2279. (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_PLLI2S) == RCC_PERIPHCLK_PLLI2S))
  2280. {
  2281. /* check for Parameters */
  2282. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  2283. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  2284. #if defined(STM32F411xE)
  2285. assert_param(IS_RCC_PLLI2SM_VALUE(PeriphClkInit->PLLI2S.PLLI2SM));
  2286. #endif /* STM32F411xE */
  2287. /* Disable the PLLI2S */
  2288. __HAL_RCC_PLLI2S_DISABLE();
  2289. /* Get tick */
  2290. tickstart = HAL_GetTick();
  2291. /* Wait till PLLI2S is disabled */
  2292. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  2293. {
  2294. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  2295. {
  2296. /* return in case of Timeout detected */
  2297. return HAL_TIMEOUT;
  2298. }
  2299. }
  2300. #if defined(STM32F411xE)
  2301. /* Configure the PLLI2S division factors */
  2302. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  2303. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  2304. __HAL_RCC_PLLI2S_I2SCLK_CONFIG(PeriphClkInit->PLLI2S.PLLI2SM, PeriphClkInit->PLLI2S.PLLI2SN, PeriphClkInit->PLLI2S.PLLI2SR);
  2305. #else
  2306. /* Configure the PLLI2S division factors */
  2307. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLM) */
  2308. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  2309. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SR);
  2310. #endif /* STM32F411xE */
  2311. /* Enable the PLLI2S */
  2312. __HAL_RCC_PLLI2S_ENABLE();
  2313. /* Get tick */
  2314. tickstart = HAL_GetTick();
  2315. /* Wait till PLLI2S is ready */
  2316. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  2317. {
  2318. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  2319. {
  2320. /* return in case of Timeout detected */
  2321. return HAL_TIMEOUT;
  2322. }
  2323. }
  2324. }
  2325. /*---------------------------- RTC configuration ---------------------------*/
  2326. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  2327. {
  2328. /* Check for RTC Parameters used to output RTCCLK */
  2329. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  2330. /* Enable Power Clock*/
  2331. __HAL_RCC_PWR_CLK_ENABLE();
  2332. /* Enable write access to Backup domain */
  2333. PWR->CR |= PWR_CR_DBP;
  2334. /* Get tick */
  2335. tickstart = HAL_GetTick();
  2336. while((PWR->CR & PWR_CR_DBP) == RESET)
  2337. {
  2338. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  2339. {
  2340. return HAL_TIMEOUT;
  2341. }
  2342. }
  2343. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  2344. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  2345. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  2346. {
  2347. /* Store the content of BDCR register before the reset of Backup Domain */
  2348. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  2349. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  2350. __HAL_RCC_BACKUPRESET_FORCE();
  2351. __HAL_RCC_BACKUPRESET_RELEASE();
  2352. /* Restore the Content of BDCR register */
  2353. RCC->BDCR = tmpreg1;
  2354. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  2355. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  2356. {
  2357. /* Get tick */
  2358. tickstart = HAL_GetTick();
  2359. /* Wait till LSE is ready */
  2360. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  2361. {
  2362. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  2363. {
  2364. return HAL_TIMEOUT;
  2365. }
  2366. }
  2367. }
  2368. }
  2369. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  2370. }
  2371. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE)
  2372. /*---------------------------- TIM configuration ---------------------------*/
  2373. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM) == (RCC_PERIPHCLK_TIM))
  2374. {
  2375. __HAL_RCC_TIMCLKPRESCALER(PeriphClkInit->TIMPresSelection);
  2376. }
  2377. #endif /* STM32F401xC || STM32F401xE || STM32F411xE */
  2378. return HAL_OK;
  2379. }
  2380. /**
  2381. * @brief Configures the RCC_OscInitStruct according to the internal
  2382. * RCC configuration registers.
  2383. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  2384. * will be configured.
  2385. * @retval None
  2386. */
  2387. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  2388. {
  2389. uint32_t tempreg;
  2390. /* Set all possible values for the extended clock type parameter------------*/
  2391. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S | RCC_PERIPHCLK_RTC;
  2392. /* Get the PLLI2S Clock configuration --------------------------------------*/
  2393. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> RCC_PLLI2SCFGR_PLLI2SN_Pos);
  2394. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> RCC_PLLI2SCFGR_PLLI2SR_Pos);
  2395. #if defined(STM32F411xE)
  2396. PeriphClkInit->PLLI2S.PLLI2SM = (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM);
  2397. #endif /* STM32F411xE */
  2398. /* Get the RTC Clock configuration -----------------------------------------*/
  2399. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  2400. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  2401. #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE)
  2402. /* Get the TIM Prescaler configuration -------------------------------------*/
  2403. if ((RCC->DCKCFGR & RCC_DCKCFGR_TIMPRE) == RESET)
  2404. {
  2405. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_DESACTIVATED;
  2406. }
  2407. else
  2408. {
  2409. PeriphClkInit->TIMPresSelection = RCC_TIMPRES_ACTIVATED;
  2410. }
  2411. #endif /* STM32F401xC || STM32F401xE || STM32F411xE */
  2412. }
  2413. /**
  2414. * @brief Return the peripheral clock frequency for a given peripheral(SAI..)
  2415. * @note Return 0 if peripheral clock identifier not managed by this API
  2416. * @param PeriphClk Peripheral clock identifier
  2417. * This parameter can be one of the following values:
  2418. * @arg RCC_PERIPHCLK_I2S: I2S peripheral clock
  2419. * @retval Frequency in KHz
  2420. */
  2421. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  2422. {
  2423. /* This variable used to store the I2S clock frequency (value in Hz) */
  2424. uint32_t frequency = 0U;
  2425. /* This variable used to store the VCO Input (value in Hz) */
  2426. uint32_t vcoinput = 0U;
  2427. uint32_t srcclk = 0U;
  2428. /* This variable used to store the VCO Output (value in Hz) */
  2429. uint32_t vcooutput = 0U;
  2430. switch (PeriphClk)
  2431. {
  2432. case RCC_PERIPHCLK_I2S:
  2433. {
  2434. /* Get the current I2S source */
  2435. srcclk = __HAL_RCC_GET_I2S_SOURCE();
  2436. switch (srcclk)
  2437. {
  2438. /* Check if I2S clock selection is External clock mapped on the I2S_CKIN pin used as I2S clock */
  2439. case RCC_I2SCLKSOURCE_EXT:
  2440. {
  2441. /* Set the I2S clock to the external clock value */
  2442. frequency = EXTERNAL_CLOCK_VALUE;
  2443. break;
  2444. }
  2445. /* Check if I2S clock selection is PLLI2S VCO output clock divided by PLLI2SR used as I2S clock */
  2446. case RCC_I2SCLKSOURCE_PLLI2S:
  2447. {
  2448. #if defined(STM32F411xE)
  2449. /* Configure the PLLI2S division factor */
  2450. /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
  2451. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  2452. {
  2453. /* Get the I2S source clock value */
  2454. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  2455. }
  2456. else
  2457. {
  2458. /* Get the I2S source clock value */
  2459. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SM));
  2460. }
  2461. #else
  2462. /* Configure the PLLI2S division factor */
  2463. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  2464. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  2465. {
  2466. /* Get the I2S source clock value */
  2467. vcoinput = (uint32_t)(HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  2468. }
  2469. else
  2470. {
  2471. /* Get the I2S source clock value */
  2472. vcoinput = (uint32_t)(HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  2473. }
  2474. #endif /* STM32F411xE */
  2475. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  2476. vcooutput = (uint32_t)(vcoinput * (((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U) & (RCC_PLLI2SCFGR_PLLI2SN >> 6U)));
  2477. /* I2S_CLK = PLLI2S_VCO Output/PLLI2SR */
  2478. frequency = (uint32_t)(vcooutput /(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U) & (RCC_PLLI2SCFGR_PLLI2SR >> 28U)));
  2479. break;
  2480. }
  2481. /* Clock not enabled for I2S*/
  2482. default:
  2483. {
  2484. frequency = 0U;
  2485. break;
  2486. }
  2487. }
  2488. break;
  2489. }
  2490. }
  2491. return frequency;
  2492. }
  2493. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F401xC || STM32F401xE || STM32F411xE */
  2494. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||\
  2495. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  2496. /**
  2497. * @brief Select LSE mode
  2498. *
  2499. * @note This mode is only available for STM32F410xx/STM32F411xx/STM32F446xx/STM32F469xx/STM32F479xx/STM32F412Zx/STM32F412Vx/STM32F412Rx/STM32F412Cx devices.
  2500. *
  2501. * @param Mode specifies the LSE mode.
  2502. * This parameter can be one of the following values:
  2503. * @arg RCC_LSE_LOWPOWER_MODE: LSE oscillator in low power mode selection
  2504. * @arg RCC_LSE_HIGHDRIVE_MODE: LSE oscillator in High Drive mode selection
  2505. * @retval None
  2506. */
  2507. void HAL_RCCEx_SelectLSEMode(uint8_t Mode)
  2508. {
  2509. /* Check the parameters */
  2510. assert_param(IS_RCC_LSE_MODE(Mode));
  2511. if(Mode == RCC_LSE_HIGHDRIVE_MODE)
  2512. {
  2513. SET_BIT(RCC->BDCR, RCC_BDCR_LSEMOD);
  2514. }
  2515. else
  2516. {
  2517. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEMOD);
  2518. }
  2519. }
  2520. #endif /* STM32F410xx || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  2521. /** @defgroup RCCEx_Exported_Functions_Group2 Extended Clock management functions
  2522. * @brief Extended Clock management functions
  2523. *
  2524. @verbatim
  2525. ===============================================================================
  2526. ##### Extended clock management functions #####
  2527. ===============================================================================
  2528. [..]
  2529. This subsection provides a set of functions allowing to control the
  2530. activation or deactivation of PLLI2S, PLLSAI.
  2531. @endverbatim
  2532. * @{
  2533. */
  2534. #if defined(RCC_PLLI2S_SUPPORT)
  2535. /**
  2536. * @brief Enable PLLI2S.
  2537. * @param PLLI2SInit pointer to an RCC_PLLI2SInitTypeDef structure that
  2538. * contains the configuration information for the PLLI2S
  2539. * @retval HAL status
  2540. */
  2541. HAL_StatusTypeDef HAL_RCCEx_EnablePLLI2S(RCC_PLLI2SInitTypeDef *PLLI2SInit)
  2542. {
  2543. uint32_t tickstart;
  2544. /* Check for parameters */
  2545. assert_param(IS_RCC_PLLI2SN_VALUE(PLLI2SInit->PLLI2SN));
  2546. assert_param(IS_RCC_PLLI2SR_VALUE(PLLI2SInit->PLLI2SR));
  2547. #if defined(RCC_PLLI2SCFGR_PLLI2SM)
  2548. assert_param(IS_RCC_PLLI2SM_VALUE(PLLI2SInit->PLLI2SM));
  2549. #endif /* RCC_PLLI2SCFGR_PLLI2SM */
  2550. #if defined(RCC_PLLI2SCFGR_PLLI2SP)
  2551. assert_param(IS_RCC_PLLI2SP_VALUE(PLLI2SInit->PLLI2SP));
  2552. #endif /* RCC_PLLI2SCFGR_PLLI2SP */
  2553. #if defined(RCC_PLLI2SCFGR_PLLI2SQ)
  2554. assert_param(IS_RCC_PLLI2SQ_VALUE(PLLI2SInit->PLLI2SQ));
  2555. #endif /* RCC_PLLI2SCFGR_PLLI2SQ */
  2556. /* Disable the PLLI2S */
  2557. __HAL_RCC_PLLI2S_DISABLE();
  2558. /* Wait till PLLI2S is disabled */
  2559. tickstart = HAL_GetTick();
  2560. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  2561. {
  2562. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  2563. {
  2564. /* return in case of Timeout detected */
  2565. return HAL_TIMEOUT;
  2566. }
  2567. }
  2568. /* Configure the PLLI2S division factors */
  2569. #if defined(STM32F446xx)
  2570. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  2571. /* I2SPCLK = PLLI2S_VCO / PLLI2SP */
  2572. /* I2SQCLK = PLLI2S_VCO / PLLI2SQ */
  2573. /* I2SRCLK = PLLI2S_VCO / PLLI2SR */
  2574. __HAL_RCC_PLLI2S_CONFIG(PLLI2SInit->PLLI2SM, PLLI2SInit->PLLI2SN, \
  2575. PLLI2SInit->PLLI2SP, PLLI2SInit->PLLI2SQ, PLLI2SInit->PLLI2SR);
  2576. #elif defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) ||\
  2577. defined(STM32F413xx) || defined(STM32F423xx)
  2578. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM)*/
  2579. /* I2SQCLK = PLLI2S_VCO / PLLI2SQ */
  2580. /* I2SRCLK = PLLI2S_VCO / PLLI2SR */
  2581. __HAL_RCC_PLLI2S_CONFIG(PLLI2SInit->PLLI2SM, PLLI2SInit->PLLI2SN, \
  2582. PLLI2SInit->PLLI2SQ, PLLI2SInit->PLLI2SR);
  2583. #elif defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  2584. defined(STM32F469xx) || defined(STM32F479xx)
  2585. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * PLLI2SN */
  2586. /* I2SQCLK = PLLI2S_VCO / PLLI2SQ */
  2587. /* I2SRCLK = PLLI2S_VCO / PLLI2SR */
  2588. __HAL_RCC_PLLI2S_SAICLK_CONFIG(PLLI2SInit->PLLI2SN, PLLI2SInit->PLLI2SQ, PLLI2SInit->PLLI2SR);
  2589. #elif defined(STM32F411xE)
  2590. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLI2SM) */
  2591. /* I2SRCLK = PLLI2S_VCO / PLLI2SR */
  2592. __HAL_RCC_PLLI2S_I2SCLK_CONFIG(PLLI2SInit->PLLI2SM, PLLI2SInit->PLLI2SN, PLLI2SInit->PLLI2SR);
  2593. #else
  2594. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) x PLLI2SN */
  2595. /* I2SRCLK = PLLI2S_VCO / PLLI2SR */
  2596. __HAL_RCC_PLLI2S_CONFIG(PLLI2SInit->PLLI2SN, PLLI2SInit->PLLI2SR);
  2597. #endif /* STM32F446xx */
  2598. /* Enable the PLLI2S */
  2599. __HAL_RCC_PLLI2S_ENABLE();
  2600. /* Wait till PLLI2S is ready */
  2601. tickstart = HAL_GetTick();
  2602. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  2603. {
  2604. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  2605. {
  2606. /* return in case of Timeout detected */
  2607. return HAL_TIMEOUT;
  2608. }
  2609. }
  2610. return HAL_OK;
  2611. }
  2612. /**
  2613. * @brief Disable PLLI2S.
  2614. * @retval HAL status
  2615. */
  2616. HAL_StatusTypeDef HAL_RCCEx_DisablePLLI2S(void)
  2617. {
  2618. uint32_t tickstart;
  2619. /* Disable the PLLI2S */
  2620. __HAL_RCC_PLLI2S_DISABLE();
  2621. /* Wait till PLLI2S is disabled */
  2622. tickstart = HAL_GetTick();
  2623. while(READ_BIT(RCC->CR, RCC_CR_PLLI2SRDY) != RESET)
  2624. {
  2625. if((HAL_GetTick() - tickstart) > PLLI2S_TIMEOUT_VALUE)
  2626. {
  2627. /* return in case of Timeout detected */
  2628. return HAL_TIMEOUT;
  2629. }
  2630. }
  2631. return HAL_OK;
  2632. }
  2633. #endif /* RCC_PLLI2S_SUPPORT */
  2634. #if defined(RCC_PLLSAI_SUPPORT)
  2635. /**
  2636. * @brief Enable PLLSAI.
  2637. * @param PLLSAIInit pointer to an RCC_PLLSAIInitTypeDef structure that
  2638. * contains the configuration information for the PLLSAI
  2639. * @retval HAL status
  2640. */
  2641. HAL_StatusTypeDef HAL_RCCEx_EnablePLLSAI(RCC_PLLSAIInitTypeDef *PLLSAIInit)
  2642. {
  2643. uint32_t tickstart;
  2644. /* Check for parameters */
  2645. assert_param(IS_RCC_PLLSAIN_VALUE(PLLSAIInit->PLLSAIN));
  2646. assert_param(IS_RCC_PLLSAIQ_VALUE(PLLSAIInit->PLLSAIQ));
  2647. #if defined(RCC_PLLSAICFGR_PLLSAIM)
  2648. assert_param(IS_RCC_PLLSAIM_VALUE(PLLSAIInit->PLLSAIM));
  2649. #endif /* RCC_PLLSAICFGR_PLLSAIM */
  2650. #if defined(RCC_PLLSAICFGR_PLLSAIP)
  2651. assert_param(IS_RCC_PLLSAIP_VALUE(PLLSAIInit->PLLSAIP));
  2652. #endif /* RCC_PLLSAICFGR_PLLSAIP */
  2653. #if defined(RCC_PLLSAICFGR_PLLSAIR)
  2654. assert_param(IS_RCC_PLLSAIR_VALUE(PLLSAIInit->PLLSAIR));
  2655. #endif /* RCC_PLLSAICFGR_PLLSAIR */
  2656. /* Disable the PLLSAI */
  2657. __HAL_RCC_PLLSAI_DISABLE();
  2658. /* Wait till PLLSAI is disabled */
  2659. tickstart = HAL_GetTick();
  2660. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  2661. {
  2662. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  2663. {
  2664. /* return in case of Timeout detected */
  2665. return HAL_TIMEOUT;
  2666. }
  2667. }
  2668. /* Configure the PLLSAI division factors */
  2669. #if defined(STM32F446xx)
  2670. /* PLLSAI_VCO = f(VCO clock) = f(PLLSAI clock input) * (PLLSAIN/PLLSAIM) */
  2671. /* SAIPCLK = PLLSAI_VCO / PLLSAIP */
  2672. /* SAIQCLK = PLLSAI_VCO / PLLSAIQ */
  2673. /* SAIRCLK = PLLSAI_VCO / PLLSAIR */
  2674. __HAL_RCC_PLLSAI_CONFIG(PLLSAIInit->PLLSAIM, PLLSAIInit->PLLSAIN, \
  2675. PLLSAIInit->PLLSAIP, PLLSAIInit->PLLSAIQ, 0U);
  2676. #elif defined(STM32F469xx) || defined(STM32F479xx)
  2677. /* PLLSAI_VCO = f(VCO clock) = f(PLLSAI clock input) * PLLSAIN */
  2678. /* SAIPCLK = PLLSAI_VCO / PLLSAIP */
  2679. /* SAIQCLK = PLLSAI_VCO / PLLSAIQ */
  2680. /* SAIRCLK = PLLSAI_VCO / PLLSAIR */
  2681. __HAL_RCC_PLLSAI_CONFIG(PLLSAIInit->PLLSAIN, PLLSAIInit->PLLSAIP, \
  2682. PLLSAIInit->PLLSAIQ, PLLSAIInit->PLLSAIR);
  2683. #else
  2684. /* PLLSAI_VCO = f(VCO clock) = f(PLLSAI clock input) x PLLSAIN */
  2685. /* SAIQCLK = PLLSAI_VCO / PLLSAIQ */
  2686. /* SAIRCLK = PLLSAI_VCO / PLLSAIR */
  2687. __HAL_RCC_PLLSAI_CONFIG(PLLSAIInit->PLLSAIN, PLLSAIInit->PLLSAIQ, PLLSAIInit->PLLSAIR);
  2688. #endif /* STM32F446xx */
  2689. /* Enable the PLLSAI */
  2690. __HAL_RCC_PLLSAI_ENABLE();
  2691. /* Wait till PLLSAI is ready */
  2692. tickstart = HAL_GetTick();
  2693. while(__HAL_RCC_PLLSAI_GET_FLAG() == RESET)
  2694. {
  2695. if((HAL_GetTick() - tickstart ) > PLLSAI_TIMEOUT_VALUE)
  2696. {
  2697. /* return in case of Timeout detected */
  2698. return HAL_TIMEOUT;
  2699. }
  2700. }
  2701. return HAL_OK;
  2702. }
  2703. /**
  2704. * @brief Disable PLLSAI.
  2705. * @retval HAL status
  2706. */
  2707. HAL_StatusTypeDef HAL_RCCEx_DisablePLLSAI(void)
  2708. {
  2709. uint32_t tickstart;
  2710. /* Disable the PLLSAI */
  2711. __HAL_RCC_PLLSAI_DISABLE();
  2712. /* Wait till PLLSAI is disabled */
  2713. tickstart = HAL_GetTick();
  2714. while(__HAL_RCC_PLLSAI_GET_FLAG() != RESET)
  2715. {
  2716. if((HAL_GetTick() - tickstart) > PLLSAI_TIMEOUT_VALUE)
  2717. {
  2718. /* return in case of Timeout detected */
  2719. return HAL_TIMEOUT;
  2720. }
  2721. }
  2722. return HAL_OK;
  2723. }
  2724. #endif /* RCC_PLLSAI_SUPPORT */
  2725. /**
  2726. * @}
  2727. */
  2728. #if defined(STM32F446xx)
  2729. /**
  2730. * @brief Returns the SYSCLK frequency
  2731. *
  2732. * @note This function implementation is valid only for STM32F446xx devices.
  2733. * @note This function add the PLL/PLLR System clock source
  2734. *
  2735. * @note The system frequency computed by this function is not the real
  2736. * frequency in the chip. It is calculated based on the predefined
  2737. * constant and the selected clock source:
  2738. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  2739. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  2740. * @note If SYSCLK source is PLL or PLLR, function returns values based on HSE_VALUE(**)
  2741. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  2742. * @note (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  2743. * 16 MHz) but the real value may vary depending on the variations
  2744. * in voltage and temperature.
  2745. * @note (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  2746. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  2747. * frequency of the crystal used. Otherwise, this function may
  2748. * have wrong result.
  2749. *
  2750. * @note The result of this function could be not correct when using fractional
  2751. * value for HSE crystal.
  2752. *
  2753. * @note This function can be used by the user application to compute the
  2754. * baudrate for the communication peripherals or configure other parameters.
  2755. *
  2756. * @note Each time SYSCLK changes, this function must be called to update the
  2757. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  2758. *
  2759. *
  2760. * @retval SYSCLK frequency
  2761. */
  2762. uint32_t HAL_RCC_GetSysClockFreq(void)
  2763. {
  2764. uint32_t pllm = 0U;
  2765. uint32_t pllvco = 0U;
  2766. uint32_t pllp = 0U;
  2767. uint32_t pllr = 0U;
  2768. uint32_t sysclockfreq = 0U;
  2769. /* Get SYSCLK source -------------------------------------------------------*/
  2770. switch (RCC->CFGR & RCC_CFGR_SWS)
  2771. {
  2772. case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
  2773. {
  2774. sysclockfreq = HSI_VALUE;
  2775. break;
  2776. }
  2777. case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
  2778. {
  2779. sysclockfreq = HSE_VALUE;
  2780. break;
  2781. }
  2782. case RCC_CFGR_SWS_PLL: /* PLL/PLLP used as system clock source */
  2783. {
  2784. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  2785. SYSCLK = PLL_VCO / PLLP */
  2786. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  2787. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
  2788. {
  2789. /* HSE used as PLL clock source */
  2790. pllvco = (uint32_t) ((((uint64_t) HSE_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
  2791. }
  2792. else
  2793. {
  2794. /* HSI used as PLL clock source */
  2795. pllvco = (uint32_t) ((((uint64_t) HSI_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
  2796. }
  2797. pllp = ((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> RCC_PLLCFGR_PLLP_Pos) + 1U) *2U);
  2798. sysclockfreq = pllvco/pllp;
  2799. break;
  2800. }
  2801. case RCC_CFGR_SWS_PLLR: /* PLL/PLLR used as system clock source */
  2802. {
  2803. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  2804. SYSCLK = PLL_VCO / PLLR */
  2805. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  2806. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
  2807. {
  2808. /* HSE used as PLL clock source */
  2809. pllvco = (uint32_t) ((((uint64_t) HSE_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
  2810. }
  2811. else
  2812. {
  2813. /* HSI used as PLL clock source */
  2814. pllvco = (uint32_t) ((((uint64_t) HSI_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
  2815. }
  2816. pllr = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos);
  2817. sysclockfreq = pllvco/pllr;
  2818. break;
  2819. }
  2820. default:
  2821. {
  2822. sysclockfreq = HSI_VALUE;
  2823. break;
  2824. }
  2825. }
  2826. return sysclockfreq;
  2827. }
  2828. #endif /* STM32F446xx */
  2829. /**
  2830. * @}
  2831. */
  2832. /**
  2833. * @}
  2834. */
  2835. /**
  2836. * @brief Resets the RCC clock configuration to the default reset state.
  2837. * @note The default reset state of the clock configuration is given below:
  2838. * - HSI ON and used as system clock source
  2839. * - HSE, PLL, PLLI2S and PLLSAI OFF
  2840. * - AHB, APB1 and APB2 prescaler set to 1.
  2841. * - CSS, MCO1 and MCO2 OFF
  2842. * - All interrupts disabled
  2843. * @note This function doesn't modify the configuration of the
  2844. * - Peripheral clocks
  2845. * - LSI, LSE and RTC clocks
  2846. * @retval HAL status
  2847. */
  2848. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  2849. {
  2850. uint32_t tickstart;
  2851. /* Get Start Tick */
  2852. tickstart = HAL_GetTick();
  2853. /* Set HSION bit to the reset value */
  2854. SET_BIT(RCC->CR, RCC_CR_HSION);
  2855. /* Wait till HSI is ready */
  2856. while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == RESET)
  2857. {
  2858. if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  2859. {
  2860. return HAL_TIMEOUT;
  2861. }
  2862. }
  2863. /* Set HSITRIM[4:0] bits to the reset value */
  2864. SET_BIT(RCC->CR, RCC_CR_HSITRIM_4);
  2865. /* Get Start Tick */
  2866. tickstart = HAL_GetTick();
  2867. /* Reset CFGR register */
  2868. CLEAR_REG(RCC->CFGR);
  2869. /* Wait till clock switch is ready */
  2870. while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != RESET)
  2871. {
  2872. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  2873. {
  2874. return HAL_TIMEOUT;
  2875. }
  2876. }
  2877. /* Get Start Tick */
  2878. tickstart = HAL_GetTick();
  2879. /* Clear HSEON, HSEBYP and CSSON bits */
  2880. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSEBYP | RCC_CR_CSSON);
  2881. /* Wait till HSE is disabled */
  2882. while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != RESET)
  2883. {
  2884. if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  2885. {
  2886. return HAL_TIMEOUT;
  2887. }
  2888. }
  2889. /* Get Start Tick */
  2890. tickstart = HAL_GetTick();
  2891. /* Clear PLLON bit */
  2892. CLEAR_BIT(RCC->CR, RCC_CR_PLLON);
  2893. /* Wait till PLL is disabled */
  2894. while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != RESET)
  2895. {
  2896. if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  2897. {
  2898. return HAL_TIMEOUT;
  2899. }
  2900. }
  2901. #if defined(RCC_PLLI2S_SUPPORT)
  2902. /* Get Start Tick */
  2903. tickstart = HAL_GetTick();
  2904. /* Reset PLLI2SON bit */
  2905. CLEAR_BIT(RCC->CR, RCC_CR_PLLI2SON);
  2906. /* Wait till PLLI2S is disabled */
  2907. while (READ_BIT(RCC->CR, RCC_CR_PLLI2SRDY) != RESET)
  2908. {
  2909. if ((HAL_GetTick() - tickstart) > PLLI2S_TIMEOUT_VALUE)
  2910. {
  2911. return HAL_TIMEOUT;
  2912. }
  2913. }
  2914. #endif /* RCC_PLLI2S_SUPPORT */
  2915. #if defined(RCC_PLLSAI_SUPPORT)
  2916. /* Get Start Tick */
  2917. tickstart = HAL_GetTick();
  2918. /* Reset PLLSAI bit */
  2919. CLEAR_BIT(RCC->CR, RCC_CR_PLLSAION);
  2920. /* Wait till PLLSAI is disabled */
  2921. while (READ_BIT(RCC->CR, RCC_CR_PLLSAIRDY) != RESET)
  2922. {
  2923. if ((HAL_GetTick() - tickstart) > PLLSAI_TIMEOUT_VALUE)
  2924. {
  2925. return HAL_TIMEOUT;
  2926. }
  2927. }
  2928. #endif /* RCC_PLLSAI_SUPPORT */
  2929. /* Once PLL, PLLI2S and PLLSAI are OFF, reset PLLCFGR register to default value */
  2930. #if defined(STM32F412Cx) || defined(STM32F412Rx) || defined(STM32F412Vx) || defined(STM32F412Zx) || defined(STM32F413xx) || \
  2931. defined(STM32F423xx) || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  2932. RCC->PLLCFGR = RCC_PLLCFGR_PLLM_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLQ_2 | RCC_PLLCFGR_PLLR_1;
  2933. #elif defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx)
  2934. RCC->PLLCFGR = RCC_PLLCFGR_PLLR_0 | RCC_PLLCFGR_PLLR_1 | RCC_PLLCFGR_PLLR_2 | RCC_PLLCFGR_PLLM_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLQ_0 | RCC_PLLCFGR_PLLQ_1 | RCC_PLLCFGR_PLLQ_2 | RCC_PLLCFGR_PLLQ_3;
  2935. #else
  2936. RCC->PLLCFGR = RCC_PLLCFGR_PLLM_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLQ_2;
  2937. #endif /* STM32F412Cx || STM32F412Rx || STM32F412Vx || STM32F412Zx || STM32F413xx || STM32F423xx || STM32F446xx || STM32F469xx || STM32F479xx */
  2938. /* Reset PLLI2SCFGR register to default value */
  2939. #if defined(STM32F412Cx) || defined(STM32F412Rx) || defined(STM32F412Vx) || defined(STM32F412Zx) || defined(STM32F413xx) || \
  2940. defined(STM32F423xx) || defined(STM32F446xx)
  2941. RCC->PLLI2SCFGR = RCC_PLLI2SCFGR_PLLI2SM_4 | RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SQ_2 | RCC_PLLI2SCFGR_PLLI2SR_1;
  2942. #elif defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)
  2943. RCC->PLLI2SCFGR = RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SR_1;
  2944. #elif defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  2945. RCC->PLLI2SCFGR = RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SQ_2 | RCC_PLLI2SCFGR_PLLI2SR_1;
  2946. #elif defined(STM32F411xE)
  2947. RCC->PLLI2SCFGR = RCC_PLLI2SCFGR_PLLI2SM_4 | RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SR_1;
  2948. #endif /* STM32F412Cx || STM32F412Rx || STM32F412Vx || STM32F412Zx || STM32F413xx || STM32F423xx || STM32F446xx */
  2949. /* Reset PLLSAICFGR register */
  2950. #if defined(STM32F427xx) || defined(STM32F429xx) || defined(STM32F437xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  2951. RCC->PLLSAICFGR = RCC_PLLSAICFGR_PLLSAIN_6 | RCC_PLLSAICFGR_PLLSAIN_7 | RCC_PLLSAICFGR_PLLSAIQ_2 | RCC_PLLSAICFGR_PLLSAIR_1;
  2952. #elif defined(STM32F446xx)
  2953. RCC->PLLSAICFGR = RCC_PLLSAICFGR_PLLSAIM_4 | RCC_PLLSAICFGR_PLLSAIN_6 | RCC_PLLSAICFGR_PLLSAIN_7 | RCC_PLLSAICFGR_PLLSAIQ_2;
  2954. #endif /* STM32F427xx || STM32F429xx || STM32F437xx || STM32F439xx || STM32F469xx || STM32F479xx */
  2955. /* Disable all interrupts */
  2956. CLEAR_BIT(RCC->CIR, RCC_CIR_LSIRDYIE | RCC_CIR_LSERDYIE | RCC_CIR_HSIRDYIE | RCC_CIR_HSERDYIE | RCC_CIR_PLLRDYIE);
  2957. #if defined(RCC_CIR_PLLI2SRDYIE)
  2958. CLEAR_BIT(RCC->CIR, RCC_CIR_PLLI2SRDYIE);
  2959. #endif /* RCC_CIR_PLLI2SRDYIE */
  2960. #if defined(RCC_CIR_PLLSAIRDYIE)
  2961. CLEAR_BIT(RCC->CIR, RCC_CIR_PLLSAIRDYIE);
  2962. #endif /* RCC_CIR_PLLSAIRDYIE */
  2963. /* Clear all interrupt flags */
  2964. SET_BIT(RCC->CIR, RCC_CIR_LSIRDYC | RCC_CIR_LSERDYC | RCC_CIR_HSIRDYC | RCC_CIR_HSERDYC | RCC_CIR_PLLRDYC | RCC_CIR_CSSC);
  2965. #if defined(RCC_CIR_PLLI2SRDYC)
  2966. SET_BIT(RCC->CIR, RCC_CIR_PLLI2SRDYC);
  2967. #endif /* RCC_CIR_PLLI2SRDYC */
  2968. #if defined(RCC_CIR_PLLSAIRDYC)
  2969. SET_BIT(RCC->CIR, RCC_CIR_PLLSAIRDYC);
  2970. #endif /* RCC_CIR_PLLSAIRDYC */
  2971. /* Clear LSION bit */
  2972. CLEAR_BIT(RCC->CSR, RCC_CSR_LSION);
  2973. /* Reset all CSR flags */
  2974. SET_BIT(RCC->CSR, RCC_CSR_RMVF);
  2975. /* Update the SystemCoreClock global variable */
  2976. SystemCoreClock = HSI_VALUE;
  2977. /* Adapt Systick interrupt period */
  2978. if(HAL_InitTick(uwTickPrio) != HAL_OK)
  2979. {
  2980. return HAL_ERROR;
  2981. }
  2982. else
  2983. {
  2984. return HAL_OK;
  2985. }
  2986. }
  2987. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||\
  2988. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  2989. /**
  2990. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  2991. * RCC_OscInitTypeDef.
  2992. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  2993. * contains the configuration information for the RCC Oscillators.
  2994. * @note The PLL is not disabled when used as system clock.
  2995. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  2996. * supported by this API. User should request a transition to LSE Off
  2997. * first and then LSE On or LSE Bypass.
  2998. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  2999. * supported by this API. User should request a transition to HSE Off
  3000. * first and then HSE On or HSE Bypass.
  3001. * @note This function add the PLL/PLLR factor management during PLL configuration this feature
  3002. * is only available in STM32F410xx/STM32F446xx/STM32F469xx/STM32F479xx/STM32F412Zx/STM32F412Vx/STM32F412Rx/STM32F412Cx devices
  3003. * @retval HAL status
  3004. */
  3005. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  3006. {
  3007. uint32_t tickstart = 0U;
  3008. /* Check the parameters */
  3009. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  3010. /*------------------------------- HSE Configuration ------------------------*/
  3011. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  3012. {
  3013. /* Check the parameters */
  3014. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  3015. /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
  3016. #if defined(STM32F446xx)
  3017. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\
  3018. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)) ||\
  3019. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLLR) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
  3020. #else
  3021. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\
  3022. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
  3023. #endif /* STM32F446xx */
  3024. {
  3025. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  3026. {
  3027. return HAL_ERROR;
  3028. }
  3029. }
  3030. else
  3031. {
  3032. /* Set the new HSE configuration ---------------------------------------*/
  3033. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  3034. /* Check the HSE State */
  3035. if((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF)
  3036. {
  3037. /* Get Start Tick*/
  3038. tickstart = HAL_GetTick();
  3039. /* Wait till HSE is ready */
  3040. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  3041. {
  3042. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  3043. {
  3044. return HAL_TIMEOUT;
  3045. }
  3046. }
  3047. }
  3048. else
  3049. {
  3050. /* Get Start Tick*/
  3051. tickstart = HAL_GetTick();
  3052. /* Wait till HSE is bypassed or disabled */
  3053. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  3054. {
  3055. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  3056. {
  3057. return HAL_TIMEOUT;
  3058. }
  3059. }
  3060. }
  3061. }
  3062. }
  3063. /*----------------------------- HSI Configuration --------------------------*/
  3064. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  3065. {
  3066. /* Check the parameters */
  3067. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  3068. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  3069. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  3070. #if defined(STM32F446xx)
  3071. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\
  3072. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)) ||\
  3073. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLLR) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
  3074. #else
  3075. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\
  3076. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
  3077. #endif /* STM32F446xx */
  3078. {
  3079. /* When HSI is used as system clock it will not disabled */
  3080. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  3081. {
  3082. return HAL_ERROR;
  3083. }
  3084. /* Otherwise, just the calibration is allowed */
  3085. else
  3086. {
  3087. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  3088. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  3089. }
  3090. }
  3091. else
  3092. {
  3093. /* Check the HSI State */
  3094. if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
  3095. {
  3096. /* Enable the Internal High Speed oscillator (HSI). */
  3097. __HAL_RCC_HSI_ENABLE();
  3098. /* Get Start Tick*/
  3099. tickstart = HAL_GetTick();
  3100. /* Wait till HSI is ready */
  3101. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  3102. {
  3103. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  3104. {
  3105. return HAL_TIMEOUT;
  3106. }
  3107. }
  3108. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  3109. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  3110. }
  3111. else
  3112. {
  3113. /* Disable the Internal High Speed oscillator (HSI). */
  3114. __HAL_RCC_HSI_DISABLE();
  3115. /* Get Start Tick*/
  3116. tickstart = HAL_GetTick();
  3117. /* Wait till HSI is ready */
  3118. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  3119. {
  3120. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  3121. {
  3122. return HAL_TIMEOUT;
  3123. }
  3124. }
  3125. }
  3126. }
  3127. }
  3128. /*------------------------------ LSI Configuration -------------------------*/
  3129. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  3130. {
  3131. /* Check the parameters */
  3132. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  3133. /* Check the LSI State */
  3134. if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
  3135. {
  3136. /* Enable the Internal Low Speed oscillator (LSI). */
  3137. __HAL_RCC_LSI_ENABLE();
  3138. /* Get Start Tick*/
  3139. tickstart = HAL_GetTick();
  3140. /* Wait till LSI is ready */
  3141. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  3142. {
  3143. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  3144. {
  3145. return HAL_TIMEOUT;
  3146. }
  3147. }
  3148. }
  3149. else
  3150. {
  3151. /* Disable the Internal Low Speed oscillator (LSI). */
  3152. __HAL_RCC_LSI_DISABLE();
  3153. /* Get Start Tick*/
  3154. tickstart = HAL_GetTick();
  3155. /* Wait till LSI is ready */
  3156. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  3157. {
  3158. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  3159. {
  3160. return HAL_TIMEOUT;
  3161. }
  3162. }
  3163. }
  3164. }
  3165. /*------------------------------ LSE Configuration -------------------------*/
  3166. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  3167. {
  3168. FlagStatus pwrclkchanged = RESET;
  3169. /* Check the parameters */
  3170. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  3171. /* Update LSE configuration in Backup Domain control register */
  3172. /* Requires to enable write access to Backup Domain of necessary */
  3173. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  3174. {
  3175. __HAL_RCC_PWR_CLK_ENABLE();
  3176. pwrclkchanged = SET;
  3177. }
  3178. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  3179. {
  3180. /* Enable write access to Backup domain */
  3181. SET_BIT(PWR->CR, PWR_CR_DBP);
  3182. /* Wait for Backup domain Write protection disable */
  3183. tickstart = HAL_GetTick();
  3184. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  3185. {
  3186. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  3187. {
  3188. return HAL_TIMEOUT;
  3189. }
  3190. }
  3191. }
  3192. /* Set the new LSE configuration -----------------------------------------*/
  3193. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  3194. /* Check the LSE State */
  3195. if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
  3196. {
  3197. /* Get Start Tick*/
  3198. tickstart = HAL_GetTick();
  3199. /* Wait till LSE is ready */
  3200. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  3201. {
  3202. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  3203. {
  3204. return HAL_TIMEOUT;
  3205. }
  3206. }
  3207. }
  3208. else
  3209. {
  3210. /* Get Start Tick*/
  3211. tickstart = HAL_GetTick();
  3212. /* Wait till LSE is ready */
  3213. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  3214. {
  3215. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  3216. {
  3217. return HAL_TIMEOUT;
  3218. }
  3219. }
  3220. }
  3221. /* Restore clock configuration if changed */
  3222. if(pwrclkchanged == SET)
  3223. {
  3224. __HAL_RCC_PWR_CLK_DISABLE();
  3225. }
  3226. }
  3227. /*-------------------------------- PLL Configuration -----------------------*/
  3228. /* Check the parameters */
  3229. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  3230. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  3231. {
  3232. /* Check if the PLL is used as system clock or not */
  3233. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  3234. {
  3235. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  3236. {
  3237. /* Check the parameters */
  3238. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  3239. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  3240. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  3241. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  3242. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  3243. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  3244. /* Disable the main PLL. */
  3245. __HAL_RCC_PLL_DISABLE();
  3246. /* Get Start Tick*/
  3247. tickstart = HAL_GetTick();
  3248. /* Wait till PLL is ready */
  3249. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  3250. {
  3251. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  3252. {
  3253. return HAL_TIMEOUT;
  3254. }
  3255. }
  3256. /* Configure the main PLL clock source, multiplication and division factors. */
  3257. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  3258. RCC_OscInitStruct->PLL.PLLM,
  3259. RCC_OscInitStruct->PLL.PLLN,
  3260. RCC_OscInitStruct->PLL.PLLP,
  3261. RCC_OscInitStruct->PLL.PLLQ,
  3262. RCC_OscInitStruct->PLL.PLLR);
  3263. /* Enable the main PLL. */
  3264. __HAL_RCC_PLL_ENABLE();
  3265. /* Get Start Tick*/
  3266. tickstart = HAL_GetTick();
  3267. /* Wait till PLL is ready */
  3268. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  3269. {
  3270. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  3271. {
  3272. return HAL_TIMEOUT;
  3273. }
  3274. }
  3275. }
  3276. else
  3277. {
  3278. /* Disable the main PLL. */
  3279. __HAL_RCC_PLL_DISABLE();
  3280. /* Get Start Tick*/
  3281. tickstart = HAL_GetTick();
  3282. /* Wait till PLL is ready */
  3283. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  3284. {
  3285. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  3286. {
  3287. return HAL_TIMEOUT;
  3288. }
  3289. }
  3290. }
  3291. }
  3292. else
  3293. {
  3294. return HAL_ERROR;
  3295. }
  3296. }
  3297. return HAL_OK;
  3298. }
  3299. /**
  3300. * @brief Configures the RCC_OscInitStruct according to the internal
  3301. * RCC configuration registers.
  3302. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that will be configured.
  3303. *
  3304. * @note This function is only available in case of STM32F410xx/STM32F446xx/STM32F469xx/STM32F479xx/STM32F412Zx/STM32F412Vx/STM32F412Rx/STM32F412Cx devices.
  3305. * @note This function add the PLL/PLLR factor management
  3306. * @retval None
  3307. */
  3308. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  3309. {
  3310. /* Set all possible values for the Oscillator type parameter ---------------*/
  3311. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  3312. /* Get the HSE configuration -----------------------------------------------*/
  3313. if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  3314. {
  3315. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  3316. }
  3317. else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
  3318. {
  3319. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  3320. }
  3321. else
  3322. {
  3323. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  3324. }
  3325. /* Get the HSI configuration -----------------------------------------------*/
  3326. if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
  3327. {
  3328. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  3329. }
  3330. else
  3331. {
  3332. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  3333. }
  3334. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR &RCC_CR_HSITRIM) >> RCC_CR_HSITRIM_Pos);
  3335. /* Get the LSE configuration -----------------------------------------------*/
  3336. if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  3337. {
  3338. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  3339. }
  3340. else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  3341. {
  3342. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  3343. }
  3344. else
  3345. {
  3346. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  3347. }
  3348. /* Get the LSI configuration -----------------------------------------------*/
  3349. if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
  3350. {
  3351. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  3352. }
  3353. else
  3354. {
  3355. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  3356. }
  3357. /* Get the PLL configuration -----------------------------------------------*/
  3358. if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
  3359. {
  3360. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  3361. }
  3362. else
  3363. {
  3364. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  3365. }
  3366. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  3367. RCC_OscInitStruct->PLL.PLLM = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM);
  3368. RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  3369. RCC_OscInitStruct->PLL.PLLP = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) + RCC_PLLCFGR_PLLP_0) << 1U) >> RCC_PLLCFGR_PLLP_Pos);
  3370. RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos);
  3371. RCC_OscInitStruct->PLL.PLLR = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos);
  3372. }
  3373. #endif /* STM32F410xx || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  3374. #endif /* HAL_RCC_MODULE_ENABLED */
  3375. /**
  3376. * @}
  3377. */
  3378. /**
  3379. * @}
  3380. */
  3381. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/