🛠 Faire clignoter une LED avec libgpiod (numérotation GPIO du A64 : PB0 = 32)

Pine64 PINE A64 / PINE A64+ (Allwinner A64) · debutant · Schéma : IMG-pine64-debutant-3-blink-gpio-libgpiod.svg — PINE A64 sur breadboard, LED avec resistance de limitation branchee sur une broche du connecteur 40 broches annotee PB0 (offset 32) vers GND, terminal affichant gpioset gpiochip0 32=1, encart rappelant bank x32 + pin

Objectives

First steps with GPIO on the PINE A64 running a mainline kernel (Armbian). Two specificities to keep in mind right away: (1) Allwinner's GPIO numbering follows the formula offset = (bank − A) × 32 + pin, so PB0 = 1×32 + 0 = 32, PD16 = 112, PH2 = 226 — forget the Raspberry Pi BCM numbering; (2) the 40-pin "Pi-2" connector does not have the RPi pinout: its UART is on PB0/PB1 (offsets 32/33, commented as "On Pi-2 connector" in the device tree). You will wire an LED, make it blink in the shell using gpioset, and then in C using libgpiod.

Hardware

Full code

# Repérer la puce GPIO (le pilote pinctrl-sun50i-a64 est gpiochip0) :
gpioinfo gpiochip0 | head

# Clignoter en shell — PB0 => offset 32 :
while true; do gpioset gpiochip0 32=1; sleep 0.5; gpioset gpiochip0 32=0; sleep 0.5; done
/* blink.c — gcc blink.c -o blink -lgpiod */
#include <gpiod.h>
#include <unistd.h>
int main(void) {
    /* PB0 : (B-A)*32 + 0 = 32 (pilote pinctrl sun50i-a64 = gpiochip0) */
    struct gpiod_chip *chip = gpiod_chip_open_by_name("gpiochip0");
    if (!chip) { perror("gpiochip0"); return 1; }
    struct gpiod_line *led = gpiod_chip_get_line(chip, 32);   /* PB0 */
    if (gpiod_line_request_output(led, "blink", 0)) { perror("request PB0"); return 1; }
    for (int i = 0; i < 10; i++) {
        gpiod_line_set_value(led, i & 1);
        sleep(1);
    }
    return 0;
}

Expected verification

The LED blinks at 1 Hz (shell script: 0.5 s). gpioinfo gpiochip0 lists the A64 pins with their names (PB0 at offset 32): this is the tool that corrects your numbering before any wiring. Since PB0/PB1 were originally the UART2 pins of the Pi-2 connector, the device tree leaves them status = "disabled" by default — as long as you do not enable uart2, you can use them as GPIO outputs without conflict.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials