Please enable javascript, or click here to visit my ecommerce web site powered by Shopify.
Jump to: navigation, search

Difference between revisions of "Gpiod"

(Created page with "{{Languages|gpiod}} = Use gpiod to control GPIO pins = Traditionally GPIO pins are exposed by Linux kernel under <code>/sys/class/gpio</code> namespace. However, this interf...")
 
(Set GPIO pin level)
 
(2 intermediate revisions by the same user not shown)
Line 132: Line 132:
 
== Set GPIO pin level ==
 
== Set GPIO pin level ==
  
     radxa@radxa-zero2:~$ sudo gpioset $(sudo gpiofind PIN_8)=0
+
     radxa@radxa-zero2:~$ sudo gpioset -m signal $(sudo gpiofind PIN_8)=0
     radxa@radxa-zero2:~$ sudo gpioset $(sudo gpiofind PIN_8)=1
+
     radxa@radxa-zero2:~$ sudo gpioset -m signal $(sudo gpiofind PIN_8)=1
 +
 
 +
== Watch GPIO level change ==
 +
 
 +
You need to specify either <code>-r</code> for rising edge events or <code>-f</code> for falling edge events, but not both.
 +
 
 +
    radxa@radxa-zero2:~$ sudo gpiomon -f $(sudo gpiofind PIN_37)
 +
    event: FALLING EDGE offset: 0 timestamp: [    3882.313792858]
 +
 
 +
== A simple GPIO test script based on gpiod ==
 +
 
 +
[https://gist.github.com/RadxaYuntian/5ef87b9655239e65c9ccb7897c5f618a Here] is a sample script that uses <code>gpiod</code> to test all onboard GPIO pins.

Latest revision as of 10:21, 31 October 2022

Use gpiod to control GPIO pins

Traditionally GPIO pins are exposed by Linux kernel under /sys/class/gpio namespace. However, this interface has been deprecated since version 4.8. The replacement is a C binding called libgpiod, along with a set of user land tools called gpiod.

In this guide we will talk about some common usage of gpiod so you can start incorporate them in your projects.

Installation

Run following command under Debian/Ubuntu:

  sudo apt update
  sudo apt install gpiod -y

List available GPIO controller

   radxa@radxa-zero2:~$ sudo gpioinfo
   gpiochip0 - 85 lines:
           line   0:     "PIN_27"       unused   input  active-high 
           line   1:     "PIN_28"       unused   input  active-high 
           line   2:      "PIN_7"       unused   input  active-high 
           line   3:     "PIN_11"       unused   input  active-high 
           line   4:     "PIN_13"       unused   input  active-high 
           line   5:     "PIN_14"       unused   input  active-high 
           line   6:     "PIN_18"       unused   input  active-high 
           line   7:     "PIN_40"       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:     "PIN_19"       unused   input  active-high 
           line  21:     "PIN_21"       unused   input  active-high 
           line  22:     "PIN_24"       unused   input  active-high 
           line  23:     "PIN_23"       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       unused   input  active-high 
           line  36:      unnamed       unused   input  active-high 
           line  37:      unnamed      "reset"  output   active-low [used]
           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         "cd"   input   active-low [used]
           line  48:     "PIN_36"       unused   input  active-high 
           line  49:     "PIN_32"       unused   input  active-high 
           line  50:     "PIN_12"       unused   input  active-high 
           line  51:     "PIN_35"       unused   input  active-high 
           line  52:      unnamed       unused   input  active-high 
           line  53:      unnamed       unused   input  active-high 
           line  54:     "PIN_38"       unused   input  active-high 
           line  55:      unnamed       unused   input  active-high 
           line  56:      unnamed       unused   input  active-high 
           line  57:      unnamed       unused   input  active-high 
           line  58:      unnamed       unused   input  active-high 
           line  59:      unnamed       unused   input  active-high 
           line  60:      unnamed       unused   input  active-high 
           line  61:      unnamed "green:status" output active-high [used]
           line  62:     "PIN_31"       unused   input  active-high 
           line  63:      "PIN_3"       unused   input  active-high 
           line  64:     "PIN_25"       unused   input  active-high 
           line  65:      unnamed       unused   input  active-high 
           line  66:      unnamed       unused   input  active-high 
           line  67:      unnamed       unused   input  active-high 
           line  68:      unnamed       unused   input  active-high 
           line  69:      unnamed       unused   input  active-high 
           line  70:      unnamed       unused   input  active-high 
           line  71:      unnamed      "reset"  output   active-low [used]
           line  72:      unnamed       unused   input  active-high 
           line  73:      unnamed       unused   input  active-high 
           line  74:      unnamed       unused   input  active-high 
           line  75:      unnamed       unused   input  active-high 
           line  76:      unnamed       unused   input  active-high 
           line  77:      unnamed       unused   input  active-high 
           line  78:      unnamed       unused   input  active-high 
           line  79:      unnamed       unused   input  active-high 
           line  80:      unnamed       unused   input  active-high 
           line  81:      unnamed       unused   input  active-high 
           line  82:      unnamed   "shutdown"  output  active-high [used]
           line  83:      unnamed       unused   input  active-high 
           line  84:      unnamed       unused   input  active-high 
   gpiochip1 - 15 lines:
           line   0:      "PIN_8"       unused   input  active-high 
           line   1:     "PIN_10"       unused   input  active-high 
           line   2:      unnamed       unused   input  active-high 
           line   3:      unnamed      "power"   input   active-low [used]
           line   4:      unnamed       unused  output  active-high 
           line   5:      unnamed       unused   input  active-high 
           line   6:      unnamed       unused   input  active-high 
           line   7:     "PIN_29"       unused   input  active-high 
           line   8:     "PIN_33"       unused   input  active-high 
           line   9:     "PIN_37"       unused   input  active-high 
           line  10:      unnamed       unused   input  active-high 
           line  11:      unnamed       unused  output  active-high 
           line  12:      unnamed       unused   input  active-high 
           line  13:      unnamed       unused   input  active-high 
           line  14:      unnamed       unused   input  active-high

Convert 40-pin header number to gpiod device number

   radxa@radxa-zero2:~$ sudo gpiofind PIN_8
   gpiochip1 0

Read GPIO pin level

   radxa@radxa-zero2:~$ sudo gpioget $(sudo gpiofind PIN_8)
   1

Set GPIO pin level

   radxa@radxa-zero2:~$ sudo gpioset -m signal $(sudo gpiofind PIN_8)=0
   radxa@radxa-zero2:~$ sudo gpioset -m signal $(sudo gpiofind PIN_8)=1

Watch GPIO level change

You need to specify either -r for rising edge events or -f for falling edge events, but not both.

   radxa@radxa-zero2:~$ sudo gpiomon -f $(sudo gpiofind PIN_37)
   event: FALLING EDGE offset: 0 timestamp: [    3882.313792858]

A simple GPIO test script based on gpiod

Here is a sample script that uses gpiod to test all onboard GPIO pins.