gpio-led-blink-loongarch.ino

BPI-5202 Loongson 2K1000LAi Embedded Single Board Industrial Computer · File : gpio-led-blink-loongarch.ino · Language : C · Use case : Allumer et éteindre une LED connectée au port d'extension GPIO pour tester la sortie numérique.

C example using Linux GPIOv2 API to control an LED via the internal bus of the extension port.

Compiled with gcc under Loongnix or LoongOS. The BPI-5202 exposes GPIOs via /dev/gpiochip0. Check the schematic for exact physical pin mapping. Run as root: sudo ./led_blink.

gpio-led-blink-loongarch.ino

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

#define GPIO_PIN 17 // Exemple de broche sur le connecteur 20-pin

int main() {
    int fd;
    struct gpiohandle_request req;
    struct gpiohandle_data data;

    // Ouvrir le contrôleur GPIO
    fd = open("/dev/gpiochip0", O_RDONLY);
    if (fd < 0) {
        perror("Failed to open gpiochip0");
        return 1;
    }

    // Configurer la demande de ligne GPIO
    req.lineoffsets[0] = GPIO_PIN;
    req.flags = GPIOHANDLE_REQUEST_OUTPUT;
    req.default_values[0] = 0; // Éteint au démarrage
    req.lines = 1;
    req.consumer_label = "bpi5202_led";

    // Demander la ligne GPIO
    if (ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req) < 0) {
        perror("Failed to request GPIO line");
        close(fd);
        return 1;
    }

    printf("Blinking LED on GPIO %d...\n", GPIO_PIN);

    while (1) {
        // Allumer
        data.values[0] = 1;
        ioctl(req.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
        sleep(1);

        // Éteindre
        data.values[0] = 0;
        ioctl(req.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
        sleep(1);
    }

    close(req.fd);
    close(fd);
    return 0;
}

← Back to the board