OpenWrt Forum Archive

Topic: How to: FIND and TEST GPIOs

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

I found it really hard to discover if my GPIO numbers were correct while making a profile for my device. Eventually i was able to test and confirm my switches using the following method:

on the devices command line (i used the serial console of my device):

cd /sys/class/gpio/
echo 10 > export
cd gpio10
cat value

this should make it possible to read a one or zero on GPIO number 10 on the device, repeat the final command (cat value) while holding button in or releasing. For other GPIOs substitute numbers accordingly, and to display leds you may be able to modify the value with:

echo 0 > value
echo 1 > value

the above would toggle the GPIO output.

I'm not sure if the direction register for GPIOs are modified by these commands, if the direction register on an input can be changed to output then it is possible to damage the chip(this depends on the manufacture of each device).

I systematically checked each one as above, if there is an easier way lets hear it!
furthermore after i tested each one i did:

echo 10 > unexport

why there are some gpio pins like 7 and 9 on my router that doesn't show up ?

root@OpenWrt:/sys/class/gpio# echo 7 > export
root@OpenWrt:/sys/class/gpio# echo 9 > export
root@OpenWrt:/sys/class/gpio# ls
export     gpiochip0  unexport

(Last edited by ashkanull on 1 May 2012, 19:02)


I would think your device (or perhaps the firmwares configuration) does not use those GPIOs. i checked mine and found the following:

root@OpenWrt:/sys/class/gpio# ls
export      gpiochip0   gpiochip24  gpiochip40  unexport
root@OpenWrt:/sys/class/gpio# cd gpiochip0
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip0# ls
base       label      ngpio      subsystem  uevent
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip0# cat ngpio
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip0# cat base
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip0# cat uevent
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip0# cat label

I presume the base is the first GPIO and ngpio is the number of GPIOs that are considered part of chip0 (built into SoC actually). if bas is higher than 9 then the numbers you tried may not be valid for your device(again the firmware could be configured incorrectly).

Now I'm not sure, testing my above assumptions on my device had different results:

root@OpenWrt:/sys/class/gpio# cd gpiochip40/
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip40# ls                           
base       label      ngpio      subsystem  uevent                              
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip40# cat base                     
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip40# cat ngpio                    
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip40# cd ..
root@OpenWrt:/sys/class/gpio# echo 52 > export                                  
ash: write error: Invalid argument                                              
root@OpenWrt:/sys/class/gpio# echo 51 > export                                  
ash: write error: Device or resource busy                                       
root@OpenWrt:/sys/class/gpio# ls                                                
export      gpiochip0   gpiochip24  gpiochip40  unexport                        

Please post if you find out more.


anarchy99 wrote:

you can simply copy commands to export all pins in your console and use

cat /sys/kernel/debug/gpio

command to show pin states

for repeating the command is convenient to use the watch:
# watch -n 1 cat /sys/kernel/debug/gpio

if your busybox compiled without this command, add watch to config:
and rebuild busybox:
$ make package/busybox/compile package/busybox/install package/index
then copy bin/<your board>/packages/busybox_*.ipk to router in /tmp.
then install it:
# opkg install /tmp/busybox_*.ipk

(Last edited by sash-kan on 23 Aug 2013, 13:31)

Is this method only work for ar71xx?
I tried this on ramips,not working
root@OpenWrt:/sys/class/gpio# echo 4 > export
ash: write error: Invalid argument
root@OpenWrt:/sys/class/gpio# echo 15 > export
ash: write error: Invalid argument

The discussion might have continued from here.