Interrupts
Alexander Nelson
October 7, 2019
University of Arkansas - Department of Computer Science and Computer Engineering
What is an Interrupt?
Interrupt – signals a request to CPU to handle an event
CPU can grant request & jump to separate instruction memory to
handle
When finished, CPU returns to code running prior to interrupt
1
Interrupts
1
1https://virtualirfan.com/history-of-interrupts
2
Interrupt Overview
Sequence:
• Interrupt Event – What triggers the interrupt
• Interrupt Request – Flag set in register asking for handling
• ISR – Code executed to handle the interrupt event
3
Interrupt Service Routine
Interrupt Service Routine – ISR
Piece of code to handle a given interrupt event
Typically short and atomic (i.e. No Nested Interrupts)
Why?
4
Interrupt Service Routine
Interrupt Service Routine – ISR
Piece of code to handle a given interrupt event
Typically short and atomic (i.e. No Nested Interrupts)
Generally implemented inside a function with no parameters & no
return value
Why?
5
Interrupt Sources
Hardware Interrupts:
• Interact with external devices or peripherals
• May be with internal peripherals on chip (e.g. Timer/ADC)
Software Interrupts:
• Triggered by software commands – Usually special operating
system tasks
• e.g. Handling exceptions
6
Common Interrupt Sources
Common sources for interrupts:
• Input pin change
• Hardware timer overflow/compare-match
• Serial Peripheral Communication
• UART/SPI/I2C – Receive/Transmit Ready/Complete
7
Advantage over Software Polling
Avoid writing code that must frequently check status of pins/flags
Polling = “busy wait” – CPU executes executions waiting for an
event
Example:
while(!Timer1CompareFlag); //Wait for Timer1 to match
8
Interrupt Service Flow
After execution of each instruction, check any interrupts
If interrupt – save PC & jump to ISR
9
Interrupt Priority
Interrupt events can co-occur
Especially when waking from a sleep mode
How do you determine interrupt priority?
10
Interrupt Priority
Interrupt events can co-occur
Especially when waking from a sleep mode
How do you determine interrupt priority?
Static vs. Dynamic – Is priority defined at program or run time?
User vs. System defined – Is priority configurable?
11
Interrupt Vector Table
Interrupt Vector – “dedicated or configurable position in memory
specifying address to which execution should jump”
Interrupt Vector Table – Table of all program memory jumps for
each interrupt request
Maps events/requests to functions – Table of function pointers
12
Nested Vectored Interrupt Controller (NVIC)
13
Interrupt Vector Table
14
NVIC Registers
15
How is it implemented?
Often IVT implemented as list of unconditional jumps at top of
program memory
AtMega 328P Interrupt Vector Code16
Keeping ISR Short
ISRs affect normal execution of program
• Can block handling other interrupts
Common strategy for ISR is to keep as short as possible
Creates stable timing and avoids system from starvation
• Not being able to service other interrupts fast enough
Each ISR should do only what it needs to do at time of event
If long ISRs are needed, consider allowing nested interrupts
17
Interrupts and Tasks
Recall: Tasks
What is a task?
18
Recall: Tasks
What is a task?
Task – Unique continuously executing behavior
e.g.
• Flash LED
• Sample Sensor Input at given frequency
• Refresh LCD
19
Concurrent Tasks
Concurrent tasks – Tasks executed during same time window
How did we implement concurrency?
20
Concurrent Tasks
Concurrent tasks – Tasks executed during same time window
How did we implement concurrency?
State machines with different tick frequencies
21
Event-Driven Programming
Event-Driven – Flow of the program determined by events
Callback – executable code, passed as argument, expected to call
back argument at given time
Interrupt – signals a request to CPU to handle an event
ISR – executable code, determined by IVT, expected to call
function at given time
22
Example
Design a concurrent state machine that determines if an active low
button on pin A0 is depressed
If the button is pressed, then three LEDs on PortB cycle clockwise;
otherwise they cycle counter clockwise
23
Example
Modify the previous SM to do the same with event driven behavior
24