# Universal GPIO Access

The library [libgpiod](https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/about/?h=v1.6.x) provides universal access to GPIOs of any device running Linux. It detects available GPIO, can read and write data to them, and wait for events to be triggered. With this, you can write ow code to talk UART to any connected device.

To install it, run the following commands:

```bash
apt-get install autoconf autoconf-archive libtool libkmod-dev pkg-config
git clone https://github.com/brgl/libgpiod.git

cd libgpiod
./autogen.sh --enable-tools=yes --prefix=/usr/local/bin
make
sudo make install
```

If compilation and installation is successful, in the subfolder `./tools` you will find binaries like `gpiodetect` and `gpioinfo` that you can use to explore the GPIOs. See the following examples.

```bash
$> ./tools/gpiodetect
gpiochip0 [pinctrl-bcm2711] (58 lines)
gpiochip1 [raspberrypi-exp-gpio] (8 lines)

./tools/gpioinfo
gpiochip0 - 58 lines:
 line   0:     "ID_SDA"       unused   input  active-high
 line   1:     "ID_SCL"       unused   input  active-high
 line   2:       "SDA1"       unused   input  active-high
 line   3:       "SCL1"       unused   input  active-high
 line   4:  "GPIO_GCLK"       unused   input  active-high
 line   5:      "GPIO5"       unused   input  active-high
...
```

If you want to work with the library, read [this article](https://www.beyondlogic.org/an-introduction-to-chardev-gpio-and-libgpiod-on-the-raspberry-pi/) for a detailed introduction.

### Conclusion

For working with I2C, SPI and UART on the Raspberry Pi, not only Python, but C++ libraries can be used as well. Specifically, you need to activate the I2C and SPI functions via `raspi-config`, which infernally loads the appropriate Kernel modules. Then you choose a client library and other necessary C++ headers. Working with the libraries follows the same principles: Determine the connected device file, configure a connection object, open the device file, then read from/write to it. Finally, the handy library libgpiod can help you to access all GPIO pins directly, which can be helpful for debugging.
