UART in Embedded Communication Protocols
In the first phase, we have mastered the basic theory and preliminary practice of UART, including the basic concepts of frame structure and baud rate. Now, we will delve into the configuration details of the UART protocol, particularly focusing on the calculation of baud rate error and the application of flow control mechanisms.01Protocol and Configuration
1. Baud Rate Error
Baud rate error refers to the difference between the actual baud rate and the expected baud rate. In UART communication, to ensure the reliability of data transmission, the error is typically required to be within 2%.
The calculation method: The actual baud rate of UART is determined by the clock frequency and the divider, as shown in the following formula:
Actual Baud Rate = Clock Frequency / (16 × Divider Value)
The divider value usually needs to be rounded, as hardware registers only accept integers.
Imagine the clock frequency as a rushing river, and the divider as a waterwheel; each turn (counting to the divider value) drives the gears to turn. The gear speed determines the baud rate, just as the speed of the waterwheel determines how quickly the mill grinds flour.
Large Divider Value: The waterwheel turns slowly, resulting in a low baud rate.Small Divider Value: The waterwheel turns quickly, resulting in a high baud rate.
The formula for calculating the percentage of error is:
Error = | (Expected Baud Rate - Actual Baud Rate) / Expected Baud Rate | × 100%
Impact:
– If the error is too large (exceeding 2%), the receiver may make errors when sampling data, leading to data distortion or loss.
– The reason is that UART is asynchronous communication; the sender and receiver rely on baud rate synchronization, and a large error can cause sampling points to shift.
2. Flow Control Mechanism
Flow control is a mechanism for managing the rate of data transmission, aimed at preventing the receiver’s buffer from overflowing. The commonly used hardware flow control method in UART is RTS/CTS.
RTS/CTS Principle:
RTS (Request to Send): A signal sent by the sender indicating “request to send data”.
CTS (Clear to Send): A signal sent by the receiver indicating “can receive data”.
– Through these two signals, the sender and receiver can dynamically coordinate the transmission process.
Workflow:
1. The sender checks the CTS signal before sending data.
2. If CTS is high (indicating the receiver is ready), the sender begins to transmit data.
3. If the receiver’s buffer is nearly full, it pulls the CTS signal low, notifying the sender to pause.
4. After the receiver processes the data, it raises the CTS signal again, allowing transmission to continue.
Application Scenarios:
– In high-speed communication or large data transfers, the receiver’s processing speed may not keep up with the sender, and flow control can effectively prevent data loss.
Supplement:
Divider Value = Clock Frequency / (16 × Expected Baud Rate)
The relationship between the divider value and baud rate is as shown in the formula above; first determine the divider value based on the expected baud rate, and then determine the actual baud rate based on the divider value.
Imagine you go to buy candy, wanting to buy 10 pieces, but the store only sells them by the bag, with each bag containing 5 pieces.
You calculate that you need 2 bags (10÷5=2), to get 10 pieces of candy, just right.
But if you want 7 pieces, 7÷5=1.4, you can only buy 1 bag (5 pieces) or 2 bags (10 pieces), and cannot buy exactly 7 pieces, which results in an “error”.
Divider Value: Like the number of bags“ can only be an integer.Expected Baud Rate: The number of candies you want.Actual Baud Rate: The number of candies you actually get.
2. The 16
You may be curious where the “16” in the formula for the divider value and baud rate comes from. This is UART‘s “secret weapon”—16times oversampling.
Function: For each data bit, UART samples 16 times, checking multiple times to determine whether it is 0 or 1, reducing errors.Result: The internal clock frequency (the frequency of the drum) must be 16 times the baud rate.
Imagery: It’s like listening to a slow-paced piece of music; to not miss any beat, you listen 16 times per second, allowing you to accurately catch every note.
3. In UART communication, the detection of buffer nearing full and hardware flow control does not require the programmer to write code; it is automatically handled by the underlying library functions and hardware.
02Preliminary PracticeCalculating Baud Rate Error
Let’s practice calculating baud rate error through a practical example.
Scenario:
– The clock frequency is **16MHz** (16,000,000 Hz).
– The expected baud rate is **9600 bps**.
Steps:
1. Calculate the divider value::
Divider Value = Clock Frequency / (16 × Expected Baud Rate) = 16,000,000 / (16 × 9600) ≈ 104.167
Since the divider value must be an integer, round it to 104.
2. Calculate the actual baud rate::
Actual Baud Rate = Clock Frequency / (16 × Divider Value) = 16,000,000 / (16 × 104) ≈ 9615.38 bps
3. Calculate the error::
Error = | (9600 - 9615.38) / 9600 | × 100% ≈ 0.16%
Conclusion:
– The error is 0.16%, which is far less than 2%, indicating that communication under this configuration is reliable.
03Verify Understanding
To ensure you have mastered the content of this phase, you need to achieve the following goals:
Baud Rate Error:
– Be able to explain the sources of error (clock frequency and rounding of the divider) and the impacts (sampling errors).
– Be able to independently perform error calculations.
Flow Control Mechanism:
– Be able to describe the working principle of RTS/CTS.
– Be able to explain typical application scenarios for flow control.
Continuously update embedded knowledge and industry frontiers