Skip to content
TheChatArchiveThe Chat Archive
Grok 4.1 Fast· xaiHugging Face Frontier Reasoning Traces

Implement a watchdog timer in C to reset the system on failure.

  1. User

    Implement a watchdog timer in C to reset the system on failure.

  2. Assistant
    /*
     * Simple Watchdog Timer Implementation for AVR Microcontrollers (e.g., Arduino/ATmega).
     * 
     * This enables the hardware Watchdog Timer (WDT), which resets the MCU if not periodically
     * "kicked" (reset). Ideal for detecting hangs or failures in long-running loops.
     * 
     * - Enable WDT with a timeout (e.g., 2 seconds).
     * - Call `kick_watchdog()` periodically in your main loop or tasks.
     * - If the system hangs and misses kicks, hardware auto-resets the MCU.
     * 
     * Compile with avr-gcc (e.g., for Arduino IDE or AVR toolchain).
     * Fuse settings: Ensure WDT is enabled in fuses if needed (usually fuse-controlled).
     */
    
    #include <avr/io.h>
    #include <avr/wdt.h>
    #include <util/delay.h>  // For demo delays
    
    // Function to kick (reset) the watchdog timer.
    // Call this every < timeout interval (e.g., every 1s if timeout=2s).
    void kick_watchdog(void) {
        wdt_reset();
    }
    
    int main(void) {
        // Enable WDT with 2-second timeout.
        // Options: WDTO_15MS, WDTO_30MS, ..., WDTO_8S, WDTO_16S (ATmega128+).
        wdt_enable(WDTO_2S);
    
        // Optional: Disable interrupts during init if needed.
        // cli();
    
        while (1) {
            // Simulate your main application work here.
            // Critical sections should be short (< timeout).
            
            _delay_ms(500);  // Demo: Half-second work.
            
            // Do important tasks...
            
            // Kick the watchdog before timeout.
            kick_watchdog();
            
            _delay_ms(500);  // Demo: Other half-second work.
            
            // Kick again if loop >1s total work.
            kick_watchdog();
        }
        
        return 0;  // Never reached.
    }
    

    Usage Notes:

    • Timeout Selection: Choose based on your loop time (e.g., WDTO_8S for slower systems). See avr/wdt.h for options.
    • Interrupts: WDT runs in all sleep modes. Use wdt_reset() in ISR if needed.
    • Disable (Debug): Call wdt_disable() early (before enabling). Once enabled, can't disable without reset.
    • Production: Set fuses to prevent disabling via software (e.g., BODLEVEL fuse).
    • Testing: Comment out kick_watchdog() to trigger reset in ~2s.

    Linux Variant (for x86/ARM SBCs like Raspberry Pi):

    If targeting Linux with a hardware watchdog (e.g., /dev/watchdog):

    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
    #include <linux/watchdog.h>
    #include <errno.h>
    
    int wfd = -1;
    
    // Initialize watchdog (returns 0 on success).
    int init_watchdog(int timeout_secs) {
        wfd = open("/dev/watchdog", O_WRONLY);
        if (wfd == -1) {
            perror("open /dev/watchdog");
            return -1;
        }
        if (ioctl(wfd, WDIOC_SETTIMEOUT, &timeout_secs) == -1) {
            perror("WDIOC_SETTIMEOUT");
            close(wfd);
            return -1;
        }
        printf("Watchdog armed: %d seconds\n", timeout_secs);
        return 0;
    }
    
    // Kick the watchdog.
    void kick_watchdog(void) {
        if (wfd != -1) {
            write(wfd, "a", 1);  // Any byte works.
        }
    }
    
    // Cleanup (disables if magic-close supported).
    void stop_watchdog(void) {
        if (wfd != -1) {
            ioctl(wfd, WDIOC_KEEPALIVE, NULL);  // Final kick.
            close(wfd);
            wfd = -1;
        }
    }
    
    int main(void) {
        if (init_watchdog(10) != 0) exit(1);  // 10s timeout.
    
        while (1) {
            // Your work...
            sleep(1);
            kick_watchdog();
        }
    
        stop_watchdog();
        return 0;
    }
    

    Load kernel module if needed: modprobe iTCO_wdt (Intel) or equivalent. Use wdctl to check status.

    This ensures system reset on hangs/failures! Adjust for your platform.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.