Consider a scenario where two (or more) master devices (the NodeMCUs, in this case) share an I2C bus with one (or more) slave device(s), like thus:
Would this be a feasible setup? In other words, could both masters request (not simultaneously) from the slave?
-
4Only if you can implement MultiMaster in the ESP8266's (software-based) I2C library.Majenko– Majenko2019年07月26日 14:16:29 +00:00Commented Jul 26, 2019 at 14:16
-
care to elaborate an answer? :)tony gil– tony gil2019年07月26日 14:17:21 +00:00Commented Jul 26, 2019 at 14:17
2 Answers 2
The situation you describe is called MultiMaster. It is normally done in hardware using the I2C peripheral in the MCU. However the ESP8266 doesn't have one (or if it does, it's not used), and uses bit-banging to implement I2C in software. If you can add MultiMaster functionality to the ESP8266's Wire library then possibly yes, you could do it.
Another option is to have some other communication channel between the two masters (it could be as simple as a wire with a pullup resistor - see below) so that only one master communicates at a time.
Electrically there is no problem with your setup - I2C is a fully open-drain system (or should be implemented as such - hence the pullup resistors needed on the SCL and SDA pins), so multiple devices can drive the lines at the same time. It's just that if they do you will get data corruption and communication won't happen.
Example: Semaphore Wire Implementation
- Connect any two GPIO pins together with a wire. Add a pullup resistor to the wire (10kΩ should suffice).
- Both GPIO pins are set to INPUT.
When a master wants to send it first reads the GPIO pin:
- If it is HIGH then it is "clear to send". So it sets the GPIO to OUTPUT and pulls the line LOW.
- If it is LOW then it waits for a random amount of time and tries again.
Once communication is finished the master sets the GPIO pin to INPUT again.
-
would strict scheduling provide a workaround?tony gil– tony gil2019年07月26日 14:36:12 +00:00Commented Jul 26, 2019 at 14:36
-
1@tonygil Only if you have zero clock drift... Simpler to have a semaphore signal. I have added more detail to my answer.Majenko– Majenko2019年07月26日 14:37:10 +00:00Commented Jul 26, 2019 at 14:37
-
1Outstanding answer. (Voted). What (if any) Arduino boards have the hardware needed to support MultiMaster? And what library/libraries do you need to us in order to take advantage of it?Duncan C– Duncan C2019年07月26日 15:29:08 +00:00Commented Jul 26, 2019 at 15:29
-
1@DuncanC The clock signal is only there when the master is communicating. Whichever master is communicating is the one that controls both the IO lines at any moment in time.Majenko– Majenko2019年07月26日 15:48:37 +00:00Commented Jul 26, 2019 at 15:48
-
1No, that would be a pull-down. You want a pull-up. Connect it to +3.3V.Majenko– Majenko2019年07月29日 18:11:29 +00:00Commented Jul 29, 2019 at 18:11
Changed diagram after comments and accepted answer:
-
1Yes, except that the upper and lower ground rails on the breadboard aren't connected except via the internals of the Arduino.Majenko– Majenko2019年07月30日 09:09:43 +00:00Commented Jul 30, 2019 at 9:09
-
adapted to reflect your comment abovetony gil– tony gil2019年07月30日 23:29:54 +00:00Commented Jul 30, 2019 at 23:29
Explore related questions
See similar questions with these tags.