gpio_watchdog_feed-8.ino

BPI-5202 Loongson 2K1000LAi Embedded Single Board Industrial Computer · File : gpio_watchdog_feed-8.ino · Language : c · Use case : Implémenter un watchdog logiciel alimenté par un GPIO pour redémarrer le système en cas de gel.

Shows how to interact with GPIOs via sysfs for a simple safety mechanism on the LoongArch platform.

Compile with gcc -o watchdog_gpio watchdog_gpio.c. Under Loongnix/LxAMP, export the GPIO via /sys/class/gpio/export before execution. This example uses the 20-pin GPIO bus. For a robust solution, prefer the /dev/watchdog interface supported by the Loongson 2K1000 SoC.

gpio_watchdog_feed-8.ino

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>

#define GPIO_PATH "/sys/class/gpio/gpio100/" // Exemple à adapter au schéma BPI-5202
#define LED_PATH GPIO_PATH "value"

int main() {
    int fd = open(LED_PATH, O_WRONLY);
    if (fd < 0) {
        perror("Impossible d'ouvrir le GPIO. Vérifiez l'exportation du pin.");
        return EXIT_FAILURE;
    }

    printf("Démarrage de la boucle Watchdog (Toggle GPIO toutes les 2s)...\n");
    printf("Note : En production, utilisez /dev/watchdog (SoftDog ou HardDog Loongson).\n");

    int state = 0;
    while (1) {
        if (lseek(fd, 0, SEEK_SET) != 0) {
            perror("Lseek error");
            break;
        }
        
        if (state) {
            write(fd, "1\n", 2);
        } else {
            write(fd, "0\n", 2);
        }
        state = !state;
        
        // Si le processus meurt, un watchdog matériel externe ou interne
        // pourrait détecter l'absence de toggle ou utiliser /dev/watchdog
        sleep(2);
        
        // Simulation de travail lourd ou gel ici
    }
    
    close(fd);
    return EXIT_SUCCESS;
}

← Back to the board