stm32l4xx_hal_rcc_ex.c 123 KB

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