[BeagleBone Black] How to set gpio pin when booting

Hi
I want to set gpio pin when booting on Beaglebone black.
I made a shell script as below.

modemgpio.sh

#!/bin/bash

echo "set modem control pin ------------------"
sudo echo 115 > /sys/class/gpio/export
sudo echo out > /sys/class/gpio/gpio115/direction
sudo echo 1 > /sys/class/gpio/gpio115/value

When I execute the shell script, it worked well.
I registered it on service because I want to set GPIO_115(GPIO3_19) to high when Beaglebone black is powered on
But after Beaglebone black booted, GPIO_115 pin had been low.

I want to know why the pin was low and how to be high when after booting.
please, help me.

Regards.

@taek8461, I’d do it this way:

Create file /usr/bin/enable-gpio-pins.sh

sudo nano /usr/bin/enable-gpio-pins.sh

#!/bin/bash

echo 115 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio115/direction
echo 1 > /sys/class/gpio/gpio115/value

sudo chmod 755 /usr/bin/enable-gpio-pins.sh

Create file /lib/systemd/system/enable-gpio-pins.service

sudo nano /lib/systemd/system/enable-gpio-pins.service

[Unit]
Description=Enable GPIO pins
After=generic-board-startup.service

[Service]
Type=simple
ExecStart=/usr/bin/enable-gpio-pins.sh

[Install]
WantedBy=multi-user.target

Enable the new systemd service

sudo systemctl daemon-reload
sudo systemctl enable enable-gpio-pins.service

Reboot and test

sudo systemctl status enable-gpio-pins.service

Regards,

1 Like

Hi
I did according to your explanation but it didnot work.

Below is the shell script I made
image

The following is related to the service.
image

GPIO115 pin was still low. what’s wrong with it?

If I run the shell script with root account after logging in, the gpio pin works high

My questions as below

  1. How to make gpio pin work high on service
  2. Why can’t I control gpio pin using sudo in debian account?

Please, help me…

Regards

@taek8461, looks like we are dealing with a race condition on the export, by waiting for generic-board-startup.service that should have let you get access.

Let’s just do it by having the device-tree control GPIO 115 (P9_27)

I created an example for you:

So on the BeagleBone Black, just do:

git clone https://github.com/beagleboard/bb.org-overlays
cd ./bb.org-overlays/
make
sudo make install

Then, in /boot/uEnv.txt add the new overlay:

uboot_overlay_addr4=/lib/firmware/BB-P9_27-LED-00A0.dtbo

Reboot and verify it loaded:

debian@bbb-pwr01-ser09:~$ sudo /opt/scripts/tools/version.sh | grep UBOOT
UBOOT: Booted Device-Tree:[am335x-boneblack-uboot-univ.dts]
UBOOT: Loaded Overlay:[AM335X-PRU-RPROC-4-14-TI-00A0]
UBOOT: Loaded Overlay:[BB-ADC-00A0]
UBOOT: Loaded Overlay:[BB-BONE-eMMC1-01-00A0]
UBOOT: Loaded Overlay:[BB-HDMI-TDA998x-00A0]
UBOOT: Loaded Overlay:[BB-P9_27-LED-00A0]          < --- P9_27 Overlay

Now let’s see if the pin showed up:

debian@bbb-pwr01-ser09:~$ cat /sys/class/leds/P9_27/brightness 
0

You can turn it high via:

debian@bbb-pwr01-ser09:~$ echo 1 > /sys/class/leds/P9_27/brightness 
debian@bbb-pwr01-ser09:~$ cat /sys/class/leds/P9_27/brightness 
1

Regards,

Hi.
I downloaded bb.org-overlays
I installed ‘make’ as below

sudo apt-get install make

command ‘make’ and ‘sudo make install’ make errors as below

There is not BB-P9_27-LED-00A0.dtbo in /lib/firmware/ directory

How can I resolve it?
Please, help me.

Regards

@taek8461, you need a few more dependices…

sudo apt update
sudo apt install build-essential device-tree-compiler

Regards,

Thank you for your help.
I want to ask you one more.
I want to control GPIO3_18 additionally.

I defined ‘#define BONE_P114 0x1A0’ in am335x-bb3-bbb-base.h
and I made the file as below

BB-P114-LED-00A0.dts

/dts-v1/;
/plugin/;

#include <dt-bindings/board/am335x-bbw-bbb-base.h>
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/am33xx.h>

