GPIO (General Purpose Input/Output)

What is it?

  • Digital pins configurable as input or output.
  • Used to interface with LEDs, buttons, relays, sensors, etc.
  • Can be basic (binary) or advanced (with alternate functions: PWM, ADC, etc.).

Why we need it

  • Direct control of and communication with external hardware.
  • Foundation of all microcontroller (MCU) hardware interfacing.

Hardware Details

  • Each pin connected to MCU’s internal bus via multiplexers and latches.
  • Input: Reads external voltage (logic HIGH/LOW), supports pull-up/pull-down resistors.
  • Output: Drives voltage (sourcing/sinking current).
  • Advanced: Interrupt-on-change, open-drain, Schmitt trigger input, analog capabilities.
  • Electrical characteristics: max current, voltage levels (e.g., 3.3V, 5V tolerant), ESD protection.

Software/Embedded Use

  • Register-based or HAL library APIs to set direction, state, and read value.
  • Edge/level-triggered interrupts (button debounce, wake-on-event).
  • Bit-banging for low-speed serial protocols.

Use Cases

  • Toggle LEDs, read switches, drive buzzers, multiplex displays.
  • Control relays, interface with low-pin-count devices.
  • Bit-banged protocols: 1-Wire, custom serial.

Advanced Usage

  • PWM for motor control or dimming LEDs.
  • Analog input (ADC) or output (DAC) multiplexed on some GPIOs.
  • Port expanders (e.g., MCP23017 over I2C/SPI) for more GPIOs.

UART (Universal Asynchronous Receiver/Transmitter)

What is it?

  • Asynchronous serial communication interface: full duplex (TX/RX).
  • Transfers data in frames (start bit, 5-9 data bits, optional parity, stop bit).
  • No shared clock line—uses start/stop bits and agreed baud rate.

Why we need it

  • Simple, low-cost, widely supported point-to-point communication.
  • Used for debug consoles, sensor/MCU communication, GPS, Bluetooth modules.

Hardware Details

  • Two lines: TX (transmit), RX (receive), optional RTS/CTS for flow control.

  • Baud rates: standard (9600, 115200 bps), can be custom.

  • Internal shift registers, baud rate generator, status registers (parity error, framing error, buffer status).

  • Electrical standards:

    • TTL UART (0-3.3V or 0-5V logic).
    • RS-232 (±12V signaling for PC serial ports).
    • RS-485 (differential signaling for long-distance, multi-drop).

Software/Embedded Use

  • Blocking/polling, interrupt-driven, or DMA-based drivers.
  • Circular buffers for efficient RX/TX.
  • Bootloaders, debug logs, wireless (HC-05/ESP8266), or PC terminal communication.
  • Protocol stacks on top: SLIP, Modbus, custom packets.

Use Cases

  • Debug console (printf over UART).
  • Firmware upgrade over serial (XMODEM, YMODEM).
  • Communication with GSM/Bluetooth/WiFi modules.
  • GNSS (GPS) receivers, RFID readers.

Advanced Usage

  • Multi-processor communication using address frames.
  • Software UART (“bit-banged”) for MCUs with limited hardware UARTs.
  • Parity/error checking and flow control for reliability.

SPI (Serial Peripheral Interface)

What is it?

  • Synchronous serial bus for high-speed, short-distance communication.
  • Master-slave topology; single master, multiple slaves.
  • Four main signals:
    • SCLK (clock), MOSI (master out, slave in), MISO (master in, slave out), SS (slave select, active low).

Why we need it

  • Fast, reliable, low-latency communication with sensors, flash memory, displays, ADC/DACs.
  • Full-duplex, much faster than UART/I2C.

Hardware Details

  • Shift registers move data on clock edges (CPOL/CPHA—clock polarity/phase).

  • Speeds: MHz range (typical 1-50 MHz, some up to 100+ MHz).

  • Each slave needs separate SS/CS line.

  • No defined protocol—device-specific commands and frames.

  • Electrical: Supports multiple voltage levels, can interface via level shifters.

  • Daisy-chaining possible but rare.

