Site icon Renzo Mischianti

i2c, SPI and UART compared

Spread the love

i2c vs SPI vs UART comparison speed distance complexity

I2C, SPI, and UART are protocols that we encounter all the time in this world, but why do we need all these protocols to interconnect external peripherals? Well! The answer is simple because there are many needs to be met, and each protocol has a well-defined scope.
In this article, we will make a summary comparison always to be able to select the correct protocol.

UART

universal asynchronous receiver transmitter (UART) is a computer hardware device for asynchronous serial communication in which the data format and transmission speeds are configurable. It sends data bits one by one, from the least significant to the most significant, framed by start and stops bits so that the communication channel handles precise timing. A driver circuit external to the UART handles the electric signaling levels.

Speed

The data rate of a UART interface is similar to that of an I2C interface. UART interfaces have a maximum data rate of around 5 Mbps. There is also some protocol overhead in the form of start, stop, and parity bits.

Interface

The polarity of the signals is +5 V for mark or high and 0 V for space or low. However, serial data transmission along the wire in an RS232 transmission interface requires −15 to −12 V for mark and +12 to +15 V for space. Line drivers are used to convert the logic levels required by the UART to those needed for the RS232 interface pins TD and RD.

TTL – Transistor-Transistor Logic is not a protocol. It’s an older technology for digital logic, but the name is often used to refer to the 5 V supply voltage, usually incorrectly referring to what should be called UART.

UART wiring one master, one slave

It can operate between devices in 3 ways:

Distance

A UART can work over long distances with appropriate line drivers: from 15 meters (m) for the RS-232 serial data bus to 1000m for RS-485 or RS-422 interfaces. The distance is also influenced by cable quality and baud rate.

Communication

UART communication example

Packet

UART data packet

Summary

Advantages

Disadvantages

I2C

I2C (Inter-Integrated Circuiteye-squared-C) is alternatively known as I2C or IIC. It is a synchronous, multi-master, multi-slave, packet-switched, single-ended, serial communication bus. Invented in 1982 by Philips Semiconductors. It is widely used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication. (cit. WiKi)

Speed

I2C supports 100 kbps, 400 kbps, and 3.4 Mbps. Some variants also support 10 Kbps and 1 Mbps.

ModeMaximum
speed
Maximum
capacitance
DriveDirection
Standard-mode (Sm)100 kbit/s400 pFOpen drainBidirectional
Fast-mode (Fm)400 kbit/s400 pFOpen drainBidirectional
Fast-mode Plus (Fm+)1 Mbit/s550 pFOpen drainBidirectional
High-speed mode (Hs)1.7 Mbit/s400 pFOpen drainBidirectional
High-speed mode (Hs)3.4 Mbit/s100 pFOpen drainBidirectional
Ultra Fast-mode (UFm)5 Mbit/sPush-pullUnidirectional

Interface

Like UART communication, I2C only uses two wires to transmit data between devices:

I2C is a serial communication protocol, so data is transferred bit by bit along a single wire (the SDA line).

Like SPI, I2C is synchronous, so the output of bits is synchronized to the sampling of bits by a clock signal shared between the master and the slave. The master always controls the clock signal.

There will be multiple slaves and multiple masters, and all masters can communicate with all the slaves.

i2c data packet

Devices connections

i2c wiring one master one slave

Because I2C uses addressing, multiple slaves can be controlled by a single master. Up to 27 slave devices can be connected/addressed in the I2C interface circuit. With a 7-bit address, 128 (27) unique addresses are available. Using 10-bit addresses is uncommon but provides 1,024 (210) unique addresses.

i2c wiring one master multiple slaves

Multiple masters can be connected to a single slave or multiple slaves. The problem with multiple masters in the same system comes when two masters try to send or receive data simultaneously over the SDA line. Each master needs to detect if the SDA line is low or high before transmitting a message to solve this problem. If the SDA line is low, this means that another master has control of the bus, and the master should wait to send the message. If the SDA line is high, then it’s safe to transmit the message. To connect multiple masters to multiple slaves

i2c wiring multiple masters multiple slaves

Distance

