Understanding UART, I2C, SPI, and Other Communication Protocols

UART, I2C, RS485… These communication protocols are commonly used, but their explanations can be ambiguous. I feel it’s necessary to organize them to consolidate and differentiate. The article is a bit long, so I suggest saving it for later reference.

UART stands for Universal Asynchronous Receiver-Transmitter.

The UART port refers to a type of physical interface (hardware).

Understanding UART, I2C, SPI, and Other Communication Protocols

UART is an asynchronous, full-duplex serial bus. It is much more complex than synchronous serial ports. There are two lines, one TXD for transmitting and one RXD for receiving.

UART’s serial data transmission does not require a clock signal for synchronization; instead, it relies on a predefined configuration between the transmitting and receiving devices.

For the sending and receiving devices, their serial communication configurations should be set to be exactly the same.

Understanding UART, I2C, SPI, and Other Communication Protocols

Start bit: Indicates the beginning of data transmission, with a logic level of “0”.

Data bits: Possible values are 5, 6, 7, 8, or 9, indicating the transmission of these bits of data. The common value is 8, as one ASCII character is 8 bits.

Parity bit: Used by the receiving end to check the received data. The number of “1” bits can be even (even parity) or odd (odd parity) to verify the correctness of data transmission; this bit can also be omitted when in use.

Stop bit: Indicates the end of a data frame. The logic level is “1”.

If simulating a UART bus with general IO ports, one input port and one output port are required.

I2C Bus

The I2C bus is a synchronous, half-duplex two-wire serial bus. It consists of two lines: the serial clock line (SCL) and the serial data line (SDA).

SCL line – responsible for generating synchronous clock pulses.

SDA line – responsible for transmitting serial data between devices.

This bus can connect multiple I2C devices to the system. Devices connected to the I2C bus can act as either master or slave devices.

Understanding UART, I2C, SPI, and Other Communication Protocols

The master device controls the communication by initializing data transmission, sending data, and generating the required synchronous clock pulses. The slave device waits for commands from the master and responds to the received commands.

Both master and slave devices can act as sending or receiving devices. Regardless of whether the master is sending or receiving, the synchronous clock signal can only be generated by the master device.

If simulating an I2C bus with general IO ports for bidirectional transmission, one input-output port (SDA) and one output port (SCL) are required.

SPI Serial Peripheral Interface

The SPI bus is a synchronous, full-duplex four-wire serial interface bus. It consists of a system of “one master device + multiple slave devices.”

In the system, as long as only one master device is active at any given time, multiple SPI master devices can exist. It is commonly used for communication between ADCs, EEPROMs, FLASH, real-time clocks, digital signal processors, and digital signal decoders.

To achieve communication, SPI has four signal lines:

Understanding UART, I2C, SPI, and Other Communication Protocols(1) Master Out Slave In (MOSI): The signal line for transmitting data from the master device to the slave device, also known as Slave Input (SI/SDI).

(2) Master In Slave Out (MISO): The signal line for transmitting data from the slave device to the master device, also known as Slave Output (SO/SDO).

(3) Serial Clock (SCLK): The signal line for transmitting the clock signal.

(4) Slave Select (SS): The signal line for selecting the slave device, active at low level.

The working timing mode of SPI is determined by the phase relationship between CPOL (Clock Polarity) and CPHA (Clock Phase). CPOL indicates the initial level state of the clock signal; CPOL = 0 means the initial state is low, while CPOL = 1 means the initial state is high. CPHA indicates on which clock edge data is sampled; CPHA = 0 means data is sampled on the first clock transition, while CPHA = 1 means data is sampled on the second clock transition.

Understanding UART, I2C, SPI, and Other Communication Protocols

Comparison of UART, SPI, and I2C

1. I2C has fewer lines and is more powerful than UART and SPI, but it is technically more complicated, as I2C requires support for bidirectional IO and uses pull-up resistors, which have weaker anti-interference capability. It is generally used for communication between chips on the same board and less often for long-distance communication.

