Merge pull request #10592 from iNavFlight/MrD_Update-parameter-description
[inav.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_rcc.c
blob5be9eb10fc5bd14e6db7f765e4139ee384773fad
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 global variable */
232 SystemCoreClock = HSI_VALUE;
234 /* Adapt Systick interrupt period */
235 if(HAL_InitTick(uwTickPrio) != HAL_OK)
237 return HAL_ERROR;
240 /* Get Start Tick */
241 tickstart = HAL_GetTick();
243 /* Wait till clock switch is ready */
244 while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != 0U)
246 if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
248 return HAL_TIMEOUT;
252 /* Get Start Tick */
253 tickstart = HAL_GetTick();
255 /* Reset CSION, CSIKERON, HSEON, HSI48ON, HSECSSON, HSIDIV bits */
256 CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSIKERON| RCC_CR_HSIDIV| RCC_CR_HSIDIVF| RCC_CR_CSION | RCC_CR_CSIKERON \
257 | RCC_CR_HSI48ON | RCC_CR_CSSHSEON);
259 /* Wait till HSE is disabled */
260 while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
262 if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
264 return HAL_TIMEOUT;
268 /* Get Start Tick */
269 tickstart = HAL_GetTick();
271 /* Clear PLLON bit */
272 CLEAR_BIT(RCC->CR, RCC_CR_PLL1ON);
274 /* Wait till PLL is disabled */
275 while (READ_BIT(RCC->CR, RCC_CR_PLL1RDY) != 0U)
277 if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
279 return HAL_TIMEOUT;
283 /* Get Start Tick */
284 tickstart = HAL_GetTick();
286 /* Reset PLL2ON bit */
287 CLEAR_BIT(RCC->CR, RCC_CR_PLL2ON);
289 /* Wait till PLL2 is disabled */
290 while (READ_BIT(RCC->CR, RCC_CR_PLL2RDY) != 0U)
292 if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
294 return HAL_TIMEOUT;
298 /* Get Start Tick */
299 tickstart = HAL_GetTick();
301 /* Reset PLL3 bit */
302 CLEAR_BIT(RCC->CR, RCC_CR_PLL3ON);
304 /* Wait till PLL3 is disabled */
305 while (READ_BIT(RCC->CR, RCC_CR_PLL3RDY) != 0U)
307 if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
309 return HAL_TIMEOUT;
313 #if defined(RCC_D1CFGR_HPRE)
314 /* Reset D1CFGR register */
315 CLEAR_REG(RCC->D1CFGR);
317 /* Reset D2CFGR register */
318 CLEAR_REG(RCC->D2CFGR);
320 /* Reset D3CFGR register */
321 CLEAR_REG(RCC->D3CFGR);
322 #else
323 /* Reset CDCFGR1 register */
324 CLEAR_REG(RCC->CDCFGR1);
326 /* Reset CDCFGR2 register */
327 CLEAR_REG(RCC->CDCFGR2);
329 /* Reset SRDCFGR register */
330 CLEAR_REG(RCC->SRDCFGR);
331 #endif
333 /* Reset PLLCKSELR register to default value */
334 RCC->PLLCKSELR= RCC_PLLCKSELR_DIVM1_5|RCC_PLLCKSELR_DIVM2_5|RCC_PLLCKSELR_DIVM3_5;
336 /* Reset PLLCFGR register to default value */
337 WRITE_REG(RCC->PLLCFGR, 0x01FF0000U);
339 /* Reset PLL1DIVR register to default value */
340 WRITE_REG(RCC->PLL1DIVR,0x01010280U);
342 /* Reset PLL1FRACR register */
343 CLEAR_REG(RCC->PLL1FRACR);
345 /* Reset PLL2DIVR register to default value */
346 WRITE_REG(RCC->PLL2DIVR,0x01010280U);
348 /* Reset PLL2FRACR register */
349 CLEAR_REG(RCC->PLL2FRACR);
351 /* Reset PLL3DIVR register to default value */
352 WRITE_REG(RCC->PLL3DIVR,0x01010280U);
354 /* Reset PLL3FRACR register */
355 CLEAR_REG(RCC->PLL3FRACR);
357 /* Reset HSEBYP bit */
358 CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
360 /* Disable all interrupts */
361 CLEAR_REG(RCC->CIER);
363 /* Clear all interrupts flags */
364 WRITE_REG(RCC->CICR,0xFFFFFFFFU);
366 /* Reset all RSR flags */
367 SET_BIT(RCC->RSR, RCC_RSR_RMVF);
369 /* Decreasing the number of wait states because of lower CPU frequency */
370 if(FLASH_LATENCY_DEFAULT < __HAL_FLASH_GET_LATENCY())
372 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
373 __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_DEFAULT);
375 /* Check that the new number of wait states is taken into account to access the Flash
376 memory by reading the FLASH_ACR register */
377 if(__HAL_FLASH_GET_LATENCY() != FLASH_LATENCY_DEFAULT)
379 return HAL_ERROR;
384 return HAL_OK;
388 * @brief Initializes the RCC Oscillators according to the specified parameters in the
389 * RCC_OscInitTypeDef.
390 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
391 * contains the configuration information for the RCC Oscillators.
392 * @note The PLL is not disabled when used as system clock.
393 * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
394 * supported by this function. User should request a transition to LSE Off
395 * first and then LSE On or LSE Bypass.
396 * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
397 * supported by this function. User should request a transition to HSE Off
398 * first and then HSE On or HSE Bypass.
399 * @retval HAL status
401 __weak HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
403 uint32_t tickstart;
404 uint32_t temp1_pllckcfg, temp2_pllckcfg;
406 /* Check Null pointer */
407 if(RCC_OscInitStruct == NULL)
409 return HAL_ERROR;
412 /* Check the parameters */
413 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
414 /*------------------------------- HSE Configuration ------------------------*/
415 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
417 /* Check the parameters */
418 assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
420 const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
421 const uint32_t temp_pllckselr = RCC->PLLCKSELR;
422 /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
423 if((temp_sysclksrc == RCC_CFGR_SWS_HSE) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_HSE)))
425 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
427 return HAL_ERROR;
430 else
432 /* Set the new HSE configuration ---------------------------------------*/
433 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
435 /* Check the HSE State */
436 if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
438 /* Get Start Tick*/
439 tickstart = HAL_GetTick();
441 /* Wait till HSE is ready */
442 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U)
444 if((uint32_t) (HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
446 return HAL_TIMEOUT;
450 else
452 /* Get Start Tick*/
453 tickstart = HAL_GetTick();
455 /* Wait till HSE is disabled */
456 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U)
458 if((uint32_t) (HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
460 return HAL_TIMEOUT;
466 /*----------------------------- HSI Configuration --------------------------*/
467 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
469 /* Check the parameters */
470 assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
471 assert_param(IS_RCC_HSICALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
473 /* When the HSI is used as system clock it will not be disabled */
474 const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
475 const uint32_t temp_pllckselr = RCC->PLLCKSELR;
476 if((temp_sysclksrc == RCC_CFGR_SWS_HSI) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_HSI)))
478 /* When HSI is used as system clock it will not be disabled */
479 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
481 return HAL_ERROR;
483 /* Otherwise, just the calibration is allowed */
484 else
486 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
487 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
491 else
493 /* Check the HSI State */
494 if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
496 /* Enable the Internal High Speed oscillator (HSI, HSIDIV2,HSIDIV4, or HSIDIV8) */
497 __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIState);
499 /* Get Start Tick*/
500 tickstart = HAL_GetTick();
502 /* Wait till HSI is ready */
503 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
505 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
507 return HAL_TIMEOUT;
511 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
512 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
514 else
516 /* Disable the Internal High Speed oscillator (HSI). */
517 __HAL_RCC_HSI_DISABLE();
519 /* Get Start Tick*/
520 tickstart = HAL_GetTick();
522 /* Wait till HSI is disabled */
523 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U)
525 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
527 return HAL_TIMEOUT;
533 /*----------------------------- CSI Configuration --------------------------*/
534 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_CSI) == RCC_OSCILLATORTYPE_CSI)
536 /* Check the parameters */
537 assert_param(IS_RCC_CSI(RCC_OscInitStruct->CSIState));
538 assert_param(IS_RCC_CSICALIBRATION_VALUE(RCC_OscInitStruct->CSICalibrationValue));
540 /* When the CSI is used as system clock it will not disabled */
541 const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
542 const uint32_t temp_pllckselr = RCC->PLLCKSELR;
543 if((temp_sysclksrc == RCC_CFGR_SWS_CSI) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_CSI)))
545 /* When CSI is used as system clock it will not disabled */
546 if((__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) != 0U) && (RCC_OscInitStruct->CSIState != RCC_CSI_ON))
548 return HAL_ERROR;
550 /* Otherwise, just the calibration is allowed */
551 else
553 /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
554 __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->CSICalibrationValue);
557 else
559 /* Check the CSI State */
560 if((RCC_OscInitStruct->CSIState)!= RCC_CSI_OFF)
562 /* Enable the Internal High Speed oscillator (CSI). */
563 __HAL_RCC_CSI_ENABLE();
565 /* Get Start Tick*/
566 tickstart = HAL_GetTick();
568 /* Wait till CSI is ready */
569 while(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) == 0U)
571 if((HAL_GetTick() - tickstart ) > CSI_TIMEOUT_VALUE)
573 return HAL_TIMEOUT;
577 /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
578 __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->CSICalibrationValue);
580 else
582 /* Disable the Internal High Speed oscillator (CSI). */
583 __HAL_RCC_CSI_DISABLE();
585 /* Get Start Tick*/
586 tickstart = HAL_GetTick();
588 /* Wait till CSI is disabled */
589 while(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) != 0U)
591 if((HAL_GetTick() - tickstart ) > CSI_TIMEOUT_VALUE)
593 return HAL_TIMEOUT;
599 /*------------------------------ LSI Configuration -------------------------*/
600 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
602 /* Check the parameters */
603 assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
605 /* Check the LSI State */
606 if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
608 /* Enable the Internal Low Speed oscillator (LSI). */
609 __HAL_RCC_LSI_ENABLE();
611 /* Get Start Tick*/
612 tickstart = HAL_GetTick();
614 /* Wait till LSI is ready */
615 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == 0U)
617 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
619 return HAL_TIMEOUT;
623 else
625 /* Disable the Internal Low Speed oscillator (LSI). */
626 __HAL_RCC_LSI_DISABLE();
628 /* Get Start Tick*/
629 tickstart = HAL_GetTick();
631 /* Wait till LSI is ready */
632 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != 0U)
634 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
636 return HAL_TIMEOUT;
642 /*------------------------------ HSI48 Configuration -------------------------*/
643 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
645 /* Check the parameters */
646 assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
648 /* Check the HSI48 State */
649 if((RCC_OscInitStruct->HSI48State)!= RCC_HSI48_OFF)
651 /* Enable the Internal Low Speed oscillator (HSI48). */
652 __HAL_RCC_HSI48_ENABLE();
654 /* Get time-out */
655 tickstart = HAL_GetTick();
657 /* Wait till HSI48 is ready */
658 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) == 0U)
660 if((HAL_GetTick() - tickstart ) > HSI48_TIMEOUT_VALUE)
662 return HAL_TIMEOUT;
666 else
668 /* Disable the Internal Low Speed oscillator (HSI48). */
669 __HAL_RCC_HSI48_DISABLE();
671 /* Get time-out */
672 tickstart = HAL_GetTick();
674 /* Wait till HSI48 is ready */
675 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) != 0U)
677 if((HAL_GetTick() - tickstart ) > HSI48_TIMEOUT_VALUE)
679 return HAL_TIMEOUT;
684 /*------------------------------ LSE Configuration -------------------------*/
685 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
687 /* Check the parameters */
688 assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
690 /* Enable write access to Backup domain */
691 PWR->CR1 |= PWR_CR1_DBP;
693 /* Wait for Backup domain Write protection disable */
694 tickstart = HAL_GetTick();
696 while((PWR->CR1 & PWR_CR1_DBP) == 0U)
698 if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
700 return HAL_TIMEOUT;
704 /* Set the new LSE configuration -----------------------------------------*/
705 __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
706 /* Check the LSE State */
707 if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
709 /* Get Start Tick*/
710 tickstart = HAL_GetTick();
712 /* Wait till LSE is ready */
713 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == 0U)
715 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
717 return HAL_TIMEOUT;
721 else
723 /* Get Start Tick*/
724 tickstart = HAL_GetTick();
726 /* Wait till LSE is disabled */
727 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != 0U)
729 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
731 return HAL_TIMEOUT;
736 /*-------------------------------- PLL Configuration -----------------------*/
737 /* Check the parameters */
738 assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
739 if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
741 /* Check if the PLL is used as system clock or not */
742 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL1)
744 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
746 /* Check the parameters */
747 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
748 assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
749 assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
750 assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
751 assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
752 assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
753 assert_param(IS_RCC_PLLFRACN_VALUE(RCC_OscInitStruct->PLL.PLLFRACN));
755 /* Disable the main PLL. */
756 __HAL_RCC_PLL_DISABLE();
758 /* Get Start Tick*/
759 tickstart = HAL_GetTick();
761 /* Wait till PLL is disabled */
762 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U)
764 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
766 return HAL_TIMEOUT;
770 /* Configure the main PLL clock source, multiplication and division factors. */
771 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
772 RCC_OscInitStruct->PLL.PLLM,
773 RCC_OscInitStruct->PLL.PLLN,
774 RCC_OscInitStruct->PLL.PLLP,
775 RCC_OscInitStruct->PLL.PLLQ,
776 RCC_OscInitStruct->PLL.PLLR);
778 /* Disable PLLFRACN . */
779 __HAL_RCC_PLLFRACN_DISABLE();
781 /* Configure PLL PLL1FRACN */
782 __HAL_RCC_PLLFRACN_CONFIG(RCC_OscInitStruct->PLL.PLLFRACN);
784 /* Select PLL1 input reference frequency range: VCI */
785 __HAL_RCC_PLL_VCIRANGE(RCC_OscInitStruct->PLL.PLLRGE) ;
787 /* Select PLL1 output frequency range : VCO */
788 __HAL_RCC_PLL_VCORANGE(RCC_OscInitStruct->PLL.PLLVCOSEL) ;
790 /* Enable PLL System Clock output. */
791 __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVP);
793 /* Enable PLL1Q Clock output. */
794 __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVQ);
796 /* Enable PLL1R Clock output. */
797 __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVR);
799 /* Enable PLL1FRACN . */
800 __HAL_RCC_PLLFRACN_ENABLE();
802 /* Enable the main PLL. */
803 __HAL_RCC_PLL_ENABLE();
805 /* Get Start Tick*/
806 tickstart = HAL_GetTick();
808 /* Wait till PLL is ready */
809 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U)
811 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
813 return HAL_TIMEOUT;
817 else
819 /* Disable the main PLL. */
820 __HAL_RCC_PLL_DISABLE();
822 /* Get Start Tick*/
823 tickstart = HAL_GetTick();
825 /* Wait till PLL is disabled */
826 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U)
828 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
830 return HAL_TIMEOUT;
835 else
837 /* Do not return HAL_ERROR if request repeats the current configuration */
838 temp1_pllckcfg = RCC->PLLCKSELR;
839 temp2_pllckcfg = RCC->PLL1DIVR;
840 if(((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF) ||
841 (READ_BIT(temp1_pllckcfg, RCC_PLLCKSELR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
842 ((READ_BIT(temp1_pllckcfg, RCC_PLLCKSELR_DIVM1) >> RCC_PLLCKSELR_DIVM1_Pos) != RCC_OscInitStruct->PLL.PLLM) ||
843 (READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_N1) != (RCC_OscInitStruct->PLL.PLLN - 1U)) ||
844 ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_P1) >> RCC_PLL1DIVR_P1_Pos) != (RCC_OscInitStruct->PLL.PLLP - 1U)) ||
845 ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_Q1) >> RCC_PLL1DIVR_Q1_Pos) != (RCC_OscInitStruct->PLL.PLLQ - 1U)) ||
846 ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_R1) >> RCC_PLL1DIVR_R1_Pos) != (RCC_OscInitStruct->PLL.PLLR - 1U)))
848 return HAL_ERROR;
852 return HAL_OK;
856 * @brief Initializes the CPU, AHB and APB buses clocks according to the specified
857 * parameters in the RCC_ClkInitStruct.
858 * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
859 * contains the configuration information for the RCC peripheral.
860 * @param FLatency: FLASH Latency, this parameter depend on device selected
862 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
863 * and updated by HAL_InitTick() function called within this function
865 * @note The HSI is used (enabled by hardware) as system clock source after
866 * start-up from Reset, wake-up from STOP and STANDBY mode, or in case
867 * of failure of the HSE used directly or indirectly as system clock
868 * (if the Clock Security System CSS is enabled).
870 * @note A switch from one clock source to another occurs only if the target
871 * clock source is ready (clock stable after start-up delay or PLL locked).
872 * If a clock source which is not yet ready is selected, the switch will
873 * occur when the clock source will be ready.
874 * You can use HAL_RCC_GetClockConfig() function to know which clock is
875 * currently used as system clock source.
876 * @note Depending on the device voltage range, the software has to set correctly
877 * D1CPRE[3:0] bits to ensure that Domain1 core clock not exceed the maximum allowed frequency
878 * (for more details refer to section above "Initialization/de-initialization functions")
879 * @retval None
881 HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
883 HAL_StatusTypeDef halstatus;
884 uint32_t tickstart;
886 /* Check Null pointer */
887 if(RCC_ClkInitStruct == NULL)
889 return HAL_ERROR;
892 /* Check the parameters */
893 assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
894 assert_param(IS_FLASH_LATENCY(FLatency));
896 /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
897 must be correctly programmed according to the frequency of the CPU clock
898 (HCLK) and the supply voltage of the device. */
900 /* Increasing the CPU frequency */
901 if(FLatency > __HAL_FLASH_GET_LATENCY())
903 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
904 __HAL_FLASH_SET_LATENCY(FLatency);
906 /* Check that the new number of wait states is taken into account to access the Flash
907 memory by reading the FLASH_ACR register */
908 if(__HAL_FLASH_GET_LATENCY() != FLatency)
910 return HAL_ERROR;
915 /* Increasing the BUS frequency divider */
916 /*-------------------------- D1PCLK1/CDPCLK1 Configuration ---------------------------*/
917 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D1PCLK1) == RCC_CLOCKTYPE_D1PCLK1)
919 #if defined (RCC_D1CFGR_D1PPRE)
920 if((RCC_ClkInitStruct->APB3CLKDivider) > (RCC->D1CFGR & RCC_D1CFGR_D1PPRE))
922 assert_param(IS_RCC_D1PCLK1(RCC_ClkInitStruct->APB3CLKDivider));
923 MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1PPRE, RCC_ClkInitStruct->APB3CLKDivider);
925 #else
926 if((RCC_ClkInitStruct->APB3CLKDivider) > (RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE))
928 assert_param(IS_RCC_CDPCLK1(RCC_ClkInitStruct->APB3CLKDivider));
929 MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDPPRE, RCC_ClkInitStruct->APB3CLKDivider);
931 #endif
934 /*-------------------------- PCLK1 Configuration ---------------------------*/
935 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
937 #if defined (RCC_D2CFGR_D2PPRE1)
938 if((RCC_ClkInitStruct->APB1CLKDivider) > (RCC->D2CFGR & RCC_D2CFGR_D2PPRE1))
940 assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
941 MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
943 #else
944 if((RCC_ClkInitStruct->APB1CLKDivider) > (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1))
946 assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
947 MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
949 #endif
951 /*-------------------------- PCLK2 Configuration ---------------------------*/
952 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
954 #if defined(RCC_D2CFGR_D2PPRE2)
955 if((RCC_ClkInitStruct->APB2CLKDivider) > (RCC->D2CFGR & RCC_D2CFGR_D2PPRE2))
957 assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
958 MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
960 #else
961 if((RCC_ClkInitStruct->APB2CLKDivider) > (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2))
963 assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
964 MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
966 #endif
969 /*-------------------------- D3PCLK1 Configuration ---------------------------*/
970 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D3PCLK1) == RCC_CLOCKTYPE_D3PCLK1)
972 #if defined(RCC_D3CFGR_D3PPRE)
973 if((RCC_ClkInitStruct->APB4CLKDivider) > (RCC->D3CFGR & RCC_D3CFGR_D3PPRE))
975 assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
976 MODIFY_REG(RCC->D3CFGR, RCC_D3CFGR_D3PPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
978 #else
979 if((RCC_ClkInitStruct->APB4CLKDivider) > (RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE))
981 assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
982 MODIFY_REG(RCC->SRDCFGR, RCC_SRDCFGR_SRDPPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
984 #endif
987 /*-------------------------- HCLK Configuration --------------------------*/
988 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
990 #if defined (RCC_D1CFGR_HPRE)
991 if((RCC_ClkInitStruct->AHBCLKDivider) > (RCC->D1CFGR & RCC_D1CFGR_HPRE))
993 /* Set the new HCLK clock divider */
994 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
995 MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
997 #else
998 if((RCC_ClkInitStruct->AHBCLKDivider) > (RCC->CDCFGR1 & RCC_CDCFGR1_HPRE))
1000 /* Set the new HCLK clock divider */
1001 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
1002 MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
1004 #endif
1007 /*------------------------- SYSCLK Configuration -------------------------*/
1008 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
1010 assert_param(IS_RCC_SYSCLK(RCC_ClkInitStruct->SYSCLKDivider));
1011 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
1012 #if defined(RCC_D1CFGR_D1CPRE)
1013 MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1CPRE, RCC_ClkInitStruct->SYSCLKDivider);
1014 #else
1015 MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDCPRE, RCC_ClkInitStruct->SYSCLKDivider);
1016 #endif
1017 /* HSE is selected as System Clock Source */
1018 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
1020 /* Check the HSE ready flag */
1021 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U)
1023 return HAL_ERROR;
1026 /* PLL is selected as System Clock Source */
1027 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
1029 /* Check the PLL ready flag */
1030 if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U)
1032 return HAL_ERROR;
1035 /* CSI is selected as System Clock Source */
1036 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_CSI)
1038 /* Check the PLL ready flag */
1039 if(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) == 0U)
1041 return HAL_ERROR;
1044 /* HSI is selected as System Clock Source */
1045 else
1047 /* Check the HSI ready flag */
1048 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
1050 return HAL_ERROR;
1053 MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
1055 /* Get Start Tick*/
1056 tickstart = HAL_GetTick();
1058 while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
1060 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
1062 return HAL_TIMEOUT;
1068 /* Decreasing the BUS frequency divider */
1069 /*-------------------------- HCLK Configuration --------------------------*/
1070 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
1072 #if defined(RCC_D1CFGR_HPRE)
1073 if((RCC_ClkInitStruct->AHBCLKDivider) < (RCC->D1CFGR & RCC_D1CFGR_HPRE))
1075 /* Set the new HCLK clock divider */
1076 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
1077 MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
1079 #else
1080 if((RCC_ClkInitStruct->AHBCLKDivider) < (RCC->CDCFGR1 & RCC_CDCFGR1_HPRE))
1082 /* Set the new HCLK clock divider */
1083 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
1084 MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
1086 #endif
1089 /* Decreasing the number of wait states because of lower CPU frequency */
1090 if(FLatency < __HAL_FLASH_GET_LATENCY())
1092 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
1093 __HAL_FLASH_SET_LATENCY(FLatency);
1095 /* Check that the new number of wait states is taken into account to access the Flash
1096 memory by reading the FLASH_ACR register */
1097 if(__HAL_FLASH_GET_LATENCY() != FLatency)
1099 return HAL_ERROR;
1103 /*-------------------------- D1PCLK1/CDPCLK Configuration ---------------------------*/
1104 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D1PCLK1) == RCC_CLOCKTYPE_D1PCLK1)
1106 #if defined(RCC_D1CFGR_D1PPRE)
1107 if((RCC_ClkInitStruct->APB3CLKDivider) < (RCC->D1CFGR & RCC_D1CFGR_D1PPRE))
1109 assert_param(IS_RCC_D1PCLK1(RCC_ClkInitStruct->APB3CLKDivider));
1110 MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1PPRE, RCC_ClkInitStruct->APB3CLKDivider);
1112 #else
1113 if((RCC_ClkInitStruct->APB3CLKDivider) < (RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE))
1115 assert_param(IS_RCC_CDPCLK1(RCC_ClkInitStruct->APB3CLKDivider));
1116 MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDPPRE, RCC_ClkInitStruct->APB3CLKDivider);
1118 #endif
1121 /*-------------------------- PCLK1 Configuration ---------------------------*/
1122 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
1124 #if defined(RCC_D2CFGR_D2PPRE1)
1125 if((RCC_ClkInitStruct->APB1CLKDivider) < (RCC->D2CFGR & RCC_D2CFGR_D2PPRE1))
1127 assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
1128 MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
1130 #else
1131 if((RCC_ClkInitStruct->APB1CLKDivider) < (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1))
1133 assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
1134 MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
1136 #endif
1139 /*-------------------------- PCLK2 Configuration ---------------------------*/
1140 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
1142 #if defined (RCC_D2CFGR_D2PPRE2)
1143 if((RCC_ClkInitStruct->APB2CLKDivider) < (RCC->D2CFGR & RCC_D2CFGR_D2PPRE2))
1145 assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
1146 MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
1148 #else
1149 if((RCC_ClkInitStruct->APB2CLKDivider) < (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2))
1151 assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
1152 MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
1154 #endif
1157 /*-------------------------- D3PCLK1/SRDPCLK1 Configuration ---------------------------*/
1158 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D3PCLK1) == RCC_CLOCKTYPE_D3PCLK1)
1160 #if defined(RCC_D3CFGR_D3PPRE)
1161 if((RCC_ClkInitStruct->APB4CLKDivider) < (RCC->D3CFGR & RCC_D3CFGR_D3PPRE))
1163 assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
1164 MODIFY_REG(RCC->D3CFGR, RCC_D3CFGR_D3PPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
1166 #else
1167 if((RCC_ClkInitStruct->APB4CLKDivider) < (RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE))
1169 assert_param(IS_RCC_SRDPCLK1(RCC_ClkInitStruct->APB4CLKDivider));
1170 MODIFY_REG(RCC->SRDCFGR, RCC_SRDCFGR_SRDPPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
1172 #endif
1175 /* Update the SystemCoreClock global variable */
1176 #if defined(RCC_D1CFGR_D1CPRE)
1177 SystemCoreClock = HAL_RCC_GetSysClockFreq() >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> RCC_D1CFGR_D1CPRE_Pos]) & 0x1FU);
1178 #else
1179 SystemCoreClock = HAL_RCC_GetSysClockFreq() >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE)>> RCC_CDCFGR1_CDCPRE_Pos]) & 0x1FU);
1180 #endif
1181 /* Configure the source of time base considering new system clocks settings*/
1182 halstatus = HAL_InitTick (uwTickPrio);
1184 return halstatus;
1188 * @}
1191 /** @defgroup RCC_Group2 Peripheral Control functions
1192 * @brief RCC clocks control functions
1194 @verbatim
1195 ===============================================================================
1196 ##### Peripheral Control functions #####
1197 ===============================================================================
1198 [..]
1199 This subsection provides a set of functions allowing to control the RCC Clocks
1200 frequencies.
1202 @endverbatim
1203 * @{
1207 * @brief Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
1208 * @note PA8/PC9 should be configured in alternate function mode.
1209 * @param RCC_MCOx: specifies the output direction for the clock source.
1210 * This parameter can be one of the following values:
1211 * @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8).
1212 * @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9).
1213 * @param RCC_MCOSource: specifies the clock source to output.
1214 * This parameter can be one of the following values:
1215 * @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source
1216 * @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source
1217 * @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source
1218 * @arg RCC_MCO1SOURCE_PLL1QCLK: PLL1Q clock selected as MCO1 source
1219 * @arg RCC_MCO1SOURCE_HSI48: HSI48 (48MHZ) selected as MCO1 source
1220 * @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source
1221 * @arg RCC_MCO2SOURCE_PLL2PCLK: PLL2P clock selected as MCO2 source
1222 * @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source
1223 * @arg RCC_MCO2SOURCE_PLLCLK: PLL1P clock selected as MCO2 source
1224 * @arg RCC_MCO2SOURCE_CSICLK: CSI clock selected as MCO2 source
1225 * @arg RCC_MCO2SOURCE_LSICLK: LSI clock selected as MCO2 source
1226 * @param RCC_MCODiv: specifies the MCOx pre-scaler.
1227 * This parameter can be one of the following values:
1228 * @arg RCC_MCODIV_1 up to RCC_MCODIV_15 : divider applied to MCOx clock
1229 * @retval None
1231 void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
1233 GPIO_InitTypeDef GPIO_InitStruct;
1234 /* Check the parameters */
1235 assert_param(IS_RCC_MCO(RCC_MCOx));
1236 assert_param(IS_RCC_MCODIV(RCC_MCODiv));
1237 /* RCC_MCO1 */
1238 if(RCC_MCOx == RCC_MCO1)
1240 assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
1242 /* MCO1 Clock Enable */
1243 MCO1_CLK_ENABLE();
1245 /* Configure the MCO1 pin in alternate function mode */
1246 GPIO_InitStruct.Pin = MCO1_PIN;
1247 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
1248 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
1249 GPIO_InitStruct.Pull = GPIO_NOPULL;
1250 GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
1251 HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
1253 /* Mask MCO1 and MCO1PRE[3:0] bits then Select MCO1 clock source and pre-scaler */
1254 MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));
1256 else
1258 assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
1260 /* MCO2 Clock Enable */
1261 MCO2_CLK_ENABLE();
1263 /* Configure the MCO2 pin in alternate function mode */
1264 GPIO_InitStruct.Pin = MCO2_PIN;
1265 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
1266 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
1267 GPIO_InitStruct.Pull = GPIO_NOPULL;
1268 GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
1269 HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
1271 /* Mask MCO2 and MCO2PRE[3:0] bits then Select MCO2 clock source and pre-scaler */
1272 MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 7U)));
1277 * @brief Enables the Clock Security System.
1278 * @note If a failure is detected on the HSE oscillator clock, this oscillator
1279 * is automatically disabled and an interrupt is generated to inform the
1280 * software about the failure (Clock Security System Interrupt, CSSI),
1281 * allowing the MCU to perform rescue operations. The CSSI is linked to
1282 * the Cortex-M NMI (Non-Mask-able Interrupt) exception vector.
1283 * @retval None
1285 void HAL_RCC_EnableCSS(void)
1287 SET_BIT(RCC->CR, RCC_CR_CSSHSEON) ;
1291 * @brief Disables the Clock Security System.
1292 * @retval None
1294 void HAL_RCC_DisableCSS(void)
1296 CLEAR_BIT(RCC->CR, RCC_CR_CSSHSEON);
1300 * @brief Returns the SYSCLK frequency
1302 * @note The system frequency computed by this function is not the real
1303 * frequency in the chip. It is calculated based on the predefined
1304 * constant and the selected clock source:
1305 * @note If SYSCLK source is CSI, function returns values based on CSI_VALUE(*)
1306 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
1307 * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
1308 * @note If SYSCLK source is PLL, function returns values based on CSI_VALUE(*),
1309 * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
1310 * @note (*) CSI_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
1311 * 4 MHz) but the real value may vary depending on the variations
1312 * in voltage and temperature.
1313 * @note (**) HSI_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
1314 * 64 MHz) but the real value may vary depending on the variations
1315 * in voltage and temperature.
1316 * @note (***) HSE_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
1317 * 25 MHz), user has to ensure that HSE_VALUE is same as the real
1318 * frequency of the crystal used. Otherwise, this function may
1319 * have wrong result.
1321 * @note The result of this function could be not correct when using fractional
1322 * value for HSE crystal.
1324 * @note This function can be used by the user application to compute the
1325 * baud rate for the communication peripherals or configure other parameters.
1327 * @note Each time SYSCLK changes, this function must be called to update the
1328 * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
1331 * @retval SYSCLK frequency
1333 uint32_t HAL_RCC_GetSysClockFreq(void)
1335 uint32_t pllp, pllsource, pllm, pllfracen, hsivalue;
1336 float_t fracn1, pllvco;
1337 uint32_t sysclockfreq;
1339 /* Get SYSCLK source -------------------------------------------------------*/
1341 switch (RCC->CFGR & RCC_CFGR_SWS)
1343 case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
1345 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
1347 sysclockfreq = (uint32_t) (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER()>> 3));
1349 else
1351 sysclockfreq = (uint32_t) HSI_VALUE;
1354 break;
1356 case RCC_CFGR_SWS_CSI: /* CSI used as system clock source */
1357 sysclockfreq = CSI_VALUE;
1358 break;
1360 case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
1361 sysclockfreq = HSE_VALUE;
1362 break;
1364 case RCC_CFGR_SWS_PLL1: /* PLL1 used as system clock source */
1366 /* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
1367 SYSCLK = PLL_VCO / PLLR
1369 pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
1370 pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> 4) ;
1371 pllfracen = ((RCC-> PLLCFGR & RCC_PLLCFGR_PLL1FRACEN)>>RCC_PLLCFGR_PLL1FRACEN_Pos);
1372 fracn1 = (float_t)(uint32_t)(pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1)>> 3));
1374 if (pllm != 0U)
1376 switch (pllsource)
1378 case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
1380 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
1382 hsivalue= (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER()>> 3));
1383 pllvco = ( (float_t)hsivalue / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
1385 else
1387 pllvco = ((float_t)HSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
1389 break;
1391 case RCC_PLLSOURCE_CSI: /* CSI used as PLL clock source */
1392 pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
1393 break;
1395 case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
1396 pllvco = ((float_t)HSE_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
1397 break;
1399 default:
1400 pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
1401 break;
1403 pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >>9) + 1U ) ;
1404 sysclockfreq = (uint32_t)(float_t)(pllvco/(float_t)pllp);
1406 else
1408 sysclockfreq = 0U;
1410 break;
1412 default:
1413 sysclockfreq = CSI_VALUE;
1414 break;
1417 return sysclockfreq;
1422 * @brief Returns the HCLK frequency
1423 * @note Each time HCLK changes, this function must be called to update the
1424 * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
1426 * @note The SystemD2Clock CMSIS variable is used to store System domain2 Clock Frequency
1427 * and updated within this function
1428 * @retval HCLK frequency
1430 uint32_t HAL_RCC_GetHCLKFreq(void)
1432 #if defined(RCC_D1CFGR_HPRE)
1433 SystemD2Clock = (HAL_RCCEx_GetD1SysClockFreq() >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
1434 #else
1435 SystemD2Clock = (HAL_RCCEx_GetD1SysClockFreq() >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
1436 #endif
1437 return SystemD2Clock;
1442 * @brief Returns the PCLK1 frequency
1443 * @note Each time PCLK1 changes, this function must be called to update the
1444 * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
1445 * @retval PCLK1 frequency
1447 uint32_t HAL_RCC_GetPCLK1Freq(void)
1449 #if defined (RCC_D2CFGR_D2PPRE1)
1450 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
1451 return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->D2CFGR & RCC_D2CFGR_D2PPRE1)>> RCC_D2CFGR_D2PPRE1_Pos]) & 0x1FU));
1452 #else
1453 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
1454 return (HAL_RCC_GetHCLKFreq() >> D1CorePrescTable[(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1)>> POSITION_VAL(RCC_CDCFGR2_CDPPRE1_0)]);
1455 #endif
1460 * @brief Returns the PCLK2 frequency
1461 * @note Each time PCLK2 changes, this function must be called to update the
1462 * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
1463 * @retval PCLK1 frequency
1465 uint32_t HAL_RCC_GetPCLK2Freq(void)
1467 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
1468 #if defined(RCC_D2CFGR_D2PPRE2)
1469 return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->D2CFGR & RCC_D2CFGR_D2PPRE2)>> RCC_D2CFGR_D2PPRE2_Pos]) & 0x1FU));
1470 #else
1471 return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2)>> RCC_CDCFGR2_CDPPRE2_Pos]) & 0x1FU));
1472 #endif
1476 * @brief Configures the RCC_OscInitStruct according to the internal
1477 * RCC configuration registers.
1478 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
1479 * will be configured.
1480 * @retval None
1482 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
1484 /* Set all possible values for the Oscillator type parameter ---------------*/
1485 RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_CSI | \
1486 RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI| RCC_OSCILLATORTYPE_HSI48;
1488 /* Get the HSE configuration -----------------------------------------------*/
1489 #if defined(RCC_CR_HSEEXT)
1490 if((RCC->CR &(RCC_CR_HSEBYP | RCC_CR_HSEEXT)) == RCC_CR_HSEBYP)
1492 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
1494 else if((RCC->CR &(RCC_CR_HSEBYP | RCC_CR_HSEEXT)) == (RCC_CR_HSEBYP | RCC_CR_HSEEXT))
1496 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS_DIGITAL;
1498 else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
1500 RCC_OscInitStruct->HSEState = RCC_HSE_ON;
1502 else
1504 RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
1506 #else
1507 if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
1509 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
1511 else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
1513 RCC_OscInitStruct->HSEState = RCC_HSE_ON;
1515 else
1517 RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
1519 #endif /* RCC_CR_HSEEXT */
1521 /* Get the CSI configuration -----------------------------------------------*/
1522 if((RCC->CR &RCC_CR_CSION) == RCC_CR_CSION)
1524 RCC_OscInitStruct->CSIState = RCC_CSI_ON;
1526 else
1528 RCC_OscInitStruct->CSIState = RCC_CSI_OFF;
1531 #if defined(RCC_VER_X)
1532 if(HAL_GetREVID() <= REV_ID_Y)
1534 RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, HAL_RCC_REV_Y_CSITRIM_Msk) >> HAL_RCC_REV_Y_CSITRIM_Pos);
1536 else
1538 RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->CSICFGR, RCC_CSICFGR_CSITRIM) >> RCC_CSICFGR_CSITRIM_Pos);
1540 #else
1541 RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->CSICFGR, RCC_CSICFGR_CSITRIM) >> RCC_CSICFGR_CSITRIM_Pos);
1542 #endif /*RCC_VER_X*/
1544 /* Get the HSI configuration -----------------------------------------------*/
1545 if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
1547 RCC_OscInitStruct->HSIState = RCC_HSI_ON;
1549 else
1551 RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
1554 #if defined(RCC_VER_X)
1555 if(HAL_GetREVID() <= REV_ID_Y)
1557 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, HAL_RCC_REV_Y_HSITRIM_Msk) >> HAL_RCC_REV_Y_HSITRIM_Pos);
1559 else
1561 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM) >> RCC_HSICFGR_HSITRIM_Pos);
1563 #else
1564 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM) >> RCC_HSICFGR_HSITRIM_Pos);
1565 #endif /*RCC_VER_X*/
1567 /* Get the LSE configuration -----------------------------------------------*/
1568 #if defined(RCC_BDCR_LSEEXT)
1569 if((RCC->BDCR &(RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT)) == RCC_BDCR_LSEBYP)
1571 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
1573 else if((RCC->BDCR &(RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT)) == (RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT))
1575 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS_DIGITAL;
1577 else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
1579 RCC_OscInitStruct->LSEState = RCC_LSE_ON;
1581 else
1583 RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
1585 #else
1586 if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
1588 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
1590 else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
1592 RCC_OscInitStruct->LSEState = RCC_LSE_ON;
1594 else
1596 RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
1598 #endif /* RCC_BDCR_LSEEXT */
1600 /* Get the LSI configuration -----------------------------------------------*/
1601 if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
1603 RCC_OscInitStruct->LSIState = RCC_LSI_ON;
1605 else
1607 RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
1610 /* Get the HSI48 configuration ---------------------------------------------*/
1611 if((RCC->CR & RCC_CR_HSI48ON) == RCC_CR_HSI48ON)
1613 RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
1615 else
1617 RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
1620 /* Get the PLL configuration -----------------------------------------------*/
1621 if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
1623 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
1625 else
1627 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
1629 RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
1630 RCC_OscInitStruct->PLL.PLLM = (uint32_t)((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> RCC_PLLCKSELR_DIVM1_Pos);
1631 RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) >> RCC_PLL1DIVR_N1_Pos)+ 1U;
1632 RCC_OscInitStruct->PLL.PLLR = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_R1) >> RCC_PLL1DIVR_R1_Pos)+ 1U;
1633 RCC_OscInitStruct->PLL.PLLP = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >> RCC_PLL1DIVR_P1_Pos)+ 1U;
1634 RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_Q1) >> RCC_PLL1DIVR_Q1_Pos)+ 1U;
1635 RCC_OscInitStruct->PLL.PLLRGE = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLL1RGE));
1636 RCC_OscInitStruct->PLL.PLLVCOSEL = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLL1VCOSEL) >> RCC_PLLCFGR_PLL1VCOSEL_Pos);
1637 RCC_OscInitStruct->PLL.PLLFRACN = (uint32_t)(((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1) >> RCC_PLL1FRACR_FRACN1_Pos));
1641 * @brief Configures the RCC_ClkInitStruct according to the internal
1642 * RCC configuration registers.
1643 * @param RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that
1644 * will be configured.
1645 * @param pFLatency: Pointer on the Flash Latency.
1646 * @retval None
1648 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
1650 /* Set all possible values for the Clock type parameter --------------------*/
1651 RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 |
1652 RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1 ;
1654 /* Get the SYSCLK configuration --------------------------------------------*/
1655 RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
1657 #if defined(RCC_D1CFGR_D1CPRE)
1658 /* Get the SYSCLK configuration ----------------------------------------------*/
1659 RCC_ClkInitStruct->SYSCLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_D1CPRE);
1661 /* Get the D1HCLK configuration ----------------------------------------------*/
1662 RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_HPRE);
1664 /* Get the APB3 configuration ----------------------------------------------*/
1665 RCC_ClkInitStruct->APB3CLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_D1PPRE);
1667 /* Get the APB1 configuration ----------------------------------------------*/
1668 RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->D2CFGR & RCC_D2CFGR_D2PPRE1);
1670 /* Get the APB2 configuration ----------------------------------------------*/
1671 RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)(RCC->D2CFGR & RCC_D2CFGR_D2PPRE2);
1673 /* Get the APB4 configuration ----------------------------------------------*/
1674 RCC_ClkInitStruct->APB4CLKDivider = (uint32_t)(RCC->D3CFGR & RCC_D3CFGR_D3PPRE);
1675 #else
1676 /* Get the SYSCLK configuration ----------------------------------------------*/
1677 RCC_ClkInitStruct->SYSCLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE);
1679 /* Get the D1HCLK configuration ----------------------------------------------*/
1680 RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE);
1682 /* Get the APB3 configuration ----------------------------------------------*/
1683 RCC_ClkInitStruct->APB3CLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE);
1685 /* Get the APB1 configuration ----------------------------------------------*/
1686 RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1);
1688 /* Get the APB2 configuration ----------------------------------------------*/
1689 RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2);
1691 /* Get the APB4 configuration ----------------------------------------------*/
1692 RCC_ClkInitStruct->APB4CLKDivider = (uint32_t)(RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE);
1693 #endif
1695 /* Get the Flash Wait State (Latency) configuration ------------------------*/
1696 *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
1700 * @brief This function handles the RCC CSS interrupt request.
1701 * @note This API should be called under the NMI_Handler().
1702 * @retval None
1704 void HAL_RCC_NMI_IRQHandler(void)
1706 /* Check RCC CSSF flag */
1707 if(__HAL_RCC_GET_IT(RCC_IT_CSS))
1709 /* RCC Clock Security System interrupt user callback */
1710 HAL_RCC_CCSCallback();
1712 /* Clear RCC CSS pending bit */
1713 __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
1718 * @brief RCC Clock Security System interrupt callback
1719 * @retval none
1721 __weak void HAL_RCC_CCSCallback(void)
1723 /* NOTE : This function Should not be modified, when the callback is needed,
1724 the HAL_RCC_CCSCallback could be implemented in the user file
1729 * @}
1733 * @}
1736 #endif /* HAL_RCC_MODULE_ENABLED */
1738 * @}
1742 * @}
1745 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/