Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 386 of 9365. Communication Protocols and Data TransferFree lesson

Mechanism and Purpose of Bit Stuffing

Explain how inserting extra bits after five consecutive identical bits maintains synchronization and prevents the receiver from losing the clock signal.

In this lesson, you will learn how "bit stuffing" acts as a vital housekeeping rule in the CAN protocol to keep the sender and receiver perfectly synchronized.

The Problem: Drifting Clocks

In communication protocols like CAN, the receiver doesn't have a separate wire for a clock signal. Instead, it must extract the timing (the clock) directly from the incoming data stream by looking for transitions—changes from a 0 (dominant) to a 1 (recessive), or vice versa.

If a long string of identical bits appears (e.g., 0000000000), the signal stays flat. If the receiver’s internal clock is slightly faster or slower than the sender’s, it can lose track of where one bit ends and the next begins. This "drift" causes framing errors and communication failure.

The Solution: Bit Stuffing

To solve this, the CAN protocol enforces a rule: whenever the transmitter detects five consecutive bits of the same value, it must automatically "stuff" an extra bit of the opposite polarity into the stream.

For example, if the data to be sent is: 111110

The transmitter detects five 1s and inserts a 0 immediately after: 11111 + 0 + 0

The resulting stream becomes 1111100. Because we forced a transition (the 0), the receiver is guaranteed to see a signal edge, allowing it to re-align its internal timer.

Receiver Logic

The receiver is aware of this rule. When it processes the incoming data, it performs "de-stuffing":

  1. It reads the stream and counts identical bits.
  2. If it encounters five identical bits, it knows the next bit must be a "stuffed" bit added by the sender.
  3. The receiver discards this extra bit and restores the original data for the application layer.

This mechanism ensures that the physical layer always provides enough "edges" for the receiver to stay locked onto the sender's timing, even if the data itself contains long sequences of the same logic level.

Key Takeaway: Bit stuffing prevents clock drift by forcing signal transitions into the data stream, ensuring the receiver maintains synchronization even when sending long sequences of identical bits.