Upload
carmel-moody
View
218
Download
0
Embed Size (px)
Citation preview
ECE291ECE291
Lecture 8Lecture 8
Peripheral devicesPeripheral devices
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 22 of 37 of 37
Lecture outlineLecture outline
Hardware I/O with portsHardware I/O with ports
8253 timer8253 timer
PC speakerPC speaker
Serial portSerial port
Parallel portParallel port
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 33 of 37 of 37
Port input and outputPort input and output
Used to communicate with many Used to communicate with many peripheral devices built into the processor, peripheral devices built into the processor, motherboard or expansion cardsmotherboard or expansion cards
ININ instruction transfers data from a device instruction transfers data from a device on the I/O bus to AL, AX, or EAXon the I/O bus to AL, AX, or EAX
OUTOUT instruction transfers data from AL, instruction transfers data from AL, AX, or EAX to a device connected to the AX, or EAX to a device connected to the I/O busI/O bus
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 44 of 37 of 37
Port input and outputPort input and output
Two forms of port addressingTwo forms of port addressing
Fixed-port addressingFixed-port addressing Allows an 8-bit I/O port address (use if port is 0 – 255)Allows an 8-bit I/O port address (use if port is 0 – 255) port number is immediate (follows the instruction opcode)port number is immediate (follows the instruction opcode)
IN AL, 6AhIN AL, 6Ah ;data from I/O address 6Ah is input to AL;data from I/O address 6Ah is input to AL
Variable-port addressingVariable-port addressing Allows using 16-bit port address (addresses up to FFFFh)Allows using 16-bit port address (addresses up to FFFFh) the I/O port number is stored in register DXthe I/O port number is stored in register DX
MOV DX, 0FA64hMOV DX, 0FA64h
OUT DX, AX OUT DX, AX ;transfers contents of AX to I/O port FA64h;transfers contents of AX to I/O port FA64h
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 55 of 37 of 37
Port I/O examplePort I/O example..START
IN al, 61h ;read port 61h: PC speaker control register
OR al, 3 ;set bits 0 and 1
OUT 61h, al ;turn speaker on
MOV cx, 1000h ;delay count; if the count is increased,
;the beep will become longer
.L1 LOOP .L1 ;time delay – spin 1000h times
IN al, 61h
AND al, 0fch ;clear bits 0 and 1
OUT 61h, al ;turn speaker off
MOV ax, 4c00h ;Normal DOS Exit
INT 21h
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 66 of 37 of 37
Keyboard controlKeyboard controlThe keyboard is connected to the 8255 Programmable The keyboard is connected to the 8255 Programmable Peripheral Interface (PPI) chip, a simple self-contained Peripheral Interface (PPI) chip, a simple self-contained or embedded microprocessoror embedded microprocessor
Contains three registers A, B, and C which are accessed on Contains three registers A, B, and C which are accessed on ports ports 60h-62h60h-62h using the in/out instructions using the in/out instructions
When a key is pressed or released two events take When a key is pressed or released two events take place:place:
an 8-bit scan code is deposited in an 8-bit scan code is deposited in register Aregister A of the PPI of the PPI
an an INT 09hINT 09h hardware interrupt is generated hardware interrupt is generated
Default ISR gets the scan code in A, acknowledges the hardware Default ISR gets the scan code in A, acknowledges the hardware interrupt, and passes information to the keyboard buffer (a memory interrupt, and passes information to the keyboard buffer (a memory buffer in BIOS data area)buffer in BIOS data area)
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 77 of 37 of 37
Keyboard controlKeyboard control
Each key action is stored in the buffer as a wordEach key action is stored in the buffer as a word normal charactersnormal characters are stored with ASCII code for the character, are stored with ASCII code for the character,
and the scan code for the keyand the scan code for the key extended codesextended codes corresponding to special keys e.g., cursor corresponding to special keys e.g., cursor
keys, shiftkeys, shift
The keyboard buffer is a The keyboard buffer is a circular buffercircular buffer, when it gets full , when it gets full and a new key is pressed:and a new key is pressed:
INT 09 ISR generates a “bell” character, which causes the INT 09 ISR generates a “bell” character, which causes the speaker to beepspeaker to beep
The data from the new key press is discardedThe data from the new key press is discarded
Keyboard buffer holds keystrokes until read by an int 16Keyboard buffer holds keystrokes until read by an int 16
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 88 of 37 of 37
8253 Programmable Timer8253 Programmable TimerIntel 8253 contains 3 independent 16-bit programmable Intel 8253 contains 3 independent 16-bit programmable counterscounters
Operate on a clock frequency of 14.31818 MHzOperate on a clock frequency of 14.31818 MHz
This frequency is divided by 12 before being sent to the This frequency is divided by 12 before being sent to the timer so the pulses arrive at frequency 1.19318 MHztimer so the pulses arrive at frequency 1.19318 MHz
Each timer can be loaded with a sixteen bit starting Each timer can be loaded with a sixteen bit starting count (0 – 65535)count (0 – 65535)
Once enabled, timers repeatedly count down to 0 from Once enabled, timers repeatedly count down to 0 from the starting count and generate an output signalthe starting count and generate an output signal
The rate of the output signal is (1193180 / starting The rate of the output signal is (1193180 / starting count) outputs per secondcount) outputs per second
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 99 of 37 of 37
8253 Programmable Timer8253 Programmable Timer
Timer 0Timer 0 Generates and INT 08h when the timer expires. Can be used to Generates and INT 08h when the timer expires. Can be used to
trigger regularly recurring events in your programs.trigger regularly recurring events in your programs. Counts down from as high as 65535 to zeroCounts down from as high as 65535 to zero The output signal occurs each (1.19318 * 10The output signal occurs each (1.19318 * 1066)/ 65535 = 18.2 Hz)/ 65535 = 18.2 Hz
Timer 1Timer 1 Causes the DRAM memory system to be refreshedCauses the DRAM memory system to be refreshed If you interfere with this function, the entire contents of the If you interfere with this function, the entire contents of the
system memory could be lostsystem memory could be lost
Timer 2Timer 2 General purpose, and optionally connected to the PC speaker General purpose, and optionally connected to the PC speaker
for generating simple tonesfor generating simple tones
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 1010 of 37 of 37
Timer control registersTimer control registers
8253 has four byte-sized control registers 8253 has four byte-sized control registers which are mapped to I/O ports 40h-43h which are mapped to I/O ports 40h-43h
PortPort DescriptionDescription
40h 40h Timer 0 Count Register Timer 0 Count Register
41h 41h Timer 1 Count Register Timer 1 Count Register
42h42h Timer 2 Count Register Timer 2 Count Register
43h43h Timer Control RegisterTimer Control Register
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 1111 of 37 of 37
TimerTimer
The Control Register allows the programmer to select The Control Register allows the programmer to select the counter, mode of operation, and type of operationthe counter, mode of operation, and type of operation
Bits Bits DescriptionDescription 7,67,6 Counter ID (00,01,10)Counter ID (00,01,10)
5,45,4 00: latch count for reading00: latch count for reading 01: Read/Set least significant byte 01: Read/Set least significant byte 10: Read/Set most significant byte 10: Read/Set most significant byte 11: Read/Set least significant byte then most 11: Read/Set least significant byte then most
significant byte significant byte
3,2,1 3,2,1 Count Mode (011=Square Wave) Count Mode (011=Square Wave) 0 0 Count Method: 0=Binary, 1=BCD Count Method: 0=Binary, 1=BCD
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 1212 of 37 of 37
Speaker controlSpeaker control
Timer 2 output from 8253 can be enabled as an analog Timer 2 output from 8253 can be enabled as an analog input to the PC speakerinput to the PC speakerEnable timer input to speaker (port 61h, bit 1) Enable timer input to speaker (port 61h, bit 1) Enable speaker (port 61h, bit 0) Enable speaker (port 61h, bit 0)
Write to Timer Control Port Write to Timer Control Port movmov al,10110110b al,10110110b outout 43h, al43h, al
10:10: Control timer 2 Control timer 2 11:11: Load Low Byte then High Byte Load Low Byte then High Byte
011:011: Select Square wave output Select Square wave output 0:0: Count in binaryCount in binary
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 1313 of 37 of 37
Speaker ControlSpeaker Control
Enable Speaker Enable Speaker ININ al, 61hal, 61hOROR al, 00000011bal, 00000011bOUTOUT 61h, al61h, al
Bit 1: Enable Timer Bit 1: Enable Timer
Bit 0: Enable Speaker Bit 0: Enable Speaker
Don't change other bitsDon't change other bits
Write count to timer Write count to timer OUT OUT 42h, al42h, al
MOVMOV al, ah al, ah OUT OUT 42h, al42h, al
AX=count (ticks) = 1193180 (ticks/sec) / Freq (1/sec) AX=count (ticks) = 1193180 (ticks/sec) / Freq (1/sec)
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 1414 of 37 of 37
ExampleExampleCR EQU 13LF EQU 10extrn dosxit, kbdin, dspmsg, binasc
; Define stack segmentstkseg SEGMENT stack
RESB 512stktop
; Define code segment cseg SEGMENT CODE; Variable declarationspbuf RESB 7crlf db CR,LF,'$' freq_msg db ' Freq = $' count_msg db ' Hz, Count = $'
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 1515 of 37 of 37
Example Example ..start..start
mainmain
mov ax, cs ; Initialize DS registermov ax, cs ; Initialize DS register
mov ds, axmov ds, ax
mov mov al,10110110b al,10110110b ; Timer2, Load L+H, square wave, ; Timer2, Load L+H, square wave, ; binary; binary
out out 43h,al 43h,al ; Write timer control byte ; Write timer control byte
in in al,61h al,61h
or or al,00000011b al,00000011b ; Enable Timer2 & Turn on speaker; Enable Timer2 & Turn on speaker
out out 61h,al61h,al
.mloop.mloop
call call kbdinkbdin
cmp cmp al,'0' al,'0' ; 0 to quit; 0 to quit
je je .mdone.mdone
sub sub al,'0' al,'0'
mov mov ah,0 ah,0
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 1616 of 37 of 37
ExampleExamplemov cl,8 ; Freq * 256 (Hz) shl ax,cl
mov dx, freq_msg call dspmsg mov bx, pbuf call binasc mov dx, pbuf call dspmsg ; Print Freq mov bx,ax mov ax,34DCh ;DX:AX=1,193,180 (tics/sec)mov dx,12h ;AX = ----------------------div bx ;BX=Freq (1/sec)mov dx, count_msg call dspmsg mov bx, pbuf call binasc
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 1717 of 37 of 37
ExampleExamplemov dx, pbuf
call dspmsg ; Print count
mov dx, crlf
call dspmsg
out 42h,al ; Write Low byte to Timer Channel 2
mov al,ah
out 42h,al ; Write High byte to Timer Channel 2
jmp .mloop
.mdone
in al,61h
and al,11111100b ; Turn off speaker
out 61h,al
call dosxit ; Exit to DOSmain
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 1818 of 37 of 37
Serial communicationSerial communication
One of the oldest, simplest, and most widely One of the oldest, simplest, and most widely used forms of communication in the world.used forms of communication in the world. Smoke signalsSmoke signals Morse codeMorse code Early computer miceEarly computer mice Classical mainframe/terminal computingClassical mainframe/terminal computing USB printers, keyboards, scanners, camerasUSB printers, keyboards, scanners, cameras EthernetEthernet The InternetThe Internet Digital cell phonesDigital cell phones Digital satellitesDigital satellites
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 1919 of 37 of 37
Serial communicationSerial communication
Can be implemented with as few as two Can be implemented with as few as two (unidirectional) or three (bidirectional) wires(unidirectional) or three (bidirectional) wires Common groundCommon ground Transmit line (Tx)Transmit line (Tx) Receive line (Rx)Receive line (Rx)
One bit transferred at a timeOne bit transferred at a time Takes numerous transfers to move a single byte, but Takes numerous transfers to move a single byte, but
that’s okaythat’s okay Data must be broken down into chunks to transferData must be broken down into chunks to transfer Maybe we want some error checking tooMaybe we want some error checking too
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 2020 of 37 of 37
Serial communicationSerial communication
Serial or communications ports are nothing more Serial or communications ports are nothing more than shift registers connected to specific I/O than shift registers connected to specific I/O portsports For transmitting, write data to the port corresponding For transmitting, write data to the port corresponding
to the shift registerto the shift registerShift register moves data out one bit at a time over a serial Shift register moves data out one bit at a time over a serial cablecable
For receiving, the shift register accepts bits as they For receiving, the shift register accepts bits as they come in, shifting them into the register.come in, shifting them into the register.
Read the data from the shift register using the appropriate Read the data from the shift register using the appropriate port addressport address
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 2121 of 37 of 37
Serial communicationSerial communication
Start bit indicates when a communication frame Start bit indicates when a communication frame beginsbeginsNext 5-8 bits are the actual data Next 5-8 bits are the actual data Number of data bits is configurableNumber of data bits is configurable 8-bits is very popular because of its obvious 8-bits is very popular because of its obvious
correspondence with the size of a bytecorrespondence with the size of a byte
Start Bit 5-8 Data Bits Parity Bit
Stop Bit(s)
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 2222 of 37 of 37
Serial communicationSerial communication
The parity bit indicates if the number of ones The parity bit indicates if the number of ones was even (0) or odd (1). Helpful with error was even (0) or odd (1). Helpful with error checking.checking.Stop bits (1, 1.5 or 2) indicate the end of a data Stop bits (1, 1.5 or 2) indicate the end of a data frameframe
Start Bit 5-8 Data Bits Parity Bit
Stop Bit(s)
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 2323 of 37 of 37
Serial communicationSerial communication
Bit or baud rate determines how quickly the bits Bit or baud rate determines how quickly the bits are shifted out of the serial portare shifted out of the serial port Can be as low as 110 bits per secondCan be as low as 110 bits per second Can be as high as 115000 bits per second with Can be as high as 115000 bits per second with
standard serial ports (USB is much faster)standard serial ports (USB is much faster)
Actual rate of data transfer depends port settingsActual rate of data transfer depends port settings 8 data bits, no parity, one stop bit means that 8 of 8 data bits, no parity, one stop bit means that 8 of
every 10 bits that transfer contain useful dataevery 10 bits that transfer contain useful data 5 data bits, parity, 2 stop bits means that only 5 of 5 data bits, parity, 2 stop bits means that only 5 of
every 9 bits contain useful dataevery 9 bits contain useful data
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 2424 of 37 of 37
Serial communicationSerial communication
For serial ports to work, both ends must For serial ports to work, both ends must agree on all the previous settingsagree on all the previous settings
Once that’s done, the hardware takes care Once that’s done, the hardware takes care of the messy detailsof the messy details
From the programming side of things, you From the programming side of things, you have to configure the port, then know how have to configure the port, then know how to write data properlyto write data properly
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 2525 of 37 of 37
Serial communicationSerial communication
Programming the serial portProgramming the serial port First you should configure the portFirst you should configure the port
Stop bits, data bits, parity, bit rateStop bits, data bits, parity, bit rate To read, check status register until a byte is ready to To read, check status register until a byte is ready to
be read, then read itbe read, then read it To write, check status register until serial port is To write, check status register until serial port is
ready, then write the byteready, then write the byte
This particular paradigm is called pollingThis particular paradigm is called pollingSerial ports also generate hardware interrupts Serial ports also generate hardware interrupts when they receive data, but we won’t be going when they receive data, but we won’t be going that far in MP2that far in MP2
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 2626 of 37 of 37
Serial communication - regsSerial communication - regs
For configuring the portFor configuring the port Line control (LCR) – Specifies the data frame (word length, Line control (LCR) – Specifies the data frame (word length,
number of stop bits, and type of parity) for transmitting and number of stop bits, and type of parity) for transmitting and receivingreceiving
Divisor latch low (DLL) – Stores lower 8 bits of the baud-rate Divisor latch low (DLL) – Stores lower 8 bits of the baud-rate generator divisorgenerator divisor
Divisor latch high (DLH) – Stores the upper 8 bits of the baud-Divisor latch high (DLH) – Stores the upper 8 bits of the baud-rate generator divisorrate generator divisor
For transferring dataFor transferring data Line status (LSR) – Contains the transmitter empty, transmit Line status (LSR) – Contains the transmitter empty, transmit
buffer empty, receive buffer full, and receive error flagsbuffer empty, receive buffer full, and receive error flags Receive buffer (RBR) – Holds the byte receivedReceive buffer (RBR) – Holds the byte received Transmit buffer (TBR) – Holds the byte to transmitTransmit buffer (TBR) – Holds the byte to transmit
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 2727 of 37 of 37
Serial communication - regsSerial communication - regs
Each serial port has a unique Port I/O address Each serial port has a unique Port I/O address
DeviceDevice Serial port base addressSerial port base address
COM0:COM0: 03F8h 03F8h
COM1:COM1: 02F8h02F8h
COM2:COM2: 03E8h03E8h
COM3:COM3: 02E8h02E8h
The serial port base addresses are stored at 0000:0400 – 0407. You The serial port base addresses are stored at 0000:0400 – 0407. You can view these addresses directly using can view these addresses directly using debugdebug or or td.td.
At the command prompt, type “debug” to get the “-” debug promptAt the command prompt, type “debug” to get the “-” debug prompt type type d0:0400d0:0400 (and hit return) (and hit return)
you get : you get : 0000:04000000:0400 F8 03 F8 02 E8 03 E8 02F8 03 F8 02 E8 03 E8 02 the first four words are addresses of the installed comm portsthe first four words are addresses of the installed comm ports
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 2828 of 37 of 37
Register addressesRegister addresses
DLL = Base + 0DLL = Base + 0 Depends on a setting in LCRDepends on a setting in LCR
TBR = Base + 0TBR = Base + 0 Write onlyWrite only
RBR = Base + 0RBR = Base + 0 Read onlyRead only
DLH = Base + 1DLH = Base + 1LCR = Base + 3LCR = Base + 3LSR = Base + 5LSR = Base + 5
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 2929 of 37 of 37
Line control registerLine control register
Must set DLAB to Must set DLAB to access DLL and DLHaccess DLL and DLH
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 3030 of 37 of 37
Baud rate divisorBaud rate divisorSetting the baud rateSetting the baud rate
Based off 1.8432 MHz Based off 1.8432 MHz clock frequencyclock frequency
Baud rate = frequency / Baud rate = frequency / (16 x divisor)(16 x divisor)
You set the divisor in a You set the divisor in a special control register special control register within the serial portwithin the serial port
Divisor Baud Rate1 1152002 576003 384004 288006 192008 1440012 960024 4800384 300
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 3131 of 37 of 37
Line status registerLine status register
Check RBF to see if a Check RBF to see if a byte has been byte has been receivedreceived
Check TE or TBE to Check TE or TBE to see of a byte has see of a byte has been transmittedbeen transmitted
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 3232 of 37 of 37
Parallel interfaceParallel interface
Originally designed for use with printers onlyOriginally designed for use with printers only
Provides 8-bit data transfer (originally output only, now bi-Provides 8-bit data transfer (originally output only, now bi-directional)directional)
Provides TTL (5V) external I/OProvides TTL (5V) external I/O
When the computer boots it detects any parallel ports and When the computer boots it detects any parallel ports and assigns ‘logical’ port names to them (LPT1, LPT2, LPT3)assigns ‘logical’ port names to them (LPT1, LPT2, LPT3)
Each parallel port uses three processor I/O portsEach parallel port uses three processor I/O ports datadata output (to send a byte of data to a printer you send the data output (to send a byte of data to a printer you send the data
to the output data register)to the output data register) statusstatus signals from the printer signals from the printer control/commandscontrol/commands to the printer to the printer
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 3333 of 37 of 37
Parallel interfaceParallel interface
Each logical device has a unique Port I/O address Each logical device has a unique Port I/O address
DeviceDevice Data PortData Port Status PortStatus Port Control PortControl Port
LPT0:LPT0: 3BCh3BCh 3BDh3BDh 3BEh 3BEh
LPT1:LPT1: 378h378h 379h379h 37Ah 37Ah
LPT2:LPT2: 278h278h 279h279h 27Ah 27Ah
The parallel port base addresses are stored at 40:0008h, and can The parallel port base addresses are stored at 40:0008h, and can be viewed by using be viewed by using debugdebug or or tdtd
At the command prompt, type “debug” to get the “-” debug promptAt the command prompt, type “debug” to get the “-” debug prompt type type d40:0008d40:0008 (and hit return) (and hit return) you get : you get : 0040:00080040:0008 BC 03 78 03 78 02BC 03 78 03 78 02 … … the first six bytes are addresses of the installed printer adaptersthe first six bytes are addresses of the installed printer adapters
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 3434 of 37 of 37
Parallel interface major signalsParallel interface major signals
Signal(s)Signal(s) Input/OutputInput/Output NotesNotes
Data[7:0]Data[7:0] Bi-directionalBi-directional Byte-wide data bus Byte-wide data bus
StrobeStrobe To deviceTo device Write signal Write signal
BusyBusy From deviceFrom device Don't send more data Don't send more data
AckAck From deviceFrom device Acknowledge interrupts Acknowledge interrupts
InitializeInitialize To deviceTo device Initialize external device Initialize external device
Out of PaperOut of Paper From deviceFrom device Status signal Status signal
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 3535 of 37 of 37
Writing to the parallel interfaceWriting to the parallel interface
ECE 291 Lecture 8ECE 291 Lecture 8
Page Page 3636 of 37 of 37
Control & Status PortsControl & Status Ports
Control PortControl Port
Bit DescriptionBit Description
7-5 Unused 7-5 Unused
4 IRQ Enable 4 IRQ Enable
3 Select [inverted]3 Select [inverted]
2 Initialize 2 Initialize
1 AutoFeed [inverted]1 AutoFeed [inverted]
0 Strobe [inverted]0 Strobe [inverted]
Status PortStatus Port
Bit DescriptionBit Description
7 Busy [inverted]7 Busy [inverted]
6 Ack 6 Ack
5 Out-of-Paper5 Out-of-Paper
4 Selected4 Selected
3 I/O Error 3 I/O Error
2-0 Unused2-0 Unused