This blog is about using some basic control methods to use the GPIO pins of the xplained board through bash commands, we'll look at using a digital pin as an output and then reading the value of it as an input pin.
I already wrote a quick post about pin control in the main roadtest comments. The user tekmeister pointed out that this is a standard way of pin control on other embedded linux platforms like Raspberry Pi which is great news! It follows that other things we would like to do in the future could be a lot easier by searching for guides based around things like the Pi or Beaglebone and modifying them a little to use on the Xplained! Sounds like a good way to get a foothold.
We start off at the Xplained command prompt, you can get there by using the quickstart guide and connecting through usb or following my blog on connecting through SSH.
Get a directory listing of /sys/class/gpio
You should see the following directory listing
export gpiochip0 gpiochip128 gpiochip32 gpiochip64 gpiochip96 unexport
next we need to export the pin we want to use into the filesystem
echo 81 > /sys/class/gpio/export
The number 81 is the assignment number of pin C17 which is located on the digital header on the xplained board. If we do another directory listing of /sys/class/gpio we will see a new directory called pioC17
You should see the same directory listing as before only a new directory called pioC17 has been created.
export gpiochip128 gpiochip64 pioC17
gpiochip0 gpiochip32 gpiochip96 unexport
If we get a directory listing on pioC17 we will see the basic control structure of that pin.
You should see the following directory listing:
active_low device direction edge power subsystem uevent value
Its easy to see that some of these are properties of the way we would use the pin. "direction" relates to whether the pin is in input or output state and the "value" relates to whether it is high or low (0 or 1) by reading it when it is input state or setting the value if we are using it as an output. we'll use "cat" to read the files and "echo" to modify them to set the pins state.
You should see the following
root@sama5d3_xplained:~# cat /sys/class/gpio/pioC17/direction
the word "in" that comes after you read the file tells us that it is set as an input. if we type cat /sys/class/gpio/pioC17/value we will get the number 1 that tells us that the pin is high, presumably through an internal pull up resistor, if we physically connect pin C17 to a suitable ground on the board, and type cat /sys/class/gpio/pioC17/value again, we will get the number 0 because the pin is now in a low state.
That shows how we can receive input through the digital header, next we will look at using it is an output
echo out > /sys/class/gpio/pioC17/direction
This will write the value "out" to the direction file. If we do cat /sys/class/gpio/pioC17/direction you should see the word "out" instead of "in" this time. If we do cat /sys/class/gpio/pioC17/value we will get "0" as the outcome, this tells us that the pin is low. If you connect a multimeter to ground and pin C17 on the digital header, it will show 0v. Type "echo 1 > /sys/class/gpio/pioC17/value" and you will notice the multimeter jumps to 3.3V.
Great! We can turn the pin on and off physically do connect to an H-Bridge or LED etc.. and in the words of Ben Heck "If we can turn on a LED we can do anything!"
My next blog is going to be using what we know about pin control and using it in Python in order to make it more useful and let us write small more complex scripts and be able to automate what we want the pins doing.
As an offshoot to my roadtest project, I'm going to be making a small GPIO control library in Python, there is already one for the Raspberry Pi that might work but the library is compiled in C and possibly wouldn't be using the same C library structure since the Xplained board is using uclibc. Feel free to try, but I'm eager to get going and try to make interrupts that run full scripts or call functions.