[][src]Module rppal::spi

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 points to an SPI bus (0, 1, 2), and S to a Slave Select pin (0, 1, 2). Which of these buses and pins is 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.

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 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-3cs to /boot/config.txt. Replace 3cs with either 2cs or 1cs if you only require 2 or 1 Slave Select pins. The associated pins are listed below.

SPI2 shares the same characteristics and limitations as SPI1. It can be enabled by adding dtoverlay=spi2-3cs to /boot/config.txt. Replace 3cs with either 2cs or 1cs if you only require 2 or 1 Slave Select pins. The associated pins are listed below.

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.