Detailed Explanation of SPI, UART, and I2C Buses

Detailed Explanation of SPI, UART, and I2C Buses

Source: Automotive ECU Design

When you connect a microcontroller to sensors, displays, or other modules, have you considered how these two devices communicate with each other? What exactly are they saying?

In fact, communication between electronic devices is like communication between humans; both parties need to speak the same language. In electronic products, these languages are called communication protocols. First, we will start with some basic concepts and then explain the working principle of SPI in detail.

01

Serial vs Parallel

Electronic devices communicate by sending data bits back and forth. Bits are binary and can only be 1 or 0. Through rapid voltage changes, bits are transmitted from one device to another. In systems operating at 5V, “0” is communicated through a short pulse at 0V, while “1” is communicated through a short pulse at 5V.

Data bits can be transmitted in parallel or serial form. In parallel communication, data bits are transmitted simultaneously across multiple lines. The following diagram shows the parallel transmission of the letter “C” in binary (01000011):
Detailed Explanation of SPI, UART, and I2C Buses
In serial communication, bits are sent one by one through a single wire. The following diagram shows the serial transmission of the letter “C” in binary (01000011):

Detailed Explanation of SPI, UART, and I2C Buses

02

SPI Bus

SPI is a common general-purpose communication protocol for devices. It has a unique advantage of allowing uninterrupted data transmission, enabling the continuous sending or receiving of any number of bits. In contrast, I2C and UART send data in packets with a limited number of bits.
In SPI devices, there are master and slave systems. The master is the controlling device (usually a microcontroller), while the slave (typically a sensor, display, or memory chip) receives commands from the master. A complete SPI communication setup includes four signal lines:
MOSI (Master Output/Slave Input) – Signal line, master outputs, slave inputs.
MISO (Master Input/Slave Output) – Signal line, master inputs, slave outputs.
SCLK (Clock) – Clock signal.
SS/CS (Slave Select/Chip Select) – Chip select signal.

Detailed Explanation of SPI, UART, and I2C Buses

SPI Protocol Features

In practice, the number of slaves is limited by the system’s load capacitance, which reduces the master’s ability to switch accurately between voltage levels.

Working Principle

Clock Signal

One bit of data is transmitted per clock cycle, so the speed of data transmission depends on the frequency of the clock signal. The clock signal is generated by the master, so SPI communication is always initiated by the master.

Any communication protocol that shares a clock signal among devices is called synchronous. SPI is a synchronous communication protocol, while some asynchronous communications do not use a clock signal. For example, in UART communication, both sides are set to a pre-configured baud rate, which determines the speed and timing of data transmission.

Chip Select Signal

The master enables communication by pulling the slave’s CS/SS low. In idle/non-transmission state, the chip select line remains high. There can be multiple CS/SS pins on the master, allowing it to communicate with multiple different slaves.

Detailed Explanation of SPI, UART, and I2C Buses

If the master has only one chip select pin available, it can connect these slave devices in the following manner:
Detailed Explanation of SPI, UART, and I2C Buses

MOSI and MISO

The master sends data to the slave via MOSI, and the slave can also send data back to the master via MISO, allowing both to occur simultaneously. Thus, theoretically, SPI is a full-duplex communication protocol.
Transmission Steps
1. The master outputs the clock signal.

Detailed Explanation of SPI, UART, and I2C Buses

2. The master pulls the SS/CS pin low to activate the slave.
Detailed Explanation of SPI, UART, and I2C Buses
3. The master sends data to the slave via MOSI.
Detailed Explanation of SPI, UART, and I2C Buses
4. If a response is needed, the slave sends data back to the master via MISO.
Detailed Explanation of SPI, UART, and I2C Buses

There are some advantages and disadvantages to using SPI, and if choosing between different communication protocols, careful consideration should be given based on project requirements.

Advantages and Disadvantages

Advantages

SPI communication has no start or stop bits, allowing data to flow continuously without interruption; it does not have a complex slave addressing system like I2C, and the data transmission rate is higher than that of I2C (almost twice as fast). Independent MISO and MOSI lines allow for simultaneous data sending and receiving.

Disadvantages

SPI uses four lines (while I2C and UART use two), lacks confirmation of successful signal reception (I2C has this feature), and does not have any form of error checking (such as parity bits in UART).

