Serial I/O

Purpose

To familiarize you with textual input and output via a Universal Asynchronous Receiver/Transmitter (UART):

Due dates

You must hand in answers to the questions at the beginning of your laboratory session on Thursday, October 16. This part of the assignment is worth 20 points.

You must be prepared to discuss your work at the beginning of your laboratory session on Tuesday, October 21, and to demonstrate your program to your TA during that period. This part of the assignment is worth 15 points.

Background

The MB5 has two 16550 UARTs (Universal Asynchronous Receiver/Transmitter) that are used to communicate with other devices via RS-232 interfaces. The HP has two RS-232 interfaces, which can be controlled manually by instances of the Tera Term terminal emulator.

MB5 UARTs

A UART is an I/O device. It's overall function is described in Section 12.2.3 of the text, and Tanenbaum's Section 3.8.1 explains how it is connected to other devices. The two gray cables coming out of the right side of the MB5 are the RS-232 cables connected to the MB5's UARTS. Just to the left of the connectors are two small driver chips, and to their left are the two larger 16550 UART chips.

Each UART defines a block of eight byte-sized ports that the CPU can use for various purposes -- sending and receiving information, controlling the transmission, etc. Click here for a summary of these ports and their functions. The UART documentation describes access to a specific port in terms of a base address for the UART and an index for the desired port. On the MB5, $200000 is the base address for UART1 and $900000 is the base address for UART2. File uart.ah defines symbols UART1 and UART2 as these addresses.

Figure 2.10 on page 46 of the text shows that the MC68000 chip has 23 address pins and 16 data pins. The address space consists of 224 bytes, which is 223 words. Thus when the MC68000 accesses memory or an I/O device, it specifies a word address and uses the 16 data pins to transmit or receive the data in that word. Byte transfers are possible because two of the five bits labeled ``I/O Transfer'' in Figure 2.10 are used to indicate which data bits are valid. One of these bits is 1 if data bits 0-7 are valid, the other is 1 if data bits 8-15 are valid.

When the CPU accesses a particular port of a particular UART, the 23 address lines must select both a UART chip and a port on that chip. In the MB5, chip select is done by the most significant four bits of the address and port select by the least significant three bits. Data bits 0-7 are always used to transmit information. Thus a CPU program has two ways of accessing a port: by using a word operation and specifying ((base address)+2*(port index)) as the address, or by using a byte operation and specifying ((base address)+2*(port index)+1) as the address. We have defined symbols for the port indices in file uart.ah under the assumption that byte operations will always be used to access the UART.

Communication with the UART device interface is illustrated in the comset.src file.

Tera Term

Tera Term is a terminal emulator program that allows you to control the COM ports of the HP manually. It can always be used to interact with the MB5 UART2 over the COM2 port, but the COM1 port can be used only if the XRAY monitor is not being used. You have been using Tera Term and the COM1 port to communicate with the MooseLoad program; the procedure for setting up the COM2 port is similar:
  1. Invoke Tera Term as usual.
  2. Select the Serial radio button, select COM2 from the Port pull-down, and click OK.
  3. Select Terminal from the Setup menu.
  4. Check the Local echo box, and click OK.
  5. Select Serial port from the Setup menu.
  6. Verify that the baud rate, data bits, and parity correspond to the values passed to the comset routine by the driver, and click OK.
We usually set the terminal for 9600 baud, 8 data bits, no parity, but you may experiment with other combinations if you like. You probably don't want to use other combinations on COM1, because the MooseLoad will reset the MB5's UART1 when the board is reset, and if the HP's COM1 port characteristics have been changed then the MooseLoad won't be able to communicate with the HP.

Two ASCII characters, CR and LF, are used to position the cursor to the beginning of a new line. The need for two characters originated in the use of mechanical devices for printing: CR moved the print head to the beginning of a line, and LF advanced the paper. Distinct characters were needed to support an ``overstrike'' capability, so that characters could be underlined and composite characters (such as a ``cent'' sign, consisting of ``c'' overstruck with ``/'') could be constructed.

The two-character legacy lives on in Windows text files, which are based on MS-DOS conventions. A ``newline'' is represented in such files by the CR followed immediately by the LF. Unix uses only a single character, LF, to indicate ``newline''; Apple Macintosh conventions also dictate a single character, but it is the CR.

