Write the clock configuration file yourself through the firmware library in STM32

Use HSE to configure the system clock

  • Because the STM project uses the firmware library, the clock has been configured when entering the main function, so we need to reset the RCC register first when we write the clock configuration file ourselves!
  • Enable HSE
  • Determine whether HSE is successfully started
  • enable prefetch
  • Set the FLASH wait period
  • Configure the multipliers of the 3 buses
  • Configure the phase-locked loop, enable the phase-locked loop
  • Wait for the PLL to stabilize
  • Select the PLL output as the system clock and wait for it to stabilize
void HSE_sysclock_config( uint32_t  RCC_PLLMul_x )
{
    //先复位RCC寄存器
    RCC_DeInit();

    /*使用HSE配置系统时钟*/

    //使能HSE
    RCC_HSEConfig( RCC_HSE_ON );

    //检测HSE是否启动成功
    if ( SUCCESS == RCC_WaitForHSEStartUp() )
    {
        //使能预取指,这是FLASH固件中的函数
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        //设置FLASH等待周期。  因为倍频成72M 所以等待两个周期。
        FLASH_SetLatency(FLASH_Latency_2);  

        //配置三个总线的倍频因子
        //HCLK --> AHB 最大为72M,所以只需要1分频
        RCC_HCLKConfig(RCC_SYSCLK_Div1);
        //PCLK1 --> APB1 最大为36M,所以要2分频
    RCC_PCLK1Config(RCC_HCLK_Div2);
        //PCLK2 --> APB2 最大为72M,所以只需要1分频
    RCC_PCLK2Config(RCC_HCLK_Div1);

        //先配置锁相环 PLLCLK = HSE * 倍频因子
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_x);

        //使能PLL
        RCC_PLLCmd(ENABLE);

        //等待PLL稳定
        while ( RESET == RCC_GetFlagStatus(RCC_FLAG_PLLRDY) );

        //选择系统时钟(选择锁相环输出)
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
        //等待选择稳定
        while ( 0x08 != RCC_GetSYSCLKSource() );
    }
    else
    {
        //HSE启动失败,用户自己编写补救措施
    }

}

Configuring System Clock Using HSI

  • Because the STM project uses the firmware library, the clock has been configured when entering the main function, so we need to reset the RCC register first when we write the clock configuration file ourselves!
  • Enable HSI
  • Determine whether the HSI is successfully started
  • enable prefetch
  • Set the FLASH wait period
  • Configure the multipliers of the 3 buses
  • Configure the phase-locked loop, enable the phase-locked loop
  • Wait for the PLL to stabilize
  • Select the PLL output as the system clock and wait for it to stabilize
void HSI_sysclock_config( uint32_t  RCC_PLLMul_x )
{
    __IO uint32_t HSIStatus = 0;

    //先复位RCC寄存器
    RCC_DeInit();

    /*使用HSI配置系统时钟*/

    //使能HSI
    RCC_HSICmd(ENABLE);

    //检测HSE是否启动成功
    HSIStatus = RCC->CR & RCC_CR_HSIRDY;
    if ( RCC_CR_HSIRDY == HSIStatus )
    {
        //使能预取指,这是FLASH固件中的函数
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        //设置FLASH等待周期。  因为倍频成72M 所以等待两个周期。
        FLASH_SetLatency(FLASH_Latency_2);  

        //配置三个总线的倍频因子
        //HCLK --> AHB 最大为72M,所以只需要1分频
        RCC_HCLKConfig(RCC_SYSCLK_Div1);
        //PCLK1 --> APB1 最大为36M,所以要2分频
    RCC_PCLK1Config(RCC_HCLK_Div2);
        //PCLK2 --> APB2 最大为72M,所以只需要1分频
    RCC_PCLK2Config(RCC_HCLK_Div1);

        //先配置锁相环 PLLCLK = HSE * 倍频因子
        RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_x);

        //使能PLL
        RCC_PLLCmd(ENABLE);

        //等待PLL稳定
        while ( RESET == RCC_GetFlagStatus(RCC_FLAG_PLLRDY) );

        //选择系统时钟(选择锁相环输出)
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
        //等待选择稳定
        while ( 0x08 != RCC_GetSYSCLKSource() );
    }
    else
    {
        //HSI启动失败,用户自己编写补救措施
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324480804&siteId=291194637