SPI or I2C board to board communication

Discussion in 'General Electronics Chat' started by adrenalina, Aug 6, 2014.

  1. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    Hello everybody. I was wondering if you could help me with the following problem I'm having. I'm working on a project that communicates a master device to multiple slaves, 12 to be exact, on different boards. The connection would be using ribbon cable. My question is what would be the most reliable to use, SPI or I2C, using a clock speed of 100 kHz? The cable length would be 40 cm. I attach an image to better explain the idea.

    I know RS-485 or CAN would be more reliable protocols, but because of cost, SPI or I2C would be better.
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,418
    3,355
    Do the slaves have to talk back or are they in receive mode only?
     
  3. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,378
    494
    The trouble with SPI is that you would need 12 select pins, one for each slave.
     
  4. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    Some would have to talk back. Others would only receive.
     
  5. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    12 IO pins are available so that wouldn't be a problem.
     
  6. MrChips

    Moderator

    Oct 2, 2009
    12,418
    3,355
    Are the slaves programmable devices, i.e. are they implemented using microcontrollers?

    If so I can think of 1-wire or 2-wire solutions without the need for separate SLAVE SELECT signals.

    You have to create a network protocol where the slave address is embedded in the message.

    Can you use standard UART 2-wire TTL signals?
     
  7. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,378
    494
    Also, in i2c all the devices share the two lines. Your block diagram shows the data flow. It does not show the actual physical layout. How are you going to connect the devices to the ribbon cable?
     
  8. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    The slaves would be microcontrollers and ICs, for example a led driver IC that uses I2C or SPI.


    I would be using headers and an IDC connector. the 2 lines would be connected to each header on the main board plus a ground and supply.
     
  9. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    You could do SPI at higher speed than 100 kbits/sec by using a shift register chain made of individual flip-flops, one for each device, for the chip selects. Preset them all to 1 and then shift a single 0 through all devices and feed the output of the last flip-flop back to the master.

    So the interface lines on the master would be
    Code ( (Unknown Language)):
    1. SPI_DATAOUT     OUTPUT
    2. SPI_DATAIN      INPUT
    3. SPI_DATA_CLOCK  OUTPUT
    4. SELECT _RESET   OUTPUT
    5. SELECT_DATA     OUTPUT
    6. SELECTINPUT     INPUT
    7.  
    Although bi-directional buffers are available for SDA under I2C I've never really trusted them. Nothing beats a buffer chip powered from 3.3V that is 5V tolerant with TTL thresholds.
     
  10. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    More info on the project. We have another pin on the header which is simply a digital IO and analog from the microcontroller on the master board. This is used for devices that don't require a communication protocol, for example and analog sensor or to read a button. Different devices that do various things can be connected.

    The idea is that any device can be connected to any header and you don't need a device address. On the programming side you would simply say the type of device that is connected on the header. So for I2C which requires an address we came up with the idea in the image. A mosfet is between the clock line that is controlled by the extra IO pin. The clock signal on the slave won't change and stay high unless the mosfet is turned on.

    The other 2 mosfets on the image are there to simulate the open collector of I2C.

    In a way this is kind of like SPI, using a "slave select", but the difference would be that the drivers are open collector and not push pull.

    Hopefully I'm clear enough.
     
  11. MrChips

    Moderator

    Oct 2, 2009
    12,418
    3,355
    I have implemented many multiprocessor networks. You can do it with one or two wires.

    I would not use SPI or I2C.
     
  12. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    How would the implementation be using 1 or 2 wires if the devices cannot have a pre programmed address and not all of the devices are microcontrollers.
     
    Last edited: Aug 7, 2014
  13. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    If each and every I2C device can be assigned it's own address then it would need fewer lines to drive them all. If that is not possible (today and in all future systems) then SPI has the advantage of the existing chip select line.

    One could make a chip select for I2C, as there exists chips for this exact purpose. I believe NXP for one makes them. I would not recommend making your own device for this, as the slave may also use the clock line (it's how it requests extra time to process an inputed byte).

    Without explicit I2C addresses or for SPI you need X signal lines for X devices all running down your cable.

    Hopefully X is finite and fixed now and forever. Add a few extrans in anyway just in any case.
     
  14. MrChips

    Moderator

    Oct 2, 2009
    12,418
    3,355
    My choice would be to redesign the system so that every device uses a microcontroller.
     
  15. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    There are few things that are unclear in your design.

    1. If devices do various, possibly unknown, things, what good does it make to connect them if MCU doesn't know what the device does?

    2. If you insert a new device, you need to re-program the MCU. Why does it make any difference if you reprogram is with device address (for I2C) or slot position (for SPI)?

    3. Are these devices removable? That is do you need a mechanism to figure out if device was disconnected or connected to your network?
     
  16. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    One huge benefit of this system is each micro can be used as an I2C slave device with a unique address set by either jumper or electronically thru internal EEPROM.
     
  17. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    When the program for the master is written it is told the type of device in each slot. There can be multiple devices of the same type and it would not be possible to change the address of a device.

    If it's not possible to change the system to only use microcontrollers and considering that the devices would not be able to have addresses would SPI be the best choice?
     
    Last edited: Aug 8, 2014
  18. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    If you cannot use addresses, it's better to go with SPI, or even UART.

    BTW: In the schematics that your posted, when master's side goes low, the slave's side will follow regardless of the state of the FET in the middle. This is because when source voltage is above drain voltage in N-FET, it simply works as a diode.
     
  19. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    As I have stated "Without explicit I2C addresses or for SPI you need X signal lines for X devices all running down your cable. Hopefully X is finite and fixed now and forever."

    As long as you don't mind adding all these signal lines the either I2C or SPI are equivalent methods, keeping in mind I2C with like addresses will need the bus drivers I mentioned (NOT fets).
     
Loading...