Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

1. Qiu Qianzhang’s Lightness Skill: UART

In the Legend of the Condor Heroes, Qiu Qianzhang says that UART is like my lightness skill, allowing me to float over water to cross rivers. To communicate (cross the river), set up hidden stakes in advance, and during the crossing, maintain a fixed step based on the distance between stakes (baud rate predetermined). If the step is too large or too small, one might fall into the water.

To avoid being discovered by his second brother, Qiu Qianren, he can arrange for guards to monitor and notify him from the other side; only start performing when there is no risk (flow control). To ensure accuracy, a special marker stake is set at intervals.

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

UART stands for Universal Asynchronous Receiver/Transmitter. The communication between both parties involves three wires: RX, TX, and GND. TX is used for sending data, while RX is for receiving data. The connections are crossed, supporting full-duplex communication.

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

How to ensure correct data transmission without clock control? When should data be sent, and how to ensure the receiver correctly receives it?

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

When A sends data to B, during idle times, A.TX and B.RX remain high (1). When A.TX sends a 0 as the start bit, it signals B to pay attention, indicating that data will be sent. Then, data is transmitted, and the data bits can be configured, typically to 5, 6, 7, or 8 bits. After sending a complete frame, A.TX sends a high signal to inform B.RX that the frame is complete. If a parity bit is enabled, it is sent before the stop bit; however, parity bits are generally unnecessary as the probability of error in short-distance wired transmission is very low. If more data follows, the previous operation is repeated.

Typically, software configures the serial port with baud rate, data bits, stop bits, parity bits, and flow control. These parameters represent transmission speed, frame length, notification of completion, whether to check for errors, and whether to control transmission. Although it seems like there are many parameters, based on personal experience, the common configuration is usually 8 data bits, 1 stop bit, no parity, and no flow control, focusing only on configuring the baud rate.
UART relies on defining the baud rate before communication, allowing both parties to read and write data bits at the defined frequency. Just like Qiu Qianzhang’s water-floating skill, once the stakes are set, one must walk with a fixed step; otherwise, mistakes will occur, and one may fall.
UART can perform water-floating projects, but its transmission efficiency is limited, typically up to 921600 baud. Beyond that, errors may occur, and further increases lead to high-altitude flight, where Qiu Qianzhang hopes to escape on Huang Rong’s giant eagle but inadvertently falls, meeting a fatal accident.

2. Will You Respond If I Call You? I2C

As the attendant watching over the silver furnace, Silver Horn King understands I2C best; amidst the crowd, if I call you and you respond, you’re doomed (only if the slave address is correct can communication occur).

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

I2C (Inter-Integrated Circuit) uses two wires: one clock line (SCL) and one data line (SDA), allowing for half-duplex communication in a master-slave mode. A single Silver Horn King can handle many monkeys, each with a different name (the I2C addresses of slave devices differ). When a name is called, that monkey is taken away by the golden gourd.

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

Assuming the master A sends data to slave B (A.SCL connects to B.SCL, A.SDA connects to B.SDA), A can simultaneously connect to B, C, and D. During idle times, the levels on SDA and SCL are high.
Start and Stop ConditionsStart condition S: When SCL is high, SDA transitions from high to low; stop condition P: When SCL is high, SDA transitions from low to high. The start and stop conditions are generally generated by the master, and the bus remains busy after the start condition until a certain time after the stop condition, when it becomes idle again.

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

When idle, both SDA and SCL are high. A first pulls SDA low, and after SDA is low, SCL is pulled low (these two actions constitute the I2C start bit), at which point SDA can send data. Meanwhile, SCL sends a series of pulses, and the relationship between SDA sending data and SCL sending pulses is that SDA must remain valid while SCL is high and send the next bit while SCL is low (SCL samples SDA on the rising edge).
Transmission and AcknowledgmentData is transmitted 8 bits at a time; after the 8 bits, A releases SDA, and SCL sends one more pulse (the ninth pulse), triggering B to pull SDA low to acknowledge (this low level is called ACK). Finally, SCL goes high first, and then SDA goes high (these two actions are called the stop condition); if B does not pull SDA low, A stops sending the next frame of data.
Overall TimingEach device on the I2C bus has a unique address, and during data packet transmission, the address is sent first, followed by the data. An address byte consists of 7 address bits (allowing for 128 devices) and 1 indication bit (7-bit addressing mode), where 0 indicates write and 1 indicates read. Generally, I2C addresses in chip manuals are 7-bit; some are related to the level of a certain pin, and the master controls the read/write bit at the end.
In practical projects, I2C libraries are typically used, with some libraries requiring an 8-bit write address and others a 7-bit address, distinguishing read/write in the interface function. Of course, the most foolish method is to cycle through addresses from 0 to 255, reading a certain register address; when the correct value is read, that address is the correct slave address.

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

Generally, when using an I2C library, besides configuring the slave address, the start, stop, and timing conditions are not heavily focused on; one just needs to configure the clock frequency, usually based on the maximum supported by the slave and the master’s system clock. A higher frequency may occasionally lead to errors, and in the absence of timing requirements, a lower clock is more stable.

3. Murong Fu’s Fight: SPI

