*This content has been reposted with the approval of the original author.
Introduction
For MCU, the clock is the key to the stable execution of the program. Usually, the clock source used by the microcontroller is an external crystal oscillator. If applied in some important occasions, the clock of the microcontroller receives external interference and leads to clock failure, so that the microcontroller can not run the program normally, the system is stuck, which may cause losses. If the microcontroller is used in a safe production place, it may cause personal accidents. Therefore, the security management of the clock is very important for the MCU. As it happens, in the APM32F03 Mini board I just acquired, there is a relevant function. Therefore, it is explored in the following.
Clock Security (CSS)
As the name suggests, the clock safety system manages the clock so that the MCU can run in a safe state. The clock security system is enabled in the RCM module. When the clock security system is enabled, the external high-speed clock will enable the clock security system at the same time, and then the clock security system will always monitor the state of the external high-speed clock. If the external high-speed clock fails due to external interference, the external high-speed clock will be turned off, and the clock ring will be closed and the internal high-speed clock will be used as the system clock. It can also be found in the clock tree of the reference book.

Although the clock security system can solve the problem of sudden failure of the external clock by selecting the internal clock source as the system clock, the internal clock is not stable compared with the external clock, and can not always be used as the system clock of the MCU. Therefore, we need to turn the external clock back on. The flag bit of the clock safety system suspends the NMI interrupt, so we can reconfigure the clock in the NMI interrupt.
Program writing
It can be seen from the above that when we enable CSS, the external high-speed clock fails, and the clock safety interrupt will occur. At the same time, the NMI interrupt program will be executed continuously until the clock safety interrupt is cleared. We can reconfigure the external clock in the NMI interrupt program and output the clock invalidation information. The following code is based on the APM32F030 standard library.
First we need to enable clock security:
RCM_EnableCCS();
Next, you write the NMI interrupt program. CSS turns off PLL when it turns off HSE. If we need to print information from the serial port, we need to re-select the serial clock source, and select the internal high-speed clock that CSS automatically turns on. At the same time, because the baud rate of the serial port is calculated according to the current clock, we also need to reconfigure the serial port. After the above two steps, you can print the information. The next step is to restart the external high-speed clock.
void NMI_Handler(void) { RCM_ConfigUSARTCLK(RCM_USART1CLK_HSI);USART_Config(&usartstruct); printf(“NMI”); /* The external clock is restarted/ /**/ / Clear the clock safe interrupt flag */ RCM_ClearIntFlag(RCC_IT_CSS);}
It is important to note that the flag of the clock-safe interrupt will always trigger the NMI interrupt. If the flag is not cleared, the program will continue to execute the NMI interrupt code. Therefore, in NMI interrupts, flag bit clearing for clock safety interrupts is required.
Finally, the program is burned into the APM32F030 development board. When the external crystal fails, the program executes smoothly and prints out the information.