This post is more an aide-memoire after I spent a day trying to get SPI working on a Raspberry Pi 2 running the latest Raspbian Jessie release.
Back it up Les, what is SPI?
No it's not another Police forensic series, well to quote Wikipedia
The Serial Peripheral Interface (SPI) bus is a synchronous serial communication interface specification used for short distance communication, primarily in embedded systems. The interface was developed by Motorola and has become a de facto standard. Typical applications include Secure Digital cards and liquid crystal displays.
SPI devices communicate in full duplex mode using a master-slave architecture with a single master. The master device originates the frame for reading and writing. Multiple slave devices are supported through selection with individual slave select (SS) lines.
Sometimes SPI is called a four-wire serial bus, contrasting with three-, two-, and one-wire serial buses. The SPI may be accurately described as a synchronous serial interface, but it is different from the Synchronous Serial Interface (SSI) protocol, which is also a four-wire synchronous serial communication protocol, but employs differential signaling and provides only a single simplex communication channel.
Okay back to the issue!
I purchased an RFID reader, which reads cards containing a special chip. They are commonly used in Oyster cards, security passes etc.
Yes dear reader I did solder the pins on
I connected the reader to the GPIO of the Raspberry Pi and then proceeded to install software to work with Python.
The software was
The board was connected correctly, I checked the wiring. Software installed with no errors and reporting that it was ready to read a card. SPI enabled using
$ sudo raspi-config
I scratched my head, asked on Twitter, but alas the solution was not forthcoming.
Then, later in the day I was sent this link to a Github issue that had been raised for SPI on the latest version of Raspbian.
So what was the fix?
tl;dr You need to do these steps to enable SPI for the 27-05-2016 Raspbian Jessie image.
In a terminal, on a Raspberry Pi connected to the Internet do the following.
Enable SPI in raspi-config
- Navigate to Advanced Options.
- In the new menu navigate to SPI.
- Enable SPI
- Exit raspi-config
Now we need to reboot the Raspberry Pi.
Once our Raspberry Pi has rebooted, open a new terminal and enter the following.
sudo apt update && sudo apt upgrade -y sudo apt install python-dev
This will update all of the software on your Raspberry Pi and then install some Python specific tools that we need for the next step.
In the terminal, we will now clone a Github repository that contains a fixed version of SPI-py. This is another branch by the person who maintains this.
git clone https://github.com/mab5vot9us9a/SPI-Py
Now navigate into the SPI-Py directory and install the software.
cd SPI-Py sudo python setup.py install
Now we need to come out of that directory and clone another Github repository, this time it is the software to read and write RFID cards.
cd .. git clone https://github.com/mxgxw/MFRC522-python
Now we need to navigate into the MFRC522-python directory and test that our reader is working.
sudo python Read.py
Welcome to the MFRC522 data read example Press Ctrl-C to stop. Card detected Card read UID: 133,48,217,101 Size: 8 Sector 8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Card detected Card read UID: 133,48,217,101 Size: 8 Sector 8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Card detected Card read UID: 195,87,55,213 Size: 8 Sector 8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Card detected Card read UID: 195,87,55,213 Size: 8 Sector 8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
I can now read RFID cards and tags.....Let the games begin!
Many thanks to all of you who tweeted suggestions, ideas and supported me as I lost my sanity :)
Special thanks to Gordon and Liam, engineers at the Raspberry Pi Foundation for their help.