Software/Embedded Use

  • Register-based or HAL library SPI drivers.
  • Configure data mode (clock polarity/phase), bit order (MSB/LSB first), speed.
  • Blocking/interrupt/DMA modes for data transfer.
  • Drivers implement higher-level protocols (e.g., SD card, display drivers).

Use Cases

  • Interfacing with Flash memory (W25Q, AT45DB), LCD/OLED screens, touch controllers.
  • Reading sensors: ADCs, accelerometers, gyros.
  • Communicating with wireless modules, SD cards.
  • Daisy-chained devices: shift registers (74HC595), LEDs (WS2812 with custom timing).

Advanced Usage

  • Multi-master/multi-slave via careful bus arbitration (not standard).
  • Use of SPI expanders/multiplexers for many peripherals.
  • Protocol converters: SPI-to-UART, SPI-to-I2C bridges.

I2C (Inter-Integrated Circuit)

What is it?

  • Synchronous, half-duplex, multi-master, multi-slave serial bus.
  • Two bidirectional lines: SDA (data), SCL (clock), pulled up to Vcc.
  • Each device has a 7/10-bit unique address.

Why we need it

  • Easy two-wire communication for connecting many devices.
  • Ideal for sensors, RTCs, EEPROMs, configuration ICs.

Hardware Details

  • Open-drain drivers, require pull-up resistors on both lines (typically 4.7k–10kΩ).

  • Bus speed: Standard (100 kHz), Fast (400 kHz), Fast+ (1 MHz), High-speed (3.4 MHz).

  • Protocol: Start, address+R/W, ACK/NACK, data, stop.

  • Arbitration: Multi-master support (bus can be shared without collisions).

  • Electrical: Can use level shifters for voltage translation, limited by bus capacitance (max ~400pF).

Software/Embedded Use

  • HAL/LL drivers or bit-banged software I2C.
  • Functions: start/stop, send/receive byte, check ACK, error handling.
  • Interrupt-based or DMA for efficiency.
  • Implement higher-level drivers: EEPROM, temperature/humidity sensors, RTCs.

Use Cases

  • Connecting multiple sensors (e.g., MPU6050, BME280) to one MCU.
  • External I2C EEPROM/Flash, port expanders.
  • Display modules (OLED, LCD), configuration ICs (PMICs, clock generators).

Advanced Usage

  • Bus recovery (in case of hung slaves).
  • Clock stretching (slower devices hold clock low).
  • SMBus (System Management Bus) is a stricter I2C superset used in PCs.

Summary Table

InterfaceWiresTopologySpeedData Dir.ProtocolUse CasesProsCons
GPIO1Point-to-pointFastIn / OutNoneLEDs, buttons, relaysSimple, flexibleNo protocol, no data rate
UART2Point-to-point300 bps–1 MbpsFull-duplexFramed (async)Debug, modulesUbiquitous, simpleNo addressing, short range
SPI3–4+Master–slave1–100+ MHzFull-duplexDevice-specificSensors, Flash, LCDsFast, simple hardwareMore wires, per-device CS
I²C2Multi-master/slave100 kHz–3.4 MHzHalf-duplexAddressedSensors, EEPROM, RTCsMulti-device, 2 wiresSlower, bus capacitance limit

System Integration and Security

Hardware

  • Level shifters: interface 5V/3.3V devices.
  • Bus resistors: I2C pull-ups, SPI terminations.
  • ESD protection, shielding for noisy environments.

Software

  • RTOS drivers: task-safe peripheral access.
  • Device trees or HAL layers for abstraction.
  • Bus arbitration and error recovery for robustness.

Security

  • Firmware validation for bootloaders over UART/I2C.
  • Encryption for sensitive data transmission (custom, or protocol-layer).