Getting Started With A PiFace Interface Card

Part 2 - Using C

I decided to use C to run my PiFace, mainly because I am more familiar with C than Python. The steps are shown here:

Step 1

The PiFace uses the Pi's SPI interface with its own on-board I/O port extender. This means it uses 5 of the Pi's GPIO pins plus power, specifically 1 (+3.3V), 2 (+5V), 6 (0V), 19 (MOSI), 21 (MIS0), 23 (SCLK), 24 (CE0N) and 26 (CE1N). The SPI interface driver is included in the later Raspbian distributions but is not enabled by default so you will need to ensure that it loads each time your start your Pi. You do this by commenting out the blacklist spi-bcm2708 line in your /etc/modprobe.d/raspi-blacklist.conf file by inserting a # in column 1. Edit the file using the command below:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Step 2

Load the packages required for C:

sudo apt-get install automake libtool

Step 3

Download the source files - this will create a directory called piface:

git clone https://github.com/thomasmacpherson/piface.git

Step 4

To install the C pfio library, move into the C directory, then call the setup scripts and then (as root) run the install command. ./autogen.sh takes a while to process with no console output, so don't think it's hung:

cd piface/c/

./autogen.sh

./configure

make

sudo make install

Step 5

Runtime libraries have been installed and you need to refresh the library cache:

sudo ldconfig

Step 6

At this point you can create a simple C program as shown below - this will light the first LED:

#include <libpiface-1.0/pfio.h>

int main(void)
{
   pfio_init();
   pfio_digital_write(1, 1);
   pfio_deinit();
}

Step 7

Compile the program:

gcc -L/usr/local/lib/ -lpiface-1.0 -o piface_program piface_program.c

Step 8

Now run the program:

./piface_program

Continue to Part 3.