Lecture 9
Lecture 9
Lecture 9
Yeongpil Cho
Hanynag University
Topics
• Interrupt Handling Basics
• Priority Management
2
Interrupt Handling Basics
3
Interrupts
• Motivation
▪ Inform processors of some
external events timely
• 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
ARMv7-M Interrupt Handling
• One Non-Maskable Interrupt (NMI) supported
• Up to 511 (496 external and 15 internal ones)
prioritizable interrupts/exceptions supported
▪ Interrupts can be masked
▪ Implementation option selects number of interrupts supported
• Nested Vectored Interrupt Controller (NVIC) is tightly co
upled with processor core
5
Interrupt Service Routine Vector Table
Address
• First entry contains initial Main SP 0x40 + 4*N External N
… …
• All other entries are addresses for 0x40 External 0
▪ exception/interrupt handlers 0x3C SysTick
▪ Must always have LSBit = 1 0x38 PendSV
(for Thumb) 0x34 Reserved
• Table can be relocated 0x30 Debug Monitor
▪ Use Vector Table Offset Register 0x2C SVC
0x1C to 0x28 Reserved (x4)
▪ Still require minimal table entries at
0x18 Usage Fault
0x0 for booting the core
0x14 Bus Fault
• Table can be generated using C 0x10 Mem Manage Fault
code 0x0C Hard Fault
0x08 NMI
0x04 Reset
0x00 Initial Main SP
6
Interrupt Service Routine Vector Table
of Cortex-M processors
7
Interrupt Handling Process
8
Stacking & Unstacking
• Current mode (either Thread mode or Handler mode)’s
stack is used for stacking/unstacking.
Old SP SP + 0x20 xxxxxxxx
SP + 0x1C xPSR
• Stacking: The processor
SP + 0x18 PC (r15)
automatically pushes these eight
SP + 0x14 LR (r14) registers into the stack before an
Full
Descending SP + 0x10 r12 interrupt handler starts
Stack SP + 0x0C r3
• Unstacking: The processor
SP + 0x08 r2 automatically pops these eight
SP + 0x04 r1 register out of the stack when an
interrupt hander exits.
New SP SP + 0x00 r0
9
Stacking & Unstacking
Interrupt
Exit
Interrupt Handler
Interrupt
Unstacking
Signal
10
Stacking & Unstacking
Interrupt Interrupt
Signal Exit
Stacking Unstacking
Time
Handler Mode Handler Mode Handler Mode
11
Exception Exits
• When the EXC_RETURN is loaded into the PC at the end of the
exception handler execution, the processor performs an exception
return sequence
• EXC_RETURN is generated and set to LR by processors when an
exception arises.
• There are three ways to trigger the interrupt return sequence:
12
Registers
• Handler mode and Thread mode
▪ Handler mode always use MSP
▪ Thread Mode uses either PSP or MSP
– Control[1] = 0, SP = MSP (default)
– Control[1] = 1, SP = PSP
An interrupt
from the
user-mode
with the MSP
LR = 0xFFFFFFF9
SP = MSP
ISRs always
run on the
Handler mode
with the MSP
15
Interrupt: Stacking & Unstacking
16
Interrupt: Stacking & Unstacking
17
Interrupt: Stacking & Unstacking
18
Interrupt: Stacking & Unstacking
19
Interrupt: Stacking & Unstacking
20
Interrupt: Stacking & Unstacking
21
Interrupt: Stacking & Unstacking
22
Interrupt: Stacking & Unstacking
23
Interrupt: Stacking & Unstacking
24
Interrupt: Stacking & Unstacking
25
Interrupt: Stacking & Unstacking
26
Interrupt: Stacking & Unstacking
27
Interrupt: Stacking & Unstacking
28
Interrupt: Stacking & Unstacking
29
Interrupt: Stacking & Unstacking
30
Interrupt: Stacking & Unstacking
31
Interrupt: Stacking & Unstacking
32
Interrupt: Stacking & Unstacking
33
Interrupt: Stacking & Unstacking
34
Interrupt Number in PSR
• Valid exception numbers on ARMv7-M
▪ 1 to 511
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Thumb state flag GE[3:0]: Greater or equal flags (only available on Cortex-M4 and M7)
Overflow flag
Carry/Borrow flag
Zero flag
35
Enable an Interrupt/Exception
• Enable a system exception
▪ Some are always enabled (cannot be disabled)
▪ No centralized registers for enabling/disabling
▪ Each is controlled by its corresponding components, such as
SysTick module
36
Enabling Peripheral Interrupts
TIM7_IRQn = 44
NVIC->ISER[1] = 1 << 12; // Enable Timer 7 interrupt
37
Disabling Peripheral Interrupts
TIM7_IRQn = 44
NVIC->ICER[1] = 1 << 12; // Diable Timer 7 interrupt
38
Priority Management
39
Interrupt/Exception 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.
40
Peripheral Interrupt Priority
• Interrupt priority is configured by Interrupt Priority
Register (IPR) 0~123
▪ 124 IPRs * 4 priority configuration per IPR = 496
(= Total # of peripheral interrupts supported on ARMv7-M)
• Each priority consists of two fields, including preempt
priority number and sub-priority number.
▪ Available bits in a priority byte are device-implementation defined.
▪ The preempt priority number defines the priority for preemption.
▪ The sub-priority number determines the order when multiple
interrupts are pending with the same preempt priority number.
41
Peripheral Interrupt Priority
• The lengths of the Preemption Priority-field and the Sub-
priority Number-field are configurable within valid priority bits.
▪ The position of LSB is adjusted.
• Why adjusting LSB?
▪ Easier porting!
▪ Programs implemented on many-bit priority-level device can run on
small-bit priority level device without inversion of priority.
▪ example: 2/5 bits priority fields → 2/3 bits priority fields
Adjusting LSB Adjusting MSB
0b10100010 0b01000110
PP: 2 → 2 PP: 2 → 2
SPN: 17 → 4 SPN: 17 → 1
0b10100010 0b01000110
priority reversal!
0b10001010 0b00010110
PP: 2 → 2 PP: 2 → 2
SPN: 5 → 1 SPN: 5 → 5 default setting
0b10001010 0b00010110
System Interrupt Priority
• 3 SHPRs (System Handler Priority Register) determine
priorities of system exceptions and interrupts,
43
Exception-masking registers
• PRIMASK: Set current execution priority to 0
▪ All interrupts are disabled except for NMI, hard fault, and reset
MOV R0, #1
MSR PRIMASK, R0
44