1. UART: The Art of Asynchronous Communication
As Qiu Qianzhang from “The Legend of the Condor Heroes” said, UART is like my lightness kung fu, gliding over the water. If you want to cross the river (communicate), you need to set up markers in advance, and your steps should be fixed according to the distance between the markers (baud rate determined beforehand). If your stride is too long or too short, you will fall into the water. To avoid being discovered by my brother Qiu Qianren, I can arrange guards on the opposite bank to monitor and notify me, starting the performance only when there is no risk (flow control). To ensure accurate timing, set up a special marker every certain distance. UART (Universal Asynchronous Receiver/Transmitter) uses three wires for communication: RX, TX, and GND. TX is for sending data, and RX is for receiving data, with both sides cross-connected, supporting full-duplex communication.
Without clock control, how do we know when to start sending data and ensure the other party receives it correctly?
For example, when A sends data to B, during idle time, A.TX and B.RX remain high. When A.TX sends a 0 as the start bit, it tells B to pay attention, as it is about to send data. Then data transmission begins, with configurable data bits, usually 5, 6, 7, or 8 bits. After sending a frame of data, A.TX sends a high signal to inform B.RX that it has finished a frame. If parity is enabled, a parity bit is sent before the stop bit, although parity is generally not needed, as the error probability in short-distance wired transmission is very low. If there is more data, the previous operation is repeated.
Typically, software configures the serial port with baud rate, data bits, stop bits, parity bits, and flow control. These represent transmission speed, frame length, stop indication, whether to check parity, and flow control. Although there seem to be many parameters, based on personal experience, it is usually fixed at 8 data bits, 1 stop bit, no parity, and no flow control, just configuring the baud rate.
UART relies on a pre-defined baud rate for data capture timing, just like Qiu Qianzhang’s water gliding; once the markers are set, you must walk according to the fixed step length, or you will fall.
UART can glide on water, but its transmission efficiency is limited, generally up to 921600; exceeding this may lead to errors. Continuing to increase the speed is like flying at high altitudes, ultimately leading to Qiu Qianzhang’s desire to escape on Huang Rong’s giant eagle, risking a fall and dying in a flying accident.
2. I2C: The Art of Addressing
As a servant boy watching over the silver furnace, the Silver Horn King understands I2C best. Among the thousands of people, if I call you, you are doomed (communication can only happen if the slave address is correct). I2C (Inter-Integrated Circuit) uses two wires: one clock line (SCL) and one data line (SDA), which makes it half-duplex communication in a master-slave mode, supporting one-to-many communication. One Silver Horn King can handle a group of monkeys, each with a different name (different I2C addresses for slave devices). When I call someone, they are taken away by the purple gold gourd.
Assuming host 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 depending on the application. During idle time, both SDA and SCL are high.
Start and Stop Conditions: Start 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. The bus remains busy after the start condition, and only after a certain time following the stop condition does the bus return to idle. During idle time, SDA and SCL are both high. A first pulls SDA low, then pulls SCL low after SDA becomes low (these two actions constitute the start bit of I2C), at which point SDA can send data, while SCL sends periodic pulses. The relationship between SDA and SCL 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 Acknowledgment: Eight bits of data are transmitted at a time. After the 8 bits are transmitted, 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, 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 Timing: Each device on the I2C bus has a unique address. 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 one indication bit (for 7-bit addressing mode), where 0 indicates write and 1 indicates read. Generally, the I2C address in chip manuals is 7 bits, with some related to the level of a certain pin, controlled by the master for the final read/write bit.
In practical projects, I2C libraries are usually used, some requiring an 8-bit write address, while others require a 7-bit address, distinguishing read/write through interface functions. Of course, the most foolish method is to cycle read a certain register address from 0 to 255, and when the correct value is read, that address is the correct slave address. In general, using the I2C library, aside from configuring the slave address, other timing such as start, stop, etc., are not heavily focused on; just configure the clock frequency based on the maximum supported by the slave and the system clock of the master, with lower frequencies being more stable when there are no timing requirements.
3. SPI: The Art of Synchronous Communication
Murong Fu from “Demi-Gods and Semi-Devils”: Although I cannot use the Eighteen Dragon-Subduing Palms like Qiao Feng, if he strikes me, I can return the favor in kind. When he outputs, he will also be hurt; when he stops the clock, I have no choice. This is like SPI, where the master generates the clock to send data, and the slave simultaneously outputs data. When the clock stops, everyone stops transmitting. SPI (Serial Peripheral Interface) is a master-slave mode, a high-speed, full-duplex synchronous communication bus. The standard SPI consists of four lines: SDI (data input), SDO (data output), SCLK (clock), and CS (chip select, sometimes called SS).
SDO/MOSI – Master Data Output, Slave Data Input; SDI/MISO – Master Data Input, Slave Data Output; SCLK – Clock signal generated by the master; CS/SS – Slave enable signal controlled by the master. When there are multiple slaves, the master selects one slave for communication using the chip select pin (I2C implements this through a software protocol, while SPI does it through hardware).
When the master controls CS and opens the clock gate, both master and slave can start sending or receiving data bits for interaction. However, there are standards for when to start. Depending on the peripheral’s working requirements, its output serial synchronous clock polarity and phase can be configured.
CPOL: Clock Polarity Selection, when 0, SPI bus idle is low, when 1, SPI bus idle is high.
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 gives rise to four modes. Taking mode 1 as an example, when idle it is low, the first clock transition samples, meaning data is sent on the falling edge. If you really can’t tell, a foolish method is to try all four modes once to find the correct one.
SPI data transmission has no bit limit; as long as you define whether to send high or low first, you can sustain high-speed transmission.
As previously mentioned, if Qiao Feng stops, Murong Fu cannot use the Eighteen Dragon-Subduing Palms effectively, but he can insult Qiao Feng, provoking him to act, allowing Murong Fu to succeed. This insult can be translated into software terms as triggering an interrupt, where the slave interrupts the master to inform it that it has something to handle; the master can also implement periodic polling, although this is less commonly used.
4. 1-Wire: The Art of One-Wire Communication
Qiu Qianzhang’s younger sister, Qiu Qianchi, is imprisoned underground. She uses her mouth to shoot jujube pits into the jujube tree; the shaking of the tree causes jujubes to fall for her to eat. This jujube pit shooting is a one-way operation; if too much force is applied, the pit penetrates the tree, and if not enough force is applied or it misses, the tree does not respond. Thus, this technique appears simple but requires precise control, timing, and position, just like 1-Wire communication, which uses a single wire for control with precise timing. The 1-Wire bus interface is simple, requiring only one wire, typically with an open-drain output internally and an external pull-up resistor.
1-Wire uses a single wire to transmit four types of signals, including reset pulses, online response pulses, write 0 time slots, write 1 time slots, and read time slots. All signals except the online response pulse are sent by the bus master, and all transmitted data and commands are in the low byte first. Data communication between the master and slave is completed through time slots, where only one bit of data can be transmitted in each time slot. The write time slot transmits data from the master to the slave, while the read time slot transmits data from the slave to the master, with the time taken to complete one bit transmission referred to as a time slot.
General operation processes refer to the peripheral chip manuals, mainly focusing on delay processing on different platforms, requiring software to implement a 1us delay interface; otherwise, data communication may be abnormal.
5. Conclusion
Each of the four interfaces has suitable application scenarios, differing in hardware port occupation, software control requirements, and communication efficiency. The first three are commonly used protocols, generally supporting hardware interfaces, and manufacturers usually provide HAL libraries, gradually lowering the requirements for software developers. This also leads to smooth code applications, with slightly lacking understanding of the underlying principles, making it difficult to troubleshoot communication anomalies or special needs. For example, using GPIO to simulate UART or using SPI to implement AT functionality.
Martial artists generally pursue lost martial arts secrets, just like software developers who hope to rely on others’ experience summaries or manufacturers’ technical support or source code when encountering problems, rather than creating new methods. Yue Buqun from “The Smiling, Proud Wanderer” was originally the leader of the Huashan School, proficient in the Purple Cloud Skill, which is first-class, but he did not continue to study his own internal skill, leading to self-mutilation for the Evil-Repelling Sword Manual. Do software developers want to repeat this mistake?
Regardless of whether one follows the sword faction or the qi faction, one should first get the function running before deducing the code principles and implementation process, or clarify the timing and principles before coding to implement functions. In the short term, the sword faction is more efficient and gets promoted quickly; the qi faction may be eliminated, especially in small companies that do not prioritize training newcomers. If both can be combined, in urgent projects, use ready-made solutions, and in spare time, research and summarize to complement each other, it would be the ideal quality of a developer.
There are no secret manuals in software development; it all relies on personal learning and summarization.
