What is bit stuffing in CAN, and what problems would occur without it?
Bit stuffing is a data encoding technique where the transmitter inserts a complementary (opposite polarity) bit after every sequence of 5 consecutive bits of the same value. The receiver recognizes and removes these stuff bits to recover the original data. Stuffing applies to most of the frame — from SOF through the CRC sequence — but not to the fixed-format fields (CRC delimiter, ACK field, EOF), which have defined bit patterns.
Without bit stuffing, CAN would fail for two fundamental reasons:
Clock synchronization would be lost. CAN nodes have independent oscillators and synchronize by detecting edges (transitions between dominant and recessive) in the data stream. If the data contained a long run of identical bits (e.g., 20 consecutive zeros), there would be no edges for 20 bit times. During this time, the receiver's oscillator drifts relative to the transmitter's oscillator. Even a 0.5% frequency difference causes a 0.1-bit-time error after 20 bits — enough to shift the sample point into the wrong bit. Bit stuffing guarantees an edge at least every 6 bits (5 data bits + 1 stuff bit), limiting the maximum drift to about 0.03 bit times at 0.5% oscillator tolerance.
Error detection would be weaker. The stuff error mechanism provides an additional layer of error detection that catches bus faults and clock problems. If 6 consecutive same-polarity bits appear, it means either the data was corrupted or a stuff bit was lost — both indicating a fault. Removing bit stuffing would eliminate this mechanism entirely.
The cost of bit stuffing is increased frame length variability. In the best case (alternating data), no stuff bits are inserted and the frame length is minimal. In the worst case (all zeros or all ones in the data field), a stuff bit is inserted every 5 bits, increasing the frame length by up to 20%. This variability must be accounted for when calculating bus load and worst-case latency.
Source: CAN Q&A
