User Tools

Site Tools


raspberry:raspberry-gpio

GPIO

The old sysfs GPIO interface is deprecated and will be phased out of the kernel. As part of this the base offset of GPIO ports has been omitted in the kernel. Setting the base GPIO number to zero will result in a boot up error on kernels from 6.6. At the same time, the sysfs interface to the GPIOs has been deprecated - some distributions have already disabled it and are expecting users to go through libgpiod. In 6.6, high-numbered GPIOs - probably still available by sysfs, but eventually that will be switched off.

Query GPIO ports

From 6.5 onwards, the Pi kernel is not forcing a base GPIO for the main GPIO driver. Whilst the sysfs API is still enabled, the base GPIOs will change. The dynamic assignment generally starts at 512 and counts down. If you really need to track down a GPIO using sysfs, look through /sys/class/gpiochipN/ for a device with an appropriate label or ngpio, and then use base to determine the GPIO number.

cat /sys/kernel/debug/gpio
--------------------------
gpiochip0: GPIOs 512-565, parent: platform/3f200000.gpio, pinctrl-bcm2835:
 gpio-512 (ID_SDA              )
 gpio-513 (ID_SCL              )
 gpio-514 (GPIO2               )
 gpio-515 (GPIO3               )
 gpio-516 (GPIO4               )
 gpio-517 (GPIO5               |sysfs               ) in  hi 
 gpio-518 (GPIO6               )
 gpio-519 (GPIO7               )
 gpio-520 (GPIO8               )
 gpio-521 (GPIO9               )
 gpio-522 (GPIO10              )
 gpio-523 (GPIO11              )
 gpio-524 (GPIO12              )
 gpio-525 (GPIO13              )
 gpio-526 (GPIO14              )
 gpio-527 (GPIO15              )
 gpio-528 (GPIO16              )
 gpio-529 (GPIO17              )
 gpio-530 (GPIO18              )
 gpio-531 (GPIO19              )
 gpio-532 (GPIO20              )
 gpio-533 (GPIO21              )
 gpio-534 (GPIO22              )
 gpio-535 (GPIO23              )
 gpio-536 (GPIO24              )
 gpio-537 (GPIO25              )
 gpio-538 (GPIO26              )
 gpio-539 (GPIO27              )
 gpio-540 (SDA0                )
 gpio-541 (SCL0                )
 gpio-542 (NC                  )
 gpio-543 (LAN_RUN             )
 gpio-544 (CAM_GPIO1           )
 gpio-545 (NC                  )
 gpio-546 (NC                  )
 gpio-547 (PWR_LOW_N           |PWR                 ) in  lo 
 gpio-548 (NC                  )
 gpio-549 (NC                  )
 gpio-550 (USB_LIMIT           )
 gpio-551 (NC                  )
 gpio-552 (PWM0_OUT            )
 gpio-553 (CAM_GPIO0           |cam1_regulator      ) out lo 
 gpio-554 (SMPS_SCL            )
 gpio-555 (SMPS_SDA            )
 gpio-556 (ETH_CLK             )
 gpio-557 (PWM1_OUT            )
 gpio-558 (HDMI_HPD_N          |hpd                 ) in  hi ACTIVE LOW
 gpio-559 (STATUS_LED          |ACT                 ) out lo 
 gpio-560 (SD_CLK_R            )
 gpio-561 (SD_CMD_R            )
 gpio-562 (SD_DATA0_R          )
 gpio-563 (SD_DATA1_R          )
 gpio-564 (SD_DATA2_R          )
 gpio-565 (SD_DATA3_R          )

gpiochip1: GPIOs 566-569, parent: usb/1-1.4:1.0, ftdi-cbus, can sleep:

GPIOD

You can install the libraries and tools to interact with this device, using sudo apt install gpiod. This will install the gpiod tools, and the libgpiod2 library to let you interact with the device from your own code.

# apt install gpiod --> runtime
# apt install libgpiod2  --> build libraries
  • gpiodetect – list all gpiochips present on the system, their names, labels and number of GPIO lines
  • gpioinfo – list all lines of specified gpiochips, their names, consumers, direction, active state and additional flags
  • gpioget – read values of specified GPIO lines
  • gpioset – set values of specified GPIO lines, potentially keep the lines exported and wait until timeout, user input or signal
  • gpiofind – find the gpiochip name and line offset given the line name
  • gpiomon – wait for events on GPIO lines, specify which events to watch, how many events to process before exiting or if the events should be reported to the console

