openSUSE:GPIO

Jump to: navigation, search

To manage GPIO, you can use tools from 'libgpiod' package, available for kernel >= 4.8, which includes Leap 15.0+ and Tumbleweed. You can also use the deprecated 'sysfs' interface.

libgpiod

Installation

To install libgpiod, just install it from main OSS repo. For Leap 15.0, you need to install it from 'hardware' repo. One click installation is also available : https://software.opensuse.org/package/libgpiod

Check available GPIO: gpiodetect

gpiodetect will list all GPIO chips, print their labels and number of GPIO lines.

 gpiodetect

will return for the RPi2:

 gpiochip0 [pinctrl-bcm2835] (54 lines)


Get information of current GPIO: gpioinfo

 gpioinfo gpiochip0

will list all lines of specified gpiochips (here, gpiochip0), their names, consumers, direction, active state and additional flags. For RPi2, it returns:

 gpiochip0 - 54 lines:
       line   0:      unnamed       unused   input  active-high 
       line   1:      unnamed       unused   input  active-high 
       line   2:      unnamed       unused   input  active-high 
       line   3:      unnamed       unused   input  active-high 
       line   4:      unnamed       unused   input  active-high 
       line   5:      unnamed       unused   input  active-high 
       line   6:      unnamed       unused   input  active-high 
       line   7:      unnamed       unused   input  active-high 
       line   8:      unnamed       unused   input  active-high 
       line   9:      unnamed       unused   input  active-high 
       line  10:      unnamed       unused   input  active-high 
       line  11:      unnamed       unused   input  active-high 
       line  12:      unnamed       unused   input  active-high 
       line  13:      unnamed       unused   input  active-high 
       line  14:      unnamed       unused   input  active-high 
       line  15:      unnamed       unused   input  active-high 
       line  16:      unnamed       unused   input  active-high 
       line  17:      unnamed       unused   input  active-high 
       line  18:      unnamed       unused   input  active-high 
       line  19:      unnamed       unused   input  active-high 
       line  20:      unnamed       unused   input  active-high 
       line  21:      unnamed       unused   input  active-high 
       line  22:      unnamed       unused   input  active-high 
       line  23:      unnamed       unused   input  active-high 
       line  24:      unnamed       unused   input  active-high 
       line  25:      unnamed       unused   input  active-high 
       line  26:      unnamed       unused   input  active-high 
       line  27:      unnamed       unused   input  active-high 
       line  28:      unnamed       unused   input  active-high 
       line  29:      unnamed       unused   input  active-high 
       line  30:      unnamed       unused   input  active-high 
       line  31:      unnamed       unused   input  active-high 
       line  32:      unnamed       unused   input  active-high 
       line  33:      unnamed       unused   input  active-high 
       line  34:      unnamed       unused   input  active-high 
       line  35:      unnamed       "led1"  output  active-high [used]
       line  36:      unnamed       unused   input  active-high 
       line  37:      unnamed       unused   input  active-high 
       line  38:      unnamed       unused   input  active-high 
       line  39:      unnamed       unused   input  active-high 
       line  40:      unnamed       unused   input  active-high 
       line  41:      unnamed       unused   input  active-high 
       line  42:      unnamed       unused   input  active-high 
       line  43:      unnamed       unused   input  active-high 
       line  44:      unnamed       unused   input  active-high 
       line  45:      unnamed       unused   input  active-high 
       line  46:      unnamed       unused   input  active-high 
       line  47:      unnamed       "led0"  output  active-high [used]
       line  48:      unnamed       unused   input  active-high 
       line  49:      unnamed       unused   input  active-high 
       line  50:      unnamed       unused   input  active-high 
       line  51:      unnamed       unused   input  active-high 
       line  52:      unnamed       unused   input  active-high 
       line  53:      unnamed       unused   input  active-high

Read values (input): gpioget

 gpioget gpiochip0 4

will read value of gpio 4 from gpiochip0 and will returns:

 1
 gpioget gpiochip0 4 5

will read value of gpio 4 and 5, from gpiochip0 and will returns:

 1 1

Write values (output): gpioset

 gpioset gpiochip0 4=1

will set the value of GPIO 4 to 1 and exit immediately.

 gpioset gpiochip0 4=1 5=0

will set the value of GPIO 4 to 1 and GPIO 5 to 0.

With --mode= option, you can wait for a signal, or an amount of time before exiting.


libgpiod bindings (C++ and Python)

You can use C++ binding. Examples are provided here: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/cxx/examples

For Python binding, you need to install python-libgpiod. Examples are here: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/python/examples


sysfs interface (deprecated)

This old (deprecated) interface is still working.

export: get access to your GPIO

 echo 229 > /sys/class/gpio/export

will get access to GPIO 229.

Note that the base, which is the N from /sys/class/gpio/gpiochipN, must be added to the GPIO number. This is never mentioned because on Raspbian N is 0.

There may be more than one chip providing GPIO. Try looking in /sys/class/gpio/gpiochipN/label to find out which is which.

direction: choose input or output pin

 echo in > /sys/class/gpio/gpio229/direction

will set GPIO 229 to input.

 echo out > /sys/class/gpio/gpio229/direction

will set GPIO 229 to out, with initial value of 0, otherwise, use high value to set as out, default to 1.

value: read/write value

 cat /sys/class/gpio/gpio229/value

will output the current value of GPIO 229.

 echo 1 > /sys/class/gpio/gpio229/value

will set GPIO 229 to 1.

unexport: release access to GPIO

Once done, release access to GPIO with:

 echo 229 > /sys/class/gpio/unexport