Determine STM32 reset source

When your STM32 processor starts up from a reset, there are a number of possible sources for that reset. You may want to perform different initialisations depending on the exact source of the reset.

A single register holds the flags which tell you why the processor was reset. It is the RCC clock control & status register (RCC_CSR) register.

A system reset is generated when one of the following events occurs:

  • A low level on the NRST pin (external reset)
  • Window watchdog end of count condition (WWDG reset)
  • Independent watchdog end of count condition (IWDG reset)
  • A software reset (SW reset)
  • Low-power management reset

Each of these works by pulling the NRST line low for 20 microseconds. There is also a power reset caused by the power supply dropping below a level needed to keep the processor running.

A typical implementation will have the NRST line connected externally to a processor supervisor chip or a simple RC circuit that brings the NRST line high more slowly than the power line.

A reset can also be caused by and external circuit or switch bringing the NRST line low briefly. This kind of reset is called a PIN reset.

The result of these actions is that all resets will look like pin resets since they will all cause the most recent action to be the NRST line going high. Thus, when looking for what caused the last reset, it is not enough to simply look at the pin reset flag. First, the code must examine the other reset sources to see if any of them were responsible and test the pin reset last.

In the RCC_CSR register, the bits to look for are:

  • Bit31: LPWRRSTF – Low-power reset flag
    Set by hardware when a Low-power management reset occurs.
  • Bit30: WWDGRSTF – Window watchdog reset flag
    Set by hardware when a window watchdog reset occurs.
  • Bit29: IWDGRSTF – Independent watchdog reset flag
    Set by hardware when an independent watchdog reset from VDD domain occurs.
  • Bit28: SFTRSTF – Software reset flag
    Set by hardware when a software reset occurs.
  • Bit27: PORRSTF – POR/PDR reset flag
    Set by hardware when a POR/PDR reset occurs.
  • Bit26: PINRSTF – PIN reset flag
    Set by hardware when a reset from the NRST pin occurs.
  • Bit25: BORRSTF – BOR reset flag
    Set by hardware when a POR/PDR or BOR reset occurs.

You may choose to examine these directly of make use of the Standard Peripheral Library functions. Note that, the status of these flags will survive a subsequent reset! That means, for example, that, if you have a power up reset and then press the reset button, the system still records the fact that there was a power up reset. To sort that out, you need to clear the flags after they have been used. This is done by writing a 1 to bit 24, the RMVF bit, in RCC_CSR. Again, this can be done directly or through a Peripheral Library function.

Here is a code fragment that should illustrate the technique of determning the most recent reset source. In this code, I write a message out to a display to show me what caused the reset.

// test the reset flags in order because the pin reset is always set. 
if  (RCC_GetFlagStatus(RCC_FLAG_SFTRST)){
  HCMS3907_puts("SFT ");
} else if  (RCC_GetFlagStatus(RCC_FLAG_PORRST)){
  HCMS3907_puts("PWR ");
} else if  (RCC_GetFlagStatus(RCC_FLAG_PINRST)){
  HCMS3907_puts("PIN ");
} else {
  HCMS3907_puts("?RST");
}
// The flags must be cleared manually after use
RCC_ClearFlag();

For full details, see section 5.1 in the STM32 Processor Reference Manual

 

Incoming search terms:

  • stm32 software reset (33)
  • stm32f4 software reset (32)
  • stm32 reset (28)
  • stm32 watchdog example (17)
  • stm32f4 watchdog (14)
  • software reset STM32F4 (2)
  • arm :performing a software reset (1)
  • STM32 window watchdog reset (1)
  • stm32 自己Reset (1)
  • stm32f103 software reset (1)
This entry was posted in Hardware, STM32. Bookmark the permalink.

4 Responses to Determine STM32 reset source

  1. David A says:

    Hi Pete,

    Which toolsuite are you using for your STM32 F4? IAR?

    Regards,

    Dave

  2. Peter Harrison says:

    I use Rowley’s Crossworks for ARM.

  3. mog123 says:

    Hi again Peter,

    An unrelated question:
    I’m thinking of buying faulhaber motors in my price range (1524) but they come equipped with really poor encoders (1cpr) As these would be my first Faulhaber motors I wanted to ask how are they built? Is it possible to strip away the encoder and add a simple optical encoder or is the shaft extended so that I could use a magnetic encoder (as in as5035 or as5040)?

    Keep up the good work with decimus 4!

    Cheers

  4. Peter Harrison says:

    Can’t say that I have ever seen Faulhaber motors with a single count per rotation. Considering the price, I would caution against taking them apart. there are other possibilities that you might consider. The AS5040 devices can be mounted on the drive wheel axle to give adequate resolution if the wheels are not too big.

Leave a Reply