ELEC3300 06 Interrupt
ELEC3300 06 Interrupt
ELEC3300 06 Interrupt
Topic 6
Interrupt Organization
Prof. Vinod Prasad
Microcontroller Structure
A/D Port
Buffering and
Serial Port Direct Memory Access
CPU
(DMA)
External Memory Port
Memory,
Interfacing to Memory,
External Interrupt Port
Memory Timing
Interrupt and applications
External Timer Port Interfacing LCD
Organization Timer and
Counter Simple I/O Port
Motor Interfacing
In this course, STM32 is used as a driving vehicle for delivering the concepts.
To be covered In progress Done
PA1
PA2
PA3
Polling and
How do we address the needs of multiple devices?
Which device should be serviced first? What are the schemes? Interrupts
ELEC3300: Fall 2021 5
Polling
• Polled I/O requires the CPU to ask a device if the device requires servicing
– For example, if the devices have changed status
– Software polls the devices to know when a device will be serviced
Program code
applications do not have a
predictable delay (e.g. In loading a Description
printer buffer with text, the buffer can Ask the device Abstract idea of project
(Define the functionality of the
run out before the printer is polled system)
again and thus printing stops) Data format / representation
Programming Language
Communication Protocol
Physical connection (Pins
assignment)
Hardware devices
(Microcontroller, Peripherals)
Program code
• Configure a GPIO as an appropriate function
(say, input or output port) Ask the device
• Implementation
• Write a while-loop / if-then-else loop for checking
the status of the GPIO
Initialization
main()
Description
{
Abstract idea of project
Configure PA0 as input port (Define the functionality of the
while() system)
Data format / representation
{
Programming Language
Check if PA0 is pressed,
Communication Protocol
if yes, …. ; break
Physical connection (Pins
if not, …. ; back to while loop assignment)
implementation } Hardware devices
(Microcontroller, Peripherals)
}
Description
D1 D6 D7
Abstract idea of project Loop Back
(Define the functionality of the
system)
Data format / representation
Programming Language
D2 D5 D8
Communication Protocol
Physical connection (Pins
assignment)
Hardware devices
(Microcontroller, Peripherals) D3 D4 D9 Next Instruction
Program code
• Configure a GPIO as an appropriate function (say,
input or output port) Ask the devices
• Implementation
• Write a while-loop / if-then-else loop for checking
the status of the GPIO
Initialization main() * remark
{ Description
Configure PA0, PA1, …., as input ports * Abstract idea of project
while() (Define the functionality of the
system)
{
Check if PA0 is pressed, Data format / representation
9
Tradeoff: Programming is easy, Latency is large.
Interrupts
• Efficient alternative to Polling.
• Interrupt I/O allows the device to interrupt the CPU
announcing that the device requires attention
– This allows CPU to ignore devices unless they request ARM
servicing via interrupt
• Interrupts do not require code to loop until the device
is ready EXTI0
Disadvantage: Software does not know when an interrupt will occur, and this makes
it more difficult to write code
ELEC3300: Fall 2021 10
Interrupts
Interrupt I/O allows the device to interrupt the CPU announcing that the
device requires attention
Description
Send request to CPU Abstract idea of project
(Define the functionality of the
CPU system)
Data format / representation
Provide Interrupt Service
Programming Language
Communication Protocol
D2 D5 D8
D3 D4 D9
ELEC3300: Fall 2021 11
Interrupt Service Routine (ISR)
For every interrupt, there is a fixed location in memory that holds the
address of its ISR.
The table of memory locations set aside to hold the addresses of ISRs is
called as the Interrupt Vector Table.
ISR is also called device driver in case of the hardware interrupts and
called exception handler in the case of software interrupts. 12
Hardware Interrupt
An electronic alerting signal sent to the processor from an external device, like
a disk controller or an external peripheral.
Example: When we press a key on the keyboard or move the mouse, they
trigger hardware interrupts which cause the processor to read the keystroke or
mouse position.
Software Interrupt
CPU grabs address of the interrupt service handler (routine) from a vector table
(each interrupt has an index into the table)
14
Start of ISR •Popping the PC, registers etc onto the stack
•Enable subsequent interrupts of the same
type from occurring.
Program code
ISR
as interrupt signal) ………
Enable the IRQ channel
Clear the interrupt pending bit
while()
}
{
Program code
………
implementation }
}
Polling: You see the clock every few hours to check whether it
is time to wake up!
The most important reason why the interrupt method is preferable is that the polling
method wastes much of the microcontroller’s time by polling devices that do not need
service.
In Interrupt method, microcontroller can serve many devices based on the priority
assigned to it.
The polling method cannot assign priority because it checks all devices in a round-
robin fashion. ELEC3300: Fall 2021
18
Polling v/s Interrupt
INTERRUPT POLLING
1 The device notices the CPU that it requires its CPU steadily checks whether the device needs
attention. attention.
3 The device is serviced by interrupt handler (ISR). The device is serviced by CPU.
4 Interrupt can take place at any time. CPU steadily ballots the device at regular or
proper interval.
5 Interrupt request line is used as indication for Command ready bit is used as indication for
indicating that device requires servicing. indicating that device requires servicing.
6 Processor is evoked only when any device Processor waste many processor cycles by
interrupts it. repeatedly checking the command-ready little bit
of each device.
I/O devices such as printers, keyboards, etc. require that the CPU execute some code to
“service” the device. For example, incoming characters have to be read from a data
register on the peripheral interface and stored in a buffer.
If a serial interface can receive up to 1000 characters/second and can only store the last
character received, it must be checked at least once per millisecond to avoid losing data
→ Speed is a concern in polling.
Polling introduces a fixed overhead for each installed device.
Some factors to consider when deciding whether to use polling or interrupts include:
If the peripheral is very simple then it may not have been designed to generate
interrupts.
On the other hand, if the application is a controller that simply monitors some
sensors and controls some actuators then polling may be the best approach.
ELEC3300: Fall 2021 21
Choosing between Polling and Interrupts
What is the maximum time allowed between polls?
If the device needs to be serviced with very little delay then it may not be practical to
use polling.
If the rate at which the device is polled is much higher than the average transfer
rate, then a large fraction of polls will be “wasted”.
General Guideline (Take home Message): Use interrupts when the overhead due
to polling would consume a large percentage of the processor time or would
complicate the design of the software.
Considerations:
Are there many devices connected to the processor?
If yes, polling many devices will consume time, may cause delay in attending the
emergency.
If no, polling would be a suitable choice because immediate attention of the processor
is possible.
What if we use Interrupt in this case?
Interrupt latency is a concern.
ELEC3300: Fall 2021 23
Choosing between Polling and Interrupts
Practical Case Study-2:
In a general purpose microcomputer, a range of devices are connected which include
switches, keyboards, printers, modems, etc. Some of the devices operate at low speed
(such as a manually operated switch), while others operate at higher speeds. What
would be your choice – Polling or Interrupt?
Considerations:
Are there many devices connected to the processor?
What is the action response time (speed) expected by the devices – Some demands
immediate response whereas others can wait?
Skip chain
Problem with this approach is time wasted in ‘skip-chain’ processing, especially when
there are many possible interrupting devices. 26
Identification of Interrupting Device
• Instead of programmed device identification, the device supplies the CPU
with interrupt vector using an “Interrupt Acknowledge” bus cycle.
The interrupting device sends a self-identifying code back to the CPU during the
“Interrupt ACK” cycle
The device with the highest priority is placed at the first position followed by
lower priority devices (in decreasing order of priority).
The CPU responds to the interrupt by enabling the interrupt acknowledge
line. This signal is received by the Device-1. The acknowledge signal passes
to Device-2 only if Device-1 is not requesting an interrupt.
Common interrupt line request
Instead of the device communicating directly with the CPU, the PIC will
communicate with the CPU after checking the interrupts (PIC is like an
outsourcing agent – makes CPU free from interrupt checking procedures).
CONS:
- Extra chip.
- Cost.
- Power and
Space.
control
31
PIC suitable for large number of priority interrupts.
Nested Interrupts
• Interrupts can occur within interrupts Which has higher priority – ISR1 or ISR2?
What will be the timing diagram if ISR1 has higher priority over ISR2?
What will be the timing diagram if ISR3 that has a higher priority over ISR2
occurs when ISR2 is being executed?
ELEC3300: Fall 2021 32
STM32 Interrupts
• Nested vectored interrupt controller (NVIC)
Prioritizes interrupts, provides scheme for handling the
interrupts that occur when CPU is processing a previously
occurred interrupt, ensures that higher priority interrupts
are serviced first.
• Features:
- 68 interrupt lines
- 16 programmable priority levels
- Low-latency expectation and interrupt handlin
- Power management control: System Control
Registers used to control low-power features (e.g.,
sleep modes).
• Reference:
- Table 61 Vector table for connectivity line devices
(intended for applications where connectivity and real-time
performances are required - industrial control, control panels for
security applications, ethernet)
- Table 62 Vector table for XL-density devices
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where density 33
of Flash memory ranges between 768 Kbytes and 1 Mbyte.
STM32 External interrupt / event controller (EXTI)
Interrupts typically execute a few lines of code whereas Events do not
necessarily execute code but can signal another peripheral to do something
without processor intervention.
Events are normally handled synchronously: the program explicitly waits for
an event to be serviced, whereas an interrupt can demand service at any time.
Event example: A timer can generate an event to tell an ADC to sample and then write
the measured value to memory using DMA without ever waking up the processor.
• Features:
– Up to 20 edge detectors (rising/falling edge in EXTI line) in connectivity
line devices
– Up to 19 edge detectors in other devices for general event / interrupt
requests
– Independent trigger and mask on each interrupt / event line
– Dedicated status bit for each interrupt line
– Generation of up to 20 software event / interrupt requests (e.g. Timer) 34
STM32 Interrupts
Topic 6: Questions 5 - 8
Microcontroller Structure
A/D Port
Buffering and
Serial Port Direct Memory Access
CPU
(DMA)
External Memory Port
Memory,
Interfacing to Memory,
External Interrupt Port
Memory Timing
Interrupt and applications
External Timer Port Interfacing LCD
Organization Timer and
Counter Simple I/O Port
Motor Interfacing
In this course, STM32 is used as a driving vehicle for delivering the concepts.
To be covered In progress Done