Interfacing To The Real World: Peripheral Chip Interfacing - vs. Mapped Software Interaction With IO Chips
Interfacing To The Real World: Peripheral Chip Interfacing - vs. Mapped Software Interaction With IO Chips
Exceptions
User and Supervisor Mode Processing
Memory Management Unit (MMU) Interfacing
Software Interrupts - Trap instructions
1
IO Interfaces to the Real World
Most microcomputer systems are designed for use in small embedded applications
where they typically interface to and control data from the real world.
CPU manufacturers provide their own family of peripheral interface chips to make the
more common interfacing functions fairly straight forward and these chips are
interfaced to the CPU in more or less the same way as memory by hooking them up
to the Address and Data Buses and using R/W and Chip Selects off Address and IO
decoders.
Address and
RS-232
Serial Data Bus
Comms
CPU
(M6850) CPU
CPU
(68000)
PB1
Digital Parallel
Push PB2
IO
CPU
Button PB3 (M6821)
Inputs PB4
C07FFF
IO Ports
C00000 void func1()
main()
8FFFFF {{
char
char *IOports
*ports = = 0xC00000;
0xC00000;
Example
Memory char
char cx;;
RAM
Map
*IOports
*ports = = 0x55
0x55 ; ;
100000 cx==*(IOports
*(ports + +1)1); ;
007FFF
}}
ROM 4
000000
Peripheral Registers
All peripheral chips regardless of how they are mapped, contain a number of
internal registers that control the configuration of the chip and supply the CPU
with important status information about the device. In addition, read and write
registers facilitate communication between CPU and outside world.
Address and
RS-232 Serial Data Bus
Comms
Chip
Control
CPU CPU
CPU
Status
Read Data
Write Data
Parallel
IO
Chip
PB1 Control
Digital
PB2 CPU Status
Push
Button
PB3 Read Data
inputs
PB4 Write Data 5
Peripheral Registers
Example 6850 Serial Comms Chip Control Register
Bits in this register control important functions in the chip.
By reading this register we can determine the operating status of the chip.
8
Polled IO
Communicating with an IO Chip?
Once initial configuration of the chip has been performed in software, the
question of how to communicate with the outside world arises.
One simple approach is to poll each device in turn as part of a program loop.
That is, we read the status of the device by interrogating its status register.
When we determine that a device has something to say, we deal with it.
A system with 3 peripheral IO chips might contain a polling loop like this one.
while(1)
{
if( serial IO port status register indicates character received)
// Get Character from device and buffer it somewhere.
9
Polled IO
Advantages of Using Polling
Software is pretty easy to write, involving nothing more than a simple loop
and a few ‘if’ tests to interrogate each device status in turn.
10
Polled IO
Disadvantages of Using a Polling Loop
Adding more IO devices to the system, degrades the response time to any
one of them since it takes longer to poll more devices in software.
It is not very efficient. The execution time for one iteration of a polling loop
could take longer than the time required to generate the response.
Inputs from the real world should never be missed even if the system is
busy. The interrupt signal will stay asserted until the CPU deals with it.
The use of IO devices capable of generating an interrupt (not all of them can
or may not be able to generate them under the conditions you want).
More complex (multi-threaded) code to deal with the interrupt and route the
request for service to a users program where the data is then made
available to the main code (i.e. there are possibly problems of mutual
exclusion and thread synchronisation associated with the use of interrupts).
Interrupts are asynchronous to the operation of the rest of the system and
can occur with unpredictable frequency and timing relative to the execution
of the program, making it very difficult to debug.
Recreating the set of circumstances that led to the bug in your code is
virtually impossible since the real world cannot be single stepped and it may
be difficult to recreate the combination of asynchronous inputs that lead to14
the failure/bug being identified in the first place.
Interrupts: the Basics
An interrupt then is an asynchronous signal generated by an IO chip to
request service from a CPU.
15
Interrupts: the Basics
Identifies and prioritises which external IRQ pin(s) are being
asserted. If two different IRQ pins are active at the same instant the
highest priority IRQ is dealt with first.
Save additional CPU registers that might be overwritten by code within the ISR.
Identify the chip asserting the IRQ by interrogating it’s status register.
Deal with the reason for the interrupt (e.g. data has been received)
Clear the interrupt within the device requesting service.
Restore the additional registers saved above.
Execute a return from Interrupt (RTI) instruction at the end of the ISR which
restores the CCR and PC registers stacked earlier.
This last instruction (RTI) returns control of the CPU back to the
program immediately following the instruction where the interrupt
occurred. This action is summarised in the next diagram.
16
Interrupt Action (the basics)
Normal Processing
Interrupt(s)
Complete
Current
Instruction [Stack P
C, CCR,
jump to
ISR]
Interrupt Service Routine (ISR)
At a purely hardware level, the CPU has been designed to fetch the address of the
Interrupt Service Routine from a special Vector Table located in Rom (so that it is
always there when power is applied.) when the interrupt occurs.
The Vector Table also deals with Resets and other Exceptions in the same way.
The Vector Table contains one entry for each IRQ that the CPU has been designed
to deal with (2 for the 6811 external interrupts, 7 for the 68000).
Each entry in the vector table contains an address. It is to this address that the
CPU jumps and expects to find your ISR code to deal with the interrupt.
If you are designing your own embedded microcontroller application, you must
place the address of your ISR into the Vector Table before you blow the ROM.
XIRQ
Hardware Processing
Complete
Current Stack PC, CCR
Instruction
ROM Vector Table Store address of your XIRQ
Index into
Vector Table for FFC0 ISR in locations $FFF4-
XIRQ Entry $FFF5, i.e. ISR can be found
at $C000 in this example
Main Program
PC = $C000 Contents = $C000 XIRQ=[FFF4,FFF5]
Suspended for
Duration of ISR
FFFF
20
Maskable Interrupts
All interrupt capable peripherals have the ability to control whether the chip should
generate an interrupt or not via interrupt mask bits in the chips control register.
In addition, all CPU’s have the ability to postpone recognition of some or all of their
interrupts until later, by “masking” the recognition of the interrupt within the CPU
status or condition code register
In the case of the 6811, the IRQ and XIRQ external interrupts are controlled by the ‘I’
and ‘X’ bits in the 6811’s condition code register.
These interrupts are initially masked after a reset to prevent spurious interrupts being
recognised while the system is being initialised.
In the 6811 all internal memory mapped peripherals with interrupt generating
capability can be masked (disabled) by setting the ‘I’ bit in the 6811’s condition code
register
There are two assembly language instructions in the 6811 instruction set to change
the value of the ‘I’ bit
CLI - Clear Interrupt bit (allowing recognition of interrupts)
SEI - Set Interrupt bit (postpone recognition until ‘I’ cleared)
To enable the ‘X’ input, use the ‘TAP’ instruction – transfer accumulator A to condition
code register to clear the ‘X’ bit.
21
Interrupts/Exceptions on other Processors
The 68000, MIPS and ARM processor are typical of more modern 16/32/64 bit CPUs in that they offer a
wider range of prioritised interrupt levels.
Support for Vectored interrupt handling, (the Peripheral chip directly points the CPU to its ISR).
Interrupts are part of a more general ‘exception
handling’ policy which (for the 68000) is closely linked
to User and Supervisor states within the CPU (see later)
s I2 I1 I0
24
68000 Vectored Interrupts
Upon receipt of an Interrupt Request
The 68000 compares the level of the interrupt request with the value of its interrupt
mask flags (I2, I1, I0) in the Status register.
If the requested interrupt level is greater than (I2, I1, I0), the interrupt is serviced,
otherwise it is postponed.
The function code pins (FC2,FC1, FC0) are set to {111} to inform the system that an
interrupt acknowledge (IACK) is in progress
The level of the interrupt being acknowledged is placed on (A3, A2, A1).
A read of the data bus is performed (during which an intelligent peripheral has the
option to supply a vector number to the CPU during this IACK cycle).
An external interrupt acknowledge decoder, decodes the address lines
(A3, A2, A1) in conjunction with the function code pins (FC2,FC1, FC0) and asserts one
of seven IACK* lines that connect back to the peripheral.
The asserted IACK* line informs the interrupting device that it is about to be serviced.
25
68000 Vectored Interrupts
After the appropriate IACK* line is asserted by the 68000, the following
operations are performed.
The peripheral supplies it’s Vector number onto the data bus lines (D0-D7).
The 68000 reads the vector number on (D0-D7), shifts this left 2 places (x4),
and accesses the interrupt service routine using the vector number as an
index into the vector table.
Only one “vector capable” device should be wired directly to each IACK* line
to prevent multiple devices responding with their own vector number during
an interrupt acknowledge cycle.
FC0
1
Function Code IACK1*
[1,1,1] = IACK FC1
1 IACK2* 7
Cycle
FC2
1 Interrupt IACK3* Levels
A01 0 Acknowledge IACK4* of
IACK Level on 1 Decoder IACK5* IACK
Address Bus A02
0 IACK6*
A03 IACK7*
IACK1*
IACK2* 7
IACK3* Levels
IACK4* of
IACK5* IACK
IACK6*
IACK7*
IACK* IACK* IACK* IACK*
Propagator Propagator Propagator Propagator
Data Bus
29
DTACK*
Auto-Vectored Interrupts
Auto-vectored interrupts are intended for ‘dumber’ peripherals that cannot
supply a vector during an Interrupt acknowledge cycle like the 6850, 6821 etc.
The process is similar to Vectored interrupts except that after the appropriate
IACK* line is asserted during an interrupt acknowledge cycle by the 68000:
The interrupting device will assert the 68000’s VPA* line (Valid Peripheral
Address)
Upon receiving an asserted VPA* line, the 68000 knows that the peripheral
is a dumb device incapable of supplying a vector. The 68000 then:
Generates its own interrupt vector number internally based upon the priority
level of the IRQ* line that is being asserted.
The 68000 reserves vector numbers 25-31 for auto vectored interrupts on
IRQ1*-IRQ7*
Several peripherals can be assigned to the same IRQ* level, in which case
the appropriate auto-vectored interrupt handler routine will have to resort to
POLLING each of the possible peripheral by reading their interrupt status
registers to determine which of them is generating the interrupt.
30
Level 2 IRQ at CPU
68000 IRQ1*
Encoded
0 IRQ2*
IPL0* IRQ3*
Interrupt 1 Priority 7
IPL1* IRQ4* Levels
Request 0 Encoder of IRQ
Inputs IPL2* IRQ5*
IRQ6*
IRQ7*
FC0
1
Function Code IACK1*
[1,1,1] = IACK FC1
1 IACK2*
Cycle
FC2
1 Interrupt IACK3*
7
Levels
A01 0 Acknowledge IACK4* of
IACK Level on
A02 1 Decoder IACK5* IACK
Address Bus IACK6*
A03 0
IACK7*
IACK on level 2
IRQ on
Status Reg Vector Table Level 2
0
1 0 0
1 0
Stack Pointer IRQ*
S bit Reset Vector IACK
Interrupt Received
Mask bits set
to level of IRQ Get
being Vector 25
ISR
recognised Dumb
Vector 26
Peripheral
VPA
Generated
Address Bus
Data Bus
31
VPA*
Normal Processing
(User Mode)
From the state transition diagram below, we see that once the CPU enters user mode, the
only way to move to the Supervisor mode is via an exception, such as a reset, interrupt, etc.
An RTE instruction is used to restore the state of the CPU to that which it was in prior to the
exception occurring, for example to change state back to user mode.
The RTE instruction itself is privileged: You can only execute it in the supervisor mode
Exception: IRQ, Reset etc
User Supervisor
Mode Mode
33
Typical 68000 MMU Interface
MMU validates each access to memory made by the
code. If the code is running in supervisor mode, access
MMU is always allowed, if in user mode, address is validated
MMU against a set of look up tables and a Page
Address
Address fault/exception may be generated by the 68000’s
Decoder
Decoder BERR* signal to signal an invalid access
Logical
68000 Address Physical
CS
Address Memory/IO
Memory
Address Bus System
System
A1 – A23 Address
AS Memory ((Decoder
Decoder plus
plus
AS
Management Memory
Memory))
BERR Unit
Page Fault exception
Device1
Device1 Device2
Device1 Device3
Device1 Device4
Device1
Reset Interrupt
Push Button Operating
Operating System
System RTC
Device1 Device1
(Supervisor
(Supervisor Mode)
Mode)
SS == 11 Full
Full Privileges
Privileges
SS == 00 Limited
Limited Privileges
Privileges
User
User Program
Program 11 User
User Program
Program 21 User
User Program
Program 31
(User
(User Mode)
Mode) (User
(User Mode)
Mode) (User
(User Mode)
Mode)
36