0
\$\begingroup\$

I read in few places, one of them is this, that one of the main differences between UART and SPI is that the former is a type of hardware and the latter is a protocol.

I'm having hard time to fully grasp this notion. This is briefly what I got so far from the reading material I found googling, embedded with some questions:

UART

UART is a piece of hardware used to communicate between two components. The UART converts parallel data that it receives from a data bus into a serial stream of bits that are transferred across only one wire and vice versa. A UART uses only 2 wires - one for receiving and one for transmitting. The rest of the details aren't important for the manner of this discussion.

SPI

SPI is a synchronous (since it uses a clock) communication protocol used between devices.
This protocol uses 4 wired (if only one slave is present):

  • MOSI (Master Out/Slave In) - used to transmit data from master to slave
  • MISO (Master In/Slave Out) - used to transmit data from slave to master
  • SCLK - (Clock) - used to synchronize the data transmission.
  • SS/CS - (Slave Select/Chip Select) - When pulled low, signals the chip to communicate.

Unlike UART, it transfers bits in a stream not divided to packets (Here confusion begins - it's compared to UART). Devices communicating via SPI are in master-slave relationship. Master is the controlling device - usually a microcontroller - while the slave - usually something like a display, sensor or memory chip - takes instructions from the master. (Sanity check - When we say that the master controls the slave, we merely mean the selection of the slave by the SS/CS wire and the clock signaling?)

Differences between UART and SPI

  • Number of wires - UART uses 2, SPI uses at least 4
  • No start/stop bits - data is sent continuously without interruptions.
  • Number of devices - UART can have only 2, SPI can virtually connect unlimited number of slaves to one master
  • Hardware vs Protocol - UART is a type of hardware while SPI is a protocol.

Finally - the question

What I'm mainly confused about is this last difference - Hardware vs Protocol. It seems that these two communication schemes are compared side-to-side in the same parameters (Number of pins, start/stop bits, Speed, etc.) - you can't compare 2 essentially different entities based on the same parameters right?

Is the difference is that UART is a dedicated piece of hardware and SPI uses general GPIOs of the microcontroller?

\$\endgroup\$
6
  • 1
    \$\begingroup\$ I wouldn't call SPI a protocol, for me UART, SPI and I²C are all just physical layers for serial communication. But then the word protocol pops up in the SPI articles and not in the UART ones. For me a protocol is how the bytes have to be arranged to make sense for the communication. The physical layer just has to make sure to get them across... But that's only my opinion. \$\endgroup\$
    – Arsenal
    Apr 1 at 8:52
  • \$\begingroup\$ A UART uses an Asynchronous Serial Communication protocol, so if you want to compare apples with apples, compare Asynchronous Serial Communication with SPI. en.wikipedia.org/wiki/Asynchronous_serial_communication Unfortunately, neither of these is a strict standard. \$\endgroup\$
    – Mattman944
    Apr 1 at 9:15
  • 2
    \$\begingroup\$ Communication Protocol is just a set of rules to communicate between two entities. UART has set of rules (for eg: start bit should be there .... etc). SPI has set of rules (for eg: Data should be launched on this edge, sampled on that edge etc). So they both are protocols. \$\endgroup\$
    – Mitu Raj
    Apr 1 at 10:22
  • 1
    \$\begingroup\$ The most important thing for you to remember is that "UART" and "RS-232" are not the same thing. \$\endgroup\$ Apr 1 at 11:30
  • 2
    \$\begingroup\$ @Arsenal but UART, SPI, I2C are not physical layers. They are logical protocols how bits are transmitted. I2C only one that also defines a physical layer, but UART and SPI do not, they are just logical way of transmitting bits. They simply use standard TTL or CMOS logic IO as the physical layer. You can use UART with many different physical layers by using a PHY chip such as RS232, RS485, optocoupler like MIDI, CAN or fiber optic to build your interface. \$\endgroup\$
    – Justme
    Apr 1 at 11:55
4
\$\begingroup\$

No, UART is not necessarily a piece of hardware, nor SPI. You can implement both in software on a device that has no hardware UART or SPI. Commonly both are found inside MCUs as hardware peripherals.

A UART simply means Universal Asynchronous Receiver Transmitter. It uses the asynchronous start stop framing protocol to transmit data symbols with selectable number of bits and parity bit for error checking. For simplicity, let's assume it sends 8-bit data symbols that are called bytes. So an UART simply provides an asynchronous serial interface at the bit level.

SPI was originally a simple hardware peripheral to interface such as TTL chips that had clocked serial interfaces. It simply off-loaded byte transmission from software to hardware, with selectable clock polarity and phase setting. So an SPI provides simply a byte-oriented synchronous serial interface at the bit level.

So in the end, they both are protocols how to send bits and bytes.

But that is different from a protocol that defines what those bytes mean. For example an SPI Flash chip defines a protocol how to use the chip select to delimit commands and which bytes are commands and which are data, and how they are used to read and write the Flash memory over SPI bus at high level. Same goes with for example YModem protocol which defines what those bytes mean to be able to send files over UART at high level.

\$\endgroup\$
0
1
\$\begingroup\$

Most of your statements are wrong and there's lots of misconceptions here...

one of the main differences between UART and SPI is that the former is a type of hardware and the latter is a protocol.

No that's nonsense. Neither is a protocol. They are both physical and data link layer standards. Consult the "OSI model" which is taught in any data communication beginner class.

I'm having hard time to fully grasp this notion.

No wonder since it's incorrect nonsense...

UART is a piece of hardware used to communicate between two components.

Rather, there is the UART bus - the data link layer - and the physical UART component, which is used to communicate over that bus. Back in the days when dinosaurs walked the earth, there was a component called 16550, developed by National Semi, which was "the UART" and placed in every PC computer.

Similarly, you need a SPI hardware peripheral. Even though it's basically just a glorified shift register attached to a bunch of signals.

The UART converts parallel data that it receives from a data bus

How you address the UART depends on the system. On modern microcontrollers, this is handled internally and we don't need to worry how. It might be parallel, it's about how the MCU is designed got nothing to do with UART.

SPI is a synchronous (since it uses a clock)

Rather, it is synchronous since it uses a clock signal together with the data. UART clocks its data too, but there may be infinite delays between data bytes. The terms synchronous vs asynchronous aren't really that helpful, it's mainly just buzzwords.

Unlike UART, it transfers bits in a stream not divided to packets

No, just like UART it transfers a minimum of 8 bits. If there's some manner of higher layer protocol present, it dictates packet formats. But that's not something present on the data link layer, but rather at the application layer.

Sanity check - When we say that the master controls the slave, we merely mean the selection of the slave by the SS/CS wire and the clock signaling?)

