Skip to content

Commit

Permalink
gpio_api: API for GPIO controllers
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
LekKit authored Mar 7, 2024
1 parent 8e44475 commit 2b526a0
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/devices/gpio_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
gpio_api.h - General-Purpose IO API
Copyright (C) 2024 LekKit <github.com/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 <https://www.gnu.org/licenses/>.
*/

#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

0 comments on commit 2b526a0

Please sign in to comment.