stm32wbxx_hal_rcc_ex.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328
  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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. * @attention
  13. *
  14. * Copyright (c) 2019 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file
  18. * in the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. *
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32wbxx_hal.h"
  25. /** @addtogroup STM32WBxx_HAL_Driver
  26. * @{
  27. */
  28. /** @defgroup RCCEx RCCEx
  29. * @brief RCC Extended HAL module driver
  30. * @{
  31. */
  32. #ifdef HAL_RCC_MODULE_ENABLED
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private defines -----------------------------------------------------------*/
  35. /** @defgroup RCCEx_Private_Constants RCCEx Private Constants
  36. * @{
  37. */
  38. #if defined(SAI1)
  39. #define PLLSAI1_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  40. #endif /* SAI1 */
  41. #define PLL_TIMEOUT_VALUE (2U) /* 2 ms (minimum Tick + 1) */
  42. #define CLOCKSMPS_TIMEOUT_VALUE (5000U) /* 5 s */
  43. #define __LSCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  44. #define LSCO1_GPIO_PORT GPIOA
  45. #define LSCO1_PIN GPIO_PIN_2
  46. #define __LSCO2_CLK_ENABLE() __HAL_RCC_GPIOH_CLK_ENABLE()
  47. #define LSCO2_GPIO_PORT GPIOH
  48. #define LSCO2_PIN GPIO_PIN_3
  49. #if defined(RCC_LSCO3_SUPPORT)
  50. #define __LSCO3_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  51. #define LSCO3_GPIO_PORT GPIOC
  52. #define LSCO3_PIN GPIO_PIN_12
  53. #endif /* RCC_LSCO3_SUPPORT */
  54. #define LSI2_TIMEOUT_VALUE (3U) /* to be adjusted with DS */
  55. /**
  56. * @}
  57. */
  58. /* Private macros ------------------------------------------------------------*/
  59. /* Private variables ---------------------------------------------------------*/
  60. /* Private function prototypes -----------------------------------------------*/
  61. /** @defgroup RCCEx_Private_Functions RCCEx Private Functions
  62. * @{
  63. */
  64. #if defined(SAI1)
  65. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNP(RCC_PLLSAI1InitTypeDef *PLLSAI1);
  66. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNQ(RCC_PLLSAI1InitTypeDef *PLLSAI1);
  67. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNR(RCC_PLLSAI1InitTypeDef *PLLSAI1);
  68. #endif /* SAI1 */
  69. static uint32_t RCC_PLL_GetFreqDomain_P(void);
  70. static uint32_t RCC_PLL_GetFreqDomain_Q(void);
  71. #if defined(SAI1)
  72. static uint32_t RCC_PLLSAI1_GetFreqDomain_R(void);
  73. static uint32_t RCC_PLLSAI1_GetFreqDomain_P(void);
  74. static uint32_t RCC_PLLSAI1_GetFreqDomain_Q(void);
  75. #endif /* SAI1 */
  76. /**
  77. * @}
  78. */
  79. /* Exported functions --------------------------------------------------------*/
  80. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  81. * @{
  82. */
  83. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  84. * @brief Extended Peripheral Control functions
  85. *
  86. @verbatim
  87. ===============================================================================
  88. ##### Extended Peripheral Control functions #####
  89. ===============================================================================
  90. [..]
  91. This subsection provides a set of functions allowing to control the RCC Clocks
  92. frequencies.
  93. [..]
  94. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  95. select the RTC clock source; in this case the Backup domain will be reset in
  96. order to modify the RTC Clock source, as consequence RTC registers (including
  97. the backup registers) and RCC_BDCR register are set to their reset values.
  98. @endverbatim
  99. * @{
  100. */
  101. /**
  102. * @brief Initialize the RCC extended peripherals clocks according to the specified
  103. * parameters in the @ref RCC_PeriphCLKInitTypeDef.
  104. * @param PeriphClkInit pointer to a @ref RCC_PeriphCLKInitTypeDef structure that
  105. * contains a field PeriphClockSelection which can be a combination of the following values:
  106. *
  107. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  108. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock
  109. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  110. * @arg @ref RCC_PERIPHCLK_I2C3 I2C3 peripheral clock
  111. * @arg @ref RCC_PERIPHCLK_LPTIM1 LPTIM1 peripheral clock
  112. * @arg @ref RCC_PERIPHCLK_LPTIM2 LPTIM2 peripheral clock
  113. * @arg @ref RCC_PERIPHCLK_SAI1 SAI1 peripheral clock
  114. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  115. * @arg @ref RCC_PERIPHCLK_RNG RNG peripheral clock
  116. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  117. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  118. * @arg @ref RCC_PERIPHCLK_RFWAKEUP RFWKP peripheral clock
  119. * @arg @ref RCC_PERIPHCLK_SMPS SMPS peripheral clock
  120. *
  121. *
  122. * @note Care must be taken when @ref HAL_RCCEx_PeriphCLKConfig() is used to select
  123. * the RTC clock source: in this case the access to Backup domain is enabled.
  124. *
  125. * @retval HAL status
  126. */
  127. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  128. {
  129. uint32_t tickstart;
  130. HAL_StatusTypeDef ret = HAL_OK; /* Intermediate status */
  131. HAL_StatusTypeDef status = HAL_OK; /* Final status */
  132. /* Check the parameters */
  133. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  134. #if defined(SAI1)
  135. /*-------------------------- SAI1 clock source configuration ---------------------*/
  136. if ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SAI1) == RCC_PERIPHCLK_SAI1))
  137. {
  138. /* Check the parameters */
  139. assert_param(IS_RCC_SAI1CLK(PeriphClkInit->Sai1ClockSelection));
  140. switch (PeriphClkInit->Sai1ClockSelection)
  141. {
  142. case RCC_SAI1CLKSOURCE_PLL: /* PLL is used as clock source for SAI1 */
  143. /* Enable SAI1 Clock output generated form System PLL . */
  144. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SAI1CLK);
  145. /* SAI1 clock source config set later after clock selection check */
  146. break;
  147. case RCC_SAI1CLKSOURCE_PLLSAI1: /* PLLSAI1 is used as clock source for SAI1 */
  148. /* PLLSAI1 parameters N & P configuration and clock output (PLLSAI1ClockOut) */
  149. ret = RCCEx_PLLSAI1_ConfigNP(&(PeriphClkInit->PLLSAI1));
  150. /* SAI1 clock source config set later after clock selection check */
  151. break;
  152. case RCC_SAI1CLKSOURCE_PIN: /* External clock is used as source of SAI1 clock*/
  153. /* SAI1 clock source config set later after clock selection check */
  154. break;
  155. case RCC_SAI1CLKSOURCE_HSI:
  156. break;
  157. default:
  158. ret = HAL_ERROR;
  159. break;
  160. }
  161. if (ret == HAL_OK)
  162. {
  163. /* Set the source of SAI1 clock*/
  164. __HAL_RCC_SAI1_CONFIG(PeriphClkInit->Sai1ClockSelection);
  165. }
  166. else
  167. {
  168. /* set overall return value */
  169. status = ret;
  170. }
  171. }
  172. #endif /* SAI1 */
  173. /*-------------------------- RTC clock source configuration ----------------------*/
  174. if ((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC)
  175. {
  176. uint32_t rtcclocksource = LL_RCC_GetRTCClockSource();
  177. /* Check for RTC Parameters used to output RTCCLK */
  178. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  179. /* Configure the clock source only if a different source is expected */
  180. if (rtcclocksource != PeriphClkInit->RTCClockSelection)
  181. {
  182. /* Enable write access to Backup domain */
  183. HAL_PWR_EnableBkUpAccess();
  184. /* If a clock source is not yet selected */
  185. if (rtcclocksource == RCC_RTCCLKSOURCE_NONE)
  186. {
  187. /* Directly set the configuration of the clock source selection */
  188. LL_RCC_SetRTCClockSource(PeriphClkInit->RTCClockSelection);
  189. }
  190. else /* A clock source is already selected */
  191. {
  192. /* Store the content of BDCR register before the reset of Backup Domain */
  193. uint32_t bdcr = LL_RCC_ReadReg(BDCR);
  194. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  195. LL_RCC_ForceBackupDomainReset();
  196. LL_RCC_ReleaseBackupDomainReset();
  197. /* Set the value of the clock source selection */
  198. MODIFY_REG(bdcr, RCC_BDCR_RTCSEL, PeriphClkInit->RTCClockSelection);
  199. /* Restore the content of BDCR register */
  200. LL_RCC_WriteReg(BDCR, bdcr);
  201. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  202. if (LL_RCC_LSE_IsEnabled() == 1U)
  203. {
  204. /* Get Start Tick*/
  205. tickstart = HAL_GetTick();
  206. /* Wait till LSE is ready */
  207. while (LL_RCC_LSE_IsReady() != 1U)
  208. {
  209. if ((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  210. {
  211. ret = HAL_TIMEOUT;
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. /* set overall return value */
  218. status = ret;
  219. }
  220. else
  221. {
  222. /* set overall return value */
  223. status = ret;
  224. }
  225. }
  226. /*-------------------------- USART1 clock source configuration -------------------*/
  227. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART1) == RCC_PERIPHCLK_USART1)
  228. {
  229. /* Check the parameters */
  230. assert_param(IS_RCC_USART1CLKSOURCE(PeriphClkInit->Usart1ClockSelection));
  231. /* Configure the USART1 clock source */
  232. __HAL_RCC_USART1_CONFIG(PeriphClkInit->Usart1ClockSelection);
  233. }
  234. #if defined(LPUART1)
  235. /*-------------------------- LPUART1 clock source configuration ------------------*/
  236. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPUART1) == RCC_PERIPHCLK_LPUART1)
  237. {
  238. /* Check the parameters */
  239. assert_param(IS_RCC_LPUART1CLKSOURCE(PeriphClkInit->Lpuart1ClockSelection));
  240. /* Configure the LPUAR1 clock source */
  241. __HAL_RCC_LPUART1_CONFIG(PeriphClkInit->Lpuart1ClockSelection);
  242. }
  243. #endif /* LPUART1 */
  244. /*-------------------------- LPTIM1 clock source configuration -------------------*/
  245. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM1) == (RCC_PERIPHCLK_LPTIM1))
  246. {
  247. assert_param(IS_RCC_LPTIM1CLK(PeriphClkInit->Lptim1ClockSelection));
  248. __HAL_RCC_LPTIM1_CONFIG(PeriphClkInit->Lptim1ClockSelection);
  249. }
  250. /*-------------------------- LPTIM2 clock source configuration -------------------*/
  251. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM2) == (RCC_PERIPHCLK_LPTIM2))
  252. {
  253. assert_param(IS_RCC_LPTIM2CLK(PeriphClkInit->Lptim2ClockSelection));
  254. __HAL_RCC_LPTIM2_CONFIG(PeriphClkInit->Lptim2ClockSelection);
  255. }
  256. /*-------------------------- I2C1 clock source configuration ---------------------*/
  257. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C1) == RCC_PERIPHCLK_I2C1)
  258. {
  259. /* Check the parameters */
  260. assert_param(IS_RCC_I2C1CLKSOURCE(PeriphClkInit->I2c1ClockSelection));
  261. /* Configure the I2C1 clock source */
  262. __HAL_RCC_I2C1_CONFIG(PeriphClkInit->I2c1ClockSelection);
  263. }
  264. #if defined(I2C3)
  265. /*-------------------------- I2C3 clock source configuration ---------------------*/
  266. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C3) == RCC_PERIPHCLK_I2C3)
  267. {
  268. /* Check the parameters */
  269. assert_param(IS_RCC_I2C3CLKSOURCE(PeriphClkInit->I2c3ClockSelection));
  270. /* Configure the I2C3 clock source */
  271. __HAL_RCC_I2C3_CONFIG(PeriphClkInit->I2c3ClockSelection);
  272. }
  273. #endif /* I2C3 */
  274. #if defined(USB)
  275. /*-------------------------- USB clock source configuration ----------------------*/
  276. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == (RCC_PERIPHCLK_USB))
  277. {
  278. assert_param(IS_RCC_USBCLKSOURCE(PeriphClkInit->UsbClockSelection));
  279. __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
  280. if (PeriphClkInit->UsbClockSelection == RCC_USBCLKSOURCE_PLL)
  281. {
  282. /* Enable PLLQ output */
  283. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_USBCLK);
  284. }
  285. #if defined(SAI1)
  286. if (PeriphClkInit->UsbClockSelection == RCC_USBCLKSOURCE_PLLSAI1)
  287. {
  288. /* PLLSAI1 parameters N & Q configuration and clock output (PLLSAI1ClockOut) */
  289. ret = RCCEx_PLLSAI1_ConfigNQ(&(PeriphClkInit->PLLSAI1));
  290. if (ret != HAL_OK)
  291. {
  292. /* set overall return value */
  293. status = ret;
  294. }
  295. }
  296. #endif /* SAI1 */
  297. }
  298. #endif /* USB */
  299. /*-------------------------- RNG clock source configuration ----------------------*/
  300. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RNG) == (RCC_PERIPHCLK_RNG))
  301. {
  302. /* Check the parameters */
  303. assert_param(IS_RCC_RNGCLKSOURCE(PeriphClkInit->RngClockSelection));
  304. /* Configure the RNG clock source */
  305. __HAL_RCC_RNG_CONFIG(PeriphClkInit->RngClockSelection);
  306. if (PeriphClkInit->RngClockSelection == RCC_RNGCLKSOURCE_PLL)
  307. {
  308. /* Enable PLLQ output */
  309. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_RNGCLK);
  310. }
  311. }
  312. /*-------------------------- ADC clock source configuration ----------------------*/
  313. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC) == RCC_PERIPHCLK_ADC)
  314. {
  315. /* Check the parameters */
  316. assert_param(IS_RCC_ADCCLKSOURCE(PeriphClkInit->AdcClockSelection));
  317. /* Configure the ADC interface clock source */
  318. __HAL_RCC_ADC_CONFIG(PeriphClkInit->AdcClockSelection);
  319. if (PeriphClkInit->AdcClockSelection == RCC_ADCCLKSOURCE_PLL)
  320. {
  321. /* Enable RCC_PLL_RNGCLK output */
  322. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_ADCCLK);
  323. }
  324. #if defined(SAI1)
  325. if (PeriphClkInit->AdcClockSelection == RCC_ADCCLKSOURCE_PLLSAI1)
  326. {
  327. /* PLLSAI1 parameters N & R configuration and clock output (PLLSAI1ClockOut) */
  328. ret = RCCEx_PLLSAI1_ConfigNR(&(PeriphClkInit->PLLSAI1));
  329. if (ret != HAL_OK)
  330. {
  331. /* set overall return value */
  332. status = ret;
  333. }
  334. }
  335. #endif /* SAI1 */
  336. }
  337. /*-------------------------- RFWKP clock source configuration ----------------------*/
  338. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RFWAKEUP) == RCC_PERIPHCLK_RFWAKEUP)
  339. {
  340. /* Check the parameters */
  341. assert_param(IS_RCC_RFWKPCLKSOURCE(PeriphClkInit->RFWakeUpClockSelection));
  342. /* Configure the RFWKP interface clock source */
  343. __HAL_RCC_RFWAKEUP_CONFIG(PeriphClkInit->RFWakeUpClockSelection);
  344. }
  345. #if defined(RCC_SMPS_SUPPORT)
  346. /*-------------------------- SMPS clock source configuration ----------------------*/
  347. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SMPS) == RCC_PERIPHCLK_SMPS)
  348. {
  349. /* Check the parameters */
  350. assert_param(IS_RCC_SMPSCLKDIV(PeriphClkInit->SmpsDivSelection));
  351. assert_param(IS_RCC_SMPSCLKSOURCE(PeriphClkInit->SmpsClockSelection));
  352. /* Configure the SMPS interface clock division factor */
  353. __HAL_RCC_SMPS_DIV_CONFIG(PeriphClkInit->SmpsDivSelection);
  354. /* Configure the SMPS interface clock source */
  355. __HAL_RCC_SMPS_CONFIG(PeriphClkInit->SmpsClockSelection);
  356. }
  357. #endif /* RCC_SMPS_SUPPORT */
  358. return status;
  359. }
  360. /**
  361. * @brief Get the RCC_ClkInitStruct according to the internal RCC configuration registers.
  362. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  363. * returns the configuration information for the Extended Peripherals
  364. * clocks(SAI1, LPTIM1, LPTIM2, I2C1, I2C3, LPUART1,
  365. * USART1, RTC, ADCx, USB, RNG, RFWKP, SMPS).
  366. * @retval None
  367. */
  368. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  369. {
  370. /* Set all possible values for the extended clock type parameter------------*/
  371. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_I2C1 | \
  372. RCC_PERIPHCLK_LPTIM1 | RCC_PERIPHCLK_LPTIM2 | \
  373. RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC | \
  374. RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_RFWAKEUP;
  375. #if defined(LPUART1)
  376. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_LPUART1;
  377. #endif /* LPUART1 */
  378. #if defined(I2C3)
  379. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2C3;
  380. #endif /* I2C3 */
  381. #if defined(SAI1)
  382. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_SAI1;
  383. #endif /* SAI1 */
  384. #if defined(USB)
  385. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USB;
  386. #endif /* USB */
  387. #if defined(RCC_SMPS_SUPPORT)
  388. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_SMPS;
  389. #endif /* RCC_SMPS_SUPPORT */
  390. #if defined(SAI1)
  391. /* Get the PLLSAI1 Clock configuration -----------------------------------------------*/
  392. PeriphClkInit->PLLSAI1.PLLN = LL_RCC_PLLSAI1_GetN();
  393. PeriphClkInit->PLLSAI1.PLLP = LL_RCC_PLLSAI1_GetP();
  394. PeriphClkInit->PLLSAI1.PLLR = LL_RCC_PLLSAI1_GetR();
  395. PeriphClkInit->PLLSAI1.PLLQ = LL_RCC_PLLSAI1_GetQ();
  396. #endif /* SAI1 */
  397. /* Get the USART1 clock source ---------------------------------------------*/
  398. PeriphClkInit->Usart1ClockSelection = __HAL_RCC_GET_USART1_SOURCE();
  399. #if defined(LPUART1)
  400. /* Get the LPUART1 clock source --------------------------------------------*/
  401. PeriphClkInit->Lpuart1ClockSelection = __HAL_RCC_GET_LPUART1_SOURCE();
  402. #endif /* LPUART1 */
  403. /* Get the I2C1 clock source -----------------------------------------------*/
  404. PeriphClkInit->I2c1ClockSelection = __HAL_RCC_GET_I2C1_SOURCE();
  405. #if defined(I2C3)
  406. /* Get the I2C3 clock source -----------------------------------------------*/
  407. PeriphClkInit->I2c3ClockSelection = __HAL_RCC_GET_I2C3_SOURCE();
  408. #endif /* I2C3 */
  409. /* Get the LPTIM1 clock source ---------------------------------------------*/
  410. PeriphClkInit->Lptim1ClockSelection = __HAL_RCC_GET_LPTIM1_SOURCE();
  411. /* Get the LPTIM2 clock source ---------------------------------------------*/
  412. PeriphClkInit->Lptim2ClockSelection = __HAL_RCC_GET_LPTIM2_SOURCE();
  413. #if defined(SAI1)
  414. /* Get the SAI1 clock source -----------------------------------------------*/
  415. PeriphClkInit->Sai1ClockSelection = __HAL_RCC_GET_SAI1_SOURCE();
  416. #endif /* SAI1 */
  417. /* Get the RTC clock source ------------------------------------------------*/
  418. PeriphClkInit->RTCClockSelection = __HAL_RCC_GET_RTC_SOURCE();
  419. #if defined(USB)
  420. /* Get the USB clock source ------------------------------------------------*/
  421. PeriphClkInit->UsbClockSelection = __HAL_RCC_GET_USB_SOURCE();
  422. #endif /* USB */
  423. /* Get the RNG clock source ------------------------------------------------*/
  424. PeriphClkInit->RngClockSelection = HAL_RCCEx_GetRngCLKSource();
  425. /* Get the ADC clock source ------------------------------------------------*/
  426. PeriphClkInit->AdcClockSelection = __HAL_RCC_GET_ADC_SOURCE();
  427. /* Get the RFWKP clock source ----------------------------------------------*/
  428. PeriphClkInit->RFWakeUpClockSelection = __HAL_RCC_GET_RFWAKEUP_SOURCE();
  429. #if defined(RCC_SMPS_SUPPORT)
  430. /* Get the SMPS clock division factor --------------------------------------*/
  431. PeriphClkInit->SmpsDivSelection = __HAL_RCC_GET_SMPS_DIV();
  432. /* Get the SMPS clock source -----------------------------------------------*/
  433. PeriphClkInit->SmpsClockSelection = __HAL_RCC_GET_SMPS_SOURCE();
  434. #endif /* RCC_SMPS_SUPPORT */
  435. }
  436. /**
  437. * @brief Return the peripheral clock frequency for peripherals with clock source
  438. * @note Return 0 if peripheral clock identifier not managed by this API
  439. * @param PeriphClk Peripheral clock identifier
  440. * This parameter can be one of the following values:
  441. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  442. * @arg @ref RCC_PERIPHCLK_ADC ADC peripheral clock
  443. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  444. * @arg @ref RCC_PERIPHCLK_I2C3 I2C3 peripheral clock
  445. * @arg @ref RCC_PERIPHCLK_LPTIM1 LPTIM1 peripheral clock
  446. * @arg @ref RCC_PERIPHCLK_LPTIM2 LPTIM2 peripheral clock
  447. * @arg @ref RCC_PERIPHCLK_LPUART1 LPUART1 peripheral clock
  448. * @arg @ref RCC_PERIPHCLK_RNG RNG peripheral clock
  449. * @arg @ref RCC_PERIPHCLK_SAI1 SAI1 peripheral clock
  450. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  451. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  452. * @arg @ref RCC_PERIPHCLK_RFWAKEUP RFWKP peripheral clock
  453. * @arg @ref RCC_PERIPHCLK_SMPS SMPS peripheral clock
  454. * @retval Frequency in Hz
  455. */
  456. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  457. {
  458. uint32_t frequency = 0U;
  459. #if defined(RCC_SMPS_SUPPORT)
  460. uint32_t smps_prescaler_index = ((LL_RCC_GetSMPSPrescaler()) >> RCC_SMPSCR_SMPSDIV_Pos);
  461. #endif /* RCC_SMPS_SUPPORT */
  462. /* Check the parameters */
  463. assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
  464. if (PeriphClk == RCC_PERIPHCLK_RTC)
  465. {
  466. uint32_t rtcClockSource = LL_RCC_GetRTCClockSource();
  467. if (rtcClockSource == LL_RCC_RTC_CLKSOURCE_LSE) /* LSE clock used as RTC clock source */
  468. {
  469. if (LL_RCC_LSE_IsReady() == 1U)
  470. {
  471. frequency = LSE_VALUE;
  472. }
  473. else
  474. {
  475. /* Nothing to do as frequency already initialized to 0U */
  476. }
  477. }
  478. else if (rtcClockSource == LL_RCC_RTC_CLKSOURCE_LSI) /* LSI clock used as RTC clock source */
  479. {
  480. const uint32_t temp_lsi1ready = LL_RCC_LSI1_IsReady();
  481. const uint32_t temp_lsi2ready = LL_RCC_LSI2_IsReady();
  482. if ((temp_lsi1ready == 1U) || (temp_lsi2ready == 1U))
  483. {
  484. frequency = LSI_VALUE;
  485. }
  486. else
  487. {
  488. /* Nothing to do as frequency already initialized to 0U */
  489. }
  490. }
  491. else if (rtcClockSource == LL_RCC_RTC_CLKSOURCE_HSE_DIV32) /* HSE clock used as RTC clock source */
  492. {
  493. frequency = HSE_VALUE / 32U;
  494. }
  495. else /* No clock used as RTC clock source */
  496. {
  497. /* Nothing to do as frequency already initialized to 0U */
  498. }
  499. }
  500. #if defined(SAI1)
  501. else if (PeriphClk == RCC_PERIPHCLK_SAI1)
  502. {
  503. switch (LL_RCC_GetSAIClockSource(LL_RCC_SAI1_CLKSOURCE))
  504. {
  505. case LL_RCC_SAI1_CLKSOURCE_HSI: /* HSI clock used as SAI1 clock source */
  506. if (LL_RCC_HSI_IsReady() == 1U)
  507. {
  508. frequency = HSI_VALUE;
  509. }
  510. else
  511. {
  512. /* Nothing to do as frequency already initialized to 0U */
  513. }
  514. break;
  515. case LL_RCC_SAI1_CLKSOURCE_PLLSAI1: /* PLLSAI1 clock used as SAI1 clock source */
  516. if (LL_RCC_PLLSAI1_IsReady() == 1U)
  517. {
  518. frequency = RCC_PLLSAI1_GetFreqDomain_P();
  519. }
  520. else
  521. {
  522. /* Nothing to do as frequency already initialized to 0U */
  523. }
  524. break;
  525. case LL_RCC_SAI1_CLKSOURCE_PLL: /* PLL clock used as SAI1 clock source */
  526. if (LL_RCC_PLL_IsReady() == 1U)
  527. {
  528. frequency = RCC_PLL_GetFreqDomain_P();
  529. }
  530. else
  531. {
  532. /* Nothing to do as frequency already initialized to 0U */
  533. }
  534. break;
  535. default: /* External input clock used as SAI1 clock source */
  536. frequency = EXTERNAL_SAI1_CLOCK_VALUE;
  537. break;
  538. }
  539. }
  540. #endif /* SAI1 */
  541. else if (PeriphClk == RCC_PERIPHCLK_RNG)
  542. {
  543. uint32_t rngClockSource = HAL_RCCEx_GetRngCLKSource();
  544. if (rngClockSource == RCC_RNGCLKSOURCE_LSI) /* LSI clock used as RNG clock source */
  545. {
  546. const uint32_t temp_lsi1ready = LL_RCC_LSI1_IsReady();
  547. const uint32_t temp_lsi2ready = LL_RCC_LSI2_IsReady();
  548. if ((temp_lsi1ready == 1U) || (temp_lsi2ready == 1U))
  549. {
  550. frequency = LSI_VALUE;
  551. }
  552. else
  553. {
  554. /* Nothing to do as frequency already initialized to 0U */
  555. }
  556. }
  557. else if (rngClockSource == RCC_RNGCLKSOURCE_LSE) /* LSE clock used as RNG clock source */
  558. {
  559. if (LL_RCC_LSE_IsReady() == 1U)
  560. {
  561. frequency = LSE_VALUE;
  562. }
  563. else
  564. {
  565. /* Nothing to do as frequency already initialized to 0U */
  566. }
  567. }
  568. else if (rngClockSource == RCC_RNGCLKSOURCE_PLL) /* PLL clock divided by 3 used as RNG clock source */
  569. {
  570. if (LL_RCC_PLL_IsReady() == 1U)
  571. {
  572. frequency = (RCC_PLL_GetFreqDomain_Q() / 3U);
  573. }
  574. else
  575. {
  576. /* Nothing to do as frequency already initialized to 0U */
  577. }
  578. }
  579. else if (rngClockSource == RCC_RNGCLKSOURCE_MSI) /* MSI clock divided by 3 used as RNG clock source */
  580. {
  581. if (LL_RCC_MSI_IsReady() == 1U)
  582. {
  583. frequency = (__LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange()) / 3U);
  584. }
  585. else
  586. {
  587. /* Nothing to do as frequency already initialized to 0U */
  588. }
  589. }
  590. #if defined(SAI1)
  591. else if (rngClockSource == RCC_RNGCLKSOURCE_PLLSAI1) /* PLLSAI1 clock used as SAI1 clock source */
  592. {
  593. if (LL_RCC_PLLSAI1_IsReady() == 1U)
  594. {
  595. frequency = RCC_PLLSAI1_GetFreqDomain_Q();
  596. }
  597. else
  598. {
  599. /* Nothing to do as frequency already initialized to 0U */
  600. }
  601. }
  602. #endif /* SAI1 */
  603. else /* HSI48 clock divided by 3 used as RNG clock source */
  604. {
  605. #if defined(RCC_HSI48_SUPPORT)
  606. if (LL_RCC_HSI48_IsReady() == 1U)
  607. {
  608. frequency = HSI48_VALUE / 3U;
  609. }
  610. else
  611. {
  612. /* Nothing to do as frequency already initialized to 0U */
  613. }
  614. #else
  615. /* Nothing to do as frequency already initialized to 0U */
  616. #endif /* RCC_HSI48_SUPPORT */
  617. }
  618. }
  619. #if defined(USB)
  620. else if (PeriphClk == RCC_PERIPHCLK_USB)
  621. {
  622. switch (LL_RCC_GetUSBClockSource(LL_RCC_USB_CLKSOURCE))
  623. {
  624. #if defined(SAI1)
  625. case LL_RCC_USB_CLKSOURCE_PLLSAI1: /* PLLSAI1 clock used as USB clock source */
  626. if (LL_RCC_PLLSAI1_IsReady() == 1U)
  627. {
  628. frequency = RCC_PLLSAI1_GetFreqDomain_Q();
  629. }
  630. else
  631. {
  632. /* Nothing to do as frequency already initialized to 0U */
  633. }
  634. break;
  635. #endif /* SAI1 */
  636. case LL_RCC_USB_CLKSOURCE_PLL: /* PLL clock used as USB clock source */
  637. if (LL_RCC_PLL_IsReady() == 1U)
  638. {
  639. frequency = RCC_PLL_GetFreqDomain_Q();
  640. }
  641. else
  642. {
  643. /* Nothing to do as frequency already initialized to 0U */
  644. }
  645. break;
  646. case LL_RCC_USB_CLKSOURCE_MSI: /* MSI clock used as USB clock source */
  647. if (LL_RCC_MSI_IsReady() == 1U)
  648. {
  649. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  650. }
  651. else
  652. {
  653. /* Nothing to do as frequency already initialized to 0U */
  654. }
  655. break;
  656. default: /* HSI48 clock used as USB clock source */
  657. if (LL_RCC_HSI48_IsReady() == 1U)
  658. {
  659. frequency = HSI48_VALUE;
  660. }
  661. else
  662. {
  663. /* Nothing to do as frequency already initialized to 0U */
  664. }
  665. break;
  666. }
  667. }
  668. #endif /* USB */
  669. else if (PeriphClk == RCC_PERIPHCLK_USART1)
  670. {
  671. switch (LL_RCC_GetUSARTClockSource(LL_RCC_USART1_CLKSOURCE))
  672. {
  673. case LL_RCC_USART1_CLKSOURCE_SYSCLK: /* USART1 Clock is System Clock */
  674. frequency = HAL_RCC_GetSysClockFreq();
  675. break;
  676. case LL_RCC_USART1_CLKSOURCE_HSI: /* USART1 Clock is HSI Osc. */
  677. if (LL_RCC_HSI_IsReady() == 1U)
  678. {
  679. frequency = HSI_VALUE;
  680. }
  681. else
  682. {
  683. /* Nothing to do as frequency already initialized to 0U */
  684. }
  685. break;
  686. case LL_RCC_USART1_CLKSOURCE_LSE: /* USART1 Clock is LSE Osc. */
  687. if (LL_RCC_LSE_IsReady() == 1U)
  688. {
  689. frequency = LSE_VALUE;
  690. }
  691. else
  692. {
  693. /* Nothing to do as frequency already initialized to 0U */
  694. }
  695. break;
  696. default: /* USART1 Clock is PCLK2 */
  697. frequency = __LL_RCC_CALC_PCLK2_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(),
  698. LL_RCC_GetAHBPrescaler()),
  699. LL_RCC_GetAPB2Prescaler());
  700. break;
  701. }
  702. }
  703. #if defined(LPUART1)
  704. else if (PeriphClk == RCC_PERIPHCLK_LPUART1)
  705. {
  706. switch (LL_RCC_GetLPUARTClockSource(LL_RCC_LPUART1_CLKSOURCE))
  707. {
  708. case LL_RCC_LPUART1_CLKSOURCE_SYSCLK: /* LPUART1 Clock is System Clock */
  709. frequency = HAL_RCC_GetSysClockFreq();
  710. break;
  711. case LL_RCC_LPUART1_CLKSOURCE_HSI: /* LPUART1 Clock is HSI Osc. */
  712. if (LL_RCC_HSI_IsReady() == 1U)
  713. {
  714. frequency = HSI_VALUE;
  715. }
  716. else
  717. {
  718. /* Nothing to do as frequency already initialized to 0U */
  719. }
  720. break;
  721. case LL_RCC_LPUART1_CLKSOURCE_LSE: /* LPUART1 Clock is LSE Osc. */
  722. if (LL_RCC_LSE_IsReady() == 1U)
  723. {
  724. frequency = LSE_VALUE;
  725. }
  726. else
  727. {
  728. /* Nothing to do as frequency already initialized to 0U */
  729. }
  730. break;
  731. default: /* LPUART1 Clock is PCLK1 */
  732. frequency = __LL_RCC_CALC_PCLK1_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(),
  733. LL_RCC_GetAHBPrescaler()),
  734. LL_RCC_GetAPB1Prescaler());
  735. break;
  736. }
  737. }
  738. #endif /* LPUART1 */
  739. else if (PeriphClk == RCC_PERIPHCLK_ADC)
  740. {
  741. switch (LL_RCC_GetADCClockSource(LL_RCC_ADC_CLKSOURCE))
  742. {
  743. #if defined(STM32WB55xx) || defined (STM32WB5Mxx) || defined(STM32WB35xx)
  744. case LL_RCC_ADC_CLKSOURCE_PLLSAI1: /* PLLSAI1 clock used as ADC clock source */
  745. if (LL_RCC_PLLSAI1_IsReady() == 1U)
  746. {
  747. frequency = RCC_PLLSAI1_GetFreqDomain_R();
  748. }
  749. else
  750. {
  751. /* Nothing to do as frequency already initialized to 0U */
  752. }
  753. break;
  754. #elif defined(STM32WB15xx) || defined(STM32WB1Mxx)
  755. case LL_RCC_ADC_CLKSOURCE_HSI: /* HSI clock used as ADC clock source */
  756. if (LL_RCC_HSI_IsReady() == 1U)
  757. {
  758. frequency = HSI_VALUE;
  759. }
  760. else
  761. {
  762. /* Nothing to do as frequency already initialized to 0U */
  763. }
  764. break;
  765. #endif /* STM32WB55xx || STM32WB5Mxx || STM32WB35xx */
  766. case LL_RCC_ADC_CLKSOURCE_SYSCLK: /* SYSCLK clock used as ADC clock source */
  767. frequency = HAL_RCC_GetSysClockFreq();
  768. break;
  769. case LL_RCC_ADC_CLKSOURCE_PLL: /* PLL clock used as ADC clock source */
  770. if (LL_RCC_PLL_IsReady() == 1U)
  771. {
  772. frequency = RCC_PLL_GetFreqDomain_P();
  773. }
  774. else
  775. {
  776. /* Nothing to do as frequency already initialized to 0U */
  777. }
  778. break;
  779. default: /* No clock used as ADC clock source */
  780. break;
  781. }
  782. }
  783. else if (PeriphClk == RCC_PERIPHCLK_I2C1)
  784. {
  785. switch (LL_RCC_GetI2CClockSource(LL_RCC_I2C1_CLKSOURCE))
  786. {
  787. case LL_RCC_I2C1_CLKSOURCE_SYSCLK: /* I2C1 Clock is System Clock */
  788. frequency = HAL_RCC_GetSysClockFreq();
  789. break;
  790. case LL_RCC_I2C1_CLKSOURCE_HSI: /* I2C1 Clock is HSI Osc. */
  791. if (LL_RCC_HSI_IsReady() == 1U)
  792. {
  793. frequency = HSI_VALUE;
  794. }
  795. else
  796. {
  797. /* Nothing to do as frequency already initialized to 0U */
  798. }
  799. break;
  800. default: /* I2C1 Clock is PCLK1 */
  801. frequency = __LL_RCC_CALC_PCLK1_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(),
  802. LL_RCC_GetAHBPrescaler()),
  803. LL_RCC_GetAPB1Prescaler());
  804. break;
  805. }
  806. }
  807. #if defined(I2C3)
  808. else if (PeriphClk == RCC_PERIPHCLK_I2C3)
  809. {
  810. switch (LL_RCC_GetI2CClockSource(LL_RCC_I2C3_CLKSOURCE))
  811. {
  812. case LL_RCC_I2C3_CLKSOURCE_SYSCLK: /* I2C3 Clock is System Clock */
  813. frequency = HAL_RCC_GetSysClockFreq();
  814. break;
  815. case LL_RCC_I2C3_CLKSOURCE_HSI: /* I2C3 Clock is HSI Osc. */
  816. if (LL_RCC_HSI_IsReady() == 1U)
  817. {
  818. frequency = HSI_VALUE;
  819. }
  820. else
  821. {
  822. /* Nothing to do as frequency already initialized to 0U */
  823. }
  824. break;
  825. default: /* I2C3 Clock is PCLK1 */
  826. frequency = __LL_RCC_CALC_PCLK1_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(),
  827. LL_RCC_GetAHBPrescaler()),
  828. LL_RCC_GetAPB1Prescaler());
  829. break;
  830. }
  831. }
  832. #endif /* I2C3 */
  833. else if (PeriphClk == RCC_PERIPHCLK_LPTIM1)
  834. {
  835. uint32_t lptimClockSource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
  836. if (lptimClockSource == LL_RCC_LPTIM1_CLKSOURCE_LSI) /* LPTIM1 Clock is LSI Osc. */
  837. {
  838. const uint32_t temp_lsi1ready = LL_RCC_LSI1_IsReady();
  839. const uint32_t temp_lsi2ready = LL_RCC_LSI2_IsReady();
  840. if ((temp_lsi1ready == 1U) || (temp_lsi2ready == 1U))
  841. {
  842. frequency = LSI_VALUE;
  843. }
  844. else
  845. {
  846. /* Nothing to do as frequency already initialized to 0U */
  847. }
  848. }
  849. else if (lptimClockSource == LL_RCC_LPTIM1_CLKSOURCE_HSI) /* LPTIM1 Clock is HSI Osc. */
  850. {
  851. if (LL_RCC_HSI_IsReady() == 1U)
  852. {
  853. frequency = HSI_VALUE;
  854. }
  855. else
  856. {
  857. /* Nothing to do as frequency already initialized to 0U */
  858. }
  859. }
  860. else if (lptimClockSource == LL_RCC_LPTIM1_CLKSOURCE_LSE) /* LPTIM1 Clock is LSE Osc. */
  861. {
  862. if (LL_RCC_LSE_IsReady() == 1U)
  863. {
  864. frequency = LSE_VALUE;
  865. }
  866. else
  867. {
  868. /* Nothing to do as frequency already initialized to 0U */
  869. }
  870. }
  871. else /* LPTIM1 Clock is PCLK1 */
  872. {
  873. frequency = __LL_RCC_CALC_PCLK1_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(),
  874. LL_RCC_GetAHBPrescaler()),
  875. LL_RCC_GetAPB1Prescaler());
  876. }
  877. }
  878. else if (PeriphClk == RCC_PERIPHCLK_LPTIM2)
  879. {
  880. uint32_t lptimClockSource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE);
  881. if (lptimClockSource == LL_RCC_LPTIM2_CLKSOURCE_LSI) /* LPTIM2 Clock is LSI Osc. */
  882. {
  883. const uint32_t temp_lsi1ready = LL_RCC_LSI1_IsReady();
  884. const uint32_t temp_lsi2ready = LL_RCC_LSI2_IsReady();
  885. if ((temp_lsi1ready == 1U) || (temp_lsi2ready == 1U))
  886. {
  887. frequency = LSI_VALUE;
  888. }
  889. else
  890. {
  891. /* Nothing to do as frequency already initialized to 0U */
  892. }
  893. }
  894. else if (lptimClockSource == LL_RCC_LPTIM2_CLKSOURCE_HSI) /* LPTIM2 Clock is HSI Osc. */
  895. {
  896. if (LL_RCC_HSI_IsReady() == 1U)
  897. {
  898. frequency = HSI_VALUE;
  899. }
  900. else
  901. {
  902. /* Nothing to do as frequency already initialized to 0U */
  903. }
  904. }
  905. else if (lptimClockSource == LL_RCC_LPTIM2_CLKSOURCE_LSE) /* LPTIM2 Clock is LSE Osc. */
  906. {
  907. if (LL_RCC_LSE_IsReady() == 1U)
  908. {
  909. frequency = LSE_VALUE;
  910. }
  911. else
  912. {
  913. /* Nothing to do as frequency already initialized to 0U */
  914. }
  915. }
  916. else /* LPTIM2 Clock is PCLK1 */
  917. {
  918. frequency = __LL_RCC_CALC_PCLK1_FREQ(__LL_RCC_CALC_HCLK1_FREQ(HAL_RCC_GetSysClockFreq(),
  919. LL_RCC_GetAHBPrescaler()),
  920. LL_RCC_GetAPB1Prescaler());
  921. }
  922. }
  923. else if (PeriphClk == RCC_PERIPHCLK_RFWAKEUP)
  924. {
  925. uint32_t rfwkpClockSource = LL_RCC_GetRFWKPClockSource();
  926. if (rfwkpClockSource == LL_RCC_RFWKP_CLKSOURCE_LSE) /* LSE clock used as RF Wakeup clock source */
  927. {
  928. if (LL_RCC_LSE_IsReady() == 1U)
  929. {
  930. frequency = LSE_VALUE;
  931. }
  932. else
  933. {
  934. /* Nothing to do as frequency already initialized to 0U */
  935. }
  936. }
  937. else if (rfwkpClockSource == LL_RCC_RFWKP_CLKSOURCE_HSE_DIV1024) /* HSE clock used as RF Wakeup clock source */
  938. {
  939. frequency = HSE_VALUE / 1024U;
  940. }
  941. else /* No clock used as RF Wakeup clock source */
  942. {
  943. /* Nothing to do as frequency already initialized to 0U */
  944. }
  945. }
  946. #if defined(RCC_SMPS_SUPPORT)
  947. else if (PeriphClk == RCC_PERIPHCLK_SMPS)
  948. {
  949. uint32_t smpsClockSource = LL_RCC_GetSMPSClockSource();
  950. if (smpsClockSource == LL_RCC_SMPS_CLKSOURCE_STATUS_HSI) /* SMPS Clock source is HSI Osc. */
  951. {
  952. if (LL_RCC_HSI_IsReady() == 1U)
  953. {
  954. frequency = HSI_VALUE / SmpsPrescalerTable[smps_prescaler_index][0];
  955. frequency = frequency >> 1U; /* Systematic Div by 2 */
  956. }
  957. else
  958. {
  959. /* Nothing to do as frequency already initialized to 0U */
  960. }
  961. }
  962. else if (smpsClockSource == LL_RCC_SMPS_CLKSOURCE_STATUS_HSE) /* SMPS Clock source is HSE Osc. */
  963. {
  964. if (LL_RCC_HSE_IsReady() == 1U)
  965. {
  966. frequency = HSE_VALUE / SmpsPrescalerTable[smps_prescaler_index][5];
  967. frequency = frequency >> 1U; /* Systematic Div by 2 */
  968. }
  969. else
  970. {
  971. /* Nothing to do as frequency already initialized to 0U */
  972. }
  973. }
  974. else if (smpsClockSource == LL_RCC_SMPS_CLKSOURCE_STATUS_MSI) /* SMPS Clock source is MSI Osc. */
  975. {
  976. switch (LL_RCC_MSI_GetRange())
  977. {
  978. case LL_RCC_MSIRANGE_8:
  979. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSIRANGE_8) / SmpsPrescalerTable[smps_prescaler_index][4];
  980. break;
  981. case LL_RCC_MSIRANGE_9:
  982. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSIRANGE_9) / SmpsPrescalerTable[smps_prescaler_index][3];
  983. break;
  984. case LL_RCC_MSIRANGE_10:
  985. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSIRANGE_10) / SmpsPrescalerTable[smps_prescaler_index][2];
  986. break;
  987. case LL_RCC_MSIRANGE_11:
  988. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSIRANGE_11) / SmpsPrescalerTable[smps_prescaler_index][1];
  989. break;
  990. default:
  991. break;
  992. }
  993. frequency = frequency >> 1U; /* Systematic Div by 2 */
  994. }
  995. else /* SMPS has no Clock */
  996. {
  997. /* Nothing to do as frequency already initialized to 0U */
  998. }
  999. }
  1000. #endif /* RCC_SMPS_SUPPORT */
  1001. return (frequency);
  1002. }
  1003. /**
  1004. * @brief Return the RNG clock source
  1005. * @retval The RNG clock source can be one of the following values:
  1006. * @arg @ref RCC_RNGCLKSOURCE_HSI48 HSI48 clock divided by 3 selected as RNG clock
  1007. * @arg @ref RCC_RNGCLKSOURCE_PLL PLL "Q" clock divided by 3 selected as RNG clock
  1008. * @arg @ref RCC_RNGCLKSOURCE_MSI MSI clock divided by 3 selected as RNG clock
  1009. * @arg @ref RCC_RNGCLKSOURCE_PLLSAI1 PLLSAI1 "Q" clock selected as RNG clock (*)
  1010. * @arg @ref RCC_RNGCLKSOURCE_LSI LSI clock selected as RNG clock
  1011. * @arg @ref RCC_RNGCLKSOURCE_LSE LSE clock selected as RNG clock
  1012. *
  1013. * (*) Value not defined in all devices.
  1014. *
  1015. */
  1016. uint32_t HAL_RCCEx_GetRngCLKSource(void)
  1017. {
  1018. uint32_t rng_clock_source = LL_RCC_GetRNGClockSource(LL_RCC_RNG_CLKSOURCE);
  1019. uint32_t clk48_clock_source;
  1020. /* RNG clock source originates from 48 MHz RC oscillator */
  1021. if (rng_clock_source == RCC_RNGCLKSOURCE_CLK48)
  1022. {
  1023. clk48_clock_source = LL_RCC_GetCLK48ClockSource(LL_RCC_CLK48_CLKSOURCE);
  1024. rng_clock_source = (CLK48_MASK | clk48_clock_source);
  1025. }
  1026. return rng_clock_source;
  1027. }
  1028. /**
  1029. * @}
  1030. */
  1031. /** @defgroup RCCEx_Exported_Functions_Group2 Extended Clock management functions
  1032. * @brief Extended Clock management functions
  1033. *
  1034. @verbatim
  1035. ===============================================================================
  1036. ##### Extended clock management functions #####
  1037. ===============================================================================
  1038. [..]
  1039. This subsection provides a set of functions allowing to control the
  1040. activation or deactivation of MSI PLL-mode, PLLSAI1, PLLSAI12, LSE CSS,
  1041. Low speed clock output and clock after wake-up from STOP mode.
  1042. @endverbatim
  1043. * @{
  1044. */
  1045. #if defined(SAI1)
  1046. /**
  1047. * @brief Enable PLLSAI1.
  1048. * @param PLLSAI1Init pointer to an RCC_PLLSAI1InitTypeDef structure that
  1049. * contains the configuration information for the PLLSAI1
  1050. * @retval HAL status
  1051. */
  1052. HAL_StatusTypeDef HAL_RCCEx_EnablePLLSAI1(RCC_PLLSAI1InitTypeDef *PLLSAI1Init)
  1053. {
  1054. uint32_t tickstart;
  1055. HAL_StatusTypeDef status = HAL_OK;
  1056. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  1057. assert_param(IS_RCC_PLLN_VALUE(PLLSAI1Init->PLLN));
  1058. assert_param(IS_RCC_PLLP_VALUE(PLLSAI1Init->PLLP));
  1059. assert_param(IS_RCC_PLLQ_VALUE(PLLSAI1Init->PLLQ));
  1060. assert_param(IS_RCC_PLLR_VALUE(PLLSAI1Init->PLLR));
  1061. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PLLSAI1Init->PLLSAI1ClockOut));
  1062. /* Disable the PLLSAI1 */
  1063. __HAL_RCC_PLLSAI1_DISABLE();
  1064. /* Get Start Tick*/
  1065. tickstart = HAL_GetTick();
  1066. /* Wait till PLLSAI1 is ready to be updated */
  1067. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  1068. {
  1069. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1070. {
  1071. status = HAL_TIMEOUT;
  1072. break;
  1073. }
  1074. }
  1075. if (status == HAL_OK)
  1076. {
  1077. /* Configure the PLLSAI1 Multiplication factor N */
  1078. /* Configure the PLLSAI1 Division factors P, Q and R */
  1079. __HAL_RCC_PLLSAI1_CONFIG(PLLSAI1Init->PLLN, PLLSAI1Init->PLLP, PLLSAI1Init->PLLQ, PLLSAI1Init->PLLR);
  1080. /* Configure the PLLSAI1 Clock output(s) */
  1081. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PLLSAI1Init->PLLSAI1ClockOut);
  1082. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  1083. __HAL_RCC_PLLSAI1_ENABLE();
  1084. /* Get Start Tick*/
  1085. tickstart = HAL_GetTick();
  1086. /* Wait till PLLSAI1 is ready */
  1087. while (LL_RCC_PLLSAI1_IsReady() != 1U)
  1088. {
  1089. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1090. {
  1091. status = HAL_TIMEOUT;
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. return status;
  1097. }
  1098. /**
  1099. * @brief Disable PLLSAI1.
  1100. * @retval HAL status
  1101. */
  1102. HAL_StatusTypeDef HAL_RCCEx_DisablePLLSAI1(void)
  1103. {
  1104. uint32_t tickstart;
  1105. HAL_StatusTypeDef status = HAL_OK;
  1106. /* Disable the PLLSAI1 */
  1107. __HAL_RCC_PLLSAI1_DISABLE();
  1108. /* Get Start Tick*/
  1109. tickstart = HAL_GetTick();
  1110. /* Wait till PLLSAI1 is ready */
  1111. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  1112. {
  1113. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1114. {
  1115. status = HAL_TIMEOUT;
  1116. break;
  1117. }
  1118. }
  1119. /* Disable the PLLSAI1 Clock outputs */
  1120. __HAL_RCC_PLLSAI1CLKOUT_DISABLE(RCC_PLLSAI1_SAI1CLK | RCC_PLLSAI1_USBCLK | RCC_PLLSAI1_ADCCLK);
  1121. return status;
  1122. }
  1123. #endif /* SAI1 */
  1124. /***********************************************************************************************/
  1125. /**
  1126. * @brief Configure the oscillator clock source for wakeup from Stop and CSS backup clock.
  1127. * @param WakeUpClk Wakeup clock
  1128. * This parameter can be one of the following values:
  1129. * @arg @ref RCC_STOP_WAKEUPCLOCK_MSI MSI oscillator selection
  1130. * @arg @ref RCC_STOP_WAKEUPCLOCK_HSI HSI oscillator selection
  1131. * @note This function shall not be called after the Clock Security System on HSE has been
  1132. * enabled.
  1133. * @retval None
  1134. */
  1135. void HAL_RCCEx_WakeUpStopCLKConfig(uint32_t WakeUpClk)
  1136. {
  1137. assert_param(IS_RCC_STOP_WAKEUPCLOCK(WakeUpClk));
  1138. __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(WakeUpClk);
  1139. }
  1140. /**
  1141. * @brief Enable the LSE Clock Security System.
  1142. * @note Prior to enable the LSE Clock Security System, LSE oscillator is to be enabled
  1143. * with HAL_RCC_OscConfig() and the LSE oscillator clock is to be selected as RTC
  1144. * clock with HAL_RCCEx_PeriphCLKConfig().
  1145. * @retval None
  1146. */
  1147. void HAL_RCCEx_EnableLSECSS(void)
  1148. {
  1149. LL_RCC_LSE_EnableCSS();
  1150. }
  1151. /**
  1152. * @brief Disable the LSE Clock Security System.
  1153. * @note LSE Clock Security System can only be disabled after a LSE failure detection.
  1154. * @retval None
  1155. */
  1156. void HAL_RCCEx_DisableLSECSS(void)
  1157. {
  1158. LL_RCC_LSE_DisableCSS();
  1159. /* Disable LSE CSS IT if any */
  1160. __HAL_RCC_DISABLE_IT(RCC_IT_LSECSS);
  1161. }
  1162. /**
  1163. * @brief Enable the LSE Clock Security System Interrupt & corresponding EXTI line.
  1164. * @note LSE Clock Security System Interrupt is mapped on RTC EXTI line 18
  1165. * @retval None
  1166. */
  1167. void HAL_RCCEx_EnableLSECSS_IT(void)
  1168. {
  1169. /* Enable LSE CSS */
  1170. LL_RCC_LSE_EnableCSS();
  1171. /* Enable LSE CSS IT */
  1172. __HAL_RCC_ENABLE_IT(RCC_IT_LSECSS);
  1173. /* Enable IT on EXTI Line 18 */
  1174. __HAL_RCC_LSECSS_EXTI_ENABLE_IT();
  1175. __HAL_RCC_LSECSS_EXTI_ENABLE_RISING_EDGE();
  1176. }
  1177. /**
  1178. * @brief Handle the RCC LSE Clock Security System interrupt request.
  1179. * @retval None
  1180. */
  1181. void HAL_RCCEx_LSECSS_IRQHandler(void)
  1182. {
  1183. /* Check RCC LSE CSSF flag */
  1184. if (__HAL_RCC_GET_IT(RCC_IT_LSECSS))
  1185. {
  1186. /* RCC LSE Clock Security System interrupt user callback */
  1187. HAL_RCCEx_LSECSS_Callback();
  1188. /* Clear RCC LSE CSS pending bit */
  1189. __HAL_RCC_CLEAR_IT(RCC_IT_LSECSS);
  1190. }
  1191. }
  1192. /**
  1193. * @brief RCCEx LSE Clock Security System interrupt callback.
  1194. * @retval none
  1195. */
  1196. __weak void HAL_RCCEx_LSECSS_Callback(void)
  1197. {
  1198. /* NOTE : This function should not be modified, when the callback is needed,
  1199. the HAL_RCCEx_LSECSS_Callback should be implemented in the user file
  1200. */
  1201. }
  1202. /**
  1203. * @brief Select the clock source to output on LSCO1 pin(PA2) or LSC02 pin (PH3) or LSCO3 pin (PC12).
  1204. * @note PA2, PH3 or PC12 should be configured in alternate function mode.
  1205. * @param RCC_LSCOx specifies the output direction for the clock source.
  1206. * @arg @ref RCC_LSCO1 Clock source to output on LSCO1 pin(PA2)
  1207. * @arg @ref RCC_LSCO2 Clock source to output on LSCO2 pin(PH3)
  1208. * @arg @ref RCC_LSCO3 Clock source to output on LSCO3 pin(PC12)
  1209. * @param RCC_LSCOSource specifies the clock source to output.
  1210. * This parameter can be one of the following values:
  1211. * @arg @ref RCC_LSCOSOURCE_LSI LSI clock selected as LSCO source
  1212. * @arg @ref RCC_LSCOSOURCE_LSE LSE clock selected as LSCO source
  1213. * @retval None
  1214. * @note LSCO should be disable with @ref HAL_RCCEx_DisableLSCO
  1215. */
  1216. void HAL_RCCEx_LSCOConfig(uint32_t RCC_LSCOx, uint32_t RCC_LSCOSource)
  1217. {
  1218. GPIO_InitTypeDef GPIO_InitStruct;
  1219. FlagStatus backupchanged;
  1220. /* Check the parameters */
  1221. assert_param(IS_RCC_LSCO(RCC_LSCOx));
  1222. assert_param(IS_RCC_LSCOSOURCE(RCC_LSCOSource));
  1223. /* Common GPIO init parameters */
  1224. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1225. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1226. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1227. /* RCC_LSCO1 */
  1228. if (RCC_LSCOx == RCC_LSCO1)
  1229. {
  1230. /* LSCO1 Clock Enable */
  1231. __LSCO1_CLK_ENABLE();
  1232. /* Configure the LSCO1 pin in alternate function mode */
  1233. GPIO_InitStruct.Pin = LSCO1_PIN;
  1234. GPIO_InitStruct.Alternate = GPIO_AF0_LSCO;
  1235. HAL_GPIO_Init(LSCO1_GPIO_PORT, &GPIO_InitStruct);
  1236. }
  1237. else if (RCC_LSCOx == RCC_LSCO2)
  1238. {
  1239. /* LSCO2 Clock Enable */
  1240. __LSCO2_CLK_ENABLE();
  1241. /* Configure the LSCO2 pin in alternate function mode */
  1242. GPIO_InitStruct.Pin = LSCO2_PIN;
  1243. GPIO_InitStruct.Alternate = GPIO_AF0_LSCO;
  1244. HAL_GPIO_Init(LSCO2_GPIO_PORT, &GPIO_InitStruct);
  1245. }
  1246. #if defined(RCC_LSCO3_SUPPORT)
  1247. else if (RCC_LSCOx == RCC_LSCO3)
  1248. {
  1249. /* LSCO3 Clock Enable */
  1250. __LSCO3_CLK_ENABLE();
  1251. /* Configure the LSCO3 pin in alternate function mode */
  1252. GPIO_InitStruct.Pin = LSCO3_PIN;
  1253. GPIO_InitStruct.Alternate = GPIO_AF6_LSCO;
  1254. HAL_GPIO_Init(LSCO3_GPIO_PORT, &GPIO_InitStruct);
  1255. }
  1256. #endif /* RCC_LSCO3_SUPPORT */
  1257. else
  1258. {
  1259. ;
  1260. }
  1261. /* Update LSCOSEL clock source in Backup Domain control register */
  1262. if (HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  1263. {
  1264. HAL_PWR_EnableBkUpAccess();
  1265. backupchanged = SET;
  1266. }
  1267. else
  1268. {
  1269. backupchanged = RESET;
  1270. }
  1271. MODIFY_REG(RCC->BDCR, RCC_BDCR_LSCOSEL | RCC_BDCR_LSCOEN, RCC_LSCOSource | RCC_BDCR_LSCOEN);
  1272. if (backupchanged == SET)
  1273. {
  1274. HAL_PWR_DisableBkUpAccess();
  1275. }
  1276. }
  1277. /**
  1278. * @brief Select the Low Speed clock source to output on LSCO pin (PA2).
  1279. * @param LSCOSource specifies the Low Speed clock source to output.
  1280. * This parameter can be one of the following values:
  1281. * @arg @ref RCC_LSCOSOURCE_LSI LSI clock selected as LSCO source
  1282. * @arg @ref RCC_LSCOSOURCE_LSE LSE clock selected as LSCO source
  1283. * @retval None
  1284. */
  1285. void HAL_RCCEx_EnableLSCO(uint32_t LSCOSource)
  1286. {
  1287. /* Check the parameters */
  1288. assert_param(IS_RCC_LSCOSOURCE(LSCOSource));
  1289. /* Update LSCO selection according to parameter and enable LSCO */
  1290. MODIFY_REG(RCC->BDCR, RCC_BDCR_LSCOSEL, LSCOSource | RCC_BDCR_LSCOEN);
  1291. }
  1292. /**
  1293. * @brief Disable the Low Speed clock output.
  1294. * @retval None
  1295. */
  1296. void HAL_RCCEx_DisableLSCO(void)
  1297. {
  1298. LL_RCC_LSCO_Disable();
  1299. }
  1300. /**
  1301. * @brief Enable the PLL-mode of the MSI.
  1302. * @note Prior to enable the PLL-mode of the MSI for automatic hardware
  1303. * calibration LSE oscillator is to be enabled with @ref HAL_RCC_OscConfig().
  1304. * @retval None
  1305. */
  1306. void HAL_RCCEx_EnableMSIPLLMode(void)
  1307. {
  1308. LL_RCC_MSI_EnablePLLMode() ;
  1309. }
  1310. /**
  1311. * @brief Disable the PLL-mode of the MSI.
  1312. * @note PLL-mode of the MSI is automatically reset when LSE oscillator is disabled.
  1313. * @retval None
  1314. */
  1315. void HAL_RCCEx_DisableMSIPLLMode(void)
  1316. {
  1317. LL_RCC_MSI_DisablePLLMode() ;
  1318. }
  1319. /**
  1320. * @brief Set trimming value
  1321. * @param OscillatorType Specifies the oscillator to be trimmed
  1322. * This parameter can be one of the following values:
  1323. * @arg @ref RCC_OSCILLATORTYPE_LSI2 LSI2 oscillator selected.
  1324. * When disabling and re-enabling the LSI2 there is no need for re-trimming
  1325. * Trimming is only needed once after a NRST reset.
  1326. * Trimming values comes from factory trimmed flash location (0x1FFF7548).
  1327. * @note The LSI2 oscillator must be disabled before calling this trimming function through @ref HAL_RCC_OscConfig
  1328. * @retval HAL status
  1329. */
  1330. HAL_StatusTypeDef HAL_RCCEx_TrimOsc(uint32_t OscillatorType)
  1331. {
  1332. #define FTLSI2TRIM (0xFUL)
  1333. HAL_StatusTypeDef status = HAL_OK;
  1334. assert_param(IS_RCC_TRIMOSC(OscillatorType));
  1335. if (OscillatorType == RCC_OSCILLATORTYPE_LSI2)
  1336. {
  1337. if (LL_RCC_LSI2_IsReady() == 1U)
  1338. {
  1339. status = HAL_ERROR;
  1340. }
  1341. else
  1342. {
  1343. /* Copy the LSI2 trimming information from the factory trimmed Flash location */
  1344. uint32_t factoryTrimming = ((*(uint32_t *)(0x1FFF7548)) & FTLSI2TRIM);
  1345. LL_RCC_LSI2_SetTrimming(factoryTrimming);
  1346. }
  1347. }
  1348. else
  1349. {
  1350. status = HAL_ERROR;
  1351. }
  1352. return status;
  1353. }
  1354. /**
  1355. * @}
  1356. */
  1357. #if defined(CRS)
  1358. /** @defgroup RCCEx_Exported_Functions_Group3 Extended Clock Recovery System Control functions
  1359. * @brief Extended Clock Recovery System Control functions
  1360. *
  1361. @verbatim
  1362. ===============================================================================
  1363. ##### Extended Clock Recovery System Control functions #####
  1364. ===============================================================================
  1365. [..]
  1366. For devices with Clock Recovery System feature (CRS), RCC Extended HAL driver can be used as follows:
  1367. (#) In System clock config, HSI48 needs to be enabled
  1368. (#) Enable CRS clock in IP MSP init which will use CRS functions
  1369. (#) Call CRS functions as follows:
  1370. (##) Prepare synchronization configuration necessary for HSI48 calibration
  1371. (+++) Default values can be set for frequency Error Measurement (reload and error limit)
  1372. and also HSI48 oscillator smooth trimming.
  1373. (+++) Macro __HAL_RCC_CRS_RELOADVALUE_CALCULATE can be also used to calculate
  1374. directly reload value with target and synchronization frequencies values
  1375. (##) Call function HAL_RCCEx_CRSConfig which
  1376. (+++) Resets CRS registers to their default values.
  1377. (+++) Configures CRS registers with synchronization configuration
  1378. (+++) Enables automatic calibration and frequency error counter feature
  1379. Note: When using USB LPM (Link Power Management) and the device is in Sleep mode, the
  1380. periodic USB SOF will not be generated by the host. No SYNC signal will therefore be
  1381. provided to the CRS to calibrate the HSI48 on the run. To guarantee the required clock
  1382. precision after waking up from Sleep mode, the LSE or reference clock on the GPIOs
  1383. should be used as SYNC signal.
  1384. (##) A polling function is provided to wait for complete synchronization
  1385. (+++) Call function HAL_RCCEx_CRSWaitSynchronization()
  1386. (+++) According to CRS status, user can decide to adjust again the calibration or continue
  1387. application if synchronization is OK
  1388. (#) User can retrieve information related to synchronization in calling function
  1389. HAL_RCCEx_CRSGetSynchronizationInfo()
  1390. (#) Regarding synchronization status and synchronization information, user can try a new calibration
  1391. in changing synchronization configuration and call again HAL_RCCEx_CRSConfig.
  1392. Note: When the SYNC event is detected during the downcounting phase (before reaching the zero value),
  1393. it means that the actual frequency is lower than the target (and so, that the TRIM value should be
  1394. incremented), while when it is detected during the upcounting phase it means that the actual frequency
  1395. is higher (and that the TRIM value should be decremented).
  1396. (#) In interrupt mode, user can resort to the available macros (__HAL_RCC_CRS_XXX_IT). Interrupts will go
  1397. through CRS Handler (CRS_IRQn/CRS_IRQHandler)
  1398. (++) Call function HAL_RCCEx_CRSConfig()
  1399. (++) Enable CRS_IRQn (thanks to NVIC functions)
  1400. (++) Enable CRS interrupt (__HAL_RCC_CRS_ENABLE_IT)
  1401. (++) Implement CRS status management in the following user callbacks called from
  1402. HAL_RCCEx_CRS_IRQHandler():
  1403. (+++) HAL_RCCEx_CRS_SyncOkCallback()
  1404. (+++) HAL_RCCEx_CRS_SyncWarnCallback()
  1405. (+++) HAL_RCCEx_CRS_ExpectedSyncCallback()
  1406. (+++) HAL_RCCEx_CRS_ErrorCallback()
  1407. (#) To force a SYNC EVENT, user can use the function HAL_RCCEx_CRSSoftwareSynchronizationGenerate().
  1408. This function can be called before calling HAL_RCCEx_CRSConfig (for instance in Systick handler)
  1409. @endverbatim
  1410. * @{
  1411. */
  1412. /**
  1413. * @brief Start automatic synchronization for polling mode
  1414. * @param pInit Pointer on RCC_CRSInitTypeDef structure
  1415. * @retval None
  1416. */
  1417. void HAL_RCCEx_CRSConfig(RCC_CRSInitTypeDef *pInit)
  1418. {
  1419. uint32_t value;
  1420. /* Check the parameters */
  1421. assert_param(IS_RCC_CRS_SYNC_DIV(pInit->Prescaler));
  1422. assert_param(IS_RCC_CRS_SYNC_SOURCE(pInit->Source));
  1423. assert_param(IS_RCC_CRS_SYNC_POLARITY(pInit->Polarity));
  1424. assert_param(IS_RCC_CRS_RELOADVALUE(pInit->ReloadValue));
  1425. assert_param(IS_RCC_CRS_ERRORLIMIT(pInit->ErrorLimitValue));
  1426. assert_param(IS_RCC_CRS_HSI48CALIBRATION(pInit->HSI48CalibrationValue));
  1427. /* CONFIGURATION */
  1428. /* Before configuration, reset CRS registers to their default values*/
  1429. __HAL_RCC_CRS_FORCE_RESET();
  1430. __HAL_RCC_CRS_RELEASE_RESET();
  1431. /* Set the SYNCDIV[2:0] bits according to Prescaler value */
  1432. /* Set the SYNCSRC[1:0] bits according to Source value */
  1433. /* Set the SYNCSPOL bit according to Polarity value */
  1434. value = (pInit->Prescaler | pInit->Source | pInit->Polarity);
  1435. /* Set the RELOAD[15:0] bits according to ReloadValue value */
  1436. value |= pInit->ReloadValue;
  1437. /* Set the FELIM[7:0] bits according to ErrorLimitValue value */
  1438. value |= (pInit->ErrorLimitValue << CRS_CFGR_FELIM_Pos);
  1439. WRITE_REG(CRS->CFGR, value);
  1440. /* Adjust HSI48 oscillator smooth trimming */
  1441. /* Set the TRIM[5:0] bits according to RCC_CRS_HSI48CalibrationValue value */
  1442. MODIFY_REG(CRS->CR, CRS_CR_TRIM, (pInit->HSI48CalibrationValue << CRS_CR_TRIM_Pos));
  1443. /* START AUTOMATIC SYNCHRONIZATION*/
  1444. /* Enable Automatic trimming & Frequency error counter */
  1445. SET_BIT(CRS->CR, CRS_CR_AUTOTRIMEN | CRS_CR_CEN);
  1446. }
  1447. /**
  1448. * @brief Generate the software synchronization event
  1449. * @retval None
  1450. */
  1451. void HAL_RCCEx_CRSSoftwareSynchronizationGenerate(void)
  1452. {
  1453. LL_CRS_GenerateEvent_SWSYNC();
  1454. }
  1455. /**
  1456. * @brief Return synchronization info
  1457. * @param pSynchroInfo Pointer on @ref RCC_CRSSynchroInfoTypeDef structure
  1458. * @retval None
  1459. */
  1460. void HAL_RCCEx_CRSGetSynchronizationInfo(RCC_CRSSynchroInfoTypeDef *pSynchroInfo)
  1461. {
  1462. /* Check the parameter */
  1463. assert_param(pSynchroInfo != (void *)NULL);
  1464. /* Get the reload value */
  1465. pSynchroInfo->ReloadValue = LL_CRS_GetReloadCounter();
  1466. /* Get HSI48 oscillator smooth trimming */
  1467. pSynchroInfo->HSI48CalibrationValue = LL_CRS_GetHSI48SmoothTrimming();
  1468. /* Get Frequency error capture */
  1469. pSynchroInfo->FreqErrorCapture = LL_CRS_GetFreqErrorCapture();
  1470. /* Get Frequency error direction */
  1471. pSynchroInfo->FreqErrorDirection = LL_CRS_GetFreqErrorDirection();
  1472. }
  1473. /**
  1474. * @brief Wait for CRS Synchronization status.
  1475. * @param Timeout Duration of the timeout
  1476. * @note Timeout is based on the maximum time to receive a SYNC event based on synchronization
  1477. * frequency.
  1478. * @note If Timeout set to HAL_MAX_DELAY, HAL_TIMEOUT will be never returned.
  1479. * @retval Combination of Synchronization status
  1480. * This parameter can be a combination of the following values:
  1481. * @arg @ref RCC_CRS_TIMEOUT
  1482. * @arg @ref RCC_CRS_SYNCOK
  1483. * @arg @ref RCC_CRS_SYNCWARN
  1484. * @arg @ref RCC_CRS_SYNCERR
  1485. * @arg @ref RCC_CRS_SYNCMISS
  1486. * @arg @ref RCC_CRS_TRIMOVF
  1487. */
  1488. uint32_t HAL_RCCEx_CRSWaitSynchronization(uint32_t Timeout)
  1489. {
  1490. uint32_t crsstatus = RCC_CRS_NONE;
  1491. uint32_t tickstart;
  1492. /* Get timeout */
  1493. tickstart = HAL_GetTick();
  1494. /* Wait for CRS flag or timeout detection */
  1495. do
  1496. {
  1497. if (Timeout != HAL_MAX_DELAY)
  1498. {
  1499. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1500. {
  1501. crsstatus = RCC_CRS_TIMEOUT;
  1502. }
  1503. }
  1504. /* Check CRS SYNCOK flag */
  1505. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCOK))
  1506. {
  1507. /* CRS SYNC event OK */
  1508. crsstatus |= RCC_CRS_SYNCOK;
  1509. /* Clear CRS SYNC event OK bit */
  1510. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCOK);
  1511. }
  1512. /* Check CRS SYNCWARN flag */
  1513. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCWARN))
  1514. {
  1515. /* CRS SYNC warning */
  1516. crsstatus |= RCC_CRS_SYNCWARN;
  1517. /* Clear CRS SYNCWARN bit */
  1518. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCWARN);
  1519. }
  1520. /* Check CRS TRIM overflow flag */
  1521. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_TRIMOVF))
  1522. {
  1523. /* CRS SYNC Error */
  1524. crsstatus |= RCC_CRS_TRIMOVF;
  1525. /* Clear CRS Error bit */
  1526. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_TRIMOVF);
  1527. }
  1528. /* Check CRS Error flag */
  1529. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCERR))
  1530. {
  1531. /* CRS SYNC Error */
  1532. crsstatus |= RCC_CRS_SYNCERR;
  1533. /* Clear CRS Error bit */
  1534. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCERR);
  1535. }
  1536. /* Check CRS SYNC Missed flag */
  1537. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCMISS))
  1538. {
  1539. /* CRS SYNC Missed */
  1540. crsstatus |= RCC_CRS_SYNCMISS;
  1541. /* Clear CRS SYNC Missed bit */
  1542. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCMISS);
  1543. }
  1544. /* Check CRS Expected SYNC flag */
  1545. if (__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_ESYNC))
  1546. {
  1547. /* frequency error counter reached a zero value */
  1548. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_ESYNC);
  1549. }
  1550. } while (RCC_CRS_NONE == crsstatus);
  1551. return crsstatus;
  1552. }
  1553. /**
  1554. * @brief Handle the Clock Recovery System interrupt request.
  1555. * @retval None
  1556. */
  1557. void HAL_RCCEx_CRS_IRQHandler(void)
  1558. {
  1559. uint32_t crserror = RCC_CRS_NONE;
  1560. /* Get current IT flags and IT sources values */
  1561. uint32_t itflags = READ_REG(CRS->ISR);
  1562. uint32_t itsources = READ_REG(CRS->CR);
  1563. /* Check CRS SYNCOK flag */
  1564. if (((itflags & RCC_CRS_FLAG_SYNCOK) != 0U) && ((itsources & RCC_CRS_IT_SYNCOK) != 0U))
  1565. {
  1566. /* Clear CRS SYNC event OK flag */
  1567. LL_CRS_ClearFlag_SYNCOK();
  1568. /* user callback */
  1569. HAL_RCCEx_CRS_SyncOkCallback();
  1570. }
  1571. /* Check CRS SYNCWARN flag */
  1572. else if (((itflags & RCC_CRS_FLAG_SYNCWARN) != 0U) && ((itsources & RCC_CRS_IT_SYNCWARN) != 0U))
  1573. {
  1574. /* Clear CRS SYNCWARN flag */
  1575. LL_CRS_ClearFlag_SYNCWARN();
  1576. /* user callback */
  1577. HAL_RCCEx_CRS_SyncWarnCallback();
  1578. }
  1579. /* Check CRS Expected SYNC flag */
  1580. else if (((itflags & RCC_CRS_FLAG_ESYNC) != 0U) && ((itsources & RCC_CRS_IT_ESYNC) != 0U))
  1581. {
  1582. /* frequency error counter reached a zero value */
  1583. LL_CRS_ClearFlag_ESYNC();
  1584. /* user callback */
  1585. HAL_RCCEx_CRS_ExpectedSyncCallback();
  1586. }
  1587. /* Check CRS Error flags */
  1588. else
  1589. {
  1590. if (((itflags & RCC_CRS_FLAG_ERR) != 0U) && ((itsources & RCC_CRS_IT_ERR) != 0U))
  1591. {
  1592. if ((itflags & RCC_CRS_FLAG_SYNCERR) != 0U)
  1593. {
  1594. crserror |= RCC_CRS_SYNCERR;
  1595. }
  1596. if ((itflags & RCC_CRS_FLAG_SYNCMISS) != 0U)
  1597. {
  1598. crserror |= RCC_CRS_SYNCMISS;
  1599. }
  1600. if ((itflags & RCC_CRS_FLAG_TRIMOVF) != 0U)
  1601. {
  1602. crserror |= RCC_CRS_TRIMOVF;
  1603. }
  1604. /* Clear CRS Error flags */
  1605. LL_CRS_ClearFlag_ERR();
  1606. /* user error callback */
  1607. HAL_RCCEx_CRS_ErrorCallback(crserror);
  1608. }
  1609. }
  1610. }
  1611. /**
  1612. * @brief RCCEx Clock Recovery System SYNCOK interrupt callback.
  1613. * @retval none
  1614. */
  1615. __weak void HAL_RCCEx_CRS_SyncOkCallback(void)
  1616. {
  1617. /* NOTE : This function should not be modified, when the callback is needed,
  1618. the @ref HAL_RCCEx_CRS_SyncOkCallback should be implemented in the user file
  1619. */
  1620. }
  1621. /**
  1622. * @brief RCCEx Clock Recovery System SYNCWARN interrupt callback.
  1623. * @retval none
  1624. */
  1625. __weak void HAL_RCCEx_CRS_SyncWarnCallback(void)
  1626. {
  1627. /* NOTE : This function should not be modified, when the callback is needed,
  1628. the HAL_RCCEx_CRS_SyncWarnCallback should be implemented in the user file
  1629. */
  1630. }
  1631. /**
  1632. * @brief RCCEx Clock Recovery System Expected SYNC interrupt callback.
  1633. * @retval none
  1634. */
  1635. __weak void HAL_RCCEx_CRS_ExpectedSyncCallback(void)
  1636. {
  1637. /* NOTE : This function should not be modified, when the callback is needed,
  1638. the HAL_RCCEx_CRS_ExpectedSyncCallback should be implemented in the user file
  1639. */
  1640. }
  1641. /**
  1642. * @brief RCCEx Clock Recovery System Error interrupt callback.
  1643. * @param Error Combination of Error status.
  1644. * This parameter can be a combination of the following values:
  1645. * @arg @ref RCC_CRS_SYNCERR
  1646. * @arg @ref RCC_CRS_SYNCMISS
  1647. * @arg @ref RCC_CRS_TRIMOVF
  1648. * @retval none
  1649. */
  1650. __weak void HAL_RCCEx_CRS_ErrorCallback(uint32_t Error)
  1651. {
  1652. /* Prevent unused argument(s) compilation warning */
  1653. UNUSED(Error);
  1654. /* NOTE : This function should not be modified, when the callback is needed,
  1655. the @ref HAL_RCCEx_CRS_ErrorCallback should be implemented in the user file
  1656. */
  1657. }
  1658. /**
  1659. * @}
  1660. */
  1661. #endif /* CRS */
  1662. /**
  1663. * @}
  1664. */
  1665. /** @addtogroup RCCEx_Private_Functions
  1666. * @{
  1667. */
  1668. #if defined(SAI1)
  1669. /**
  1670. * @brief Configure the parameters N & P of PLLSAI1 and enable PLLSAI1 output clock(s).
  1671. * @param PLLSAI1 pointer to an RCC_PLLSAI1InitTypeDef structure that
  1672. * contains the configuration parameters N & P as well as PLLSAI1 output clock(s)
  1673. *
  1674. * @note PLLSAI1 is temporary disable to apply new parameters
  1675. *
  1676. * @retval HAL status
  1677. */
  1678. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNP(RCC_PLLSAI1InitTypeDef *PLLSAI1)
  1679. {
  1680. uint32_t tickstart;
  1681. HAL_StatusTypeDef status = HAL_OK;
  1682. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  1683. assert_param(IS_RCC_PLLN_VALUE(PLLSAI1->PLLN));
  1684. assert_param(IS_RCC_PLLP_VALUE(PLLSAI1->PLLP));
  1685. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PLLSAI1->PLLSAI1ClockOut));
  1686. /* Disable the PLLSAI1 */
  1687. __HAL_RCC_PLLSAI1_DISABLE();
  1688. /* Get Start Tick*/
  1689. tickstart = HAL_GetTick();
  1690. /* Wait till PLLSAI1 is ready to be updated */
  1691. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  1692. {
  1693. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1694. {
  1695. status = HAL_TIMEOUT;
  1696. break;
  1697. }
  1698. }
  1699. if (status == HAL_OK)
  1700. {
  1701. /* Configure the PLLSAI1 Multiplication factor N */
  1702. __HAL_RCC_PLLSAI1_MULN_CONFIG(PLLSAI1->PLLN);
  1703. /* Configure the PLLSAI1 Division factor P */
  1704. __HAL_RCC_PLLSAI1_DIVP_CONFIG(PLLSAI1->PLLP);
  1705. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  1706. __HAL_RCC_PLLSAI1_ENABLE();
  1707. /* Get Start Tick*/
  1708. tickstart = HAL_GetTick();
  1709. /* Wait till PLLSAI1 is ready */
  1710. while (LL_RCC_PLLSAI1_IsReady() != 1U)
  1711. {
  1712. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1713. {
  1714. status = HAL_TIMEOUT;
  1715. break;
  1716. }
  1717. }
  1718. if (status == HAL_OK)
  1719. {
  1720. /* Configure the PLLSAI1 Clock output(s) */
  1721. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PLLSAI1->PLLSAI1ClockOut);
  1722. }
  1723. }
  1724. return status;
  1725. }
  1726. /**
  1727. * @brief Configure the parameters N & Q of PLLSAI1 and enable PLLSAI1 output clock(s).
  1728. * @param PLLSAI1 pointer to an RCC_PLLSAI1InitTypeDef structure that
  1729. * contains the configuration parameters N & Q as well as PLLSAI1 output clock(s)
  1730. *
  1731. * @note PLLSAI1 is temporary disable to apply new parameters
  1732. *
  1733. * @retval HAL status
  1734. */
  1735. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNQ(RCC_PLLSAI1InitTypeDef *PLLSAI1)
  1736. {
  1737. uint32_t tickstart;
  1738. HAL_StatusTypeDef status = HAL_OK;
  1739. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  1740. assert_param(IS_RCC_PLLN_VALUE(PLLSAI1->PLLN));
  1741. assert_param(IS_RCC_PLLQ_VALUE(PLLSAI1->PLLQ));
  1742. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PLLSAI1->PLLSAI1ClockOut));
  1743. /* Disable the PLLSAI1 */
  1744. __HAL_RCC_PLLSAI1_DISABLE();
  1745. /* Get Start Tick*/
  1746. tickstart = HAL_GetTick();
  1747. /* Wait till PLLSAI1 is ready to be updated */
  1748. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  1749. {
  1750. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1751. {
  1752. status = HAL_TIMEOUT;
  1753. break;
  1754. }
  1755. }
  1756. if (status == HAL_OK)
  1757. {
  1758. /* Configure the PLLSAI1 Multiplication factor N */
  1759. __HAL_RCC_PLLSAI1_MULN_CONFIG(PLLSAI1->PLLN);
  1760. /* Configure the PLLSAI1 Division factor Q */
  1761. __HAL_RCC_PLLSAI1_DIVQ_CONFIG(PLLSAI1->PLLQ);
  1762. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  1763. __HAL_RCC_PLLSAI1_ENABLE();
  1764. /* Get Start Tick*/
  1765. tickstart = HAL_GetTick();
  1766. /* Wait till PLLSAI1 is ready */
  1767. while (LL_RCC_PLLSAI1_IsReady() != 1U)
  1768. {
  1769. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1770. {
  1771. status = HAL_TIMEOUT;
  1772. break;
  1773. }
  1774. }
  1775. if (status == HAL_OK)
  1776. {
  1777. /* Configure the PLLSAI1 Clock output(s) */
  1778. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PLLSAI1->PLLSAI1ClockOut);
  1779. }
  1780. }
  1781. return status;
  1782. }
  1783. /**
  1784. * @brief Configure the parameters N & R of PLLSAI1 and enable PLLSAI1 output clock(s).
  1785. * @param PLLSAI1 pointer to an RCC_PLLSAI1InitTypeDef structure that
  1786. * contains the configuration parameters N & R as well as PLLSAI1 output clock(s)
  1787. *
  1788. * @note PLLSAI1 is temporary disable to apply new parameters
  1789. *
  1790. * @retval HAL status
  1791. */
  1792. static HAL_StatusTypeDef RCCEx_PLLSAI1_ConfigNR(RCC_PLLSAI1InitTypeDef *PLLSAI1)
  1793. {
  1794. uint32_t tickstart;
  1795. HAL_StatusTypeDef status = HAL_OK;
  1796. /* check for PLLSAI1 Parameters used to output PLLSAI1CLK */
  1797. assert_param(IS_RCC_PLLN_VALUE(PLLSAI1->PLLN));
  1798. assert_param(IS_RCC_PLLR_VALUE(PLLSAI1->PLLR));
  1799. assert_param(IS_RCC_PLLSAI1CLOCKOUT_VALUE(PLLSAI1->PLLSAI1ClockOut));
  1800. /* Disable the PLLSAI1 */
  1801. __HAL_RCC_PLLSAI1_DISABLE();
  1802. /* Get Start Tick*/
  1803. tickstart = HAL_GetTick();
  1804. /* Wait till PLLSAI1 is ready to be updated */
  1805. while (LL_RCC_PLLSAI1_IsReady() != 0U)
  1806. {
  1807. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1808. {
  1809. status = HAL_TIMEOUT;
  1810. break;
  1811. }
  1812. }
  1813. if (status == HAL_OK)
  1814. {
  1815. /* Configure the PLLSAI1 Multiplication factor N */
  1816. __HAL_RCC_PLLSAI1_MULN_CONFIG(PLLSAI1->PLLN);
  1817. /* Configure the PLLSAI1 Division factor R */
  1818. __HAL_RCC_PLLSAI1_DIVR_CONFIG(PLLSAI1->PLLR);
  1819. /* Enable the PLLSAI1 again by setting PLLSAI1ON to 1*/
  1820. __HAL_RCC_PLLSAI1_ENABLE();
  1821. /* Get Start Tick*/
  1822. tickstart = HAL_GetTick();
  1823. /* Wait till PLLSAI1 is ready */
  1824. while (LL_RCC_PLLSAI1_IsReady() != 1U)
  1825. {
  1826. if ((HAL_GetTick() - tickstart) > PLLSAI1_TIMEOUT_VALUE)
  1827. {
  1828. status = HAL_TIMEOUT;
  1829. break;
  1830. }
  1831. }
  1832. if (status == HAL_OK)
  1833. {
  1834. /* Configure the PLLSAI1 Clock output(s) */
  1835. __HAL_RCC_PLLSAI1CLKOUT_ENABLE(PLLSAI1->PLLSAI1ClockOut);
  1836. }
  1837. }
  1838. return status;
  1839. }
  1840. #endif /* SAI1 */
  1841. /**
  1842. * @brief Return PLL clock (PLLPCLK) frequency used for SAI domain
  1843. * @retval PLLPCLK clock frequency (in Hz)
  1844. */
  1845. static uint32_t RCC_PLL_GetFreqDomain_P(void)
  1846. {
  1847. uint32_t pllinputfreq;
  1848. uint32_t pllsource;
  1849. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI Value / PLLM) * PLLN
  1850. SAI Domain clock = PLL_VCO / PLLP
  1851. */
  1852. pllsource = LL_RCC_PLL_GetMainSource();
  1853. switch (pllsource)
  1854. {
  1855. case LL_RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1856. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1857. break;
  1858. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1859. pllinputfreq = HSI_VALUE;
  1860. break;
  1861. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1862. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  1863. {
  1864. pllinputfreq = HSE_VALUE / 2U;
  1865. }
  1866. else
  1867. {
  1868. pllinputfreq = HSE_VALUE;
  1869. }
  1870. break;
  1871. default:
  1872. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1873. break;
  1874. }
  1875. return __LL_RCC_CALC_PLLCLK_ADC_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1876. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetP());
  1877. }
  1878. /**
  1879. * @brief Return PLL clock (PLLQCLK) frequency used for 48 MHz domain
  1880. * @retval PLLQCLK clock frequency (in Hz)
  1881. */
  1882. static uint32_t RCC_PLL_GetFreqDomain_Q(void)
  1883. {
  1884. uint32_t pllinputfreq;
  1885. uint32_t pllsource;
  1886. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI Value/ PLLM) * PLLN
  1887. 48M Domain clock = PLL_VCO / PLLQ
  1888. */
  1889. pllsource = LL_RCC_PLL_GetMainSource();
  1890. switch (pllsource)
  1891. {
  1892. case LL_RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1893. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1894. break;
  1895. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1896. pllinputfreq = HSI_VALUE;
  1897. break;
  1898. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1899. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  1900. {
  1901. pllinputfreq = HSE_VALUE / 2U;
  1902. }
  1903. else
  1904. {
  1905. pllinputfreq = HSE_VALUE;
  1906. }
  1907. break;
  1908. default:
  1909. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1910. break;
  1911. }
  1912. return __LL_RCC_CALC_PLLCLK_48M_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1913. LL_RCC_PLL_GetN(), LL_RCC_PLL_GetQ());
  1914. }
  1915. #if defined(SAI1)
  1916. /**
  1917. * @brief Return PLLSAI1 clock (PLLSAI1RCLK) frequency used for ADC domain
  1918. * @retval PLLSAI1RCLK clock frequency (in Hz)
  1919. */
  1920. static uint32_t RCC_PLLSAI1_GetFreqDomain_R(void)
  1921. {
  1922. uint32_t pllinputfreq;
  1923. uint32_t pllsource;
  1924. /* PLLSAI1_VCO = (HSE_VALUE or HSI_VALUE or MSI Value/ PLLM) * PLLSAI1N */
  1925. /* 48M Domain clock = PLLSAI1_VCO / PLLSAI1R */
  1926. pllsource = LL_RCC_PLL_GetMainSource();
  1927. switch (pllsource)
  1928. {
  1929. case LL_RCC_PLLSOURCE_MSI: /* MSI used as PLLSAI1 clock source */
  1930. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1931. break;
  1932. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLLSAI1 clock source */
  1933. pllinputfreq = HSI_VALUE;
  1934. break;
  1935. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLLSAI1 clock source */
  1936. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  1937. {
  1938. pllinputfreq = HSE_VALUE / 2U;
  1939. }
  1940. else
  1941. {
  1942. pllinputfreq = HSE_VALUE;
  1943. }
  1944. break;
  1945. default:
  1946. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1947. break;
  1948. }
  1949. return __LL_RCC_CALC_PLLSAI1_ADC_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1950. LL_RCC_PLLSAI1_GetN(), LL_RCC_PLLSAI1_GetR());
  1951. }
  1952. /**
  1953. * @brief Return PLLSAI1 clock (PLLSAI1PCLK) frequency used for SAI domain
  1954. * @retval PLLSAI1PCLK clock frequency (in Hz)
  1955. */
  1956. static uint32_t RCC_PLLSAI1_GetFreqDomain_P(void)
  1957. {
  1958. uint32_t pllinputfreq;
  1959. uint32_t pllsource;
  1960. /* PLLSAI1_VCO = (HSE_VALUE or HSI_VALUE or MSI Value/ PLLM) * PLLSAI1N */
  1961. /* SAI Domain clock = PLLSAI1_VCO / PLLSAI1P */
  1962. pllsource = LL_RCC_PLL_GetMainSource();
  1963. switch (pllsource)
  1964. {
  1965. case LL_RCC_PLLSOURCE_MSI: /* MSI used as PLLSAI1 clock source */
  1966. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1967. break;
  1968. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLLSAI1 clock source */
  1969. pllinputfreq = HSI_VALUE;
  1970. break;
  1971. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLLSAI1 clock source */
  1972. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  1973. {
  1974. pllinputfreq = HSE_VALUE / 2U;
  1975. }
  1976. else
  1977. {
  1978. pllinputfreq = HSE_VALUE;
  1979. }
  1980. break;
  1981. default:
  1982. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  1983. break;
  1984. }
  1985. return __LL_RCC_CALC_PLLSAI1_SAI_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  1986. LL_RCC_PLLSAI1_GetN(), LL_RCC_PLLSAI1_GetP());
  1987. }
  1988. /**
  1989. * @brief Return PLLSAI1 clock (PLLSAI1QCLK) frequency used for 48Mhz domain
  1990. * @retval PLLSAI1QCLK clock frequency (in Hz)
  1991. */
  1992. static uint32_t RCC_PLLSAI1_GetFreqDomain_Q(void)
  1993. {
  1994. uint32_t pllinputfreq;
  1995. uint32_t pllsource;
  1996. /* PLLSAI1_VCO = (HSE_VALUE or HSI_VALUE or MSI Value/ PLLM) * PLLSAI1N */
  1997. /* 48M Domain clock = PLLSAI1_VCO / PLLSAI1Q */
  1998. pllsource = LL_RCC_PLL_GetMainSource();
  1999. switch (pllsource)
  2000. {
  2001. case LL_RCC_PLLSOURCE_MSI: /* MSI used as PLLSAI1 clock source */
  2002. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  2003. break;
  2004. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLLSAI1 clock source */
  2005. pllinputfreq = HSI_VALUE;
  2006. break;
  2007. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLLSAI1 clock source */
  2008. if (LL_RCC_HSE_IsEnabledDiv2() == 1U)
  2009. {
  2010. pllinputfreq = HSE_VALUE / 2U;
  2011. }
  2012. else
  2013. {
  2014. pllinputfreq = HSE_VALUE;
  2015. }
  2016. break;
  2017. default:
  2018. pllinputfreq = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  2019. break;
  2020. }
  2021. return __LL_RCC_CALC_PLLSAI1_48M_FREQ(pllinputfreq, LL_RCC_PLL_GetDivider(),
  2022. LL_RCC_PLLSAI1_GetN(), LL_RCC_PLLSAI1_GetQ());
  2023. }
  2024. #endif /* SAI1 */
  2025. /**
  2026. * @}
  2027. */
  2028. #endif /* HAL_RCC_MODULE_ENABLED */
  2029. /**
  2030. * @}
  2031. */
  2032. /**
  2033. * @}
  2034. */