UART (Universal Asynchronous Receiver/Transmitter) and SPI (Serial Peripheral Interface) are both serial communication protocols that are commonly used in embedded systems and microcontroller applications. However, there are some key differences between the two:
Overview of UART
UART is an asynchronous serial communication protocol, meaning there is no clock signal to synchronize the sending and receiving devices. The data is transmitted one bit at a time.
The UART protocol consists of two data lines:
- TX – Transmit data
- RX – Receive data
To communicate, two UARTs connect to each other using their transmit and receive lines. The UART transmitting the data sends each bit at a predefined baudrate. The receiving UART samples the line at the same baudrate to recover the transmitted bits.
Some key characteristics of UART communication:
- Asynchronous – No clock signal
- Point to point – One transmitter, one receiver
- Full duplex – Transmit and receive can occur simultaneously
- No built-in addressing scheme
- Lower throughput than synchronous protocols like SPI
UART is commonly used for:
- Debug/console interfaces
- Communication between microcontrollers
- Interface to peripheral devices like GPS, barcode scanners, etc.
Overview of SPI
SPI (Serial Peripheral Interface) is a synchronous serial communication protocol. It uses separate clock and data lines for communication between a master device and one or more slave devices.
The SPI bus consists of 4 main lines:
- SCLK – Serial clock from master
- MOSI – Master out, slave in data
- MISO – Master in, slave out data
- SS/CS – Slave select/chip select
The master generates the SCLK clock signal to synchronize the data transfer. Data is transmitted between the MOSI and MISO lines. The SS/CS lines are used by the master to select individual slave devices on the bus.
Key characteristics of SPI communication:
- Synchronous – Clock signal provided by master
- Master/slave relationship – One master, multiple slaves
- Full duplex – Transmit and receive can occur simultaneously
- Higher throughput than asynchronous protocols like UART
- Built-in addressing scheme via SS/CS lines
SPI is commonly used for:
- Communication between microcontrollers and peripheral devices (sensors, SD cards, LCD displays, etc.)
- Flash memory and EEPROM interfacing
- Chip-to-chip communication in embedded systems
Key Differences Between UART and SPI
Here are some of the main differences between UART and SPI:
- Synchronicity – UART is asynchronous, SPI is synchronous
- Clock – UART has no clock, SPI has a clock signal from master
- Number of devices – UART is point to point, SPI supports multiple slaves
- Addressing – UART has no addressing, SPI uses SS/CS for slave addressing
- Speed – UART max speed is typically lower than SPI
- Complexity – UART is simpler to implement than SPI
- Distance – UART can communicate over longer distances than SPI
- Full duplex – Both UART and SPI support full duplex communication
UART Communication Protocol
Now let’s take a deeper look at how UART serial communication works:
UART Frame Format
UART data frames are sent as follows:
- Start Bit – Indicates start of data frame, always low
- Data Bits – Sent LSB first, common sizes are 5, 6, 7, 8 bits
- Parity Bit – Optional, for error checking
- Stop Bits – Indicates end of data, commonly 1, 1.5 or 2 bits
For example, a common frame format is:
- 1 start bit
- 8 data bits
- No parity
- 1 stop bit
UART Signal Levels
UART signals utilize two voltage levels:
- Logical 0: 0V to 0.8V
- Logical 1: 2.4V to VCC (5V)
The unused state sits at the midpoint between these at 1.2V to 1.8V.
UART Transmission
To transmit data, the UART line is held high at idle. The start bit pulls the line low to signal the start of data. The data bits are sent LSB first. The stop bit pulls the line high again to mark the end of data.
UART Reception
The UART receiver monitors the line continuously. A falling edge indicates the start of data. The receiver then reads in the data bits at the defined baud rate. When the stop bit is received the data frame is complete.
UART Flow Control
Because UART is asynchronous, flow control is important to prevent receiver buffer overrun.
Software flow control uses special ASCII control characters like Ctrl+S and Ctrl+Q. Hardware flow control uses additional CTS and RTS lines. CTS indicates clear to send while RTS is request to send.
SPI Communication Protocol
Now let’s look at the SPI serial protocol:
SPI Bus Topology
The SPI bus has a single master device which controls the SCLK line. It can have multiple slave devices which share the MOSI, MISO and SCLK lines. Each slave has its own SS/CS line for individual selection by the master.
SPI Signal Levels
SPI signals typically operate at 5V or 3.3V logic levels. Some key parameters are:
- Clock polarity (CPOL) – Clock can be idle high or low
- Clock phase (CPHA) – Data can be sampled on leading or trailing clock edge
- There are four SPI modes based on CPOL and CPHA
SPI Communication
To communicate, the master first selects a slave using the SS/CS line. Data transfer then occurs simultaneously on MOSI and MISO at the SCLK defined rate. The master generates the clock signal and each slave must synchronize to this clock.
The master and slave can both transmit and receive at the same time (full duplex). Each byte is sent MSB first.
SPI Modes
As mentioned earlier, there are four different SPI modes based on the polarity and phase of the clock signal:
- Mode 0 – CPOL = 0, CPHA = 0 – Clock starts low, data read on rising edge
- Mode 1 – CPOL = 0, CPHA = 1 – Clock starts low, data read on falling edge
- Mode 2 – CPOL = 1, CPHA = 0 – Clock starts high, data read on falling edge
- Mode 3 – CPOL = 1, CPHA = 1 – Clock starts high, data read on rising edge
The master and slave must use the same SPI mode for proper communication. Mode 0 is the most common mode.
Summary of Key Differences
In summary, the main differences between UART and SPI are:
- UART is asynchronous while SPI is synchronous
- UART has no master clock while SPI has a master-controlled clock
- UART supports only point to point, SPI can have multiple slaves
- UART has lower throughput than SPI
- UART communication is simpler than SPI
In practice:
- UART is commonly used for simple low speed communication like debug interfaces
- SPI is used where higher speed is required or multiple peripherals need to communicate with a microcontroller
Both protocols have advantages and disadvantages. For any application, factors like speed, throughput, complexity, cost, distance and communication requirements should be evaluated to select the best serial protocol.
Example Applications
Here are some examples of how UART and SPI are used in embedded systems and microcontroller applications:
UART Example Applications
- Debug console interface between a computer and microcontroller
- GPS module interfacing with navigation/tracking systems
- Barcode/RFID scanner interfacing with point of sale systems
- Serial communication between two microcontrollers
- Serial port based data transfer
- Infrared remote control interfacing
SPI Example Applications
- LCD TFT display interfacing with a microcontroller
- SD card communication in portable devices
- ADC/DAC interfacing with microcontrollers
- Communicating with multiple peripheral ICs
- Chip-to-chip communication in multi-chip modules
- Flash memory interfacing
Conclusion
In summary, UART and SPI are both common serial communication protocols used in embedded systems. UART is better suited for simple low-speed communication with just two devices connected over shorter distances. SPI is better where higher throughput, multiple peripherals and synchronization is needed.
The choice between UART vs SPI depends on the specific requirements of an application. Factors like speed, complexity, hardware availability, noise susceptibility and communication distance should be evaluated. For many applications, both these protocols can potentially be used with tradeoffs.