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

Afkar Rafique afkar.ec at gmail.com
Mon Oct 19 01:42:28 EDT 2015


I have configured button definition in the mach-file as below:
                 .desc           = "LAN WIFI button",
                .type           = EV_KEY,
                .code           = KEY_LAN_WIFI_BUTTON,
                .debounce_interval = DB120_KEYS_DEBOUNCE_INTERVAL,
                .gpio           = DB120_GPIO_BTN_LAN_WIFI,
                .active_low     = 1,

and add script under file : etc/hotplug.d/button/50-buttonpress

if [ "$ACTION" = "pressed" -a "$BUTTON" = "lanwifi" ]; 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

In this case if i move the slide switch, it works fine but one issue is
there. When i do reset the status LED always ON. If we move slide switch
only it works i mean to say suppose now slide switch in low state means
Status LED is OFF then if i do reset the device then also the Status LED
becomes ON.



On Sun, Oct 18, 2015 at 6:07 AM, Hartmut Knaack <knaack.h at gmx.de> wrote:

> Afkar Rafique schrieb am 16.10.2015 um 03:47:
> > Thanks for the reply.
> >
> > I have change the script as below:
> >
> > #!/bin/sh /etc/rc.common
> >
> > START=19
> > start() {
> >         echo in > /sys/class/gpio/gpio16/direction 2> /dev/null
> >         echo 16 > /sys/class/gpio/export 2> /dev/null
>
> Still, you can only set the direction AFTER exporting the GPIO (otherwise
> the gpioXX
> directory with its contents like direction don't exist).
>
> >         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
> > }
> >
> > I need to monitor the LED on/off all the time during runtime.
> >
> > During start of this script its working, but not getting how to make it
> work run time.
> >
> > Could you please explain how i can do it in gpio-button.
>
> That would require you to add a custom button definition in the mach-file
> of your
> specific device and compile your own firmware image.
> You could however use a small script which infinitely loops through your
> poll and sleeps
> for a second, so the CPU can work on other tasks, too (while true; do
> if...then...else
> sleep 1; done). Downside is, that you can only react to "button press
> events", which are
> long enough (one second, as this is the low limit of sleep time). Also,
> don't forget to
> let it run in background, so you don't block your init process.
>
> >
> > On Fri, Oct 16, 2015 at 3:38 AM, Hartmut Knaack <knaack.h at gmx.de
> <mailto:knaack.h at gmx.de>> wrote:
> >
> >     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> <mailto:
> 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> <mailto: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 <mailto:
> openwrt-devel at lists.openwrt.org>
> >     > https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
> >     >
> >
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20151019/b00a9986/attachment.htm>
-------------- next part --------------
_______________________________________________
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