1. UART: The Lightness of Qiu Qianzhang’s Skills
In “The Legend of the Condor Heroes”, Qiu Qianzhang says, “UART is like my lightness skill, floating over the water to cross the river.” To cross the river (communication), one must prepare in advance, setting markers at fixed intervals (baud rate predetermined). If the steps are too big or too small, one will fall into the water. To avoid being exposed by his brother Qiu Qianren, he arranges guards on the opposite bank to monitor and notify him, starting the performance only when there is no risk (flow control). To ensure accurate timing, he places rough wooden stakes with special markings at intervals.
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 a cross connection between both parties supporting full-duplex communication.
Since there is no clock control, how does one know when to start sending data and ensure the other party receives it correctly?
When A sends data to B, during idle time, A.TX and B.RX remain high. When A.TX first sends a 0 as the start bit, it tells B to pay attention, “I am about to send data.” Then it starts sending data, which can be configured to be 5, 6, 7, or 8 bits long. After sending a frame of data, A.TX sends a high signal to inform B.RX that it has finished sending a frame. If a parity bit is enabled, it sends a parity bit before the stop bit, though typically, parity bits are not needed as the error probability is very low over short distances. If there is more data, the previous operation is repeated.Generally, software configures the serial port with baud rate, data bits, stop bits, parity bits, and flow control, representing transmission speed, frame length, stop notification, parity checks, and transmission control. While it seems there are many parameters, based on personal experience, it’s usually fixed at 8 data bits, 1 stop bit, no parity, and no flow control—just configuring the baud rate.UART relies on defining the baud rate before communication, with both parties reading and writing data bits at the defined frequency, just like Qiu Qianzhang’s water walking; once the markers are installed, one must walk with fixed steps, or else errors will occur and one will fall.UART can perform water walking, but its transmission efficiency is limited, typically up to 921600 baud. Beyond that, errors may occur, and increasing it further is like flying high; eventually, Qiu Qianzhang hopes to walk freely in the air but risks falling and dying in a flying accident.
2. I2C: Will You Respond When I Call Your Name?
As the boy watching the silver furnace for Taishang Laojun, the Silver Horn King understands I2C best: among thousands, when I call your name, if you respond, you’re in trouble (communication requires the correct slave address).
IIC (Inter Integrated Circuit) uses two wires, one for the clock line SCL and one for the data line SDA, making it half-duplex communication in a master-slave mode. One master can handle multiple slaves, with each slave having a different I2C address. When called, the identified slave is taken away by the purple gold gourd.
Assuming host A sends data to slave B (A.SCL connected to B.SCL, A.SDA connected to B.SDA), A can simultaneously connect to B, C, and D based on the application. When idle, 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, and the bus is busy after the start condition until a certain time after the stop condition, when the bus becomes idle again.
When idle, both SDA and SCL are high. A pulls SDA low first, then pulls SCL low after SDA is low (these two actions constitute the start bit of I2C), allowing SDA to send data while SCL sends periodic pulses. 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 when SCL is low (SCL samples SDA on the rising edge).Transmission and Acknowledgment: A transmits 8 bits of data at a time. After the 8 bits are transmitted, A releases SDA, and SCL sends another pulse (this is the ninth pulse), prompting 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 to 0, 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 bits are sent first, followed by the data. An address byte consists of 7 address bits (allowing for 128 devices) and 1 indication bit (in 7-bit addressing mode), where 0 indicates write and 1 indicates read. Typically, I2C addresses in chip manuals are 7 bits, with some dependent on the level of a certain pin, controlled by the master for the final read/write bit.In practical projects, I2C libraries are generally used, with some requiring an 8-bit write address, while others use a 7-bit address, distinguishing read/write through the interface function. Of course, the most foolish method is to loop through addresses from 0 to 255, reading a certain register address, and the address that returns the correct value is the correct slave address.
Generally, when using I2C libraries, aside from configuring the slave address, other timing concerns like start and stop are not heavily focused on; one only needs to configure the clock frequency, based on the maximum supported by the slave and the master’s system clock. If the clock is too high, errors may occasionally occur; in the absence of timing requirements, a lower clock is more stable.
3. SPI: Can You Handle It Like Murong Fu?
Murong Fu from “Demi-Gods and Semi-Devils”: Although I cannot perform the Eighteen Dragon-Subduing Palms like Qiao Feng, when he strikes at me, I return the favor, causing mutual harm; when he stops the clock, I am helpless. Just like SPI, when the master starts the clock to send data, the slave also outputs simultaneously; when the clock stops, everyone halts.
SPI (Serial Peripheral Interface) operates in a master-slave mode, providing a high-speed, full-duplex synchronous communication bus. Standard SPI utilizes 4 lines: SDI (Serial Data Input), SDO (Serial Data Output), SCLK (Serial Clock), and CS (Chip Select, sometimes referred to as 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 via the chip select pin (I2C achieves this through software protocol, while SPI uses hardware).
When the master controls CS and opens the clock gate, both master and slave can start sending or receiving data bits. However, the timing for starting has its standards. Depending on the peripheral’s operational requirements, the output serial synchronous clock’s polarity and phase can be configured.
CPOL: Clock Polarity Selection, where 0 means the SPI bus is idle at low, and 1 means it is idle at high.
CPHA: Clock Phase Selection, where 0 means sampling occurs on the first clock edge, and 1 means sampling occurs on the second clock edge.ModeCPOLCPHA
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
Thus, there are four modes. Taking mode 1 as an example, when idle, it is low; the first clock transition samples on the rising edge, while data is sent on the falling edge. If one is still unsure, a foolish method is to try all four modes until the correct one is identified.SPI has no limit on the number of bits for data transmission, as long as the high and low bit order is defined, allowing for continuous high-speed transmission.As mentioned earlier, if Qiao Feng stops, Murong Fu cannot unleash the Eighteen Dragon-Subduing Palms effectively; however, he can insult Qiao Feng, provoking him to act, thus achieving his scheme. This insult can be translated into software terms as triggering an interrupt, where the slave sends an interrupt to inform the master that it has something to discuss; the master can also implement periodic polling, albeit with less common usage.
4. Qiu Qianchi’s Unique Skill with 1-Wire
Qiu Qianzhang’s third sister, Qiu Qianchi, is imprisoned underground. She shoots jujube pits at the tree, and the shaking of the tree will cause jujubes to fall for her to eat. This jujube pit nail operates unidirectionally; if too much force is used, the pit goes through the tree, and if too little or misdirected, the tree does not react, leading to tragedy when the pits run out. This skill appears simple, but it hides the need for precise control, as timing and positioning must be perfect, just like 1-wire communication, which is controlled by a single line with precise timing.
1-wire bus interface is simple, requiring only one wire, generally with open-drain output and external pull-up hardware.
1-wire transmits four types of signals over a single wire, including reset pulses and online response pulses for the reset sequence, write 0 time slot, write 1 time slot, and read time slot. Except for the online response pulse, all other signals are sent by the bus master, and all data and commands sent are in low bit first order. Communication between the master and slave is completed through time slots, with only one bit of data transmitted in each slot. Data can be sent from the master to the slave via the write time slot, and from the slave to the master via the read time slot, with the time taken to complete a bit transmission referred to as a time slot.Generally, the operational process refers 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 suitable application scenarios, differing in hardware port usage, software control requirements, and communication efficiency. The first three are common protocols, usually supporting hardware interfaces, with manufacturers generally providing HAL libraries, gradually lowering the requirements for software developers. This also results in smooth code applications, while the underlying principles are often slightly lacking; when communication errors occur or special needs arise, one may be at a loss. For example, simulating UART using GPIO or implementing AT functionality with SPI.Martial artists often pursue lost martial arts secrets, just like software developers who hope to rely on others’ experience summaries or manufacturers’ technical support or source code, rather than creating new techniques. Yue Buqun from “The Smiling, Proud Wanderer” was originally the leader of the Huashan Sect, proficient in the Purple Mist Divine Skill, which is first-rate, but he did not continue to study his sect’s internal skills and castrated himself for the Evil-Repelling Sword Manual; do software developers want to repeat this mistake?Regardless of the sword or qi school, one must first get the functionality running before deducing the code principles and implementation processes, or clarify the timing and principles before coding the functionality. In the short term, the sword school is more efficient, and salary increases faster; the qi school may be eliminated, especially in small companies that value immediate results over nurturing newcomers. If one merges both approaches, using what is available in urgent projects while studying and summarizing during idle times, it reflects the qualities of a perfect developer.There are no secret techniques in software development; it all relies on individual learning and summarization.

Summary of STM32 Serial Data Transmission and Reception Methods!Common Communication Interfaces for Microcontrollers: I2C, SPI, UART, etc.I2C Operation Notes – Taking AT24C04 as an ExampleConfused for a Long Time? Detailed Explanation of Pull-up and Pull-down ResistorsRS485 Interface EMC Circuit Design SolutionsWhat is the LIN Bus? Differences Between High-Speed CAN, Fault-Tolerant CAN, and LIN Bus?PCB Design Must Consider Basic ProcessesC Language: Show Operations | Three Encapsulation Methods for Function Macros
