Copyright 1999, Analog Devices, Inc. All rights reserved. Analog Devices assumes no responsibility for customer product design or the use or application of customers’ products
or for any infringements of patents or rights of others which may result from Analog Devices assistance. All trademarks and logos are property of their respective holders.
Information furnished by Analog Devices Applications and Development Tools Engineers is believed to be accurate and reliable, however no responsibility is assumed by Analog
Devices regarding the technical accuracy of the content provided in all Analog Devices’ Engineer-to-Engineer Notes.
Implementing A Software UART On The
ADSP-2181 EZ-KIT-LITE
a
EE-89Page 2
Technical Notes on using Analog Devices’ DSP components and development tools
This document describes a software implementation of a Universal Asynchronous Receiver Transmitter
(UART). The UART is implemented as a program running on the ADSP-2181 EZ Kit Lite. This document
will also provide basic background on serial and asynchronous transmission.
2. BACKGROUND
2.1 Serial Communication
Digital data are transmitted along paths that may physically consist of wires, radio waves, microwaves.
Such paths are often referred to as data buses. The digital data that are carried on such buses are encoded in a
digital format such as BCD, ASCII, or 8-bit words.
When data are transmitted serially, only one data path is required since the data are sent one bit at a
time. Cabling costs are lower than other methods of communication since serial communication requires the
minimum amount of wires. Serial transmission methods are characterized by how many bits per second they
can transmit. This unit of measurement is call a baud (1 bit/s). Common baud rates in serial systems are 110,
300, 600, 1200, 2400, 4800, 9600 and 19200.
2.2 Asynchronous Serial Communication
Most serial interfacing is done in an asynchronous manner. In this method, the transmitter sends a
character whenever one is available. And there is no synchronization clock pulse between the transmitter and
receiver to control the start of a character. This method is relatively slow since it requires a handshake for each
character of data transfer. In serial asynchronous systems, however, handshaking is performed by using a start
bit and one or two stop bits at the beginning and end of each character that is transmitted (As shown in Figure
1). The start bit is a logic low and the stop bit is a logic high. A parity bit is sometime added after the end of
the data bits for the receiver to detect transmission error. The number of such start, stop, and parity bits must
be agreed upon by both the transmitter and receiver.
EE-89Page 4
Technical Notes on using Analog Devices’ DSP components and development tools
Start <-------------------- Data Bits -------------------> Parity Stop
idle state 0 1 2 3 4 5 6 7 idle state
Figure 1 - ASCII character ‘8’ with one start bit, one even parity bit and one stop bit
2.3 Universal Asynchronous Receiver/Transmitter
The Universal Asynchronous Receiver/Transmitter (UART) is a dedicated single chip that is controlled
by a internal clock. This chip converts characters into a series of pulses or a series of pulses back into
characters. Using its clock, the UART transmitter controls the duration of pulses with no delay between them
as it sends a character. The receiver always monitors the incoming line and when a start bit is detected, the
UART will clock in bits at a set baud rate. When the receiver has collected all the data bits, it converts them
into a character and stops sampling.
A Digital Signal Processor (DSP) can be programmed to perform the same operation as a dedicated
UART chip. This can be accomplished by one of three methods. The first method is to tie the data receive line
to the interrupt input of the processor so that when a start bit comes along, the processor gets interrupted. This
lets the processor perform some other task and interrupts occur whenever data comes along on the serial line.
The second method is to have the transmit and receive lines of the serial device connected to the FLAG OUT
and the FLAG IN pins on the DSP. The Processor will have to poll the FLAG IN pin continuously using a
timer to see if a start bit is present. This method may tie up the processor. The third method uses the serial port
instead of the timer and the FLAG IN pin to sample and output data. The main advantage of this technique is
that it uses no timer interrupt, keeping the timer free for other operations. Also, no overhead will be produced
with the timer interrupt service routine (3 times per bit * 8 bits = 24 services).
The example program and the UART driver in this example polls the FLAG IN pin continuously and
uses the timer to implement UART. In this example, the ADSP-2181 transmits and receives serial data
asynchronously by connecting the transmit and receive lines of the serial device to the FLAG OUT and the
EE-89Page 5
Technical Notes on using Analog Devices’ DSP components and development tools
FLAG IN pins. When there is a character to be sent by the DSP, the software transmitter converts it to a
sequence of zeros and ones, and sends them down the line contiguously, LSB (Least Significant Bit) first. When
the DSP finishes sending a character, the line goes into an idle (high) state until the transmitter sends other
character. When a character is available to be received, the software receiver shifts in bits and constructs
words by sampling the FLAG IN pin.
3. HARDWARE
A serial cable is used to connect he EZ Kit Lite board to the serial device. The ADSP-2181 is
interfaced to an RS-232 line driver which is in turn connected to any RS-232 compatible device. The line
driver is used to convert the 5 volt logic level of the ADSP-2181 to the proper RS-232 line voltages, and vice
versa.
Figure 2 shows the connection between the ADSP-2181 and the line driver ADM232AAR. The
FLAG IN and FLAG OUT pins are used as independent receive and transmit lines.
DSP ADM232AAR RS-232 Connector
ADSP-2181FO RX
FI TX
Figure 2 - EZ Kit Lite System Configuration
EE-89Page 6
Technical Notes on using Analog Devices’ DSP components and development tools
The UART program supports most common baud rates. However, the autobaud feature can only
detect 2400, 3600, 9200 and 19200 baud. The word to be transmitted or received should have 8 data bits
and no parity bit. The program adds 1 start bit and 1 stop bit automatically when transmitting.
4.2 Program Flow
The UART program consists of the following subroutines:
• Bit processing timer interrupt service routine (uart_isr)
• Transmit routine (trans_bit)
The Automatic baud rate detection routine must be called first after a system reset. This subroutine
will wait for a ASCII ‘8’ character to be downloaded from the serial device. Then this subroutine configures
Serial Port 1 as the FLAG IN and FLAG OUT pin for receiving and transmitting. Once the character is
received, the routine figures out the current baud rate setting of the serial device and pass it on to the initialization
routine.
The Initialization routine must be called after the automatic baud rate detection routine. This routine
reads the detected baud rate constant from the autobaud routine and sets the internal timer of the UART to
match the baud rate. Then this subroutine configures Serial Port 1 as the FLAG IN and FLAG OUT pin for
receiving and transmitting. The status flags of the UART are set or reset here to indicate that the UART is ready
for operation.
After the autobaud and initialization routines are called upon after system reset, the Bit processingtimer interrupt service routine is called. This happens every time the timer expires and issues an interrupt.
This routine will set or reset the FLAG OUT pin to transmit one bit at a time if there is a character to be sent
out. The routine then checks to see If there are bits to be received from the FLAG IN pin by polling the FLAG
EE-89Page 7
Technical Notes on using Analog Devices’ DSP components and development tools
IN pin continuously to look for the start bit (logic low). If the start bit is detected, the routine will shift in one bit
at a time and signal if a whole character has been received.
The Transmit routine is used to send a character transmit request to the UART. This routine copies the
character to be transmitted to the transmit buffer with the correct number of stop and start bits added. The
transmit routine then sets a flag to notify the UART that a character is waiting to be transmitted.
EE-89Page 8
Technical Notes on using Analog Devices’ DSP components and development tools
For the software UART to function properly, the program must know the baud rate setting of the serial
device that it is trying to communicate with. Instead of setting the baud manually, this autobaud feature of the
UART will automatically detect and match the current serial device’s baudrate.
The autobaud subroutine must be called after a system restart. The user should transmit an ‘8’ from the
serial terminal to the EZ Kit Lite board at restart. The character ‘8’ was chosen because
representation has 3 consecutive high bits with all other bits low. Autobauding works as follows:
1. The subroutine loads the timer with maximum value (0xFFFF).
2. The subroutine waits for a start bit (0) by polling the FLAG IN pin.
3. When a start bit is detected, the routine waits for the beginning of the three high bits in ‘8’’s ASCII
representation to arrive.
4. The timer counts down during the receiving period of the 3 high bits.
5. The number of cycles taken to receive those 3 bits is then compared to the number of cycles a known
baud rate took to receive 3 bits.
6. If a match is found, the autobaud sets the timer constant to operate the UART at the same baud rate.
The number of cycles needed to process 3 bits and the timer constant for common baud rates can be
found by using the following formula. Use the following formula to find the number of cycles needed for
processing 1/3 bit:
Number of Cycles Needed to process 1/3 bit = ( ( processor frequency / ( 3 * baud rate ) ) - 1 *
BAUD Rate
(bits/sec)
Cycles Needed
for 3 Bits
Cycles Needed
for 1/3 Bit
2400416614629
4800208262314
9600104041156
EE-89Page 9
Technical Notes on using Analog Devices’ DSP components and development tools