Module rppal::spi
[−]
[src]
Interface for the SPI peripherals.
RPPAL provides access to the available SPI peripherals by using the spidev 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.
- 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 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.
- 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 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.
- 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
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 adding
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
.
Unsupported features
Several features offered 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
Spi |
Provides access to the Raspberry Pi's SPI peripherals. |
TransferSegment |
Part of a multi-segment transfer. |
Enums
BitOrder |
Bit orders. |
Bus |
SPI buses. |
Error |
Errors that can occur when accessing the SPI peripherals. |
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 |
Type Definitions
Result |
Result type returned from methods that can have |