stm32f4xx_hal_rcc_ex.c 154 KB

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