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,855
  • 15
  • 61
  • 119
Sergei Basharov
  • 283
  • 1
  • 3
  • 6
  • "Is it possible to make it like this?" -> Seems to me that's exactly how it is, except the Pi has CE0 and CE1 instead of SS1, SS2, SS3. The SCLK, MOSI, and MISO bus lines are shared amongst all devices in both cases. – goldilocks May 26 '15 at 18:40

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,024
  • 5
  • 73
  • 106
  • 1
    "allow arbitrary gpios" a good idea in its own right and what one might expect from an embedded system. Thanks for the input. – Ghanima May 26 '15 at 19:01
  • Joan, please explain how to use "another SPI device with 3 chip selects" with "pigpio" library, or link to code example. This will help answer the question. Thanks – Alex Mar 16 '16 at 15:28
  • See http://abyz.co.uk/rpi/pigpio/cif.html#spiOpen. GPIO 16-21 are connected to the auxiliary SPI. See http://abyz.co.uk/rpi/pigpio/index.html#Type_3. Alternatively the now current Linux SPI driver (spi_bcm2853) is said to support arbitrary GPIO as chip selects. See /boot/overlays/README. – joan Mar 16 '16 at 15:34
  • "The Pis with the 40 pin expansion header have another SPI device with 3 chip selects" there is only 2 cs on 40 pin header –  Oct 03 '16 at 16:36
  • 1
    @meh There are 5 chip selects on the 40 pin expansion header. Two for the main SPI and 3 for the auxiliary SPI. The auxiliary SPI signals are shown in lower case at http://abyz.co.uk/rpi/pigpio/index.html#Type_3 – joan Oct 03 '16 at 16:43
  • @joan does this mean that, without any extra hardware multiplexers, I can use GPIO pins as chip selects for any number of SPI devices (eg small TFT display screens)? For example using https://github.com/cskau/Python_ST7735/blob/master/examples/image.py and calling GPIO.output(n, 0) to select display n before the last disp.display(image) command? – pinhead Sep 12 '17 at 18:05
  • @pinhead Yes, you can use any GPIO as a chip select (as long as its not being used for another purpose). – joan Sep 14 '17 at 03:55
  • Does anyone have the answer with the new driver? – Adam Tegen Feb 06 '18 at 18:12
  • @AdamTegen The new driver supports arbitrary slave selects. For full details see /boot/overlays/README. – joan Feb 06 '18 at 18:43
  • Does it support an arbitrary number of them, or just two? The documentation was not clear to me. – Adam Tegen Feb 06 '18 at 20:21
  • @AdamTegen It is not as flexible as I thought. It only supports the number of slave selects supported by the underlying hardware (2 for SPI0, 3 for SP1). I don't understand that restriction. Perhaps look at the source for spi1-3cs (wherever that is) and adapt that? – joan Feb 06 '18 at 21:10
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
  • 8
  • 15
Ghanima
  • 15,855
  • 15
  • 61
  • 119
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
  • 59,890
  • 31
  • 101
  • 209
Alexander
  • 463
  • 3
  • 6