Detailed Explanation of SPI, UART, and I2C Buses
UART stands for Universal Asynchronous Receiver/Transmitter, also known as serial communication. Unlike communication protocols like SPI and I2C, it is a physical circuit in the microcontroller or an independent IC. The main purpose of UART is to send and receive serial data, and its best advantage is that it only uses two wires to transmit data between devices. The principle of UART is easy to understand, but if you haven’t read about the SPI communication protocol, it might be a good starting point.

03

UART Communication

In UART communication, two UARTs communicate directly with each other. The sending UART converts the parallel data from the controlling device (like a CPU) into serial form and sends it to the receiving UART. Only two wires are needed to transmit data between the two UARTs, with data flowing from the Tx pin of the sending UART to the Rx pin of the receiving UART:
Detailed Explanation of SPI, UART, and I2C Buses
UART is an asynchronous communication, which means there is no clock signal; instead, start and stop bits are added to the data packet. These bits define the start and end of the data packet, so the receiving UART knows when to read this data.

When the receiving UART detects the start bit, it reads the bits in the data frame at a specific baud rate. Baud rate is a measure of the speed of data transmission, expressed in bits per second (bps). The two UARTs must operate at approximately the same baud rate, with the baud rate between the sending and receiving UARTs differing by no more than about 10%.

Detailed Explanation of SPI, UART, and I2C Buses

Working Principle

After the sending UART obtains the parallel data from the data bus, it adds a start bit, a parity bit, and a stop bit to form a data packet, which is then serially output bit by bit from the Tx pin. The receiving UART reads the data packet bit by bit on its Rx pin.

Detailed Explanation of SPI, UART, and I2C Buses

UART data consists of 1 start bit, 5 to 9 data bits (depending on UART), an optional parity bit, and 1 or 2 stop bits:

Detailed Explanation of SPI, UART, and I2C Buses

Start Bit

The UART data transmission line typically remains at a high voltage level when no data is being transmitted. When transmission begins, the sending UART pulls the transmission line from high to low within one clock cycle, and when the receiving UART detects the transition from high to low voltage, it begins reading the bits in the data frame at the baud rate.

Data Frame

The data frame contains the actual data being transmitted. If a parity bit is used, it can be 5 bits, up to 8 bits. If no parity bit is used, the length of the data frame can be 9 bits.

Parity Bit

The parity bit is a way for the receiving UART to determine if any data changes occurred during transmission. After the receiving UART reads the data frame, it counts the number of bits that are 1 and checks whether the total is even or odd, and whether it matches the data.

Stop Bit

To signal the end of the data packet, the sending UART drives the data transmission line from low voltage to high voltage for at least two bit times.

Transmission Steps

1. The sending UART receives parallel data from the data bus:

Detailed Explanation of SPI, UART, and I2C Buses

2. The sending UART adds the start bit, parity bit, and stop bit to the data frame:

Detailed Explanation of SPI, UART, and I2C Buses

3. The entire data packet is serially sent from the sending UART to the receiving UART. The receiving UART samples the data line at the pre-configured baud rate:

Detailed Explanation of SPI, UART, and I2C Buses

4. The receiving UART discards the start bit, parity bit, and stop bit from the data frame:

Detailed Explanation of SPI, UART, and I2C Buses

5. The receiving UART converts the serial data back to parallel data and transmits it to the data bus of the receiving end:

Detailed Explanation of SPI, UART, and I2C Buses

Advantages and Disadvantages

Advantages

  • Only two wires are used.

  • No clock signal is required.

  • Parity bit allows for error checking.

  • As long as both sides set up the structure of the data packet.

  • Well-documented and widely used method.

Disadvantages

  • Maximum size of data frame is 9 bits.

  • Does not support multiple slave systems or multiple master systems.

  • The baud rate of each UART must be within 10% of each other.

04

I2C Bus

I2C bus is a simple, bidirectional, two-wire synchronous serial bus developed by Philips. It requires only two wires to transmit information. It combines the advantages of SPI and UART, allowing multiple slaves to connect to a single master (like SPI), and also enabling multiple masters to control one or more slaves. This is particularly useful when multiple microcontrollers want to log data to a single storage card or display text on a single LCD.

Detailed Explanation of SPI, UART, and I2C Buses

SDA (Serial Data) – Data line.

SCL (Serial Clock) – Clock line.

I2C is a serial communication protocol, so data is transmitted bit by bit along the SDA line. Like SPI, I2C also requires a clock synchronization signal, and the clock is always controlled by the master.

Detailed Explanation of SPI, UART, and I2C Buses

Working Principle

