These are general notes, there are often caveats with GPIO/SPI applications on any embedded device.
shows two SPI ports
spi1 for most non-Compute Module Raspberry Pi models.
have three SPI ports, with the addition of
spi2 that’s only available on the Compute Modules.
Python is a popular way to access the Raspberry Pi SPI.
is a popular Python module to use SPI.
For example, the Raspberry Pi SPI maximum clock speed is set to 1 MHz by
import spidev spi = spidev.SpiDev() spi.open(bus, device) spi.max_speed_hz=1000000
As noted in later sections, having the correct maximum clock speed and correct polarity is essential for SPI communications to work.
You might be familiar with asynchronous protocols such as used in RS232 connections, where the baud rate must be agreed upon in advance by communicating peripherals. Only the SPI controller initiates communications, and the controller must know how many bytes to receive for a given command so as not to cut off the communications too early. Be sure polarity and phase are compatible between devices.
SPI consists of four wires: clock, select, MISO and MOSI.
The clock line is unidirectional from the controller to peripheral. The peripheral cannot initiate communications. Faster clocks require shorter wires, as remember a 10 MHz square wave needs several harmonics to keep usable shape–perhaps up to 100 MHz. How good of a transmission line is your knotted hookup wire at 100 MHz? not so good perhaps.
Consider clock speed if a peripheral is not responding, or responding erratically, especially considering that the Raspberry Pi 2/3 SPI clock varies with the VPU speed.
The Raspberry Pi can select more than two peripherals by using a binary decoder chip, assuming you have a recent kernel and the appropriate software and configuration.
MISO is unidirectional from each peripheral to controller. The MISO line is shared between all peripherals. Only one peripheral, selected by Chip Select line may use MISO at once.
MOSI is unidirectional from controller to all peripherals. The MOSI line is shared between all peripherals. Only the peripheral selected by Chip Select should respond to MOSI on MISO.
SPI is a full-duplex protocol, that is, the controller can send while the peripheral transmits (for capable hardware).
Debugging SPI is most convenient with a four-channel oscilloscope, particularly if the scope has SPI decode.
Note: In some peripherals, MISO is daisy-chained with MOSI, but your datasheet would tell you for those cases.
/boot/overlays/README for more details on configuring the GPIO for SPI.