It means that it controls /SS as well as the clock. Thereby the master controls when data transmission starts and how fast it is. The slave just have to fall in line. And usually this means that the slave must have the data ready at all times, because it can often not tell when it is time to send it. There's lots of variations on this, since SPI isn't really that well standardized.

UART can have only 2

With higher layer standards such as RS-485, you can make larger networks. Usually "star networks" where all information passes a master, who then re-directs it. But you can have a manner of multi-drop network too, as long as two nodes don't send at the same time. It's possible but overall rather crude.

\$\endgroup\$
5
  • \$\begingroup\$ Protocol, unfortunately, can have a lot of meanings. en.wikipedia.org/wiki/Category:Physical_layer_protocols \$\endgroup\$
    – Mattman944
    Apr 1 at 13:20
  • \$\begingroup\$ @Mattman944 Well yes but the de facto meaning is a higher layer concept. Where you have sync bytes, size bytes, payload, checksum etc. On the data link layer we talk about frames and this is a widely standardized term, also part of the mentioned "OSI model". So on this lower level, we talk about UART frames, SPI frames, CAN frames or whatever. \$\endgroup\$
    – Lundin
    Apr 1 at 13:38
  • \$\begingroup\$ If you know where there is a table that maps SPI into the OSI model, it would be really helpful to the OP. \$\endgroup\$
    – Mattman944
    Apr 1 at 14:00
  • \$\begingroup\$ @Mattman944 SPI specifically is not often used with much in the way of higher layer protocols, since it is almost always on-board communication only. Mostly it's some component data sheet saying something like "send 2 bytes for the address, then 2 bytes for the data, using big endian". That's a higher layer protocol, even if very crude. UART on the other hand can have complex application tiers. \$\endgroup\$
    – Lundin
    Apr 1 at 14:15
  • 1
    \$\begingroup\$ I find the answer confusing. First you say neither is a protocol, and then you say both belong to data link and physical layers of ISO model. These layers of ISO model do define a protocol how bits or frames are transmitted between devices. So if there exist a mutual agreement between two devices how to send and receive bits, how is that not a protocol then? I do agree, UART is not a protocol, it is a device that implements the transmission and reception of data using asynchronous start stop protocol. \$\endgroup\$
    – Justme
    Apr 1 at 17:52
0
\$\begingroup\$

UART stands for Universal asynchronous receiver-transmitter, which Wikipedia defines as A universal asynchronous receiver-transmitter is a computer hardware device for asynchronous serial communication in which the data format and transmission speeds are configurable.

Justme, perhaps refers to a serial connection, which can be over a UART or also over software.

