IO4: Reliable firmware: interrupts, RTOS tasks, watchdogs and safe state

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Watchdogs and recovering by yourself

🎯You'll be able to

Use a watchdog timer so a hung device restarts by itself, and design firmware that recovers cleanly after a reset.

🏠The everyday picture

A night guard must press a button every hour. If they don't, an alarm calls the manager, who checks on them. The button proves someone is still awake and working.

📘What it really is

📝A worked example

Input: the kit's harness treats a crash as a watchdog reset: RTC memory is kept, the firmware starts again.

Steps:

  1. Make a sensor fault a handled error (SensorError), so the wake still completes and sleeps.
  2. Let truly unexpected errors reset the device; on the next wake, reset_cause() is watchdog.
  3. Count watchdog resets in the state and report them in the status message.

Expected output: a device that keeps working with a dead sensor and tells the server something is wrong.

🌍Where it's used

📝Case scenario

A device fed its watchdog from a timer interrupt. When the main loop hung waiting for a modem, the timer kept feeding, and the device stayed frozen for days. Feeding only after each completed step let the watchdog do its job.

🖱️Try it

In the starter, find where an I2C error would crash a wake. Write what should happen instead, and what should be reported. Success: a handled error, a completed wake, and a counter in the state.

⚠️Common mistakes

🛠️For your project

Project 1's test 3 checks that a dead sensor doesn't crash the device; every wake still ends in deep sleep.

✅Quick check

What does a watchdog timer do?

Show the answer

It restarts the chip if the firmware stops feeding it, which happens when the firmware hangs.

Why shouldn't a timer interrupt feed the watchdog?

Show the answer

It can keep feeding while the main work is stuck.

← PreviousRTOS tasks, queues and priorities
ProLearners Assist