2. SPI is simpler to implement, while UART requires a fixed baud rate, meaning the time interval between two bits of data must be equal, whereas SPI does not have this limitation due to its clocked protocol.

3. I2C is slightly slower than SPI, and its protocol is more complex, but it requires fewer connections than standard SPI.

4. A frame in UART can transmit 5/6/7/8 bits, while I2C must be 8 bits. Both I2C and SPI start transmission from the highest bit.

5. SPI uses chip select signals to select slaves, while I2C uses address selection.

Understanding UART, I2C, SPI, and Other Communication Protocols

RS232 Serial Communication

There are two transmission lines and one ground line. The levels are negative logic:

-3V to -15V represents logic “1”, +3V to +15V represents logic “0”.

RS-232 serial communication can transmit distances of about 15 meters. It can achieve bidirectional transmission and full-duplex communication, with a transmission rate of up to 20kbps.

The following figure shows the definition of the DB9 male and female connectors, with the most commonly used signals being RXD, TXD, and GND.

Understanding UART, I2C, SPI, and Other Communication Protocols

TTL and RS-232 Conversion

Microcontroller interfaces generally use TTL levels. If connecting to RS-232 level peripherals, a TTL to RS232 conversion module is required. The MAX232 chip can be used for conversion as shown below.

Understanding UART, I2C, SPI, and Other Communication Protocols

RS422 Serial Communication

RS-422 has four signal lines: two for transmitting, two for receiving, and one ground line. It supports full-duplex communication.

It has one master device, and the rest are slave devices. Slave devices cannot communicate with each other, so RS-422 supports point-to-multipoint bidirectional communication.

Understanding UART, I2C, SPI, and Other Communication Protocols

RS485 Serial Communication

RS-485 uses balanced transmission and differential reception, thus having the ability to suppress common-mode interference.

It uses two-wire half-duplex transmission, with a maximum rate of 10Mb/s. The level logic is determined by the voltage difference between the two wires, enhancing anti-interference capability and allowing long transmission distances (from tens to thousands of meters).

+2V to +6V represents logic “1”, -2V to -6V represents logic “0”.

TTL to RS-485 conversion is common, such as using the MAX485 chip, with the reference circuit shown below.

Understanding UART, I2C, SPI, and Other Communication Protocols

RE pin: Receiver output enable (active low).

DE pin: Transmitter output enable (active high). This can be directly controlled through the MCU’s IO port.

TTL

In embedded systems, the serial port generally refers to the UART port. It has four pins (Vcc, GND, RX, TX), using TTL levels.

The COM port in a PC refers to the serial communication port, commonly known as the serial port. It has nine pins and uses RS232 levels.

Understanding UART, I2C, SPI, and Other Communication Protocols

The serial port and COM port refer to the physical interface (hardware), while TTL, RS-232, and RS-485 refer to the level standards (electrical signals).

Understanding UART, I2C, SPI, and Other Communication Protocols

The schematic diagram of communication between a microcontroller and a PC is shown below:

Understanding UART, I2C, SPI, and Other Communication Protocols

CAN Bus

CAN stands for Controller Area Network, a serial communication network capable of achieving distributed real-time control. The CAN bus is complex and intelligent, primarily used for automotive communication.

The CAN bus network mainly connects at CAN_H and CAN_L, with various nodes implementing differential serial transmission through these two lines. To avoid signal reflection and interference, a 120-ohm terminating resistor is required between CAN_H and CAN_L.

Understanding UART, I2C, SPI, and Other Communication ProtocolsEach device can act as either a master or a slave. The communication distance of the CAN bus can reach 10 kilometers (with a speed below 5Kbps), and speeds can reach 1Mbps (with a communication distance less than 40m).

Understanding UART, I2C, SPI, and Other Communication Protocols

CAN Level Logic

The CAN bus uses the “AND” rule for bus arbitration, where 1&0 equals 0, hence 0 is called dominant and 1 is recessive.

From a potential perspective, high potential is defined as 0 and low potential as 1. When signals are sent simultaneously, the actual presentation is high potential, making it appear that 0 covers 1, hence 0 is dominant and 1 is recessive.

Understanding UART, I2C, SPI, and Other Communication Protocols

USB Communication Serial Bus

The USB interface has at least four wires, of which two are data lines. All USB data transmission is accomplished through these two lines. Its communication is far more complex than that of serial ports.

The two data lines use differential transmission, meaning two data lines must work together to transmit one bit, resulting in half-duplex communication, where only one can send or receive at a time.

USB specifies that if the voltage level remains unchanged, it represents logic 1; if the voltage level changes, it represents logic 0.

Understanding UART, I2C, SPI, and Other Communication ProtocolsUSB to TTL

USB to serial conversion typically uses the CH340G chip.

Understanding UART, I2C, SPI, and Other Communication Protocols

Serial communication is simpler than USB because serial communication does not have protocols.

SD Card

The SD card is a type of storage card that can be used in mobile phones as a memory card.

In embedded systems, there are two modes for microcontroller communication with SD cards:

1. SPI bus communication mode;

2. SD bus communication mode.

Understanding UART, I2C, SPI, and Other Communication Protocols

It is worth noting that the SD bus mode has four data lines; the SPI bus mode has only one data line (MOSI and MISO cannot read data simultaneously or write data simultaneously). Thus, when microcontrollers communicate with SD cards in embedded systems, the SD bus mode is several times faster than the SPI bus mode.

Understanding UART, I2C, SPI, and Other Communication Protocols

1-WIRE Bus

1-Wire was introduced by Dallas Semiconductor, and it is an asynchronous half-duplex serial transmission method. It uses a single signal line to transmit both clock and data, and the data transmission is bidirectional.

Understanding UART, I2C, SPI, and Other Communication Protocols

The data transmission rate of the single wire is generally 16.3Kbit/s, with a maximum of 142 Kbit/s, typically transmitting data at rates below 100Kbit/s.

The 1-Wire line port is usually an open-drain or tri-state gate port, so pull-up resistors (Rp) are generally needed, usually selecting 5K~10KΩ.

Main applications include: identification of printer cartridges or medical consumables; identification and authentication of printed circuit boards, components, and peripherals.

DMA Direct Memory Access

DMA is a hardware module within STM32 that operates independently of the CPU, transferring data between peripheral devices and memory, freeing up the CPU and significantly improving its efficiency.

Understanding UART, I2C, SPI, and Other Communication Protocols

It can access peripherals and memory at high speed, with transfers not controlled by the CPU, and it supports bidirectional communication. Therefore, using DMA can greatly enhance data transfer speeds, which is a highlight of the ARM architecture – DMA bus control.

DMA is akin to a high-speed highway, characterized by its exclusivity and speed. If DMA is not used, the goal can still be achieved, but the time taken will be longer.

Ethernet

Ethernet is currently the most widely used LAN technology.

As you know, Ethernet interfaces can be divided into protocol and physical layers.

The protocol layer is implemented by a single module called the MAC (Media Access Layer) controller.

The physical layer consists of two parts: PHY (Physical Layer) and the transceiver.

Currently, many motherboard southbridge chips include Ethernet MAC control functionality but do not provide physical layer interfaces. Therefore, an external PHY chip is needed to provide access to Ethernet.

Understanding UART, I2C, SPI, and Other Communication Protocols

The role of the network transformer is:

To couple differential signals and enhance anti-interference capability;

To isolate different voltage levels of different devices at the network end and to isolate DC signals.

Ethernet Interface Reference Circuit

Understanding UART, I2C, SPI, and Other Communication Protocols

Recommended to follow the public account below to learn more about electronic technology knowledge!

Understanding UART, I2C, SPI, and Other Communication Protocols
  • Do you call this broken thing a CPU?

  • The multimeter burned out, disassemble, disassemble, disassemble, and the cow dung chip exploded and was replaced with a hard seal, and it was actually repaired by me!

  • Science popularization | Understand in 30 seconds, the world’s most powerful chip company!

  • Hardware reliability and improvement

  • SVPWM algorithm principles and details

Leave a Comment