Interrupcions: Processor - I/O Speed Mismatch
Interrupcions: Processor - I/O Speed Mismatch
• Input: device may not be ready to send data as fast as the processor
loads it
– Also, might be waiting for human to act
• What to do?
1
Synchronization: Mechanisms
Blind Cycle: Software simply waits for a fixed amount of time and assumes the I/O
will complete after that fixed delay.
Usage?
Where I/O speed is short and predictable
Gadfly (busy waiting, polling): is a software loop that checks the I/O status waiting
for done status.
Usage?
When real time response is not important (CPU can wait)
Interrupts: uses hardware to cause special software execution i.e. input device will
cause interrupt when it has new data!
Usage?
When real time response is crucial
Periodic Polling: Uses a clock interrupt to periodically check the I/O Status (i.e. The
MCU or CPU will check the status)
Usage?
In situations that require interrupts but the I/O device does not support requests
DMA: Transfer data directly to/from memory or I/O without CPU intervention.
Usage?
In situations where Bandwidth and latency are important.
8-bit
Data Out Port DATA
2
Gadfly (Busy Waiting)
Interrupts
Event
main ( )
Triggers
interrupt {
signal
• Several steps have to be
completed by the processor to
}
return to the instruction
following the instruction that was
interrupted.
Handle
Interrupt
3
Support for interrupts
Questions
• Who keeps track of status of all the devices, handle errors, know
where to put/supply the I/O data?
4
Some parameters
Interrupts
• An I/O interrupt is like overflow exceptions except:
– An I/O interrupt is “asynchronous”
– More information needs to be conveyed
5
Interrupts, traps & exceptions
Interrupts: sources
Interrupts from on-chip resources.
examples: Serial Interface Module, timer overflow, ADC, …
External Interrupts.
Examples: Input ports, IRQ line…
Software Interrupts.
Exceptions.
Examples: Opcode Trap, stack overflow, divide by zero…
6
PIC18 interrupt logic
7
INTCON
INTCON2
8
INTCON3
RCON
9
PIR1
IPR1
10
PIE1
Resum
11
PIC18 Interrupt Operation
- All interrupts are divided into core group and peripheral group.
The following interrupts are in the core group:
1. INT0…INT2 pin interrupts
2. TMR0 overflow interrupt
3. PORTB input pin (RB7…RB4) change interrupts
- The interrupt sources in the core group can be enabled by setting the GIE bit and the
corresponding enable bit of the interrupt source.
Ex: To enable TMR0 interrupt, one must set both the GIE and the TMR0IE bits to 1.
- The interrupts in the peripheral group can be enabled by setting the GIE, PEIE, and the
associated interrupt enable bits.
Ex: To enable A/D interrupt, one needs to set the GIE, PEIE, and the ADIE bits
- In order to identify the cause of interrupt, one need to check each individual interrupt
flag bit.
- When an interrupt is responded to, the GIE bit is cleared to disable further interrupt,
the return address is pushed onto return address stack and the PC is loaded with the
interrupt vector.
- Interrupt flags must be cleared in the interrupt service routine to avoid reiterative
interrupts.
12
Interrupt Programming
Step 1. Write the interrupt service routine and place it in the predefined program memory.
org ox08
… ; interrupt service for high priority interrupts
retfie
org 0x18
… ; interrupt service for low priority interrupts
retfie
Step 3. Set the appropriate interrupt enable bits to enable the desired interrupt.
13
High-priority interrupt structure
- High-priority interrupts are able to suspend the execution of low-priority ones
- When a high-priority interrupt occurs, STATUS, WREG and BSR registers are
automatically copied to shadow registers
- retfie FAST restores the shadow registers, the PC, and sets GIEH bit
- Never use retfie FAST with low-priority interrupts
Example
- By enabling the INT0 interrupt, the INT0 pin will generate an interrupt to the CPU
once every second.
- The interrupt service routine simply increments a memory counter, outputs it to the Port
D pins, and returns.
- A LED is turned on when its driving Port D pin outputs a low.
14
example
#include <p18F45K22.inc>
count equ 0x00
org 0x00
goto start
org 0x08
goto ISR_hi
org 0x18
retfie
start setf count ; count down from 255
clrf TRISD ; configure port D for output
movff count,PORTD ; output count to LEDs
bsf RCON,IPEN ; enable priority interrupt
movlw 0x90 ; enable INT0 interrupt
movwf INTCON ; and clear INT0IF flag
forever nop
bra forever
15
Example -C language version-
#include <xc.h>
- The user can use the retfie fast instruction to retrieve these registers before returning
from the interrupt service routine.
- One can save additional registers in the stack if the interrupt service needs to modify
these registers. These registers must be restored before returning from the service
routine.
- In C language, one can add a save clause to the #pragma statement to inform the C
compiler to generate appropriate instructions for saving additional registers.
#pragma interrupt high_ISR save = reg1,…, regn
#pragma interrupt low_ISR save = reg1,…, regn
16
Saving context in RAM
If retfie fast is not used
Resets
- Resets can establish the initial values for the CPU registers, flip-flops, and control
registers of the interface chips so that the computer can function properly.
- The reset service routine will be executed after the CPU leaves the reset state.
- The reset service routine has a fixed starting address and is stored in the read only
memory.
- The PIC18 can differentiate the following types of reset:
1. Power-on reset (POR)
2. MCLR pin reset during normal operation
3. MCLR pin reset during sleep mode
4. Watchdog timer (WDT) reset (during normal operation)
5. Programmable brown-out reset (BOR)
6. RESET instruction
7. Stack full reset
8. Stack underflow reset
17
Resets vs. Interrupts
Reset/interrupts: What are the similarities and differences?
Similarities:
1. Both are extraordinary events that cause the MPU to deviate from fetch &
decode (i.e., asynchronous)
2. Both cause the MPU to copy a special address to its Program Counter and
execute a different program (ISR).
Differences:
1. Resets cause MPU to abort its normal fetch and execute then prepares it to start
from scratch
2. Interrupts cause MPU to abort its normal fetch and execute but returns the MPU
to the instruction following the one that was interrupted.
Interrupt latency
The 8-bit PIC-16/PIC-18 MCUs take 12 to 20 clock cycles to get to the ISR — depending
on the type of instruction that was in progress at interrupt time.
Then, in the ISR, the compiler will add instructions to determine where the interrupt
originated and to push some registers.
18