I2C data transmission occurs in the form of multiple messages, each containing the binary address frame of the slave, one or more data frames, along with start conditions, stop conditions, read/write bit, and ACK/NACK bit between data frames:

Detailed Explanation of SPI, UART, and I2C Buses

Start Condition: When SCL is high, SDA switches from high to low.

Stop Condition: When SCL is high, SDA switches from low to high.

Detailed Explanation of SPI, UART, and I2C Buses
Address Frame: A unique 7-bit or 10-bit sequence for each slave device used for address identification between master and slave devices.
Read/Write Bit: A bit that is low if the master is sending data to the slave and high if requesting data.
ACK/NACK: Each frame in the message is followed by an ACK/NACK bit. If the address frame or data frame is successfully received, the receiving device returns an ACK bit to confirm.

Addressing

Since I2C does not have chip select lines like SPI, it requires another method to confirm a specific slave device, which is addressing.

The master sends the address of the slave it wants to communicate with to each slave, and each slave compares it with its own address. If the addresses match, it sends a low-level ACK bit back to the master. If they do not match, no action is taken, and the SDA line remains high.

Read/Write Bit

The end of the address frame contains a read/write bit. If the master is sending data to the slave, it is low. If the master is requesting data from the slave, it is high.

Data Frame

After the master detects the ACK bit from the slave, it can send the first data frame. The data frame is always 8 bits, and each data frame is followed by an ACK/NACK bit to verify the reception status. After sending all data frames, the master can send a stop condition to terminate communication.
Transmission Steps
1. When the SCL line is high, the master starts bus communication by switching the SDA line from high to low.
2. The master sends the 7-bit or 10-bit address of the slave it wants to communicate with, along with the read/write bit:
Detailed Explanation of SPI, UART, and I2C Buses

3. Each slave compares the address sent by the master with its own address. If the addresses match, the slave returns an ACK bit by pulling the SDA line low. If the master’s address does not match the slave’s address, the slave pulls the SDA line high.

Detailed Explanation of SPI, UART, and I2C Buses

4. The master sends or receives data frames:

Detailed Explanation of SPI, UART, and I2C Buses

5. After each data frame is transmitted, the receiving device returns another ACK bit to the sender to confirm that the frame has been successfully received:

Detailed Explanation of SPI, UART, and I2C Buses

6. The master then switches the SCL to high, followed by switching the SDA to high, sending a stop condition to the slave.

Single Master vs Multiple Slaves

Since I2C uses addressing, a single master can control multiple slaves. When using a 7-bit address, up to 128 (2^7) unique addresses can be used. Although using a 10-bit address is not common, it can provide 1,024 (2^10) unique addresses. When connecting multiple slaves to a single master, use a pull-up resistor of 4.7K ohms to connect them, such as connecting the SDA and SCL lines to Vcc:

Detailed Explanation of SPI, UART, and I2C Buses

Multiple Masters vs Multiple Slaves

I2C supports multiple masters connected to multiple slaves at the same time. However, issues can arise when two masters try to send or receive data simultaneously via the SDA line. Therefore, each master needs to check whether the SDA line is low or high before sending a message. If the SDA line is low, it means another master is controlling the bus. If the SDA line is high, it is safe to send data. When connecting multiple masters to multiple slaves, use a pull-up resistor of 4.7K ohms to connect the SDA and SCL lines to Vcc:

Detailed Explanation of SPI, UART, and I2C Buses

Advantages and Disadvantages
Compared to other protocols, I2C may sound complex. Here are some advantages and disadvantages to help you determine whether they meet your project needs:

Advantages

  • Only two wires are used.

  • Supports multiple masters and multiple slaves.

  • ACK/NACK bits are used to confirm that each frame has been successfully transmitted.

  • Hardware is simpler than UART.

  • Well-known and widely used protocol.

Disadvantages

  • Data transmission rate is slower than SPI.

  • Data frame size is limited to 8 bits.

Author Bio: www.cuitbasics.com, translated by eating without brushing teeth
Recommended Reading

Want to switch to autonomous driving? This article tells you how to start.

Safety challenges in vehicle diagnostic systems.

Understanding ASPICE.

Detailed explanation of automotive Bootloader design.

Research on the safety of Tesla Autopilot system | includes dbc download.

Detailed explanation of functional safety concept phase.

In-depth analysis of the high-voltage system in Volkswagen ID.4.

Battery management system of Tesla Model 3.

Sharing is not easy, please click 【see again】

Leave a Comment