This is a /dev interface to the PIO/GPIO controller which can be used to toggle GPIO pins from userspace.
Note: This interface is deprecated. Please use the GPIO sysfs interface instead, available in 2.6.27 and later kernels. Documentation can be found in Documentation/gpio.txt in the kernel source tree.
To set up a GPIO device, first mount configfs under /config like this:
mkdir /config mount -t configfs config /config
GPIO "objects" are represented as subdirectories under /config/gpio. To create a new object, simply create a new directory:
mkdir /config/gpio/leds
After this, you will find four files under /config/gpio/leds:
gpio_id
specifies which PIO controller to use (PIOA = 0, etc.)pin_mask
specifies which particular pins to use on the controller.oe_mask
specifies which pins are used as outputs. pin_mask
is used to mask the value you write here, so remember to re-check oe_mask
whenever you change pin_mask
.enabled
specifies whether the gpio object is active. Write "1" to this file in order to create the gpio device.So if you want to control the LEDS using bits 0-7 on PORTB, you can do it like this:
echo 1 > /config/gpio/leds/gpio_id echo 0xff > /config/gpio/leds/pin_mask # Use pins 0-7 echo 0xff > /config/gpio/leds/oe_mask # Enable output on pins 0-7 echo 1 > /config/gpio/leds/enabled # Create the device
If mdev is enabled, /dev/gpio0 should appear after this. If you've created other GPIO devices before, the number may be different. Please check "dmesg" if you're unsure.
Now, you can control the LEDs by writing to /dev/gpio0:
echo -ne '\x00\x00\x00\xaa' > /dev/gpio0 # Turn on all odd-numbered LEDs
The "gpio-test" program demonstrates various ways to read data from the GPIO device. Depending on the command line options, it can use regular blocking reads, async I/O (signals), poll(), nonblocking I/O or a combination.