From 2b526a0c136a84f16a95446ecb52f52cb3678585 Mon Sep 17 00:00:00 2001 From: LekKit <50500857+LekKit@users.noreply.github.com> Date: Thu, 7 Mar 2024 19:04:37 +0200 Subject: [PATCH] gpio_api: API for GPIO controllers - Somewhat similar to chardev API, but for GPIO pins - For controllers: Use gpio_pins_out() to signal about pins state change; Hangle gpio->pins_in() & gpio->pins_read() for setting input pins or reading output pins - For devices: Use gpio_pins_in() to write pins to controller, gpio_pins_read() to read output pins; Handle (optionally) gpio->pins_out() to be notified when output pins change - Request for comments --- src/devices/gpio_api.h | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/devices/gpio_api.h diff --git a/src/devices/gpio_api.h b/src/devices/gpio_api.h new file mode 100644 index 000000000..56a510941 --- /dev/null +++ b/src/devices/gpio_api.h @@ -0,0 +1,68 @@ +/* +gpio_api.h - General-Purpose IO API +Copyright (C) 2024 LekKit + +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 +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef RVVM_GPIO_API_H +#define RVVM_GPIO_API_H + +typedef struct rvvm_gpio_dev rvvm_gpio_dev_t; + +struct rvvm_gpio_dev { + // IO Dev -> GPIO Dev calls + bool (*pins_out)(rvvm_gpio_dev_t* dev, size_t off, uint32_t pins); + + // GPIO Dev -> IO Dev calls + bool (*pins_in)(rvvm_gpio_dev_t* dev, size_t off, uint32_t pins); + uint32_t (*pins_read)(rvvm_gpio_dev_t* dev, size_t off); + + // Common RVVM API features + void (*update)(rvvm_gpio_dev_t* dev); + void (*remove)(rvvm_gpio_dev_t* dev); + + void* data; + void* io_dev; +}; + +static inline bool gpio_pins_out(rvvm_gpio_dev_t* dev, size_t off, uint32_t pins) +{ + if (dev) return dev->pins_out(dev, off, pins); + return false; +} + +static inline bool gpio_pins_in(rvvm_gpio_dev_t* dev, size_t off, uint32_t pins) +{ + if (dev) return dev->pins_in(dev, off, pins); + return false; +} + +static inline uint32_t gpio_pins_read(rvvm_gpio_dev_t* dev, size_t off) +{ + if (dev) return dev->pins_read(dev, off); + return 0; +} + +static inline void gpio_free(rvvm_gpio_dev_t* dev) +{ + if (dev && dev->remove) dev->remove(dev); +} + +static inline void gpio_update(rvvm_gpio_dev_t* dev) +{ + if (dev && dev->update) dev->update(dev); +} + +#endif