/ {
    /*
     * Helper to show loaded overlays under: /proc/device-tree/chosen/overlays/
     */
    fragment@0 {
	    target-path="/";
	    __overlay__ {

		    chosen {
			    overlays {
				    BB-P114-LED-00A0 = __TIMESTAMP__;
			    };
		    };
	    };
    };

    /*
     * Free up the pins used by the cape from the pinmux helpers.
     */
    fragment@1 {
	    target = <&ocp>;
	    __overlay__ {
		    P114_pinmux { status = "disabled"; };	/* P114: mcasp0_fsr.gpio3_18 */
	    };
    };

    fragment@2 {
	    target = <&am33xx_pinmux>;
	    __overlay__ {

		    bb_gpio_led_pins: pinmux_bb_gpio_led_pins {
			    pinctrl-single,pins = <
				BONE_P114 (PIN_OUTPUT_PULLDOWN | MUX_MODE7)	/* P114: mcasp0_fsr.gpio3_18 */
			>;
		    };
	    };
    };

    fragment@3 {
	    target-path="/";
	    __overlay__ {

		    leds {
			    pinctrl-names = "default";
			    pinctrl-0 = <&bb_gpio_led_pins>;

			    compatible = "gpio-leds";

			    P114 {
				    label = "P114";
				    gpios = <&gpio3 18 GPIO_ACTIVE_HIGH>;
				    default-state = "off";
			    };
		    };
	    };
    };
};

I did ‘make’ and ‘sudo make install’
and then I added ‘uboot-overlay_addr5=/lib/firmware/BB-P114-LED-00A0.dtbo’ in uEnv.txt

but there is no /sys/class/leds/P114.
I don’t know how to control P3_18(P114)
Could you help me?

Regards.

@taek8461, just combine them into one file:

P114_pinmux { status = "disabled"; };	/* P114: mcasp0_fsr.gpio3_18 */

This ^ isn’t needed for non-header pins…

Regards,

Hi
I made the file ( BB-P9_27-LED-00A0.dts ) as your instruction.
The following is content of BB-P9_27-LED-00A0.dts.

/*
 * Copyright (C) 2020 Robert Nelson <robertcnelson@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/dts-v1/;
/plugin/;

#include <dt-bindings/board/am335x-bbw-bbb-base.h>
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/am33xx.h>

/ {
        /*
         * Helper to show loaded overlays under: /proc/device-tree/chosen/overlays/
         */
        fragment@0 {
                target-path="/";
                __overlay__ {

                        chosen {
                                overlays {
                                        BB-P9_27-LED-00A0 = __TIMESTAMP__;
                                };
                        };
                };
        };

        /*
         * Free up the pins used by the cape from the pinmux helpers.
         */
        fragment@1 {
                target = <&ocp>;
                __overlay__ {
                        P9_27_pinmux { status = "disabled"; };  /* P9_27: mcasp0_fsr.gpio3_19 */
                };
        };

        fragment@2 {
                target = <&am33xx_pinmux>;
                __overlay__ {

                        bb_gpio_led_pins: pinmux_bb_gpio_led_pins {
                                pinctrl-single,pins = <
                                        BONE_P9_27 (PIN_OUTPUT_PULLDOWN | MUX_MODE7)  /* P9_27: mcasp0_fsr.gpio3_19 */
                                        AM33XX_IOPAD(0x9a0, PIN_OUTPUT_PULLDOWN | MUX_MODE7) /* (B12) mcasp0_aclkr.gpio3[18] */
                                >;
                        };
                };
        };

        fragment@3 {
                target-path="/";
                __overlay__ {

                        leds {
                                pinctrl-names = "default";
                                pinctrl-0 = <&bb_gpio_led_pins>;

                                compatible = "gpio-leds";

                                P9_27 {
                                        label = "P9_27";
                                        gpios = <&gpio3 19 GPIO_ACTIVE_HIGH>;
                                        default-state = "off";
                                };

                                P114 {
                                        label = "P114";
                                        gpios = <&gpio3 18 GPIO_ACTIVE_HIGH>;
                                        default-state = "off";
                                };
                        };
                };
        };
};

The following is uEnv.txt
image

The following is version.sh | grep uboot

I think two directory would be created

  1. /sys/class/leds/P9_27
  2. /sys/class/leds/P114

but there is any directory in /sys/class/leds directory.
What’s wrong with that?
The following is the content of directory of leds
image

Please, let me know how to fix it and how to control P114 (P3_18) pin

Regards.

Hi @taek8461, please run, to validate overlay loaded:

sudo /opt/scripts/tools/version.sh

Edit, please run the full “version.sh” script, i’m wondering if we get a pinmux error at the bottom…

Regards,

Hi.
I run ‘sudo /opt/scripts/tools/version.sh’ and the message is as below

I didnot modify version.sh
There is still no directory in ‘/sys/class/leds’ directory
My linux version is 4.19.31-bone-rt-r31

Please, help me.

Hi @taek8461, there’s a conflict with P9_92_pinmux add it the disabled list:

P9_92_pinmux { status = "disabled"; };

See:

Regards,

HI
I resolved it.
Thank you so much~

Regards.