Skip to content

Latest commit

 

History

History
134 lines (91 loc) · 5.24 KB

File metadata and controls

134 lines (91 loc) · 5.24 KB

⏱️ Chapter 2: Protocol Math and Timing

In SPI and I2C, there was a "Clock Line" (SCK/SCL). The Master would say, "Read data when I toggle the clock." UART, however, is Asynchronous. There is no shared clock line. The Receiver and Transmitter must have sworn (agreed) beforehand on the speed at which data will arrive.

If one speaks at 9600 and the other at 9650, communication will collapse after a few bytes. This is called Baud Rate Drift.


2.1 UART Packet: The 8N1 Standard 📦

On a UART line, the voltage is HIGH (1) when Idle. Communication begins when the line is pulled LOW (0).

A standard data packet (Frame) consists of the following wagons:

  1. Start Bit (1 Bit): Always 0 (Low). Tells the receiver: "Wake up, data is coming!"
  2. Data Bits (5-9 Bits): Usually 8 bits (1 Byte). The Least Significant Bit (LSB) is sent first.
  3. Parity Bit (Optional): Added for error checking (Odd/Even/None).
  4. Stop Bit (1/1.5/2 Bits): Always 1 (High). Returns the line to idle and provides breathing room for the next byte.

Industry Standard: 99% of devices worldwide use 8N1.

  • 8: Data Bits
  • N: No Parity
  • 1: 1 Stop Bit
graph LR
    subgraph IDLE [Idle State]
    I[HIGH]
    end

    subgraph FRAME [Data Frame (8N1)]
    S[Start Bit\n(LOW)] --> D0[Bit 0]
    D0 --> D1[Bit 1]
    D1 --> D2[...]
    D2 --> D7[Bit 7]
    D7 --> ST[Stop Bit\n(HIGH)]
    end

    IDLE --> S
    
    style S fill:#ffccbc,stroke:#d84315
    style ST fill:#ffe0b2,stroke:#ef6c00
    style D0 fill:#e3f2fd,stroke:#1565c0
    style D7 fill:#e3f2fd,stroke:#1565c0
Loading

2.2 Baud Rate Math 🧮

Baud Rate is the number of symbols (bits) per second. The higher the Baud Rate, the shorter the duration of a single bit.

$$T_{bit} = \frac{1}{\text{Baud Rate}}$$

Critical Timing Table

Baud Rate 1 Bit Duration ($T_{bit}$) 1 Byte Duration (Total 10 bits) Use Case
9600 104.16 µs ~1.04 ms Legacy sensors, GPS, Consoles
115200 8.68 µs ~86 µs ESP32 Debug, Bootloader
921600 1.08 µs ~10 µs High-Speed Data (Camera, etc.)
250000 4.00 µs 40 µs DMX512 (Stage Lighting)

⚠️ Field Note: With 9600 Baud, you can send a maximum of 960 Bytes per second (9600 / 10). Do not ask "Why does 1KB of data take longer than 1 second?". The Start and Stop bits create a 20% "tax" (overhead).


2.3 Tolerance: The 2% Rule and Causes of Error 📉

The UART receiver starts its stopwatch the moment it sees the Start bit (falling edge). It tries to read the line exactly in the middle (center sampling) of every $T_{bit}$ duration.

What happens if the sender is 3% slower?

  • 1st Bit: Receiver reads correctly near the center. (No problem)
  • 4th Bit: Receiver reads slightly off-center. (Risky)
  • 8th Bit: Receiver has drifted so much that it reads the wrong bit entirely.

Result: Data arrives as 0xE5 instead of 0x55. Rule: The Baud Rate difference between the two sides must not exceed 2%.

Why Does It Drift?

  1. Internal Oscillator: The crystal of the Arduino/ESP32 can be affected by heat and change frequency.
  2. Division Error: If you cannot divide the 80 MHz CPU clock by integers to get exactly 115200, you mathematically miss the target speed.

2.4 Error Types (The Usual Suspects) 🕵️‍♂️

If you see strange characters or error flags in the terminal, let's identify the culprit:

1. Framing Error

  • Event: Receiver read 8 data bits and now expects a "Stop Bit" (HIGH). But the line is still LOW!
  • Meaning: "Synchronization lost, wrong Baud Rate, or interference."
  • Symptom: FE flag raised.

2. Parity Error

  • Event: The sum of incoming bits should be "Even" but turned out "Odd".
  • Meaning: "A bit flipped along the way (Noise)."
  • Symptom: PE flag raised.

3. Break Condition

  • Event: The line stayed LOW for longer than a full packet duration.
  • Meaning: "Cable cut or the other side is intentionally sending a 'Reset' signal." (Common in LIN Bus protocol).

✅ Summary

  1. Overhead: To send 1 Byte, you actually spend the time of 10 bits (Start + 8 Data + Stop).
  2. Sensitivity: If the difference between Baud Rates exceeds 2%, data corruption occurs.
  3. Default: Unless stated otherwise in the datasheet, the setting is 9600 - 8N1.
  4. Oscilloscope: When analyzing a UART line, measure the "narrowest pulse width". This gives you the duration of 1 bit, and thus the Baud Rate.


Chapter 1: Physical Layer Main Menu Chapter 3: ESP32 Integration