stm32l4xx_hal_rcc.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_rcc.c
  4. * @author MCD Application Team
  5. * @brief RCC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Reset and Clock Control (RCC) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. *
  11. @verbatim
  12. ==============================================================================
  13. ##### RCC specific features #####
  14. ==============================================================================
  15. [..]
  16. After reset the device is running from Multiple Speed Internal oscillator
  17. (4 MHz) with Flash 0 wait state. Flash prefetch buffer, D-Cache
  18. and I-Cache are disabled, and all peripherals are off except internal
  19. SRAM, Flash and JTAG.
  20. (+) There is no prescaler on High speed (AHBs) and Low speed (APBs) busses:
  21. all peripherals mapped on these busses are running at MSI speed.
  22. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  23. (+) All GPIOs are in analog mode, except the JTAG pins which
  24. are assigned to be used for debug purpose.
  25. [..]
  26. Once the device started from reset, the user application has to:
  27. (+) Configure the clock source to be used to drive the System clock
  28. (if the application needs higher frequency/performance)
  29. (+) Configure the System clock frequency and Flash settings
  30. (+) Configure the AHB and APB busses prescalers
  31. (+) Enable the clock for the peripheral(s) to be used
  32. (+) Configure the clock source(s) for peripherals which clocks are not
  33. derived from the System clock (SAIx, RTC, ADC, USB OTG FS/SDMMC1/RNG)
  34. @endverbatim
  35. ******************************************************************************
  36. * @attention
  37. *
  38. * Copyright (c) 2017 STMicroelectronics.
  39. * All rights reserved.
  40. *
  41. * This software is licensed under terms that can be found in the LICENSE file in
  42. * the root directory of this software component.
  43. * If no LICENSE file comes with this software, it is provided AS-IS.
  44. ******************************************************************************
  45. */
  46. /* Includes ------------------------------------------------------------------*/
  47. #include "stm32l4xx_hal.h"
  48. /** @addtogroup STM32L4xx_HAL_Driver
  49. * @{
  50. */
  51. /** @defgroup RCC RCC
  52. * @brief RCC HAL module driver
  53. * @{
  54. */
  55. #ifdef HAL_RCC_MODULE_ENABLED
  56. /* Private typedef -----------------------------------------------------------*/
  57. /* Private define ------------------------------------------------------------*/
  58. /** @defgroup RCC_Private_Constants RCC Private Constants
  59. * @{
  60. */
  61. #define HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
  62. #define HSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  63. #define MSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  64. #if defined(RCC_CSR_LSIPREDIV)
  65. #define LSI_TIMEOUT_VALUE 17U /* 17 ms (16 ms starting time + 1) */
  66. #else
  67. #define LSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  68. #endif /* RCC_CSR_LSIPREDIV */
  69. #define HSI48_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  70. #define PLL_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  71. #define CLOCKSWITCH_TIMEOUT_VALUE 5000U /* 5 s */
  72. /**
  73. * @}
  74. */
  75. /* Private macro -------------------------------------------------------------*/
  76. /** @defgroup RCC_Private_Macros RCC Private Macros
  77. * @{
  78. */
  79. #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  80. #define MCO1_GPIO_PORT GPIOA
  81. #define MCO1_PIN GPIO_PIN_8
  82. #define RCC_PLL_OSCSOURCE_CONFIG(__HAL_RCC_PLLSOURCE__) \
  83. (MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, (__HAL_RCC_PLLSOURCE__)))
  84. /**
  85. * @}
  86. */
  87. /* Private variables ---------------------------------------------------------*/
  88. /* Private function prototypes -----------------------------------------------*/
  89. /** @defgroup RCC_Private_Functions RCC Private Functions
  90. * @{
  91. */
  92. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange);
  93. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  94. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  95. static uint32_t RCC_GetSysClockFreqFromPLLSource(void);
  96. #endif
  97. /**
  98. * @}
  99. */
  100. /* Exported functions --------------------------------------------------------*/
  101. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  102. * @{
  103. */
  104. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  105. * @brief Initialization and Configuration functions
  106. *
  107. @verbatim
  108. ===============================================================================
  109. ##### Initialization and de-initialization functions #####
  110. ===============================================================================
  111. [..]
  112. This section provides functions allowing to configure the internal and external oscillators
  113. (HSE, HSI, LSE, MSI, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1
  114. and APB2).
  115. [..] Internal/external clock and PLL configuration
  116. (+) HSI (high-speed internal): 16 MHz factory-trimmed RC used directly or through
  117. the PLL as System clock source.
  118. (+) MSI (Multiple Speed Internal): Its frequency is software trimmable from 100KHZ to 48MHZ.
  119. It can be used to generate the clock for the USB OTG FS (48 MHz).
  120. The number of flash wait states is automatically adjusted when MSI range is updated with
  121. HAL_RCC_OscConfig() and the MSI is used as System clock source.
  122. (+) LSI (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
  123. clock source.
  124. (+) HSE (high-speed external): 4 to 48 MHz crystal oscillator used directly or
  125. through the PLL as System clock source. Can be used also optionally as RTC clock source.
  126. (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.
  127. (+) PLL (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  128. (++) The first output is used to generate the high speed system clock (up to 80MHz).
  129. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
  130. the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
  131. (++) The third output is used to generate an accurate clock to achieve
  132. high-quality audio performance on SAI interface.
  133. (+) PLLSAI1 (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  134. (++) The first output is used to generate SAR ADC1 clock.
  135. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
  136. the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
  137. (++) The third output is used to generate an accurate clock to achieve
  138. high-quality audio performance on SAI interface.
  139. (+) PLLSAI2 (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  140. (++) The first output is used to generate an accurate clock to achieve
  141. high-quality audio performance on SAI interface.
  142. (++) The second output is used to generate either SAR ADC2 clock if ADC2 is present
  143. or LCD clock if LTDC is present.
  144. (++) The third output is used to generate DSI clock if DSI is present.
  145. (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
  146. (HSE used directly or through PLL as System clock source), the System clock
  147. is automatically switched to HSI and an interrupt is generated if enabled.
  148. The interrupt is linked to the Cortex-M4 NMI (Non-Maskable Interrupt)
  149. exception vector.
  150. (+) MCO (microcontroller clock output): used to output MSI, LSI, HSI, LSE, HSE or
  151. main PLL clock (through a configurable prescaler) on PA8 pin.
  152. [..] System, AHB and APB busses clocks configuration
  153. (+) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI,
  154. HSE and main PLL.
  155. The AHB clock (HCLK) is derived from System clock through configurable
  156. prescaler and used to clock the CPU, memory and peripherals mapped
  157. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  158. from AHB clock through configurable prescalers and used to clock
  159. the peripherals mapped on these busses. You can use
  160. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  161. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  162. (+@) SAI: the SAI clock can be derived either from a specific PLL (PLLSAI1) or (PLLSAI2) or
  163. from an external clock mapped on the SAI_CKIN pin.
  164. You have to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  165. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  166. divided by 2 to 31.
  167. You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
  168. to configure this clock.
  169. (+@) USB OTG FS, SDMMC1 and RNG: USB OTG FS requires a frequency equal to 48 MHz
  170. to work correctly, while the SDMMC1 and RNG peripherals require a frequency
  171. equal or lower than to 48 MHz. This clock is derived of the main PLL or PLLSAI1
  172. through PLLQ divider. You have to enable the peripheral clock and use
  173. HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  174. (+@) IWDG clock which is always the LSI clock.
  175. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 80 MHz.
  176. The clock source frequency should be adapted depending on the device voltage range
  177. as listed in the Reference Manual "Clock source frequency versus voltage scaling" chapter.
  178. @endverbatim
  179. Table 1. HCLK clock frequency for other STM32L4 devices
  180. +-------------------------------------------------------+
  181. | Latency | HCLK clock frequency (MHz) |
  182. | |-------------------------------------|
  183. | | voltage range 1 | voltage range 2 |
  184. | | 1.2 V | 1.0 V |
  185. |-----------------|------------------|------------------|
  186. |0WS(1 CPU cycles)| 0 < HCLK <= 16 | 0 < HCLK <= 6 |
  187. |-----------------|------------------|------------------|
  188. |1WS(2 CPU cycles)| 16 < HCLK <= 32 | 6 < HCLK <= 12 |
  189. |-----------------|------------------|------------------|
  190. |2WS(3 CPU cycles)| 32 < HCLK <= 48 | 12 < HCLK <= 18 |
  191. |-----------------|------------------|------------------|
  192. |3WS(4 CPU cycles)| 48 < HCLK <= 64 | 18 < HCLK <= 26 |
  193. |-----------------|------------------|------------------|
  194. |4WS(5 CPU cycles)| 64 < HCLK <= 80 | 18 < HCLK <= 26 |
  195. +-------------------------------------------------------+
  196. Table 2. HCLK clock frequency for STM32L4+ devices
  197. +--------------------------------------------------------+
  198. | Latency | HCLK clock frequency (MHz) |
  199. | |--------------------------------------|
  200. | | voltage range 1 | voltage range 2 |
  201. | | 1.2 V | 1.0 V |
  202. |-----------------|-------------------|------------------|
  203. |0WS(1 CPU cycles)| 0 < HCLK <= 20 | 0 < HCLK <= 8 |
  204. |-----------------|-------------------|------------------|
  205. |1WS(2 CPU cycles)| 20 < HCLK <= 40 | 8 < HCLK <= 16 |
  206. |-----------------|-------------------|------------------|
  207. |2WS(3 CPU cycles)| 40 < HCLK <= 60 | 16 < HCLK <= 26 |
  208. |-----------------|-------------------|------------------|
  209. |3WS(4 CPU cycles)| 60 < HCLK <= 80 | 16 < HCLK <= 26 |
  210. |-----------------|-------------------|------------------|
  211. |4WS(5 CPU cycles)| 80 < HCLK <= 100 | 16 < HCLK <= 26 |
  212. |-----------------|-------------------|------------------|
  213. |5WS(6 CPU cycles)| 100 < HCLK <= 120 | 16 < HCLK <= 26 |
  214. +--------------------------------------------------------+
  215. * @{
  216. */
  217. /**
  218. * @brief Reset the RCC clock configuration to the default reset state.
  219. * @note The default reset state of the clock configuration is given below:
  220. * - MSI ON and used as system clock source
  221. * - HSE, HSI, PLL, PLLSAI1 and PLLSAI2 OFF
  222. * - AHB, APB1 and APB2 prescalers set to 1.
  223. * - CSS, MCO1 OFF
  224. * - All interrupts disabled
  225. * - All interrupt and reset flags cleared
  226. * @note This function does not modify the configuration of the
  227. * - Peripheral clock sources
  228. * - LSI, LSE and RTC clocks (Backup domain)
  229. * @retval HAL status
  230. */
  231. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  232. {
  233. uint32_t tickstart;
  234. /* Reset to default System clock */
  235. /* Set MSION bit */
  236. SET_BIT(RCC->CR, RCC_CR_MSION);
  237. /* Insure MSIRDY bit is set before writing default MSIRANGE value */
  238. /* Get start tick */
  239. tickstart = HAL_GetTick();
  240. /* Wait till MSI is ready */
  241. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
  242. {
  243. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  244. {
  245. return HAL_TIMEOUT;
  246. }
  247. }
  248. /* Set MSIRANGE default value */
  249. MODIFY_REG(RCC->CR, RCC_CR_MSIRANGE, RCC_MSIRANGE_6);
  250. /* Reset CFGR register (MSI is selected as system clock source) */
  251. CLEAR_REG(RCC->CFGR);
  252. /* Update the SystemCoreClock global variable for MSI as system clock source */
  253. SystemCoreClock = MSI_VALUE;
  254. /* Configure the source of time base considering new system clock settings */
  255. if(HAL_InitTick(uwTickPrio) != HAL_OK)
  256. {
  257. return HAL_ERROR;
  258. }
  259. /* Insure MSI selected as system clock source */
  260. /* Get start tick */
  261. tickstart = HAL_GetTick();
  262. /* Wait till system clock source is ready */
  263. while(READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != RCC_CFGR_SWS_MSI)
  264. {
  265. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  266. {
  267. return HAL_TIMEOUT;
  268. }
  269. }
  270. /* Reset HSION, HSIKERON, HSIASFS, HSEON, HSECSSON, PLLON, PLLSAIxON bits */
  271. #if defined(RCC_PLLSAI2_SUPPORT)
  272. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON | RCC_CR_PLLSAI1ON | RCC_CR_PLLSAI2ON);
  273. #elif defined(RCC_PLLSAI1_SUPPORT)
  274. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON | RCC_CR_PLLSAI1ON);
  275. #else
  276. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON);
  277. #endif /* RCC_PLLSAI2_SUPPORT */
  278. /* Insure PLLRDY, PLLSAI1RDY and PLLSAI2RDY (if present) are reset */
  279. /* Get start tick */
  280. tickstart = HAL_GetTick();
  281. #if defined(RCC_PLLSAI2_SUPPORT)
  282. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY | RCC_CR_PLLSAI2RDY) != 0U)
  283. #elif defined(RCC_PLLSAI1_SUPPORT)
  284. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY) != 0U)
  285. #else
  286. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  287. #endif
  288. {
  289. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  290. {
  291. return HAL_TIMEOUT;
  292. }
  293. }
  294. /* Reset PLLCFGR register */
  295. CLEAR_REG(RCC->PLLCFGR);
  296. SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN_4 );
  297. #if defined(RCC_PLLSAI1_SUPPORT)
  298. /* Reset PLLSAI1CFGR register */
  299. CLEAR_REG(RCC->PLLSAI1CFGR);
  300. SET_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N_4 );
  301. #endif /* RCC_PLLSAI1_SUPPORT */
  302. #if defined(RCC_PLLSAI2_SUPPORT)
  303. /* Reset PLLSAI2CFGR register */
  304. CLEAR_REG(RCC->PLLSAI2CFGR);
  305. SET_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N_4 );
  306. #endif /* RCC_PLLSAI2_SUPPORT */
  307. /* Reset HSEBYP bit */
  308. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  309. /* Disable all interrupts */
  310. CLEAR_REG(RCC->CIER);
  311. /* Clear all interrupt flags */
  312. WRITE_REG(RCC->CICR, 0xFFFFFFFFU);
  313. /* Clear all reset flags */
  314. SET_BIT(RCC->CSR, RCC_CSR_RMVF);
  315. return HAL_OK;
  316. }
  317. /**
  318. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  319. * RCC_OscInitTypeDef.
  320. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  321. * contains the configuration information for the RCC Oscillators.
  322. * @note The PLL is not disabled when used as system clock.
  323. * @note The PLL source is not updated when used as PLLSAI(s) clock source.
  324. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  325. * supported by this macro. User should request a transition to LSE Off
  326. * first and then LSE On or LSE Bypass.
  327. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  328. * supported by this macro. User should request a transition to HSE Off
  329. * first and then HSE On or HSE Bypass.
  330. * @note If HSE failed to start, HSE should be disabled before recalling
  331. HAL_RCC_OscConfig().
  332. * @retval HAL status
  333. */
  334. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  335. {
  336. uint32_t tickstart;
  337. HAL_StatusTypeDef status;
  338. uint32_t sysclk_source, pll_config;
  339. /* Check Null pointer */
  340. if(RCC_OscInitStruct == NULL)
  341. {
  342. return HAL_ERROR;
  343. }
  344. /* Check the parameters */
  345. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  346. sysclk_source = __HAL_RCC_GET_SYSCLK_SOURCE();
  347. pll_config = __HAL_RCC_GET_PLL_OSCSOURCE();
  348. /*----------------------------- MSI Configuration --------------------------*/
  349. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI)
  350. {
  351. /* Check the parameters */
  352. assert_param(IS_RCC_MSI(RCC_OscInitStruct->MSIState));
  353. assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
  354. assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
  355. /* Check if MSI is used as system clock or as PLL source when PLL is selected as system clock */
  356. if((sysclk_source == RCC_CFGR_SWS_MSI) ||
  357. ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_config == RCC_PLLSOURCE_MSI)))
  358. {
  359. if((READ_BIT(RCC->CR, RCC_CR_MSIRDY) != 0U) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF))
  360. {
  361. return HAL_ERROR;
  362. }
  363. /* Otherwise, just the calibration and MSI range change are allowed */
  364. else
  365. {
  366. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  367. must be correctly programmed according to the frequency of the CPU clock
  368. (HCLK) and the supply voltage of the device. */
  369. if(RCC_OscInitStruct->MSIClockRange > __HAL_RCC_GET_MSI_RANGE())
  370. {
  371. /* First increase number of wait states update if necessary */
  372. if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  373. {
  374. return HAL_ERROR;
  375. }
  376. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  377. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  378. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  379. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  380. }
  381. else
  382. {
  383. /* Else, keep current flash latency while decreasing applies */
  384. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  385. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  386. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  387. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  388. /* Decrease number of wait states update if necessary */
  389. /* Only possible when MSI is the System clock source */
  390. if(sysclk_source == RCC_CFGR_SWS_MSI)
  391. {
  392. if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  393. {
  394. return HAL_ERROR;
  395. }
  396. }
  397. }
  398. /* Update the SystemCoreClock global variable */
  399. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> (AHBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos] & 0x1FU);
  400. /* Configure the source of time base considering new system clocks settings*/
  401. status = HAL_InitTick(uwTickPrio);
  402. if(status != HAL_OK)
  403. {
  404. return status;
  405. }
  406. }
  407. }
  408. else
  409. {
  410. /* Check the MSI State */
  411. if(RCC_OscInitStruct->MSIState != RCC_MSI_OFF)
  412. {
  413. /* Enable the Internal High Speed oscillator (MSI). */
  414. __HAL_RCC_MSI_ENABLE();
  415. /* Get timeout */
  416. tickstart = HAL_GetTick();
  417. /* Wait till MSI is ready */
  418. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
  419. {
  420. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  421. {
  422. return HAL_TIMEOUT;
  423. }
  424. }
  425. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  426. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  427. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  428. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  429. }
  430. else
  431. {
  432. /* Disable the Internal High Speed oscillator (MSI). */
  433. __HAL_RCC_MSI_DISABLE();
  434. /* Get timeout */
  435. tickstart = HAL_GetTick();
  436. /* Wait till MSI is ready */
  437. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) != 0U)
  438. {
  439. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  440. {
  441. return HAL_TIMEOUT;
  442. }
  443. }
  444. }
  445. }
  446. }
  447. /*------------------------------- HSE Configuration ------------------------*/
  448. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  449. {
  450. /* Check the parameters */
  451. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  452. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  453. if((sysclk_source == RCC_CFGR_SWS_HSE) ||
  454. ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_config == RCC_PLLSOURCE_HSE)))
  455. {
  456. if((READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  457. {
  458. return HAL_ERROR;
  459. }
  460. }
  461. else
  462. {
  463. /* Set the new HSE configuration ---------------------------------------*/
  464. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  465. /* Check the HSE State */
  466. if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  467. {
  468. /* Get Start Tick*/
  469. tickstart = HAL_GetTick();
  470. /* Wait till HSE is ready */
  471. while(READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  472. {
  473. if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  474. {
  475. return HAL_TIMEOUT;
  476. }
  477. }
  478. }
  479. else
  480. {
  481. /* Get Start Tick*/
  482. tickstart = HAL_GetTick();
  483. /* Wait till HSE is disabled */
  484. while(READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
  485. {
  486. if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  487. {
  488. return HAL_TIMEOUT;
  489. }
  490. }
  491. }
  492. }
  493. }
  494. /*----------------------------- HSI Configuration --------------------------*/
  495. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  496. {
  497. /* Check the parameters */
  498. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  499. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  500. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  501. if((sysclk_source == RCC_CFGR_SWS_HSI) ||
  502. ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_config == RCC_PLLSOURCE_HSI)))
  503. {
  504. /* When HSI is used as system clock it will not be disabled */
  505. if((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  506. {
  507. return HAL_ERROR;
  508. }
  509. /* Otherwise, just the calibration is allowed */
  510. else
  511. {
  512. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  513. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  514. }
  515. }
  516. else
  517. {
  518. /* Check the HSI State */
  519. if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  520. {
  521. /* Enable the Internal High Speed oscillator (HSI). */
  522. __HAL_RCC_HSI_ENABLE();
  523. /* Get Start Tick*/
  524. tickstart = HAL_GetTick();
  525. /* Wait till HSI is ready */
  526. while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  527. {
  528. if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  529. {
  530. return HAL_TIMEOUT;
  531. }
  532. }
  533. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  534. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  535. }
  536. else
  537. {
  538. /* Disable the Internal High Speed oscillator (HSI). */
  539. __HAL_RCC_HSI_DISABLE();
  540. /* Get Start Tick*/
  541. tickstart = HAL_GetTick();
  542. /* Wait till HSI is disabled */
  543. while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U)
  544. {
  545. if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  546. {
  547. return HAL_TIMEOUT;
  548. }
  549. }
  550. }
  551. }
  552. }
  553. /*------------------------------ LSI Configuration -------------------------*/
  554. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  555. {
  556. /* Check the parameters */
  557. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  558. /* Check the LSI State */
  559. if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  560. {
  561. #if defined(RCC_CSR_LSIPREDIV)
  562. uint32_t csr_temp = RCC->CSR;
  563. /* Check LSI division factor */
  564. assert_param(IS_RCC_LSIDIV(RCC_OscInitStruct->LSIDiv));
  565. if (RCC_OscInitStruct->LSIDiv != (csr_temp & RCC_CSR_LSIPREDIV))
  566. {
  567. if (((csr_temp & RCC_CSR_LSIRDY) == RCC_CSR_LSIRDY) && \
  568. ((csr_temp & RCC_CSR_LSION) != RCC_CSR_LSION))
  569. {
  570. /* If LSIRDY is set while LSION is not enabled,
  571. LSIPREDIV can't be updated */
  572. return HAL_ERROR;
  573. }
  574. /* Turn off LSI before changing RCC_CSR_LSIPREDIV */
  575. if ((csr_temp & RCC_CSR_LSION) == RCC_CSR_LSION)
  576. {
  577. __HAL_RCC_LSI_DISABLE();
  578. /* Get Start Tick*/
  579. tickstart = HAL_GetTick();
  580. /* Wait till LSI is disabled */
  581. while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
  582. {
  583. if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  584. {
  585. return HAL_TIMEOUT;
  586. }
  587. }
  588. }
  589. /* Set LSI division factor */
  590. MODIFY_REG(RCC->CSR, RCC_CSR_LSIPREDIV, RCC_OscInitStruct->LSIDiv);
  591. }
  592. #endif /* RCC_CSR_LSIPREDIV */
  593. /* Enable the Internal Low Speed oscillator (LSI). */
  594. __HAL_RCC_LSI_ENABLE();
  595. /* Get Start Tick*/
  596. tickstart = HAL_GetTick();
  597. /* Wait till LSI is ready */
  598. while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
  599. {
  600. if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  601. {
  602. return HAL_TIMEOUT;
  603. }
  604. }
  605. }
  606. else
  607. {
  608. /* Disable the Internal Low Speed oscillator (LSI). */
  609. __HAL_RCC_LSI_DISABLE();
  610. /* Get Start Tick*/
  611. tickstart = HAL_GetTick();
  612. /* Wait till LSI is disabled */
  613. while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
  614. {
  615. if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  616. {
  617. return HAL_TIMEOUT;
  618. }
  619. }
  620. }
  621. }
  622. /*------------------------------ LSE Configuration -------------------------*/
  623. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  624. {
  625. FlagStatus pwrclkchanged = RESET;
  626. /* Check the parameters */
  627. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  628. /* Update LSE configuration in Backup Domain control register */
  629. /* Requires to enable write access to Backup Domain of necessary */
  630. if(HAL_IS_BIT_CLR(RCC->APB1ENR1, RCC_APB1ENR1_PWREN))
  631. {
  632. __HAL_RCC_PWR_CLK_ENABLE();
  633. pwrclkchanged = SET;
  634. }
  635. if(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  636. {
  637. /* Enable write access to Backup domain */
  638. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  639. /* Wait for Backup domain Write protection disable */
  640. tickstart = HAL_GetTick();
  641. while(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  642. {
  643. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  644. {
  645. return HAL_TIMEOUT;
  646. }
  647. }
  648. }
  649. /* Set the new LSE configuration -----------------------------------------*/
  650. #if defined(RCC_BDCR_LSESYSDIS)
  651. if((RCC_OscInitStruct->LSEState & RCC_BDCR_LSEON) != 0U)
  652. {
  653. /* Set LSESYSDIS bit according to LSE propagation option (enabled or disabled) */
  654. MODIFY_REG(RCC->BDCR, RCC_BDCR_LSESYSDIS, (RCC_OscInitStruct->LSEState & RCC_BDCR_LSESYSDIS));
  655. if((RCC_OscInitStruct->LSEState & RCC_BDCR_LSEBYP) != 0U)
  656. {
  657. /* LSE oscillator bypass enable */
  658. SET_BIT(RCC->BDCR, RCC_BDCR_LSEBYP);
  659. SET_BIT(RCC->BDCR, RCC_BDCR_LSEON);
  660. }
  661. else
  662. {
  663. /* LSE oscillator enable */
  664. SET_BIT(RCC->BDCR, RCC_BDCR_LSEON);
  665. }
  666. }
  667. else
  668. {
  669. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEON);
  670. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEBYP);
  671. }
  672. #else
  673. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  674. #endif /* RCC_BDCR_LSESYSDIS */
  675. /* Check the LSE State */
  676. if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  677. {
  678. /* Get Start Tick*/
  679. tickstart = HAL_GetTick();
  680. /* Wait till LSE is ready */
  681. while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
  682. {
  683. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  684. {
  685. return HAL_TIMEOUT;
  686. }
  687. }
  688. }
  689. else
  690. {
  691. /* Get Start Tick*/
  692. tickstart = HAL_GetTick();
  693. /* Wait till LSE is disabled */
  694. while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != 0U)
  695. {
  696. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  697. {
  698. return HAL_TIMEOUT;
  699. }
  700. }
  701. #if defined(RCC_BDCR_LSESYSDIS)
  702. /* By default, stop disabling LSE propagation */
  703. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSESYSDIS);
  704. #endif /* RCC_BDCR_LSESYSDIS */
  705. }
  706. /* Restore clock configuration if changed */
  707. if(pwrclkchanged == SET)
  708. {
  709. __HAL_RCC_PWR_CLK_DISABLE();
  710. }
  711. }
  712. #if defined(RCC_HSI48_SUPPORT)
  713. /*------------------------------ HSI48 Configuration -----------------------*/
  714. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  715. {
  716. /* Check the parameters */
  717. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  718. /* Check the LSI State */
  719. if(RCC_OscInitStruct->HSI48State != RCC_HSI48_OFF)
  720. {
  721. /* Enable the Internal Low Speed oscillator (HSI48). */
  722. __HAL_RCC_HSI48_ENABLE();
  723. /* Get Start Tick*/
  724. tickstart = HAL_GetTick();
  725. /* Wait till HSI48 is ready */
  726. while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) == 0U)
  727. {
  728. if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  729. {
  730. return HAL_TIMEOUT;
  731. }
  732. }
  733. }
  734. else
  735. {
  736. /* Disable the Internal Low Speed oscillator (HSI48). */
  737. __HAL_RCC_HSI48_DISABLE();
  738. /* Get Start Tick*/
  739. tickstart = HAL_GetTick();
  740. /* Wait till HSI48 is disabled */
  741. while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) != 0U)
  742. {
  743. if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  744. {
  745. return HAL_TIMEOUT;
  746. }
  747. }
  748. }
  749. }
  750. #endif /* RCC_HSI48_SUPPORT */
  751. /*-------------------------------- PLL Configuration -----------------------*/
  752. /* Check the parameters */
  753. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  754. if(RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
  755. {
  756. /* PLL On ? */
  757. if(RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
  758. {
  759. /* Check the parameters */
  760. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  761. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  762. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  763. #if defined(RCC_PLLP_SUPPORT)
  764. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  765. #endif /* RCC_PLLP_SUPPORT */
  766. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  767. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  768. /* Do nothing if PLL configuration is the unchanged */
  769. pll_config = RCC->PLLCFGR;
  770. if((READ_BIT(pll_config, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
  771. (READ_BIT(pll_config, RCC_PLLCFGR_PLLM) != ((RCC_OscInitStruct->PLL.PLLM - 1U) << RCC_PLLCFGR_PLLM_Pos)) ||
  772. (READ_BIT(pll_config, RCC_PLLCFGR_PLLN) != (RCC_OscInitStruct->PLL.PLLN << RCC_PLLCFGR_PLLN_Pos)) ||
  773. #if defined(RCC_PLLP_SUPPORT)
  774. #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
  775. (READ_BIT(pll_config, RCC_PLLCFGR_PLLPDIV) != (RCC_OscInitStruct->PLL.PLLP << RCC_PLLCFGR_PLLPDIV_Pos)) ||
  776. #else
  777. (READ_BIT(pll_config, RCC_PLLCFGR_PLLP) != ((RCC_OscInitStruct->PLL.PLLP == RCC_PLLP_DIV7) ? 0U : 1U)) ||
  778. #endif
  779. #endif
  780. (READ_BIT(pll_config, RCC_PLLCFGR_PLLQ) != ((((RCC_OscInitStruct->PLL.PLLQ) >> 1U) - 1U) << RCC_PLLCFGR_PLLQ_Pos)) ||
  781. (READ_BIT(pll_config, RCC_PLLCFGR_PLLR) != ((((RCC_OscInitStruct->PLL.PLLR) >> 1U) - 1U) << RCC_PLLCFGR_PLLR_Pos)))
  782. {
  783. /* Check if the PLL is used as system clock or not */
  784. if(sysclk_source != RCC_CFGR_SWS_PLL)
  785. {
  786. #if defined(RCC_PLLSAI1_SUPPORT) || defined(RCC_PLLSAI2_SUPPORT)
  787. /* Check if main PLL can be updated */
  788. /* Not possible if the source is shared by other enabled PLLSAIx */
  789. if((READ_BIT(RCC->CR, RCC_CR_PLLSAI1ON) != 0U)
  790. #if defined(RCC_PLLSAI2_SUPPORT)
  791. || (READ_BIT(RCC->CR, RCC_CR_PLLSAI2ON) != 0U)
  792. #endif
  793. )
  794. {
  795. return HAL_ERROR;
  796. }
  797. else
  798. #endif /* RCC_PLLSAI1_SUPPORT || RCC_PLLSAI2_SUPPORT */
  799. {
  800. /* Disable the main PLL. */
  801. __HAL_RCC_PLL_DISABLE();
  802. /* Get Start Tick*/
  803. tickstart = HAL_GetTick();
  804. /* Wait till PLL is ready */
  805. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  806. {
  807. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  808. {
  809. return HAL_TIMEOUT;
  810. }
  811. }
  812. /* Configure the main PLL clock source, multiplication and division factors. */
  813. #if defined(RCC_PLLP_SUPPORT)
  814. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  815. RCC_OscInitStruct->PLL.PLLM,
  816. RCC_OscInitStruct->PLL.PLLN,
  817. RCC_OscInitStruct->PLL.PLLP,
  818. RCC_OscInitStruct->PLL.PLLQ,
  819. RCC_OscInitStruct->PLL.PLLR);
  820. #else
  821. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  822. RCC_OscInitStruct->PLL.PLLM,
  823. RCC_OscInitStruct->PLL.PLLN,
  824. RCC_OscInitStruct->PLL.PLLQ,
  825. RCC_OscInitStruct->PLL.PLLR);
  826. #endif
  827. /* Enable the main PLL. */
  828. __HAL_RCC_PLL_ENABLE();
  829. /* Enable PLL System Clock output. */
  830. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
  831. /* Get Start Tick*/
  832. tickstart = HAL_GetTick();
  833. /* Wait till PLL is ready */
  834. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  835. {
  836. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  837. {
  838. return HAL_TIMEOUT;
  839. }
  840. }
  841. }
  842. }
  843. else
  844. {
  845. /* PLL is already used as System core clock */
  846. return HAL_ERROR;
  847. }
  848. }
  849. else
  850. {
  851. /* PLL configuration is unchanged */
  852. /* Re-enable PLL if it was disabled (ie. low power mode) */
  853. if(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  854. {
  855. /* Enable the main PLL. */
  856. __HAL_RCC_PLL_ENABLE();
  857. /* Enable PLL System Clock output. */
  858. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
  859. /* Get Start Tick*/
  860. tickstart = HAL_GetTick();
  861. /* Wait till PLL is ready */
  862. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  863. {
  864. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  865. {
  866. return HAL_TIMEOUT;
  867. }
  868. }
  869. }
  870. }
  871. }
  872. else
  873. {
  874. /* Check that PLL is not used as system clock or not */
  875. if(sysclk_source != RCC_CFGR_SWS_PLL)
  876. {
  877. /* Disable the main PLL. */
  878. __HAL_RCC_PLL_DISABLE();
  879. /* Get Start Tick*/
  880. tickstart = HAL_GetTick();
  881. /* Wait till PLL is disabled */
  882. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
  883. {
  884. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  885. {
  886. return HAL_TIMEOUT;
  887. }
  888. }
  889. /* Unselect main PLL clock source and disable main PLL outputs to save power */
  890. #if defined(RCC_PLLSAI2_SUPPORT)
  891. RCC->PLLCFGR &= ~(RCC_PLLCFGR_PLLSRC | RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI3CLK);
  892. #elif defined(RCC_PLLSAI1_SUPPORT)
  893. RCC->PLLCFGR &= ~(RCC_PLLCFGR_PLLSRC | RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI2CLK);
  894. #else
  895. RCC->PLLCFGR &= ~(RCC_PLLCFGR_PLLSRC | RCC_PLL_SYSCLK | RCC_PLL_48M1CLK);
  896. #endif /* RCC_PLLSAI2_SUPPORT */
  897. }
  898. else
  899. {
  900. /* PLL is already used as System core clock */
  901. return HAL_ERROR;
  902. }
  903. }
  904. }
  905. return HAL_OK;
  906. }
  907. /**
  908. * @brief Initialize the CPU, AHB and APB busses clocks according to the specified
  909. * parameters in the RCC_ClkInitStruct.
  910. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  911. * contains the configuration information for the RCC peripheral.
  912. * @param FLatency FLASH Latency
  913. * This parameter can be one of the following values:
  914. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  915. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  916. * @arg FLASH_LATENCY_2 FLASH 2 Latency cycles
  917. * @arg FLASH_LATENCY_3 FLASH 3 Latency cycles
  918. * @arg FLASH_LATENCY_4 FLASH 4 Latency cycles
  919. @if STM32L4S9xx
  920. * @arg FLASH_LATENCY_5 FLASH 5 Latency cycles
  921. * @arg FLASH_LATENCY_6 FLASH 6 Latency cycles
  922. * @arg FLASH_LATENCY_7 FLASH 7 Latency cycles
  923. * @arg FLASH_LATENCY_8 FLASH 8 Latency cycles
  924. * @arg FLASH_LATENCY_9 FLASH 9 Latency cycles
  925. * @arg FLASH_LATENCY_10 FLASH 10 Latency cycles
  926. * @arg FLASH_LATENCY_11 FLASH 11 Latency cycles
  927. * @arg FLASH_LATENCY_12 FLASH 12 Latency cycles
  928. * @arg FLASH_LATENCY_13 FLASH 13 Latency cycles
  929. * @arg FLASH_LATENCY_14 FLASH 14 Latency cycles
  930. * @arg FLASH_LATENCY_15 FLASH 15 Latency cycles
  931. @endif
  932. *
  933. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  934. * and updated by HAL_RCC_GetHCLKFreq() function called within this function
  935. *
  936. * @note The MSI is used by default as system clock source after
  937. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  938. * the MSI frequency is set to its default value 4 MHz.
  939. *
  940. * @note The HSI can be selected as system clock source after
  941. * from STOP modes or in case of failure of the HSE used directly or indirectly
  942. * as system clock (if the Clock Security System CSS is enabled).
  943. *
  944. * @note A switch from one clock source to another occurs only if the target
  945. * clock source is ready (clock stable after startup delay or PLL locked).
  946. * If a clock source which is not yet ready is selected, the switch will
  947. * occur when the clock source is ready.
  948. *
  949. * @note You can use HAL_RCC_GetClockConfig() function to know which clock is
  950. * currently used as system clock source.
  951. *
  952. * @note Depending on the device voltage range, the software has to set correctly
  953. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  954. * (for more details refer to section above "Initialization/de-initialization functions")
  955. * @retval None
  956. */
  957. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  958. {
  959. uint32_t tickstart;
  960. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  961. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  962. uint32_t hpre = RCC_SYSCLK_DIV1;
  963. #endif
  964. HAL_StatusTypeDef status;
  965. /* Check Null pointer */
  966. if(RCC_ClkInitStruct == NULL)
  967. {
  968. return HAL_ERROR;
  969. }
  970. /* Check the parameters */
  971. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  972. assert_param(IS_FLASH_LATENCY(FLatency));
  973. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  974. must be correctly programmed according to the frequency of the CPU clock
  975. (HCLK) and the supply voltage of the device. */
  976. /* Increasing the number of wait states because of higher CPU frequency */
  977. if(FLatency > __HAL_FLASH_GET_LATENCY())
  978. {
  979. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  980. __HAL_FLASH_SET_LATENCY(FLatency);
  981. /* Check that the new number of wait states is taken into account to access the Flash
  982. memory by reading the FLASH_ACR register */
  983. if(__HAL_FLASH_GET_LATENCY() != FLatency)
  984. {
  985. return HAL_ERROR;
  986. }
  987. }
  988. /*----------------- HCLK Configuration prior to SYSCLK----------------------*/
  989. /* Apply higher HCLK prescaler request here to ensure CPU clock is not of of spec when SYSCLK is increased */
  990. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  991. {
  992. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  993. if(RCC_ClkInitStruct->AHBCLKDivider > READ_BIT(RCC->CFGR, RCC_CFGR_HPRE))
  994. {
  995. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  996. }
  997. }
  998. /*------------------------- SYSCLK Configuration ---------------------------*/
  999. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  1000. {
  1001. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  1002. /* PLL is selected as System Clock Source */
  1003. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  1004. {
  1005. /* Check the PLL ready flag */
  1006. if(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
  1007. {
  1008. return HAL_ERROR;
  1009. }
  1010. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  1011. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1012. /* Undershoot management when selection PLL as SYSCLK source and frequency above 80Mhz */
  1013. /* Compute target PLL output frequency */
  1014. if(RCC_GetSysClockFreqFromPLLSource() > 80000000U)
  1015. {
  1016. /* If lowest HCLK prescaler, apply intermediate step with HCLK prescaler 2 necessary before to go over 80Mhz */
  1017. if(READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1)
  1018. {
  1019. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
  1020. hpre = RCC_SYSCLK_DIV2;
  1021. }
  1022. }
  1023. #endif
  1024. }
  1025. else
  1026. {
  1027. /* HSE is selected as System Clock Source */
  1028. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  1029. {
  1030. /* Check the HSE ready flag */
  1031. if(READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
  1032. {
  1033. return HAL_ERROR;
  1034. }
  1035. }
  1036. /* MSI is selected as System Clock Source */
  1037. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  1038. {
  1039. /* Check the MSI ready flag */
  1040. if(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
  1041. {
  1042. return HAL_ERROR;
  1043. }
  1044. }
  1045. /* HSI is selected as System Clock Source */
  1046. else
  1047. {
  1048. /* Check the HSI ready flag */
  1049. if(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
  1050. {
  1051. return HAL_ERROR;
  1052. }
  1053. }
  1054. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  1055. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1056. /* Overshoot management when going down from PLL as SYSCLK source and frequency above 80Mhz */
  1057. if(HAL_RCC_GetSysClockFreq() > 80000000U)
  1058. {
  1059. /* If lowest HCLK prescaler, apply intermediate step with HCLK prescaler 2 necessary before to go under 80Mhz */
  1060. if(READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1)
  1061. {
  1062. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
  1063. hpre = RCC_SYSCLK_DIV2;
  1064. }
  1065. }
  1066. #endif
  1067. }
  1068. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  1069. /* Get Start Tick*/
  1070. tickstart = HAL_GetTick();
  1071. while(__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  1072. {
  1073. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  1074. {
  1075. return HAL_TIMEOUT;
  1076. }
  1077. }
  1078. }
  1079. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  1080. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1081. /* Is intermediate HCLK prescaler 2 applied internally, resume with HCLK prescaler 1 */
  1082. if(hpre == RCC_SYSCLK_DIV2)
  1083. {
  1084. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV1);
  1085. }
  1086. #endif
  1087. /*----------------- HCLK Configuration after SYSCLK-------------------------*/
  1088. /* Apply lower HCLK prescaler request here to ensure CPU clock is not of of spec when SYSCLK is set */
  1089. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  1090. {
  1091. if(RCC_ClkInitStruct->AHBCLKDivider < READ_BIT(RCC->CFGR, RCC_CFGR_HPRE))
  1092. {
  1093. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  1094. }
  1095. }
  1096. /* Allow decreasing of the number of wait states (because of lower CPU frequency expected) */
  1097. if(FLatency < __HAL_FLASH_GET_LATENCY())
  1098. {
  1099. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  1100. __HAL_FLASH_SET_LATENCY(FLatency);
  1101. /* Check that the new number of wait states is taken into account to access the Flash
  1102. memory by reading the FLASH_ACR register */
  1103. if(__HAL_FLASH_GET_LATENCY() != FLatency)
  1104. {
  1105. return HAL_ERROR;
  1106. }
  1107. }
  1108. /*-------------------------- PCLK1 Configuration ---------------------------*/
  1109. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  1110. {
  1111. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  1112. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  1113. }
  1114. /*-------------------------- PCLK2 Configuration ---------------------------*/
  1115. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  1116. {
  1117. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  1118. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  1119. }
  1120. /* Update the SystemCoreClock global variable */
  1121. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> (AHBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos] & 0x1FU);
  1122. /* Configure the source of time base considering new system clocks settings*/
  1123. status = HAL_InitTick(uwTickPrio);
  1124. return status;
  1125. }
  1126. /**
  1127. * @}
  1128. */
  1129. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  1130. * @brief RCC clocks control functions
  1131. *
  1132. @verbatim
  1133. ===============================================================================
  1134. ##### Peripheral Control functions #####
  1135. ===============================================================================
  1136. [..]
  1137. This subsection provides a set of functions allowing to:
  1138. (+) Output clock to MCO pin.
  1139. (+) Retrieve current clock frequencies.
  1140. (+) Enable the Clock Security System.
  1141. @endverbatim
  1142. * @{
  1143. */
  1144. /**
  1145. * @brief Select the clock source to output on MCO pin(PA8).
  1146. * @note PA8 should be configured in alternate function mode.
  1147. * @param RCC_MCOx specifies the output direction for the clock source.
  1148. * For STM32L4xx family this parameter can have only one value:
  1149. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
  1150. * @param RCC_MCOSource specifies the clock source to output.
  1151. * This parameter can be one of the following values:
  1152. * @arg @ref RCC_MCO1SOURCE_NOCLOCK MCO output disabled, no clock on MCO
  1153. * @arg @ref RCC_MCO1SOURCE_SYSCLK system clock selected as MCO source
  1154. * @arg @ref RCC_MCO1SOURCE_MSI MSI clock selected as MCO source
  1155. * @arg @ref RCC_MCO1SOURCE_HSI HSI clock selected as MCO source
  1156. * @arg @ref RCC_MCO1SOURCE_HSE HSE clock selected as MCO source
  1157. * @arg @ref RCC_MCO1SOURCE_PLLCLK main PLL clock selected as MCO source
  1158. * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO source
  1159. * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO source
  1160. @if STM32L443xx
  1161. * @arg @ref RCC_MCO1SOURCE_HSI48 HSI48 clock selected as MCO source for devices with HSI48
  1162. @endif
  1163. * @param RCC_MCODiv specifies the MCO prescaler.
  1164. * This parameter can be one of the following values:
  1165. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  1166. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  1167. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  1168. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  1169. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  1170. * @retval None
  1171. */
  1172. void HAL_RCC_MCOConfig( uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  1173. {
  1174. GPIO_InitTypeDef GPIO_InitStruct;
  1175. /* Check the parameters */
  1176. assert_param(IS_RCC_MCO(RCC_MCOx));
  1177. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  1178. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  1179. /* Prevent unused argument(s) compilation warning if no assert_param check */
  1180. UNUSED(RCC_MCOx);
  1181. /* MCO Clock Enable */
  1182. __MCO1_CLK_ENABLE();
  1183. /* Configure the MCO1 pin in alternate function mode */
  1184. GPIO_InitStruct.Pin = MCO1_PIN;
  1185. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1186. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  1187. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1188. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  1189. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  1190. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO1 clock source and prescaler */
  1191. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE), (RCC_MCOSource | RCC_MCODiv ));
  1192. }
  1193. /**
  1194. * @brief Return the SYSCLK frequency.
  1195. *
  1196. * @note The system frequency computed by this function is not the real
  1197. * frequency in the chip. It is calculated based on the predefined
  1198. * constant and the selected clock source:
  1199. * @note If SYSCLK source is MSI, function returns values based on MSI
  1200. * Value as defined by the MSI range.
  1201. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  1202. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  1203. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**),
  1204. * HSI_VALUE(*) or MSI Value multiplied/divided by the PLL factors.
  1205. * @note (*) HSI_VALUE is a constant defined in stm32l4xx_hal_conf.h file (default value
  1206. * 16 MHz) but the real value may vary depending on the variations
  1207. * in voltage and temperature.
  1208. * @note (**) HSE_VALUE is a constant defined in stm32l4xx_hal_conf.h file (default value
  1209. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  1210. * frequency of the crystal used. Otherwise, this function may
  1211. * have wrong result.
  1212. *
  1213. * @note The result of this function could be not correct when using fractional
  1214. * value for HSE crystal.
  1215. *
  1216. * @note This function can be used by the user application to compute the
  1217. * baudrate for the communication peripherals or configure other parameters.
  1218. *
  1219. * @note Each time SYSCLK changes, this function must be called to update the
  1220. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  1221. *
  1222. *
  1223. * @retval SYSCLK frequency
  1224. */
  1225. uint32_t HAL_RCC_GetSysClockFreq(void)
  1226. {
  1227. uint32_t msirange = 0U, sysclockfreq = 0U;
  1228. uint32_t pllvco, pllsource, pllr, pllm; /* no init needed */
  1229. uint32_t sysclk_source, pll_oscsource;
  1230. sysclk_source = __HAL_RCC_GET_SYSCLK_SOURCE();
  1231. pll_oscsource = __HAL_RCC_GET_PLL_OSCSOURCE();
  1232. if((sysclk_source == RCC_CFGR_SWS_MSI) ||
  1233. ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_oscsource == RCC_PLLSOURCE_MSI)))
  1234. {
  1235. /* MSI or PLL with MSI source used as system clock source */
  1236. /* Get SYSCLK source */
  1237. if(READ_BIT(RCC->CR, RCC_CR_MSIRGSEL) == 0U)
  1238. { /* MSISRANGE from RCC_CSR applies */
  1239. msirange = READ_BIT(RCC->CSR, RCC_CSR_MSISRANGE) >> RCC_CSR_MSISRANGE_Pos;
  1240. }
  1241. else
  1242. { /* MSIRANGE from RCC_CR applies */
  1243. msirange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos;
  1244. }
  1245. /*MSI frequency range in HZ*/
  1246. msirange = MSIRangeTable[msirange];
  1247. if(sysclk_source == RCC_CFGR_SWS_MSI)
  1248. {
  1249. /* MSI used as system clock source */
  1250. sysclockfreq = msirange;
  1251. }
  1252. }
  1253. else if(sysclk_source == RCC_CFGR_SWS_HSI)
  1254. {
  1255. /* HSI used as system clock source */
  1256. sysclockfreq = HSI_VALUE;
  1257. }
  1258. else if(sysclk_source == RCC_CFGR_SWS_HSE)
  1259. {
  1260. /* HSE used as system clock source */
  1261. sysclockfreq = HSE_VALUE;
  1262. }
  1263. else
  1264. {
  1265. /* unexpected case: sysclockfreq at 0 */
  1266. }
  1267. if(sysclk_source == RCC_CFGR_SWS_PLL)
  1268. {
  1269. /* PLL used as system clock source */
  1270. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE) * PLLN / PLLM
  1271. SYSCLK = PLL_VCO / PLLR
  1272. */
  1273. pllsource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
  1274. switch (pllsource)
  1275. {
  1276. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1277. pllvco = HSI_VALUE;
  1278. break;
  1279. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1280. pllvco = HSE_VALUE;
  1281. break;
  1282. case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1283. default:
  1284. pllvco = msirange;
  1285. break;
  1286. }
  1287. pllm = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
  1288. pllvco = (pllvco * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)) / pllm;
  1289. pllr = ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U ) * 2U;
  1290. sysclockfreq = pllvco / pllr;
  1291. }
  1292. return sysclockfreq;
  1293. }
  1294. /**
  1295. * @brief Return the HCLK frequency.
  1296. * @note Each time HCLK changes, this function must be called to update the
  1297. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  1298. *
  1299. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
  1300. * @retval HCLK frequency in Hz
  1301. */
  1302. uint32_t HAL_RCC_GetHCLKFreq(void)
  1303. {
  1304. return SystemCoreClock;
  1305. }
  1306. /**
  1307. * @brief Return the PCLK1 frequency.
  1308. * @note Each time PCLK1 changes, this function must be called to update the
  1309. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1310. * @retval PCLK1 frequency in Hz
  1311. */
  1312. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1313. {
  1314. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1315. return (HAL_RCC_GetHCLKFreq() >> (APBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos] & 0x1FU));
  1316. }
  1317. /**
  1318. * @brief Return the PCLK2 frequency.
  1319. * @note Each time PCLK2 changes, this function must be called to update the
  1320. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1321. * @retval PCLK2 frequency in Hz
  1322. */
  1323. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1324. {
  1325. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  1326. return (HAL_RCC_GetHCLKFreq()>> (APBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_Pos] & 0x1FU));
  1327. }
  1328. /**
  1329. * @brief Configure the RCC_OscInitStruct according to the internal
  1330. * RCC configuration registers.
  1331. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1332. * will be configured.
  1333. * @retval None
  1334. */
  1335. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1336. {
  1337. /* Check the parameters */
  1338. assert_param(RCC_OscInitStruct != (void *)NULL);
  1339. /* Set all possible values for the Oscillator type parameter ---------------*/
  1340. #if defined(RCC_HSI48_SUPPORT)
  1341. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
  1342. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSI48;
  1343. #else
  1344. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
  1345. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  1346. #endif /* RCC_HSI48_SUPPORT */
  1347. /* Get the HSE configuration -----------------------------------------------*/
  1348. if(READ_BIT(RCC->CR, RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1349. {
  1350. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1351. }
  1352. else if(READ_BIT(RCC->CR, RCC_CR_HSEON) == RCC_CR_HSEON)
  1353. {
  1354. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1355. }
  1356. else
  1357. {
  1358. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1359. }
  1360. /* Get the MSI configuration -----------------------------------------------*/
  1361. if(READ_BIT(RCC->CR, RCC_CR_MSION) == RCC_CR_MSION)
  1362. {
  1363. RCC_OscInitStruct->MSIState = RCC_MSI_ON;
  1364. }
  1365. else
  1366. {
  1367. RCC_OscInitStruct->MSIState = RCC_MSI_OFF;
  1368. }
  1369. RCC_OscInitStruct->MSICalibrationValue = READ_BIT(RCC->ICSCR, RCC_ICSCR_MSITRIM) >> RCC_ICSCR_MSITRIM_Pos;
  1370. RCC_OscInitStruct->MSIClockRange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE);
  1371. /* Get the HSI configuration -----------------------------------------------*/
  1372. if(READ_BIT(RCC->CR, RCC_CR_HSION) == RCC_CR_HSION)
  1373. {
  1374. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1375. }
  1376. else
  1377. {
  1378. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1379. }
  1380. RCC_OscInitStruct->HSICalibrationValue = READ_BIT(RCC->ICSCR, RCC_ICSCR_HSITRIM) >> RCC_ICSCR_HSITRIM_Pos;
  1381. /* Get the LSE configuration -----------------------------------------------*/
  1382. if(READ_BIT(RCC->BDCR, RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1383. {
  1384. #if defined(RCC_BDCR_LSESYSDIS)
  1385. if((RCC->BDCR & RCC_BDCR_LSESYSDIS) == RCC_BDCR_LSESYSDIS)
  1386. {
  1387. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS_RTC_ONLY;
  1388. }
  1389. else
  1390. #endif /* RCC_BDCR_LSESYSDIS */
  1391. {
  1392. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1393. }
  1394. }
  1395. else if(READ_BIT(RCC->BDCR, RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1396. {
  1397. #if defined(RCC_BDCR_LSESYSDIS)
  1398. if((RCC->BDCR & RCC_BDCR_LSESYSDIS) == RCC_BDCR_LSESYSDIS)
  1399. {
  1400. RCC_OscInitStruct->LSEState = RCC_LSE_ON_RTC_ONLY;
  1401. }
  1402. else
  1403. #endif /* RCC_BDCR_LSESYSDIS */
  1404. {
  1405. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1406. }
  1407. }
  1408. else
  1409. {
  1410. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1411. }
  1412. /* Get the LSI configuration -----------------------------------------------*/
  1413. if(READ_BIT(RCC->CSR, RCC_CSR_LSION) == RCC_CSR_LSION)
  1414. {
  1415. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1416. }
  1417. else
  1418. {
  1419. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1420. }
  1421. #if defined(RCC_CSR_LSIPREDIV)
  1422. /* Get the LSI configuration -----------------------------------------------*/
  1423. if((RCC->CSR & RCC_CSR_LSIPREDIV) == RCC_CSR_LSIPREDIV)
  1424. {
  1425. RCC_OscInitStruct->LSIDiv = RCC_LSI_DIV128;
  1426. }
  1427. else
  1428. {
  1429. RCC_OscInitStruct->LSIDiv = RCC_LSI_DIV1;
  1430. }
  1431. #endif /* RCC_CSR_LSIPREDIV */
  1432. #if defined(RCC_HSI48_SUPPORT)
  1433. /* Get the HSI48 configuration ---------------------------------------------*/
  1434. if(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48ON) == RCC_CRRCR_HSI48ON)
  1435. {
  1436. RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
  1437. }
  1438. else
  1439. {
  1440. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1441. }
  1442. #else
  1443. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1444. #endif /* RCC_HSI48_SUPPORT */
  1445. /* Get the PLL configuration -----------------------------------------------*/
  1446. if(READ_BIT(RCC->CR, RCC_CR_PLLON) == RCC_CR_PLLON)
  1447. {
  1448. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1449. }
  1450. else
  1451. {
  1452. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1453. }
  1454. RCC_OscInitStruct->PLL.PLLSource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
  1455. RCC_OscInitStruct->PLL.PLLM = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U;
  1456. RCC_OscInitStruct->PLL.PLLN = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  1457. RCC_OscInitStruct->PLL.PLLQ = (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U) << 1U);
  1458. RCC_OscInitStruct->PLL.PLLR = (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U) << 1U);
  1459. #if defined(RCC_PLLP_SUPPORT)
  1460. #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
  1461. RCC_OscInitStruct->PLL.PLLP = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos;
  1462. #else
  1463. if(READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) != 0U)
  1464. {
  1465. RCC_OscInitStruct->PLL.PLLP = RCC_PLLP_DIV17;
  1466. }
  1467. else
  1468. {
  1469. RCC_OscInitStruct->PLL.PLLP = RCC_PLLP_DIV7;
  1470. }
  1471. #endif /* RCC_PLLP_DIV_2_31_SUPPORT */
  1472. #endif /* RCC_PLLP_SUPPORT */
  1473. }
  1474. /**
  1475. * @brief Configure the RCC_ClkInitStruct according to the internal
  1476. * RCC configuration registers.
  1477. * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
  1478. * will be configured.
  1479. * @param pFLatency Pointer on the Flash Latency.
  1480. * @retval None
  1481. */
  1482. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1483. {
  1484. /* Check the parameters */
  1485. assert_param(RCC_ClkInitStruct != (void *)NULL);
  1486. assert_param(pFLatency != (void *)NULL);
  1487. /* Set all possible values for the Clock type parameter --------------------*/
  1488. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  1489. /* Get the SYSCLK configuration --------------------------------------------*/
  1490. RCC_ClkInitStruct->SYSCLKSource = READ_BIT(RCC->CFGR, RCC_CFGR_SW);
  1491. /* Get the HCLK configuration ----------------------------------------------*/
  1492. RCC_ClkInitStruct->AHBCLKDivider = READ_BIT(RCC->CFGR, RCC_CFGR_HPRE);
  1493. /* Get the APB1 configuration ----------------------------------------------*/
  1494. RCC_ClkInitStruct->APB1CLKDivider = READ_BIT(RCC->CFGR, RCC_CFGR_PPRE1);
  1495. /* Get the APB2 configuration ----------------------------------------------*/
  1496. RCC_ClkInitStruct->APB2CLKDivider = (READ_BIT(RCC->CFGR, RCC_CFGR_PPRE2) >> 3U);
  1497. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1498. *pFLatency = __HAL_FLASH_GET_LATENCY();
  1499. }
  1500. /**
  1501. * @brief Enable the Clock Security System.
  1502. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1503. * is automatically disabled and an interrupt is generated to inform the
  1504. * software about the failure (Clock Security System Interrupt, CSSI),
  1505. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1506. * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector.
  1507. * @note The Clock Security System can only be cleared by reset.
  1508. * @retval None
  1509. */
  1510. void HAL_RCC_EnableCSS(void)
  1511. {
  1512. SET_BIT(RCC->CR, RCC_CR_CSSON) ;
  1513. }
  1514. /**
  1515. * @brief Handle the RCC Clock Security System interrupt request.
  1516. * @note This API should be called under the NMI_Handler().
  1517. * @retval None
  1518. */
  1519. void HAL_RCC_NMI_IRQHandler(void)
  1520. {
  1521. /* Check RCC CSSF interrupt flag */
  1522. if(__HAL_RCC_GET_IT(RCC_IT_CSS))
  1523. {
  1524. /* RCC Clock Security System interrupt user callback */
  1525. HAL_RCC_CSSCallback();
  1526. /* Clear RCC CSS pending bit */
  1527. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  1528. }
  1529. }
  1530. /**
  1531. * @brief RCC Clock Security System interrupt callback.
  1532. * @retval none
  1533. */
  1534. __weak void HAL_RCC_CSSCallback(void)
  1535. {
  1536. /* NOTE : This function should not be modified, when the callback is needed,
  1537. the HAL_RCC_CSSCallback should be implemented in the user file
  1538. */
  1539. }
  1540. /**
  1541. * @brief Get and clear reset flags
  1542. * @param None
  1543. * @note Once reset flags are retrieved, this API is clearing them in order
  1544. * to isolate next reset reason.
  1545. * @retval can be a combination of @ref RCC_Reset_Flag
  1546. */
  1547. uint32_t HAL_RCC_GetResetSource(void)
  1548. {
  1549. uint32_t reset;
  1550. /* Get all reset flags */
  1551. reset = RCC->CSR & RCC_RESET_FLAG_ALL;
  1552. /* Clear Reset flags */
  1553. RCC->CSR |= RCC_CSR_RMVF;
  1554. return reset;
  1555. }
  1556. /** * @}
  1557. */
  1558. /**
  1559. * @}
  1560. */
  1561. /* Private function prototypes -----------------------------------------------*/
  1562. /** @addtogroup RCC_Private_Functions
  1563. * @{
  1564. */
  1565. /**
  1566. * @brief Update number of Flash wait states in line with MSI range and current
  1567. voltage range.
  1568. * @param msirange MSI range value from RCC_MSIRANGE_0 to RCC_MSIRANGE_11
  1569. * @retval HAL status
  1570. */
  1571. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange)
  1572. {
  1573. uint32_t vos;
  1574. uint32_t latency = FLASH_LATENCY_0; /* default value 0WS */
  1575. if(__HAL_RCC_PWR_IS_CLK_ENABLED())
  1576. {
  1577. vos = HAL_PWREx_GetVoltageRange();
  1578. }
  1579. else
  1580. {
  1581. __HAL_RCC_PWR_CLK_ENABLE();
  1582. vos = HAL_PWREx_GetVoltageRange();
  1583. __HAL_RCC_PWR_CLK_DISABLE();
  1584. }
  1585. if(vos == PWR_REGULATOR_VOLTAGE_SCALE1)
  1586. {
  1587. if(msirange > RCC_MSIRANGE_8)
  1588. {
  1589. /* MSI > 16Mhz */
  1590. if(msirange > RCC_MSIRANGE_10)
  1591. {
  1592. /* MSI 48Mhz */
  1593. latency = FLASH_LATENCY_2; /* 2WS */
  1594. }
  1595. else
  1596. {
  1597. /* MSI 24Mhz or 32Mhz */
  1598. latency = FLASH_LATENCY_1; /* 1WS */
  1599. }
  1600. }
  1601. /* else MSI <= 16Mhz default FLASH_LATENCY_0 0WS */
  1602. }
  1603. else
  1604. {
  1605. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  1606. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1607. if(msirange >= RCC_MSIRANGE_8)
  1608. {
  1609. /* MSI >= 16Mhz */
  1610. latency = FLASH_LATENCY_2; /* 2WS */
  1611. }
  1612. else
  1613. {
  1614. if(msirange == RCC_MSIRANGE_7)
  1615. {
  1616. /* MSI 8Mhz */
  1617. latency = FLASH_LATENCY_1; /* 1WS */
  1618. }
  1619. /* else MSI < 8Mhz default FLASH_LATENCY_0 0WS */
  1620. }
  1621. #else
  1622. if(msirange > RCC_MSIRANGE_8)
  1623. {
  1624. /* MSI > 16Mhz */
  1625. latency = FLASH_LATENCY_3; /* 3WS */
  1626. }
  1627. else
  1628. {
  1629. if(msirange == RCC_MSIRANGE_8)
  1630. {
  1631. /* MSI 16Mhz */
  1632. latency = FLASH_LATENCY_2; /* 2WS */
  1633. }
  1634. else if(msirange == RCC_MSIRANGE_7)
  1635. {
  1636. /* MSI 8Mhz */
  1637. latency = FLASH_LATENCY_1; /* 1WS */
  1638. }
  1639. else
  1640. {
  1641. /* else MSI < 8Mhz default FLASH_LATENCY_0 0WS */
  1642. /* nothing to do */
  1643. }
  1644. }
  1645. #endif
  1646. }
  1647. __HAL_FLASH_SET_LATENCY(latency);
  1648. /* Check that the new number of wait states is taken into account to access the Flash
  1649. memory by reading the FLASH_ACR register */
  1650. if(__HAL_FLASH_GET_LATENCY() != latency)
  1651. {
  1652. return HAL_ERROR;
  1653. }
  1654. return HAL_OK;
  1655. }
  1656. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || \
  1657. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1658. /**
  1659. * @brief Compute SYSCLK frequency based on PLL SYSCLK source.
  1660. * @retval SYSCLK frequency
  1661. */
  1662. static uint32_t RCC_GetSysClockFreqFromPLLSource(void)
  1663. {
  1664. uint32_t msirange, pllvco, pllsource, pllr, pllm, sysclockfreq; /* no init needed */
  1665. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE) * PLLN / PLLM
  1666. SYSCLK = PLL_VCO / PLLR
  1667. */
  1668. pllsource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
  1669. switch (pllsource)
  1670. {
  1671. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1672. pllvco = HSI_VALUE;
  1673. break;
  1674. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1675. pllvco = HSE_VALUE;
  1676. break;
  1677. case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1678. /* Get MSI range source */
  1679. if(READ_BIT(RCC->CR, RCC_CR_MSIRGSEL) == 0U)
  1680. { /* MSISRANGE from RCC_CSR applies */
  1681. msirange = READ_BIT(RCC->CSR, RCC_CSR_MSISRANGE) >> RCC_CSR_MSISRANGE_Pos;
  1682. }
  1683. else
  1684. { /* MSIRANGE from RCC_CR applies */
  1685. msirange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos;
  1686. }
  1687. /*MSI frequency range in HZ*/
  1688. pllvco = MSIRangeTable[msirange];
  1689. break;
  1690. default:
  1691. /* unexpected */
  1692. pllvco = 0;
  1693. break;
  1694. }
  1695. pllm = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
  1696. pllvco = (pllvco * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)) / pllm;
  1697. pllr = ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U ) * 2U;
  1698. sysclockfreq = pllvco / pllr;
  1699. return sysclockfreq;
  1700. }
  1701. #endif
  1702. /**
  1703. * @}
  1704. */
  1705. #endif /* HAL_RCC_MODULE_ENABLED */
  1706. /**
  1707. * @}
  1708. */
  1709. /**
  1710. * @}
  1711. */