Create release.yml
[betaflight.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_rcc.c
blob17c241b8d66e8543d8c4cc99829314e7c5f7bd1c
1 /**
2 ******************************************************************************
3 * @file stm32h7xx_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
11 @verbatim
12 ==============================================================================
13 ##### RCC specific features #####
14 ==============================================================================
15 [..]
16 After reset the device is running from Internal High Speed oscillator
17 (HSI 64MHz) with Flash 0 wait state,and all peripherals are off except
18 internal SRAM, Flash, JTAG and PWR
19 (+) There is no pre-scaler on High speed (AHB) and Low speed (APB) buses;
20 all peripherals mapped on these buses are running at HSI speed.
21 (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
22 (+) All GPIOs are in analogue mode , except the JTAG pins which
23 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 buses pre-scalers
31 (+) Enable the clock for the peripheral(s) to be used
32 (+) Configure the clock kernel source(s) for peripherals which clocks are not
33 derived from the System clock through :RCC_D1CCIPR,RCC_D2CCIP1R,RCC_D2CCIP2R
34 and RCC_D3CCIPR registers
36 ##### RCC Limitations #####
37 ==============================================================================
38 [..]
39 A delay between an RCC peripheral clock enable and the effective peripheral
40 enabling should be taken into account in order to manage the peripheral read/write
41 from/to registers.
42 (+) This delay depends on the peripheral mapping.
43 (+) If peripheral is mapped on AHB: the delay is 2 AHB clock cycle
44 after the clock enable bit is set on the hardware register
45 (+) If peripheral is mapped on APB: the delay is 2 APB clock cycle
46 after the clock enable bit is set on the hardware register
48 [..]
49 Implemented Workaround:
50 (+) For AHB & APB peripherals, a dummy read to the peripheral register has been
51 inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.
53 @endverbatim
54 ******************************************************************************
55 * @attention
57 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics.
58 * All rights reserved.</center></h2>
60 * This software component is licensed by ST under BSD 3-Clause license,
61 * the "License"; You may not use this file except in compliance with the
62 * License. You may obtain a copy of the License at:
63 * opensource.org/licenses/BSD-3-Clause
65 ******************************************************************************
68 /* Includes ------------------------------------------------------------------*/
69 #include "stm32h7xx_hal.h"
71 /** @addtogroup STM32H7xx_HAL_Driver
72 * @{
75 /** @defgroup RCC RCC
76 * @brief RCC HAL module driver
77 * @{
80 #ifdef HAL_RCC_MODULE_ENABLED
82 /* Private typedef -----------------------------------------------------------*/
83 /* Private define ------------------------------------------------------------*/
84 /* Private macro -------------------------------------------------------------*/
85 /** @defgroup RCC_Private_Macros RCC Private Macros
86 * @{
88 #define MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
89 #define MCO1_GPIO_PORT GPIOA
90 #define MCO1_PIN GPIO_PIN_8
92 #define MCO2_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
93 #define MCO2_GPIO_PORT GPIOC
94 #define MCO2_PIN GPIO_PIN_9
96 /**
97 * @}
99 /* Private variables ---------------------------------------------------------*/
100 /** @defgroup RCC_Private_Variables RCC Private Variables
101 * @{
105 * @}
107 /* Private function prototypes -----------------------------------------------*/
108 /* Exported functions --------------------------------------------------------*/
110 /** @defgroup RCC_Exported_Functions RCC Exported Functions
111 * @{
114 /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
115 * @brief Initialization and Configuration functions
117 @verbatim
118 ===============================================================================
119 ##### Initialization and de-initialization functions #####
120 ===============================================================================
121 [..]
122 This section provides functions allowing to configure the internal/external oscillators
123 (HSE, HSI, LSE,CSI, LSI,HSI48, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB3, AHB1
124 AHB2,AHB4,APB3, APB1L, APB1H, APB2, and APB4).
126 [..] Internal/external clock and PLL configuration
127 (#) HSI (high-speed internal), 64 MHz factory-trimmed RC used directly or through
128 the PLL as System clock source.
129 (#) CSI is a low-power RC oscillator which can be used directly as system clock, peripheral
130 clock, or PLL input.But even with frequency calibration, is less accurate than an
131 external crystal oscillator or ceramic resonator.
132 (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC
133 clock source.
135 (#) HSE (high-speed external), 4 to 48 MHz crystal oscillator used directly or
136 through the PLL as System clock source. Can be used also as RTC clock source.
138 (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
140 (#) PLL , The RCC features three independent PLLs (clocked by HSI , HSE or CSI),
141 featuring three different output clocks and able to work either in integer or Fractional mode.
142 (++) A main PLL, PLL1, which is generally used to provide clocks to the CPU
143 and to some peripherals.
144 (++) Two dedicated PLLs, PLL2 and PLL3, which are used to generate the kernel clock for peripherals.
147 (#) CSS (Clock security system), once enabled and if a HSE clock failure occurs
148 (HSE used directly or through PLL as System clock source), the System clock
149 is automatically switched to HSI and an interrupt is generated if enabled.
150 The interrupt is linked to the Cortex-M NMI (Non-Mask-able Interrupt)
151 exception vector.
153 (#) MCO1 (micro controller clock output), used to output HSI, LSE, HSE, PLL1(PLL1_Q)
154 or HSI48 clock (through a configurable pre-scaler) on PA8 pin.
156 (#) MCO2 (micro controller clock output), used to output HSE, PLL2(PLL2_P), SYSCLK,
157 LSI, CSI, or PLL1(PLL1_P) clock (through a configurable pre-scaler) on PC9 pin.
159 [..] System, AHB and APB buses clocks configuration
160 (#) Several clock sources can be used to drive the System clock (SYSCLK): CSI,HSI,
161 HSE and PLL.
162 The AHB clock (HCLK) is derived from System core clock through configurable
163 pre-scaler and used to clock the CPU, memory and peripherals mapped
164 on AHB and APB bus of the 3 Domains (D1, D2, D3)* through configurable pre-scalers
165 and used to clock the peripherals mapped on these buses. You can use
166 "HAL_RCC_GetSysClockFreq()" function to retrieve system clock frequency.
168 -@- All the peripheral clocks are derived from the System clock (SYSCLK) except those
169 with dual clock domain where kernel source clock could be selected through
170 RCC_D1CCIPR,RCC_D2CCIP1R,RCC_D2CCIP2R and RCC_D3CCIPR registers.
172 (*) : 2 Domains (CD and SRD) for stm32h7a3xx and stm32h7b3xx family lines.
173 @endverbatim
174 * @{
178 * @brief Resets the RCC clock configuration to the default reset state.
179 * @note The default reset state of the clock configuration is given below:
180 * - HSI ON and used as system clock source
181 * - HSE, PLL1, PLL2 and PLL3 OFF
182 * - AHB, APB Bus pre-scaler set to 1.
183 * - CSS, MCO1 and MCO2 OFF
184 * - All interrupts disabled
185 * @note This function doesn't modify the configuration of the
186 * - Peripheral clocks
187 * - LSI, LSE and RTC clocks
188 * @retval HAL status
190 HAL_StatusTypeDef HAL_RCC_DeInit(void)
192 uint32_t tickstart;
194 /* Increasing the CPU frequency */
195 if(FLASH_LATENCY_DEFAULT > __HAL_FLASH_GET_LATENCY())
197 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
198 __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_DEFAULT);
200 /* Check that the new number of wait states is taken into account to access the Flash
201 memory by reading the FLASH_ACR register */
202 if(__HAL_FLASH_GET_LATENCY() != FLASH_LATENCY_DEFAULT)
204 return HAL_ERROR;
210 /* Get Start Tick */
211 tickstart = HAL_GetTick();
213 /* Set HSION bit */
214 SET_BIT(RCC->CR, RCC_CR_HSION);
216 /* Wait till HSI is ready */
217 while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
219 if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
221 return HAL_TIMEOUT;
225 /* Set HSITRIM[6:0] bits to the reset value */
226 SET_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM_6);
228 /* Reset CFGR register */
229 CLEAR_REG(RCC->CFGR);
231 /* Update the SystemCoreClock and SystemD2Clock global variables */
232 SystemCoreClock = HSI_VALUE;
233 SystemD2Clock = HSI_VALUE;
235 /* Adapt Systick interrupt period */
236 if(HAL_InitTick(uwTickPrio) != HAL_OK)
238 return HAL_ERROR;
241 /* Get Start Tick */
242 tickstart = HAL_GetTick();
244 /* Wait till clock switch is ready */
245 while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != 0U)
247 if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
249 return HAL_TIMEOUT;
253 /* Get Start Tick */
254 tickstart = HAL_GetTick();
256 /* Reset CSION, CSIKERON, HSEON, HSI48ON, HSECSSON, HSIDIV bits */
257 CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSIKERON| RCC_CR_HSIDIV| RCC_CR_HSIDIVF| RCC_CR_CSION | RCC_CR_CSIKERON \
258 | RCC_CR_HSI48ON | RCC_CR_CSSHSEON);
260 /* Wait till HSE is disabled */
261 while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
263 if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
265 return HAL_TIMEOUT;
269 /* Get Start Tick */
270 tickstart = HAL_GetTick();
272 /* Clear PLLON bit */
273 CLEAR_BIT(RCC->CR, RCC_CR_PLL1ON);
275 /* Wait till PLL is disabled */
276 while (READ_BIT(RCC->CR, RCC_CR_PLL1RDY) != 0U)
278 if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
280 return HAL_TIMEOUT;
284 /* Get Start Tick */
285 tickstart = HAL_GetTick();
287 /* Reset PLL2ON bit */
288 CLEAR_BIT(RCC->CR, RCC_CR_PLL2ON);
290 /* Wait till PLL2 is disabled */
291 while (READ_BIT(RCC->CR, RCC_CR_PLL2RDY) != 0U)
293 if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
295 return HAL_TIMEOUT;
299 /* Get Start Tick */
300 tickstart = HAL_GetTick();
302 /* Reset PLL3 bit */
303 CLEAR_BIT(RCC->CR, RCC_CR_PLL3ON);
305 /* Wait till PLL3 is disabled */
306 while (READ_BIT(RCC->CR, RCC_CR_PLL3RDY) != 0U)
308 if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
310 return HAL_TIMEOUT;
314 #if defined(RCC_D1CFGR_HPRE)
315 /* Reset D1CFGR register */
316 CLEAR_REG(RCC->D1CFGR);
318 /* Reset D2CFGR register */
319 CLEAR_REG(RCC->D2CFGR);
321 /* Reset D3CFGR register */
322 CLEAR_REG(RCC->D3CFGR);
323 #else
324 /* Reset CDCFGR1 register */
325 CLEAR_REG(RCC->CDCFGR1);
327 /* Reset CDCFGR2 register */
328 CLEAR_REG(RCC->CDCFGR2);
330 /* Reset SRDCFGR register */
331 CLEAR_REG(RCC->SRDCFGR);
332 #endif
334 /* Reset PLLCKSELR register to default value */
335 RCC->PLLCKSELR= RCC_PLLCKSELR_DIVM1_5|RCC_PLLCKSELR_DIVM2_5|RCC_PLLCKSELR_DIVM3_5;
337 /* Reset PLLCFGR register to default value */
338 WRITE_REG(RCC->PLLCFGR, 0x01FF0000U);
340 /* Reset PLL1DIVR register to default value */
341 WRITE_REG(RCC->PLL1DIVR,0x01010280U);
343 /* Reset PLL1FRACR register */
344 CLEAR_REG(RCC->PLL1FRACR);
346 /* Reset PLL2DIVR register to default value */
347 WRITE_REG(RCC->PLL2DIVR,0x01010280U);
349 /* Reset PLL2FRACR register */
350 CLEAR_REG(RCC->PLL2FRACR);
352 /* Reset PLL3DIVR register to default value */
353 WRITE_REG(RCC->PLL3DIVR,0x01010280U);
355 /* Reset PLL3FRACR register */
356 CLEAR_REG(RCC->PLL3FRACR);
358 /* Reset HSEBYP bit */
359 CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
361 /* Disable all interrupts */
362 CLEAR_REG(RCC->CIER);
364 /* Clear all interrupts flags */
365 WRITE_REG(RCC->CICR,0xFFFFFFFFU);
367 /* Reset all RSR flags */
368 SET_BIT(RCC->RSR, RCC_RSR_RMVF);
370 /* Decreasing the number of wait states because of lower CPU frequency */
371 if(FLASH_LATENCY_DEFAULT < __HAL_FLASH_GET_LATENCY())
373 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
374 __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_DEFAULT);
376 /* Check that the new number of wait states is taken into account to access the Flash
377 memory by reading the FLASH_ACR register */
378 if(__HAL_FLASH_GET_LATENCY() != FLASH_LATENCY_DEFAULT)
380 return HAL_ERROR;
385 return HAL_OK;
389 * @brief Initializes the RCC Oscillators according to the specified parameters in the
390 * RCC_OscInitTypeDef.
391 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
392 * contains the configuration information for the RCC Oscillators.
393 * @note The PLL is not disabled when used as system clock.
394 * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
395 * supported by this function. User should request a transition to LSE Off
396 * first and then LSE On or LSE Bypass.
397 * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
398 * supported by this function. User should request a transition to HSE Off
399 * first and then HSE On or HSE Bypass.
400 * @retval HAL status
402 __weak HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
404 uint32_t tickstart;
405 uint32_t temp1_pllckcfg, temp2_pllckcfg;
407 /* Check Null pointer */
408 if(RCC_OscInitStruct == NULL)
410 return HAL_ERROR;
413 /* Check the parameters */
414 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
415 /*------------------------------- HSE Configuration ------------------------*/
416 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
418 /* Check the parameters */
419 assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
421 const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
422 const uint32_t temp_pllckselr = RCC->PLLCKSELR;
423 /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
424 if((temp_sysclksrc == RCC_CFGR_SWS_HSE) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_HSE)))
426 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
428 return HAL_ERROR;
431 else
433 /* Set the new HSE configuration ---------------------------------------*/
434 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
436 /* Check the HSE State */
437 if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
439 /* Get Start Tick*/
440 tickstart = HAL_GetTick();
442 /* Wait till HSE is ready */
443 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U)
445 if((uint32_t) (HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
447 return HAL_TIMEOUT;
451 else
453 /* Get Start Tick*/
454 tickstart = HAL_GetTick();
456 /* Wait till HSE is disabled */
457 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U)
459 if((uint32_t) (HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
461 return HAL_TIMEOUT;
467 /*----------------------------- HSI Configuration --------------------------*/
468 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
470 /* Check the parameters */
471 assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
472 assert_param(IS_RCC_HSICALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
474 /* When the HSI is used as system clock it will not be disabled */
475 const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
476 const uint32_t temp_pllckselr = RCC->PLLCKSELR;
477 if((temp_sysclksrc == RCC_CFGR_SWS_HSI) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_HSI)))
479 /* When HSI is used as system clock it will not be disabled */
480 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
482 return HAL_ERROR;
484 /* Otherwise, just the calibration is allowed */
485 else
487 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
488 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
492 else
494 /* Check the HSI State */
495 if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
497 /* Enable the Internal High Speed oscillator (HSI, HSIDIV2,HSIDIV4, or HSIDIV8) */
498 __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIState);
500 /* Get Start Tick*/
501 tickstart = HAL_GetTick();
503 /* Wait till HSI is ready */
504 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
506 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
508 return HAL_TIMEOUT;
512 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
513 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
515 else
517 /* Disable the Internal High Speed oscillator (HSI). */
518 __HAL_RCC_HSI_DISABLE();
520 /* Get Start Tick*/
521 tickstart = HAL_GetTick();
523 /* Wait till HSI is disabled */
524 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U)
526 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
528 return HAL_TIMEOUT;
534 /*----------------------------- CSI Configuration --------------------------*/
535 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_CSI) == RCC_OSCILLATORTYPE_CSI)
537 /* Check the parameters */
538 assert_param(IS_RCC_CSI(RCC_OscInitStruct->CSIState));
539 assert_param(IS_RCC_CSICALIBRATION_VALUE(RCC_OscInitStruct->CSICalibrationValue));
541 /* When the CSI is used as system clock it will not disabled */
542 const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
543 const uint32_t temp_pllckselr = RCC->PLLCKSELR;
544 if((temp_sysclksrc == RCC_CFGR_SWS_CSI) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_CSI)))
546 /* When CSI is used as system clock it will not disabled */
547 if((__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) != 0U) && (RCC_OscInitStruct->CSIState != RCC_CSI_ON))
549 return HAL_ERROR;
551 /* Otherwise, just the calibration is allowed */
552 else
554 /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
555 __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->CSICalibrationValue);
558 else
560 /* Check the CSI State */
561 if((RCC_OscInitStruct->CSIState)!= RCC_CSI_OFF)
563 /* Enable the Internal High Speed oscillator (CSI). */
564 __HAL_RCC_CSI_ENABLE();
566 /* Get Start Tick*/
567 tickstart = HAL_GetTick();
569 /* Wait till CSI is ready */
570 while(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) == 0U)
572 if((HAL_GetTick() - tickstart ) > CSI_TIMEOUT_VALUE)
574 return HAL_TIMEOUT;
578 /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
579 __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->CSICalibrationValue);
581 else
583 /* Disable the Internal High Speed oscillator (CSI). */
584 __HAL_RCC_CSI_DISABLE();
586 /* Get Start Tick*/
587 tickstart = HAL_GetTick();
589 /* Wait till CSI is disabled */
590 while(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) != 0U)
592 if((HAL_GetTick() - tickstart ) > CSI_TIMEOUT_VALUE)
594 return HAL_TIMEOUT;
600 /*------------------------------ LSI Configuration -------------------------*/
601 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
603 /* Check the parameters */
604 assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
606 /* Check the LSI State */
607 if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
609 /* Enable the Internal Low Speed oscillator (LSI). */
610 __HAL_RCC_LSI_ENABLE();
612 /* Get Start Tick*/
613 tickstart = HAL_GetTick();
615 /* Wait till LSI is ready */
616 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == 0U)
618 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
620 return HAL_TIMEOUT;
624 else
626 /* Disable the Internal Low Speed oscillator (LSI). */
627 __HAL_RCC_LSI_DISABLE();
629 /* Get Start Tick*/
630 tickstart = HAL_GetTick();
632 /* Wait till LSI is ready */
633 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != 0U)
635 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
637 return HAL_TIMEOUT;
643 /*------------------------------ HSI48 Configuration -------------------------*/
644 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
646 /* Check the parameters */
647 assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
649 /* Check the HSI48 State */
650 if((RCC_OscInitStruct->HSI48State)!= RCC_HSI48_OFF)
652 /* Enable the Internal Low Speed oscillator (HSI48). */
653 __HAL_RCC_HSI48_ENABLE();
655 /* Get time-out */
656 tickstart = HAL_GetTick();
658 /* Wait till HSI48 is ready */
659 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) == 0U)
661 if((HAL_GetTick() - tickstart ) > HSI48_TIMEOUT_VALUE)
663 return HAL_TIMEOUT;
667 else
669 /* Disable the Internal Low Speed oscillator (HSI48). */
670 __HAL_RCC_HSI48_DISABLE();
672 /* Get time-out */
673 tickstart = HAL_GetTick();
675 /* Wait till HSI48 is ready */
676 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) != 0U)
678 if((HAL_GetTick() - tickstart ) > HSI48_TIMEOUT_VALUE)
680 return HAL_TIMEOUT;
685 /*------------------------------ LSE Configuration -------------------------*/
686 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
688 /* Check the parameters */
689 assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
691 /* Enable write access to Backup domain */
692 PWR->CR1 |= PWR_CR1_DBP;
694 /* Wait for Backup domain Write protection disable */
695 tickstart = HAL_GetTick();
697 while((PWR->CR1 & PWR_CR1_DBP) == 0U)
699 if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
701 return HAL_TIMEOUT;
705 /* Set the new LSE configuration -----------------------------------------*/
706 __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
707 /* Check the LSE State */
708 if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
710 /* Get Start Tick*/
711 tickstart = HAL_GetTick();
713 /* Wait till LSE is ready */
714 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == 0U)
716 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
718 return HAL_TIMEOUT;
722 else
724 /* Get Start Tick*/
725 tickstart = HAL_GetTick();
727 /* Wait till LSE is disabled */
728 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != 0U)
730 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
732 return HAL_TIMEOUT;
737 /*-------------------------------- PLL Configuration -----------------------*/
738 /* Check the parameters */
739 assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
740 if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
742 /* Check if the PLL is used as system clock or not */
743 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL1)
745 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
747 /* Check the parameters */
748 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
749 assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
750 assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
751 assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
752 assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
753 assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
754 assert_param(IS_RCC_PLLFRACN_VALUE(RCC_OscInitStruct->PLL.PLLFRACN));
756 /* Disable the main PLL. */
757 __HAL_RCC_PLL_DISABLE();
759 /* Get Start Tick*/
760 tickstart = HAL_GetTick();
762 /* Wait till PLL is disabled */
763 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U)
765 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
767 return HAL_TIMEOUT;
771 /* Configure the main PLL clock source, multiplication and division factors. */
772 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
773 RCC_OscInitStruct->PLL.PLLM,
774 RCC_OscInitStruct->PLL.PLLN,
775 RCC_OscInitStruct->PLL.PLLP,
776 RCC_OscInitStruct->PLL.PLLQ,
777 RCC_OscInitStruct->PLL.PLLR);
779 /* Disable PLLFRACN . */
780 __HAL_RCC_PLLFRACN_DISABLE();
782 /* Configure PLL PLL1FRACN */
783 __HAL_RCC_PLLFRACN_CONFIG(RCC_OscInitStruct->PLL.PLLFRACN);
785 /* Select PLL1 input reference frequency range: VCI */
786 __HAL_RCC_PLL_VCIRANGE(RCC_OscInitStruct->PLL.PLLRGE) ;
788 /* Select PLL1 output frequency range : VCO */
789 __HAL_RCC_PLL_VCORANGE(RCC_OscInitStruct->PLL.PLLVCOSEL) ;
791 /* Enable PLL System Clock output. */
792 __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVP);
794 /* Enable PLL1Q Clock output. */
795 __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVQ);
797 /* Enable PLL1R Clock output. */
798 __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVR);
800 /* Enable PLL1FRACN . */
801 __HAL_RCC_PLLFRACN_ENABLE();
803 /* Enable the main PLL. */
804 __HAL_RCC_PLL_ENABLE();
806 /* Get Start Tick*/
807 tickstart = HAL_GetTick();
809 /* Wait till PLL is ready */
810 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U)
812 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
814 return HAL_TIMEOUT;
818 else
820 /* Disable the main PLL. */
821 __HAL_RCC_PLL_DISABLE();
823 /* Get Start Tick*/
824 tickstart = HAL_GetTick();
826 /* Wait till PLL is disabled */
827 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U)
829 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
831 return HAL_TIMEOUT;
836 else
838 /* Do not return HAL_ERROR if request repeats the current configuration */
839 temp1_pllckcfg = RCC->PLLCKSELR;
840 temp2_pllckcfg = RCC->PLL1DIVR;
841 if(((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF) ||
842 (READ_BIT(temp1_pllckcfg, RCC_PLLCKSELR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
843 ((READ_BIT(temp1_pllckcfg, RCC_PLLCKSELR_DIVM1) >> RCC_PLLCKSELR_DIVM1_Pos) != RCC_OscInitStruct->PLL.PLLM) ||
844 (READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_N1) != (RCC_OscInitStruct->PLL.PLLN - 1U)) ||
845 ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_P1) >> RCC_PLL1DIVR_P1_Pos) != (RCC_OscInitStruct->PLL.PLLP - 1U)) ||
846 ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_Q1) >> RCC_PLL1DIVR_Q1_Pos) != (RCC_OscInitStruct->PLL.PLLQ - 1U)) ||
847 ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_R1) >> RCC_PLL1DIVR_R1_Pos) != (RCC_OscInitStruct->PLL.PLLR - 1U)))
849 return HAL_ERROR;
853 return HAL_OK;
857 * @brief Initializes the CPU, AHB and APB buses clocks according to the specified
858 * parameters in the RCC_ClkInitStruct.
859 * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
860 * contains the configuration information for the RCC peripheral.
861 * @param FLatency: FLASH Latency, this parameter depend on device selected
863 * @note The SystemCoreClock CMSIS variable is used to store System Core Clock Frequency
864 * and updated by HAL_InitTick() function called within this function
866 * @note The HSI is used (enabled by hardware) as system clock source after
867 * start-up from Reset, wake-up from STOP and STANDBY mode, or in case
868 * of failure of the HSE used directly or indirectly as system clock
869 * (if the Clock Security System CSS is enabled).
871 * @note A switch from one clock source to another occurs only if the target
872 * clock source is ready (clock stable after start-up delay or PLL locked).
873 * If a clock source which is not yet ready is selected, the switch will
874 * occur when the clock source will be ready.
875 * You can use HAL_RCC_GetClockConfig() function to know which clock is
876 * currently used as system clock source.
877 * @note Depending on the device voltage range, the software has to set correctly
878 * D1CPRE[3:0] bits to ensure that Domain1 core clock not exceed the maximum allowed frequency
879 * (for more details refer to section above "Initialization/de-initialization functions")
880 * @retval None
882 HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
884 HAL_StatusTypeDef halstatus;
885 uint32_t tickstart;
886 uint32_t common_system_clock;
888 /* Check Null pointer */
889 if(RCC_ClkInitStruct == NULL)
891 return HAL_ERROR;
894 /* Check the parameters */
895 assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
896 assert_param(IS_FLASH_LATENCY(FLatency));
898 /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
899 must be correctly programmed according to the frequency of the CPU clock
900 (HCLK) and the supply voltage of the device. */
902 /* Increasing the CPU frequency */
903 if(FLatency > __HAL_FLASH_GET_LATENCY())
905 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
906 __HAL_FLASH_SET_LATENCY(FLatency);
908 /* Check that the new number of wait states is taken into account to access the Flash
909 memory by reading the FLASH_ACR register */
910 if(__HAL_FLASH_GET_LATENCY() != FLatency)
912 return HAL_ERROR;
917 /* Increasing the BUS frequency divider */
918 /*-------------------------- D1PCLK1/CDPCLK1 Configuration ---------------------------*/
919 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D1PCLK1) == RCC_CLOCKTYPE_D1PCLK1)
921 #if defined (RCC_D1CFGR_D1PPRE)
922 if((RCC_ClkInitStruct->APB3CLKDivider) > (RCC->D1CFGR & RCC_D1CFGR_D1PPRE))
924 assert_param(IS_RCC_D1PCLK1(RCC_ClkInitStruct->APB3CLKDivider));
925 MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1PPRE, RCC_ClkInitStruct->APB3CLKDivider);
927 #else
928 if((RCC_ClkInitStruct->APB3CLKDivider) > (RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE))
930 assert_param(IS_RCC_CDPCLK1(RCC_ClkInitStruct->APB3CLKDivider));
931 MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDPPRE, RCC_ClkInitStruct->APB3CLKDivider);
933 #endif
936 /*-------------------------- PCLK1 Configuration ---------------------------*/
937 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
939 #if defined (RCC_D2CFGR_D2PPRE1)
940 if((RCC_ClkInitStruct->APB1CLKDivider) > (RCC->D2CFGR & RCC_D2CFGR_D2PPRE1))
942 assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
943 MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
945 #else
946 if((RCC_ClkInitStruct->APB1CLKDivider) > (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1))
948 assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
949 MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
951 #endif
953 /*-------------------------- PCLK2 Configuration ---------------------------*/
954 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
956 #if defined(RCC_D2CFGR_D2PPRE2)
957 if((RCC_ClkInitStruct->APB2CLKDivider) > (RCC->D2CFGR & RCC_D2CFGR_D2PPRE2))
959 assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
960 MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
962 #else
963 if((RCC_ClkInitStruct->APB2CLKDivider) > (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2))
965 assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
966 MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
968 #endif
971 /*-------------------------- D3PCLK1 Configuration ---------------------------*/
972 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D3PCLK1) == RCC_CLOCKTYPE_D3PCLK1)
974 #if defined(RCC_D3CFGR_D3PPRE)
975 if((RCC_ClkInitStruct->APB4CLKDivider) > (RCC->D3CFGR & RCC_D3CFGR_D3PPRE))
977 assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
978 MODIFY_REG(RCC->D3CFGR, RCC_D3CFGR_D3PPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
980 #else
981 if((RCC_ClkInitStruct->APB4CLKDivider) > (RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE))
983 assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
984 MODIFY_REG(RCC->SRDCFGR, RCC_SRDCFGR_SRDPPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
986 #endif
989 /*-------------------------- HCLK Configuration --------------------------*/
990 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
992 #if defined (RCC_D1CFGR_HPRE)
993 if((RCC_ClkInitStruct->AHBCLKDivider) > (RCC->D1CFGR & RCC_D1CFGR_HPRE))
995 /* Set the new HCLK clock divider */
996 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
997 MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
999 #else
1000 if((RCC_ClkInitStruct->AHBCLKDivider) > (RCC->CDCFGR1 & RCC_CDCFGR1_HPRE))
1002 /* Set the new HCLK clock divider */
1003 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
1004 MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
1006 #endif
1009 /*------------------------- SYSCLK Configuration -------------------------*/
1010 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
1012 assert_param(IS_RCC_SYSCLK(RCC_ClkInitStruct->SYSCLKDivider));
1013 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
1014 #if defined(RCC_D1CFGR_D1CPRE)
1015 MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1CPRE, RCC_ClkInitStruct->SYSCLKDivider);
1016 #else
1017 MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDCPRE, RCC_ClkInitStruct->SYSCLKDivider);
1018 #endif
1019 /* HSE is selected as System Clock Source */
1020 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
1022 /* Check the HSE ready flag */
1023 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U)
1025 return HAL_ERROR;
1028 /* PLL is selected as System Clock Source */
1029 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
1031 /* Check the PLL ready flag */
1032 if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U)
1034 return HAL_ERROR;
1037 /* CSI is selected as System Clock Source */
1038 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_CSI)
1040 /* Check the PLL ready flag */
1041 if(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) == 0U)
1043 return HAL_ERROR;
1046 /* HSI is selected as System Clock Source */
1047 else
1049 /* Check the HSI ready flag */
1050 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
1052 return HAL_ERROR;
1055 MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
1057 /* Get Start Tick*/
1058 tickstart = HAL_GetTick();
1060 while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
1062 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
1064 return HAL_TIMEOUT;
1070 /* Decreasing the BUS frequency divider */
1071 /*-------------------------- HCLK Configuration --------------------------*/
1072 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
1074 #if defined(RCC_D1CFGR_HPRE)
1075 if((RCC_ClkInitStruct->AHBCLKDivider) < (RCC->D1CFGR & RCC_D1CFGR_HPRE))
1077 /* Set the new HCLK clock divider */
1078 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
1079 MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
1081 #else
1082 if((RCC_ClkInitStruct->AHBCLKDivider) < (RCC->CDCFGR1 & RCC_CDCFGR1_HPRE))
1084 /* Set the new HCLK clock divider */
1085 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
1086 MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
1088 #endif
1091 /* Decreasing the number of wait states because of lower CPU frequency */
1092 if(FLatency < __HAL_FLASH_GET_LATENCY())
1094 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
1095 __HAL_FLASH_SET_LATENCY(FLatency);
1097 /* Check that the new number of wait states is taken into account to access the Flash
1098 memory by reading the FLASH_ACR register */
1099 if(__HAL_FLASH_GET_LATENCY() != FLatency)
1101 return HAL_ERROR;
1105 /*-------------------------- D1PCLK1/CDPCLK Configuration ---------------------------*/
1106 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D1PCLK1) == RCC_CLOCKTYPE_D1PCLK1)
1108 #if defined(RCC_D1CFGR_D1PPRE)
1109 if((RCC_ClkInitStruct->APB3CLKDivider) < (RCC->D1CFGR & RCC_D1CFGR_D1PPRE))
1111 assert_param(IS_RCC_D1PCLK1(RCC_ClkInitStruct->APB3CLKDivider));
1112 MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1PPRE, RCC_ClkInitStruct->APB3CLKDivider);
1114 #else
1115 if((RCC_ClkInitStruct->APB3CLKDivider) < (RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE))
1117 assert_param(IS_RCC_CDPCLK1(RCC_ClkInitStruct->APB3CLKDivider));
1118 MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDPPRE, RCC_ClkInitStruct->APB3CLKDivider);
1120 #endif
1123 /*-------------------------- PCLK1 Configuration ---------------------------*/
1124 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
1126 #if defined(RCC_D2CFGR_D2PPRE1)
1127 if((RCC_ClkInitStruct->APB1CLKDivider) < (RCC->D2CFGR & RCC_D2CFGR_D2PPRE1))
1129 assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
1130 MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
1132 #else
1133 if((RCC_ClkInitStruct->APB1CLKDivider) < (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1))
1135 assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
1136 MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
1138 #endif
1141 /*-------------------------- PCLK2 Configuration ---------------------------*/
1142 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
1144 #if defined (RCC_D2CFGR_D2PPRE2)
1145 if((RCC_ClkInitStruct->APB2CLKDivider) < (RCC->D2CFGR & RCC_D2CFGR_D2PPRE2))
1147 assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
1148 MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
1150 #else
1151 if((RCC_ClkInitStruct->APB2CLKDivider) < (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2))
1153 assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
1154 MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
1156 #endif
1159 /*-------------------------- D3PCLK1/SRDPCLK1 Configuration ---------------------------*/
1160 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D3PCLK1) == RCC_CLOCKTYPE_D3PCLK1)
1162 #if defined(RCC_D3CFGR_D3PPRE)
1163 if((RCC_ClkInitStruct->APB4CLKDivider) < (RCC->D3CFGR & RCC_D3CFGR_D3PPRE))
1165 assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
1166 MODIFY_REG(RCC->D3CFGR, RCC_D3CFGR_D3PPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
1168 #else
1169 if((RCC_ClkInitStruct->APB4CLKDivider) < (RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE))
1171 assert_param(IS_RCC_SRDPCLK1(RCC_ClkInitStruct->APB4CLKDivider));
1172 MODIFY_REG(RCC->SRDCFGR, RCC_SRDCFGR_SRDPPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
1174 #endif
1177 /* Update the SystemCoreClock global variable */
1178 #if defined(RCC_D1CFGR_D1CPRE)
1179 common_system_clock = HAL_RCC_GetSysClockFreq() >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> RCC_D1CFGR_D1CPRE_Pos]) & 0x1FU);
1180 #else
1181 common_system_clock = HAL_RCC_GetSysClockFreq() >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE)>> RCC_CDCFGR1_CDCPRE_Pos]) & 0x1FU);
1182 #endif
1184 #if defined(RCC_D1CFGR_HPRE)
1185 SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
1186 #else
1187 SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
1188 #endif
1190 #if defined(DUAL_CORE) && defined(CORE_CM4)
1191 SystemCoreClock = SystemD2Clock;
1192 #else
1193 SystemCoreClock = common_system_clock;
1194 #endif /* DUAL_CORE && CORE_CM4 */
1196 /* Configure the source of time base considering new system clocks settings*/
1197 halstatus = HAL_InitTick (uwTickPrio);
1199 return halstatus;
1203 * @}
1206 /** @defgroup RCC_Group2 Peripheral Control functions
1207 * @brief RCC clocks control functions
1209 @verbatim
1210 ===============================================================================
1211 ##### Peripheral Control functions #####
1212 ===============================================================================
1213 [..]
1214 This subsection provides a set of functions allowing to control the RCC Clocks
1215 frequencies.
1217 @endverbatim
1218 * @{
1222 * @brief Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
1223 * @note PA8/PC9 should be configured in alternate function mode.
1224 * @param RCC_MCOx: specifies the output direction for the clock source.
1225 * This parameter can be one of the following values:
1226 * @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8).
1227 * @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9).
1228 * @param RCC_MCOSource: specifies the clock source to output.
1229 * This parameter can be one of the following values:
1230 * @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source
1231 * @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source
1232 * @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source
1233 * @arg RCC_MCO1SOURCE_PLL1QCLK: PLL1Q clock selected as MCO1 source
1234 * @arg RCC_MCO1SOURCE_HSI48: HSI48 (48MHZ) selected as MCO1 source
1235 * @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source
1236 * @arg RCC_MCO2SOURCE_PLL2PCLK: PLL2P clock selected as MCO2 source
1237 * @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source
1238 * @arg RCC_MCO2SOURCE_PLLCLK: PLL1P clock selected as MCO2 source
1239 * @arg RCC_MCO2SOURCE_CSICLK: CSI clock selected as MCO2 source
1240 * @arg RCC_MCO2SOURCE_LSICLK: LSI clock selected as MCO2 source
1241 * @param RCC_MCODiv: specifies the MCOx pre-scaler.
1242 * This parameter can be one of the following values:
1243 * @arg RCC_MCODIV_1 up to RCC_MCODIV_15 : divider applied to MCOx clock
1244 * @retval None
1246 void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
1248 GPIO_InitTypeDef GPIO_InitStruct;
1249 /* Check the parameters */
1250 assert_param(IS_RCC_MCO(RCC_MCOx));
1251 assert_param(IS_RCC_MCODIV(RCC_MCODiv));
1252 /* RCC_MCO1 */
1253 if(RCC_MCOx == RCC_MCO1)
1255 assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
1257 /* MCO1 Clock Enable */
1258 MCO1_CLK_ENABLE();
1260 /* Configure the MCO1 pin in alternate function mode */
1261 GPIO_InitStruct.Pin = MCO1_PIN;
1262 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
1263 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
1264 GPIO_InitStruct.Pull = GPIO_NOPULL;
1265 GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
1266 HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
1268 /* Mask MCO1 and MCO1PRE[3:0] bits then Select MCO1 clock source and pre-scaler */
1269 MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));
1271 else
1273 assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
1275 /* MCO2 Clock Enable */
1276 MCO2_CLK_ENABLE();
1278 /* Configure the MCO2 pin in alternate function mode */
1279 GPIO_InitStruct.Pin = MCO2_PIN;
1280 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
1281 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
1282 GPIO_InitStruct.Pull = GPIO_NOPULL;
1283 GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
1284 HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
1286 /* Mask MCO2 and MCO2PRE[3:0] bits then Select MCO2 clock source and pre-scaler */
1287 MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 7U)));
1292 * @brief Enables the Clock Security System.
1293 * @note If a failure is detected on the HSE oscillator clock, this oscillator
1294 * is automatically disabled and an interrupt is generated to inform the
1295 * software about the failure (Clock Security System Interrupt, CSSI),
1296 * allowing the MCU to perform rescue operations. The CSSI is linked to
1297 * the Cortex-M NMI (Non-Mask-able Interrupt) exception vector.
1298 * @retval None
1300 void HAL_RCC_EnableCSS(void)
1302 SET_BIT(RCC->CR, RCC_CR_CSSHSEON) ;
1306 * @brief Disables the Clock Security System.
1307 * @retval None
1309 void HAL_RCC_DisableCSS(void)
1311 CLEAR_BIT(RCC->CR, RCC_CR_CSSHSEON);
1315 * @brief Returns the SYSCLK frequency
1317 * @note The system frequency computed by this function is not the real
1318 * frequency in the chip. It is calculated based on the predefined
1319 * constant and the selected clock source:
1320 * @note If SYSCLK source is CSI, function returns values based on CSI_VALUE(*)
1321 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
1322 * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
1323 * @note If SYSCLK source is PLL, function returns values based on CSI_VALUE(*),
1324 * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
1325 * @note (*) CSI_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
1326 * 4 MHz) but the real value may vary depending on the variations
1327 * in voltage and temperature.
1328 * @note (**) HSI_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
1329 * 64 MHz) but the real value may vary depending on the variations
1330 * in voltage and temperature.
1331 * @note (***) HSE_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
1332 * 25 MHz), user has to ensure that HSE_VALUE is same as the real
1333 * frequency of the crystal used. Otherwise, this function may
1334 * have wrong result.
1336 * @note The result of this function could be not correct when using fractional
1337 * value for HSE crystal.
1339 * @note This function can be used by the user application to compute the
1340 * baud rate for the communication peripherals or configure other parameters.
1342 * @note Each time SYSCLK changes, this function must be called to update the
1343 * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
1346 * @retval SYSCLK frequency
1348 uint32_t HAL_RCC_GetSysClockFreq(void)
1350 uint32_t pllp, pllsource, pllm, pllfracen, hsivalue;
1351 float_t fracn1, pllvco;
1352 uint32_t sysclockfreq;
1354 /* Get SYSCLK source -------------------------------------------------------*/
1356 switch (RCC->CFGR & RCC_CFGR_SWS)
1358 case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
1360 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
1362 sysclockfreq = (uint32_t) (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER()>> 3));
1364 else
1366 sysclockfreq = (uint32_t) HSI_VALUE;
1369 break;
1371 case RCC_CFGR_SWS_CSI: /* CSI used as system clock source */
1372 sysclockfreq = CSI_VALUE;
1373 break;
1375 case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
1376 sysclockfreq = HSE_VALUE;
1377 break;
1379 case RCC_CFGR_SWS_PLL1: /* PLL1 used as system clock source */
1381 /* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
1382 SYSCLK = PLL_VCO / PLLR
1384 pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
1385 pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> 4) ;
1386 pllfracen = ((RCC-> PLLCFGR & RCC_PLLCFGR_PLL1FRACEN)>>RCC_PLLCFGR_PLL1FRACEN_Pos);
1387 fracn1 = (float_t)(uint32_t)(pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1)>> 3));
1389 if (pllm != 0U)
1391 switch (pllsource)
1393 case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
1395 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
1397 hsivalue= (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER()>> 3));
1398 pllvco = ( (float_t)hsivalue / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
1400 else
1402 pllvco = ((float_t)HSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
1404 break;
1406 case RCC_PLLSOURCE_CSI: /* CSI used as PLL clock source */
1407 pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
1408 break;
1410 case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
1411 pllvco = ((float_t)HSE_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
1412 break;
1414 default:
1415 pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
1416 break;
1418 pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >>9) + 1U ) ;
1419 sysclockfreq = (uint32_t)(float_t)(pllvco/(float_t)pllp);
1421 else
1423 sysclockfreq = 0U;
1425 break;
1427 default:
1428 sysclockfreq = CSI_VALUE;
1429 break;
1432 return sysclockfreq;
1437 * @brief Returns the HCLK frequency
1438 * @note Each time HCLK changes, this function must be called to update the
1439 * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
1441 * @note The SystemD2Clock CMSIS variable is used to store System domain2 Clock Frequency
1442 * and updated within this function
1443 * @retval HCLK frequency
1445 uint32_t HAL_RCC_GetHCLKFreq(void)
1447 uint32_t common_system_clock;
1449 #if defined(RCC_D1CFGR_D1CPRE)
1450 common_system_clock = HAL_RCC_GetSysClockFreq() >> (D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> RCC_D1CFGR_D1CPRE_Pos] & 0x1FU);
1451 #else
1452 common_system_clock = HAL_RCC_GetSysClockFreq() >> (D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE)>> RCC_CDCFGR1_CDCPRE_Pos] & 0x1FU);
1453 #endif
1455 #if defined(RCC_D1CFGR_HPRE)
1456 SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
1457 #else
1458 SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
1459 #endif
1461 #if defined(DUAL_CORE) && defined(CORE_CM4)
1462 SystemCoreClock = SystemD2Clock;
1463 #else
1464 SystemCoreClock = common_system_clock;
1465 #endif /* DUAL_CORE && CORE_CM4 */
1467 return SystemD2Clock;
1472 * @brief Returns the PCLK1 frequency
1473 * @note Each time PCLK1 changes, this function must be called to update the
1474 * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
1475 * @retval PCLK1 frequency
1477 uint32_t HAL_RCC_GetPCLK1Freq(void)
1479 #if defined (RCC_D2CFGR_D2PPRE1)
1480 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
1481 return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->D2CFGR & RCC_D2CFGR_D2PPRE1)>> RCC_D2CFGR_D2PPRE1_Pos]) & 0x1FU));
1482 #else
1483 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
1484 return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1)>> RCC_CDCFGR2_CDPPRE1_Pos]) & 0x1FU));
1485 #endif
1490 * @brief Returns the PCLK2 frequency
1491 * @note Each time PCLK2 changes, this function must be called to update the
1492 * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
1493 * @retval PCLK1 frequency
1495 uint32_t HAL_RCC_GetPCLK2Freq(void)
1497 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
1498 #if defined(RCC_D2CFGR_D2PPRE2)
1499 return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->D2CFGR & RCC_D2CFGR_D2PPRE2)>> RCC_D2CFGR_D2PPRE2_Pos]) & 0x1FU));
1500 #else
1501 return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2)>> RCC_CDCFGR2_CDPPRE2_Pos]) & 0x1FU));
1502 #endif
1506 * @brief Configures the RCC_OscInitStruct according to the internal
1507 * RCC configuration registers.
1508 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
1509 * will be configured.
1510 * @retval None
1512 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
1514 /* Set all possible values for the Oscillator type parameter ---------------*/
1515 RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_CSI | \
1516 RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI| RCC_OSCILLATORTYPE_HSI48;
1518 /* Get the HSE configuration -----------------------------------------------*/
1519 #if defined(RCC_CR_HSEEXT)
1520 if((RCC->CR &(RCC_CR_HSEBYP | RCC_CR_HSEEXT)) == RCC_CR_HSEBYP)
1522 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
1524 else if((RCC->CR &(RCC_CR_HSEBYP | RCC_CR_HSEEXT)) == (RCC_CR_HSEBYP | RCC_CR_HSEEXT))
1526 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS_DIGITAL;
1528 else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
1530 RCC_OscInitStruct->HSEState = RCC_HSE_ON;
1532 else
1534 RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
1536 #else
1537 if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
1539 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
1541 else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
1543 RCC_OscInitStruct->HSEState = RCC_HSE_ON;
1545 else
1547 RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
1549 #endif /* RCC_CR_HSEEXT */
1551 /* Get the CSI configuration -----------------------------------------------*/
1552 if((RCC->CR &RCC_CR_CSION) == RCC_CR_CSION)
1554 RCC_OscInitStruct->CSIState = RCC_CSI_ON;
1556 else
1558 RCC_OscInitStruct->CSIState = RCC_CSI_OFF;
1561 #if defined(RCC_VER_X)
1562 if(HAL_GetREVID() <= REV_ID_Y)
1564 RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, HAL_RCC_REV_Y_CSITRIM_Msk) >> HAL_RCC_REV_Y_CSITRIM_Pos);
1566 else
1568 RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->CSICFGR, RCC_CSICFGR_CSITRIM) >> RCC_CSICFGR_CSITRIM_Pos);
1570 #else
1571 RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->CSICFGR, RCC_CSICFGR_CSITRIM) >> RCC_CSICFGR_CSITRIM_Pos);
1572 #endif /*RCC_VER_X*/
1574 /* Get the HSI configuration -----------------------------------------------*/
1575 if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
1577 RCC_OscInitStruct->HSIState = RCC_HSI_ON;
1579 else
1581 RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
1584 #if defined(RCC_VER_X)
1585 if(HAL_GetREVID() <= REV_ID_Y)
1587 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, HAL_RCC_REV_Y_HSITRIM_Msk) >> HAL_RCC_REV_Y_HSITRIM_Pos);
1589 else
1591 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM) >> RCC_HSICFGR_HSITRIM_Pos);
1593 #else
1594 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM) >> RCC_HSICFGR_HSITRIM_Pos);
1595 #endif /*RCC_VER_X*/
1597 /* Get the LSE configuration -----------------------------------------------*/
1598 #if defined(RCC_BDCR_LSEEXT)
1599 if((RCC->BDCR &(RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT)) == RCC_BDCR_LSEBYP)
1601 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
1603 else if((RCC->BDCR &(RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT)) == (RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT))
1605 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS_DIGITAL;
1607 else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
1609 RCC_OscInitStruct->LSEState = RCC_LSE_ON;
1611 else
1613 RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
1615 #else
1616 if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
1618 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
1620 else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
1622 RCC_OscInitStruct->LSEState = RCC_LSE_ON;
1624 else
1626 RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
1628 #endif /* RCC_BDCR_LSEEXT */
1630 /* Get the LSI configuration -----------------------------------------------*/
1631 if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
1633 RCC_OscInitStruct->LSIState = RCC_LSI_ON;
1635 else
1637 RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
1640 /* Get the HSI48 configuration ---------------------------------------------*/
1641 if((RCC->CR & RCC_CR_HSI48ON) == RCC_CR_HSI48ON)
1643 RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
1645 else
1647 RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
1650 /* Get the PLL configuration -----------------------------------------------*/
1651 if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
1653 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
1655 else
1657 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
1659 RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
1660 RCC_OscInitStruct->PLL.PLLM = (uint32_t)((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> RCC_PLLCKSELR_DIVM1_Pos);
1661 RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) >> RCC_PLL1DIVR_N1_Pos)+ 1U;
1662 RCC_OscInitStruct->PLL.PLLR = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_R1) >> RCC_PLL1DIVR_R1_Pos)+ 1U;
1663 RCC_OscInitStruct->PLL.PLLP = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >> RCC_PLL1DIVR_P1_Pos)+ 1U;
1664 RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_Q1) >> RCC_PLL1DIVR_Q1_Pos)+ 1U;
1665 RCC_OscInitStruct->PLL.PLLRGE = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLL1RGE));
1666 RCC_OscInitStruct->PLL.PLLVCOSEL = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLL1VCOSEL) >> RCC_PLLCFGR_PLL1VCOSEL_Pos);
1667 RCC_OscInitStruct->PLL.PLLFRACN = (uint32_t)(((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1) >> RCC_PLL1FRACR_FRACN1_Pos));
1671 * @brief Configures the RCC_ClkInitStruct according to the internal
1672 * RCC configuration registers.
1673 * @param RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that
1674 * will be configured.
1675 * @param pFLatency: Pointer on the Flash Latency.
1676 * @retval None
1678 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
1680 /* Set all possible values for the Clock type parameter --------------------*/
1681 RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 |
1682 RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1 ;
1684 /* Get the SYSCLK configuration --------------------------------------------*/
1685 RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
1687 #if defined(RCC_D1CFGR_D1CPRE)
1688 /* Get the SYSCLK configuration ----------------------------------------------*/
1689 RCC_ClkInitStruct->SYSCLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_D1CPRE);
1691 /* Get the D1HCLK configuration ----------------------------------------------*/
1692 RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_HPRE);
1694 /* Get the APB3 configuration ----------------------------------------------*/
1695 RCC_ClkInitStruct->APB3CLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_D1PPRE);
1697 /* Get the APB1 configuration ----------------------------------------------*/
1698 RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->D2CFGR & RCC_D2CFGR_D2PPRE1);
1700 /* Get the APB2 configuration ----------------------------------------------*/
1701 RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)(RCC->D2CFGR & RCC_D2CFGR_D2PPRE2);
1703 /* Get the APB4 configuration ----------------------------------------------*/
1704 RCC_ClkInitStruct->APB4CLKDivider = (uint32_t)(RCC->D3CFGR & RCC_D3CFGR_D3PPRE);
1705 #else
1706 /* Get the SYSCLK configuration ----------------------------------------------*/
1707 RCC_ClkInitStruct->SYSCLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE);
1709 /* Get the D1HCLK configuration ----------------------------------------------*/
1710 RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE);
1712 /* Get the APB3 configuration ----------------------------------------------*/
1713 RCC_ClkInitStruct->APB3CLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE);
1715 /* Get the APB1 configuration ----------------------------------------------*/
1716 RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1);
1718 /* Get the APB2 configuration ----------------------------------------------*/
1719 RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2);
1721 /* Get the APB4 configuration ----------------------------------------------*/
1722 RCC_ClkInitStruct->APB4CLKDivider = (uint32_t)(RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE);
1723 #endif
1725 /* Get the Flash Wait State (Latency) configuration ------------------------*/
1726 *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
1730 * @brief This function handles the RCC CSS interrupt request.
1731 * @note This API should be called under the NMI_Handler().
1732 * @retval None
1734 void HAL_RCC_NMI_IRQHandler(void)
1736 /* Check RCC CSSF flag */
1737 if(__HAL_RCC_GET_IT(RCC_IT_CSS))
1739 /* RCC Clock Security System interrupt user callback */
1740 HAL_RCC_CCSCallback();
1742 /* Clear RCC CSS pending bit */
1743 __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
1748 * @brief RCC Clock Security System interrupt callback
1749 * @retval none
1751 __weak void HAL_RCC_CCSCallback(void)
1753 /* NOTE : This function Should not be modified, when the callback is needed,
1754 the HAL_RCC_CCSCallback could be implemented in the user file
1759 * @}
1763 * @}
1766 #endif /* HAL_RCC_MODULE_ENABLED */
1768 * @}
1772 * @}
1775 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/