Lec5 Interrupts
Lec5 Interrupts
Lecture 5:
Interrupts
7-1
Interrupts
An interrupt is the automatic transfer of
software execution in response to a
hardware event (trigger) that is
asynchronous with current software
execution.
external I/O device (like a keyboard or
printer) or
an internal event (like an op code fault, or a
periodic timer.)
Occurs when the hardware needs or can
service
7-2
Interrupt Processing
Hardware
needs
service
Main
Thread Saves
execution Restores
state execution
Interrupt state
Thread
ISR time
provides
service
7-3
ARM Cortex-M Interrupts
7-4
Interrupt Conditions
7-5
Interrupt Processing
7-7
Priority Mask Register PRIMASK
Disable interrupts (I=1)
CPSID I
CPSIE I
MRS R0,PRIMASK
CPSID I
MRS PRIMASK,R0
7-8
Program Status Register PSR
Accessed separately (APSR, IPSR, EPSR) or all
at once (PSR)
7-9
Interrupt Program Status Register (ISPR)
1. Each interrupt/exception
source has a unique
ISR_NUMBER in IPSR
2. The vector address =
ISR_NUMBER * 4
3. The processor will find the
ISR address using the
corresponding vector
address
7-10
Interrupt Context Switch
51
8-12
Nested Vectored Interrupt Controller (NVIC)
Example priority registers for some interrupts
Vector Number IRQ ISR name NVIC priority Priority
address startup_msp432.s bits
0x0000002C 11 -5 SVC_Handler SCB_SHPR2 31 – 29
0x00000038 14 -2 PendSV_Handler SCB_SHPR3 23 – 21
0x0000003C 15 -1 SysTick_Handler SCB_SHPR3 31 – 29
0x00000060 24 8 TA0_0_IRQHandler NVIC_IPR2 7–5
0x00000064 25 9 TA0_N_IRQHandler NVIC_IPR2 15 – 13
0x00000068 26 10 TA1_0_IRQHandler NVIC_IPR2 23 – 21
0x0000006C 27 11 TA1_N_IRQHandler NVIC_IPR2 31 – 29
0x00000070 28 12 TA2_0_IRQHandler NVIC_IPR3 7–5
0x00000074 29 13 TA2_N_IRQHandler NVIC_IPR3 15 – 13
0x00000078 30 14 TA3_0_IRQHandler NVIC_IPR3 23 – 21
0x0000007C 31 15 TA3_N_IRQHandler NVIC_IPR3 31 – 29
0x00000080 32 16 EUSCIA0_IRQHandler NVIC_IPR4 7–5
0x00000084 33 17 EUSCIA1_IRQHandler NVIC_IPR4 15 – 13
0x00000088 34 18 EUSCIA2_IRQHandler NVIC_IPR4 23 – 21
0x0000008C 35 19 EUSCIA3_IRQHandler NVIC_IPR4 31 – 29
0x00000090 36 20 EUSCIB0_IRQHandler NVIC_IPR5 7–5
0x00000094 37 21 EUSCIB1_IRQHandler NVIC_IPR5 15 – 13
0x00000098 38 22 EUSCIB2_IRQHandler NVIC_IPR5 23 – 21
0x0000009C 39 23 EUSCIB3_IRQHandler NVIC_IPR5 31 – 29
0x000000CC 51 35 PORT1_IRQHandler NVIC_IPR8 31 – 29
0x000000D0 52 36 PORT2_IRQHandler NVIC_IPR9 7–5
0x000000D4 53 37 PORT3_IRQHandler NVIC_IPR9 15 – 13
0x000000D8 54 38 PORT4_IRQHandler NVIC_IPR9 23 – 21
0x000000DC 55 39 PORT5_IRQHandler NVIC_IPR9 31 – 29
0x000000E0 56 40 PORT6_IRQHandler NVIC_IPR10 7–5
8-13
Nested Vectored Interrupt Controller (NVIC)
Example priority registers for some interrupts
8-14
NVIC Interrupt Set Enable Registers
8-15
GPIO Interrupt Registers
Each GPIO port has four interrupt control
and status registers used to process
interrupts on pins
8-16
Interrupt Initialization
Things you must do:
Initialize data structures (counters, pointers)
Arm (specify a flag may interrupt)
Configure NVIC
o Enable interrupt (e.g. NVIC_ISER1)
o Set priority (e.g. NVIC_IPR8)
Enable global Interrupts
o Assembly code CPSIE I
o C code EnableInterrupts();
8-17
Interrupt Service Routine (ISR)
8-18
Interrupt Events
Respond to infrequent but important events
Alarm conditions like low battery power
Error conditions
I/O synchronization
Trigger interrupt when signal on a port changes
Periodic interrupts
Generated by the timer at a regular rate
Systick timer can generate interrupt when it hits zero
Reload value + frequency determine interrupt rate
8-19
Synchronization
Main Main
program ISR program
ISR
Flag = 1
Other calculations Flag = 1
0
Flag
0
Flag Other calculations 1
1 Flag = 0
Do important stuff
Flag = 0
Do important stuff
Semaphore
One thread sets the flag
The other thread waits for, and clears
Use global variable to communicate
8-20
Example (Textbook section 5.4.1.2)
uint16_t ret = 0;
while(1) {}
WaitForInterrupt(); // wait for interrupt to happen...
}
void PORT4_IRQHandler(void)
{
char ret = P4IV; // clear P4IFG register
P3OUT ^= BIT2; // toggle LED0 on & off
} 8-22
Homework
8-23