OSFEC.019: Task-Level Watchdog Supervision — Progress Deadlines, Fault Latching and Timer Wraparound

Colored-pencil technical illustration of a microcontroller development board connected to diagnostic test equipment.

Open-Source Firmware Engineering • Intermediate • Estimated study time: 60 minutes.

A hardware watchdog can reset a stalled processor, but a periodic feed from an otherwise healthy interrupt can conceal a deadlocked application task. The watchdog troubleshooting lesson explains reset diagnosis; the engineering problem is designing a supervisor that distinguishes useful task progress from mere execution.

Prerequisites and learning objectives

Review C structures, unsigned arithmetic and RTOS synchronization. Learners will distinguish hardware and task watchdogs, define useful progress checkpoints, compute wrap-safe elapsed times, implement startup and fault-latching behavior, and design timing and recovery verification.

Supervision architecture

A hardware watchdog runs from a configured timer and can trigger reset when its feed deadline is missed. A task watchdog tracks individual execution contexts. The Zephyr task watchdog sample demonstrates an intentionally stalled control thread while the main thread remains active, with optional hardware fallback. The ESP-IDF watchdog documentation distinguishes interrupt monitoring from task monitoring and describes task and user subscriptions.

A heartbeat should follow completion of a meaningful unit of work, such as validating a sensor sample or committing a verified command, rather than an unconditional timer interrupt. A single supervisor evaluates all required tasks before authorizing a hardware feed. If the supervisor stalls, the independent hardware timeout remains necessary. A windowed peripheral may additionally prohibit feeding too early.

Using The Watchdog Timer To Find Bugs? Embedded System Explained — introductory discussion of watchdog implementation and timeout selection. Original video.

Deadlines, startup and clock arithmetic

The illustrative tasks use deadlines of 100, 200 and 300 ms. All required tasks must demonstrate initial progress before the supervisor authorizes feeding. A task arriving exactly at its deadline is late. After a deadline violation, a fault remains latched until an authorized restart; a late heartbeat cannot hide it. The boot design must allow initialization without exceeding the real hardware watchdog timeout. Select each deadline from measured worst-case progress intervals plus justified scheduling and clock margins, while satisfying the maximum permitted fault-response time.

A 32-bit unsigned millisecond timestamp wraps after approximately 49.71 days. Subtract the previous timestamp from the current timestamp and convert to uint32_t to obtain elapsed time modulo 2³². The result is meaningful only when the actual elapsed interval is less than one complete counter period and observations arrive in chronological order. Frequent polling is essential; the model cannot distinguish additional unobserved counter revolutions.

Executable C17 host laboratory

Save the following program as supervisor.c. The supervisor state belongs to one serialized execution context; an RTOS implementation must synchronize reports or pass them through a queue, including ordering and timestamp validation. The example does not touch hardware registers or invoke a physical watchdog. The original laboratory code follows:

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#define TASKS 3u
#define ALL ((1u << TASKS) - 1u)

typedef struct {
    uint32_t last[TASKS];
    unsigned seen;
    bool fault;
} Supervisor;

static const uint32_t deadline[TASKS] = {100u, 200u, 300u};

static bool healthy(Supervisor *s, uint32_t now) {
    if (s->fault) return false;
    for (unsigned i = 0; i < TASKS; ++i) {
        if ((uint32_t)(now - s->last[i]) >= deadline[i]) {
            s->fault = true;
            return false;
        }
    }
    return s->seen == ALL;
}

static bool progress(Supervisor *s, unsigned id, uint32_t now) {
    if (id >= TASKS || s->fault) return false;
    (void)healthy(s, now); /* Check expiry before accepting progress. */
    if (s->fault) return false;
    s->last[id] = now;
    s->seen |= 1u << id;
    return true;
}

static Supervisor start(uint32_t now) {
    return (Supervisor){{now, now, now}, 0u, false};
}

#define CHECK(x) do { if (!(x)) { puts("FAIL: " #x); return 1; } } while (0)
int main(void) {
    Supervisor s = start(0u);
    CHECK(!healthy(&s, 0u));
    CHECK(progress(&s, 0u, 10u));
    CHECK(progress(&s, 1u, 20u));
    CHECK(progress(&s, 2u, 30u));
    CHECK(healthy(&s, 109u));
    puts("PASS: startup and healthy tasks");
    CHECK(!healthy(&s, 110u));
    CHECK(!progress(&s, 0u, 111u));
    CHECK(!healthy(&s, 112u));
    puts("PASS: exact deadline and latched fault");
    s = start(0u);
    CHECK(!progress(&s, TASKS, 1u));
    CHECK(!progress(&s, 0u, 100u));
    puts("PASS: invalid ID and late progress");
    s = start(UINT32_MAX - 20u);
    CHECK(progress(&s, 0u, UINT32_MAX - 10u));
    CHECK(progress(&s, 1u, UINT32_MAX - 9u));
    CHECK(progress(&s, 2u, UINT32_MAX - 8u));
    CHECK(healthy(&s, 88u));
    CHECK(!healthy(&s, 89u));
    puts("PASS: timestamp wraparound");
    return 0;
}

Compile and execute the program on a Linux host with GCC using the following command:

gcc -std=c17 -Wall -Wextra -Wpedantic -Werror -fsanitize=undefined -O1 supervisor.c -o supervisor
./supervisor

Expected output, independently reproduced with GCC 14.2.0:

PASS: startup and healthy tasks
PASS: exact deadline and latched fault
PASS: invalid ID and late progress
PASS: timestamp wraparound

Timing and hardware validation

The Memfault watchdog guide illustrates why one functioning task cannot establish overall application health. Apply the earlier interrupt latency and jitter lesson to measure worst-case supervisor polling latency. Account for fault detection delay, the remaining hardware countdown, reset propagation and safe-output transition. For example, with a 100 ms task deadline, 10 ms polling and a 500 ms hardware countdown, a conservative illustrative upper bound before reset initiation is 610 ms, assuming bounded scheduling, no feed after fault recognition and a functioning watchdog clock. Do not treat the example as a certified response-time guarantee.

STM32 Independent Watchdog (IWDG) Explained | Prevent Firmware Freezes — a device-specific configuration example; the portable host program above is not STM32 register code. Original video.

Use the hardware-in-the-loop testing lesson to design controlled stalls, blocked resources, boot delays, timer wraparound and reset-cause checks. Verify safe outputs and preserve diagnostic evidence before restarting where practical. Use an isolated low-voltage development board and disconnect hazardous actuators. Never intentionally stall a live production or safety-critical controller. Host tests establish logic behavior only; target clock, reset circuitry, scheduling, and independent watchdog response require separate validation.

Exercises and answer key

A. Why is one periodic interrupt insufficient evidence of application health? B. Where should a task report progress? C. What is elapsed time at now = 89 and last = UINT32_MAX − 10? D. Can a late heartbeat clear a latched fault? E. Name three hardware-in-the-loop checks.

Answers: A. A critical task may be blocked while the interrupt still runs. B. After completing meaningful validated work. C. 100 ms modulo 2³². D. No; recovery requires authorized reinitialization. E. Verify independent watchdog timeout, safe output state and persistent reset-cause evidence, among other target-specific requirements.

Editor’s Note: We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please scan the QR code below

BitcoinVersus.Tech donation QR code for 5,000 satoshis

For mobile scanning, open the article on another display and scan the QR code with a Bitcoin wallet. The voluntary donation amount is 5,000 sats. Verify the receiving address 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb before sending.

BitcoinVersus.Tech publishes technology and financial journalism for informational purposes and does not provide financial, investment, or legal advice.

Leave a Reply