1. Qiu Qianzhang’s Lightness Skill: UART
In “The Legend of the Condor Heroes”, Qiu Qianzhang says, “UART is like my lightness skill, gliding over the water to cross the river.” To cross the river (communication), one must set up hidden stakes in advance, and while moving, the steps must be fixed according to the distance between the stakes (baud rate predetermined). If the steps are too big or too small, one will fall into the water. To avoid being discovered by his brother Qiu Qianren, he can arrange guards on the opposite bank to monitor and inform him, starting the performance only when there is no risk (flow control). To ensure accurate stepping, one must set special markers (stakes) at certain intervals.
UART (Universal Asynchronous Receiver/Transmitter) connects the two communicating parties with three wires: RX, TX, and GND. TX is used for sending data, while RX is for receiving data, with cross-connection for full-duplex communication.
Without clock control, how do we determine when to send data and ensure the other party receives it correctly?
For instance, if A sends data to B, while idle, A.TX and B.RX remain at 1. When A.TX sends 0 as a start bit, it tells B to pay attention because data is about to be sent. Then data transmission begins, and the data bits can be configured to be 5, 6, 7, or 8 bits long. After sending one frame of data, A.TX sends a high signal to indicate to B.RX that the frame has been completed. If a parity bit is enabled, a parity bit 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 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 parameters represent transmission speed, frame length, notification of completion, whether to check for errors, and whether to control transmission. Although it seems there are many parameters, based on personal experience, it is usually fixed at 8 data bits, 1 stop bit, no parity, and no flow control, only configuring the baud rate.
UART lacks clock control for data capture timing, relying on defining the baud rate before communication, with both parties reading and writing data bits at the defined frequency, just like Qiu Qianzhang’s gliding over water. Once the hidden stakes are installed, one must walk with fixed steps; otherwise, errors may occur and one may fall into the water.
UART can glide on water, but the transmission efficiency is limited, generally up to 921600. If it goes higher, errors may occur, and further increases lead to high-altitude flight. In the end, Qiu Qianzhang hopes to walk freely in the sky, wanting to escape on the giant eagle ridden by Huang Rong, but inadvertently falls and dies in a flying accident.
2. Will You Respond When I Call You? – I2C
As the young attendant of the Supreme Lord watching the silver furnace, the Silver Horn King understands I2C the best. Among thousands, if I call you, and you respond, you’re doomed (the slave address must be correct for communication).
IIC (Inter Integrated Circuit) uses two wires: one clock line (SCL) and one data line (SDA), thus it is half-duplex communication in a master-slave mode, supporting one-to-many communication. One Silver Horn King can deal with a group of monkeys, each with a different name (different I2C addresses for slave devices). When called, the one named will be taken away by the purple gold gourd.
Assuming master 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 at high level.
Start and Stop Start condition S: When SCL is high, SDA changes from high to low; Stop condition P: When SCL is high, SDA changes from low to high. The start and stop conditions are generally generated by the master, and the bus is busy after the start condition, returning to idle only after a certain time following the stop condition.
When idle, both SDA and SCL are high. A first pulls SDA low, then pulls SCL low after SDA has changed to low (these two actions constitute the start bit of I2C). At this point, SDA can send data, while SCL generates periodic pulses. The relationship that must be maintained between SDA sending data and SCL sending pulses is: SDA must remain valid when SCL is high, and the next bit is sent when 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-bit transmission, A releases SDA, and SCL sends another pulse (the ninth pulse), prompting B to pull SDA low to indicate acknowledgment (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 bit is sent first, followed by the data. An address byte consists of 7 address bits (allowing for 128 devices) and 1 indicator bit (7-bit addressing mode), where 0 indicates write and 1 indicates read. Generally, I2C addresses in chip manuals are 7 bits, some of which are related to the level of a certain pin, with the master controlling the final read/write bit.
In practical projects, I2C libraries are typically used, some requiring an 8-bit write address, while others require a 7-bit address, with interface functions distinguishing between read and write. Of course, the most foolish method is to loop from 0 to 255 to read a specific 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 aspects such as start, stop, etc., are not closely monitored; one only needs to configure the clock frequency, typically considering how much the slave can support and the master’s system clock; too high may occasionally cause errors, and in the absence of timing requirements, the lower the clock, the more stable it is.
3. Murong Fu’s Star Shift – SPI
Murong Fu from “Demi-Gods and Semi-Devils” says: Although I cannot perform the Eighteen Dragon-Subduing Palms like Qiao Feng, if he strikes me, I will return the attack in kind. When the other party outputs, they also suffer backlash, and when they stop the clock, I am helpless. Just like SPI, the master starts the clock to send data, and the slave also outputs simultaneously; when the clock stops, everyone halts.
SPI (Serial Peripheral Interface) is a master-slave mode, 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 also called SS).
SDO/MOSI – Master device data output, slave device data input; SDI/MISO – Master device data input, slave device data output; SCLK – Clock signal generated by the master; CS/SS – Slave device enable signal controlled by the master. When there are multiple slaves, the master selects one slave for communication through the chip select pin (I2C achieves this via 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 for interaction. However, the timing of when to start is standardized. Depending on the peripheral’s operational requirements, its output serial synchronous clock polarity and phase can be configured. CPOL: Clock polarity selection, 0 means SPI bus idle is low, 1 means SPI bus idle is high. CPHA: Clock phase selection, 0 means sampling at the first clock edge, 1 means sampling at the second clock edge.
| Mode | CPOL | CPHA |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
This results in four modes. For example, in mode 1, idle is low, the first clock edge samples, meaning data is sent on the falling edge. If one cannot distinguish, a foolish method is to try all four modes once to determine the correct mode.
SPI data transmission has no bit limit; as long as the high and low bits are defined, it can transmit at high speed continuously.
As mentioned earlier, if Qiao Feng stops, Murong Fu cannot perform the Eighteen Dragon-Subduing Palms effectively; however, he can insult Qiao Feng, who, in anger, will retaliate, allowing Murong Fu to succeed in his scheme. This insult can be translated into software terminology as triggering an interrupt, where the slave sends an interrupt to inform the master that it has something to address; the master can also implement timed queries, although this is less common.
4. Qiu Qianchi’s Unique Skill of Spitting Jujube Pits and 1-Wire
Qiu Qianzhang’s third sister, Qiu Qianchi, was imprisoned underground. She shoots jujube pits at the tree, causing it to shake and drop jujubes for sustenance. This jujube pit nail operates unidirectionally; if too much force is applied, the pit passes through the tree; if too little force or misaimed, the tree doesn’t respond, leading to tragedy when the pits run out. This skill appears simple but actually requires precise control over timing and positioning, just like 1-wire communication, which uses a single wire with precise clock control.
The 1-wire bus interface is simple, requiring only one wire, generally with open-drain output internally and external pull-up hardware.
1-wire uses a single wire to transmit four types of signals, including the reset pulse and online response pulse 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 byte format. 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 allows data to be sent from the master to the slave, while the read time slot allows data to be sent from the slave device to the master, completing one bit transmission time known as a time slot.
General operation processes refer to peripheral chip manuals, mainly involving different platform delay handling, requiring software to implement a 1us delay interface; otherwise, data communication may become abnormal.
5. Secret Techniques
Each of the four interfaces has suitable application scenarios, differing in hardware port occupation, software control requirements, and communication efficiency. Especially the first three are common protocols, generally supporting hardware interfaces, with manufacturers often providing HAL libraries, gradually lowering the requirements for software developers. This also leads to smoother code applications, while the actual underlying principles may be slightly lacking; once communication anomalies occur or special requirements arise, it becomes challenging to address. For example, using GPIO to simulate UART or using SPI to implement AT functions.
Martial artists generally pursue lost martial arts secrets, akin to software developers who often rely on others’ experience summaries or manufacturers’ technical support or source code, rather than creating new techniques. Yue Buqun, who is the head of the Huashan School, is proficient in the Purple Mist Divine Skill, which is first-class, but he did not continue to study his own internal skills and castrated himself for the Evil-Repelling Sword Manual; do software developers wish to repeat this mistake?
Regardless of sword sect or qi sect, first ensure that functions run smoothly before deducing code principles and implementation processes, or clarify timing and principles before coding to implement functions. In the short term, sword sect efficiency is high, and salary increases quickly; however, qi sect may face elimination, especially in small companies that do not prioritize training newcomers. If both can be combined, for urgent projects, use ready-made solutions, while during downtime, study and summarize to complement each other, this would be the ideal quality of a developer.
Software development has no secret techniques; it relies entirely on personal learning and summarization.
Highlights Review
● Automatic Download Circuit for STC Microcontroller● Quick Introduction to Simple PID Algorithms
● Do You Know How to Accurately Delay in 51 Microcontrollers?
● It Turns Out SPI Is Not As Simple As I Thought
● What Is a State Machine? Implementing a 5-State Model in C Language
● Exciting Article: How Does the CPU Actually Identify Code?