22 May 2013

Raspberry Pi + PiFace Access the I/O pins through GPIO interface

I purchased a PiFace last week from modmypi and have played with it for a few days. If you need to connect your Raspberry Pi to real world then you may need more IO pins. The PiFace is an IO expansion board for the Raspberry which features 2 changeover relays, 8 open-collector outputs, 8 digital inputs, 8 LED indictors and 4 switches. It uses the MCP23S17 SPI GPIO expansion chip. With a slight change on the Linux kernel, it is possible to access I/O pins through file system.

Get the kernel source

The Raspberry Pi cannot run a mainline Linux kernel. A patched version of the kernel is maintained by the Raspberry Pi Foundation and should be downloaded from the RPI linux section on GitHub. You will need to get a compiler in order to build kernel. Please see this wiki page for more details.

Make change

Modify the bcm2708.c file as shown in bellow.

Enabling the SPI module

To load the SPI driver manually, type:
# modprobe spi-bcm2708
This will not persist after a reboot. To permanently enable the SPI module comment out the spi module blacklist line in /etc/modprobe.d/raspi-blacklist.conf (you will have to be root).

Build the kernel module

The MCP23xxx I/O expander driver needs to be enabled in your kernel configuration. The drivers can be built as loadable modules or as built-in drivers (the loadable method is shown here):

        Device Drivers 
            GPIO Support 
                Microchip MCP23xxx I/O expander [M]

Now "make" your kernel as usual.

    make ARCH=arm CROSS_COMPILE=${<your-cross-toolchain-prefix>}

Trasnsfer your build to Raspberry Pi

Write the kernel image (Image) file to SD card.
Install the kernel modules on SD card.

Check it works

Open up a terminal and then turn on the first LED.
$ sudo su -
# echo 64 > /sys/class/gpio/export
# echo out > /sys/class/gpio/gpio64/direction
# echo 1 > /sys/class/gpio/gpio64/value
The first LED should now be illuminated.