theoatvecna
Posts: 7
Joined: Wed Mar 02, 2016 5:20 pm

More than 2 SPI slaves on RPI3

Sat Apr 16, 2016 4:22 am

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!

User avatar
mikronauts
Posts: 2722
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: More than 2 SPI slaves on RPI3

Sat Apr 16, 2016 7:39 pm

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!
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
adun
Posts: 102
Joined: Fri Mar 20, 2015 9:25 am
Location: Switzerland

Re: More than 2 SPI slaves on RPI3

Sat Apr 16, 2016 8:47 pm

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

User avatar
joan
Posts: 14266
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: More than 2 SPI slaves on RPI3

Sat Apr 16, 2016 9:32 pm

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.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: More than 2 SPI slaves on RPI3

Sun Apr 17, 2016 4:01 am

Depending on the device connected it is possible to have up to 8 devices per CS line. The MCP23S16 is one such device.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

ChristophK
Posts: 15
Joined: Thu Oct 06, 2016 1:34 pm

Re: More than 2 SPI slaves on RPI3

Thu Oct 06, 2016 1:40 pm

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?

User avatar
joan
Posts: 14266
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: More than 2 SPI slaves on RPI3

Thu Oct 06, 2016 3:35 pm

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.

ChristophK
Posts: 15
Joined: Thu Oct 06, 2016 1:34 pm

Re: More than 2 SPI slaves on RPI3

Mon Oct 10, 2016 6:44 am

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...

Return to “Interfacing (DSI, CSI, I2C, etc.)”