0
\$\begingroup\$

I am trying to implement a circuit where 2 SPI drives (basically two masters) speak to 1 slave. Both operate on a 3 wire SPI.

Keeping that in mind I was going to have the CLK and MOSI of both as inputs (4 inputs), and would need 2 as output. A 4:2 multiplexer.

Was going to use CS, or some other signal to act the select signal to decide between which spi goes through.

Can't seem to find anything of the sort anywhere. Is there a different type of switch I should use?

I could add two 2:1 mux's together, but I'd much rather have 1 IC - space and cost is a premium for me.

Any thoughts?

edit: To make thing's clearer, the reason i would like to add a way of isolating the two masters is because one MCU turns on before the other by a couple of seconds. I am not entirely sure the default state for the MCU that turns on later. I would like to avoid the possibility where the second MCU tries to communicate whilst the first MCU is speaking to the slave device as they will both be share the same MOSI line.

\$\endgroup\$
  • 3
    \$\begingroup\$ It's not immediately clear why you think you need a multiplexer. If one of your SPI masters is talking to the slave then the other is passive and not interfering. Unless the other master needs to be using this same SPI port to talk to some other slave at the same time there should be no conflict. \$\endgroup\$ – brhans Jan 8 at 12:13
  • \$\begingroup\$ There lots of 2:1 ICs that contain 2 or more MUXes in a single package. You can also use SPDT analog switches, if they are rated for the speeds that you are running at. \$\endgroup\$ – crj11 Jan 8 at 12:57
  • \$\begingroup\$ Can you consider the idea of crafted assembly coding as part of the existing code? Or is that impossible for your situation? \$\endgroup\$ – jonk Jan 8 at 15:01
0
\$\begingroup\$

CLK & SDA are I2C signals, not SPI. SPI uses CLK, MISO, MOSI,and chip select (slave select) to let the slave communicate. CLK, MOSI, and CS must be isolated to not drive at the same time. MISO comes from the slave and connect back to both masters. You can use two 74HC125, which is a 4-gate buffer, with each gate having an output enable. Enable one chip or the other to let the three signals thru to talk to the slave. Cost: 48 cents each. DIP and SMD packages, as low as 37 cents a chip. https://www.digikey.com/products/en?keywords=74hc125

\$\endgroup\$
  • 1
    \$\begingroup\$ Apologies, when i used SDA here, i meant more like a 3 wire SPI , where the SDA acts as MOSI and MISO (even though with cases like LCD it's almost always just MOSI). I'll check this one out, thanks! \$\endgroup\$ – Hassan Nasir Jan 10 at 20:47
0
\$\begingroup\$

If both masters are MCUs with plain GPIO, then simply connect /SS to such a port. Set the port as input, read the /SS line before initiating a transmission. If available, switch port to output and drive the line low, then send data. All that's needed is a single external pull-up.

You only need an external switch if one MCU has priority and is allowed to interrupt the other in the middle of transmission. If so, you can use 74HC125 or similar, as suggested in another answer.

\$\endgroup\$
  • 1
    \$\begingroup\$ If I follow, a problem with that might be the fact that two asynchronous MCUs might both sample the line near in time and find they can use shared pins, and then both take over improperly. You may get lucky most of the time. But it isn't robust. \$\endgroup\$ – jonk Jan 8 at 14:05
  • \$\begingroup\$ @jonk That really depends on your timing spec and how fast the GPIO hardware switches from input to output. It is true that a slow toggling GPIO hardware might cause race condition phenomenon. But the same problem exists with an external switch, if both MCUs decide to switch at the same time. \$\endgroup\$ – Lundin Jan 8 at 14:11
  • \$\begingroup\$ But there are solutions that require no added ic which are also fully robust, as well. I may add an answer showing how. If I get time. \$\endgroup\$ – jonk Jan 8 at 14:13
  • \$\begingroup\$ @jonk Given the lack of details, it isn't possible to give much in the way of a detailed answer. MCUs could handshake, you could daisy-chain the SPI etc etc. \$\endgroup\$ – Lundin Jan 8 at 14:16
  • \$\begingroup\$ The solution I'm thinking of can be used by two systems which were not originally designed to share mastering, run on different clocks, and after literally patched together for only temporary cooperation for any shared mastering lines. Had to do it, in fact, several times. Daisy chaining works, if you have the extra pins for it and can accept priority assignment. I couldn't use either. So can be done entirely using existing lines. Just requires careful thinking and coding. \$\endgroup\$ – jonk Jan 8 at 14:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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