Catto / Topic Map · Digital Techniques II Level 4
Digital Techniques II · 120 h · Topic 5 of 6

Serial communications and protocols

A CAN frame carries at most 8 data bytes and 47 control bits. That is why, even at 1 Mbit/s, a car’s bus carries no more than about 72 kB/s of useful data.

UART SPI I²C CAN Line coding

01Why almost everything is serial

For decades data traveled in parallel: eight, sixteen or thirty-two lines at a time. At high speed that stops working: the bits of a single data word arrive with timing differences because of the different lengths of the traces, and every line that switches adds noise to its neighbors. A single differential line, properly matched, reaches tens of gigabits per second today. That is why USB, SATA, PCI Express and Ethernet replaced their parallel predecessors, and on the board, two or three wires of a serial bus replace an entire data bus.

The fundamental difference is how the receiver knows when to read each bit. In a synchronous link a separate clock travels along; in an asynchronous one, the two ends agree on the speed and synchronize with a start bit; in fast links, the clock is embedded in the data and the receiver recovers it. The circuits are presented in the technician program, in serial communication and serial buses.

02Inside the board: UART, SPI and I²C

UARTSPII²C
LinesTX and RXSCLK, MOSI, MISO and one select per slaveSDA and SCL, open-drain with pull-up resistors
ClockAsynchronous, agreed speedSynchronous, generated by the masterSynchronous, generated by the master
Typical speed9600 to 1 Mbit/s1 to 50 MHz100 kHz, 400 kHz or 1 MHz
DevicesTwoOne per select lineUp to 127, by 7-bit address
OverheadStart and stop per byteNoneAddress and an acknowledge bit per byte

The UART tolerates a limited speed difference between the two ends: the receiver samples at the center of each bit counting from the start edge, and by the tenth bit the accumulated error cannot exceed half a bit. In practice, the sum of the errors of the two clocks must stay below about 4 %, which sometimes rules out the internal RC oscillator.

In I²C, the lines are pulled low actively and rise through the pull-up resistors. The rise time, which the standard limits, sets the maximum value of the resistor:

\[ R_{p,max} = \frac{t_r}{0.8473\; C_b} \] \( t_r \) is the rise time between 30 % and 70 % and \( C_b \) the total capacitance of the bus. At 400 kHz (\( t_r \le 300 \) ns) with 200 pF of bus, the resistor cannot exceed 1.8 kΩ. With more devices or longer cables, you have to lower the speed.

03Fieldbuses: CAN and RS-485

Outside the board, between pieces of equipment, buses use differential signals: two twisted wires that carry the signal and its opposite, so that noise that affects both equally is canceled at the receiver. RS-485 defines only the electrical layer, for up to 32 unit loads over 1200 m. CAN, designed for cars, adds a complete protocol:

  • Nondestructive arbitration. The 0 bit is dominant: if two nodes transmit at the same time, the one sending a 1 and reading a 0 backs off without losing anything. The lowest identifier wins, which is the one with the highest priority.
  • Bit stuffing. After five identical bits an opposite one is inserted, so that there are enough edges to resynchronize the clocks. The frame length depends on the data.
  • Error detection. 15-bit CRC, acknowledgment by all receivers, and counters that disconnect a faulty node.
Lab · the cost of a frame

\( n \) bytes are sent over the chosen protocol. The chart shows the first bits of the frame, with its fields (the first data byte is 0x4B), and below, how much of the frame is useful data. In CAN, the identifier is 0x123 and the real stuffing is calculated together with the CRC.

04High speed

Above a few hundred megabits per second, each link is a differential transmission line of controlled impedance, typically 90 or 100 Ω, terminated at the receiver. LVDS uses a swing of just 350 mV, which switches fast and consumes little. The clock does not travel separately: it is encoded in the data to guarantee transitions, and the receiver recovers it with a PLL.

LinkSpeed per laneEncodingEfficiency
USB 2.0 high speed480 Mbit/sNRZI with bit stuffingDepends on the data: in the worst case, one extra bit every six
PCI Express 2.0 and USB 3.05 Gbit/s8b/10b80 %
PCI Express 3.08 Gbit/s128b/130b98.5 %
Ethernet 10GBASE-R10.3 Gbit/s64b/66b97 %

The quality of a fast link is evaluated with the eye diagram: all the bits synchronized with the recovered clock are superimposed on the oscilloscope. An open eye means that the receiver tells zeros from ones well; noise, jitter and reflections close it.

05Where it appears in electronics

ApplicationTypical bus
Temperature sensor or accelerometer on a boardI²C or SPI
Display or external flash memorySPI, or QSPI with four data lines
Microcontroller debug consoleUART
Automobile, agricultural machineryCAN and CAN FD
Industrial automationRS-485 with Modbus, or industrial Ethernet
Graphics card, solid-state drivePCI Express

06In the lab

Activity 1 · Decoding frames

With a logic analyzer, capture an I²C transaction with a sensor and an SPI one with a memory. Identify the address, acknowledgments and data, and measure the real clock speed.

Activity 2 · Rise time in I²C

Measure the rise time of SDA with pull-ups of 10 kΩ, 4.7 kΩ and 1 kΩ, and verify which one meets the standard at 400 kHz.

Activity 3 · Two CAN nodes

Build a CAN bus with two microcontrollers and their transceivers, with 120 Ω terminations. Send frames with different identifiers at the same time and observe the arbitration.

07Common mistakes

  • Forgetting the I²C pull-ups, or adding too many. Each module brings its own, and in parallel they lower the resistance so much that the devices cannot pull the line low.
  • Mixing up the SPI mode. The clock polarity and phase have to match on master and slave; otherwise the data arrives shifted by one bit.
  • Not terminating a CAN or RS-485 bus. Without the resistors at the ends, reflections corrupt the bits.
  • Calculating the time of a CAN frame without the stuffing. In the worst case it adds 24 bits to a 111-bit frame: almost 22 %.

08Self-assessment

How long does it take to send one byte over 8N1 UART at 115,200 bit/s?

10 bits: 86.8 µs.

How many bits does writing 2 bytes to a device take in I²C?

Start, 9 address bits and 9 per byte, and stop: \( 9 \cdot 3 + 2 = 29 \).

Two CAN nodes transmit identifiers 0x120 and 0x123 at the same time. Which one wins?

0x120, the lower one: at the first bit where they differ it sends a dominant 0.

What efficiency does 8b/10b encoding have?

80 %: for every 8 data bits, 10 are transmitted.

09Further reading

  • NXP Semiconductors. UM10204: I²C-bus Specification and User Manual. Rev. 7, 2021. The I²C bus standard, with the timing, the speeds and the calculation of the pull-up resistors.
  • Wilfried Voss. A Comprehensible Guide to Controller Area Network. 2nd ed., Copperhill, 2005. CAN explained from the physical layer to error handling.
  • Howard Johnson and Martin Graham. High-Speed Signal Propagation: Advanced Black Magic. Prentice Hall, 2003. Fast serial links as a problem of transmission lines, losses and equalization.
Development of the topic “Serial communications and protocols” of Digital Techniques II (Level 4), based on the curriculum of the UTN Electronic Engineering program, 2023 curriculum — Ordinance No. 1849 of the UTN Higher Council. Back to the Topic Map · catto.ar