cm3588-hw-watchdog-reset-8.ino

FriendlyElec CM3588 SOM (Rockchip RK3588) · File : cm3588-hw-watchdog-reset-8.ino · Language : C · Use case : Configurer le watchdog matériel interne du RK3588 pour réinitialiser le système en cas de gelage logiciel.

Minimal C program to enable the RK3588 hardware watchdog (dw_wdt) and trigger a reset if the service is not periodically refreshed.

The RK3588 features an internal watchdog accessible via /dev/watchdog. In Linux, the 'dw_wdt' module must be loaded. This code demonstrates the watchdog API. In production, use systemd or a service script to feed the watchdog (WDIOC_KEEPALIVE) every N/2 seconds.

cm3588-hw-watchdog-reset-8.ino

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/watchdog.h>

#define DEV_NAME "/dev/watchdog"

int main() {
    int fd;
    struct watchdog_info ident;
    struct watchdog_status status;
    int timeout = 15; // Timeout en secondes

    // Ouvrir le fichier de périphérique watchdog
    fd = open(DEV_NAME, O_WRONLY);
    if (fd < 0) {
        perror("Erreur: Impossible d'ouvrir /dev/watchdog");
        return 1;
    }

    // Définir le délai avant réinitialisation
    if (ioctl(fd, WDIOC_SETTIMEOUT, &timeout) < 0) {
        perror("Erreur: Impossible de définir le timeout");
        close(fd);
        return 1;
    }

    printf("Watchdog activé avec un timeout de %d secondes.\n", timeout);
    printf("Le programme va maintenant 'geler' volontairement pour tester le reset...\n");

    // Boucle qui arrête de nourrir le watchdog (pet-ting)
    // Normalement, on utiliserait un while(1) { ioctl(fd, WDIOC_KEEPALIVE, 0); sleep(5); }
    
    int i = 0;
    while (1) {
        // On ne nourrit PAS le watchdog ici pour provoquer un crash/reset
        sleep(1);
        if (i++ % 5 == 0) {
            printf("... {countdown}\n");
        }
        if (i > 20) return 0; // Ne devrait pas être atteint si le watchdog fonctionne
    }

    close(fd);
    return 0;
}

← Back to the board