Module rppal::spi[][src]

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 and 400.

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.

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.

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.

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.

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.

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.

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.

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

Segment

Part of a multi-segment transfer.

Spi

Provides access to the Raspberry Pi's SPI peripherals.

Enums

BitOrder

Bit orders.

Bus

SPI buses.

Error

Errors that can occur when accessing the SPI peripheral.

Mode

SPI modes indicating the clock polarity and phase.

Polarity

Slave Select polarities.

SlaveSelect

Slave Select pins.

Functions

reverse_bits

Reverses the bits of each byte in buffer.

Type Definitions

Result

Result type returned from methods that can have spi::Errors.