Topic: How to: FIND and TEST GPIOs

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.

NOTE:
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

2 (edited by ashkanull 2012-05-01 17:02:57)

Re: How to: FIND and TEST GPIOs

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
root@OpenWrt:/sys/class/gpio#

Re: How to: FIND and TEST GPIOs

Ashkanull,

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
24
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip0# cat base
0
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip0# cat uevent
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip0# cat label
RT305X-GPIO0

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                     
40                                                                              
root@OpenWrt:/sys/devices/virtual/gpio/gpiochip40# cat ngpio                    
12                                                                              
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                        
root@OpenWrt:/sys/class/gpio#

Please post if you find out more.

Dave.

4 (edited by sash-kan 2013-08-23 11:31:32)

Re: How to: FIND and TEST GPIOs

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:
$ echo CONFIG_BUSYBOX_CONFIG_WATCH=y >> .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

prefer to communicate in russian