Checking SPI on Raspberry Pi

In my previous post, I described how to get SPI working on my Raspberry Pi. Now I wanted to check if it actually works.

I found a useful description on the web, but used only the second part about “testing”: http://www.brianhensley.net/2012/07/getting-spi-working-on-raspberry-pi.html

Given that the linux kernel module dealing with SPI is loaded, there is a small test application written in C that you need to download:

pi@raspberrypi ~ $ wget http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git\;a=blob_plain\;f=Documentation/spi/spidev_test.c -O spidev_test.c

In spidev_test.c, change the following line

static const char *device = "/dev/spidev1.1";
to
static const char *device = "/dev/spidev0.0";
Now compile the C source code using gcc:
gcc spidev_test.c -o spidev_test
Run the test application:
pi@raspberrypi ~ $ sudo ./spidev_test
spi mode: 0 
bits per word: 8
max speed: 500000 Hz (500 KHz) 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00
Now connect the MISO and MOSI pins and execute this test application again:
pi@raspberrypi ~ $ sudo ./spidev_test
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)
FF FF FF FF FF FF 
40 00 00 00 00 95 
FF FF FF FF FF FF 
FF FF FF FF FF FF 
FF FF FF FF FF FF 
DE AD BE EF BA AD 
F0 0D
Looks good! This is what we expect.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s