Understanding IO communication in general and RS-232 in particular
In the last session, we have connected GPIO controller to our little CPU.
We discussed that, with this general purpose IO we can control digital
output lines and we can read digital input lines.
Using output lines we can turn on and off LEDs, turn on and off compressor
unit in air conditioners. We can also turn on and off water inlet and water
outlet valves in washing machine. In fact we can also control the speed of
a DC motor by making it on and off very fast. Making on and off very fast,
will not stop the motor, but make it run slower than in continuous on.
Using input lines, we can read the status of switches or push buttons.
We can read status of water tank, whether it is full or not. Door is open
or closed. Seat belt in the car is taken or not. Temperature in the water
heater reached maximum or not. The possibilities are limit less. Thats why
it is called general purpose IO. we can do many things with it.
Data transfer or communication protocols
Another use of GPIO lines is to implement communication protocols.
The communication protocols involve transfer of data between two embedded
devices, or between embedded device and another chip or between embedded
device and computer. Commnication means data transfer, data transfer
means moving, bytes of data between the two things involved in the
communication. Note that, undestanding this concept of communication
(data transfer) is very important. There exist many ways of transfer, and
each way is one protocol. In the last article, it was told to pay attention
on how data is flowing between IO chip and IO device. So between every IO
chip and IO device there exist a data transfer protocol.
We can implement almost any kind of protocol with the help of GPIO lines.
But to implement the data transfer protocol, we have to write lot of
code (program) and CPU has to execute it continuously. So there will be
load on the CPU, then CPU will have less time to do more important things.
So solution is to build a special IO chip to implement a communication
protocol. Now this special chip is connected to the CPUs bus. CPU
simply writes the data byte, it wish to transfer into the register of
this special chip. Now chip will do all the prtocol stuff and transfer that
byte according to protocol. This is like taking some license from goverment
office. There are lot of protocols like filling lot of forms, attaching
lot of proofs and also doing some other things. Instead we can approach
an agent, agent will collect all the required things and follow all
kinds of protocols and get you a license. Agent is specialized in this
kind of services and saves our time. We can spend our time on more important
IO Interfaces and supporting chips
There exist so many communication protocols used for interfacing
- RS-232 serial communication
- Inter IC (I2C) communication
- Inter-IC Sound (I2S) protocol
- Serial Peripheral Interface (SPI)
- PS/2 protocol used with PS/2 keyboard and mouse
- Centronics Printer Port or Parallel port protocol
- Parallel ATA (PATA) used to communicate with older hard disks
- Serial ATA (SATA) used to communicate with current hard disks
- Small Computer System Interface (SCSI)
- Universal Serial Bus (USB)
- Ethernet LAN
- Controller Area Network (CAN)
There are many more protocols than listed above. All of them describe, how
to transfer data between one computer/device/chip to other
computer/device/chip. And there exist specialized IO chips to implement these
data transfer protocols. All these chips can be interfaced to the CPU's bus.
CPU sends and receives data through these chips, by writing and reading the
registers of the chip. These IO chips are acting as bridge (agent) between
CPU and IO devices or some other chips.
RS-232 Serial communication and UART controller
OK, we have discussed enough about how CPU can do data transfers. Now let us
discuss about the oldest and most widely used communication available in
embedded hardware, that is RS-232 serial communication protocol. The IO chip
that supports RS-232 serial communication is called Universal Asynchronous
Receiver Transmitter (UART). RS-232 communication is called asynchronous
communication, but there exist synchronous communication also, some chips
support that mode of communication also. So these chips are called USART chips.
Additional S in the name indicates synchrounous. In asynchronous communication
each byte is transfered independently. But in synchronous mode a set of
bytes will be transfered continuously without any gap between them. Right
now we need not worry much about these differences, just remember that
RS-232 is an asynchonous communication and we can transfer byte at a time.
The following figure shows how UART chip is interfaced to the CPU. Every
IO chip is interfaced to the CPU's bus in the same way. So every IO chip will
have chip select pin, read pin, write pin address pins and data pins for
connecting to the CPU's bus. Number of address pins, depends on the number of
registers present on the chip. The address placed on the address lines
identifies the register.
Typical UART chips will have 8 to 10 registers. But our simple UART
is having only 3 registers. These are transmit buffer register, receive buffer
register and status register. The corresponding register addresses are
0xD2, 0xD1 and 0xD0. When CPU places these addresses and writes data, data
will go to these registers. In this way CPU does not know or care, to which
chip it is writing. It just places the address on address lines, puts
the data on data lines and activates the write line. If this address
happens to be RAM location it will get written to RAM. If address happens
to be GPIO or UART register address, data will get written to register
in that chip.
IO Interface pins
In the last article, we have noted that every IO chip will have two types
of pins. One set of pins are bus interface pins for connecting to the
CPU's bus. Another set of pins are IO interface pins for connecting to IO
device (some cases other IO chip). IO interface pins vary from IO chip to
IO chip. In the case of GPIO chip discussed earlier, it got 8 digital IO
lines. Now in the case of our simple UART, it got two IO pins called
transmit pin and receive pin. Real UART chips may have some more IO pins
called RTS, CTS, DSR, DTR etc..
Transmit pin is used to transmit data byte serially bit by bit. Similarly
receive pin is used to receive data byte serially bit by bit. Each bit
is transmitted on the transmit pin, for a particular duration. This duration
desides the transfer speed. Transfer speed is measured as bits per second.
Typical transfer speeds are 9600, 19200, 38400, 57600 and 115200 bits per
second. If transfer speed is 9600, then each bit duration will be 1/9600.
That is 104 micro seconds.
Serial communication devices
One most common use of RS-232 serial communication is between embedded
hardware and PC (personal computer). Through this communication, from PC
we can transfer the program code to the embedded board. Using RS-232 port,
embedded hardware can connect to other devices like GPS (global positioning
system) receiver and GSM/GPRS modem. In every device or PC that support
RS-232 communication, will have UART controller in it. The transmit pin of
embedded board UART is connected to the receive pin of UART in PC/GPS/GSM.
Similarly embedded board UART's receive pin is connected to the transmit
pin of UART in PC/GPS/GSM
When data tranfer is not happening, the transmit pin is in the high position.
In this condition tranmit buffer register is empty. That is, there is nothing
in the transmit buffer register to transmit. This state is indicated by a
bit in the status register. The second bit (bit 1) of the status register is
called TXEMPTY bit. If this bit is 1, it indicates that tx buffer is empty.
That means we can transfer a byte, by writing a byte value in to this
tx buffer register. In the figure above, the second bit of status register is
shown as zero. It means that, UART is currently transmitting some byte present
in the tx buffer.
If we wish to transfer a byte (char), first we should read the status register
and ensure that TX_EMPTY bit of status register is 1. Next we should write
the byte into tx buffer register. Then the UART will transmit first a zero
bit called start bit. Because of this zero start bit, the tx line, which is
high will become low. The tx line going from high to low, will indicate the
receiver that a byte transfer is started. Now receiver will become ready
to receive. Receiver ignores this start bit and receives next 8 data bits.
Receiver must know the bit interval (transfer speed) of transmitter. Both
UARTs should work at the same transfer speed. After transmitting the 8 bits
of data, transmitter will transmit one bit called stop bit. Because of this
stop bit, the tx line will come to high position and remain at high till
a new byte transfer is started. So to transfer one byte of data total
10 bits are transfered, which includes one start bit and one stop bit.
When UART receives one complete byte, it places this byte in the receive
buffer register and sets the bit 0 in the status register. This bit 0 of
status register is called RX_READY bit. If we wish to read a byte from the
UART, first we should read status register and check if RX_READY bit is 1.
If this bit is 1, it indicates that UART has received some data byte and
placed it in rx buffer register. Now we can read the rx buffer register and
get the value.
It is very easy to use UART and transfer data to external PC or devices.
Similarly we can receive data from PC or other devices, through UART