Getting started with SPI on Raspberry PI
Serial Peripheral Interface (SPI) synchronous serial communication interface first developed by Motorola in the 1980s.
I want to use some SPI based transceiver modules in future projects e.g. this CC2500 based module.
The first step is to get SPI working on the Raspberry Pi.
Enable SPI
I enabled SPI using the raspi-config
tool.
From a Raspberry Pi terminal:
raspi-config
From the raspi-config
menu:
- 5 Interfacing Options
- 4 SPI
- Yes (Enable SPI)
- Finish
I rebooted the Pi. (I’m not sure if this is necessary.)
Testing SPI on the Raspberry Pi
A Raspberry Pi Model B was used. The MOSI pin was connected to the MISO pin according to a PIN out diagram that I found for the Model B:
The Raspberry Pi SPI documentation describes how to do a loopback test. From a Raspberry Pi terminal:
wget https://raw.githubusercontent.com/raspberrypi/linux/rpi-3.10.y/Documentation/spi/spidev_test.c
gcc -o spidev_test spidev_test.c
./spidev_test -D /dev/spidev0.0
This is the expected result:
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
If spidev_test
fails, the output looks like this:
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