Simvik

Greenhouse: report why it restarted

Read the reset-cause flags and report the one that matters. An easy embedded C problem (Watchdogs), run and graded in the browser.

easy · Watchdogs · watchdog · reset · bits · product

Product: Smart greenhouse controller (Agriculture)

Requirement. At every start-up the controller logs why it restarted, so a technician can tell a power cut from a firmware hang.

The flags in mcusr accumulate until firmware clears them, so several can be set at once. Report the most telling one, in this order: watchdog (the firmware hung), then brown-out (the supply sagged), then external (someone pressed reset), then power-on. With none of these bits set, report unknown.

Starter code

const char *reset_cause(uint8_t mcusr) {
  if (mcusr == (1 << WDRF)) return "watchdog";
  if (mcusr == (1 << PORF)) return "power-on";
  return "unknown";
}

Example tests

One flag each

printf("%s %s\n", reset_cause(0x08), reset_cause(0x01));

Expected output:

watchdog power-on

Flags left over from earlier resets

printf("%s %s\n", reset_cause(0x09), reset_cause(0x04));

Expected output:

watchdog brown-out

Learn it first

  • Watchdog timer · Independent and windowed watchdogs, and feeding them correctly.

More Watchdogs problems