January 18, 2020, 09:24:21 am


Have you visited the Allwinner Chipset wiki? - http://linux-sunxi.org/

GPIO not working

Started by GastonLagaffe, June 03, 2013, 12:56:39 pm

Previous topic - Next topic


June 03, 2013, 12:56:39 pm Last Edit: June 04, 2013, 11:16:30 am by GastonLagaffe
Hi there,
I have now tried for days to get GPIO working without success.
I follwed the threts in this forum but nothing works, so please do not refer me to any thread about GPIO - I read them all...
Actual status:
- I downloaded the SD image from the cubieboard.org homepage (Linux cubieboard 3.0.62)
- had to add keyboard support (German)
- had to fix /etc/hosts to be able to use sudo
- had to fix /etc/resolv.conf to be able to access github
- successfully compiled sunxi-tools
- fetched script.bin from nanda mounted on /media/nanda
- added to script.fex:
gpio_used = 1
gpio_num = 1
gpio_pin_1 = port:PD20<0><default><default><default>

and converted to script.bin and copied back to nanda
After the reboot, I find in /sys/class/gpio two files export and unexport
I find nothing with gpio in the /sys/devices/virtual tree
modprobe sun4i-gpio fails (mdoule not found)
as well as any attempt to use modprobe with sun4i-gpio.ko or gpio

So finally I am out of options. Any help is welcome

Ciao, Mathias


June 03, 2013, 10:32:17 pm #1 Last Edit: June 03, 2013, 11:00:25 pm by patwood
There are two different gpio drivers for allwinner linux:  sun4i-gpio and gpio-sunxi.  The first is a custom hack made by allwinner; it's mostly deprecated in recent linux kernels but still used on Android with the old 3.0.x kernels.  Unfortunately, most of the allwinner wikis refer to this version, and there's lots of posts about it in various forums because it's still in use with Android.

The second is designed to follow the standard linux gpio, which appears to be what you have, as the /sys/class/gpio/export file is what's used to set it up.  It's really a much better system -- it allows you to dynamically set the direction of the gpio pin at runtime, poll on the value of a gpio interrupt, and is compatible (but not particularly portable without a lot of abstractions) with the gpio implementations across almost all embedded linux systems.

You can read all about linux gpio here: https://www.kernel.org/doc/Documentation/gpio.txt, here: http://elinux.org/GPIO, here: http://squidge.sourceforge.net/gpio, and here: http://www.cubieforums.com/index.php/topic,103.msg545.html#msg545


Quote from: GastonLagaffe on June 03, 2013, 12:56:39 pm
- I downloaded the SD image from the cubieboard.org homepage (Linux cubieboard 3.0.62)
... converted to script.bin and copied back to nanda

Hi Mathias,

you got me a bit confused here: I thought that if you are booting from SD (which seem to be the case?), the script.bin should be on the boot partition of your SD card?
Anyhow, this still would not explain why the module load fails with "module not found". But I think patwood wrote all there is to know about that (which is much more than what I would have known). You might try to compile/install the module by yourself.



June 04, 2013, 05:16:57 am #3 Last Edit: June 04, 2013, 05:20:44 am by GastonLagaffe
Salut patwood,