The vast majority of applications use I2C in the way it was designed initially—peripheral ICs directly wired to a processor on the same printed circuit board, and therefore over relatively short distances of less than 1 foot (30 cm) without a connector. However, using a different driver, an alternate version of I2C can communicate up to 20 meters (possibly over 100 meters) over CAT5 or other cables.

Summary

Advantages

Disadvantages

SPI

The Serial Peripheral Interface (SPI) is a synchronous serial communication interface specification used for short-distance communication, primarily in embedded systems. SPI devices communicate in full-duplex mode using a master-slave architecture with a single master. The master device originates the frame for reading and writing. Multiple slave-devices are supported through selection with individual slave select (SS), sometimes called chip select (CS), lines.

Speed

The maximum data rate limit is not specified in the SPI interface. Usually supports about 10 Mbps to 20 Mbps.

There is no standard set of speeds defined for the SPI protocol. Typical bus speeds are in the 50 MHz range. Since there is no overhead added by the protocol, such as addressing and flow control, the throughput that can be achieved using SPI mirrors the clock frequency. For a 50 MHz SPI line, the throughput is 50 Mbps. One more thing to remember is that the communication is full-duplex. So, the effective throughput in the case mentioned can be as high as 100 Mbps (if both the master and slave can send meaningful data to each other).

Interface

The SPI bus specifies four logic signals:

Slave Select has the same functionality as chip select and is used instead of an addressing concept. MOSI on a master connects to MOSI on a slave. MISO on a master connects to MISO on a slave.

Note: on a slave-only device, MOSI may be labeled as SDI (Serial Data In), and MISO may be labeled as SDO (Serial Data Out)

The signal names above can be used to label both the master and slave device pins as well as the signal lines between them in an unambiguous way and are the most common in modern products. Pin names are always capitalized e.g. “Slave Select,” not “slave select.”

Older products can have nonstandard SPI pin names:

Each company or manufacturer has its own specific protocols to communicate with peripherals. Hence one needs to read the datasheet to know the read/write protocol for SPI communication to be established. For example, we would like SPI communication between the microcontroller and EPROM. Here one needs to go through the read/write operational diagram in the EPROM datasheet.

Communication

SPI wiring one master, one slave

Each slave requires a separate enable signal in a multi-slave system, which is slightly more complicated on hardware than the I2C system.

SPI wiring one master multiple slaves

The SPI interface is actually two simple shift registers in the internal hardware. It is transmitted bit by bit under the slave enable signal and shift pulse generated by the master device. The transmitted data is 8 bits. The high bit is in the front, and the low bit is in the back.

The SPI interface is synchronous serial data transmission between the CPU and the low-speed peripheral device. Under the shift pulse of the master device, the data is transmitted bit by bit. The high bit is in the front, and the low bit is in the back. It is full-duplex communication, and the data transmission speed is overall faster than the I2C bus that can reach speeds of a few Mbps.

Distance

While the SPI communication method is generally suitable for distances up to approximately 10m can bridge longer distances, a repeater is often needed because of attenuation due to the increased line resistance of long cables.

Summary

Advantages

Disadvantages

Comparison table

UARTI2CSPI
Full nameUniversal Asynchronous Receiver-TransmitterInter-Integrated CircuitSerial Peripheral Interface
ComplexityVery lowLow with a lot of devices alsoComplex wiring and management with a lot of devices
Min wires224
DuplexFull-duplexHalf-duplexFull-Duplex
Type of communicationAsynchronousSynchronousSynchronous
Slave number1128 with default 7-bit address. By default, up to 27 slave devices can be connected/addressed in the I2C interface circuit.Limited by the available SS pins
Master numberNot ApplicationOne or more1
Distancefrom 15 to 1000mfrom 20 to 100mmore than 10m
Speedmax data rate around 5Mbpsmax data rate around 3.4Mbps to 5Mbpsdefault max data rate around 50Mbps but with particular condition 100Mbps

Conclusion

For me, there isn’t a winner, and all protocols have Their scope. UART can be used for long distances and when you want a simple protocol implementation. i2c can be used to connect many low-speed devices with simple hardware connections. SPI can be used with some high-speed devices.

Thanks


Spread the love
Exit mobile version