19
\$\begingroup\$

The I²C protocol allows, in theory and with 7-bit addressing, up to 127 devices to be connected to the master. This is a large number, so why would any low-cost microcontroller (e.g. this PIC24), have more than one I²C port? Why is it needed?

\$\endgroup\$
23
\$\begingroup\$

Sensor hub arrangement

In this scenario, there are two I²C buses. Let's call them local bus and main bus. The purpose of the local bus is to connect a bunch of sensors to a microcontroller (μC). The purpose of the μC is to poll the sensors, aggregate information from them, and detect certain events. A μC in such role is called sensor hub. The sensor hub is not responsible for higher order functions; there is a powerful main processor for that. The main bus connects the sensor hub to the main processor. So, the sensor hub μC is a master on the local I²C bus and a slave on the I²C main bus.

SPI and I²C

The PIC linked in the original post doesn't share the pins between SPI and I²C. However, there are other PICs that use the same pins for hardware SPI and I²C, because both are implemented with the same MSSP peripheral. If a PIC has two separate MSSP peripherals, then one can be used for hardware SPI, while the other one is used for hardware I²C.

\$\endgroup\$
21
\$\begingroup\$

A very common reason to need more than one bus is having devices that run at different speeds. Originally, I²C ran at at a maximum 100 kHz. Later, the speed was increased to a maximum of 400 kHz, and still later, to 1 MHz and above.

The gotcha is, since the address of each device is embedded in the I²C protocol, then if you have devices with different speed ratings on the same bus, say 100 kHz and 400 kHz, you always have to run the bus at the lowest speed common to all devices on the same bus (100 kHz in this case).

If you ran the bus at the higher speed (400 kHz), obviously the lower-speed device would not work properly, and it might even interpret the address of the high-speed device as its own, causing the 400 kHz device to fail as well. But even if you initially ran the bus at 100 kHz, and then tried to speed up the bus to 400 kHz after addressing a higher-speed chip, it would be possible (although probably unlikely) for the lower-speed chip to interpret one of the high-speed data packets incorrectly as its address, and thus mess up the communication on the bus. In either case, at the end of the interchange with the 400 kHz device, the 100 kHz device would probably be in an unknown state.

So it is most efficient, if you have devices running at different speeds and you have multiple I²C ports and you have the spare pins to allow for such luxury, to have one I²C say for 100 kHz devices, another for 400 kHz devices, and another for 1 MHz devices, as your needs may dictate.

This is not an issue with SPI because each device is enabled (addressed) in hardware by a separate chip select line. So the clock speed can be matched to the speed of the selected chip (10 MHz, 20 MHz, whatever) without having any effect on other chips on the same bus, since they are not enabled.

\$\endgroup\$
19
\$\begingroup\$

Also it could let you support two devices with the same address. Yes most devices let you select maybe the bottom two bits of their address with straps. Recently I had to support 4 devices who each only allowed you to set the LSB of their address with a resistor. Having two ports means no extra cost for me.

Maybe I want one to be the master for a bunch of devices and present the other as the slave port, so my master doesn't have to wait to grab the bus to give me a command while I poll a temperature sensor for the 10,000th time.

There seem to be a bunch of other good responses in this thread, just adding my 2 cents.

\$\endgroup\$
9
\$\begingroup\$

Speed.

If you should need to access two I2C devices at the same time, you could have a significant conflict. Or, one would have to wait for the other.

\$\endgroup\$
9
\$\begingroup\$

Second, I²C can be used to get more bandwidth (you can receive or send two bytes at the same time).

If you have some ADC that works continuously, one I²C is busy all the time.

Switching address takes some time too, so in some cases - you can use two ports for two devices and avoid address changes to save time.

\$\endgroup\$
0
\$\begingroup\$

Others mentioned many reasons to do this, I will add one:

You got a bunch of 5V I2C devices, and a bunch of 3V3 I2C devices.

For example I have a 3.3V micro which has two I2C ports, one 5V tolerant, the other 3V3 only.

\$\endgroup\$
  • \$\begingroup\$ Out of curiosity, which micro is it? \$\endgroup\$ – Nick Alexeev Mar 17 '17 at 21:29
  • 1
    \$\begingroup\$ LPC4330, here's excerpt from manual: " Open-drain 5 V tolerant digital I/O pad, compatible with I2C-bus Fast Mode Plus specification. This pad requires an external pull-up to provide output functionality. When power is switched off, this pin connected to the I2C-bus is floating and does not disturb the I2C lines." \$\endgroup\$ – peufeu Mar 18 '17 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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