thanks for the calification. It explains the traps I was stumbling into. For all like me hunting for details on how to use the GPIO, I summarize here the steps I have taken:

  • Download SD Card image from cubieboard.org/download (bottom of page) and write to SD card

  • initial uses have their userid set as password

  • sudo does not work, as the hostame gets not resolved correctly. Therfore, change to root (su -) using the password as mentioned above and add an entry in /etc/hosts (this file was empty) cubieboard

  • as root change /etc/resolv.conf to use the right gateway (in my case

  • as root adjust your keyboard settings if required

sudo dpkg-reconfigure keyboard-configuration

  • as root modprobe gpio-sunxi

Changing the gpio parameters has to happen in the script.bin file of the SD card image. This is located at /media/cubie/<sd id> where <sd id> is the id of the SD card image (something like 9EBA-83CB)
I had to compile the sunxi-tools:

$ git clone --recursive git://github.com/linux-sunxi/sunxi-tools.git
$ cd sunxi-tools
$ make
$ cp /media/cubie/9EBA-83CB/script.bin .
$ cp /media/cubie/9EBA-83CB/script.bin script.bin.SAVE.media
$ ./bin2fex script.bin > script.fex
$ ./bin2fex script.bin > script.fex.SAVE.media
$ vi script.fex
$ ./fex2bin script.fex > script.bin
$ sudo cp script.bin /media/cubie/9EBA-83CB/script.bin

I used the files *.SAVE.media as backup so that I can always revert to the originals. Instead of vi I used leafpad.
In the section [gpio_para] I found 11 entries defining pin1 to pin11 to use PE1 to PE11. I added a line
gpio_pin_12 = port:PD20<1><default><default><0>
and modified gpio_num to 12.
After converting the fex to bin and copy to the SD card root and a reboot, I was able to write to the pin:
# cd /sys/class/gpio
# echo 12 > export
# echo out > gpio12_pd20/direction
# echo 1 > gpio12_pd20/value
# echo 0 > gpio12_pd20/value

The first call creates the link gpio12_pd20 to /sys/devices/platform/gpio-sunxi/gpio/gpio12_pd20 and creates that directory. The second command sets the pin to be an output pin. The third and fourth command switches the pin on and off as an attached LED (with resistor) demonstrated.

Finally, by adding a line with gpio-sunxi to /etc/modules loads the module at boot time.

Once again a GREAT THANK YOU to patwood for helping me!!!!
Ciao, Mathias


My pleasure.  I've been working on the gpio and led devices for a few weeks now for a project at the office, so I've had to learn this stuff pretty fast.  Glad to be able to give little back here.

Nice summary. It covers all the different steps from the very beginning.


June 15, 2013, 11:14:22 pm #5 Last Edit: June 15, 2013, 11:34:06 pm by throwcomputer
I'm having similar issues although I've got everything set up and drivers running as far as I can tell.

I've installed Cubian:

grabbed sunxi-tools and built fex2bin using make fex2bin.

Altered the cubieboard_argon.fex on /boot to include:

gpio_used = 1
gpio_num = 4
gpio_pin_1 = port:PG00<1><default><default><default>
gpio_pin_2 = port:PB19<1><default><default><default>
gpio_pin_3 = port:PG02<1><default><default><default>
gpio_pin_4 = port:PG04<1><default><default><default>

converted the .fex file to script.bin using fex2bin

checked to make sure gpio-sunxi driver exists.. which it does on the default Cubian IMG located here:

set the gpio-sunxi driver to load on boot by editing /etc/modules with:

#For GPIO and LEDS

I can confirm the driver loads and GPIO pins are initiated on reboot in the dmesg results:
[   10.700000] sunxi_gpio driver init ver 1.3
[   10.730000] gpiochip_add: registered GPIOs 1 to 4 on device: A1X_GPIO

Yet after all this when I try to set up the pins using export I receive permission denied whether using sudo or not as follows:

echo 1 > /sys/class/gpio/export
-bash: /sys/class/gpio/export: Permission denied

sudo echo 1 > /sys/class/gpio/export
-bash: /sys/class/gpio/export: Permission denied

Not sure if this means the default gpio-sunxi driver present with Cubian IMG is wrong or what not?  Any suggestions?


Ok.. answered my own question after a few hours of searching turned up a working solution on raspberry pi forum for same issue:

-create group gpio
-add user to group
-change /sys/class/gpio group to gpio recursively
-change permissions to 775 on gpio recursively

sudo groupadd gpio
sudo usermod -aG gpio <username>
su <username>
sudo chgrp gpio -HR /sys/class/gpio
sudo chmod -R 775 /sys/class/gpio

Once this is done gpio pins can be set up properly using default admin account:

echo 1 > /sys/class/gpio/export

It works..unfortunately this does not stick on reboot and requires /sys/class/gpio to be reassigned group and permissions each boot.


June 16, 2013, 10:25:01 am #7 Last Edit: June 16, 2013, 10:28:31 am by patwood
Add the chgrp/chmod lines (w/o sudo) to /etc/rc.local or addgroup you user name to the group owner of the gpio files.


Another solution is to create a udev rule to chown/chgrp the sysfs files when the driver creates them.  This is a bit out of my area (just starting to learn about this now), but there's lots of information out there on it: http://forums.fedoraforum.org/showthread.php?t=274629