15

As what I see on the scheme, there are 2 channels for slaves - CS0 and CS1. Does it mean that I can only connect 2 chips, like MCP3008 or something? Or can I attach more slaves to these 2 attached slaves.

Does Raspberry scheme have support or anything in common with this SPI slaves connection scheme taken from Wikipedia? Is it possible to make it like this?

SPI slaves

Ghanima
  • 15,958
  • 17
  • 65
  • 125
Sergei Basharov
  • 283
  • 1
  • 3
  • 6

3 Answers3

15

Many ways.

You can sort of bit bang the slave selects, i.e. connect all the devices with shared MISO/MOSI/SCLK and ground but separate CS. Just set CS low for the device you want before calling the SPI driver. The SPI driver will try to set an CS but won't know it is not connected.

The Pis with the 40 pin expansion header have another SPI device with 3 chip selects. My pigpio library supports that device.

You can software bit bang the whole protocol.

You can add additional hardware to switch the CS line to any device you want.

Etc., etc.

EDITED TO ADD

The "proper" Raspberry Pi Linux SPI driver is currently going through review to allow arbitrary gpios to be used as CS.

EDITED TO ADD 2

The current Linux SPI driver (spi_bcm2853) is said to support arbitrary GPIO as chip selects. See /boot/overlays/README.

joan
  • 71,852
  • 5
  • 76
  • 108
4

You are right that the RaspberryPi does provide only two chip selects at its SPI bus (see here). And I assume that the SPI drivers and software solutions rely on that fact (although @joan's answer suggests that different libraries handle it differently and the official driver will allow arbitrary GPIO pins to be used as chip selects in the future).

You have however to keep in mind that a chip select is nothing more than a dedicated digital output of the Pi. Therefore you can always have a larger amount of chip selects if you use the GPIO pins. In which case however your software will be responsible to set the appropriate pins to address the right SPI slave.

Since the whole idea of chip selects is to mutually exclusively select just one single slave you can furthermore use some simple digital circuitry to decrease the amount of "wasted" GPIO pins (if you need them for other purposes too). Something like the 74HC/HCT138 a 3-to-8 line decoder/demultiplexer could be used to address 8 slaves via 8 chip selects with just 3 GPIO pins.

It's noteworthy to keep in mind that the bus lines SCLK and MOSI are shared amongst all slaves. So when plugging multiple slaves to the Pi make sure that the fan-out of the Pi is not exceeded by the load (resistance and capacitance of the input pins of the slaves) - although this is less of a problem today with the low capacitance and high resistance of digital inputs.

Glorfindel
  • 620
  • 1
  • 9
  • 16
Ghanima
  • 15,958
  • 17
  • 65
  • 125
2

There is actually an very good answer for this by an RPi engineer, PhilE, at the raspberry forum, see Sep 28, 2015. Basically, he gives an example of a Device Tree overlay that uses the possibility with spi-bcm2835 to have any free GPIOs as chip select.

Milliways
  • 62,573
  • 32
  • 113
  • 225
Alexander
  • 463
  • 3
  • 6