GPIO usage for linux sunxi
In order to you can to use GPIOs signals of your board, you have to determine if there is a GPIO driver for your platform. Besides, you must know if this driver follows the standard GPIO Linux framework. Check under the folder /drivers/gpio/xxxx.c whether there is a GPIO driver. If so, this driver uses the standard GPIO framework. If not, you have to look in other folders.
For the case of processors Allwinner, there was a driver called sun4i-gpio, but was removed in favor of the new gpio-sunxi. This new driver uses GPIO Linux framework. This means that you can manipulate the pins I/O through the entries in the /sys directory. In addition, there are many other benefits to using the standard framework.
Since you have found the driver of the GPIO to board processor, how can we use it in user space? You either can access the GPIO signals through command line or by software. The second option is obviously more interesting for those who make projects. I will present both.
Defining the pin number
You can also get information about number pin by GPIO controller, referenced in the kernel as gpiochipN. GPIO controllers have paths like /sys/class/gpio/gpiochip42 (for the controller implementing GPIOs starting at #42) and have the following read-only attributes under /sys/class/gpio/gpiochipN: base, label and ngpio.
See the manufacturer's documentation to know the correct number for your platform. On Allwinner, the correct number is in script.fex file. For example, if we want to use pin PG9 on A13-OLinuxino:
[gpio_para] gpio_used = 1 gpio_num = 6 gpio_pin_1 = port:PB03<1><0>you will pass the number 6 as parameter. This suits to commands and software. Let's see!
gpio_pin_2 = port:PB04<1><0> gpio_pin_3 = port:PB05<1><0> gpio_pin_4 = port:PB06<1><0> gpio_pin_5 = port:PB07<1><1> gpio_pin_6 = port:PG09<1><1>
GPIO Example usage via command line
GPIO=6 cd /sys/class/gpio echo $GPIO > export
If export was successful, you should see a new folder. Get in.
Set the direction of the pin using the strings "in" or "out". They are self-explanatory. Example of reading:
echo "in" > direction cat value
Example of writing:
echo "out" > direction echo 1 > value
GPIO Example usage via software
gpio = atoi(argv); // take pin as parameter gpio_export(gpio); gpio_set_dir(gpio, 1); gpio_set_value(gpio, 1); // Set high level sleep(1); gpio_set_value(gpio, 0); // set low levelPlease, always keep the license and credit to the original author!
The code shown above will only work if you apply this patch: gpio-sunxi.patch, created by Emilio Lopes.
Video on Youtube: https://www.youtube.com/watch?v=iN3mEWqaf1s