Toggling GPIO on BeagleBoneBlack with libgpiod

Hello,

I have issues toggling GPIO pins with the gpioset tool provided by libgpiod-dev.

I detect the line first:

gpioinfo | grep P9_12
debian@beaglebone:/lib/firmware$ gpioinfo | grep P9_12
	line  28:      "P9_12"       unused  output  active-high 

then I tried this with a connected LED

gpioset gpiochip1 28=1

but this does not really work, the pin seems to be floating, I can not really control the pin.

It works when using the sys/class/gpio interface:

cd /sys/class/gpio
echo 60 > export
cd gpio60
echo "out" > direction
echo 1 > value

Anyone have any idea why the gpiod interface does not work?
Some information about Linux/capes:

debian@beaglebone:/sys/class/gpio/gpio60$ uname -r
5.10.41-ti-rt-r10
debian@beaglebone:/sys/class/gpio/gpio60$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 10 (buster)
Release:	10
Codename:	buster
debian@beaglebone:/sys/class/gpio/gpio60$ sudo /opt/scripts/tools/version.sh 
git:/opt/scripts/:[e8ae28ccc34a177e9435a0d24cdf8421e081c19a]
eeprom:[A335BNLT000C2043BBBG0470]
model:[TI_AM335x_BeagleBone_Black]
bootloader:[eMMC-(default)]:[/dev/mmcblk1]:[U-Boot SPL 2019.04-00004-g72e2ec7 (Aug 02 2021 - 17:06:40 +0200)]:[location: dd MBR]
bootloader:[eMMC-(default)]:[/dev/mmcblk1]:[U-Boot 2019.04-00004-g72e2ec7]:[location: dd MBR]
UBOOT: Booted Device-Tree:[am335x-boneblack-uboot.dts]
UBOOT: Loaded Overlay:[BB-ADC-00A0.kernel]
UBOOT: Loaded Overlay:[BB-BONE-eMMC1-01-00A0.kernel]
UBOOT: Loaded Overlay:[BB-HDMI-TDA998x-00A0.kernel]
kernel:[5.10.41-ti-rt-r10]
/boot/uEnv.txt Settings:
uboot_overlay_options:[enable_uboot_overlays=1]
pkg check: to individually upgrade run: [sudo apt install --only-upgrade <pkg>]
pkg:[bb-cape-overlays]:[4.14.20210821.0-0~buster+20210821]
pkg:[bb-customizations]:[1.20221108.0-0~buster+20221108]
WARNING:pkg:[bb-usb-gadgets]:[NOT_INSTALLED]
WARNING:pkg:[bb-wl18xx-firmware]:[NOT_INSTALLED]
pkg:[kmod]:[26-1]
WARNING:pkg:[librobotcontrol]:[NOT_INSTALLED]
pkg:[firmware-ti-connectivity]:[20190717-2rcnee1~buster+20200305]
groups:[debian : debian adm kmem dialout cdrom floppy audio dip video plugdev users systemd-journal input netdev i2c gpio admin spi iio docker tisdk weston-launch xenomai bluetooth cloud9ide pwm eqep remoteproc]
cmdline:[console=ttyO0,115200n8 root=UUID=564e4313-578f-48a2-a8e4-49dabe86b8db ro rootfstype=ext4 rootwait]
dmesg | grep remote
[    4.763773] remoteproc remoteproc0: wkup_m3 is available
[    5.221582] remoteproc remoteproc0: powering up wkup_m3
[    5.224440] remoteproc remoteproc0: Booting fw image am335x-pm-firmware.elf, size 217148
[    5.224737] remoteproc remoteproc0: remote processor wkup_m3 is now up
[   12.431327] remoteproc remoteproc1: 4a334000.pru is available
[   12.451596] remoteproc remoteproc2: 4a338000.pru is available
dmesg | grep pru
[   12.431327] remoteproc remoteproc1: 4a334000.pru is available
[   12.451596] remoteproc remoteproc2: 4a338000.pru is available
dmesg | grep pinctrl-single
[    4.514937] pinctrl-single 44e10800.pinmux: 142 pins, size 568
dmesg | grep gpio-of-helper
lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
END

Kind Regards

For example

Hello,

Some further testing: I updated debian to 11 (bullseye) now, but still can’t use libgpiod to toggle GPIOs.
I tried using the Perl script to print out pins

debian@beaglebone:/opt/scripts/device/bone$ sudo /opt/scripts/device/bone/show-pins.pl 
parse error at /opt/scripts/device/bone/show-pins.pl line 129, <> line 2.

Maybe this is a hint to something being wrong?

UPDATE: There seems to be a patch commit for the show-pins perl utility: Fix parse error on kernel 5.10 · mvduin/bbb-pin-utils@d51ed53 · GitHub . Might try that out…

Kind Regards

Okay, I managed to get the show-pins perl script to work with this fix: Fix parse error for show-pins.pl by robamu · Pull Request #132 · RobertCNelson/boot-scripts · GitHub

debian@beaglebone:/opt/scripts/device/bone$ sudo ./show-pins.pl 
P8.25 / eMMC d0                    0  U7 @44e10800 00000031 fast rx  up  m1 mmc 1 d0         mmc@481d8000 (pinmux_emmc_pins)
P8.24 / eMMC d1                    1  V7 @44e10804 00000031 fast rx  up  m1 mmc 1 d1         mmc@481d8000 (pinmux_emmc_pins)
P8.05 / eMMC d2                    2  R8 @44e10808 00000031 fast rx  up  m1 mmc 1 d2         mmc@481d8000 (pinmux_emmc_pins)
P8.06 / eMMC d3                    3  T8 @44e1080c 00000031 fast rx  up  m1 mmc 1 d3         mmc@481d8000 (pinmux_emmc_pins)
P8.23 / eMMC d4                    4  U8 @44e10810 00000031 fast rx  up  m1 mmc 1 d4         mmc@481d8000 (pinmux_emmc_pins)
P8.22 / eMMC d5                    5  V8 @44e10814 00000031 fast rx  up  m1 mmc 1 d5         mmc@481d8000 (pinmux_emmc_pins)
P8.03 / eMMC d6                    6  R9 @44e10818 00000031 fast rx  up  m1 mmc 1 d6         mmc@481d8000 (pinmux_emmc_pins)
P8.04 / eMMC d7                    7  T9 @44e1081c 00000031 fast rx  up  m1 mmc 1 d7         mmc@481d8000 (pinmux_emmc_pins)
P8.19                              8 U10 @44e10820 00000027 fast rx down m7 gpio 0.22
P8.13                              9 T10 @44e10824 00000027 fast rx down m7 gpio 0.23
P8.14                             10 T11 @44e10828 00000027 fast rx down m7 gpio 0.26
P8.17                             11 U12 @44e1082c 00000027 fast rx down m7 gpio 0.27
P8.12                             12 T12 @44e10830 00000027 fast rx down m7 gpio 1.12
P8.11                             13 R12 @44e10834 00000027 fast rx down m7 gpio 1.13
P8.16                             14 V13 @44e10838 00000027 fast rx down m7 gpio 1.14
P8.15                             15 U13 @44e1083c 00000027 fast rx down m7 gpio 1.15
P9.15                             16 R13 @44e10840 00000027 fast rx down m7 gpio 1.16
P9.23                             17 V14 @44e10844 00000027 fast rx down m7 gpio 1.17
P9.14                             18 U14 @44e10848 00000027 fast rx down m7 gpio 1.18
P9.16                             19 T14 @44e1084c 00000027 fast rx down m7 gpio 1.19
P9.11                             28 T17 @44e10870 00000037 fast rx  up  m7 gpio 0.30
P9.13                             29 U17 @44e10874 00000037 fast rx  up  m7 gpio 0.31
P9.12                             30 U18 @44e10878 00000037 fast rx  up  m7 gpio 1.28
P8.26                             31  V6 @44e1087c 00000037 fast rx  up  m7 gpio 1.29
P8.21 / eMMC clk                  32  U9 @44e10880 00000032 fast rx  up  m2 mmc 1 clk        mmc@481d8000 (pinmux_emmc_pins)
P8.20 / eMMC cmd                  33  V9 @44e10884 00000032 fast rx  up  m2 mmc 1 cmd        mmc@481d8000 (pinmux_emmc_pins)
P9.15                             34 T13 @44e10888 00000037 fast rx  up  m7 gpio 2.00
P8.18                             35 V12 @44e1088c 00000027 fast rx down m7 gpio 2.01
P8.07                             36  R7 @44e10890 00000037 fast rx  up  m7 gpio 2.02
P8.08                             37  T7 @44e10894 00000037 fast rx  up  m7 gpio 2.03
P8.10                             38  U6 @44e10898 00000037 fast rx  up  m7 gpio 2.04
P8.09                             39  T6 @44e1089c 00000037 fast rx  up  m7 gpio 2.05
P8.45 / hdmi / sysboot 0          40  R1 @44e108a0 00000008 fast         m0 lcd d0           0-0070 (nxp_hdmi_bonelt_pins)
P8.46 / hdmi / sysboot 1          41  R2 @44e108a4 00000008 fast         m0 lcd d1           0-0070 (nxp_hdmi_bonelt_pins)
P8.43 / hdmi / sysboot 2          42  R3 @44e108a8 00000008 fast         m0 lcd d2           0-0070 (nxp_hdmi_bonelt_pins)
P8.44 / hdmi / sysboot 3          43  R4 @44e108ac 00000008 fast         m0 lcd d3           0-0070 (nxp_hdmi_bonelt_pins)
P8.41 / hdmi / sysboot 4          44  T1 @44e108b0 00000008 fast         m0 lcd d4           0-0070 (nxp_hdmi_bonelt_pins)
P8.42 / hdmi / sysboot 5          45  T2 @44e108b4 00000008 fast         m0 lcd d5           0-0070 (nxp_hdmi_bonelt_pins)
P8.39 / hdmi / sysboot 6          46  T3 @44e108b8 00000008 fast         m0 lcd d6           0-0070 (nxp_hdmi_bonelt_pins)
P8.40 / hdmi / sysboot 7          47  T4 @44e108bc 00000008 fast         m0 lcd d7           0-0070 (nxp_hdmi_bonelt_pins)
P8.37 / hdmi / sysboot 8          48  U1 @44e108c0 00000008 fast         m0 lcd d8           0-0070 (nxp_hdmi_bonelt_pins)
P8.38 / hdmi / sysboot 9          49  U2 @44e108c4 00000008 fast         m0 lcd d9           0-0070 (nxp_hdmi_bonelt_pins)
P8.36 / hdmi / sysboot 10         50  U3 @44e108c8 00000008 fast         m0 lcd d10          0-0070 (nxp_hdmi_bonelt_pins)
P8.34 / hdmi / sysboot 11         51  U4 @44e108cc 00000008 fast         m0 lcd d11          0-0070 (nxp_hdmi_bonelt_pins)
P8.35 / hdmi / sysboot 12         52  V2 @44e108d0 00000008 fast         m0 lcd d12          0-0070 (nxp_hdmi_bonelt_pins)
P8.33 / hdmi / sysboot 13         53  V3 @44e108d4 00000008 fast         m0 lcd d13          0-0070 (nxp_hdmi_bonelt_pins)
P8.31 / hdmi / sysboot 14         54  V4 @44e108d8 00000008 fast         m0 lcd d14          0-0070 (nxp_hdmi_bonelt_pins)
P8.32 / hdmi / sysboot 15         55  T5 @44e108dc 00000008 fast         m0 lcd d15          0-0070 (nxp_hdmi_bonelt_pins)
P8.27 / hdmi                      56  U5 @44e108e0 00000000 fast    down m0 lcd vsync        0-0070 (nxp_hdmi_bonelt_pins)
P8.29 / hdmi                      57  R5 @44e108e4 00000000 fast    down m0 lcd hsync        0-0070 (nxp_hdmi_bonelt_pins)
P8.28 / hdmi                      58  V5 @44e108e8 00000000 fast    down m0 lcd pclk         0-0070 (nxp_hdmi_bonelt_pins)
P8.30 / hdmi                      59  R6 @44e108ec 00000000 fast    down m0 lcd oe/acb       0-0070 (nxp_hdmi_bonelt_pins)
P9.22 / spi boot clk              84 A17 @44e10950 00000037 fast rx  up  m7 gpio 0.02
P9.21 / spi boot in               85 B17 @44e10954 00000037 fast rx  up  m7 gpio 0.03
P9.18 / spi boot out              86 B16 @44e10958 00000037 fast rx  up  m7 gpio 0.04
P9.17 / spi boot cs               87 A16 @44e1095c 00000037 fast rx  up  m7 gpio 0.05
P9.42a                            89 C18 @44e10964 00000027 fast rx down m7 gpio 0.07
P9.20 / cape i²c sda             94 D18 @44e10978 00000033 fast rx  up  m3 i²c 2 sda       i2c@4819c000 (pinmux_i2c2_pins)
P9.19 / cape i²c scl             95 D17 @44e1097c 00000033 fast rx  up  m3 i²c 2 scl       i2c@4819c000 (pinmux_i2c2_pins)
P9.26                             96 D16 @44e10980 00000037 fast rx  up  m7 gpio 0.14
P9.24                             97 D15 @44e10984 00000037 fast rx  up  m7 gpio 0.15
P9.31 / hdmi audio clk           100 A13 @44e10990 00000000 fast    down m0 asp 0 tx clk     mcasp@48038000 (mcasp0_pins)
P9.29 / hdmi audio fs            101 B13 @44e10994 00000010 fast     up  m0 asp 0 tx fs      mcasp@48038000 (mcasp0_pins)
P9.30                            102 D12 @44e10998 00000027 fast rx down m7 gpio 3.16
P9.28 / hdmi audio data          103 C12 @44e1099c 00000002 fast    down m2 asp 0 data 2     mcasp@48038000 (mcasp0_pins)
P9.42b                           104 B12 @44e109a0 00000027 fast rx down m7 gpio 3.18
P9.27                            105 C13 @44e109a4 00000027 fast rx down m7 gpio 3.19
P9.41                            106 D13 @44e109a8 00000027 fast rx down m7 gpio 3.20
P9.25 / audio osc                107 A14 @44e109ac 00000030 fast rx  up  m0 asp 0 tx hclk    mcasp@48038000 (mcasp0_pins)
P9.41 / jtag emu3                109 D14 @44e109b4 00000027 fast rx down m7 gpio 0.20

