This document explains how to manage the GPIO lines on the FOX Board G20 using the Sysfs interface
First of all let's try to blink a led using a GPIO line. We need a led and a 1K resistor wired on pin J7.3 as shown below. Please note that the led has a polarity so you have to wire it properly.
If Python is not installed in your FOX read this tutorial.
This a basic example on how to blink an external led wired on J7 connector in Python:
File: http://foxg20.acmesystems.it/download/examples/led.py -
#!/usr/bin/python import time import fox print "Blinking led" print "Type ctrl-C to exit" led = fox.Pin('J7.3','low') while True: time.sleep(1) led.on() time.sleep(1) led.off()
This example use the standard module time installed by default with Python and the module fox wrote by me to simplify the access to the FOX Board G20 lines.
To install this example and the last version of fox.py module type:
debarm:/# wget http://foxg20.acmesystems.it/download/examples/fox.py debarm:/# wget http://foxg20.acmesystems.it/download/examples/led.py
The run the example typing:
debarm:/# python led.py
or run directly the code typing:
debarm:/# chmod +x led.py debarm:/# ./led.py
This a basic example on how read an external button J7.5 pin in Python:
File: http://foxg20.acmesystems.it/download/examples/button.py -
#!/usr/bin/python import time import fox print "Pressing button" print "Type ctrl-C to exit" led = fox.Pin('J7.3','low') button = fox.Pin('J7.5','in') while True: if button.getValue()==0: led.on() else: led.off()
The J7.3 pin is identified by the Kernel id 82. Before use any GPIO line needs to be exported writing the id in the /sys/class/gpio/export file. To do that manually type:
debarm:/# echo 82 > /sys/class/gpio/export
A new directory called /sys/class/gpio/gpio82 will be created with some files inside:
debarm:/# ls /sys/class/gpio/gpio82 direction edge power subsystem uevent value
To set the line as output type “out” in the file direction:
debarm:/# echo out > /sys/class/gpio/gpio82/direction
Now to turn on and off the led type:
debarm:/# echo 1 > /sys/class/gpio/gpio82/value debarm:/# echo 0 > /sys/class/gpio/gpio82/value
This methos is not so fast but is compatible with any programming languages.
The AT91SM9G20 CPU used on the FOX Board G20 has 3 ports called A,B and C with 32 bit each. Any bit is wired out to the CPU pins as I/O line. Unfortunately not all the I/O lines are wired out to the FOX Board connector and not all the I/O line available on the FOX Board G20 connector are available also to the user as general purpose I/O (GPIO). That's way I/O lines are multiplexed with other peripherals like serial ports, SPI, I2C, A/D converter etc, microSD, data flash, USB etc. On the factory default configuration FOX Board G20 has 28 I/O lines available for general purpose.
On the software side Linux provides a standard way to manage the GPIO lines base on Sysfs. This is a virtual file system that exports information about devices and drivers from the kernel device model to the userspace. The Sysfs related to the GPIO is available starting from this path /sys/class/gpio/.
Each I/O lines is identified by a kernel ID. The table below shows the Kernel ID of each GPIO line available on the J6 and J7 connectors (see the complete pin-out):
| Pin number | Kernel ID | Note |
|---|---|---|
| J7.3 | 82 | |
| J7.4 | 83 | |
| J7.5 | 80 | |
| J7.6 | 81 | |
| J7.7 | 66 | |
| J7.8 | 67 | |
| J7.9 | 64 | |
| J7.10 | 65 | |
| J7.11 | 110 | 1.8V |
| J7.12 | 111 | 1.8V |
| J7.13 | 108 | 1.8V |
| J7.14 | 109 | 1.8V |
| J7.15 | 105 | 1.8V |
| J7.19 | 101 | 1.8V |
| J7.35 | 60 | |
| J7.36 | 59 | |
| J7.37 | 58 | |
| J7.38 | 57 |
| Pin number | Kernel ID | Note |
|---|---|---|
| J6.17 | 85 | |
| J6.18 | 84 | |
| J6.19 | 95 | |
| J6.20 | 94 | |
| J6.24 | 38 | |
| J6.25 | 39 | |
| J6.26 | 41 | |
| J6.36 | 42 | |
| J6.37 | 54 | |
| J6.38 | 43 |