OpenWrt Forum Archive

Topic: How to config GPIO and read the GPIO value in OpenWRT

The content of this topic has been archived on 21 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,

I am working on AR9344 Openwrt. I need to configure the GPIO16 and read the status of GPIO.

Based on GPIO read value, has to ON/OFF the status LED.

Could anyone please explain how i can configure and read GPIO value.

Regards,

echo "16" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio16/direction
echo "1" > /sys/class/gpio/gpio16/value

Thanks for the reply.

When i am using "echo "16" > /sys/class/gpio/export" the below error getting.
ash: write error: Device or resource busy

Regards,

For that to work, your system must know about the existence of that gpio at kernel level, and also the gpio must not be in use by a driver. So most probably in this case there is a driver that has already acquired that gpio for it's own purposes.

I am able configure the GPIO using
echo "16" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio16/direction
echo "1" > /sys/class/gpio/gpio16/value

but i am using "cat /sys/class/gpio/gpio16/value" command to read the GPIO value its always showing 0

If we switch to high or low.

Another way i have Create /etc/init.d/buttons and written below script:

#!/bin/sh /etc/rc.common
START=19
start() {
        /bin/umount /etc/config 2>/dev/null
        echo out > /sys/class/gpio/gpio16/direction 2> /dev/null       
        echo 16 > /sys/class/gpio/export 2> /dev/null
        if [ "$(cat /sys/class/gpio/gpio16/value)" == "1" ] ; then
            echo "255" > /sys/devices/platform/leds-gpio/leds/db120:green:status/brightness     
        else
            echo "0" > /sys/devices/platform/leds-gpio/leds/db120:green:status/brightness     
        fi
}

and restart .

chmod +x /etc/init.d/buttons
/etc/init.d/buttons enable
/etc/init.d/buttons start

If i check GPIO details in board using

"cat /sys/kernel/debug/gpio" command it showing that GPIO16 configure as below

gpio-11  (db120:green:usb     ) out hi
gpio-12  (db120:green:wlan-5g ) out hi
gpio-13  (db120:green:wlan-2g ) out hi
gpio-14  (db120:green:status  ) out hi
gpio-15  (db120:green:wps     ) out hi
gpio-16  (sysfs               ) out lo
gpio-17  (WPS button          ) in  hi

but the LED is not glowing when we switch the GPIO to high or low and even GPIO value always getting 0.

(Last edited by afkar.ec on 15 Oct 2015, 02:33)

i have Create /etc/init.d/buttons and written below script:

#!/bin/sh /etc/rc.common
START=19
start() {
        /bin/umount /etc/config 2>/dev/null
        echo out > /sys/class/gpio/gpio16/direction 2> /dev/null       
        echo 16 > /sys/class/gpio/export 2> /dev/null
        if [ "$(cat /sys/class/gpio/gpio16/value)" == "1" ] ; then
            echo "255" > /sys/devices/platform/leds-gpio/leds/db120:green:status/brightness     
        else
            echo "0" > /sys/devices/platform/leds-gpio/leds/db120:green:status/brightness     
        fi
}

and restart .

chmod +x /etc/init.d/buttons
/etc/init.d/buttons enable
/etc/init.d/buttons start

Now i am able to read the GPIO 16 value 1/0 based on GPIO state, but as above script i have written to ON/OFF the Status LED based on GPIO value, Status LED is not getting OFF even if GPIO16 value is "0".

Can anyone please help on this

The discussion might have continued from here.