Okay, I now tried toggling the pins in a Rust application using the gpiod crate and that works totally fine.
Maybe it is something with the way the gpiod CLI utilities work…

1 Like

Hello,

@robamu , I have found that in C, it works but on the am335x supported BBB…

  1. The pin is left floating.
    a. cannot be used in other languages or w/out libgpiod.
    b. a reboot is necessary to clear the pin of this issue.

I am not sure why this is the way it is but for now, this is all I can configure.

/*
Simple gpiod example of toggling a LED connected to a gpio line from
the BeagleBone Black Wireless and RelayCape.
Exits with or without CTRL-C.
*/

// This source can be found here: https://github.com/tranter/blogs/blob/master/gpio/part9/example.c
// It has been changed by me, Seth, to handle the RelayCape and BBBW Linux based SiP SBC.

// kernel: 5.10.140-ti-r52
// image : BeagleBoard.org Debian Bullseye Minimal Image 2022-11-01

// type gpioinfo and look for this line: line 20: "P9_41B" "relay1" output active-high [used]
// That line shows us the info. we need to make an educated decision on what fd we will use, i.e. relay1.
// We will also need to locate which chipname is being utilized. For instance: gpiochip0 - 32 lines:

// #include <linux/gpio.h>
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    const char *chipname = "gpiochip0";
    struct gpiod_chip *chip;
    struct gpiod_line *lineLED;

