Expand description
Interface for the main and auxiliary SPI peripherals.
RPPAL provides access to the available SPI buses by using the spidev
device
interface through /dev/spidevB.S
, where B refers to an SPI bus, and S to
a Slave Select pin. Which buses and pins are available depends on your
Raspberry Pi model and configuration, as explained below.
SPI buses
The Raspberry Pi’s GPIO header exposes several SPI buses. SPI0 is available on all Raspberry Pi models. SPI1 is available on models with a 40-pin header. SPI2 is only available on the Compute and Compute 3. SPI3 through SPI6 are only available on the Raspberry Pi 4 B, 400 and 5.
SPI0
SPI0 is disabled by default. You can enable it by running
sudo raspi-config
, or by manually adding dtparam=spi=on
to
/boot/config.txt
. The associated pins are listed below.
- MISO: BCM GPIO 9 (physical pin 21)
- MOSI: BCM GPIO 10 (physical pin 19)
- SCLK: BCM GPIO 11 (physical pin 23)
- SS:
Ss0
BCM GPIO 8 (physical pin 24),Ss1
BCM GPIO 7 (physical pin 26)
SPI1
SPI1 is an auxiliary peripheral that’s referred to as mini SPI. According
to the BCM2835 documentation, using higher clock speeds on SPI1 requires
additional CPU time compared to SPI0, caused by smaller FIFOs and no DMA
support. It doesn’t support Mode1
or Mode3
. SPI1 can be enabled by
adding dtoverlay=spi1-1cs
to /boot/config.txt
. Replace 1cs
with
either 2cs
or 3cs
if you require 2 or 3 Slave Select pins.
The associated pins are listed below.
- MISO: BCM GPIO 19 (physical pin 35)
- MOSI: BCM GPIO 20 (physical pin 38)
- SCLK: BCM GPIO 21 (physical pin 40)
- SS:
Ss0
BCM GPIO 18 (physical pin 12),Ss1
BCM GPIO 17 (physical pin 11),Ss2
BCM GPIO 16 (physical pin 36)
SPI2
SPI2 shares the same characteristics and limitations as SPI1. It can be
enabled by adding dtoverlay=spi2-1cs
to /boot/config.txt
. Replace
1cs
with either 2cs
or 3cs
if you require 2 or 3 Slave Select
pins. The associated pins are listed below.
- MISO: BCM GPIO 40
- MOSI: BCM GPIO 41
- SCLK: BCM GPIO 42
- SS:
Ss0
BCM GPIO 43,Ss1
BCM GPIO 44,Ss2
BCM GPIO 45
SPI3
SPI3 can be enabled by adding dtoverlay=spi3-1cs
to /boot/config.txt
. Replace
1cs
with 2cs
if you require 2 Slave Select pins. The associated pins are listed below.
- MISO: BCM GPIO 1 (physical pin 28)
- MOSI: BCM GPIO 2 (physical pin 3)
- SCLK: BCM GPIO 3 (physical pin 5)
- SS:
Ss0
BCM GPIO 0 (physical pin 27),Ss1
BCM GPIO 24 (physical pin 18)
SPI4
SPI4 can be enabled by adding dtoverlay=spi4-1cs
to /boot/config.txt
. Replace
1cs
with 2cs
if you require 2 Slave Select pins. The associated pins are listed below.
- MISO: BCM GPIO 5 (physical pin 29)
- MOSI: BCM GPIO 6 (physical pin 31)
- SCLK: BCM GPIO 7 (physical pin 26)
- SS:
Ss0
BCM GPIO 4 (physical pin 7),Ss1
BCM GPIO 25 (physical pin 22)
SPI5
SPI5 can be enabled by adding dtoverlay=spi5-1cs
to /boot/config.txt
. Replace
1cs
with 2cs
if you require 2 Slave Select pins. The associated pins are listed below.
- MISO: BCM GPIO 13 (physical pin 33)
- MOSI: BCM GPIO 14 (physical pin 8)
- SCLK: BCM GPIO 15 (physical pin 10)
- SS:
Ss0
BCM GPIO 12 (physical pin 32),Ss1
BCM GPIO 26 (physical pin 37)
SPI6
SPI6 can be enabled by adding dtoverlay=spi6-1cs
to /boot/config.txt
. Replace
1cs
with 2cs
if you require 2 Slave Select pins. The associated pins are listed below.
- MISO: BCM GPIO 19 (physical pin 35)
- MOSI: BCM GPIO 20 (physical pin 38)
- SCLK: BCM GPIO 21 (physical pin 40)
- SS:
Ss0
BCM GPIO 18 (physical pin 12),Ss1
BCM GPIO 27 (physical pin 13)
SPI6 is tied to the same GPIO pins as SPI1. It’s not possible to enable both buses at the same time.
Alternative pins
The GPIO pin numbers mentioned above are part of the default configuration.
Some of their functionality can be moved to different pins. Read
/boot/overlays/README
for more information.
Buffer size limits
By default, spidev
can handle up to 4096 bytes in a single transfer. You
can increase this limit to a maximum of 65536 bytes by appending
spidev.bufsiz=65536
to the single line of parameters in /boot/cmdline.txt
.
Remember to reboot the Raspberry Pi afterwards. The current value of bufsiz
can be checked with cat /sys/module/spidev/parameters/bufsiz
.
Not supported
Some features exposed by the generic spidev
interface aren’t fully
supported by the underlying driver or the BCM283x SoC: SPI_LSB_FIRST
(LSB
first bit order), SPI_3WIRE
(bidirectional mode), SPI_LOOP
(loopback mode),
SPI_NO_CS
(no Slave Select), SPI_READY
(slave ready signal),
SPI_TX_DUAL
/SPI_RX_DUAL
(dual SPI), SPI_TX_QUAD
/SPI_RX_QUAD
(quad SPI),
and any number of bits per word other than 8.
If your slave device requires SPI_LSB_FIRST
, you can use the
reverse_bits
function instead to reverse the bit order in software.
SPI_LOOP
mode can be achieved by connecting the MOSI and MISO pins
together.
SPI_NO_CS
can be implemented by connecting the Slave Select pin on your
slave device to any other available GPIO pin on the Pi, and manually
changing it to high and low as needed.
Structs
- Part of a multi-segment transfer.
- Provides access to the Raspberry Pi’s SPI peripherals.
Enums
- Bit orders.
- SPI buses.
- Errors that can occur when accessing the SPI peripheral.
- SPI modes indicating the clock polarity and phase.
- Slave Select polarities.
- Slave Select pins.
Functions
- Reverses the bits of each byte in
buffer
.
Type Definitions
- Result type returned from methods that can have
spi::Error
s.