1
Homework
Reading (linked from my web page) S and S Extracts National Semiconductor UART Data Sheet Machine Projects mp2 due at start of class 12 Labs Continue labs in your assigned section2
Addressing I/O Devices
Intel I/O devices have addresses assigned in an 00rthogonal00space from memory addresses Remember the M/IO# signal that is used with the address bus to select memory versus I/O devices? Use I/O instructions for I/O device addressesinw inb
outw outb
3
Addressing I/O Devices
The 00nput00 instruction 00direct addressinginw $0xdd, %ax # 8 bit address
inb $0xdd, %al # 8 bit address
The 00nput00 instruction 00indirect addressingmovw $0x3f8, %dx
inw (%dx), %ax # 16 bit address
inb (%dx), %al # 16 bit address
Reads from an I/O device to a register4
Addressing I/O Devices
The 00utput00 instruction 00direct addressingoutw %ax, $0xdd # 8 bit address
outb %al, $0xdd # 8 bit address
The 00utput00 instruction 00indirect addressingmovw $0x3f8, %dx
outw %ax, (%dx) # 16 bit address
outb %al, (%dx) # 16 bit address
Writes from a register to an I/O device5
Addressing I/O Devices
In some processor architectures (Motorola), there is no M/IO# signal in the control bus This is called using 00emory mapped I/O00/font> I/O device addresses are in the same address space as memory I/O device ports are accessed as memory addresses Use equivalent of 00ove00instructions to write or read data to or from I/O device registers as memory6
Addressing I/O Devices
COM port addresses (Neveln, Table 4.2)* corrects an error in the text
COM Port
I/O Addresses
1
2
3
4
0x3F8-0x3FF
0x2F8-0x2FF
0x3E8-0x3EF
0x2E8-0x2EF *
7
Accessing the Serial Port
PC specification allows up to four serial ports COM1: base address is 0x3f8 COM2: base address is 0x2f80x3f8
0x3fc
0x3fb
Write
Read
D7
D6
D5
D4
D3
D2
D1
D0
D7
D6
D5
D4
D3
D2
D1
D0
DLAB
Set
Brk
Evn
Par
Par
Enb
#
Stop
Loop
Out2
Out1
RTS
DTR
Len
Sel 1
Len
Sel 0
0x3fd
0x3fe
Same as Write
Same as Write
- - -
- - -
0
0
Stk
Par
DCD
RI
DSR
CTS
DCD
CHG
DSR
CHG
CTS
CHG
TE
RI
RX
ERR
TX
EMP
THRE
BRK
Int
FRM
ERR
PAR
ERR
OVRN
ERR
Data
RDY
0
8
Accessing the Serial Port
Don00 want to use hard coded numbers! Look at $pcinc/serial.h for symbolic constants#define COM1_BASE 0x3f8
#define COM2_BASE 0x2f8
#define UART_TX 0 /* send data */
#define UART_RX 0 /* recv data */
. . .
#define UART_LCR 3 /* line control */
#define UART_MCR 4 /* modem control */
#define UART_LSR 5 /* line status */
#define UART_MSR 6 /* modem status */
9
Parallel Serial Conversion
UART performs double buffered, bidirectional, parallel-to-serial / serial-to-parallel conversion:Transmit Holding
Register
Transmit Shift
Register
TXD
(Serial)
THRE
TX Empty
Data Bus
(Parallel)
RXD
(Serial)
Data Ready
Receive Shift
Register
Receive Holding
Register
Overrun Error
10
Strategies for I/O Driver Code
Two Basic Strategies for I/O Driver Code Status Polling Interrupt Driven Status Polling Uses only the port addresses on the I/O device Ties up the entire processor for the duration of I/O Interrupt Driven Adds an interrupt line from I/O device to processor Allows processor to do other work during I/O11
Status Polling
Review the serial port details: Status and Control Registers We will look at assembly language driver to send and receive data in 00ull duplex00 mode Half Duplex 00 Sending or receiving alternately (data going only one direction at a time) Full Duplex 00 Sending and receiving at same time (data going both directions simultaneously)12
Initializing the UART
Tutor does this for us on COM1: and COM2: Select speed, data bits, parity, and number of stop bits Turn on DTR and wait for DSR on Half duplex mode modem signal handshake: Transmit: Turn on RTS and wait for CTS on Receive: Turn off RTS and wait for DCD on Full duplex mode modem signal handshake: Turn on RTS and leave it on Transmit whenever CTS on Receive whenever DCD on13
Status Polling
Loop on send/receive data to/from COM2:(Assume Tutor has initialized bit rate and line control)
1. Turn on DTR & RTS, wait for DSR, CTS, & DCD
2. Read data ready (DR)
3. If data is ready, read a byte of receive data
4. Read transmit holding register empty (THRE)
5. If THR is empty, write a byte of transmit data
6. Jump back to step 2
Processor loop is much faster than byte transfer rate But, hard to do other work while looping on status14
Status Polling Assembly Code
Step 1a: Turn on DTR and RTSmovw $0x2fc, %dx # modem control
inb (%dx), %al # get current
orb $0x03, %al # or on 2 lsbs
outb %al, (%dx) # set control
15
Status Polling Assembly Code
Step 1b: Wait for DSR, CTS, and DCDmovw $0x2fe, %dx # modem status
loop1:
inb (%dx), %al # get current
andb $0xb0, %al # get 3 signals
xorb $0xb0, %al # check all 3
jnz loop1 # some missing
# all 3 are on now
16
Status Polling Assembly Code
Step 2: Read Data Ready Step 3: If ready, read a byte from receive dataloop2:
movw $0x2fd, %dx # line status
inb (%dx), %al # get data ready
andb $0x01, %al # look at dr
jz xmit # if recv data
movw $0x2f8, %dx # i/o data addr
inb (%dx), %al # move rx to %al
movb %al, somewhere # save it somewhere
movw $0x2fd, %dx # line status
17
Status Polling Assembly Code
Step 4: Read transmit holding register empty Step 5: If empty, write a byte to transmit dataxmit:
inb (%dx), %al # get thre
andb $0x20, %al # look at thre
jz loop2 # if tx hr empty
movb somewhere, %al # get data to send
movw $0x2f8, %dx # i/o data addr
outb %al, (%dx) # send it
jmp loop2 # and loop
18
COM Port Driver in C
#include <serial.h>
void pollputc(unsigned char ch)
{
/* polling loop, waiting for THRE bit to go on */
while ((inpt(COM1_BASE + UART_LSR) & UART_LSR_THRE) == 0)
;
/* output character */
outpt(COM1_BASE + UART_TX, ch);
}
19
Implementing a Comm Protocol
You can now write a 00ow-level00serial port driver! But, can you put a sequence of bytes out the serial port to implement standard communication protocol? Not without a lot more information about the protocol format for messages and procedures that are used to interact in a standard fashion with other devices That is a higher level 00rotocol00that is usually coded in some other portion of the system than the 00river00/font> Example of a protocol procedure is 00low control00/font>20
SW and HW Flow Control
A flow control mechanism allows receiver to stop transmitter from sending more characters than it is ready to handle (e.g. out of buffers) Software - Use X-ON and X-OFF characters Send X-OFF (ASCII DC3) to stop remote TX Send X-ON (ASCII DC1) to start remote TX Hardware - Use control signal leads Turn off RTS (crossed to CTS) to stop remote TX Turn on RTS (crossed to CTS) to start remote TX