The UART on the hardware level is a system that will serialize data and send them bit per bit on a line. By itself, it does not implement any protocol, either hardware or software, and can be used with transceivers to and protocols to generate buses like RS232, RS485, MODBUS, DMX and so forth.

UART is often used to create those buses to then communicate between different systems. It is also sometimes used to communicate with hardware modules, like it is often the case with Bluetooth modules.

UART sends data without a clock line and requires the devices to have the same baud rate, which requires configuration on both sides and also requires an oscillator (clock of some sort).

UART does not have a slave/master side but can be implemented in software.

SPI (Serial Peripheral Interface) by itself also is a hardware implementation and does not contain software protocol. It does have some set of rules, like the data lines, and an enable line, and a clock at the minimum.

It also considers that there are a master and a slave, the master controlling the clock and the enable line.

SPI does not have rules for the logical level of the lines, thus some chips may use different logical levels.

The data format sent over SPI is not specified either and is implemented by software depending on what it is communicating with.

SPI is usually used within a PCB to communicate with other ICs.

Because the SPI has a clock line, there is no need for configuration on the slave side which also means the slave doesn't need to have an oscillator.

There are also some buses that are based on SPI that defines more rules like SPI_RX or some other that are manufacturer dependent.

SPI is used mostly for inter-chip communication within a PCB.

UART is mostly used for communication over cables, but sometimes also with some modules within a PCB.

SPI and UART on their own have arguably similar speeds. Uart usually operates at a slower speed because it is used over longer distances and because of the oscillator frequency tolerances while SPI is not dependent on clock synchronization.

\$\endgroup\$
2
  • \$\begingroup\$ I don't think it's helpful to mix up for example RS-232 and Modbus. RS-232 etc are physical extensions to UART, while Modbus is an application layer on the very top. \$\endgroup\$
    – Lundin
    Apr 1 at 13:41
  • 1
    \$\begingroup\$ To transfer data between two UARTs, they have to both agree on how data is serialized between them, and if an agreement how to serialize data between two devices is not a protocol, what is it? I would say the UART implements a certain protocol how to serialize the data, the asynchronous start stop protocol. \$\endgroup\$
    – Justme
    Apr 1 at 18:04
-3
\$\begingroup\$

UART was born in the '70s while SPI in the 90's.

Both the interfaces UART and SPI have a plethora of CMOS transistors inside therefore they are hardware interfaces. The whole lot of these CMOS transistors is called "hardware controller".

UART has 2 pins: TX, RX

SPI has 4 pins: TX, RX, CLOCK, ENABLE

They both trasmit and receive one byte at a time, that is one after the other.

SPI is much faster than UART because bits are written and read at the rising or falling edge of the CLOCK signal. You speed up the clock rate, you speed up communications.

UART has no clock support therefore the receiving device must generate a local clock signal which can't be synchronous to the transmitting one. That means that the clock rate can't be too high and communications are pretty slow.


To answer your question:

A protocol, in computer science is a set of rules.

Hardware in electronics is a set of transistors, diodes, resistors, etc, interconnected with each other.


Hardware is designed in order to run protocols.

\$\endgroup\$
9
  • \$\begingroup\$ Async serial was around way before the 70’s and spi was available as a hardware peripheral in the 80’s. \$\endgroup\$
    – Kartman
    Apr 1 at 10:53
  • \$\begingroup\$ I think your years are off by at least a decade or two. First UART as an IC may have been available early 70s, but before that UARTs were built from discrete logic gates and the first ones were electromechanical devices. SPI as a MCU peripheral was first introduced in the 80s, but it changed very little as synchronous serial comms were already a thing. \$\endgroup\$
    – Justme
    Apr 1 at 11:47
  • 4
    \$\begingroup\$ You have used non-standard names for SPI pins, and using TX and RX is particularly confusing because these pin names have different meaning than they do for a UART. \$\endgroup\$ Apr 1 at 12:50
  • \$\begingroup\$ Indeed. SPI isn't standardized but the de facto standard names are MOSI, MISO and /SS (inverted) and those should be used. The clock can be called different things, SCLK etc. \$\endgroup\$
    – Lundin
    Apr 1 at 13:43
  • 1
    \$\begingroup\$ @EnricoMigliore Neither UART or SPI speed is limited by anything except the implementation. I have used UART comms with PC at 3 Mbps for years, mostly because it was the fastest rate the USB serial port chip was capable of. Interfaces that use UARTs like RS-485 or RS-422 are typically thought to go up to 10 Mbps for many meters. SPI can go quicker, but as it is synchronous, it has the problem of having the master wait for the time of flight for the clock synchronized data back from the slave. So you can't have a 75 MHz SPI bus unless the distance is extremely small, like few centimeters. \$\endgroup\$
    – Justme
    Apr 1 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.