Professional Documents
Culture Documents
Interrupt
Spring 2023
Merriam-Webster:
“to break the uniformity or continuity of”
2
Polling vs Interrupt
Polling:
You pick up the phone every three seconds
to check whether you are getting a call.
Interrupt:
Do whatever you should do and pick up the
phone when it rings.
4
Polling vs Interrupt
device CPU
Interrupt-driven operations
Allows CPU to perform other main()
tasks until external/internal Busy
devices require service
CPU automatically stops the
current code and starts to
Ready
execute an interrupt handler
(or called interrupt service ISR()
routine, ISR)
time
5
Polling vs Interrupt
Polling Interrupt
Software periodically checks CPU only takes actions only if an
event occurs
Waste lot of CPU cycles Does not waste much CPU cycle
Triggered by software Triggered by hardware or software
Occurs periodically Occurs at any time
6
How to support interrupt?
Interrupt
Interrupt Request
Interrupt
Request Execution
Controller
Core
Flash
Microcontroller Chip
7
How to support interrupt?
Interrupt
Interrupt Request
Interrupt
Request Execution
Controller
Core
Interrupt
Interrupt Request
Request NVIC Execution
Core
Nested Vectored
Interrupt Controller
Coordinates multiple interrupt sources
UART • Enable and disable a specific interrupt
• Which one service first (interrupt priority)
• How to locate the corresponding ISR?
• How to resume the code that has been suspended?
Microcontroller Chip
9
Link Register for calling functions
void foo(void) ;
void foo (void) {
● ● ●
int main(void){
● ● ●
● ● ● return;
foo(); }
● ● ●
}
Compiler
LR = PC + 4
● ● ● PC = foo
PC BL foo foo PROC
PC + 4 ● ● ● ● ● ●
● ● ●
BX LR
ENDP
When software calls a subroutine, LR holds the returning address.
Interrupt Vector Table ( IVT)
Each interrupt has an interrupt service routine(ISR) defined somewhere in the code
memory.
How the processor determines where the ISR is located in code memory for the
specific interrupt?
microcontrollers make use of interrupt vector tables to find the starting address
of ISR routines.
11
Interrupt Vector Table ( IVT)
Vector table is a table that contains memory addresses.
Address of interrupts.
The interrupt vector table contains addresses of interrupt service routines handler
functions.
12
Interrupt Service Routine Vector Table
Start address for the exception Address Priority
Type of
priority
Acronym Description
13
13 0x00000074 DMA1_Channel3_IRQHandler
12 0x00000070 DMA1_Channel2_IRQHandler
void DMA1_Channel1_IRQHandler () {
11 0x0000006C DMA1_Channel1_IRQHandler ...
}
10 0x00000068 EXTI4_IRQHandler
8
0x00000064
0x00000060
EXTI3_IRQHandler
EXTI2_IRQHandler
void EXTI1_Handler () {
}
...
7 0x0000005C EXTI1_IRQHandler
void EXTI0_Handler () {
6 0x00000058 EXTI0_IRQHandler ...
}
5 0x00000054 RCC_IRQHandler
4 0x00000050 FLASH_IRQHandler
3 0x0000004C RTC_WKUP_IRQHandler
2 0x00000048 TAMPER_STAMP_IRQHandler
1 0x00000044 PVD_IRQHandler
0 0x00000040 WWDG_IRQHandler
CMSIS Interrupt Number = 16 + n -1 0x0000003C SysTick_Handler
void SysTick_Handler () {
...
}
-2 0x00000038 PendSV_Handler
-3 0x00000034 Reserved
-4 0x00000030 DebugMon_Handler
void SVC_Handler () {
-5 0x0000002C SVC_Handler ...
}
-6 0x00000028 Reserved
-7 0x00000024 Reserved
System -8 0x00000020 Reserved
Exceptions
-9 0x0000001C Reserved
15
Link Register (LR)
Link
Register
17
Automatic Stacking & Unstacking
18
Link Register for calling functions
void foo(void) ;
void foo (void) {
● ● ●
int main(void){
● ● ●
● ● ● return;
foo(); }
● ● ●
}
Compiler
LR = PC + 4
● ● ● PC = foo
PC BL foo foo PROC
PC + 4 ● ● ● ● ● ●
● ● ●
BX LR
ENDP
When software calls a subroutine, LR holds the returning address.
Register values in an interrupt
service routine
LR = 0xFFFFFFF9
SP = MSP
ISR is always in
handler mode.
20
Enable an Interrupt
Interrupt
Request
Peripheral ARM Cortex-M
Interrupt
Request
Two steps:
NVIC Execution
1. Program the peripheral Core
control register to allow Nested Vectored
it to generate interrupts Interrupt Controller
2. Program NVIC to allow
it to accept interrupts
21
Enable an Interrupt
Interrupt
Request
Peripheral ARM Cortex-M
Interrupt
Request
Two steps:
NVIC Execution
1. Program the peripheral Core
control register to allow Nested Vectored
it to generate interrupts Interrupt Controller
2. Program NVIC to allow
it to accept interrupts
How?
22
Interrupt Number in CMSIS Library
Interrupt number defined in ARM software library (256 interrupts)
System Peripheral interrupt
Exceptions Interrupts number
-16 -1 0 239
Thumb state flag GE[3:0]: Greater or equal flags (only available on Cortex-M4 and M7)
Overflow flag
Carry/Borrow flag
Zero flag
25
Enable an Interrupt
Enable a system exception
Some are always enabled (cannot be disabled)
No centralized registers for enabling/disabling
Each are control by its corresponding components, such as SysTick module
26
Enabling Peripheral Interrupts
TIM7_IRQn = 44
NVIC->ISER[1] = 1 << 12; // Enable Timer 7 interrupt
27
Disabling Peripheral Interrupts
TIM7_IRQn = 44
NVIC->ICER[1] = 1 << 12; // Diable Timer 7 interrupt
28
Disable/Enable Peripheral Interrupts
For all peripheral interrupts: IRQn ≥ 0
Method 1:
NVIC_EnableIRQ (IRQn); // Enable interrupt
NVIC_DisableIRQ (IRQn); // Disable interrupt
Method 2:
Enable:
NVIC->ISER[ IRQn / 32] = 1 << (IRQn % 32);
Better solution:
NVIC->ISER[ IRQn >> 5] = 1 << (IRQn & 0x1F);
Disable:
NVIC->ICER[ IRQn >> 5] = 1 << (IRQn & 0x1F);
29
Multiple interrupts at the same time!
30
Interrupt Priority
Inverse Relationship:
Lower priority value means higher urgency.
Priority of Interrupt A = 5,
Priority of Interrupt B = 2,
B has a higher priority/urgency than A.
default setting
32
Interrupt Priority Levels
NVIC_SetPriority(7, 6);
core_cm4.h or core_cm3.h
0 1 1 0 0 0 0 0 typedef struct {
...
// Interrupt Priority Register
volatile uint8_t IP[240];
...
} NVIC_Type;
IP = 0x60 = 96
It is equivalent to:
NVIC->IP[7] = (6 << 4) & 0xff;
33
Preemption and Sub-priority Configuration
NVIC_SetPriorityGrouping(n)
Perform unlock, and update AIRCR register
# of bits in # of bits in sub-
n
preemption priority priority
0 0 4
1 1 3
2 (default) 2 2
3 3 1
4 4 0
Default
n=2
34