Skip to the content.
« Previous Index Next »

Firmware Coding Standard — Watchdog, Startup & Shutdown (FreeRTOS)

1) Purpose

Establish robust patterns for system startup, watchdog servicing, and controlled shutdown/reset. Prevent undefined states, guarantee safe recovery, and ensure fault causes are observable.


2) Principles


3) Startup Rules


4) Watchdog Rules


5) Shutdown & Reset Rules


6) Fault Escalation


7) Anti-Patterns


8) Review Checklist


9) CI/Lint Gates


10) Example Patterns

Watchdog supervisor task

static EventGroupHandle_t wdEvt;
#define WD_TASK_A (1<<0)
#define WD_TASK_B (1<<1)

void wd_task(void *arg) {
    for (;;) {
        EventBits_t bits = xEventGroupWaitBits(
            wdEvt, WD_TASK_A | WD_TASK_B,
            pdTRUE, pdTRUE,
            pdMS_TO_TICKS(WD_TIMEOUT_MS)
        );

        if ((bits & (WD_TASK_A | WD_TASK_B)) == (WD_TASK_A | WD_TASK_B)) {
            watchdog_kick_hw();   // all tasks reported
        } else {
            LOG_CRIT("WDT", "Missed heartbeat, bits=0x%X", bits);
            system_reset(SYS_RST_WATCHDOG);
        }
    }
}

Startup sequence

void system_init(void) {
    hw_init_clocks();
    hw_init_watchdog();   // enable early
    hw_init_uart();
    drv_init_all();

    create_rtos_objects();
    start_application();

    vTaskStartScheduler();
}

Logging reset cause

uint32_t cause = hw_read_reset_cause();
store_retention_reg(RESET_CAUSE, cause);
LOG_INFO("SYS", "Reset cause=0x%08X", cause);

« Previous Index Next »