Programming the UART

In the C programming language the I/O space address can be accessed using the functions inb() an outb(). The function declaration of inb() and outb() is given below.

char inb(char address);
outb(char pattern, char address);

The function inb() reads a byte from the address specified.The function outb() writes the pattern to the address specified. Only super user can access this functions. The user space needs a higher privilage level to gain access to I/O space. This can be achieved using the function iopl(). The declaration is given below.

int iopl(int level);

The call to iopl() changes the input output privilage level to the level specified.

The program should be compiled with the -O option. O stands for the optimization option.

Before using UART it must be properly initialized. The UART programming can be divided into

UART Initialization

The first step in initializing the UART is setting the baud rate. Data format register is used for this purpose. Baud rate=main reference frequency/(16*divisor) In the PC a main reference frequency of 1.8432Mhz is used generated by an external oscillator.

In this case baud rate equal to 115200/divisor. The divisor is stored in the divisor latch register. The divisor latch register is accessed by setting the DLAB bit of data format register. After writing the divisor latch register DLAB bit is cleared.

The next step is to define the data format. Data format register can be used for this purpose. Here the format of 8 data bit, One stop bit and no parity is used.

UART Interrupt Handling

With the interrupt enabled register we can control the interrupt request. Two types of interrupts are possible. In the first case an interrupt is occured when the reived data is ready. In the second case the interrupt is occured when the transmitter buffer is empty. In the Interrupt enable register, the high order nibble register is equal to zero and cannot be altered. The first bit is used to enable the interrupt of the first type and the second bit is used for enabling the interrupt of the second type. If the interrupts are enabled, the corresponding IRQ lines will be high, whenever an interrupt is occured. COM1 is configured with IRQ4 and COM2 is configured with IRQ4.

Using the interrupt identification register we can identify the interrupt. Two types of interrupts are possible in this case as early mentioned. The second and third bits of this register are used for the interrupt identification.

With modem control register we can set master interrupt bit to enable individual interrupt.

Data transmission and reception

Serialization status register is used to know whether data byte is available at the receiver buffer register. If the data byte is available corresponding data is retrieved. To transmit the data the corresponding data byte is loaded into the transmit hold register, if the transmit buffer empty flag of serialization status register is set. These tests are used to avoid character overrun errors.