stm32l4xx_hal_rcc.c 66 KB

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