Chapter 11 Interrupt
Chapter 11 Interrupt
and C
Chapter 11
Interrupt
Spring 2018
1
Interrupts
Motivations
Inform a program of some external events timely
Polling vs Interrupt
Implement multi-tasking with priority support
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.
0x0000_0034 - - - Reserved
4
…
…
ISR Vector
13 0x00000074 DMA1_Channel3_IRQHandler
12 0x00000070 DMA1_Channel2_IRQHandler
void DMA1_Channel1_IRQHandler () {
11 0x0000006C DMA1_Channel1_IRQHandler ...
…
9 0x00000064 EXTI3_IRQHandler void EXTI1_Handler () {
...
8 0x00000060 EXTI2_IRQHandler }
7 0x0000005C EXTI1_IRQHandler
void EXTI0_Handler () {
6 0x00000058 EXTI0_IRQHandler ...
}
5 0x00000054 RCC_IRQHandler
4 0x00000050 FLASH_IRQHandler
3 0x0000004C RTC_WKUP_IRQHandler
…
For interrupt number n: 2 0x00000048 TAMPER_STAMP_IRQHandler
0 0x00000040 WWDG_IRQHandler
void SysTick_Handler () {
-1 0x0000003C 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
…
-10 0x00000018 UsageFault_Handler
6
Interrupt
7
Stacking & Unstacking
Interrupt
Exit
Interrupt Handler
Interrupt
Unstacking
Signal
8
Registers
10
11
The CONTROL register can only be modified in the
privileged access level and can be read in both privileged
and unprivileged access levels. The definition of each bit
field in the CONTROL register is shown in Table
12
After reset, the CONTROL register is 0. This means the Thread
mode uses the Main Stack Pointer as Stack Pointer and Thread
mode has privileged accesses. Programs in privileged Thread
mode can switch the Stack Pointer selection or switch to
unprivileged access level by writing to CONTROL (Figure 4.10).
However, once nPRIV (CONTROL bit 0) is set, the program
running in Thread can no longer access the CONTROL register.
13
A program in unprivileged access level cannot switch itself back
to privileged access level. This is essential in order to provide a
basic security usage model. For example, an embedded system
might contain untrusted applications running in unprivileged
access level and the access permission of these applications
must be restricted to prevent security breaches or to prevent an
unreliable application from crashing the whole system.
If it is necessary to switch the processor back to using privileged
access level in Thread mode, then the exception mechanism is
needed. During exception handling, the exception handler can
clear the nPRIV bit (Figure 4.11). When returning to Thread
mode, the processor will be in privileged access level.
14
When an embedded OS is used, the CONTROL register
could be reprogrammed at each context switch to allow
some application tasks to run with privileged access level
and the others to run with unprivileged access level.
The settings of nPRIV and SPSEL are orthogonal. Four
different combinations of nPRIV and SPSEL are possible,
although only three of them are commonly used in
real world applications, as shown in Table 4.4.
15
Register values in a
interrupt service
routine
LR = 0xFFFFFFF9
SP = MSP
ISR always in
hander mode.
16
Which stack to use
when exiting an interrupt?
Link Register (LR) now has two usages:
LR = address of the instruction immediately after BL
LR indicates whether MSP or PSP is used to restore register from
when exiting an interrupt
LR value generated by processor
The processor set LR to 0xFFFFFFFF on reset.
If return to handler mode, the MSP stack is always used
17
Which stack to use
when exiting an interrupt?
Link Register (LR) now has two usages:
LR = address of the instruction immediately after BL
LR indicates whether MSP or PSP is used to restore register from when
exiting an interrupt
No FP extension:
Link Register (LR) Return Mode Return Stack
0xFFFFFFF1 Handler SP = MSP
0xFFFFFFF9 Thread SP = MSP
0xFFFFFFFD Thread SP = PSP
With FP extension:
Link Register (LR) Return Mode Return Stack
0xFFFFFFE1 Handler SP = MSP
0xFFFFFFE9 Thread SP = MSP
0xFFFFFFED Thread SP = PSP
18
Stacking & Unstacking
Control[1] = 0 ⟹ User program uses MSP.
Interrupt
Exit
Interrupt Handler
Interrupt
Unstacking
Signal
from MSP
20
Which is being used?
When exiting an interrupt:
If LR = 0xFFFFFFF9, then SP = MSP
If LR = 0xFFFFFFFD, then SP = PSP
9 = 1001
D = 1101
21
Interrupt: Stacking & Unstacking
__main PROC R0 0 xxxxxxxx 0x20000200
addr = 0x08000044 0x200001FC
… R1 1
MOV r3,#0 R2 2 0x200001F8
… 0x200001F4
R3 3
ENDP
addr = 0x0800001C R4 4 0x200001F0
R12 12 0x200001EC
SysTick_Handler PROC
EXPORT SysTick_Handler R13(SP) MSP 0x200001E8
ADD r3, #1 R14(LR) 0x08001000 0x200001E4
ADD r4, #1 R15(PC) 0x08000044 0x200001E0
BX lr
0x200001DC
ENDP
xPSR 0x21000000 0x200001D8
0x200001D4
MSP 0x20000200
0x200001D0
PSP 0x00000000
0x200001CF
Memory
22
Suppose SysTick interrupt occurs
Interrupt: Stacking
when PC&=Unstacking
0x08000044
__main PROC R0 0 xxxxxxxx 0x20000200
addr = 0x08000044 0x200001FC
… R1 1
MOV r3,#0 R2 2 0x200001F8
… 0x200001F4
R3 3
ENDP
addr = 0x0800001C R4 4 0x200001F0
R12 12 0x200001EC
SysTick_Handler PROC
EXPORT SysTick_Handler R13(SP) MSP 0x200001E8
ADD r3, #1 R14(LR) 0x08001000 0x200001E4
ADD r4, #1 R15(PC) 0x08000044 0x200001E0
BX lr
0x200001DC
ENDP
xPSR 0x21000000 0x200001D8
0x200001D4
MSP 0x20000200
0x200001D0
PSP 0x00000000
0x200001CF
Memory
23
Interrupt: Stacking & Unstacking
STACKING
__main PROC R0 0 xxxxxxxx 0x20000200
addr = 0x08000044 xPSR 0x21000000 0x200001FC
… R1 1
MOV r3,#0 R2 2 PC 0x08000044 0x200001F8
…
R3 3 LR 0x08001000 0x200001F4
ENDP
addr = 0x0800001C R4 4 R12 12 0x200001F0
Fix
40 the bug! Method 3 (not recommended)
Memory
Interrupt Number
Cortex-M supports up to 256 interrupts.
System Peripheral interrupt
Exceptions Interrupts number
-16 -1 0 239
CMSIS Interrupt Number
First 16 are system exceptions
CMSIS defines their interrupt numbers as negative
Defined by ARM core
The rest 240 are peripheral interrupts
Peripheral interrupt number starts with 0.
Defined by chip manufacturers.
41
Interrupt Number
in CMSIS vs in PSR
Interrupt number for CMSIS functions
NVIC_DisableIRQ (IRQn); // Disable interrupt
NVIC_EnableIRQ (IRQn); // Enable interrupt
NVIC_ClearingPending (IRQn); // clear pending status
NVIC_SetPriority (IRQn, priority); // set priority level
Thumb state flag GE[3:0]: Greater or equal flags (only available on Cortex-M4 and M7)
Overflow flag
Carry/Borrow flag
Zero flag
44
Enabling Peripheral Interrupts
TIM7_IRQn = 44
NVIC->ISER[1] = 1 << 12; // Enable Timer 7 interrupt
45
Disabling Peripheral Interrupts
TIM7_IRQn = 44
NVIC->ICER[1] = 1 << 12; // Diable Timer 7 interrupt
46
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);
47
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
49
Interrupt Priority Levels
NVIC_SetPriority(7, 6);
core_cm4.h or core_cm3.h
typedef struct {
0 1 1 0 0 0 0 0 ...
// Interrupt Priority Register
volatile uint8_t IP[240];
...
} NVIC_Type;
IP = 0x60 = 96
It is equivalent to:
NVIC->IP[7] = (6 << 4) & 0xff;
50
Preemption and Sub-priority
Configuration
NVIC_SetPriorityGrouping(n)
Perform unlock, and update AIRCR register
Default
n=2
51
Priority of Peripheral Interrupts
// Critical code
...
54
Exception-masking registers (PRIMASK,
FAULTMASK and BASEPRI)
PRIMASK: Used to disable all exceptions except Non-maskable interrupt
(NMI) and hard fault.
Write 1 to PRIMASK to disable all interrupts except NMI
MOV R0, #1
MSR PRIMASK, R0
BASEPRI: Disable interrupts only with priority lower than a certain level
Example, disable all exceptions with priority level higher than 0x60
MOV R0, #0x60
MSR BASEPRI, R0
55