Murong Fu from the Demi-Gods and Semi-Devils: Although I cannot use the Dragon Subduing Palm like Qiao Feng, when he strikes me, I can return the favor, and when he attacks, he also gets hurt. If he stops the clock, I have no way to respond. Just like SPI, when the master turns on the clock to send data, the slave also outputs simultaneously; if the clock stops, everyone stops.

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

SPI (Serial Peripheral Interface) operates in a master-slave mode, providing a high-speed, full-duplex synchronous communication bus. Standard SPI consists of 4 lines: SDI (data input), SDO (data output), SCLK (clock), and CS (chip select, sometimes called SS).

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

SDO/MOSI:Master device data output, slave device data input; master output slave input;

SDI/MISO:Master device data input, slave device data output; master input slave output;

SCLK:Clock signal generated by the master device;

CS/SS:Slave device enable signal controlled by the master device. When multiple slave devices are present, the master selects one of them for communication using the chip select pin.

(I2C implements multiple selections through software protocol, while SPI achieves it through hardware.)

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

When the master controls CS and opens the clock gate, both master and slave can start sending or receiving data bits. However, there are standards for when to start. Depending on the requirements of the peripheral device, its output serial synchronous clock polarity and phase can be configured.
CPOL:Clock polarity selection; when 0, the SPI bus is idle at low level; when 1, the SPI bus is idle at high level.
CPHA:Clock phase selection; when 0, sampling occurs on the first clock transition; when 1, sampling occurs on the second clock transition.
Mode CPOL CPHA
0 0 0
1 0 1
2 1 0
3 1 1
This results in four modes. Taking mode 1 as an example, when idle, the level is low, the first clock transition samples on the rising edge, while data is sent on the falling edge. If it is still unclear, a foolish method is to try all four modes once to determine the correct one.

SPI data transmission has no bit limit; as long as it is defined whether to send high bits first or low bits first, high-speed transmission can continue.

As mentioned earlier, if Qiao Feng stops, Murong Fu cannot unleash the Dragon Subduing Palm’s effect, but he can insult Qiao Feng, triggering Qiao Feng’s anger, which allows Murong Fu to achieve his sinister plan. This insult can be translated into software terms as triggering an interrupt; the slave informs the master that it has something to address; the master can also implement a periodic query, although this is less common.

4. Qiu Qianchi’s Jujube Pit Technique and 1-Wire

Qiu Qianzhang’s third sister, Qiu Qianchi, was imprisoned underground. She uses her mouth to shoot jujube pits into a jujube tree; the shaking of the tree will drop jujubes for her to eat. This jujube pit technique is unidirectional; if too much force is used, the pit goes through the tree; if too little or misdirected, the tree does not respond, leading to a tragic outcome when the pits run out. This technique appears simple but requires precise control over timing and position, similar to 1-wire communication, which uses a single wire for control and requires precise timing.

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

The 1-wire bus interface is simple, requiring only one wire, typically using open-drain output with an external pull-up resistor.

Understanding UART, I2C, SPI, and 1-Wire Communication Interfaces

1-wire transmits four types of signals over a single wire, including reset pulses and online response pulses for reset sequences, write 0 time slots, write 1 time slots, and read time slots. Except for the online response pulse, all other signals are sent by the bus master, and all data and commands sent are in byte format with the least significant bit first. Data communication between the master and slave is completed through time slots, with only one bit of data transmitted in each time slot. The write time slot can send data from the master to the slave, while the read time slot can send data from the slave to the master, completing one bit transmission in a time slot.
The general operation process should refer to the peripheral chip manual, mainly concerning the delay handling of different platforms, requiring software to implement a 1us delay interface; otherwise, data communication may be abnormal.

5. Secret Techniques

Each of the four interfaces has its suitable application scenarios, differing in hardware port occupancy, software control requirements, and communication efficiency. The first three are common protocols, generally supporting hardware interfaces, and manufacturers usually provide HAL libraries, gradually lowering the requirements for software developers. This leads to fluent code applications, while the fundamental principles may be slightly lacking; once communication errors occur or special needs arise, one may not know how to proceed, such as using GPIO to simulate UART or using SPI to implement AT functions.
Martial arts practitioners often seek lost martial arts secrets, just as software developers often rely on others’ experiences or manufacturers’ technical support or source code instead of creating new techniques. Yue Buqun, the head of the Huashan School, excels in the Purple Mist Divine Skill but fails to continue researching his internal skills, ultimately castrating himself for the Evil-Repelling Sword Manual. Do software developers wish to repeat this mistake?
Regardless of whether one follows the sword school or the qi school, first ensure the functionality works before tracing back the code principles and implementation processes, or clarify the timing and principles before coding to achieve functionality. In the short term, the sword school may be more efficient and receive quicker salary increases; the qi school may risk being eliminated, especially in small companies that do not focus on training newcomers. If one can combine both, using ready-made solutions for urgent projects and dedicating free time to research and summarize, it would be the ideal quality of a developer.
Software development has no secret techniques; it relies entirely on personal learning and summarization.
END
Source: Embedded Systems
Copyright belongs to the original author. If there is any infringement, please contact for deletion.
Recommended Reading
Impressive Linux Terminal Tools!
Why does the CPU have 8 cores, but the network card keeps messing with core one?
Why are Chinese numbers grouped in four and Western numbers in three?
→ Follow for more updates ←

Leave a Comment