Buses/protocols for communication

Shashi ShekharShashi Shekhar
5 min read

Embedded firmware relies on several communication buses to enable interaction between microcontrollers, sensors, memory, and other peripherals. These buses serve as pathways to transfer data between master and slave devices. The most common buses in embedded systems include I2C, SPI, and UART. Below is an explanation of these buses and the associated protocols.


1. UART (Universal Asynchronous Receiver Transmitter)

Overview

  • UART is a serial, asynchronous communication protocol.

  • Used for point-to-point communication (one master, one slave).

  • Data is transmitted without a clock signal; both devices must agree on baud rate (bits per second).

Key Features

  • Asynchronous: No clock signal; uses start/stop bits for synchronization.

  • Full-duplex: Transmit (TX) and Receive (RX) occur simultaneously using two separate lines.

  • Baud Rate: Determines transmission speed (e.g., 9600, 115200 bps).

Communication Lines

  • TX (Transmit): Sends data to the receiving device.

  • RX (Receive): Receives data from the transmitting device.

  • GND (Ground): Common ground reference for both devices.

Frame Format

  • Data transmitted as frames: | START | DATA (8 bits) | PARITY (optional) | STOP |

  • Start bit: Indicates the beginning of a frame (logic '0').

  • Data bits: Typically 8 bits (1 byte).

  • Parity bit: Optional error-checking bit.

  • Stop bit: Signals the end of the frame (logic '1').

Advantages

  • Simple and widely supported.

  • Requires only 2 wires (TX, RX) for basic communication.

Disadvantages

  • No error correction; limited to simple error detection.

  • Only one-to-one communication (no multi-slave support).

  • Requires matching baud rates between devices.

Use Cases

  • Debugging firmware via serial communication.

  • Communication with sensors, GPS modules, and PCs.


2. I2C (Inter-Integrated Circuit)

Overview

  • I2C is a synchronous, multi-master, multi-slave serial communication protocol.

  • Uses only 2 wires for data transfer, making it ideal for systems with multiple devices.

Key Features

  • Synchronous: Uses a shared clock signal (SCL).

  • Multi-Master: Multiple masters can control the bus.

  • Addressing: Each slave device has a unique 7-bit or 10-bit address.

  • Data transfer rates: Standard mode (100 kbps), Fast mode (400 kbps), Fast-mode Plus (1 Mbps).

Communication Lines

  1. SDA (Serial Data): Bi-directional line for data transfer.

  2. SCL (Serial Clock): Clock line generated by the master to synchronize communication.

Data Transfer Sequence

  1. Start Condition: Master pulls SDA low while SCL remains high.

  2. Addressing: Master sends the 7-bit slave address and a Read/Write (R/W) bit.

  3. ACK/NACK: Slave acknowledges (ACK) or does not acknowledge (NACK).

  4. Data Transfer: Data is sent 1 byte at a time, with acknowledgment after each byte.

  5. Stop Condition: Master releases SDA and SCL.

Advantages

  • Requires only 2 wires, saving GPIO pins.

  • Supports multi-master and multi-slave configurations.

  • Addressing allows communication with multiple devices on the same bus.

Disadvantages

  • Slower compared to SPI (limited to ~1 Mbps).

  • Requires pull-up resistors for SDA and SCL lines.

  • Limited cable length due to capacitance.

Use Cases

  • Communication with sensors (e.g., temperature, accelerometer).

  • EEPROM (Electrically Erasable Programmable Read-Only Memory).

  • Real-time clocks (RTC).


3. SPI (Serial Peripheral Interface)

Overview

  • SPI is a synchronous, full-duplex communication protocol.

  • Supports one master and multiple slaves.

  • Faster than I2C but requires more wires.

Key Features

  • Synchronous: Uses a shared clock signal (SCK).

  • Full-Duplex: Data is transmitted and received simultaneously.

  • Higher speed than I2C, with data rates up to tens of MHz.

Communication Lines

  1. MOSI (Master Out Slave In): Data sent from master to slave.

  2. MISO (Master In Slave Out): Data sent from slave to master.

  3. SCK (Serial Clock): Clock line generated by the master.

  4. CS/SS (Chip Select/Slave Select): Used to select the specific slave device.

Data Transfer Sequence

  1. Master pulls the CS line low to select the slave.

  2. Master generates the clock on SCK.

  3. Data is shifted in/out on MOSI and MISO lines, synchronized with the clock.

  4. After communication, master pulls the CS line high to deselect the slave.

Advantages

  • Very high-speed communication.

  • Simple protocol with minimal overhead.

  • Supports full-duplex communication.

Disadvantages

  • Requires more wires than I2C (MOSI, MISO, SCK, CS).

  • No standard addressing mechanism for slaves.

  • Only one master can control the bus.

Use Cases

  • Communication with high-speed devices like:

    • Displays (LCD, OLED).

    • Flash memory.

    • Sensors (gyroscope, ADCs).

    • Wi-Fi or Ethernet modules.


Comparison Between I2C, SPI, and UART

FeatureUARTI2CSPI
TypeAsynchronousSynchronousSynchronous
Wires Required2 (TX, RX)2 (SDA, SCL)4+ (MOSI, MISO, SCK, CS)
SpeedUp to 1 MbpsUp to 1 MbpsTens of MHz
Multi-MasterNoYesNo
Multi-SlaveNoYesYes
Error DetectionLimited (parity bit)ACK/NACK mechanismNone
ComplexitySimpleModerateSimple

Protocols Used in Firmware

Firmware uses these buses to implement higher-level protocols. For example:

  1. I2C Protocol:

    • Devices like EEPROM, RTC, and sensors use I2C for addressing and communication.
  2. SPI Protocol:

    • Flash memory chips, ADCs, and display drivers use SPI for fast data transfer.
  3. UART Protocol:

    • Debugging output, serial communication, and communication with GPS or GSM modules.

These protocols help firmware interact with peripherals efficiently.


Choosing Between I2C, SPI, and UART

  • Use I2C when:

    • Multiple devices need to share the same bus.

    • You have limited pins available.

  • Use SPI when:

    • Speed is critical, and you have enough pins for MOSI, MISO, SCK, and CS.

    • Point-to-point or multi-slave communication is required.

  • Use UART when:

    • Simple serial communication (e.g., debugging, logging) is needed.

    • Only two devices are communicating.


Summary

  • UART is asynchronous and ideal for simple, point-to-point communication.

  • I2C is a multi-master, multi-slave protocol that is pin-efficient but slower.

  • SPI offers fast, full-duplex communication but uses more pins.

Understanding these buses and their protocols is crucial when working on firmware development in embedded systems, as it allows seamless interaction between the software and hardware components

0
Subscribe to my newsletter

Read articles from Shashi Shekhar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shashi Shekhar
Shashi Shekhar