11

(I am mainly from a software background and just started to work on an embedded platform. Excuse me if this is a naive question...)

I am planning to write a driver for an EEPROM which is connected to the MCU via an I2C bus. So I am reading a tutorial about the I2C bus.

The tutorial says:

Both SCL and SDA lines are "open drain" drivers. What this means is that the chip can drive its output low, but it cannot drive it high. For the line to be able to go high you must provide pull-up resistors to the 5v supply. There should be a resistor from the SCL line to the 5v line and another from the SDA line to the 5v line. You only need one set of pull-up resistors for the whole I2C bus, not for each device, as illustrated below:

Picture of three devices plus MCU on I2C bus with pull-up resistors on SCL and SDA

The red part in the above image is added by me. I guess that's how the whole picture is.

My questions are:

  • Is my adding of the red MCU correct?
  • The bold part the chip is the red MCU, right?
  • Why is it true that the chip cannot drive it high? Is it unable or forbidden to?
  • What does open drain mean in plain English?

I searched a bit but seems cannot find a clear explanation...maybe an analogy will help.

smwikipedia
  • 1,192
  • 2
  • 18
  • 35
  • 6
    Cannot means "must not" here, as in: The I²C spec forbids that. – Turbo J Jun 27 '16 at 06:55
  • 5
    And almost any I2C interface will also be unable to, because why would they bother making the chip able to do that when it's not allowed to anyway? – user253751 Jun 27 '16 at 10:22
  • @immibis Most microcontrollers, actually.... some have all kinds of configurable drive options, and it's not guaranteed to be configured correctly just because the I2C peripheral is enabled -- you need to take care to make sure the pin is configured properly – Daniel Jun 27 '16 at 16:21
  • See open collector for an explanation of the terminology. (A bus of this type can be driven either by a bipolar transistor or a field-effect transitor; the terminal of the transistor that's connected to the bus is called the "collector" for a BJT and the "drain" for a FET, but from the bus point of view it's the same thing). – hmakholm left over Monica Jun 27 '16 at 18:49
  • @Daniel: If an I2C bus has more than one slave hard-connected to SDA, then it may be possible for the slaves to get into a state where communication will be impossible unless or until the devices are forcibly reset or the CPU is able to raise the state of SDA on one despite the other device trying to pull it down. – supercat Jun 27 '16 at 20:41
  • @supercat Uh, technically yes.. but.. that's a pretty unusual condition, yeah? I2C is by design a multi-slave bussed protocol! – Daniel Jun 27 '16 at 21:15
  • @Daniel: I don't know in what scenarios would have that as a plausible failure mode, but a lot of I2C systems use internal pull-ups rather than physical resistors, and if the master gets asynchronously reset while the bus is being pulled up, that could leave the bus state in limbo until the master comes out of reset. – supercat Jun 27 '16 at 21:21

4 Answers4

19

Is my adding of the red MCU correct?

Not exactly. The MCU is just another member of the bus like the rest of the Devices. There is nothing particularly special about how the MCU operates on the I2C bus. The MCU can recognize when either line is high or low, and the MCU can pull on either (or both) of the lines itself in order to communicate with all the other devices on the bus.

The bold part the chip is the red MCU, right?

It implies that the MCU is somehow different than the other devices on the I2C bus. But in reality, it is NOT special.

Why the chip cannot drive it high?

If you understood the nature of passive pullup, open-drain busses, you would understand that is not a proper question. You don't WANT any of the devices to drive it high because that would completely preclude any communication on the bus. The devices can only communicate if they can pull the bus LOW so that all the other devices on the bus can recognize that activity is taking place.

What does the open drain mean in plain English?

It is just like the signaling system used on many pubic transportation vehicles. There is a long "rope" along the length of the car, and ANYONE along the line can pull down on the "rope" to signal that they want to get off at the next stop. If you fixed the "rope" so that it was always HIGH, then nobody could pull on it to signal a stop.

Richard Crowley
  • 14,460
  • 2
  • 21
  • 37
  • 5
    "It implies that the MCU is somehow different than the other devices on the I2C bus. But in reality, it is NOT special." When you're talking about just the hardware, sure.. but this is probably too confusing because the next conversation is about the protocol and who is mastering the bus, which DOES make the MCU special. – Daniel Jun 27 '16 at 16:19
  • 1
    @Daniel. Yes, I agree completely. But the question appears to be about the OSI Layer-1: Physical Layer. And at that level, the MCU is an equal player with all the other devices on the bus. And remember that the bus master is NOT ALWAYS the MCU. Master status can be transferred to other devices where necessary. – Richard Crowley Jun 27 '16 at 16:38
  • 1
    Ah the infamous multi-master... difficult to design, hard to debug! ;) – Daniel Jun 27 '16 at 17:01
  • The CPU is special, because it's the bus master, and if nobody is capable of pulling SDA high when any device is pulling it low, then a glitch which causes a slave device to get out of sync could leave the bus in a state where nothing would be able to communicate again (e.g. if two EEPROM devices are both full of zeroes, but a glitch causes one to see an extra clock pulse while the master is sending out the read command, then each device will release SDA for one out of nine cycles, but they'll never both release it simultaneously. – supercat Jun 27 '16 at 20:59
  • The CPU is not always the bus master. Any device on the bus can be the master. So the CPU is NOT special in that regard. 2) The question appears to be asking about the PHYSICAL LAYER, the passively pulled-up bus with the open-source devices. Again, in that regard the CPU is exactly like every other device on the bus. If we think the quesiton is about the Data Link Layer or the Network Layer then we need @smwikipedia to return and clarify the question. I am refering to the 7-layer model: https://en.wikipedia.org/wiki/OSI_model
  • – Richard Crowley Jun 27 '16 at 21:08