gpiodetect

The first column represents name of the chip (also the device node name). The second column (in square brackets) is the device label which is normally set by the GPIO driver (this will match the value of /sys/class/gpio/gpiochipX/label). The last column shows how many GPIO lines the chip has.

# gpiodetect
------------
gpiochip0 [pinctrl-bcm2835] (54 lines)
gpiochip1 [ftdi-cbus] (4 lines)
# gpioinfo
----------
gpiochip0 - 54 lines:
	line   0:     "ID_SDA"       unused   input  active-high 
	line   1:     "ID_SCL"       unused   input  active-high 
	line   2:      "GPIO2"       unused   input  active-high 
	line   3:      "GPIO3"       unused   input  active-high 
	line   4:      "GPIO4"       unused   input  active-high 
	line   5:      "GPIO5"      "sysfs"   input  active-high [used]
	line   6:      "GPIO6"       unused   input  active-high 
	line   7:      "GPIO7"       unused   input  active-high 
	line   8:      "GPIO8"       unused   input  active-high 
	line   9:      "GPIO9"       unused   input  active-high 
	line  10:     "GPIO10"       unused   input  active-high 
	line  11:     "GPIO11"       unused   input  active-high 
	line  12:     "GPIO12"       unused   input  active-high 
	line  13:     "GPIO13"       unused   input  active-high 
	line  14:     "GPIO14"       unused   input  active-high 
	line  15:     "GPIO15"       unused   input  active-high 
	line  16:     "GPIO16"       unused   input  active-high 
	line  17:     "GPIO17"       unused   input  active-high 
	line  18:     "GPIO18"       unused   input  active-high 
	line  19:     "GPIO19"       unused   input  active-high 
	line  20:     "GPIO20"       unused   input  active-high 
	line  21:     "GPIO21"       unused   input  active-high 
	line  22:     "GPIO22"       unused   input  active-high 
	line  23:     "GPIO23"       unused   input  active-high 
	line  24:     "GPIO24"       unused   input  active-high 
	line  25:     "GPIO25"       unused   input  active-high 
	line  26:     "GPIO26"       unused   input  active-high 
	line  27:     "GPIO27"       unused   input  active-high 
	line  28:       "SDA0"       unused   input  active-high 
	line  29:       "SCL0"       unused   input  active-high 
	line  30:         "NC"       unused   input  active-high 
	line  31:    "LAN_RUN"       unused  output  active-high 
	line  32:  "CAM_GPIO1"       unused  output  active-high 
	line  33:         "NC"       unused   input  active-high 
	line  34:         "NC"       unused   input  active-high 
	line  35:  "PWR_LOW_N"        "PWR"   input  active-high [used]
	line  36:         "NC"       unused   input  active-high 
	line  37:         "NC"       unused   input  active-high 
	line  38:  "USB_LIMIT"       unused  output  active-high 
	line  39:         "NC"       unused   input  active-high 
	line  40:   "PWM0_OUT"       unused   input  active-high 
	line  41:  "CAM_GPIO0" "cam1_regulator" output active-high [used]
	line  42:   "SMPS_SCL"       unused  output  active-high 
	line  43:   "SMPS_SDA"       unused   input  active-high 
	line  44:    "ETH_CLK"       unused   input  active-high 
	line  45:   "PWM1_OUT"       unused   input  active-high 
	line  46: "HDMI_HPD_N"        "hpd"   input   active-low [used]
	line  47: "STATUS_LED"        "ACT"  output  active-high [used]
	line  48:   "SD_CLK_R"       unused   input  active-high 
	line  49:   "SD_CMD_R"       unused   input  active-high 
	line  50: "SD_DATA0_R"       unused   input  active-high 
	line  51: "SD_DATA1_R"       unused   input  active-high 
	line  52: "SD_DATA2_R"       unused   input  active-high 
	line  53: "SD_DATA3_R"       unused   input  active-high 
gpiochip1 - 4 lines:
	line   0:      unnamed       kernel   input  active-high [used]
	line   1:      unnamed       kernel   input  active-high [used]
	line   2:      unnamed       kernel   input  active-high [used]
	line   3:      unnamed       kernel   input  active-high [used]
raspberry/raspberry-gpio.txt · Last modified: by oscar