gpio_watchdog_feed-8.ino
Montre comment interagir avec les GPIO via sysfs pour un mécanisme de sécurité simple sur la plateforme LoongArch.
Compilez avec gcc -o watchdog_gpio watchdog_gpio.c. Sous Loongnix/LxAMP, exportez le GPIO via /sys/class/gpio/export avant l'exécution. Cet exemple utilise le bus GPIO de 20 broches. Pour une solution robuste, privilégiez l'interface /dev/watchdog supportée par le SoC Loongson 2K1000.
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;
}