stm32l4xx_hal_rcc_ex.c 122 KB

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