Raspberry Pi SPI

SPI-Python: Hardware SPI for RasPi from Python

This implementation of SPI in Python is hardware based. Linux-level C code is compiled as a Python module. I first learned how to get SPI working on the Raspberry Pi with help from a tutorial by Brian Hensley. However, I love Python and I wanted to use this hardware implementation of SPI from within Python. I little searching told me that what I wanted to do was to “extend” Python with C and so this reference was very helpful.

1 – GETTING SPI TO WORK IN LINUX WITH C

Before you do anything, make sure you have more than 2GB at your disposal on your SD card. You may need to simply expand the system’s partition if you are using a larger card.

Now create a short between MOSI and MISO (=>)

Download the test file:

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 that file, we want to change the device to “spidev0.0″. Compile the modified file – we’ll use it later!

gcc spidev_test.c

First, let’s try to get SPI working on the Raspi the easy way:

sudo modprobe spi_bcm2708

To see if it works, try executing the file that we just compiled:

sudo ./a.out

If you are successful, you should get the following sequence back:

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

If that doesn’t work, try the command-line sequence extracted from Brian’s tutorial which pretty much updates everything:

sudo apt-get update

sudo apt-get upgrade

sudo apt-get install git

sudo wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update

sudo chmod +x /usr/bin/rpi-update

sudo rpi-update

sudo shutdown -h now

Now try again with “sudo ./a.out”. If it doesn’t work… check your wiring or leave a comment I guess :S

Regardless of the method used, you should modify “/etc/modprobe.d/raspi-blacklist.conf” and remove spi_bcm2708 so that SPI works on boot-up.

2 – GETTING SPI TO WORK FROM PYTHON

Up until now I was pretty much summarizing the sequence from Brian’s blog. Right now we have C code that runs the hardware SPI but wouldn’t it be nice to access it all from Python? This is where a C Extension to Python comes in handy. I modified the spidev.c file so that it would create a C extension. The results are hosted here on Github (If you want to recompile them, the instructions are in the comments of the spidev.c file).

If you want to try things out with an Arduino, wire things up as shown on MitchTech and upload the sample Arduino sketch from Github.Otherwise, just keep things shorted and you’ll get the same results as before. Make sure spi.so is locally accessible and launch Python using sudo (otherwise you can’t access the SPI peripheral) and try the following:

The Arduino sketch simply echos back the byte it just received. Notice that the first byte received was 0 because the Arduino was echoing nothing back and also the 8 never gets echoed back because we stopped listening!

Here is the (boring) output from the Arduino:

NOTE: YOU MAY SHORTEN THE LIFE OF YOUR PI WITH THIS CONFIGURATION. Get a logic shifter chip to bring the 5V Arduino signal down to a safe 3.3V for the Raspberry Pi. This might work.

Here is a comprehensive list of functions.

 

TO-DO: Right now, the intialize function is only OK. You can initialize with no arguments for default settings, or you can input four integers to put different parameters. Simply write

spi.initialize(MODE, BITSperMESSAGE, SPEED, DELAY)

I tried to add more functions like, spi.initialize(BITSperMESSAGE, SPEED) but it didn’t seem to work for me. Please branch and fix it if you can!