From ebb2d203b6ba7469913ac9a19308c8a24576617c Mon Sep 17 00:00:00 2001 From: Jason Yang Date: Wed, 14 Oct 2026 13:38:45 +0800 Subject: [PATCH 1/4] common: add a GPIO layer for libgpiod 1.x and 2.x gpiod_compat.h gives the demos one open/request/set/get/release API that builds unchanged against libgpiod 1.x or 2.x. Compiles against gpiod.h 1.0 to 2.3.1 (ten releases) without warnings. Run on Kakip with libgpiod 2.2.1; the 1.x path is compile-tested only. --- common/gpiod_compat.h | 270 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 common/gpiod_compat.h diff --git a/common/gpiod_compat.h b/common/gpiod_compat.h new file mode 100644 index 0000000..76de502 --- /dev/null +++ b/common/gpiod_compat.h @@ -0,0 +1,270 @@ +/* + * Author: Jason Yang + * Date: 2026-10-14 + * + * One GPIO API over libgpiod 1.x and 2.x, picked from gpiod.h. + */ + +#ifndef GPIOD_COMPAT_H +#define GPIOD_COMPAT_H + +#include +#include +#include +#include +#include +#include + +struct gpio_chip; +struct gpio_line; + +static inline struct gpio_chip *gpio_chip_open(const char *name_or_path); +static inline struct gpio_chip *gpio_chip_open_by_label(const char *label); +/* release the lines first */ +static inline void gpio_chip_close(struct gpio_chip *chip); + +static inline struct gpio_line *gpio_line_request_output(struct gpio_chip *chip, + unsigned int offset, + const char *consumer, + int value); +static inline struct gpio_line *gpio_line_request_input(struct gpio_chip *chip, + unsigned int offset, + const char *consumer); +static inline int gpio_line_set_value(struct gpio_line *line, int value); +/* 0 or 1, -1 on error */ +static inline int gpio_line_get_value(struct gpio_line *line); +static inline void gpio_line_release(struct gpio_line *line); + +struct gpio_chip { + struct gpiod_chip *chip; +}; + +static inline struct gpio_chip *gpio_chip_wrap(struct gpiod_chip *c) +{ + struct gpio_chip *chip; + + if (!c) + return NULL; + chip = malloc(sizeof(*chip)); + if (!chip) { + gpiod_chip_close(c); + return NULL; + } + chip->chip = c; + return chip; +} + +static inline void gpio_chip_close(struct gpio_chip *chip) +{ + if (!chip) + return; + gpiod_chip_close(chip->chip); + free(chip); +} + +#ifdef GPIOD_LINE_BULK_MAX_LINES /* 1.x only */ + +struct gpio_line { + struct gpiod_line *line; +}; + +static inline struct gpio_chip *gpio_chip_open(const char *name_or_path) +{ + return gpio_chip_wrap(gpiod_chip_open_lookup(name_or_path)); +} + +static inline struct gpio_chip *gpio_chip_open_by_label(const char *label) +{ + return gpio_chip_wrap(gpiod_chip_open_by_label(label)); +} + +static inline struct gpio_line *gpio_line_wrap(struct gpiod_line *l) +{ + struct gpio_line *line = malloc(sizeof(*line)); + + if (!line) { + gpiod_line_release(l); + return NULL; + } + line->line = l; + return line; +} + +static inline struct gpio_line *gpio_line_request_output(struct gpio_chip *chip, + unsigned int offset, + const char *consumer, + int value) +{ + struct gpiod_line *l = gpiod_chip_get_line(chip->chip, offset); + + if (!l || gpiod_line_request_output(l, consumer, value) < 0) + return NULL; + return gpio_line_wrap(l); +} + +static inline struct gpio_line *gpio_line_request_input(struct gpio_chip *chip, + unsigned int offset, + const char *consumer) +{ + struct gpiod_line *l = gpiod_chip_get_line(chip->chip, offset); + + if (!l || gpiod_line_request_input(l, consumer) < 0) + return NULL; + return gpio_line_wrap(l); +} + +static inline int gpio_line_set_value(struct gpio_line *line, int value) +{ + return gpiod_line_set_value(line->line, value); +} + +static inline int gpio_line_get_value(struct gpio_line *line) +{ + return gpiod_line_get_value(line->line); +} + +static inline void gpio_line_release(struct gpio_line *line) +{ + if (!line) + return; + gpiod_line_release(line->line); + free(line); +} + +#else /* libgpiod 2.x */ + +struct gpio_line { + struct gpiod_line_request *req; + unsigned int offset; +}; + +static inline struct gpio_chip *gpio_chip_open(const char *name_or_path) +{ + char path[64]; + + if (name_or_path[0] != '/') { + snprintf(path, sizeof(path), "/dev/%s", name_or_path); + name_or_path = path; + } + return gpio_chip_wrap(gpiod_chip_open(name_or_path)); +} + +static inline struct gpio_chip *gpio_chip_open_by_label(const char *label) +{ + DIR *dev = opendir("/dev"); + struct dirent *ent; + struct gpiod_chip *c = NULL; + + if (!dev) + return NULL; + while (!c && (ent = readdir(dev))) { + char path[sizeof("/dev/") + sizeof(ent->d_name)]; + struct gpiod_chip_info *info; + + snprintf(path, sizeof(path), "/dev/%s", ent->d_name); + if (!gpiod_is_gpiochip_device(path)) + continue; + c = gpiod_chip_open(path); + if (!c) + continue; + info = gpiod_chip_get_info(c); + if (!info || strcmp(gpiod_chip_info_get_label(info), label) != 0) { + gpiod_chip_close(c); + c = NULL; + } + if (info) + gpiod_chip_info_free(info); + } + closedir(dev); + if (!c) + errno = ENOENT; + return gpio_chip_wrap(c); +} + +static inline struct gpio_line *gpio_line_request(struct gpio_chip *chip, + unsigned int offset, + const char *consumer, + enum gpiod_line_direction dir, + int value) +{ + struct gpiod_line_settings *settings = gpiod_line_settings_new(); + struct gpiod_line_config *line_cfg = gpiod_line_config_new(); + struct gpiod_request_config *req_cfg = gpiod_request_config_new(); + struct gpiod_line_request *req = NULL; + struct gpio_line *line = NULL; + + if (!settings || !line_cfg || !req_cfg) + goto out; + gpiod_line_settings_set_direction(settings, dir); + if (dir == GPIOD_LINE_DIRECTION_OUTPUT) + gpiod_line_settings_set_output_value(settings, + value ? GPIOD_LINE_VALUE_ACTIVE + : GPIOD_LINE_VALUE_INACTIVE); + gpiod_request_config_set_consumer(req_cfg, consumer); + if (gpiod_line_config_add_line_settings(line_cfg, &offset, 1, settings) < 0) + goto out; + req = gpiod_chip_request_lines(chip->chip, req_cfg, line_cfg); + if (!req) + goto out; + line = malloc(sizeof(*line)); + if (!line) { + gpiod_line_request_release(req); + goto out; + } + line->req = req; + line->offset = offset; +out: + if (req_cfg) + gpiod_request_config_free(req_cfg); + if (line_cfg) + gpiod_line_config_free(line_cfg); + if (settings) + gpiod_line_settings_free(settings); + return line; +} + +static inline struct gpio_line *gpio_line_request_output(struct gpio_chip *chip, + unsigned int offset, + const char *consumer, + int value) +{ + return gpio_line_request(chip, offset, consumer, + GPIOD_LINE_DIRECTION_OUTPUT, value); +} + +static inline struct gpio_line *gpio_line_request_input(struct gpio_chip *chip, + unsigned int offset, + const char *consumer) +{ + return gpio_line_request(chip, offset, consumer, + GPIOD_LINE_DIRECTION_INPUT, 0); +} + +static inline int gpio_line_set_value(struct gpio_line *line, int value) +{ + return gpiod_line_request_set_value(line->req, line->offset, + value ? GPIOD_LINE_VALUE_ACTIVE + : GPIOD_LINE_VALUE_INACTIVE); +} + +static inline int gpio_line_get_value(struct gpio_line *line) +{ + enum gpiod_line_value v = gpiod_line_request_get_value(line->req, + line->offset); + + if (v == GPIOD_LINE_VALUE_ERROR) + return -1; + return v == GPIOD_LINE_VALUE_ACTIVE; +} + +static inline void gpio_line_release(struct gpio_line *line) +{ + if (!line) + return; + gpiod_line_request_release(line->req); + free(line); +} + +#endif /* libgpiod 2.x */ + +#endif /* GPIOD_COMPAT_H */ From 33d057f98e99fbb4051283585244fa46e2358e7a Mon Sep 17 00:00:00 2001 From: Jason Yang Date: Wed, 14 Oct 2026 13:48:27 +0800 Subject: [PATCH 2/4] pixpaper-213-m: kakip: use gpiod_compat.h Ubuntu 26.04 ships libgpiod 2.x, which is not source compatible with the 1.x API this demo called directly. Use common/gpiod_compat.h, which builds against either. --- .../mono/spi/pixpaper-213-m-robot-eye-kakip.c | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c b/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c index fc71e5b..3b44102 100644 --- a/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c +++ b/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c @@ -12,6 +12,7 @@ * periodically to clear ghosting. * * Build (target): gcc -o robot-eye pixpaper-213-m-robot-eye-kakip.c -lgpiod -lm + * (libgpiod 1.x or 2.x, see common/gpiod_compat.h) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,7 +21,6 @@ */ #include -#include #include #include #include @@ -31,6 +31,8 @@ #include #include +#include "../../../common/gpiod_compat.h" + #define EPD_SPI_DEVICE "/dev/spidev0.0" #define EPD_GPIO_CHIP "gpiochip0" @@ -55,8 +57,8 @@ #define FULL_REFRESH_EVERY 60 /* partial frames between ghost-clearing full refreshes */ int spi_fd; -struct gpiod_chip *chip; -struct gpiod_line *epd_dc_line, *epd_rst_line, *epd_busy_line; +struct gpio_chip *chip; +struct gpio_line *epd_dc_line, *epd_rst_line, *epd_busy_line; static uint8_t fb[EPD_H * EPD_W]; /* 0xFF = white, 0x00 = black */ static uint8_t packed[EPD_W * 16]; @@ -87,20 +89,20 @@ void spi_write(uint8_t *data, int len) { } void epd_writeCommand(uint8_t command) { - gpiod_line_set_value(epd_dc_line, 0); + gpio_line_set_value(epd_dc_line, 0); sleep_us(1); spi_write(&command, 1); } void epd_writeData(uint8_t data) { - gpiod_line_set_value(epd_dc_line, 1); + gpio_line_set_value(epd_dc_line, 1); sleep_us(1); spi_write(&data, 1); } /* bulk data write: one DC toggle, chunked SPI transfers */ void epd_writeDataBuf(const uint8_t *data, int len) { - gpiod_line_set_value(epd_dc_line, 1); + gpio_line_set_value(epd_dc_line, 1); sleep_us(1); while (len > 0) { int n = len > 2048 ? 2048 : len; @@ -113,16 +115,16 @@ void epd_writeDataBuf(const uint8_t *data, int len) { void epd_waitUntilIdle() { sleep_ms(2); while (1) { - if (gpiod_line_get_value(epd_busy_line) == 0) + if (gpio_line_get_value(epd_busy_line) == 0) break; } } void epd_HWreset() { sleep_ms(50); - gpiod_line_set_value(epd_rst_line, 0); + gpio_line_set_value(epd_rst_line, 0); sleep_ms(50); - gpiod_line_set_value(epd_rst_line, 1); + gpio_line_set_value(epd_rst_line, 1); sleep_ms(50); } @@ -352,23 +354,20 @@ int main(void) { uint32_t speed = SPI_SPEED; ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); - chip = gpiod_chip_open_by_name(EPD_GPIO_CHIP); + chip = gpio_chip_open(EPD_GPIO_CHIP); if (!chip) { perror("Error opening GPIO chip"); exit(1); } - epd_dc_line = gpiod_chip_get_line(chip, EPD_DC_PIN); - epd_rst_line = gpiod_chip_get_line(chip, EPD_RST_PIN); - epd_busy_line = gpiod_chip_get_line(chip, EPD_BUSY_PIN); + epd_dc_line = gpio_line_request_output(chip, EPD_DC_PIN, "epd_dc", 0); + epd_rst_line = gpio_line_request_output(chip, EPD_RST_PIN, "epd_rst", 0); + epd_busy_line = gpio_line_request_input(chip, EPD_BUSY_PIN, "epd_busy"); if (!epd_dc_line || !epd_rst_line || !epd_busy_line) { - perror("Error getting GPIO lines"); - gpiod_chip_close(chip); + perror("Error requesting GPIO lines"); + gpio_chip_close(chip); exit(1); } - gpiod_line_request_output(epd_dc_line, "epd_dc", 0); - gpiod_line_request_output(epd_rst_line, "epd_rst", 0); - gpiod_line_request_input(epd_busy_line, "epd_busy"); srand(time(NULL)); epd_init(); @@ -394,6 +393,9 @@ int main(void) { } close(spi_fd); - gpiod_chip_close(chip); + gpio_line_release(epd_dc_line); + gpio_line_release(epd_rst_line); + gpio_line_release(epd_busy_line); + gpio_chip_close(chip); return 0; } From fb2a21b430679dac4a353155777ed8594371e1a3 Mon Sep 17 00:00:00 2001 From: Jason Yang Date: Wed, 14 Oct 2026 13:50:47 +0800 Subject: [PATCH 3/4] pixpaper-213-m: kakip: open the pinctrl gpiochip by label Open the RZ/V2H pinctrl by its label instead of gpiochip0; the number depends on which GPIO controllers probe first. --- 2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c b/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c index 3b44102..dc67305 100644 --- a/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c +++ b/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c @@ -34,7 +34,7 @@ #include "../../../common/gpiod_compat.h" #define EPD_SPI_DEVICE "/dev/spidev0.0" -#define EPD_GPIO_CHIP "gpiochip0" +#define EPD_GPIO_CHIP_LABEL "10410000.pinctrl" #define EPD_DC_PIN 60 #define EPD_RST_PIN 87 @@ -354,7 +354,7 @@ int main(void) { uint32_t speed = SPI_SPEED; ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); - chip = gpio_chip_open(EPD_GPIO_CHIP); + chip = gpio_chip_open_by_label(EPD_GPIO_CHIP_LABEL); if (!chip) { perror("Error opening GPIO chip"); exit(1); From e8491a1022430bf48d3aa0d9f333fd0223746e58 Mon Sep 17 00:00:00 2001 From: Jason Yang Date: Wed, 14 Oct 2026 13:53:27 +0800 Subject: [PATCH 4/4] pixpaper-213-m: kakip: make the SPI device selectable On the Kakip 6.1 kernel the 40-pin header is SPI bus 1, so default to /dev/spidev1.0 and take the device path as the first argument: sudo ./robot-eye # /dev/spidev1.0 sudo ./robot-eye /dev/spidev1.1 # second chip select --- 2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c b/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c index dc67305..838b79d 100644 --- a/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c +++ b/2.13/mono/spi/pixpaper-213-m-robot-eye-kakip.c @@ -13,6 +13,7 @@ * * Build (target): gcc -o robot-eye pixpaper-213-m-robot-eye-kakip.c -lgpiod -lm * (libgpiod 1.x or 2.x, see common/gpiod_compat.h) + * Usage: robot-eye [/dev/spidevB.C] (default /dev/spidev1.0) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,7 +34,7 @@ #include "../../../common/gpiod_compat.h" -#define EPD_SPI_DEVICE "/dev/spidev0.0" +#define EPD_SPI_DEVICE "/dev/spidev1.0" #define EPD_GPIO_CHIP_LABEL "10410000.pinctrl" #define EPD_DC_PIN 60 @@ -342,8 +343,8 @@ void pick_gaze(int *tx, int *ty) { } while (*tx * *tx + *ty * *ty > GAZE_MAX_X * GAZE_MAX_X); } -int main(void) { - spi_fd = open(EPD_SPI_DEVICE, O_RDWR); +int main(int argc, char **argv) { + spi_fd = open(argc > 1 ? argv[1] : EPD_SPI_DEVICE, O_RDWR); if (spi_fd < 0) { perror("Error opening SPI device"); exit(1);