This module defines an object type that allows SPI transactions on hosts running the Linux kernel. The host kernel must have SPI support and SPI device interface support. All of these can be either built-in to the kernel, or loaded from modules.
Because the SPI device interface is opened R/W, users of this module usually must have root permissions.
This tutorial is written for the use with a Raspberry Pi (Raspbian Wheezy distribution), but it should match for several others, too.
First things first, it’s always the best to keep your Raspberry Pi up to date, otherwise some things here may not work. Open a new console and execute the following commands:
sudo apt-get update
sudo apt-get upgrade
If you don’t have done this yet, grab some coffee or something else because this may take up to an hour or two.
If you don’t have installed it already you need to install the Python Dev package:
sudo apt-get install python-dev
On Fedora (Pidora) the equivalent of that last command is:
sudo yum install yum-utils
sudo yum-builddep python
You need to be sure that SPI is enabled in your system (disabled by default because rarely used).
Open a new terminal and enter:
sudo nano /etc/modprobe.d/raspi-blacklist.conf
This will open a text editor. You should see a line like this in this file:
Put a # before this line to comment it out. Save (Ctrl+O) and exit (Ctrl+X). After a reboot the device should be found.
sudo reboot # --- after reboot --- lsmod
In the list lsmod outputs you should find the entry “spi_bcm2708” now.
Also you need the SpiDev Python module. Open a new terminal and execute the following commands:
Now the SPI device should be available to Python.
How to run python scripts
Of course there are several ways. You can either simple save your script and run it using:
sudo python myscript.py
Or you can mark the file as executable. For this you need to add one line at the top of your script:
Now mark it as executable:
sudo chmod +x myscript.py
Now you can execute it like any other application:
This example will open SPI and writes a byte (0xAA) to it each 0.1 second until you cancel it with Ctrl+C.
Code: Select all
spi = spidev.SpiDev() # create spi object
spi.open(0, 1) # open spi port 0, device (CS) 1
resp = spi.xfer2([0xAA]) # transfer one byte
time.sleep(0.1) # sleep for 0.1 seconds
except KeyboardInterrupt: # Ctrl+C pressed, so…
spi.close() # … close the port before exit
This script will reverse the bit ordering in one byte (if you are not able to change LSB / MSB first to your needs.
Code: Select all
byte = ((byte & 0xF0) >> 4) | ((byte & 0x0F) << 4)
byte = ((byte & 0xCC) >> 2) | ((byte & 0x33) << 2)
byte = ((byte & 0xAA) >> 1) | ((byte & 0x55) << 1)
This script will print out a byte array in a human readable format (hexadecimal). This is often useful during debugging.
Code: Select all
return ''.join(["0x%02X " % x for x in Bytes]).strip()
Description: Property that gets / sets the bits per word.
Range: 8 .. 16
Description: Disconnects the object from the interface.
Description: Property that gets / sets if the CS is active high.
Description: Property that gets / sets “loopback configuration”. This is used to test the pcb for example. Anything that gets received will be echoed back.
Description: Property that gets / sets if LSB should be transferred first or last.
Remarks: Needs Boolean value. However, this property seems to be read only and the value dependences on the endianess of the controller / cpu. The Raspberry Pi can only send MSB first, so you may need to convert the byte(s) manually (see code examples for such a script).
Description: Property that gets / sets the maximum bus speed in Hz.
Description: Property that gets / sets the SPI mode as two bit pattern of Clock Polarity and Phase [CPOL|CPHA]
Range: 0b00 = 0 .. 0b11 = 3
Syntax: open(bus, device)
Description: Connects the object to the specified SPI device.
open(X,Y) will open /dev/spidev-X.Y
Description: Read len bytes from SPI device.
Description: Property that gets / sets “SI/SO signals shared” so you have only 1 data line. Read the data sheets for more details.
Description: Write bytes to SPI device.
Description: Perform SPI transaction.
CS will be released and reactivated between blocks. delay specifies delay in μsec between blocks.
Description: Perform SPI transaction. CS will be held active between blocks.