Simple SPI on Raspberry Pi

 
    This article aims to show a simple method of setting up the SPI interface on the Raspberry Pi Computer with python. The SPI interface is one of the busses made available on the 26 pin GPIO header of the Raspberry Pi. After the setup has been completed, a test circuit using a TLC549 8 bit A/D converter with a potentiometer is used and the output displayed in a python window. The image shows the test circuit attached to our Raspberry Pi Propeller development board, but the circuit can be directly attached to the GPIO header on the R-Pi. The following procedure assumes a clean install of Raspbian 2013-02-09.


  

    An image of the GPIO header is shown to the right with the necessary connections marked in purple. They include the MOSI, MISO, CLK, CE0, and CE1 pins. In addition the +3.3v, and Ground pins will also be needed to power the slave device.

    Setup for SPI use is rather simple. I had to do a lot of Googling to find the simplest method to enable the SPI and have come up with the following procedure:

First it is necessary to enable the peripheral.

    Edit:    
 /etc/modprobe.d/raspi-blacklsit.conf

    Add '#' in front of the line spi-bcm2708. Save the file.

    Next reboot with:
 sudo reboot

    At the prompt type:
 lsmod

You should see spi_bcm2708 in the list.

So far so good.


    Next we have to update the Raspberry Pi in order to be able to find the files
in the following steps. Update using
:
 sudo apt-get update

    With that now complete, it is necessary to install python-dev with:
 sudo apt-get install python-dev

    Finally, it is time to install the python SPI wrapper needed for python to access the port.
mkdir python-spi
cd python-spi
wget https://raw.github.com/doceme/py-spidev/master/setup.py

wget https://raw.github.com/doceme/py-spidev/master/spidev_module.c
sudo python setup.py install

    With all the setup now complete, it's time to write some python to test the interface.
Make sure that the python script is in the same directory as the spidev file created above. it will
be in the /build/lib.linux-armv6l-2.7 directory.
Open a python editor, either through IDLE, or just a text file, and type the following:
import spidev
import time

spi = spidev.SpiDev()
spi.open(0,0)
while True:
    resp = spi.xfer2([0x00])
    print resp[0]
    time.sleep(1)
 
    This python script will send one byte = 0 and read one byte and display it in the window,
    wait one second and repeat.

    The above program will work nicely with the schematic below:


 The key parts of the SPI interface are as follows:
    spi.open(0,0) will open bus 0, CE0.
    spi.open(0,1) will open bus 0, CE1.

    spi.xfer2([array of bytes]) will send an array of bytes keeping the CE asserted the whole time.
    spi.xfer([array of bytes])   will send an array of bytes de-asserting and re-asserting the CE with every byte.

    The above circuit is pin compatible with the Microchip MCP3001 10 bit A/D converter. However, it will be necessary to alter the python script to send 2 bytes and format the 2 byte response for a 10 bit value as per the datasheet.

Copyright © 2012 by 100RandomTasks. All rights reserved.
Comments