int i, ret;

// Open GPIO chip
chip = gpiod_chip_open_by_name(chipname);
if (!chip) {
    perror("Open chip failed\n");
    return 1;
}

// Open GPIO lines
lineLED = gpiod_chip_get_line(chip, 20);
if (!lineLED) {
    perror("Get line failed\n");
    return 1;
}

// Open LED lines for output
ret = gpiod_line_request_output(lineLED, "relay1", 0);
if (ret < 0) {
    perror("Request line as output failed\n");
    return 1;
}

// Blink a LED
i = 0;
while (true) {
    ret = gpiod_line_set_value(lineLED, (i & 1) != 0);
    if (ret < 0) {
        perror("Set line output failed\n");
        return 1;
    }
    usleep(1000000);
    i++;
}

// Release lines and chip
gpiod_line_release(lineLED);
gpiod_chip_close(chip);
return 0;
}

So, in my idea here, I would have thought the gpiod_line_release and gpiod_chip_close remedies would have alleviated this issue. If you see where I am making an issue, please let me know.

Please remember, once you run the above source to handle a LED or whatever, the pin is left in a floating state which disables running other source in other languages outside of libgpiod being able to be used.

Also…that specific source is for the RelayCape on the BBB. So, the RelayCape is autodetected and the pins muxed accordingly.

Seth

P.S. If you need to use config-pin on the BBB, this may provide a valuable way of muxing your pin that you want to have available. In Rust, did you come across this issue?

  1. Run the Rust source for libgpiod.
  2. Run another source without the libgpiod data.
  3. See the floating pin?