12

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

  • "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
12

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.

  • 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 abyz.co.uk/rpi/pigpio/cif.html#spiOpen. GPIO 16-21 are connected to the auxiliary SPI. See 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 – user45238 Oct 3 '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 abyz.co.uk/rpi/pigpio/index.html#Type_3 – joan Oct 3 '16 at 16:43
3

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.

protected by Community Dec 29 '16 at 8:50

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.