Simvik

Decode the reset cause

Name the reset an STM32F4 came out of from RCC->CSR, when several flags are set at once. An easy embedded C problem (Fault injection: resets, brown-outs and…

easy · Fault injection: resets, brown-outs and flipped bits · reset · stm32 · bits

An STM32F4's RCC->CSR keeps a flag for each reset source. Several are set together: every internal reset (the watchdog, a software reset) also pulls the NRST pin low and sets PINRSTF, and a power-on reset sets BORRSTF as well as PORRSTF.

Write reset_cause(csr), returning the most specific cause:

FlagsReturn
IWDGRSTF or WWDGRSTF set"watchdog"
SFTRSTF set"software"
LPWRRSTF set"low-power"
PORRSTF set"power-on"
BORRSTF set"brown-out"
PINRSTF set"reset pin"
none"unknown"

The rows are in priority order: the first that applies wins. The flag masks are defined for you.

Starter code

const char *reset_cause(uint32_t csr) {
  if (csr & PINRSTF) return "reset pin";
  if (csr & PORRSTF) return "power-on";
  if (csr & IWDGRSTF) return "watchdog";
  return "unknown";
}

Example tests

Power-on, with the brown-out and pin flags beside it

show(PORRSTF | BORRSTF | PINRSTF);

Expected output:

0E000000 power-on

The watchdog, with the pin flag

show(IWDGRSTF | PINRSTF);

Expected output:

24000000 watchdog

Learn it first

More Fault injection: resets, brown-outs and flipped bits problems

  • A record that survives a reset · Count boots in RAM that keeps its contents through a reset but not a power cut, trusting the record only when it checks out.