Lecture5_IO Interfaces
Lecture5_IO Interfaces
CPU: Week
ARM Cortex-M 4
Curriculum Memory
and Bus
Week
5-6
Real-time Week
Operating systems 10-12
Project Week
15
Objectives
3
Outline
• Input and Output Programming
• I/O Mechanism: Polling vs. Interrupt
• Interrupt Mechanism
• Summary
4
I/O devices
• Usually includes some analog or nonelectronic component.
• Typical digital interface to CPU:
– Includes data, status, and control registers accessed by CPU.
– Some registers may be read-only, while others may be
readable or writable
Ctrl,
Status
mechanism
regs
CPU
data
regs
5
I/O Addressing: memory-mapped I/O and standard I/O
6
I/O Programming
• Two types of instructions can support I/O:
– special-purpose I/O instructions;
– memory-mapped load/store instructions.
• Intel x86 provides in, out instructions.
– provide a separate address space for I/O devices
• Most other CPUs (e.g. ARM processors) use
memory-mapped I/O:
– Memory-mapped I/O enables software to view these registers
as locations in memory.
– These registers can be accessed using only Load and Store
instructions
• I/O instructions do not preclude memory-mapped
I/O.
7
ARM memory-mapped I/O
8
I/O Programming
• A peripheral requires an initialization
process before it can be used
• Initialization process includes:
– Programming the clock control circuitry
– Configuring the operation mode of the I/O pins
• multiplexed I/O pins that can be used for multiple purposes be
necessary to configure to the expected modes (input/output
direction, function, etc.) electrical characteristics (e.g. voltage, pull
up/down, open drain, etc.).
– Peripheral configuration
– Interrupt configuration
9
Outline
• Input and Output Programming
• I/O Mechanism: Polling vs. Interrupt
• Interrupt Mechanism
• Summary
11
Polling vs. Interrupt
• Suppose a peripheral intermittently receives
data, which must be serviced by the processor
– The processor can poll the peripheral regularly to
see if data has arrived – wasteful (S/W solution)
– The peripheral can interrupt the processor when it
has data
• Int (H/W solution) requires an extra pin or pins:
If Int is 1, processor suspends current program,
jumps to an Interrupt Service Routine (ISR)
– Known as interrupt-driven I/O
– Essentially, “polling” of the interrupt pin is built-into
the hardware, so no extra time!
12
Polling
Polling
Continuously
checking the status of a
peripheral; e.g. read
data from an input
keyboard.
Polling is relatively
straightforward in
design and
programming with the
sacrifice of system
performance.
13
Interrupt
• Polling is very inefficient.
– CPU can’t do other work while testing device,
therefore hard to do simultaneous I/O.
– it is difficult to define priorities between different
services using polling
– it is not energy efficient
• Interrupts allow a device to change the flow of
control in the CPU.
– Causes subroutine call to handle device (interrupt
handler).
14
Interrupt
Interrupt Process
Device “interrupts” CPU to
indicate that it needs service. (These
events only occur if the interrupt is
enabled.)
CPU waits until the current
instruction has finished being
executed.
Save the contents of internal
registers of the CPU & the state
information within Control Unit
The PC is loaded with address of
the Interrupt Service Routine (ISR)
ISR is executed.
CPU returns to where it left off in
15
the main program.
Polling vs. Interrupt
Polling Interrupt
16
Outline
• Programming Input and Output
• Polling vs. Interrupt
• Interrupt Mechanism
• Summary
17
Interrupt
Interrupt Handler Features:
• Differs from subroutine because it is executed at any time due to
interrupt, not due to Call
18
Debugging interrupt code
• What if you forget to save registers?
– Foreground program can exhibit mysterious bugs.
– Bugs will be hard to repeat---depend on interrupt
timing.
Foreground Code:
y = Ax + b:
for (i = 0; i < M; i++) {
y[i] = b[i];
for (j = 0; j < N; j++)
y[i] = y[i] + A[i,j]*x[j];
}
19
Interrupt
• How to determine the address (interrupt
address vector) of the ISR?
– Fixed interrupt
• Address built into microprocessor, cannot be changed
• Either ISR stored at address or a jump to actual ISR
stored if not enough bytes available
– Vectored interrupt
• Peripheral must provide the address
• Common when microprocessor has multiple peripherals
connected by a system bus
20
Interrupt-driven I/O using fixed ISR location
22
Interrupt-driven I/O using fixed ISR location
23
Interrupt-driven I/O using fixed ISR location
24
Interrupt-driven I/O using fixed ISR location
4(a): The ISR reads data from Program memory μP Data memory
0x8000, modifies the data, and ISR
writes the resulting data to 16: MOV R0, 0x8000
17: # modifies R0 System bus
0x8001.
18: MOV 0x8001, R0
4(b): After being read, P1 19: RETI # ISR return
... Int P1 P1 P2
deasserts Int.
Main program 0
... PC 0x8000 0x8001
100: instructio
101: ninstruction 100
25
Interrupt-driven I/O using fixed ISR location
26
Interrupt-driven I/O using vectored interrupt
intr request
status
mechanism
intr ack reg
PC
IR
CPU
data/address data
reg
:CPU :device
receive
request
receive
ack
receive
vector
28
Interrupt-driven I/O using vectored interrupt
30
Interrupt-driven I/O using vectored interrupt
31
Interrupt-driven I/O using vectored interrupt
32
Interrupt-driven I/O using vectored interrupt
33
Interrupt-driven I/O using vectored interrupt
34
Interrupt-driven I/O using vectored interrupt
35
Quiz
37
Priorities and vectors
• Two mechanisms allow us to make interrupts
more flexible:
– Priorities determine what interrupt gets CPU first.
– Interrupt vectors Table allows the different
interrupting devices to be handled by different
handler
• Vectors number (or Interrupt number) determine what
code is called for each type of interrupt.
• Handler address points to Interrupt entry point in memory
• Mechanisms are orthogonal: most CPUs provide
both.
38
Interrupt vectors
Vector Number
Addres of handler 0 Vector 0
Addres of handler 1 Vector 1
Addres of handler 2 Vector 2
interrupt
Acknowledge
(m bits)
L1 L2 .. Ln
CPU
40
Prioritized interrupts
:interrupts :foreground :A :B :C
A,B
42
Prioritized interrupts
L1 L2 L3
CPU 43
Prioritized interrupts
L1 L2 L3
CPU
46
Interrupt Management in Cortex-M Processors
• Nested Vectored Interrupt Controller (NVIC)
4747
ARM Cortex-M’s Exception Model
• An exception may be an
internal interrupt or a
hardware error.
• Each exception has exception
number, priority number and
vector address
• Vector table base address is
fixed at 0x00
• Vector table is normally
defined in the startup codes
(startup.s)
48
ARM Cortex-M3’s Exception Model
SP value: the reset value of the
stack pointer.
Reset is invoked on power up or a
warm reset
A Non-Maskable Interrupt (NMI)
A HardFault is an exception that
occurs because of an error.
• A Supervisor Call (SVC) is an
exception that is triggered by the
SVC instruction
• PendSV is an interrupt-driven
request for system-level service
• a SysTick exception is generated
when the SysTick timer reaches
zero
49
ARM Cortex-M’s Exception Model
• Interrupt management
Excp. Number Exception Type Priority Excp. Number Exception Type Priority
11 SVC Programmable …
5050
ARM Cortex-M’s Exception Model
5151
ARM Cortex-M’s Exception Model
Vector Table Implementation (Startup.s in uVision):
; Vector Table Mapped to Address 0 at Reset
5353
ARM Exception Sequence
• CPU actions:
– Save PC. Copy CPSR to SPSR.
– Force bits in CPSR to record interrupt.
– Force PC to Exception handler.
• Handler responsibilities:
– Restore proper PC.
– Restore CPSR from SPSR.
– Clear interrupt disable flags.
54
ARM Exception Sequence
An exception entrance sequence contains:
1. Stacking of a number of registers
2. Fetching the exception vector
3. Fetching the instructions for the exception handler to be
executed.
4. Update NVIC and core registers
5555
ARM Exception Sequence
• Late arrival
• Pop preemption
5959
Exception handling optimization
Tail chaining:
6060
Exception handling optimization
Late arrival:
6161
Exception handling optimization
Pop preemption:
6262
Summary
• The two major styles of I/O are polled and interrupt driven
– Interrupts may be vectorized and prioritized
– Nested Vectored Interrupt Controller (NVIC) for interrupt handling
63
Quiz
64
Quiz
Q9: Why do most programs use interrupt-driven I/O over polling I/O? When
would you prefer to use polling I/O over interrupt-driven I/O?
65