|
Posted at 2/8/2016 19:22:36
Only Author
4#
Hello everybody,
I've tried to understand how to use a free GPIO with the firefly and I've finally succeded. It took me quite a long time because it was not my only work and because documentation is very scarce on the subject.
Here is how I did it. First, I must say it is certainly not the best way to do it. I guess the official documentation is the better way to handle things, the only problem is I've never found the files they indicate. For instance, files like pinctrl-rockchip.c do not exist on my ubuntu for firefly and can't be changed. I haven't found firefly-rk3288.dts either. So, the way to use GPIO in the official documentation simply doesn't work (or it is very badly explained).
I found another solution. GPIO ports in linux are all in the /sys/class/gpio/ directory. Here you can find gpiochipX directories. X is a number and goes from 0 to 288. gpiochip directories are controllers for all the gpio firefly uses. What does it mean ? If you go in a gpiochip directory, you will see label. If you do cat label, it will give you the name of the bank of gpio for the firefly. For instance gpiochip0 has a label gpio0, gpiochip32 has a label gpio1, gpiochip64 has a label gpio2, etc. You can see that gpiochip directories jump from a number to the next number + 32. That is why we have 0, 32, 64, 96, 128, 160, 192, 224, 256 and 288 after gpiochip. There is a second file named in each of these directories called ngpio which gives the number of gpio controlled by this particular gpiochip and each time you have 32. so the first number of gpiochip correspond to the 9 banks of gpio described in the documentation (from gpio0 to gpio8) and the last one (gpiochip288) correspond to a gpio15 (?).
Now, how to use a particular gpio. Simple, in the gpio directory, just type echo X > export and then a directory gpioX will appear. In this, you will have all the method needed to use this particular gpio. How to set the right number ? You need to know on which bank is this gpio port and to understand the position of it in this bank.
I you read the page about gpio for firefly, they tell you some gpio ports are free to use. They give you an image of the pins you can find on the board. For example, for the j10 you have P7_A3 which is free (you can take P0_B5 or another one, just look). On the physical board you can spot them easily (P7_A3 is written A3, P0_B5 is written B5). So P7_A3 means it the A3 position of the gpio7. If we take gpiochip, gpio7 is controlled by gpiochip224 and A3 is the 4th position (it starts at A0). It means you must type echo 227 > export in the gpio directory to gain access to controls of this particular gpio.
In the gpio227 directory, you must first set the direction to out (echo out > direction) and then change the value to 1 (echo 1 > value) and then the electricity flows ! If you want to stop it, type echo 0 > value (of course).
To test it, I used a small led with a 100 ohm resistor and it worked fine but be careful with the led and resistor characteristics. I know that this gpio port of the firefly has a 3.3 volts current but I just guessed the intensity was 0.02 ampere (like the raspberry) but I am not sure at all. Be careful on this point. You can find many tutorials on internet about making a simple electronic circuit with one led, they can be useful.
For the moment, in my program (C++) I use a command like system("echo 227 > gpio") because I use linux and it works just fine (my program is not sophisticated for the gpio, I just need to give one pulse of electricity for a few ms). So I won't go further but it would be nice if someone knows how to do it more properly and give a clear explanation. |
|