Finally figured out: the differences and applications of SPI, UART, and I2C communication!
Click on the blue " Linux " in the upper left corner and select " Set as Star "
Communication between electronic devices is like communication between humans, both parties need to speak the same language. In electronics, these languages are called communication protocols.
I have previously shared separate articles on SPI, UART, and I2C communication. This article will make some comparisons between them.
Serial VS Parallel
Electronic devices talk to each other by sending bits of data. Bits are binary and can only be 1 or 0. Bits are transferred from one device to another through rapid changes in voltage. In a system operating at 5V, a "0" communicates via a short pulse of 0V, while a "1" communicates via a short pulse of 5V.
Data bits can be transmitted in parallel or serial form. In parallel communication, data bits are transmitted simultaneously on a wire. The image below shows the parallel transmission of the letter "C" in binary (01000011):
In serial communication, bits are sent one by one over a single wire. The image below shows the serial transmission of the letter "C" in binary (01000011):
SPI communication
SPI is a common communication protocol for devices. It has a unique advantage that it can transmit data without interruption and can continuously send or receive any number of bits. In I2C and UART, data is sent in the form of data packets with a limited number of bits.
In SPI devices, the devices are divided into host and slave systems . The master is the control device (usually a microcontroller), and the slave (usually a sensor, display, or memory chip) obtains instructions from the master.
A set of SPI communication contains four signal lines: MOSI (Master Output/Slave Input) – signal line, host output, slave input. MISO (Master Input/Slave Output) – signal line, master input, slave output. SCLK (Clock) – Clock signal. SS/CS (Slave Select/Chip Select) – chip select signal.
SPI protocol features
In practice, the number of slaves is limited by the system load capacitance, which reduces the master's ability to accurately switch between voltage levels.
working principle
clock signal
One bit of data is transferred per clock cycle, so the speed of data transfer depends on the frequency of the clock signal. Since the clock signal is generated by the host configuration, SPI communication is always initiated by the host.
Any communication protocol in which devices share a clock signal is called synchronization. SPI is a synchronous communication protocol, and there are some asynchronous communications that do not use clock signals. For example, in UART communication, both sides are set to a preconfigured baud rate, which determines the speed and timing of data transfer.
chip select signal
The master enables communication by pulling the slave's CS/SS low. In the idle/non-transmitting state, the chip select line remains high . There can be multiple CS/SS pins on the master, allowing the master to communicate with multiple different slaves.
If the master has only one chip select pin available, these slave devices can be connected via:
MOSI和MISO
The master sends data to the slave in serial mode through MOSI, and the slave can also send data to the master through MISO, and both can be done at the same time. So in theory, SPI is a full-duplex communication protocol.
Transfer steps
1. Host output clock signal
2. The master pulls the SS/CS pin low to activate the slave
3. The master sends data to the slave through MOSI
4. If a response is required, the slave returns the data to the host through MISO
There are some advantages and disadvantages to using SPI, which should be fully considered based on project requirements if choosing between different communication protocols.
pros and cons
advantage
SPI communication has no start bits and stop bits, so data can be streamed continuously without interruption; there is no complex slave addressing system like I2C, and the data transfer rate is higher than I2C (almost twice as fast). Independent MISO and MOSI lines can send and receive data simultaneously.
shortcoming
SPI uses four wires (I2C and UART use two wires), there is no acknowledgment of successful signal reception (I2C has this feature), and there is no error checking of any kind (like parity bits in UART, etc.).
UART stands for Universal Asynchronous Receiver/Transmitter also known as serial communication, it is not a communication protocol like SPI and I2C, but a physical circuit in a microcontroller or a separate IC.
The main purpose of UART is to send and receive serial data, and its best advantage is that it uses only two wires to transfer data between devices. The principles of UART are easy to understand, but if you haven't read the SPI communication protocol, that might be a good place to start.
UART communication
In UART communication, two UARTs communicate directly with each other. The transmitting UART converts the parallel data of the control device (such as CPU) into serial form and sends it serially to the receiving UART. Only two wires are needed to transfer data between two UARTs, data flows from the Tx pin of the transmitting UART to the Rx pin of the receiving UART:
UART is asynchronous communication , which means there is no clock signal, instead start and stop bits are added to the data packet. These bits define the start and end of the packet, so the receiving UART knows when to read this data.
When the receiving UART detects the start bit, it will read it at a specific baud rate. Baud rate is a measure of data transfer speed, expressed in bits per second (bps). Both UARTs must operate at approximately the same baud rate, the baud rate between the transmitting and receiving UARTs can only differ by approximately 10%.
working principle
After the sending UART gets the parallel data from the data bus, it adds a start bit, a parity bit and a stop bit to form the data packet and output it serially bit by bit from the Tx pin, the receiving UART outputs it serially on its Rx pin Read the packet bit by bit.
UART data consists of 1 start bit, 5 to 9 data bits (depending on the UART), an optional parity bit, and 1 or 2 stop bits:
Starting bit:
UART data transmission lines typically remain at a high voltage level when not transmitting data. When starting a transmission, the transmitting UART pulls the transmission line from high to low in one clock cycle. When the receiving UART detects the high voltage to low voltage transition, it starts reading the data frame at the frequency of the baud rate. Bit.
Data Frame:
The data frame contains the actual data being transmitted. If using parity bits, it can be 5 bits, up to 8 bits. If parity bits are not used, the data frame can be 9 bits long.
Check Digit:
The parity bit is how the receiving UART determines whether any data changed during the transmission. After receiving the UART to read the data frame, it will count the number of bits with a value of 1 and check whether the total number is even or odd and matches the data.
Stop bit:
To signal the end of the packet, the sending UART drives the data transmission line from low voltage to high voltage for at least two bits.
Transfer steps
-
The sending UART receives data from the data bus in parallel:
2. Send UART to add start bit, parity bit and stop bit to the data frame:
3. The entire data packet is sent serially from the transmitting UART to the receiving UART. The receiving UART samples the data line at a preconfigured baud rate:
4. The receiving UART discards the start bit, parity bit and stop bit in the data frame:
5. The receiving UART converts the serial data back to parallel data and transmits it to the data bus at the receiving end:
pros and cons
No communication protocol is perfect, but UART is very good at its job. Here are some pros and cons to help you decide if they are right for your project needs:
advantage
-
Use only two wires
-
No clock signal required
-
Has a parity bit to allow error checking
-
As long as both parties set up the structure of the data packet
-
Well-documented and widely used methods
shortcoming
-
The size of the data frame is up to 9 bits
-
Multiple slave systems or multiple master systems are not supported
-
The baud rate of each UART must be within 10% of each other
I2C communication
The I2C bus is a simple, bidirectional two-wire synchronous serial bus developed by Philips. It only requires two wires to transmit information. It combines the advantages of SPI and UART. You can connect multiple slaves to a single host (like SPI), or you can use multiple hosts to control one or more slaves. This is useful when you want multiple microcontrollers to log data to a single memory card or display text to a single LCD.
SDA (Serial Data) – data line.
SCL (Serial Clock) – clock line.
I2C is a serial communication protocol, so data is transferred bit by bit along the SDA. Like SPI, I2C also requires a clock synchronization signal and the clock is always controlled by the host.
working principle
I2C data transmission is carried out in the form of multiple msg. Each msg contains the binary address frame of the slave, and one or more data frames. It also includes start conditions and stop conditions, read/write bits and data frames. ACK/NACK bits:
Start condition: When SCL is high level, SDA switches from high level to low level.
Stop condition: When SCL is high level, SDA switches from low level to high level.
Address frame: A unique 7-bit or 10-bit sequence for each slave device, used for address identification between master and slave devices.
Read/write bit: one bit, if the host is sending data to the slave, it is low level, and when requesting data, it is high level.
ACK/NACK: Each frame in the message is followed by an ACK/NACK bit. If the address frame or data frame is successfully received, the receiving device will return an ACK bit to indicate confirmation.
addressing
Since I2C does not have a chip select line like SPI, it needs to use another method to confirm a slave device, and this method is - addressing.
The master sends each slave the address of the slave it wants to communicate with, and each slave compares it to its own address. If the addresses match, it sends a low ACK bit to the host. If there is a mismatch, no action is performed and the SDA line remains high.
read/write bit
The end of the address frame contains a read/write bit. If the master wants to send data to the slave, it is low level. If the master requests data from the slave, it is high level.
Data Frame
When the master detects the ACK bit of the slave, it can send the first data frame. The data frame is always 8 bits, and each data frame is followed by an ACK/NACK bit to verify the reception status. When all data frames have been sent, the master can send a stop condition to the slave to terminate the communication.
Transfer steps
1. The host initiates bus communication by switching the SDA line from high to low while the SCL line is high.
2. The master sends the 7-bit or 10-bit address of the slave it wants to communicate with, along with the read/write bits:
3. Each slave compares the address sent by the master with its own address. If the addresses match, the slave returns an ACK bit by pulling the SDA line low one bit. If the master's address does not match the slave's address, the slave pulls the SDA line high.
4. The host sends or receives data frames:
5. After each data frame is transmitted, the receiving device returns another ACK bit to the sender to confirm that the frame was successfully received:
6. The master then switches SCL high and then SDA high to send a stop condition to the slave.
Single master VS multiple slaves
Since I2C uses the addressing function, multiple slaves can be controlled by one master. When using 7-bit addresses, up to 128 (27) unique addresses can be used. Using 10-digit addresses is uncommon, but provides 1,024 (210) unique addresses. If you want to connect multiple slaves to a single master, connect them using 4.7K ohm pull-up resistors, for example connecting the SDA and SCL lines to Vcc:
Multiple masters VS multiple slaves
I2C supports multiple masters connected to multiple slaves at the same time. Problems arise when two masters try to send or receive data at the same time through the SDA line. Therefore each host needs to detect whether the SDA line is low or high before sending a message. If the SDA line is low, it means another master is controlling the bus. If the SDA line is high, it is safe to send data. If you want to connect multiple masters to multiple slaves, connect the SDA and SCL lines to Vcc using 4.7K ohm pull-up resistors:
pros and cons
I2C may sound complicated compared to other protocols. Here are some pros and cons to help you decide if they are right for your project needs:
advantage
-
Use only two wires
-
Support multiple masters and multiple slaves
-
The baud rate of each UART must be within 10% of each other
-
Hardware is simpler than UART
-
Well-known and widely used protocol
-
shortcoming
-
Data transfer rate is slower than SPI
-
Data frame size is limited to 8 bits
Statement: This account remains neutral with respect to the statements and opinions of all original and reprinted articles. Articles posted are only for readers’ learning and communication. The copyright of articles, pictures, etc. belongs to the original author. If there is any infringement, please contact us to delete it.
end
A mouthful of Linux
Follow and reply [ 1024 ] Massive Linux information will be given away
Collection of wonderful articles
Article recommendation