Page 1 of 1

More than 2 SPI slaves on RPI3

Posted: Sat Apr 16, 2016 4:22 am
by theoatvecna
I have searched for a couple of hours to find some clear guidance on this, but perhaps my search foo is poor. Hopefully this question is addressed elsewhere and I just haven't been able to find it.

So, I would like to interface several SPI devices with my RPI3. I have found many posts indicating that this is possible, but they all seem to reference the older rpis. I thought I saw somewhere that RPI3 had hardware support for 3 SPI Chip Enable/Select lines, but haven't been able to find it. Only two seem to be called out on the GPIO diagrams. So assuming there are actually only two, then what is the appropriate way to use a 74hc138 or similar address decoder on a RPI3? (or if I have enough GPIO available, can I just use other pins as additional SPI CE pins?)

For specifics, right now I want to connect a mcp3008 ADC, and a LSM9DS0 IMU (which requires two CE pins as it has two separate SPI slaves, one for accelerometer and magnetometer, and one for gyros). It is likely that I will want to connect a couple more SPI devices in a future rev.

Any help or guidance is much appreciated!

Re: More than 2 SPI slaves on RPI3

Posted: Sat Apr 16, 2016 7:39 pm
by mikronauts
http://www.mikronauts.com/raspberry-pi/ ... and-howto/

Gives 8 chip selects from one, will work just fine on a Pi3
theoatvecna wrote:I have searched for a couple of hours to find some clear guidance on this, but perhaps my search foo is poor. Hopefully this question is addressed elsewhere and I just haven't been able to find it.

So, I would like to interface several SPI devices with my RPI3. I have found many posts indicating that this is possible, but they all seem to reference the older rpis. I thought I saw somewhere that RPI3 had hardware support for 3 SPI Chip Enable/Select lines, but haven't been able to find it. Only two seem to be called out on the GPIO diagrams. So assuming there are actually only two, then what is the appropriate way to use a 74hc138 or similar address decoder on a RPI3? (or if I have enough GPIO available, can I just use other pins as additional SPI CE pins?)

For specifics, right now I want to connect a mcp3008 ADC, and a LSM9DS0 IMU (which requires two CE pins as it has two separate SPI slaves, one for accelerometer and magnetometer, and one for gyros). It is likely that I will want to connect a couple more SPI devices in a future rev.

Any help or guidance is much appreciated!

Re: More than 2 SPI slaves on RPI3

Posted: Sat Apr 16, 2016 8:47 pm
by adun
With current bcm2835 driver you can use any other GPIO as additional chip select pins without needing additional hardware.
You simply have to write an overlay to enable the additional CS pins.
I've made an overlay for SPI0 for the miscap pHAT for the Pi-Zero, where GPIO 5 and 6 are enabled as CE3 and CE2 .
So this gives you the ability to connect 4 SPI devices to SPI0 bus.
Have a look at viewtopic.php?f=45&t=127522&p=950107#p950107

Re: More than 2 SPI slaves on RPI3

Posted: Sat Apr 16, 2016 9:32 pm
by joan
My pigpio will let you use the two slave selects for the main SPI device and the three slave selects for the auxiliary SPI device for a total of 5 SPI devices.

If you need more just choose your own GPIO for slave selects and toggle them yourself. It is all the SPI driver does and it's less fuss than writing a device tree blob.

Re: More than 2 SPI slaves on RPI3

Posted: Sun Apr 17, 2016 4:01 am
by Richard-TX
Depending on the device connected it is possible to have up to 8 devices per CS line. The MCP23S16 is one such device.

Re: More than 2 SPI slaves on RPI3

Posted: Thu Oct 06, 2016 1:40 pm
by ChristophK
Joan, how do I tell pigpio's spi_open not to use the official CE pins?
Can I use a spi_channel number different from 0..1 /0..2 to switch it off?
Or should I tell it via the u2/u1/u0 bits in spi_flags that all CE pins are occupied?

Re: More than 2 SPI slaves on RPI3

Posted: Thu Oct 06, 2016 3:35 pm
by joan
ChristophK wrote:Joan, how do I tell pigpio's spi_open not to use the official CE pins?
Can I use a spi_channel number different from 0..1 /0..2 to switch it off?
Or should I tell it via the u2/u1/u0 bits in spi_flags that all CE pins are occupied?
You use the spi_flags.

Code: Select all

spi_flags consists of the least significant 22 bits.

21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
 b  b  b  b  b  b  R  T  n  n  n  n  W  A u2 u1 u0 p2 p1 p0  m  m
If you want to use your own chip select GPIO set one or more of u0, u1, and u2 to 1.

E.g. to stop pigpio setting any chip selects logical or 32|64|128 into the flags.

You will have to set the GPIO you want to use low then high around the calls to spi read/write/xfer.

Re: More than 2 SPI slaves on RPI3

Posted: Mon Oct 10, 2016 6:44 am
by ChristophK
Thanks for your answer. I already found in the source at github, that channel numbers different from 0..2 / 0..1 result in an SOFT ERROR message, so my second way was right.

I also had posted an answer to this 1 year old question here:
http://raspberrypi.stackexchange.com/qu ... rom-an-adc
but this was transferred to chat http://chat.stackexchange.com/transcript/46416 for users with good reputation only.
Same problem, CE-pins are non-standard. Thats the main reason why he couldn't send a "hello" to his python-compatible ADC...