SPI0

SPI0 uses GPIO7-11 and is found on all rPi’s to date.

It has two CS lines – 0 and 1 – allowing control 2 devices natively. It is also possible to run the SPI module without a CS line – i.e. enabling the use of any free GPIO pin as a manual CS line.

It supports clock speeds of 8kHz to 125mHz.

The device reports supporting the following Linux SPIdev options:

  • SPI_CS_HIGH (CS inverted)
  • SPI_3WIRE (MOSI & MISO share the same line)
  • SPI_NO_CS (Device is signalled elsewhere)

Prerequisite Configuration

/boot/config.txt

Add the following to enable SPI0

dtparam=spi=on

Kernel Module

The module is called spi_bcm2835. There is an older module called spi_bcm2708 which amongst other things only supports clock frequencies in powers of 2. See here for a discussion: https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=43442&p=347073

If SPI0 is enabled in the config.txt the kernel module should load automatically after a reboot. Check with lsmod.

$ lsmod | grep spi
spi_bcm2835             7216  0

If it doesn’t appear make sure this isn’t blacklisted in/etc/modprobe.d/raspi-blacklist.conf. When the module is running you will find two device nodes have been created.

crw-rw---- 1 root spi 153, 0 Jan 11 15:47 spidev0.0
crw-rw---- 1 root spi 153, 1 Jan 11 15:47 spidev0.1

There is one device per CS line

System Config

You should already have a user group called ‘spi’ which has read/write access to the device nodes. Add yourself and any other accounts that need access to this group if you have not already done so.

sudo usermod -a -G spi <acccount>

Issues

Despite the Broadcom 2835 ARM Peripherals document (p150) discussing multiple transfer sizes including 9bit ‘Lossi’  – commonly used by LCD screens – however when the device is queried it only supports 8bit transfers. This is a real pity. Perhaps someone can explain that to me.