STM32H735xx HAL User Manual
stm32h7xx_hal_rcc.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h7xx_hal_rcc.c
00004   * @author  MCD Application Team
00005   * @brief   RCC HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Reset and Clock Control (RCC) peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + Peripheral Control functions
00010   *
00011   @verbatim
00012   ==============================================================================
00013                       ##### RCC specific features #####
00014   ==============================================================================
00015     [..]
00016       After reset the device is running from Internal High Speed oscillator
00017       (HSI 64MHz) with Flash 0 wait state,and all peripherals are off except
00018       internal SRAM, Flash, JTAG and PWR
00019       (+) There is no pre-scaler on High speed (AHB) and Low speed (APB) buses;
00020           all peripherals mapped on these buses are running at HSI speed.
00021       (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
00022       (+) All GPIOs are in analogue mode , except the JTAG pins which
00023           are assigned to be used for debug purpose.
00024 
00025     [..]
00026       Once the device started from reset, the user application has to:
00027       (+) Configure the clock source to be used to drive the System clock
00028           (if the application needs higher frequency/performance)
00029       (+) Configure the System clock frequency and Flash settings
00030       (+) Configure the AHB and APB buses pre-scalers
00031       (+) Enable the clock for the peripheral(s) to be used
00032       (+) Configure the clock kernel source(s) for peripherals which clocks are not
00033           derived from the System clock through :RCC_D1CCIPR,RCC_D2CCIP1R,RCC_D2CCIP2R
00034           and RCC_D3CCIPR registers
00035 
00036                       ##### RCC Limitations #####
00037   ==============================================================================
00038     [..]
00039       A delay between an RCC peripheral clock enable and the effective peripheral
00040       enabling should be taken into account in order to manage the peripheral read/write
00041       from/to registers.
00042       (+) This delay depends on the peripheral mapping.
00043       (+) If peripheral is mapped on AHB: the delay is 2 AHB clock cycle
00044           after the clock enable bit is set on the hardware register
00045       (+) If peripheral is mapped on APB: the delay is 2 APB clock cycle
00046           after the clock enable bit is set on the hardware register
00047 
00048     [..]
00049       Implemented Workaround:
00050       (+) For AHB & APB peripherals, a dummy read to the peripheral register has been
00051           inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.
00052 
00053   @endverbatim
00054  ******************************************************************************
00055   * @attention
00056   *
00057   * Copyright (c) 2017 STMicroelectronics.
00058   * All rights reserved.
00059   *
00060   * This software is licensed under terms that can be found in the LICENSE file in
00061   * the root directory of this software component.
00062   * If no LICENSE file comes with this software, it is provided AS-IS.
00063   ******************************************************************************
00064   */
00065 
00066 /* Includes ------------------------------------------------------------------*/
00067 #include "stm32h7xx_hal.h"
00068 
00069 /** @addtogroup STM32H7xx_HAL_Driver
00070   * @{
00071   */
00072 
00073 /** @defgroup RCC  RCC
00074   * @brief RCC HAL module driver
00075   * @{
00076   */
00077 
00078 #ifdef HAL_RCC_MODULE_ENABLED
00079 
00080 /* Private typedef -----------------------------------------------------------*/
00081 /* Private define ------------------------------------------------------------*/
00082 /* Private macro -------------------------------------------------------------*/
00083 /** @defgroup RCC_Private_Macros RCC Private Macros
00084   * @{
00085   */
00086 #define MCO1_CLK_ENABLE()     __HAL_RCC_GPIOA_CLK_ENABLE()
00087 #define MCO1_GPIO_PORT        GPIOA
00088 #define MCO1_PIN              GPIO_PIN_8
00089 
00090 #define MCO2_CLK_ENABLE()      __HAL_RCC_GPIOC_CLK_ENABLE()
00091 #define MCO2_GPIO_PORT         GPIOC
00092 #define MCO2_PIN               GPIO_PIN_9
00093 
00094 /**
00095   * @}
00096   */
00097 /* Private variables ---------------------------------------------------------*/
00098 /** @defgroup RCC_Private_Variables RCC Private Variables
00099   * @{
00100   */
00101 
00102 /**
00103   * @}
00104   */
00105 /* Private function prototypes -----------------------------------------------*/
00106 /* Exported functions --------------------------------------------------------*/
00107 
00108 /** @defgroup RCC_Exported_Functions RCC Exported Functions
00109   * @{
00110   */
00111 
00112 /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
00113  *  @brief    Initialization and Configuration functions
00114  *
00115 @verbatim
00116  ===============================================================================
00117            ##### Initialization and de-initialization functions #####
00118  ===============================================================================
00119     [..]
00120       This section provides functions allowing to configure the internal/external oscillators
00121       (HSE, HSI, LSE,CSI, LSI,HSI48, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB3, AHB1
00122        AHB2,AHB4,APB3, APB1L, APB1H, APB2, and APB4).
00123 
00124     [..] Internal/external clock and PLL configuration
00125          (#) HSI (high-speed internal), 64 MHz factory-trimmed RC used directly or through
00126              the PLL as System clock source.
00127          (#) CSI is a low-power RC oscillator which can be used directly as system clock, peripheral
00128              clock, or PLL input.But even with frequency calibration, is less accurate than an
00129              external crystal oscillator or ceramic resonator.
00130          (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC
00131              clock source.
00132 
00133          (#) HSE (high-speed external), 4 to 48 MHz crystal oscillator used directly or
00134              through the PLL as System clock source. Can be used also as RTC clock source.
00135 
00136          (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
00137 
00138          (#) PLL , The RCC features three independent PLLs (clocked by HSI , HSE or CSI),
00139              featuring three different output clocks and able  to work either in integer or Fractional mode.
00140            (++) A main PLL, PLL1, which is generally used to provide clocks to the CPU
00141                 and to some peripherals.
00142            (++) Two dedicated PLLs, PLL2 and PLL3, which are used to generate the kernel clock for peripherals.
00143 
00144 
00145          (#) CSS (Clock security system), once enabled and if a HSE clock failure occurs
00146             (HSE used directly or through PLL as System clock source), the System clock
00147              is automatically switched to HSI and an interrupt is generated if enabled.
00148              The interrupt is linked to the Cortex-M NMI (Non-Mask-able Interrupt)
00149              exception vector.
00150 
00151          (#) MCO1 (micro controller clock output), used to output HSI, LSE, HSE, PLL1(PLL1_Q)
00152              or HSI48 clock (through a configurable pre-scaler) on PA8 pin.
00153 
00154          (#) MCO2 (micro controller clock output), used to output HSE, PLL2(PLL2_P), SYSCLK,
00155              LSI, CSI, or PLL1(PLL1_P) clock (through a configurable pre-scaler) on PC9 pin.
00156 
00157     [..] System, AHB and APB buses clocks configuration
00158          (#) Several clock sources can be used to drive the System clock (SYSCLK): CSI,HSI,
00159              HSE and PLL.
00160              The AHB clock (HCLK) is derived from System core clock through configurable
00161              pre-scaler and used to clock the CPU, memory and peripherals mapped
00162              on AHB and APB bus of the 3 Domains (D1, D2, D3)* through configurable pre-scalers
00163              and used to clock the peripherals mapped on these buses. You can use
00164              "HAL_RCC_GetSysClockFreq()" function to retrieve system clock frequency.
00165 
00166          -@- All the peripheral clocks are derived from the System clock (SYSCLK) except those
00167              with dual clock domain where kernel source clock could be selected through
00168              RCC_D1CCIPR,RCC_D2CCIP1R,RCC_D2CCIP2R and RCC_D3CCIPR registers.
00169 
00170      (*) : 2 Domains (CD and SRD) for stm32h7a3xx and stm32h7b3xx family lines.
00171 @endverbatim
00172   * @{
00173   */
00174 
00175 /**
00176   * @brief  Resets the RCC clock configuration to the default reset state.
00177   * @note   The default reset state of the clock configuration is given below:
00178   *            - HSI ON and used as system clock source
00179   *            - HSE, PLL1, PLL2 and PLL3 OFF
00180   *            - AHB, APB Bus pre-scaler set to 1.
00181   *            - CSS, MCO1 and MCO2 OFF
00182   *            - All interrupts disabled
00183   * @note   This function doesn't modify the configuration of the
00184   *            - Peripheral clocks
00185   *            - LSI, LSE and RTC clocks
00186   * @retval HAL status
00187   */
00188 HAL_StatusTypeDef HAL_RCC_DeInit(void)
00189 {
00190   uint32_t tickstart;
00191 
00192         /* Increasing the CPU frequency */
00193   if(FLASH_LATENCY_DEFAULT  > __HAL_FLASH_GET_LATENCY())
00194   {
00195     /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
00196     __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_DEFAULT);
00197 
00198     /* Check that the new number of wait states is taken into account to access the Flash
00199     memory by reading the FLASH_ACR register */
00200     if(__HAL_FLASH_GET_LATENCY() != FLASH_LATENCY_DEFAULT)
00201     {
00202       return HAL_ERROR;
00203     }
00204 
00205   }
00206 
00207 
00208   /* Get Start Tick */
00209   tickstart = HAL_GetTick();
00210 
00211   /* Set HSION bit */
00212   SET_BIT(RCC->CR, RCC_CR_HSION);
00213 
00214   /* Wait till HSI is ready */
00215   while (READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
00216   {
00217     if ((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
00218     {
00219       return HAL_TIMEOUT;
00220     }
00221   }
00222 
00223   /* Set HSITRIM[6:0] bits to the reset value */
00224   SET_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM_6);
00225 
00226   /* Reset CFGR register */
00227   CLEAR_REG(RCC->CFGR);
00228 
00229   /* Update the SystemCoreClock and SystemD2Clock global variables */
00230   SystemCoreClock = HSI_VALUE;
00231   SystemD2Clock = HSI_VALUE;
00232 
00233   /* Adapt Systick interrupt period */
00234   if(HAL_InitTick(uwTickPrio) != HAL_OK)
00235   {
00236     return HAL_ERROR;
00237   }
00238 
00239   /* Get Start Tick */
00240   tickstart = HAL_GetTick();
00241 
00242   /* Wait till clock switch is ready */
00243   while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != 0U)
00244   {
00245     if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
00246     {
00247       return HAL_TIMEOUT;
00248     }
00249   }
00250 
00251   /* Get Start Tick */
00252   tickstart = HAL_GetTick();
00253 
00254   /* Reset CSION, CSIKERON, HSEON, HSI48ON, HSECSSON, HSIDIV bits */
00255   CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSIKERON| RCC_CR_HSIDIV| RCC_CR_HSIDIVF| RCC_CR_CSION | RCC_CR_CSIKERON  \
00256   | RCC_CR_HSI48ON | RCC_CR_CSSHSEON);
00257 
00258   /* Wait till HSE is disabled */
00259   while (READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
00260   {
00261     if ((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
00262     {
00263       return HAL_TIMEOUT;
00264     }
00265   }
00266 
00267   /* Get Start Tick */
00268   tickstart = HAL_GetTick();
00269 
00270   /* Clear PLLON bit */
00271   CLEAR_BIT(RCC->CR, RCC_CR_PLL1ON);
00272 
00273   /* Wait till PLL is disabled */
00274   while (READ_BIT(RCC->CR, RCC_CR_PLL1RDY) != 0U)
00275   {
00276     if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
00277     {
00278       return HAL_TIMEOUT;
00279     }
00280   }
00281 
00282   /* Get Start Tick */
00283   tickstart = HAL_GetTick();
00284 
00285   /* Reset PLL2ON bit */
00286   CLEAR_BIT(RCC->CR, RCC_CR_PLL2ON);
00287 
00288   /* Wait till PLL2 is disabled */
00289   while (READ_BIT(RCC->CR, RCC_CR_PLL2RDY) != 0U)
00290   {
00291     if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
00292     {
00293       return HAL_TIMEOUT;
00294     }
00295   }
00296 
00297   /* Get Start Tick */
00298   tickstart = HAL_GetTick();
00299 
00300   /* Reset PLL3 bit */
00301   CLEAR_BIT(RCC->CR, RCC_CR_PLL3ON);
00302 
00303   /* Wait till PLL3 is disabled */
00304   while (READ_BIT(RCC->CR, RCC_CR_PLL3RDY) != 0U)
00305   {
00306     if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
00307     {
00308       return HAL_TIMEOUT;
00309     }
00310   }
00311 
00312 #if defined(RCC_D1CFGR_HPRE)
00313   /* Reset D1CFGR register */
00314   CLEAR_REG(RCC->D1CFGR);
00315 
00316   /* Reset D2CFGR register */
00317   CLEAR_REG(RCC->D2CFGR);
00318 
00319   /* Reset D3CFGR register */
00320   CLEAR_REG(RCC->D3CFGR);
00321 #else
00322   /* Reset CDCFGR1 register */
00323   CLEAR_REG(RCC->CDCFGR1);
00324 
00325   /* Reset CDCFGR2 register */
00326   CLEAR_REG(RCC->CDCFGR2);
00327 
00328   /* Reset SRDCFGR register */
00329   CLEAR_REG(RCC->SRDCFGR);
00330 #endif
00331 
00332   /* Reset PLLCKSELR register to default value */
00333   RCC->PLLCKSELR= RCC_PLLCKSELR_DIVM1_5|RCC_PLLCKSELR_DIVM2_5|RCC_PLLCKSELR_DIVM3_5;
00334 
00335   /* Reset PLLCFGR register to default value */
00336   WRITE_REG(RCC->PLLCFGR, 0x01FF0000U);
00337 
00338   /* Reset PLL1DIVR register to default value */
00339   WRITE_REG(RCC->PLL1DIVR,0x01010280U);
00340 
00341   /* Reset PLL1FRACR register */
00342   CLEAR_REG(RCC->PLL1FRACR);
00343 
00344   /* Reset PLL2DIVR register to default value */
00345   WRITE_REG(RCC->PLL2DIVR,0x01010280U);
00346 
00347   /* Reset PLL2FRACR register */
00348   CLEAR_REG(RCC->PLL2FRACR);
00349 
00350   /* Reset PLL3DIVR register to default value */
00351   WRITE_REG(RCC->PLL3DIVR,0x01010280U);
00352 
00353   /* Reset PLL3FRACR register */
00354   CLEAR_REG(RCC->PLL3FRACR);
00355 
00356 #if defined(RCC_CR_HSEEXT)
00357   /* Reset HSEEXT  */
00358   CLEAR_BIT(RCC->CR, RCC_CR_HSEEXT);
00359 #endif /* RCC_CR_HSEEXT */
00360 
00361   /* Reset HSEBYP bit */
00362   CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
00363 
00364   /* Disable all interrupts */
00365   CLEAR_REG(RCC->CIER);
00366 
00367   /* Clear all interrupts flags */
00368   WRITE_REG(RCC->CICR,0xFFFFFFFFU);
00369 
00370   /* Reset all RSR flags */
00371   SET_BIT(RCC->RSR, RCC_RSR_RMVF);
00372 
00373       /* Decreasing the number of wait states because of lower CPU frequency */
00374   if(FLASH_LATENCY_DEFAULT  < __HAL_FLASH_GET_LATENCY())
00375   {
00376     /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
00377     __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_DEFAULT);
00378 
00379     /* Check that the new number of wait states is taken into account to access the Flash
00380     memory by reading the FLASH_ACR register */
00381     if(__HAL_FLASH_GET_LATENCY() != FLASH_LATENCY_DEFAULT)
00382     {
00383       return HAL_ERROR;
00384     }
00385 
00386 }
00387 
00388   return HAL_OK;
00389 }
00390 
00391 /**
00392   * @brief  Initializes the RCC Oscillators according to the specified parameters in the
00393   *         RCC_OscInitTypeDef.
00394   * @param  RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
00395   *         contains the configuration information for the RCC Oscillators.
00396   * @note   The PLL is not disabled when used as system clock.
00397   * @note   Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
00398   *         supported by this function. User should request a transition to LSE Off
00399   *         first and then LSE On or LSE Bypass.
00400   * @note   Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
00401   *         supported by this function. User should request a transition to HSE Off
00402   *         first and then HSE On or HSE Bypass.
00403   * @retval HAL status
00404   */
00405 __weak HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef  *RCC_OscInitStruct)
00406 {
00407   uint32_t tickstart;
00408   uint32_t temp1_pllckcfg, temp2_pllckcfg;
00409 
00410     /* Check Null pointer */
00411   if(RCC_OscInitStruct == NULL)
00412   {
00413     return HAL_ERROR;
00414   }
00415 
00416   /* Check the parameters */
00417   assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
00418   /*------------------------------- HSE Configuration ------------------------*/
00419   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
00420   {
00421     /* Check the parameters */
00422     assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
00423 
00424     const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
00425     const uint32_t temp_pllckselr = RCC->PLLCKSELR;
00426     /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
00427     if((temp_sysclksrc == RCC_CFGR_SWS_HSE) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_HSE)))
00428     {
00429       if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
00430       {
00431         return HAL_ERROR;
00432       }
00433     }
00434     else
00435     {
00436       /* Set the new HSE configuration ---------------------------------------*/
00437       __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
00438 
00439       /* Check the HSE State */
00440       if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
00441       {
00442         /* Get Start Tick*/
00443         tickstart = HAL_GetTick();
00444 
00445         /* Wait till HSE is ready */
00446         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U)
00447         {
00448           if((uint32_t) (HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
00449           {
00450             return HAL_TIMEOUT;
00451           }
00452         }
00453       }
00454       else
00455       {
00456         /* Get Start Tick*/
00457         tickstart = HAL_GetTick();
00458 
00459         /* Wait till HSE is disabled */
00460         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U)
00461         {
00462           if((uint32_t) (HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
00463           {
00464             return HAL_TIMEOUT;
00465           }
00466         }
00467       }
00468     }
00469   }
00470   /*----------------------------- HSI Configuration --------------------------*/
00471   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
00472   {
00473     /* Check the parameters */
00474     assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
00475     assert_param(IS_RCC_HSICALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
00476 
00477     /* When the HSI is used as system clock it will not be disabled */
00478     const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
00479     const uint32_t temp_pllckselr = RCC->PLLCKSELR;
00480     if((temp_sysclksrc == RCC_CFGR_SWS_HSI) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_HSI)))
00481     {
00482       /* When HSI is used as system clock it will not be disabled */
00483       if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
00484       {
00485         return HAL_ERROR;
00486       }
00487       /* Otherwise, only HSI division and calibration are allowed */
00488       else
00489       {
00490           /* Enable the Internal High Speed oscillator (HSI, HSIDIV2, HSIDIV4, or HSIDIV8) */
00491           __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIState);
00492 
00493           /* Get Start Tick*/
00494           tickstart = HAL_GetTick();
00495 
00496           /* Wait till HSI is ready */
00497           while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
00498           {
00499             if((uint32_t) (HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
00500             {
00501               return HAL_TIMEOUT;
00502             }
00503           }
00504         /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
00505         __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
00506       }
00507     }
00508 
00509     else
00510     {
00511       /* Check the HSI State */
00512       if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
00513       {
00514      /* Enable the Internal High Speed oscillator (HSI, HSIDIV2,HSIDIV4, or HSIDIV8) */
00515         __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIState);
00516 
00517         /* Get Start Tick*/
00518         tickstart = HAL_GetTick();
00519 
00520         /* Wait till HSI is ready */
00521         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
00522         {
00523           if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
00524           {
00525             return HAL_TIMEOUT;
00526           }
00527         }
00528 
00529         /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
00530         __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
00531       }
00532       else
00533       {
00534         /* Disable the Internal High Speed oscillator (HSI). */
00535         __HAL_RCC_HSI_DISABLE();
00536 
00537         /* Get Start Tick*/
00538         tickstart = HAL_GetTick();
00539 
00540         /* Wait till HSI is disabled */
00541         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U)
00542         {
00543           if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
00544           {
00545             return HAL_TIMEOUT;
00546           }
00547         }
00548       }
00549     }
00550   }
00551   /*----------------------------- CSI Configuration --------------------------*/
00552   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_CSI) == RCC_OSCILLATORTYPE_CSI)
00553   {
00554     /* Check the parameters */
00555     assert_param(IS_RCC_CSI(RCC_OscInitStruct->CSIState));
00556     assert_param(IS_RCC_CSICALIBRATION_VALUE(RCC_OscInitStruct->CSICalibrationValue));
00557 
00558     /* When the CSI is used as system clock it will not disabled */
00559     const uint32_t temp_sysclksrc = __HAL_RCC_GET_SYSCLK_SOURCE();
00560     const uint32_t temp_pllckselr = RCC->PLLCKSELR;
00561     if((temp_sysclksrc == RCC_CFGR_SWS_CSI) || ((temp_sysclksrc == RCC_CFGR_SWS_PLL1) && ((temp_pllckselr & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_CSI)))
00562     {
00563       /* When CSI is used as system clock it will not disabled */
00564       if((__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) != 0U) && (RCC_OscInitStruct->CSIState != RCC_CSI_ON))
00565       {
00566         return HAL_ERROR;
00567       }
00568       /* Otherwise, just the calibration is allowed */
00569       else
00570       {
00571         /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
00572         __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->CSICalibrationValue);
00573       }
00574     }
00575     else
00576     {
00577       /* Check the CSI State */
00578       if((RCC_OscInitStruct->CSIState)!= RCC_CSI_OFF)
00579       {
00580         /* Enable the Internal High Speed oscillator (CSI). */
00581         __HAL_RCC_CSI_ENABLE();
00582 
00583         /* Get Start Tick*/
00584         tickstart = HAL_GetTick();
00585 
00586         /* Wait till CSI is ready */
00587         while(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) == 0U)
00588         {
00589           if((HAL_GetTick() - tickstart ) > CSI_TIMEOUT_VALUE)
00590           {
00591             return HAL_TIMEOUT;
00592           }
00593         }
00594 
00595         /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
00596         __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->CSICalibrationValue);
00597       }
00598       else
00599       {
00600         /* Disable the Internal High Speed oscillator (CSI). */
00601         __HAL_RCC_CSI_DISABLE();
00602 
00603         /* Get Start Tick*/
00604         tickstart = HAL_GetTick();
00605 
00606         /* Wait till CSI is disabled */
00607         while(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) != 0U)
00608         {
00609           if((HAL_GetTick() - tickstart ) > CSI_TIMEOUT_VALUE)
00610           {
00611             return HAL_TIMEOUT;
00612           }
00613         }
00614       }
00615     }
00616   }
00617   /*------------------------------ LSI Configuration -------------------------*/
00618   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
00619   {
00620     /* Check the parameters */
00621     assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
00622 
00623     /* Check the LSI State */
00624     if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
00625     {
00626       /* Enable the Internal Low Speed oscillator (LSI). */
00627       __HAL_RCC_LSI_ENABLE();
00628 
00629       /* Get Start Tick*/
00630       tickstart = HAL_GetTick();
00631 
00632       /* Wait till LSI is ready */
00633       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == 0U)
00634       {
00635         if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
00636         {
00637           return HAL_TIMEOUT;
00638         }
00639       }
00640     }
00641     else
00642     {
00643       /* Disable the Internal Low Speed oscillator (LSI). */
00644       __HAL_RCC_LSI_DISABLE();
00645 
00646       /* Get Start Tick*/
00647       tickstart = HAL_GetTick();
00648 
00649       /* Wait till LSI is ready */
00650       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != 0U)
00651       {
00652         if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
00653         {
00654           return HAL_TIMEOUT;
00655         }
00656       }
00657     }
00658   }
00659 
00660   /*------------------------------ HSI48 Configuration -------------------------*/
00661   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
00662   {
00663     /* Check the parameters */
00664     assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
00665 
00666     /* Check the HSI48 State */
00667     if((RCC_OscInitStruct->HSI48State)!= RCC_HSI48_OFF)
00668     {
00669       /* Enable the Internal Low Speed oscillator (HSI48). */
00670       __HAL_RCC_HSI48_ENABLE();
00671 
00672       /* Get time-out */
00673       tickstart = HAL_GetTick();
00674 
00675       /* Wait till HSI48 is ready */
00676       while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) == 0U)
00677       {
00678         if((HAL_GetTick() - tickstart ) > HSI48_TIMEOUT_VALUE)
00679         {
00680           return HAL_TIMEOUT;
00681         }
00682       }
00683     }
00684     else
00685     {
00686       /* Disable the Internal Low Speed oscillator (HSI48). */
00687       __HAL_RCC_HSI48_DISABLE();
00688 
00689       /* Get time-out */
00690       tickstart = HAL_GetTick();
00691 
00692       /* Wait till HSI48 is ready */
00693       while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) != 0U)
00694       {
00695         if((HAL_GetTick() - tickstart ) > HSI48_TIMEOUT_VALUE)
00696         {
00697           return HAL_TIMEOUT;
00698         }
00699       }
00700     }
00701   }
00702   /*------------------------------ LSE Configuration -------------------------*/
00703   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
00704   {
00705     /* Check the parameters */
00706     assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
00707 
00708     /* Enable write access to Backup domain */
00709     PWR->CR1 |= PWR_CR1_DBP;
00710 
00711     /* Wait for Backup domain Write protection disable */
00712     tickstart = HAL_GetTick();
00713 
00714     while((PWR->CR1 & PWR_CR1_DBP) == 0U)
00715     {
00716       if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
00717       {
00718         return HAL_TIMEOUT;
00719       }
00720     }
00721 
00722     /* Set the new LSE configuration -----------------------------------------*/
00723     __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
00724     /* Check the LSE State */
00725     if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
00726     {
00727       /* Get Start Tick*/
00728       tickstart = HAL_GetTick();
00729 
00730       /* Wait till LSE is ready */
00731       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == 0U)
00732       {
00733         if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
00734         {
00735           return HAL_TIMEOUT;
00736         }
00737       }
00738     }
00739     else
00740     {
00741       /* Get Start Tick*/
00742       tickstart = HAL_GetTick();
00743 
00744       /* Wait till LSE is disabled */
00745       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != 0U)
00746       {
00747         if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
00748         {
00749           return HAL_TIMEOUT;
00750         }
00751       }
00752     }
00753   }
00754   /*-------------------------------- PLL Configuration -----------------------*/
00755   /* Check the parameters */
00756   assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
00757   if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
00758   {
00759     /* Check if the PLL is used as system clock or not */
00760     if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL1)
00761     {
00762       if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
00763       {
00764         /* Check the parameters */
00765         assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
00766         assert_param(IS_RCC_PLLRGE_VALUE(RCC_OscInitStruct->PLL.PLLRGE));
00767         assert_param(IS_RCC_PLLVCO_VALUE(RCC_OscInitStruct->PLL.PLLVCOSEL));
00768         assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
00769         assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
00770         assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
00771         assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
00772         assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
00773         assert_param(IS_RCC_PLLFRACN_VALUE(RCC_OscInitStruct->PLL.PLLFRACN));
00774 
00775         /* Disable the main PLL. */
00776         __HAL_RCC_PLL_DISABLE();
00777 
00778         /* Get Start Tick*/
00779         tickstart = HAL_GetTick();
00780 
00781         /* Wait till PLL is disabled */
00782         while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U)
00783         {
00784           if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
00785           {
00786             return HAL_TIMEOUT;
00787           }
00788         }
00789 
00790         /* Configure the main PLL clock source, multiplication and division factors. */
00791         __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
00792                              RCC_OscInitStruct->PLL.PLLM,
00793                              RCC_OscInitStruct->PLL.PLLN,
00794                              RCC_OscInitStruct->PLL.PLLP,
00795                              RCC_OscInitStruct->PLL.PLLQ,
00796                              RCC_OscInitStruct->PLL.PLLR);
00797 
00798          /* Disable PLLFRACN . */
00799          __HAL_RCC_PLLFRACN_DISABLE();
00800 
00801          /* Configure PLL PLL1FRACN */
00802          __HAL_RCC_PLLFRACN_CONFIG(RCC_OscInitStruct->PLL.PLLFRACN);
00803 
00804         /* Select PLL1 input reference frequency range: VCI */
00805         __HAL_RCC_PLL_VCIRANGE(RCC_OscInitStruct->PLL.PLLRGE) ;
00806 
00807         /* Select PLL1 output frequency range : VCO */
00808         __HAL_RCC_PLL_VCORANGE(RCC_OscInitStruct->PLL.PLLVCOSEL) ;
00809 
00810         /* Enable PLL System Clock output. */
00811          __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVP);
00812 
00813         /* Enable PLL1Q Clock output. */
00814          __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVQ);
00815 
00816         /* Enable PLL1R  Clock output. */
00817          __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVR);
00818 
00819         /* Enable PLL1FRACN . */
00820          __HAL_RCC_PLLFRACN_ENABLE();
00821 
00822         /* Enable the main PLL. */
00823         __HAL_RCC_PLL_ENABLE();
00824 
00825         /* Get Start Tick*/
00826         tickstart = HAL_GetTick();
00827 
00828         /* Wait till PLL is ready */
00829         while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U)
00830         {
00831           if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
00832           {
00833             return HAL_TIMEOUT;
00834           }
00835         }
00836       }
00837       else
00838       {
00839         /* Disable the main PLL. */
00840         __HAL_RCC_PLL_DISABLE();
00841 
00842         /* Get Start Tick*/
00843         tickstart = HAL_GetTick();
00844 
00845         /* Wait till PLL is disabled */
00846         while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U)
00847         {
00848           if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
00849           {
00850             return HAL_TIMEOUT;
00851           }
00852         }
00853       }
00854     }
00855     else
00856     {
00857       /* Do not return HAL_ERROR if request repeats the current configuration */
00858       temp1_pllckcfg = RCC->PLLCKSELR;
00859       temp2_pllckcfg = RCC->PLL1DIVR;
00860       if(((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF) ||
00861          (READ_BIT(temp1_pllckcfg, RCC_PLLCKSELR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
00862          ((READ_BIT(temp1_pllckcfg, RCC_PLLCKSELR_DIVM1) >> RCC_PLLCKSELR_DIVM1_Pos) != RCC_OscInitStruct->PLL.PLLM) ||
00863          (READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_N1) != (RCC_OscInitStruct->PLL.PLLN - 1U)) ||
00864          ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_P1) >> RCC_PLL1DIVR_P1_Pos) != (RCC_OscInitStruct->PLL.PLLP - 1U)) ||
00865          ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_Q1) >> RCC_PLL1DIVR_Q1_Pos) != (RCC_OscInitStruct->PLL.PLLQ - 1U)) ||
00866          ((READ_BIT(temp2_pllckcfg, RCC_PLL1DIVR_R1) >> RCC_PLL1DIVR_R1_Pos) != (RCC_OscInitStruct->PLL.PLLR - 1U)))
00867       {
00868         return HAL_ERROR;
00869       }
00870     }
00871   }
00872   return HAL_OK;
00873 }
00874 
00875 /**
00876   * @brief  Initializes the CPU, AHB and APB buses clocks according to the specified
00877   *         parameters in the RCC_ClkInitStruct.
00878   * @param  RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
00879   *         contains the configuration information for the RCC peripheral.
00880   * @param  FLatency: FLASH Latency, this parameter depend on device selected
00881   *
00882   * @note   The SystemCoreClock CMSIS variable is used to store System Core Clock Frequency
00883   *         and updated by HAL_InitTick() function called within this function
00884   *
00885   * @note   The HSI is used (enabled by hardware) as system clock source after
00886   *         start-up from Reset, wake-up from STOP and STANDBY mode, or in case
00887   *         of failure of the HSE used directly or indirectly as system clock
00888   *         (if the Clock Security System CSS is enabled).
00889   *
00890   * @note   A switch from one clock source to another occurs only if the target
00891   *         clock source is ready (clock stable after start-up delay or PLL locked).
00892   *         If a clock source which is not yet ready is selected, the switch will
00893   *         occur when the clock source will be ready.
00894   *         You can use HAL_RCC_GetClockConfig() function to know which clock is
00895   *         currently used as system clock source.
00896   * @note   Depending on the device voltage range, the software has to set correctly
00897   *         D1CPRE[3:0] bits to ensure that  Domain1 core clock not exceed the maximum allowed frequency
00898   *         (for more details refer to section above "Initialization/de-initialization functions")
00899   * @retval None
00900   */
00901 HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef  *RCC_ClkInitStruct, uint32_t FLatency)
00902 {
00903   HAL_StatusTypeDef halstatus;
00904   uint32_t tickstart;
00905   uint32_t common_system_clock;
00906 
00907    /* Check Null pointer */
00908   if(RCC_ClkInitStruct == NULL)
00909   {
00910     return HAL_ERROR;
00911   }
00912 
00913   /* Check the parameters */
00914   assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
00915   assert_param(IS_FLASH_LATENCY(FLatency));
00916 
00917   /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
00918     must be correctly programmed according to the frequency of the CPU clock
00919     (HCLK) and the supply voltage of the device. */
00920 
00921   /* Increasing the CPU frequency */
00922   if(FLatency > __HAL_FLASH_GET_LATENCY())
00923   {
00924     /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
00925     __HAL_FLASH_SET_LATENCY(FLatency);
00926 
00927     /* Check that the new number of wait states is taken into account to access the Flash
00928     memory by reading the FLASH_ACR register */
00929     if(__HAL_FLASH_GET_LATENCY() != FLatency)
00930     {
00931       return HAL_ERROR;
00932     }
00933 
00934   }
00935 
00936   /* Increasing the BUS frequency divider */
00937   /*-------------------------- D1PCLK1/CDPCLK1 Configuration ---------------------------*/
00938   if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D1PCLK1) == RCC_CLOCKTYPE_D1PCLK1)
00939   {
00940 #if defined (RCC_D1CFGR_D1PPRE)
00941     if((RCC_ClkInitStruct->APB3CLKDivider) > (RCC->D1CFGR & RCC_D1CFGR_D1PPRE))
00942     {
00943       assert_param(IS_RCC_D1PCLK1(RCC_ClkInitStruct->APB3CLKDivider));
00944       MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1PPRE, RCC_ClkInitStruct->APB3CLKDivider);
00945     }
00946 #else
00947     if((RCC_ClkInitStruct->APB3CLKDivider) > (RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE))
00948     {
00949       assert_param(IS_RCC_CDPCLK1(RCC_ClkInitStruct->APB3CLKDivider));
00950       MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDPPRE, RCC_ClkInitStruct->APB3CLKDivider);
00951     }
00952 #endif
00953   }
00954 
00955   /*-------------------------- PCLK1 Configuration ---------------------------*/
00956   if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
00957   {
00958 #if defined (RCC_D2CFGR_D2PPRE1)
00959     if((RCC_ClkInitStruct->APB1CLKDivider) > (RCC->D2CFGR & RCC_D2CFGR_D2PPRE1))
00960     {
00961       assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
00962       MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
00963     }
00964 #else
00965     if((RCC_ClkInitStruct->APB1CLKDivider) > (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1))
00966     {
00967       assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
00968       MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
00969   }
00970 #endif
00971     }
00972   /*-------------------------- PCLK2 Configuration ---------------------------*/
00973   if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
00974   {
00975 #if defined(RCC_D2CFGR_D2PPRE2)
00976     if((RCC_ClkInitStruct->APB2CLKDivider) > (RCC->D2CFGR & RCC_D2CFGR_D2PPRE2))
00977     {
00978       assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
00979       MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
00980     }
00981 #else
00982      if((RCC_ClkInitStruct->APB2CLKDivider) > (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2))
00983     {
00984       assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
00985       MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
00986     }
00987 #endif
00988   }
00989 
00990   /*-------------------------- D3PCLK1 Configuration ---------------------------*/
00991   if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D3PCLK1) == RCC_CLOCKTYPE_D3PCLK1)
00992   {
00993 #if defined(RCC_D3CFGR_D3PPRE)
00994     if((RCC_ClkInitStruct->APB4CLKDivider) > (RCC->D3CFGR & RCC_D3CFGR_D3PPRE))
00995     {
00996       assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
00997       MODIFY_REG(RCC->D3CFGR, RCC_D3CFGR_D3PPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
00998     }
00999 #else
01000     if((RCC_ClkInitStruct->APB4CLKDivider) > (RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE))
01001     {
01002       assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
01003       MODIFY_REG(RCC->SRDCFGR, RCC_SRDCFGR_SRDPPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
01004     }
01005 #endif
01006   }
01007 
01008    /*-------------------------- HCLK Configuration --------------------------*/
01009   if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
01010   {
01011 #if defined (RCC_D1CFGR_HPRE)
01012     if((RCC_ClkInitStruct->AHBCLKDivider) > (RCC->D1CFGR & RCC_D1CFGR_HPRE))
01013     {
01014       /* Set the new HCLK clock divider */
01015       assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
01016       MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
01017     }
01018 #else
01019         if((RCC_ClkInitStruct->AHBCLKDivider) > (RCC->CDCFGR1 & RCC_CDCFGR1_HPRE))
01020     {
01021       /* Set the new HCLK clock divider */
01022       assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
01023       MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
01024     }
01025 #endif
01026   }
01027 
01028     /*------------------------- SYSCLK Configuration -------------------------*/
01029     if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
01030     {
01031       assert_param(IS_RCC_SYSCLK(RCC_ClkInitStruct->SYSCLKDivider));
01032       assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
01033 #if defined(RCC_D1CFGR_D1CPRE)
01034       MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1CPRE, RCC_ClkInitStruct->SYSCLKDivider);
01035 #else
01036       MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDCPRE, RCC_ClkInitStruct->SYSCLKDivider);
01037 #endif
01038       /* HSE is selected as System Clock Source */
01039       if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
01040       {
01041         /* Check the HSE ready flag */
01042         if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U)
01043         {
01044           return HAL_ERROR;
01045         }
01046       }
01047       /* PLL is selected as System Clock Source */
01048       else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
01049       {
01050         /* Check the PLL ready flag */
01051         if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U)
01052         {
01053           return HAL_ERROR;
01054         }
01055       }
01056       /* CSI is selected as System Clock Source */
01057       else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_CSI)
01058       {
01059         /* Check the PLL ready flag */
01060         if(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) == 0U)
01061         {
01062           return HAL_ERROR;
01063         }
01064       }
01065       /* HSI is selected as System Clock Source */
01066       else
01067       {
01068         /* Check the HSI ready flag */
01069         if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
01070         {
01071           return HAL_ERROR;
01072         }
01073       }
01074       MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
01075 
01076       /* Get Start Tick*/
01077       tickstart = HAL_GetTick();
01078 
01079         while (__HAL_RCC_GET_SYSCLK_SOURCE() !=  (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
01080         {
01081           if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
01082           {
01083             return HAL_TIMEOUT;
01084           }
01085         }
01086 
01087     }
01088 
01089     /* Decreasing the BUS frequency divider */
01090    /*-------------------------- HCLK Configuration --------------------------*/
01091   if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
01092   {
01093 #if defined(RCC_D1CFGR_HPRE)
01094     if((RCC_ClkInitStruct->AHBCLKDivider) < (RCC->D1CFGR & RCC_D1CFGR_HPRE))
01095     {
01096       /* Set the new HCLK clock divider */
01097       assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
01098       MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
01099     }
01100 #else
01101     if((RCC_ClkInitStruct->AHBCLKDivider) < (RCC->CDCFGR1 & RCC_CDCFGR1_HPRE))
01102     {
01103       /* Set the new HCLK clock divider */
01104       assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
01105       MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
01106     }
01107 #endif
01108   }
01109 
01110   /* Decreasing the number of wait states because of lower CPU frequency */
01111   if(FLatency < __HAL_FLASH_GET_LATENCY())
01112   {
01113     /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
01114     __HAL_FLASH_SET_LATENCY(FLatency);
01115 
01116     /* Check that the new number of wait states is taken into account to access the Flash
01117     memory by reading the FLASH_ACR register */
01118     if(__HAL_FLASH_GET_LATENCY() != FLatency)
01119     {
01120       return HAL_ERROR;
01121     }
01122  }
01123 
01124   /*-------------------------- D1PCLK1/CDPCLK Configuration ---------------------------*/
01125  if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D1PCLK1) == RCC_CLOCKTYPE_D1PCLK1)
01126  {
01127 #if defined(RCC_D1CFGR_D1PPRE)
01128    if((RCC_ClkInitStruct->APB3CLKDivider) < (RCC->D1CFGR & RCC_D1CFGR_D1PPRE))
01129    {
01130      assert_param(IS_RCC_D1PCLK1(RCC_ClkInitStruct->APB3CLKDivider));
01131      MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1PPRE, RCC_ClkInitStruct->APB3CLKDivider);
01132    }
01133 #else
01134    if((RCC_ClkInitStruct->APB3CLKDivider) < (RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE))
01135    {
01136      assert_param(IS_RCC_CDPCLK1(RCC_ClkInitStruct->APB3CLKDivider));
01137      MODIFY_REG(RCC->CDCFGR1, RCC_CDCFGR1_CDPPRE, RCC_ClkInitStruct->APB3CLKDivider);
01138    }
01139 #endif
01140  }
01141 
01142   /*-------------------------- PCLK1 Configuration ---------------------------*/
01143  if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
01144  {
01145 #if defined(RCC_D2CFGR_D2PPRE1)
01146    if((RCC_ClkInitStruct->APB1CLKDivider) < (RCC->D2CFGR & RCC_D2CFGR_D2PPRE1))
01147    {
01148      assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
01149      MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
01150    }
01151 #else
01152    if((RCC_ClkInitStruct->APB1CLKDivider) < (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1))
01153    {
01154      assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
01155      MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
01156    }
01157 #endif
01158  }
01159 
01160   /*-------------------------- PCLK2 Configuration ---------------------------*/
01161  if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
01162  {
01163 #if defined (RCC_D2CFGR_D2PPRE2)
01164    if((RCC_ClkInitStruct->APB2CLKDivider) < (RCC->D2CFGR & RCC_D2CFGR_D2PPRE2))
01165    {
01166      assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
01167      MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
01168    }
01169 #else
01170    if((RCC_ClkInitStruct->APB2CLKDivider) < (RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2))
01171    {
01172      assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
01173      MODIFY_REG(RCC->CDCFGR2, RCC_CDCFGR2_CDPPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
01174    }
01175 #endif
01176  }
01177 
01178   /*-------------------------- D3PCLK1/SRDPCLK1 Configuration ---------------------------*/
01179  if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D3PCLK1) == RCC_CLOCKTYPE_D3PCLK1)
01180  {
01181 #if defined(RCC_D3CFGR_D3PPRE)
01182    if((RCC_ClkInitStruct->APB4CLKDivider) < (RCC->D3CFGR & RCC_D3CFGR_D3PPRE))
01183    {
01184      assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
01185      MODIFY_REG(RCC->D3CFGR, RCC_D3CFGR_D3PPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
01186    }
01187 #else
01188    if((RCC_ClkInitStruct->APB4CLKDivider) < (RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE))
01189    {
01190      assert_param(IS_RCC_SRDPCLK1(RCC_ClkInitStruct->APB4CLKDivider));
01191      MODIFY_REG(RCC->SRDCFGR, RCC_SRDCFGR_SRDPPRE, (RCC_ClkInitStruct->APB4CLKDivider) );
01192    }
01193 #endif
01194  }
01195 
01196   /* Update the SystemCoreClock global variable */
01197 #if defined(RCC_D1CFGR_D1CPRE)
01198   common_system_clock = HAL_RCC_GetSysClockFreq() >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> RCC_D1CFGR_D1CPRE_Pos]) & 0x1FU);
01199 #else
01200   common_system_clock = HAL_RCC_GetSysClockFreq() >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE)>> RCC_CDCFGR1_CDCPRE_Pos]) & 0x1FU);
01201 #endif
01202 
01203 #if defined(RCC_D1CFGR_HPRE)
01204   SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
01205 #else
01206   SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
01207 #endif
01208 
01209 #if defined(DUAL_CORE) && defined(CORE_CM4)
01210   SystemCoreClock = SystemD2Clock;
01211 #else
01212   SystemCoreClock = common_system_clock;
01213 #endif /* DUAL_CORE && CORE_CM4 */
01214 
01215   /* Configure the source of time base considering new system clocks settings*/
01216   halstatus = HAL_InitTick (uwTickPrio);
01217 
01218   return halstatus;
01219 }
01220 
01221 /**
01222   * @}
01223   */
01224 
01225 /** @defgroup RCC_Group2 Peripheral Control functions
01226  *  @brief   RCC clocks control functions
01227  *
01228 @verbatim
01229  ===============================================================================
01230                       ##### Peripheral Control functions #####
01231  ===============================================================================
01232     [..]
01233     This subsection provides a set of functions allowing to control the RCC Clocks
01234     frequencies.
01235 
01236 @endverbatim
01237   * @{
01238   */
01239 
01240 /**
01241   * @brief  Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
01242   * @note   PA8/PC9 should be configured in alternate function mode.
01243   * @param  RCC_MCOx: specifies the output direction for the clock source.
01244   *          This parameter can be one of the following values:
01245   *            @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8).
01246   *            @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9).
01247   * @param  RCC_MCOSource: specifies the clock source to output.
01248   *          This parameter can be one of the following values:
01249   *            @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source
01250   *            @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source
01251   *            @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source
01252   *            @arg RCC_MCO1SOURCE_PLL1QCLK:  PLL1Q clock selected as MCO1 source
01253   *            @arg RCC_MCO1SOURCE_HSI48: HSI48 (48MHZ) selected as MCO1 source
01254   *            @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source
01255   *            @arg RCC_MCO2SOURCE_PLL2PCLK: PLL2P clock selected as MCO2 source
01256   *            @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source
01257   *            @arg RCC_MCO2SOURCE_PLLCLK:  PLL1P clock selected as MCO2 source
01258   *            @arg RCC_MCO2SOURCE_CSICLK:  CSI clock selected as MCO2 source
01259   *            @arg RCC_MCO2SOURCE_LSICLK:  LSI clock selected as MCO2 source
01260   * @param  RCC_MCODiv: specifies the MCOx pre-scaler.
01261   *          This parameter can be one of the following values:
01262   *            @arg RCC_MCODIV_1 up to RCC_MCODIV_15  : divider applied to MCOx clock
01263   * @retval None
01264   */
01265 void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
01266 {
01267   GPIO_InitTypeDef GPIO_InitStruct;
01268   /* Check the parameters */
01269   assert_param(IS_RCC_MCO(RCC_MCOx));
01270   assert_param(IS_RCC_MCODIV(RCC_MCODiv));
01271   /* RCC_MCO1 */
01272   if(RCC_MCOx == RCC_MCO1)
01273   {
01274     assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
01275 
01276     /* MCO1 Clock Enable */
01277     MCO1_CLK_ENABLE();
01278 
01279     /* Configure the MCO1 pin in alternate function mode */
01280     GPIO_InitStruct.Pin = MCO1_PIN;
01281     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
01282     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
01283     GPIO_InitStruct.Pull = GPIO_NOPULL;
01284     GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
01285     HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
01286 
01287     /* Mask MCO1 and MCO1PRE[3:0] bits then Select MCO1 clock source and pre-scaler */
01288     MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));
01289   }
01290   else
01291   {
01292     assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
01293 
01294     /* MCO2 Clock Enable */
01295     MCO2_CLK_ENABLE();
01296 
01297     /* Configure the MCO2 pin in alternate function mode */
01298     GPIO_InitStruct.Pin = MCO2_PIN;
01299     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
01300     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
01301     GPIO_InitStruct.Pull = GPIO_NOPULL;
01302     GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
01303     HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
01304 
01305     /* Mask MCO2 and MCO2PRE[3:0] bits then Select MCO2 clock source and pre-scaler */
01306     MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 7U)));
01307   }
01308 }
01309 
01310 /**
01311   * @brief  Enables the Clock Security System.
01312   * @note   If a failure is detected on the HSE oscillator clock, this oscillator
01313   *         is automatically disabled and an interrupt is generated to inform the
01314   *         software about the failure (Clock Security System Interrupt, CSSI),
01315   *         allowing the MCU to perform rescue operations. The CSSI is linked to
01316   *         the Cortex-M NMI (Non-Mask-able Interrupt) exception vector.
01317   * @retval None
01318   */
01319 void HAL_RCC_EnableCSS(void)
01320 {
01321   SET_BIT(RCC->CR, RCC_CR_CSSHSEON) ;
01322 }
01323 
01324 /**
01325   * @brief  Disables the Clock Security System.
01326   * @retval None
01327   */
01328 void HAL_RCC_DisableCSS(void)
01329 {
01330   CLEAR_BIT(RCC->CR, RCC_CR_CSSHSEON);
01331 }
01332 
01333 /**
01334   * @brief  Returns the SYSCLK frequency
01335   *
01336   * @note   The system frequency computed by this function is not the real
01337   *         frequency in the chip. It is calculated based on the predefined
01338   *         constant and the selected clock source:
01339   * @note     If SYSCLK source is CSI, function returns values based on CSI_VALUE(*)
01340   * @note     If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
01341   * @note     If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
01342   * @note     If SYSCLK source is PLL, function returns values based on CSI_VALUE(*),
01343   *           HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
01344   * @note     (*) CSI_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
01345   *               4 MHz) but the real value may vary depending on the variations
01346   *               in voltage and temperature.
01347   * @note     (**) HSI_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
01348   *               64 MHz) but the real value may vary depending on the variations
01349   *               in voltage and temperature.
01350   * @note     (***) HSE_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
01351   *                25 MHz), user has to ensure that HSE_VALUE is same as the real
01352   *                frequency of the crystal used. Otherwise, this function may
01353   *                have wrong result.
01354   *
01355   * @note   The result of this function could be not correct when using fractional
01356   *         value for HSE crystal.
01357   *
01358   * @note   This function can be used by the user application to compute the
01359   *         baud rate for the communication peripherals or configure other parameters.
01360   *
01361   * @note   Each time SYSCLK changes, this function must be called to update the
01362   *         right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
01363   *
01364   *
01365   * @retval SYSCLK frequency
01366   */
01367 uint32_t HAL_RCC_GetSysClockFreq(void)
01368 {
01369   uint32_t pllp, pllsource, pllm, pllfracen, hsivalue;
01370   float_t fracn1, pllvco;
01371   uint32_t sysclockfreq;
01372 
01373   /* Get SYSCLK source -------------------------------------------------------*/
01374 
01375   switch (RCC->CFGR & RCC_CFGR_SWS)
01376   {
01377   case RCC_CFGR_SWS_HSI:  /* HSI used as system clock source */
01378 
01379    if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
01380       {
01381         sysclockfreq = (uint32_t) (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER()>> 3));
01382       }
01383       else
01384       {
01385         sysclockfreq = (uint32_t) HSI_VALUE;
01386       }
01387 
01388     break;
01389 
01390   case RCC_CFGR_SWS_CSI:  /* CSI used as system clock  source */
01391     sysclockfreq = CSI_VALUE;
01392     break;
01393 
01394   case RCC_CFGR_SWS_HSE:  /* HSE used as system clock  source */
01395     sysclockfreq = HSE_VALUE;
01396     break;
01397 
01398   case RCC_CFGR_SWS_PLL1:  /* PLL1 used as system clock  source */
01399 
01400     /* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
01401     SYSCLK = PLL_VCO / PLLR
01402     */
01403     pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
01404     pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> 4)  ;
01405     pllfracen = ((RCC-> PLLCFGR & RCC_PLLCFGR_PLL1FRACEN)>>RCC_PLLCFGR_PLL1FRACEN_Pos);
01406     fracn1 = (float_t)(uint32_t)(pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1)>> 3));
01407 
01408     if (pllm != 0U)
01409     {
01410       switch (pllsource)
01411       {
01412       case RCC_PLLSOURCE_HSI:  /* HSI used as PLL clock source */
01413 
01414        if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
01415         {
01416           hsivalue= (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER()>> 3));
01417           pllvco = ( (float_t)hsivalue / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
01418         }
01419         else
01420         {
01421           pllvco = ((float_t)HSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
01422         }
01423         break;
01424 
01425       case RCC_PLLSOURCE_CSI:  /* CSI used as PLL clock source */
01426         pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
01427         break;
01428 
01429       case RCC_PLLSOURCE_HSE:  /* HSE used as PLL clock source */
01430         pllvco = ((float_t)HSE_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
01431         break;
01432 
01433       default:
01434         pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
01435         break;
01436       }
01437       pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >>9) + 1U ) ;
01438       sysclockfreq =  (uint32_t)(float_t)(pllvco/(float_t)pllp);
01439     }
01440     else
01441     {
01442       sysclockfreq = 0U;
01443     }
01444     break;
01445 
01446   default:
01447     sysclockfreq = CSI_VALUE;
01448     break;
01449   }
01450 
01451   return sysclockfreq;
01452 }
01453 
01454 
01455 /**
01456   * @brief  Returns the HCLK frequency
01457   * @note   Each time HCLK changes, this function must be called to update the
01458   *         right HCLK value. Otherwise, any configuration based on this function will be incorrect.
01459   *
01460   * @note   The SystemD2Clock CMSIS variable is used to store System domain2 Clock Frequency
01461   *         and updated within this function
01462   * @retval HCLK frequency
01463   */
01464 uint32_t HAL_RCC_GetHCLKFreq(void)
01465 {
01466 uint32_t common_system_clock;
01467 
01468 #if defined(RCC_D1CFGR_D1CPRE)
01469   common_system_clock = HAL_RCC_GetSysClockFreq() >> (D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> RCC_D1CFGR_D1CPRE_Pos] & 0x1FU);
01470 #else
01471   common_system_clock = HAL_RCC_GetSysClockFreq() >> (D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE)>> RCC_CDCFGR1_CDCPRE_Pos] & 0x1FU);
01472 #endif
01473 
01474 #if defined(RCC_D1CFGR_HPRE)
01475   SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
01476 #else
01477   SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
01478 #endif
01479 
01480 #if defined(DUAL_CORE) && defined(CORE_CM4)
01481   SystemCoreClock = SystemD2Clock;
01482 #else
01483   SystemCoreClock = common_system_clock;
01484 #endif /* DUAL_CORE && CORE_CM4 */
01485 
01486   return SystemD2Clock;
01487 }
01488 
01489 
01490 /**
01491   * @brief  Returns the PCLK1 frequency
01492   * @note   Each time PCLK1 changes, this function must be called to update the
01493   *         right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
01494   * @retval PCLK1 frequency
01495   */
01496 uint32_t HAL_RCC_GetPCLK1Freq(void)
01497 {
01498 #if defined (RCC_D2CFGR_D2PPRE1)
01499   /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
01500   return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->D2CFGR & RCC_D2CFGR_D2PPRE1)>> RCC_D2CFGR_D2PPRE1_Pos]) & 0x1FU));
01501 #else
01502  /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
01503   return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1)>> RCC_CDCFGR2_CDPPRE1_Pos]) & 0x1FU));
01504 #endif
01505 }
01506 
01507 
01508 /**
01509   * @brief  Returns the PCLK2 frequency
01510   * @note   Each time PCLK2 changes, this function must be called to update the
01511   *         right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
01512   * @retval PCLK1 frequency
01513   */
01514 uint32_t HAL_RCC_GetPCLK2Freq(void)
01515 {
01516   /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
01517 #if defined(RCC_D2CFGR_D2PPRE2)
01518   return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->D2CFGR & RCC_D2CFGR_D2PPRE2)>> RCC_D2CFGR_D2PPRE2_Pos]) & 0x1FU));
01519 #else
01520   return (HAL_RCC_GetHCLKFreq() >> ((D1CorePrescTable[(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2)>> RCC_CDCFGR2_CDPPRE2_Pos]) & 0x1FU));
01521 #endif
01522 }
01523 
01524 /**
01525   * @brief  Configures the RCC_OscInitStruct according to the internal
01526   * RCC configuration registers.
01527   * @param  RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
01528   * will be configured.
01529   * @retval None
01530   */
01531 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef  *RCC_OscInitStruct)
01532 {
01533   /* Set all possible values for the Oscillator type parameter ---------------*/
01534   RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_CSI | \
01535                                       RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI| RCC_OSCILLATORTYPE_HSI48;
01536 
01537   /* Get the HSE configuration -----------------------------------------------*/
01538 #if defined(RCC_CR_HSEEXT)
01539   if((RCC->CR &(RCC_CR_HSEBYP | RCC_CR_HSEEXT)) == RCC_CR_HSEBYP)
01540   {
01541     RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
01542   }
01543   else if((RCC->CR &(RCC_CR_HSEBYP | RCC_CR_HSEEXT)) == (RCC_CR_HSEBYP | RCC_CR_HSEEXT))
01544   {
01545     RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS_DIGITAL;
01546   }
01547   else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
01548   {
01549     RCC_OscInitStruct->HSEState = RCC_HSE_ON;
01550   }
01551   else
01552   {
01553     RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
01554   }
01555 #else
01556   if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
01557   {
01558     RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
01559   }
01560   else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
01561   {
01562     RCC_OscInitStruct->HSEState = RCC_HSE_ON;
01563   }
01564   else
01565   {
01566     RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
01567   }
01568 #endif /* RCC_CR_HSEEXT */
01569 
01570    /* Get the CSI configuration -----------------------------------------------*/
01571   if((RCC->CR &RCC_CR_CSION) == RCC_CR_CSION)
01572   {
01573     RCC_OscInitStruct->CSIState = RCC_CSI_ON;
01574   }
01575   else
01576   {
01577     RCC_OscInitStruct->CSIState = RCC_CSI_OFF;
01578   }
01579 
01580 #if defined(RCC_VER_X)
01581   if(HAL_GetREVID() <= REV_ID_Y)
01582   {
01583     RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, HAL_RCC_REV_Y_CSITRIM_Msk) >> HAL_RCC_REV_Y_CSITRIM_Pos);
01584   }
01585   else
01586   {
01587     RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->CSICFGR, RCC_CSICFGR_CSITRIM) >> RCC_CSICFGR_CSITRIM_Pos);
01588   }
01589 #else
01590  RCC_OscInitStruct->CSICalibrationValue = (uint32_t)(READ_BIT(RCC->CSICFGR, RCC_CSICFGR_CSITRIM) >> RCC_CSICFGR_CSITRIM_Pos);
01591 #endif /*RCC_VER_X*/
01592 
01593   /* Get the HSI configuration -----------------------------------------------*/
01594   if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
01595   {
01596     RCC_OscInitStruct->HSIState = RCC_HSI_ON;
01597   }
01598   else
01599   {
01600     RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
01601   }
01602 
01603 #if defined(RCC_VER_X)
01604   if(HAL_GetREVID() <= REV_ID_Y)
01605   {
01606     RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, HAL_RCC_REV_Y_HSITRIM_Msk) >> HAL_RCC_REV_Y_HSITRIM_Pos);
01607   }
01608   else
01609   {
01610     RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM) >> RCC_HSICFGR_HSITRIM_Pos);
01611   }
01612 #else
01613     RCC_OscInitStruct->HSICalibrationValue = (uint32_t)(READ_BIT(RCC->HSICFGR, RCC_HSICFGR_HSITRIM) >> RCC_HSICFGR_HSITRIM_Pos);
01614 #endif /*RCC_VER_X*/
01615 
01616   /* Get the LSE configuration -----------------------------------------------*/
01617 #if defined(RCC_BDCR_LSEEXT)
01618   if((RCC->BDCR &(RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT)) == RCC_BDCR_LSEBYP)
01619   {
01620     RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
01621   }
01622   else if((RCC->BDCR &(RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT)) == (RCC_BDCR_LSEBYP|RCC_BDCR_LSEEXT))
01623   {
01624     RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS_DIGITAL;
01625   }
01626   else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
01627   {
01628     RCC_OscInitStruct->LSEState = RCC_LSE_ON;
01629   }
01630   else
01631   {
01632     RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
01633   }
01634 #else
01635   if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
01636   {
01637     RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
01638   }
01639   else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
01640   {
01641     RCC_OscInitStruct->LSEState = RCC_LSE_ON;
01642   }
01643   else
01644   {
01645     RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
01646   }
01647 #endif /* RCC_BDCR_LSEEXT */
01648 
01649   /* Get the LSI configuration -----------------------------------------------*/
01650   if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
01651   {
01652     RCC_OscInitStruct->LSIState = RCC_LSI_ON;
01653   }
01654   else
01655   {
01656     RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
01657   }
01658 
01659   /* Get the HSI48 configuration ---------------------------------------------*/
01660   if((RCC->CR & RCC_CR_HSI48ON) == RCC_CR_HSI48ON)
01661   {
01662     RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
01663   }
01664   else
01665   {
01666     RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
01667   }
01668 
01669   /* Get the PLL configuration -----------------------------------------------*/
01670   if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
01671   {
01672     RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
01673   }
01674   else
01675   {
01676     RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
01677   }
01678   RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
01679   RCC_OscInitStruct->PLL.PLLM = (uint32_t)((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> RCC_PLLCKSELR_DIVM1_Pos);
01680   RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) >> RCC_PLL1DIVR_N1_Pos)+ 1U;
01681   RCC_OscInitStruct->PLL.PLLR = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_R1) >> RCC_PLL1DIVR_R1_Pos)+ 1U;
01682   RCC_OscInitStruct->PLL.PLLP = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >> RCC_PLL1DIVR_P1_Pos)+ 1U;
01683   RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_Q1) >> RCC_PLL1DIVR_Q1_Pos)+ 1U;
01684   RCC_OscInitStruct->PLL.PLLRGE = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLL1RGE));
01685   RCC_OscInitStruct->PLL.PLLVCOSEL = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLL1VCOSEL) >> RCC_PLLCFGR_PLL1VCOSEL_Pos);
01686   RCC_OscInitStruct->PLL.PLLFRACN = (uint32_t)(((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1) >> RCC_PLL1FRACR_FRACN1_Pos));
01687 }
01688 
01689 /**
01690   * @brief  Configures the RCC_ClkInitStruct according to the internal
01691   * RCC configuration registers.
01692   * @param  RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that
01693   * will be configured.
01694   * @param  pFLatency: Pointer on the Flash Latency.
01695   * @retval None
01696   */
01697 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef  *RCC_ClkInitStruct, uint32_t *pFLatency)
01698 {
01699   /* Set all possible values for the Clock type parameter --------------------*/
01700   RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 |
01701                                  RCC_CLOCKTYPE_PCLK2 |  RCC_CLOCKTYPE_D3PCLK1  ;
01702 
01703   /* Get the SYSCLK configuration --------------------------------------------*/
01704   RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
01705 
01706 #if defined(RCC_D1CFGR_D1CPRE)
01707   /* Get the SYSCLK configuration ----------------------------------------------*/
01708   RCC_ClkInitStruct->SYSCLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_D1CPRE);
01709 
01710   /* Get the D1HCLK configuration ----------------------------------------------*/
01711   RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_HPRE);
01712 
01713   /* Get the APB3 configuration ----------------------------------------------*/
01714   RCC_ClkInitStruct->APB3CLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_D1PPRE);
01715 
01716   /* Get the APB1 configuration ----------------------------------------------*/
01717   RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->D2CFGR & RCC_D2CFGR_D2PPRE1);
01718 
01719   /* Get the APB2 configuration ----------------------------------------------*/
01720   RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)(RCC->D2CFGR & RCC_D2CFGR_D2PPRE2);
01721 
01722   /* Get the APB4 configuration ----------------------------------------------*/
01723   RCC_ClkInitStruct->APB4CLKDivider = (uint32_t)(RCC->D3CFGR & RCC_D3CFGR_D3PPRE);
01724 #else
01725   /* Get the SYSCLK configuration ----------------------------------------------*/
01726   RCC_ClkInitStruct->SYSCLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE);
01727 
01728   /* Get the D1HCLK configuration ----------------------------------------------*/
01729   RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE);
01730 
01731   /* Get the APB3 configuration ----------------------------------------------*/
01732   RCC_ClkInitStruct->APB3CLKDivider = (uint32_t)(RCC->CDCFGR1 & RCC_CDCFGR1_CDPPRE);
01733 
01734   /* Get the APB1 configuration ----------------------------------------------*/
01735   RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE1);
01736 
01737   /* Get the APB2 configuration ----------------------------------------------*/
01738   RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)(RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE2);
01739 
01740   /* Get the APB4 configuration ----------------------------------------------*/
01741   RCC_ClkInitStruct->APB4CLKDivider = (uint32_t)(RCC->SRDCFGR & RCC_SRDCFGR_SRDPPRE);
01742 #endif
01743 
01744   /* Get the Flash Wait State (Latency) configuration ------------------------*/
01745   *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
01746 }
01747 
01748 /**
01749   * @brief This function handles the RCC CSS interrupt request.
01750   * @note This API should be called under the NMI_Handler().
01751   * @retval None
01752   */
01753 void HAL_RCC_NMI_IRQHandler(void)
01754 {
01755   /* Check RCC CSSF flag  */
01756   if(__HAL_RCC_GET_IT(RCC_IT_CSS))
01757   {
01758     /* RCC Clock Security System interrupt user callback */
01759     HAL_RCC_CCSCallback();
01760 
01761     /* Clear RCC CSS pending bit */
01762     __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
01763   }
01764 }
01765 
01766 /**
01767   * @brief  RCC Clock Security System interrupt callback
01768   * @retval none
01769   */
01770 __weak void HAL_RCC_CCSCallback(void)
01771 {
01772   /* NOTE : This function Should not be modified, when the callback is needed,
01773             the HAL_RCC_CCSCallback could be implemented in the user file
01774    */
01775 }
01776 
01777 /**
01778   * @}
01779   */
01780 
01781 /**
01782   * @}
01783   */
01784 
01785 #endif /* HAL_RCC_MODULE_ENABLED */
01786 /**
01787   * @}
01788   */
01789 
01790 /**
01791   * @}
01792   */
01793