Raspberry Pi SPI Loopback Testing

This post explains how to do SPI loopback testing in RaspberryPi.
This is the pinout of RaspbeyyPi GPIO header.

Next short the MOSI and MISO pins (GPIO10 and GPIO09)

Now download SPI test code from below link

https://raw.githubusercontent.com/raspberrypi/linux/rpi-3.10.y/Documentation/spi/spidev_test.c

Compile it

gcc -o spidev_test spidev_test.c

Run the code using spi dev

./spidev_test -D /dev/spidev0.0

If you are getting the hex characters as shown in screenshot, the SPI loopback is working.

Enabling SPI on Raspberry Pi

The Serial Peripheral Interface bus (SPI) is a synchronous serial communication interface specification used for short distance communication, primarily in embedded systems. The interface was developed by Motorola.

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.

Read more details on Wikipedia

The SPI bus specifies five logic signals:

SCLK: Serial Clock (output from master).
MOSI: Master Out Slave In (data output from master).
MISO: Master In Slave Out (data output from slave).
SDIO: Serial Data I/O (bidirectional I/O)
SS: Slave Select (often active low, output from master).

I am using RaspberryPi Model B. First i turned on the Pi, i used NOOBS Raspbian operating system (https://www.raspberrypi.org/downloads/noobs/). I don’t have a HDMI display, so i connected it to network found out the ip of the Pi and did ssh using the following command

ssh pi@ip-address

The default password for ssh is “raspberry” if you are using NOOBS debian distro.

The SPI is not enabled by default. To enable SPI issue the following command

sudo raspi-config

Then Select Interfacing Options

Select SPI

It will ask for Enable or Disable, Enable SPI

Finish the config and reboot

After reboot you can see two spidev’s by issuing the command

ls /dev/spi*

This mean SPI is enabled on your Raspberry Pi