Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

In microcontroller development, interfaces 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.

Universal Asynchronous Receiver-Transmitter (UART)

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

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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

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

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

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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, representing the number of bits transmitted. The common value is 8, as an ASCII character is 8 bits.

Parity Bit: Used by the receiver to check the received data, ensuring that the number of “1” bits is 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 with 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).

The SCL line is responsible for generating synchronous clock pulses.

The SDA line is 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, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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 receive commands.

Both the 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 with general I/O ports and achieving bidirectional transmission, one input/output port (SDA) and one output port (SCL) are required.

Serial Peripheral Interface (SPI)

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

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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 state of the clock signal; CPOL = 0 means the initial state is low, while CPOL = 1 means the initial state is high. CPHA indicates at 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, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

Comparison of UART, SPI, and I2C

  • I2C has fewer lines and is more powerful than UART and SPI, but it is technically more complicated because I2C requires support for bidirectional I/O 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.

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

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

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

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

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

RS232 Serial Communication

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

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

RS-232 serial communication can transmit distances 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 RXD, TXD, and GND being the most commonly used signals.

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

TTL and RS-232 Conversion

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

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

RS422 Serial Communication

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

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

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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, enhancing resistance to interference and allowing for long transmission distances (from tens of meters to thousands of meters).

+2V to +6V represents logic “1”, while -2V to -6V represents 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, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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, which has 4 pins (Vcc, GND, RX, TX) and uses TTL levels.

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

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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 is mainly connected through 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 is required between CAN_H and CAN_L.

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

Each device can act as both a master and a slave. The communication distance of the CAN bus can reach 10 kilometers (at speeds below 5 Kbps), with speeds up to 1 Mbps (for communication distances less than 40 m).

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

CAN Logic Levels

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

From a voltage perspective, since the high voltage is defined as 0 and the low voltage as 1, when signals are sent simultaneously, the actual signal appears as high voltage, visually resembling 0 covering 1, hence 0 is dominant and 1 is recessive.

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

USB Serial Bus Communication

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 are needed to transmit one bit, thus it is half-duplex communication, allowing only sending or receiving 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, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

USB to TTL

Generally, USB to serial conversion uses the CH340G chip.

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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 as a memory card in mobile phones.

In embedded systems, communication between microcontrollers and SD cards can occur in two modes:

  • SPI bus communication mode

  • SD bus communication mode

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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 microcontrollers and SD cards using SD bus mode is several times faster than using SPI bus mode.

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

The data transmission rate of the 1-Wire bus is generally 16.3 Kbit/s, with a maximum of 142 Kbit/s, and typically operates at rates below 100 Kbit/s.

The 1-Wire line port is an open-drain or tri-state port, so a pull-up resistor (Rp) is generally required, typically chosen to be 5K to 10KΩ.

It is mainly used for identifying printer cartridges or medical consumables, as well as for identifying and authenticating printed circuit boards, components, and peripherals.

Direct Memory Access (DMA)

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

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

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

DMA is analogous to a highway, characterized by its dedicated and high-speed nature. While it is possible to achieve the same goals without using DMA, it takes significantly longer.

Ethernet

Ethernet is currently the most widely used local area network technology.

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: PHY (Physical Layer) and the transceiver.

Many motherboard southbridge chips now 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, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

The role of the network transformer is:

  • To couple differential signals, enhancing interference resistance.

  • To isolate different voltage levels of different devices at the network end, isolating DC signals.

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

Understanding UART, I2C, SPI, TTL, RS232, RS422, RS485, CAN, USB, SD Card, 1-WIRE, and Ethernet in Embedded Development

Leave a Comment