STM32 system clock monitoring and switching

Publisher:phi31Latest update time:2018-10-10 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the company's project some time ago, it was required to use the STM32 processor to automatically switch to the internal crystal oscillator when the external crystal oscillator is abnormal. After searching for a lot of information on the Internet, I finally found an official seminar PPT on the Internet that briefly introduced it. So I followed this idea to try to write code. I didn't expect that the idea provided by the official was quite reliable!
———————————————————I am a gorgeous dividing line——————————————————— —————————————————I am a gorgeous dividing line——————————————————— Function 1, void INIT_CLOCK(void) {    #if 1     ErrorStatus HSEStartUpStatus;     RCC_DeInit(); //Reset RCC register     RCC_HSEConfig(RCC_HSE_ON); //Set external high-speed crystal oscillator (HSE)     HSEStartUpStatus = RCC_WaitForHSEStartUp(); //Wait for HSE to start #if 1     //Judge whether the external crystal oscillator is OK. If OK, use the external crystal oscillator. If not, use the internal crystal oscillator, giving priority to the external crystal oscillator.     if(HSEStartUpStatus == SUCCESS)     {         RCC_ClockSecuritySystemCmd(ENABLE); //Start the clock security system CSS         //External crystal configuration         RCC_HCLKConfig(RCC_SYSCLK_Div1); //Set ABH clock (HCLK): HCLK=SYSCLK         //Set PLL clock source and multiplication factor, PLLCLK = HSE*PLLMul = 8*8 = 64MHz Actually: 8*8=64MHz         RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_2);         RCC_PLLCmd(ENABLE); //Enable PLL         //Check whether the specified RCC flag is set or not         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // Wait till PLL is ready         RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Set system clock (SYSCLK)         //Return the clock source used as system clock         // Wait till PLL is used as system clock source         while(RCC_GetSYSCLKSource() != 0x08);     }     else #endif     {         //Internal crystal configuration         RCC_DeInit(); //Reset RCC register         RCC_HSEConfig(RCC_HSE_OFF); //Turn off external crystal (HSE)         RCC_HCLKConfig(RCC_SYSCLK_Div1); //Set ABH clock (HCLK): HCLK=SYSCLK         //Set PLL clock source and multiplication factor, PLLCLK = HSI/2*PLLMul = 8/2*12 = 48MHz Actually: 48MHz         RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12);         RCC_PLLCmd(ENABLE); //Enable PLL RCC_SYSCLKConfig         (RCC_SYSCLKSource_PLLCLK         ); //Set system clock (SYSCLK)         //Return the clock source of the system clock         // Wait till PLL is used as system clock source         while(RCC_GetSYSCLKSource() != 0x08);     }     #endif } —————————————————I am a gorgeous dividing line———————————————————         In which of the above functions is the clock safety system started only during the external crystal oscillator startup initialization (PS: the red bold part) because only when the external crystal oscillator fails initially will it switch to the internal crystal oscillator. In this function, the external crystal oscillator is started first and the internal crystal oscillator is started only when the external crystal oscillator fails to start. Function 2, void NMIException(void) {     if(RCC_GetITStatus(RCC_IT_CSS) != RESET)     { #if 0 //Internal crystal configuration         RCC_DeInit(); //Reset RCC register         RCC_HSEConfig(RCC_HSE_OFF); //Turn off external crystal (HSE)         RCC_HCLKConfig(RCC_SYSCLK_Div1); //Set ABH clock (HCLK): HCLK=SYSCLK
[Reprint] Monitoring and switching of STM32 system clock

[Reprint] Monitoring and switching of STM32 system clock

[Reprint] Monitoring and switching of STM32 system clock

































































       //Set PLL clock source and multiplication factor, PLLCLK = HSI/2*PLLMul = 8/2*12 = 48MHz Actually: 48MHz
        RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12);
        RCC_PLLCmd(ENABLE); //Enable PLL

        //Check whether the specified RCC flag is set
        //while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Set system clock (SYSCLK)

        //Return the clock source used as system clock
        // Wait till PLL is used as system clock source
        while(RCC_GetSYSCLKSource() != 0x08);
        CLKOUT_8M_INIT(); //PA8 port clock output provides 8MHz clock for 24025RCC_ITConfig

        (RCC_IT_HSIRDY,ENABLE); //Enable HSI ready interruptRCC_ITConfig
        (RCC_IT_PLLRDY,ENABLE); //Enable PLL ready interrupt
#else
        NVIC_SystemReset(); //RestartRCC_ITConfig
        (RCC_IT_HSERDY,ENABLE); //Enable HSE ready
        interruptRCC_ITConfig(RCC_IT_PLLRDY,ENABLE); //Enable PLL ready interrupt       
#endif
        RCC_ClearITPendingBit(RCC_IT_CSS); //Clear the pending bit of the clock security system interrupt
    }
}
———————————————————I am a gorgeous dividing line—————————————————————
The NMIException() function is the crystal oscillator exception function processing function. This function is called in the exception interrupt function. The red part is the internal crystal oscillator initialization. So the red part of the code can achieve the "seamless" switch of the external crystal oscillator to the internal crystal oscillator (if it is used in specific occasions such as: medical equipment can use this code), but because our product does not have very high requirements for real-time performance, I decided to restart the device and initialize the crystal oscillator.
Function three,

void NMI_Handler(void)
{
    NMIException();
}
—————————————————I am a gorgeous dividing line———————————————————
The NMI_Handler() function is in the "stm32f0xx_it.c" in the STM32 library file. We put the crystal oscillator exception processing function in this non-shieldable exception interrupt function to achieve the equivalent of handling when the crystal oscillator is abnormal.

—————————————————I am a gorgeous dividing line———————————————————
int main(void)
{
    SystemInit();
    INIT_CLOCK(); //Initialize the system clock using the HSI clock
    for(;;)
    {
     ......
    }
}

Keywords:STM32 Reference address:STM32 system clock monitoring and switching

Previous article:STM32F103 clock configuration process
Next article:OK6410-Experimental Instruction Notes

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号