Tera Term can be configured to handle either Windows or Macintosh conventions, but not Unix conventions. Select Terminal from the Setup menu, and then choose appropriate values in the Receive and Transmit pull-downs under Newline. Your Receive choice dictates the typed characters that will be recognized as defining a new line, and your Transmit choice dictates what characters will be sent out the HP's serial interface when a new line is recognized in typed input. (The Enter key produces CR and Ctrl-J produces LF.) A reasonable choice is to use CR for Receive and CR+LF for Transmit.

You can use vim to convert text files between the three formats. When you have the file in vim, simply give one of the following commands before writing it out:

Task

Click here for a zip file containing a C-coded driver file, interface specifications, and an assembly-coded initialization routine for the UARTs on the MB5 board. Design and implement polled character I/O routines having the general form of Figure 12.6 (page 374) of the text. Your routines must conform to the interface specification in file single.h. You must use the symbols defined by file uart.ah in all contexts that require you to say anything about the device interface.

The driver program that has been supplied uses both of the MB5's UARTs, and therefore the COM1 port is not available for use by the XRAY monitor. You might want to develop your own driver for testing purposes, using only the COM2 port. Once your character I/O routines are working, you can link them with the supplied driver to actually run the application.

When you have established a COM2 window and completed any testing that you want to do using the XRAY monitor, make a file suitable for downloading via the MooseLoad. This program must use the driver supplied in the zip file. Download this file over the COM1 port. After you have started the program, and MooseLoad has typed ``Executing.....'', select Terminal from the Setup menu of the COM1 window, check the Local echo box, and click OK.

Now type characters into the COM1 window. The characters you type will be echoed in the COM1 window, and should also appear in the COM2 window until you type Ctrl-d. Any futher characters will echo in the COM1 window, but will not appear in the COM2 window.

Ctrl-d switches the direction of communication. After typing Ctrl-d, use the HP's mouse to move the cursor into the COM2 window and left-click to make the COM2 window active. Now when you type characters into the COM2 window, they will appear in the COM1 window. You can switch back and forth as many times as you like, with the Ctrl-d giving up control to the other window.

Typing Ctrl-c terminates the program. The MooseLoad menu will be displayed in the COM1 window, and you can restart the program if you like.

Questions

  1. (2 points) Assume the 7-bit ASCII code, even parity, and 2 stop bits and sketch the timing diagram of the letter 'j' (lower-case j) as it is transmitted over an asynchronous serial communication link.

  2. For this question, assume that you have a computer with a display and keyboard. Furthermore, assume that the computer is connected to a remote machine via a UART on a COM port. The UART is set to communicate at 9600 baud with 7-bit ASCII, even parity, 1 start bit and 1 stop bit. Also assume that it takes about 1 microsecond for the computer to move a character from memory to the UART.
    1. (1 point) What is the maximum number of characters that the computer can send to the remote machine in one second if the computer is reading characters from a string in memory?
    2. (1 point) If the characters being sent are being typed on the computer's keyboard, what is the average number of characters that the computer can display on the remote machine in one second? You may answer this generally or assume a typing speed of 60 words per minute with an average of 5 characters (plus a space or punctuation mark) per word, that is, 6 keystrokes per word.
    3. (1 point) If the characters being sent to the remote machine are being typed on the computer's keyboard, will the computer ever have to wait for the UART? You may assume normal operations, that is, no bizarre events happening to the UART. Explain briefly.

  3. (2 points) Briefly explain why two UARTs cannot communicate if they are set to different baud rates. Be specific.

  4. (3 points) Suppose that one of two communicating UARTs is set for 1 stop bit and the other for 2. Will this mismatch prevent communication? Explain briefly.

  5. Consider the UART initialization routine implemented by file comset.src, whose interface is defined by file comset.h.
    1. (3 points) Carefully explain the rationale for the definitions of port, baud, and parity in comset.src.
    2. (2 points) What is the purpose of the variable given?
    3. (5 points) Although comset is a C-callable routine, it does not use link or unlk. Give an argument for or against the omission of these instructions.

Demonstration

You must demonstrate your program to your TA on one of the machines in the Microlab.
Instructor Revision 1.39 (2007/08/27 00:15:44)