Communication Protocols 2
Communication Protocols 2
Communication between electronic devices is like communication between humans. Both sides
need to speak the same language. In electronics, these languages are called communication
protocols. Luckily for us, there are only a few communication protocols we need to know when
building most electronics projects. In this series of articles, we will discuss the basics of the three
most common protocols: SPI, I2C and UART. SPI, I2C, and UART are quite a bit slower than
protocols like USB, Ethernet, Bluetooth, and Wi-Fi, but they’re a lot simpler and use less hardware
and system resources. SPI, I2C, and UART are ideal for communication between microcontrollers
and between microcontrollers and sensors where large amounts of high speed data don’t need to be
transferred.
Transmission: In data transmission if the data can be transmitted and received, it is a duplex
transmission.
Simplex: Data is transmitted in only one direction i.e. from TX to RX only one TX and one RX
only
Half duplex: Data is transmitted in two directions but only one way at a time i.e. two TX's, two
RX’s and one line
Full duplex: Data is transmitted both ways at the same time i.e. two TX's, two RX’s and two lines
UART
COMMUNICATION
In UART communication, two UARTs communicate directly with each other. The transmitting
UART converts parallel data from a controlling device like a CPU into serial form, transmits it in
serial to the receiving UART, which then converts the serial data back into parallel data for the
receiving device. Only two wires are needed to transmit data between two UARTs. Data flows from
the Tx pin of the transmitting UART to the Rx pin of the receiving UART:
UARTs transmit data asynchronously, which means there is no clock signal to synchronize the
output of bits from the transmitting UART to the sampling of bits by the receiving UART. Instead
of a clock signal, the transmitting UART adds start and stop bits to the data packet being
transferred. These bits define the beginning and end of the data packet so the receiving UART
knows when to start reading the bits.
When the receiving UART detects a start bit, it starts to read the incoming bits at a specific
frequency known as the baud rate. Baud rate is a measure of the speed of data transfer, expressed in
bits per second (bps). Both UARTs must operate at about the same baud rate. The baud rate between
the transmitting and receiving UARTs can only differ by about 10% before the timing of bits gets
too far off. Both UARTs must be configured to transmit and receive the same data packet structure.
START BIT
The UART data transmission line is normally held at a high voltage level when it’s not transmitting
data. To start the transfer of data, the transmitting UART pulls the transmission line from high to
lowfor one clock cycle. When the receiving UART detects the high to low voltage transition, it
begins reading the bits in the data frame at the frequency of the baud rate.
DATA FRAME
The data frame contains the actual data being transferred. It can be 5 bits to 9 bits long if a parity bit
is used. If no parity bit is used, the data frame can be 8 bits long. In most cases, the data is sent with
the least significant bit first.
PARITY
Parity describes the evenness or oddness of a number. The parity bit is a way for the receiving
UART to tell if any data has changed during transmission. Bits can be changed by electromagnetic
radiation, mismatched baud rates, or long distance data transfers. After the receiving UART reads
the data frame, it counts the number of bits with a value of 1 and checks if the total is an even or
odd number. If the parity bit is a 0 (even parity), the 1 bits in the data frame should total to an even
number. If the parity bit is a 1 (odd parity), the 1 bits in the data frame should total to an odd
number. When the parity bit matches the data, the UART knows that the transmission was free of
errors. But if the parity bit is a 0, and the total is odd; or the parity bit is a 1, and the total is even,
the UART knows that bits in the data frame have changed.
STOP BITS
The Stop Bit, as the name suggests, marks the end of the data packet. It is usually two bits long but
often only on bit is used. In order to end the transmission, the UART maintains the data line at high
voltage (1).
2. The transmitting UART adds the start bit, parity bit, and the stop bit(s) to the data frame:
3. The entire packet is sent serially from the transmitting UART to the receiving UART. The
receiving UART samples the data line at the pre-configured baud rate:
No communication protocol is perfect, but UARTs are pretty good at what they do. Here are some
pros and cons to help you decide whether or not they fit the needs of your project:
ADVANTAGES
Only uses two wires
No clock signal is necessary
Has a parity bit to allow for error checking
The structure of the data packet can be changed as long as both sides are set up for it
Well documented and widely used method
DISADVANTAGES
The size of the data frame is limited to a maximum of 9 bits
Doesn’t support multiple slave or multiple master systems
The baud rates of each UART must be within 10% of each other
UART is one of the most simple and most commonly used Serial Communication techniques.
Today, UART is being used in many applications like GPS Receivers, Bluetooth Modules, GSM
and GPRS Modems, Wireless Communication Systems, RFID based applications etc.
MOSI (Master Output/Slave Input) – Line for the master to send data to the slave.
MISO (Master Input/Slave Output) – Line for the slave to send data to the master
SCLK (Clock) – Line for the clock signal.
SS/CS (Slave Select/Chip Select) – Line for the master to select which slave to send
data to.
*In practice, the number of slaves is limited by the load capacitance of the system,
which reduces the ability of the master to accurately switch between voltage levels.
The clock signal in SPI can be modified using the properties of clock
polarity and clock phase. These two properties work together to define when the
bits are output and when they are sampled. Clock polarity can be set by the master
to allow for bits to be output and sampled on either the rising or falling edge of the
clock cycle. Clock phase can be set for output and sampling to occur on either the
first edge or second edge of the clock cycle, regardless of whether it is rising or
falling.
SLAVE SELECT
The master can choose which slave it wants to talk to by setting the slave’s
CS/SS line to a low voltage level. In the idle, non-transmitting state, the slave select
line is kept at a high voltage level. Multiple CS/SS pins may be available on the
master, which allows for multiple slaves to be wired in parallel. If only one CS/SS pin
is present, multiple slaves can be wired to the master by daisy-chaining.
MULTIPLE SLAVES
SPI can be set up to operate with a single master and a single slave, and it can
be set up with multiple slaves controlled by a single master. There are two ways to
connect multiple slaves to the master. If the master has multiple slave select pins,
the slaves can be wired in parallel like this:
If only one slave select pin is available, the slaves can be daisy-chained like this:
MOSI AND MISO
The master sends data to the slave bit by bit, in serial through the MOSI line.
The slave receives the data sent from the master at the MOSI pin. Data sent from the
master to the slave is usually sent with the most significant bit first.
The slave can also send data back to the master through the MISO line in
serial. The data sent from the slave back to the master is usually sent with the least
significant bit first.
2. The master switches the SS/CS pin to a low voltage state, which activates the
slave:
3. The master sends the data one bit at a time to the slave along the MOSI line. The
slave reads the bits as they are received:
4. If a response is needed, the slave returns data one bit at a time to the master
along the MISO line. The master reads the bits as they are received:
ADVANTAGES AND DISADVANTAGES OF SPI
There are some advantages and disadvantages to using SPI, and if given the
choice between different communication protocols, you should know when to use
SPI according to the requirements of your project:
ADVANTAGES
No start and stop bits, so the data can be streamed continuously without
interruption
No complicated slave addressing system like I2C
Higher data transfer rate than I2C (almost twice as fast)
Separate MISO and MOSI lines, so data can be sent and received at the same
time
DISADVANTAGES
Uses four wires (I2C and UARTs use two)
No acknowledgement that the data has been successfully received (I2C has this)
No form of error checking like the parity bit in UART
Only allows for a single master
Start Condition: The SDA line switches from a high voltage level to a low voltage
level before the SCL line switches from high to low.
Stop Condition: The SDA line switches from a low voltage level to a high voltage
level after the SCL line switches from low to high.
Address Frame: A 7 or 10 bit sequence unique to each slave that identifies the slave
when the master wants to talk to it.
Read/Write Bit: A single bit specifying whether the master is sending data to the
slave (low voltage level) or requesting data from it (high voltage level).
The master sends the address of the slave it wants to communicate with to
every slave connected to it. Each slave then compares the address sent from the
master to its own address. If the address matches, it sends a low voltage ACK bit
back to the master. If the address doesn’t match, the slave does nothing and the SDA
line remains high.
READ/WRITE BIT
The address frame includes a single bit at the end that informs the slave
whether the master wants to write data to it or receive data from it. If the master
wants to send data to the slave, the read/write bit is a low voltage level. If the
master is requesting data from the slave, the bit is a high voltage level.
The data frame is always 8 bits long, and sent with the most significant bit
first. Each data frame is immediately followed by an ACK/NACK bit to verify that the
frame has been received successfully. The ACK bit must be received by either the
master or the slave (depending on who is sending the data) before the next data
frame can be sent.
After all of the data frames have been sent, the master can send a stop
condition to the slave to halt the transmission. The stop condition is a voltage
transition from low to high on the SDA line after a low to high transition on the SCL
line, with the SCL line remaining high.
3. Each slave compares the address sent from the master to its own address. If the
address matches, the slave returns an ACK bit by pulling the SDA line low for one bit.
If the address from the master does not match the slave’s own address, the slave
leaves the SDA line high.
4. The master sends or receives the data frame:
5. After each data frame has been transferred, the receiving device returns
another ACK bit to the sender to acknowledge successful receipt of the frame:
6. To stop the data transmission, the master sends a stop condition to the slave by
switching SCL high before switching SDA high:
SINGLE MASTER WITH MULTIPLE SLAVES
Because I2C uses addressing, multiple slaves can be controlled from a single
master. With a 7 bit address, 128 (27 ) unique address are available. Using 10 bit
addresses is uncommon, but provides 1,024 (210) unique addresses. To connect
multiple slaves to a single master, wire them like this, with 4.7K/10K Ohm pull-up
resistors connecting the SDA and SCL lines to Vcc:
ADVANTAGES
Only uses two wires
Supports multiple masters and multiple slaves
ACK/NACK bit gives confirmation that each frame is transferred successfully
Hardware is less complicated than with UARTs
Well known and widely used protocol
DISADVANTAGES
Slower data transfer rate than SPI
The size of the data frame is limited to 8 bits
More complicated hardware needed to implement than SPI
USB offers users simple connectivity. It eliminates the mix of different connectors for different
devices like printers, keyboards, mice, and other peripherals. That means USB-bus allows many
peripherals to be connected using a single standardized interface socket. It supports all kinds of
data, from slow mouse inputs to digitized audio and compressed video.
USB also allows hot swapping. The "hot-swapping" means that the devices can be plugged and
unplugged without rebooting the computer or turning off the device. That means, when plugged in,
everything configures automatically. Once the user is finished, they can simply unplug the cable
out; the host will detect its absence and automatically unload the driver. This makes the USB a
plug-and-play interface between a computer and add-on devices.
USB is now the most used interface to connect devices like mouse, keyboards, PDAs, game-pads
and joysticks, scanners, digital cameras, printers, personal media players, and flash drives to
personal computers. USB sends data in serial mode i.e. the parallel data is serialized before sends
and de-serialized after receiving.
The benefits of USB are low cost, expandability, auto-configuration, hotplugging and outstanding
performance. It also provides power to the bus, enabling many peripherals to operate without the
added need for an AC power adapter.
USB1.0: USB 1.0 is the original release of USB having the capability of transferring 12Mbps,
supporting up to 127 devices. This USB 1.0 specification model was introduced in January 1996.
USB1.1: USB 1.1 came out in September 1998. USB 1.1 is also known as full speed USB. This
version is similar to the original release of USB; however, there are minor modifications for the
hardware and the specifications. USB version 1.1 supported two speeds, a full speed mode of
12Mbits/s and a low speed mode of 1.5Mbits/s.
USB2.0: Hewlett-Packard, Intel, LSI Corporation, Microsoft, NEC, and Philips jointly led the
initiative to develop a higher data transfer rate than the 1.1 specifications. The USB 2.0
specification was released in April 2000 and was standardized at the end of 2001.
Supporting three speed modes (1.5, 12 and 480 Mbps), USB 2.0 supports low bandwidth devices
such as keyboards and mice, as well as high-bandwidth ones like high-resolution Web-cams,
scanners, printers and high-capacity storage systems. USB 2.0, also known as hi-speed USB. This
hi-speed USB is capable of supporting a transfer rate of up to 480 Mbps, compared to 12 Mbps of
USB 1.1. That's about 40 times as fast! Wow!
USB3.0: USB 3.0 is the latest version of USB release. It is also called as SuperSpeed USB having a
data transfer rate of 4.8Gbps (600 MB/s). That means it can deliver over 10x the speed of today's
Hi-Speed USB connections. The USB 3.0 specification was released by Intel and its partners in
August 2008. Products using the 3.0 specifications are come out in 2010.
The USB system is made up of a host, multiple numbers of USB ports, and multiple peripheral
devices connected in a tiered-star topology. The host is the USB system's master, and as such,
controls and schedules all communications activities. Peripherals, the devices controlled by USB,
are slaves responding to commands from the host. USB devices are linked in series through
hubs.
There always exists one hub known as the root hub, which is built in to the host
controller.
USB connectors:
Connecting a USB device to a computer is very simple -- you find the USB
connector on the back of your machine and plug the USB connector into it. If it is a
new device, the operating system auto-detects it and asks for the driver disk. If the
device has already been installed, the computer activates it and starts talking to it.
The USB standard specifies two kinds of cables and connectors.
Fig: USB Type A & B Connectors
The USB standard uses "A" and "B" connectors mainly to avoid confusion:
1. "A" connectors head "upstream" toward the computer.
2. "B" connectors head "downstream" and connect to individual devices.
By using different connectors on the upstream and downstream end, it is
impossible to install a cable incorrectly, because the two types are physically
different.
Control transfers exchange configuration, setup and command information between the device and
host. The host can also send commands or query parameters with control packets.
Isochronous transfer is used by time critical, streaming device such as speakers and video cameras.
It is time sensitive information so, within limitations, it has guaranteed access to the USB bus.
Bulk transfer is used by devices like printers & scanners, which receives data in one big packet.
Interrupt transfer is used by peripherals exchanging small amounts of data that need immediate
attention.
All USB data is sent serially. USB data transfer is essentially in the form of packets of data, sent
back and forth between the host and peripheral devices. Initially all packets are sent from the host,
via the root hub and possibly more hubs, to devices.