User's Manual

PMAC User Manual
248 Writing a Host Communications Program
Base Address
The first thing to know is the base address of the COM port in the computer’s I/O space. In an IBM-PC, the
COM1 port base address is at 3F8 hex (1016 decimal), and the COM2 port is at 2F8 hex (760 decimal).
Baud Rate
Set up the baud rate counter in the host computer to match the PMAC baud rate, which is determined by
the master clock and jumpers E40-E43. The baud rate counter must be given a value equal to 115,200
divided by the baud rate (e.g., for 9600 baud, the value is 115,200/9600 = 12).
The following program segment illustrates how this can be done:
outportb (combase + 3, 131); /* Put COM port in setup mode */
baud_count = 115200/baud; /* Calculate counter value */
outportb (combase, baud_count); /* Write to low byte of counter */
outportb (combase + 1, baud_count/256); /* Write to high byte of counter */
outportb (combase + 3, 3); /* Put COM port back in normal mode */
/* with 8 bits, 1 stop bit */
The command outportb is a byte-write command; combase is the base address; baud is the baud rate in
bits per second.
It is a good idea in the initial set up to compute a timeout value, related to both the baud rate and the host
computer’s speed. As the host polls PMAC to see if it is ready to communicate, a counter increments; if
the counter exceeds the timeout value, the host should give up on this attempt to talk to PMAC.
Depending on the circumstances, it should either just try again later (as when waiting for some
asynchronous communications) or assume there is an error condition. A good equation for the timeout
value is:
)100baudcount(speed7timeout
+
=
where speed is 1 or 2 for a PC-XT, 3 or 4 for a 286-based computer, 5 to 6 for a 386-based computer, and
7 to 9 for a 486-based computer.
Sending a Character
In polled communications, the host must see two status bits (write-ready bits) in the serial interface
registers become one before it may write a character to the serial output port. These two bits are Bit 5 of
{base + 5}, and Bit 4 of {Base +6}. A sample C code segment to do this is:
i = 0; /* Reset counter */
while (i++<timeout && (inportb(combase+5)&32==0); /* Loop until bit true */
while (i++<timeout && (inportb(combase+6)&16==0); /* Loop until bit true */
if (i < timeout)outportb(combase, outchar); /* Send character unless timed out */
Sending an entire line simply involves repeated calls to this routine, with a different outchar each time.
Reading a Character
To read a character from the serial port, the host must prepare the port to read (it may want to do this for
an entire line), then poll a status bit (read-ready bit) in a serial interface register; when this becomes one,
the character may be read. A sample C code segment to do this is:
i = 0; /* Reset counter */
outportb(combase + 4, 2); /* Set port for input */
while (i++<timeout && (inportb(combase+5)==0); /* Loop until bit true */
if (i < timeout) inchar = inportb(combase); /* Get char. unless timed out */
disable(); /* Disable interrupts */
outportb(combase + 4, 0); /* Set port for output */
enable(); /* Re-enable interrupts */
To read an entire line in a single routine, turn around the port at the beginning and end of the line.