Serial Interfaces

Note: in the following, master/slave *terminology *(M/S) is replaced with controller/peripheral (C/P).

Synchronous VS asynchronous

When microcontrollers communicate, they need to agree on timing, as they each (presumably) run on their own clock. This timing information can be implicit or explicit, leading to two different strategies:

  • Synchronous: both parties remain in sync, usually thanks to an extra transmission line providing clock ticks (when to sample the data).

  • Asynchronous: next data block could arrive any time, and is delimited by a start marker, and possibly stop marker (unless data length is known beforehand).

source: https://en-support.renesas.com/knowledgeBase/17943143

Another strategy is to embed the clock information within the data, as achieved by Manchester code; this is an example of isochronous protocol.

source: https://en.wikipedia.org/wiki/Self-clocking_signal

Half Duplex VS full Duplex

Either both parties can transmit at the same time in a dedicated channel (full duplex), or need to share the same channel and transmit in alternation (half duplex). Full duplex can always achieve faster transmit speeds as long as flow control is carefully designed.

Error detection

Checking for data integrity can be implemented in almost any of the OSI layers, but the datalink layer is a good place to start. Adding metadata to a message can achieve two things:

  • Error detection: easily implemented with a checksum, this lets the receiving end simply drop the message in case of inconsistency. When the protocol is based on acknowledgements, data can be easily retransmitted until the ACK is received.
  • Error correction codes: introduce enough redundancy in the message to recover from sparse errors. The amount of overhead can be designed to guarantee a specific error resilience.

source: http://alanclements.org/errordetectingcodes.html

UART

The asynchronous, full-duplex, 8-bit word protocol that we all know and love. The transmission line is normally HIGH, then a start bit drives the line LOW, followed by 8-bits of data. After this, there is usually 1 stop bit or more, and an optional parity bit to help with error detection.

I2C

I2C is a half-duplex open-drain, synchronous protocol for communication between multiple controllers and multiple peripherals. An pull-up resistor is needed somewhere on the line, as it can only be pulled down by the open-drains.

source: https://www.robot-electronics.co.uk/i2c-tutorial

SPI

SPI is a synchronous and full-duplex protocol for single-controller and multiple peripherals. Notable examples of SPI peripherals include:

  • flash/EEPROM
  • SD cards

source: https://maker.pro/custom/tutorial/an-introduction-to-spi-communications-protocol

SPI typically has a single data line in each of the MOSI and MISO directions, but may have more in parallel (e.g. QSPI) for accelerating transfers to flash chips.

CAN, LIN

Both of these robust protocols are popular in the automotive industry. The use of a single bus to control almost all functionalities of the car was a significant simplification for manufacturing. LIN is simpler to implement as it requires a single communication cable, and is now used for non-critical peripherals.

RS232, RS422 and RS485

Those interface protocols define the physical layer for basic serial communication. In brief, RS232 is full-duplex, while RS485 is half-duplex and makes use of a balanced pair, more robust against interference. RS422 takes the best of both worlds with full-duplex balanced pairs.

source: https://www.optcore.net/difference-between-rs-232-rs-422-and-rs-485/

USB

Universal Serial Bus (USB) dominates when it comes to interfacing with consumer electronics. Up to USB 2.0, it comprised 4-pins: 5V/GND, and a half-duplex differential line D+/D-. USB 3.0 introduced faster rates and more pins to enable full-duplex.

There is a long list of device classes supported by USB. The most relevant ones here are:

  • Communications Device Class (CDC): emulates a serial line over USB, with a fictive baudrate and settings (useful for implementing your own USB serial adapter).
  • Human Interface Device (HID): keyboard, mouse, etc.
  • Mass Storage Device: detected by the OS as a storage device, usually for interfacing with flash memory or a hard drive.

A good place to start playing with USB is the SAMD microcontroller family, as it has hardware support for USB.

Other serial protocols

  • I2S for audio