This is the fourth day of my participation in Gwen Challenge
In the project, sometimes the microcontroller inexplicably reset, in order to determine which reason causes the reset, it is necessary to judge the reset source of the microcontroller. STM32F103C8T6 microcontroller is used for testing. What are the single-chip reset sources first?
As can be seen in the STM32 Chinese reference manual, the reset source can be read through the status register. So write a program to read the reset state register:
void Check_Rst(void) { printf(" CSR = %x\r\n", RCC->CSR); if(RCC_GetFlagStatus(RCC_FLAG_PINRST) ! Printf ("PIN RESET \r\n"); } if(RCC_GetFlagStatus(RCC_FLAG_PORRST) ! Printf ("POR/PDR RESET r\n"); } if(RCC_GetFlagStatus(RCC_FLAG_SFTRST) ! Printf ("Software RESET \r\n"); } if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) ! Printf ("Independent watchdog RESET r\n"); } if(RCC_GetFlagStatus(RCC_FLAG_WWDGRST) ! Printf ("Window watchdog RESET r\n"); } if(RCC_GetFlagStatus(RCC_FLAG_LPWRRST) ! Printf ("(low-power RESET \r\n"); printf("(low-power RESET \r\n"); } RCC_ClearFlag(); // Clear the reset flag printf("\r\n"); }Copy the code
Print the information through the serial port. Now write a program test, pin reset, power reset, watchdog reset are better test. Software reset is implemented in code. Software reset can be divided into system reset and kernel reset. System reset resets all hardware circuits, including the I/O port status. Kernel reset Resets only the kernel and does not affect hardware circuits. The system reset code is as follows:
Void McUSysRestart (void) {__set_FAULTMASK(1); // Close all interrupts NVIC_SystemReset(); Static __INLINE void NVIC_SystemReset(void) function}Copy the code
The kernel reset code is as follows:
// Kernel reset does not affect peripherals and other circuits void McUCoreRestart (void) {__DSB(); / / setting VECTREST SCB - > AIRCR = ((0 x5fa < < SCB_AIRCR_VECTKEY_Pos) | (SCB - > AIRCR & SCB_AIRCR_PRIGROUP_Msk) | SCB_AIRCR_VECTRESET_Msk); __DSB(); while(1); }Copy the code
Different reset situations are controlled by different keys in the program, and the printed information is as follows:
Power-on reset:
Press the reset button to reset:
Independent watchdog reset:
Window watchdog reset:
System reset:
Kernel reset:
It can be seen from the above reset test that each reset will detect NRST pin reset. Will each reset electrically lower the reset pin? Look at the reset pin level through the oscilloscope:
It can be seen from the reset pin waveform that the reset pin will have a very short negative pulse when other reset conditions occur. Note The reset pin level changes when other reset sources are generated. It appears that the above procedure is correct.