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-onFlags left over from earlier resets
printf("%s %s\n", reset_cause(0x09), reset_cause(0x04));Expected output:
watchdog brown-outLearn it first
- Watchdog timer · Independent and windowed watchdogs, and feeding them correctly.
More Watchdogs problems
- Watchdog reload value · Turn a timeout in milliseconds into the reload count of a prescaled watchdog clock.
- Kick only when everyone is alive · Feed the watchdog only if every required task has checked in since the last kick.