sexta-feira, 4 de abril de 2014

Using GPIOs from userspace

GPIO usage for linux sunxi


What I'll state here applies to many boards on the market today. However, it is targeted specifically for boards with Allwinner processors. In this article we will use GPIO Sysfs Interface for Userspace.

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


First you must determine the number corresponding to the pin you want to use. The number that kernel "see". In Linux there is a standard to define this number. For example, if your CPU have a GPIO with 2 ports, each port containing 32 pins, so GPIO1.5 will be 5, GPIO1.32 will be 32 and GPIO2.1 will be 33. Do the calculation according to the number of pins and ports of your GPIO.

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

you will pass the number 6 as parameter. This suits to commands and software. Let's see!

GPIO Example usage via command line


Usage via command is only useful for testing. Perhaps with a script you can do something more dynamic. For more complex tasks, the method for software is the preferred.
GPIO=6
cd /sys/class/gpio
echo $GPIO > export
If export was successful, you should see a new folder. Get in.
cd gpio$GPIO
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


In case the software is interesting to create a library with functions as such export, set direction, read, write, etc. I did a library based on RidgeRun's code.  I just rearranged the code!
https://www.dropbox.com/sh/gaqxgasjuixmnr3/7rHxvPxdoC
We can set a pin at high level and low level as follows:
gpio = atoi(argv[1]); // 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 level
Please, 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

References


http://www.softwarelivre.blog.br/2014/05/usando-gpio-partir-do-userspace.html
https://github.com/torvalds/linux/blob/master/Documentation/gpio/sysfs.txt
https://developer.ridgerun.com/wiki/index.php/How_to_use_GPIO_signals
http://sergioprado.org/user-space-device-drivers-no-linux-parte-2/

Nenhum comentário:

Postar um comentário