A13-OLinuXino playing with GPIOs


Today I will show you how to access A13-OLinuXino GPIOs.

First make sure you have installed the GPIO drivers by

# lsmod

you will see the list of the installed kernel modules and if “sun4i_gpio” is there everything is OK

If you got A13-OLinuXino-SD with the pre-installed Debian everything is already setup for you, if not you can follow the instructions of this post and make one SD card yourself: https://olimex.wordpress.com/2012/10/12/building-bootable-sd-card-with-debian-linux-image-for-a13-olinuxino/ do not forget when compile the kernel to enable SUN4I_GPIO_UGLY = y inside .config , why UGLY? you will understand soon 😉

Allwinner do not care too much for the GPIOs as they primary support Android and their major market is the tablet manufacturers. This SUN4I-GPIO is made by Tom Cubie who work at Allwinner and made available to community, this driver though is not so flexible as the sysfs and you can’t switch between input and output easily.

With this driver you permanently assign one port to be input or output in the script.bin and when this gpio kernel module is loading it reads from script.bin the assignments and make them permanently, so if you want to change some GPIO to input or output you have to unload the driver make change in the script.bin and load it again and this is ugly 🙂

Anyway with the card we ship we made these gpios defined:

pe4, pe5, pe6,  pe7: inputs

pe8,  pe9,  pg10,  pg11,  pg9, pb10,  pb3,  pb4,  pe10,  pe11:  outputs

pg9 is GPIO output with the green status LED connected to it

to switch LED on you can do:

# echo 1 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9

and to switch LED off you can do:

# echo 0 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9

Now let’s see how fast we can toggle LED from shell script:

# vi gpio

and enter this code:

while [ true]


echo 1 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9

echo 0 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9


# chmod +x gpio


with the oscilloscope is seen square wave with 220 uS period i.e. 45 454 Hz! this is quite more fast than the 300Hz we achieved on iMX233 GPIOs from shell https://olimex.wordpress.com/2012/09/07/imx233-olinuxino-working-with-gpios/

now let’s try same in C:

# vi gpioc.c

#include <stdio.h>
#include <fcntl.h>
#include <string.h>

int main() {

char s_0[] = “0”;
char s_1[] = “1”;
int fd;

//toggle pg9 LED gpio

if((fd=open(“/sys/devices/virtual/misc/sun4i-gpio/pin/pg9”,O_RDWR))<0) return(1);
else {

while(1) {

if(write(fd,s_1,strlen(s_1))<0) return(1);

if(write(fd,s_0,strlen(s_0))<0) return(1);

if(close(fd)<0) return(1);


# cc gpioc.c -o gpioc

# chmod +x gpioc

# ./gpioc

the oscilloscope show 4.2 uS period square wave i.e. about 238 095Hz not so fast than iMX233 140kHz. But obviously if we chase speed should not use file system.

Now next step is to make access via /dev/mem as mmgpio with iMX233 https://olimex.wordpress.com/2012/09/11/imx233-olinuxino-gpios-faster-and-faster/

Using directly /dev/mem iMX233 achieved up to 5.7 Mhz GPIO toggle, I do wonder how fast we can go with A13, but this will be done in my next blog about it.


14 Comments (+add yours?)

  1. David Goadby
    Oct 23, 2012 @ 18:36:14

    Just what I wanted. Although most of my I/O is i2C I needed some direct I/0’s for panel lamps a reset button and a sounder. I don’t need to change port direction in use so that restriction is not an issue. Toggling at 2Hz is plenty too 😉


  2. Johannes H.
    Oct 24, 2012 @ 22:33:18

    On a Cortex M4 (168 MHz) you can toggle a GPIO Port with up to 84 / 2 MHz.
    On a Cortex A8 (1 GHz) with a few KHz??? What????


  3. Johannes H.
    Oct 24, 2012 @ 22:52:17

    without any OS….but the comparison shows the (immense) overhead of the linux kernel??


  4. Thomas
    Nov 06, 2012 @ 20:54:13

    First of all thank you for these nice tutorials about your hardware.
    I’m using A13-Olinuxino-WiFi with your Debian image in A13-olinuxino-SD.

    When I’m typing as root “lsmod”, I do not see sun4i_gpio.
    In addition, the directory /sys/devices/virtual/misc/sun4i_gpio does not exist.

    Please tell me what to do 🙂
    Thank you.

    root@debian:~# lsmod
    Module Size Used by
    bfcon 35991 70
    bitblit 4187 1 fbcon
    softcursor 1123 1 bitblit
    font 7767 1 fbcon
    8192cu 511517 0


  5. Kyle Muffley
    Dec 10, 2012 @ 18:54:20

    I am very new to Linux but with your great tutorials I am up to a good point so far.
    I have installed Debian on an SD Card and it is up and running. I am using a script.bin file that defaults pins (pb2, pb3, pb4, pb5, and pb9) in the …/sun4i-gpio/pin directory and I was successfully able to toggle the output LED using your echo 1, echo 0 commands but I am now at the point I would like to receive inputs. Is there any direction you can point me in to understanding a little more on:
    –>Reading Inputs
    –>Getting a GPIOs Direction (Input Or Output)
    –>Changing a GPIOs Direction

    Any help would be greatly appreciated.


  6. sandro76
    Feb 06, 2013 @ 18:44:02

    Maybe to increase the speed on gpio can do usefull give an
    fdatasync(fd) instruction after a write instruction.


  7. markome
    Apr 10, 2013 @ 16:36:14

    Hi there!
    Are there any chances to have GPIO access in an Android image?


  8. Mateusz
    Dec 22, 2013 @ 00:17:48

    Is it possible to use couple of pins as one parallel port? Something like:
    I want to connect some external device to olinuxino but I need to write data into this device using 16bit parallel port. Is it possible to do this from linux app?


  9. David Goadby
    Dec 23, 2013 @ 14:57:25

    If you need to present 16 bits at one time (possibly with a strobe?) then I would suggest a hardware solution such as 74F675A which is a serial in parallel out chip. You can drive this with less than outputs and the data is presented in one “hit” so no data skew issues. I drive an 8-bit thermal printer this way.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: