Understanding UART, I2C, SPI, and Other Communication Protocols

Understanding UART, I2C, SPI, and Other Communication Protocols

Understanding UART, I2C, SPI, and Other Communication Protocols

In microcontroller development, protocols such as UART, I2C, and RS485 are commonly used, and understanding them can be somewhat ambiguous. This article organizes these concepts. The reading time is approximately 10 minutes.

UART 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 wires: one TXD for transmission and one RXD for reception.

UART’s serial data transmission does not require a clock signal for synchronization; instead, it relies on predefined configurations between the sending and receiving devices, as shown in the STM32 example: pressing a button sends data through the serial port.

For the sending and receiving devices, both must have their serial communication configurations set to be identical.

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 number of bits transmitted. Typically, it is set to 8, as one ASCII character value is 8 bits.

Parity bit: Used by the receiver to check the received data. The number of “1” bits must be even (even parity) or odd (odd parity) to verify the correctness of data transmission; this bit is optional.

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

If simulating a UART bus using general I/O ports, one input port and one output port are required.

I2C Bus

The I2C bus is a synchronous, half-duplex, bidirectional 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 is responsible for controlling communication by initializing data transmission and generating the required synchronous clock pulses. The slave device waits for commands from the master and responds to receive commands.

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

If simulating an I2C bus using general I/O 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, bidirectional four-wire serial interface bus. It consists of a system of “one master device + multiple slave devices.”

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

Understanding UART, I2C, SPI, and Other Communication Protocols

To achieve communication, SPI has four signal lines:

  • 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).

  • 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).

  • Serial Clock (SCLK): The signal line for transmitting clock signals.

  • Slave Select (SS): The signal line used to select the slave device, active low.

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 status of the clock signal; CPOL = 0 indicates the initial state is low, and CPOL = 1 indicates the initial state is high. CPHA indicates which clock edge samples the data; CPHA = 0 indicates sampling on the first clock edge, while CPHA = 1 indicates sampling on the second clock edge.

Understanding UART, I2C, SPI, and Other Communication Protocols

Comparison of UART, SPI, and I2C

  • I2C uses fewer lines and is more powerful than UART and SPI, but it is technically more complicated because I2C requires bidirectional I/O support and uses pull-up resistors, making it less resistant to interference. It is generally used for communication between chips on the same board and less often for long-distance communication.

  • SPI is simpler to implement; UART requires a fixed baud rate, meaning the interval between two bits of data must be equal, while SPI does not have this restriction because it is a clocked protocol.

  • I2C is slightly slower than SPI, the protocol is more complex than SPI, but it requires fewer connections than standard SPI.

  • UART can transmit 5/6/7/8 bits in one frame, while I2C must be 8 bits. Both I2C and SPI transmit from the most significant bit.

  • SPI uses chip select signals to choose slaves, while I2C uses addresses to select slaves.

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 is logic “1”, +3V to +15V is logic “0”.

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

The following diagram shows the definitions 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

The microcontroller interface generally uses TTL levels. If connecting to an RS232 level device, a TTL to RS232 conversion module is required. The following diagram shows that the MAX232 chip can be used for conversion.

Understanding UART, I2C, SPI, and Other Communication Protocols

RS422 Serial Communication

RS-422 has four signal lines: two for transmission, two for reception, and one ground line, allowing for full-duplex communication.

There is 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, providing the ability to suppress common-mode interference.

It uses two-wire half-duplex transmission, with a maximum rate of 10 Mb/s. The logic levels are determined by the voltage difference between the two wires, improving resistance to interference and allowing for long transmission distances (from dozens of meters to over a thousand meters).

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

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

Understanding UART, I2C, SPI, and Other Communication Protocols

RE pin: Receiver output enable (active low).

DE pin: Transmitter output enable (active high). It can be directly controlled through the MCU’s I/O port.

TTL

In embedded systems, the term serial port generally refers to the UART port. It has 4 pins (Vcc, GND, RX, TX) and uses TTL levels.

The COM port in a PC refers to the serial communication port, also known as the serial port. It has 9 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 voltage 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, which is a serial communication network capable of distributed real-time control. The CAN bus is complex and intelligent, primarily used for automotive communication.

The CAN bus network is mainly connected via CAN_H and CAN_L, with each node achieving serial differential signal transmission through these two lines. To avoid signal reflection and interference, a 120-ohm termination resistor should be connected between CAN_H and CAN_L.

Understanding UART, I2C, SPI, and Other Communication Protocols

Each 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 5 Kbps), and speeds can reach 1 Mbps (with a communication distance of less than 40 meters).

Understanding UART, I2C, SPI, and Other Communication Protocols

CAN Logic Levels

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

From a potential perspective, since the high potential is defined as 0 and the low potential as 1, when signals are sent simultaneously, the actual representation is high potential, making it appear that 0 covers 1, thus 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, two of which are data lines, and all USB data transmission is completed through these two lines. Its communication is much more complex than that of serial ports.

The two data lines use differential transmission, meaning that both data lines must work together to transmit one bit, thus it is half-duplex communication, where only sending or receiving can occur at the same 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 Protocols

USB to TTL

Generally, USB to serial conversion uses the CH340G chip.

Understanding UART, I2C, SPI, and Other Communication Protocols

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

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, communication between the microcontroller and the SD card can occur in two modes:

  • SPI Bus Communication Mode

  • SD Bus Communication Mode

Understanding UART, I2C, SPI, and Other Communication Protocols

It is worth noting that in SD bus mode, there are four data lines; in SPI bus mode, there is only one data line (MOSI and MISO cannot read or write data simultaneously). Therefore, in embedded systems, communication between the microcontroller and the SD card is faster in SD bus mode than in SPI bus mode.

Understanding UART, I2C, SPI, and Other Communication Protocols

1-WIRE Bus

1-Wire was introduced by Dallas Semiconductor and is an asynchronous half-duplex serial transmission. 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 1-Wire line is generally 16.3 Kbit/s, with a maximum of 142 Kbit/s, and typically operates at a transmission rate below 100 Kbit/s.

The 1-Wire port is a drain open collector or tri-state gate port, so a pull-up resistor Rp is usually required, typically chosen to be between 5K and 10KΩ.

The 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 the STM32, related articles: Detailed Explanation of DMA Principles in STM32. It operates independently of the CPU, transferring data between peripheral devices and memory, freeing up the CPU and greatly improving its efficiency.

Understanding UART, I2C, SPI, and Other Communication Protocols

It can access peripherals and memory at high speeds, transferring data without CPU control, and supports bidirectional communication. Therefore, using DMA can significantly improve data transfer speeds, which is also a highlight of the ARM architecture—DMA bus control.

DMA is analogous to a highway, with dedicated and high-speed characteristics. While it is possible to achieve objectives without using DMA, it takes longer to do so.

Ethernet

Ethernet is currently the most widely used LAN technology.

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

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

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

Many current motherboards’ southbridge chips include Ethernet MAC control functionality but do not provide a physical layer interface. Therefore, an external PHY chip is needed to provide access to Ethernet.

Understanding UART, I2C, SPI, and Other Communication Protocols

The function of the network transformer is:

  • To couple differential signals, providing stronger anti-interference capabilities.

  • To isolate different voltage levels between devices connected to the network cable, thus isolating DC signals.

The reference circuit for the Ethernet interface is shown in the diagram below.

Understanding UART, I2C, SPI, and Other Communication Protocols

Understanding UART, I2C, SPI, and Other Communication Protocols

Leave a Comment