[OpenWrt-Devel] How to config GPIO and read the GPIO value in OpenWRT

Hartmut Knaack knaack.h at gmx.de
Thu Oct 15 15:38:47 EDT 2015


Afkar Rafique schrieb am 15.10.2015 um 04:07:
> Thanks for the reply.
> 
> 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
> 

Quite a mess you've got there. No idea why you need to umount /etc/config,
but anyway: First thing in GPIO setup is exporting, then you set its direction.
If you intend to read the GPIO status, then better use "in" as direction.
Now, do you just want to do an action (LED on/off) just during start of this
script, or all the time during runtime, when the GPIO status changes? In the
latter case, you should look into gpio-button.

> 
> 
> On Thu, Oct 15, 2015 at 12:37 AM, Martin Blumenstingl <martin.blumenstingl at googlemail.com <mailto:martin.blumenstingl at googlemail.com>> wrote:
> 
>     On Wed, Oct 14, 2015 at 12:19 PM, Afkar Rafique <afkar.ec at gmail.com <mailto:afkar.ec at gmail.com>> wrote:
>     > Could anyone please explain how i can configure and read GPIO value.
>     There's an article on the wiki which explains how to set a GPIO to a
>     specific value: [0]
>     If you want to read a GPIO value then you use direction "in" and then
>     simply cat to read the "value".
> 
>     The only thing that you might have to do is doing a bit of maths to
>     get the correct GPIO number.
>     When you are trying to read GPIO #16 then it's GPIO #16 of a specific
>     chip -> /sys/class/gpio/ should contain a file gpiochipNNN.
>     What you do is take NNN (let's assume it's 456) and add your GPIO
>     number to it: 456 + 16 = 472 -> this is the number needef to "export"
>     the GPIO.
> 
> 
>     [0] http://wiki.openwrt.org/doc/hardware/port.gpio#software
> 
> 
> 
> 
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel at lists.openwrt.org
> https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
> 
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list