ARM STM32F407 Interrupts
ARM STM32F407 Interrupts
1
Cortex-M structure
Nested Vectored
Interrupt Controller
2
CMSIS = Cortex Microcontroller Software Interface Standard
Cortex CPU core registers
• Two processor modes:
• Thread mode for User tasks
• Handler mode for O/S tasks and exceptions
• Stack-based exception model
• Vector table contains addresses
3
Cortex-M4 processor operating modes
Exception
stack frame
6
Cortex-M CPU and peripheral exceptions
Priority1 IRQ#2 Notes
Reset -3 Power-up or warm reset
NMI -2 -14 Non-maskable interrupt from peripheral or software
HardFault -1 -13 Error during exception processing or no other handler
CPU Exceptions
• Peripherals use
positive IRQ #s
• CPU exceptions use
negative IRQ #s
• IRQ # used in CMSIS function
calls
• Cortex-M4 allows up to
240 IRQs
Tech. Ref.
Table 61
(Refer to
Startup
Code)
9
STM32F4 vector table from startup code (partial)
__Vectors DCD __initial_sp ; Top of Stack
DCD Reset_Handler ; Reset Handler
DCD NMI_Handler ; NMI Handler
……
DCD SVC_Handler ; SVCall Handler
DCD DebugMon_Handler ; Debug Monitor Handler
DCD 0 ; Reserved
DCD PendSV_Handler ; PendSV Handler
DCD SysTick_Handler ; SysTick Handler
; External Interrupts
DCD WWDG_IRQHandler ; Window WatchDog
DCD PVD_IRQHandler ; PVD via EXTI Line detection
DCD TAMP_STAMP_IRQHandler ; Tamper/TimeStamps via EXTI
DCD RTC_WKUP_IRQHandler ; RTC Wakeup via EXTI line
DCD FLASH_IRQHandler ; FLASH
DCD RCC_IRQHandler ; RCC
DCD EXTI0_IRQHandler ; EXTI Line0
DCD EXTI1_IRQHandler ; EXTI Line1
10 DCD EXTI2_IRQHandler ; EXTI Line2
Special CPU registers
ARM instructions to “access special registers”
MRS Rd,spec ;move from special register (other than R0-R15) to Rd
MSR spec,Rs ;move from register Rs to special register
Use CMSIS1 functions to clear/set PRIMASK
__enable_irq(); //enable interrupts (set PRIMASK=0)
__disable_irq(); //disable interrupts (set PRIMASK=1)
(double-underscore at beginning)
Special Cortex-M Assembly Language Instructions
CPSIE I ;Change Processor State/Enable Interrupts (sets PRIMASK = 0)
CPSID I ;Change Processor State/Disable Interrupts (sets PRIMASK = 1)
# of current
exception
(lower priority
cannot
1
11 Cortex Microcontroller Software Interface Standard – Functions for all interrupt)
ARM Cortex-M CPUs, defined in project header files: core_cmFunc.h, core_cm3.h
Prioritized interrupts
• NVIC does not unstack registers and then stack them again, if
going directly to another ISR.
• NVIC can halt stacking (and remember its place) if a new IRQ is
received.
13
Exception return
The exception mechanism detects when the processor has
completed an exception handler.
Exception return occurs when:
1. Processor is in Handler mode
2. EXC_RETURN loaded to PC
3. Processor executes one of these instructions:
LDM or POP that loads the PC
LDR with PC as the destination
BX using any register
EXC_RETURN value loaded into LR on exception entry (after
stacking original LR)
Lowest 5 bits of EXC_RETURN provide information on the return
stack and processor mode.
14
Interrupt signal: from device to CPU Peripheral Device
Registers:
In each peripheral device: Enable Flag
Each potential interrupt source has a separate arm (enable) bit xIE xF
Set for devices from which interrupts, are to be accepted
Clear to prevent the peripheral from interrupting the CPU
&
Each potential interrupt source has a separate flag bit
hardware sets the flag when an “event” occurs Peripheral
Interrupt request = (flag & enable) IRQn
ISR software must clear the flag to acknowledge the request
test flags in software if interrupts not desired
Nested Vectored Interrupt Controller (NVIC)
Receives all interrupt requests
NVIC
Each has an enable bit and a priority within the VIC
Highest priority enabled interrupt sent to the CPU
Within the CPU: PRIMASK
16
NVIC registers (one bit for each IRQ#)
NVIC_ISERx/NVIC_ICERx
Each IRQ has its own enable bit within NVIC EnableK
Interrupt Set/Clear Enable Register
1 = Set (enable) interrupt/Clear (disable) interrupt PendK
NVIC_ISPRx/NVIC_ICPRx
Interrupt Set/Clear Pending Register PriorityK
Read 1 from ISPR if interrupt in pending state
Write 1 to set interrupt to pending or clear from pending state
NVIC_IABRx – Interrupt Active Bit Register
Read 1 if interrupt in active state
x = 0..7 for each register type, with 32 bits per register, to support
up to 240 IRQs (82 in STM32F4xx)
Each bit controls one interrupt, identified by its IRQ# (0..239)
Register# x = IRQ# DIV 32
Bit n in the register = IRQ# MOD 32
17
NVIC registers (continued)
NVIC_IPRx (x=0..59) – Interrupt Priority Registers
Supports up to 240 interrupts: 0..239 (82 in STM32F4)
8-bit priority field for each interrupts (4-bit field in STM32F4)
4 priority values per register (STM32F4 – upper 4 bits of each byte)
0 = highest priority
Register# x = IRQ# DIV 4
Byte offset within the register = IRQ# MOD 4
Ex. IRQ85:
o 85/4 = 21 with remainder 1 (register 21, byte offset 1)
Write priority<<8 to NVIC_IPR2
o 85/32 = 2 with remainder 21: write 1<<21 to NVIC_SER2
20
CMSIS functions
Available when CMSIS Core is included in the project
NVC_EnableIRQ(IRQn_Type IRQn)
NVIC_DisableIRQ(IRQn_Type IRQn)
Set bit IRQn in NVIC_ISERx/NVIC/ICERx
NVIC_SetPendingIRQ(IRQn_Type IRQn)
NVIC_ClearPendingIRQ(IRQn_Type IRQn)
NVIC_GetPendingIRQ(IRQn_Type IRQn)
Set/read/read bit IRQn in NVIC_ISPRx/NVIC_ICPRx
NVIC_SetPriority(IRQn_Type IRQn,unit32_t priority)
NVIC_GetPriority(IRQn_Type IRQn)
Set/get IRQn priority in NVIC_IPRxI
21
NVIC CMSIS example: enable interrupts
IRQ
to
NVIC
External
interrupt
signal
(GPIO pin)
23
STM32F4xx external interrupt sources
(select in System Configuration Module – SYSCFG)
• 16 multiplexers select GPIO pins as external interrupts EXTI0..EXTI15
• Mux inputs selected via 4-bit fields of EXTICR[k] registers (k=0..3)
• EXTIx = 0 selects PAx, 1 selects PBx, 2 selects PCx, etc.
• EXTICR[0] selects EXTI3-EXTI0; EXTICR[1] selects EXTI7-EXTI4, etc
15 12 11 8 7 4 3 0
SYSCFG_EXTICR1 is
SYSCFG->EXTICR[0] EXTI3 EXTI2 EXTI1 EXTI0
25
STM32F4 EXTI Registers
23 bits per register - control 23 interrupts/events
EXTI_IMR – interrupt mask register
0 masks (disables) the interrupt
1 unmasks (enables) the interrupt
EXTI_RTSR/FTSR – rising/falling trigger selection register
1 to enable rising/falling edge to trigger the interrupt/event
0 to ignore the rising/falling edge
EXTI_PR – interrupt/event pending register
read 1 if interrupt/event occurred
clear bit by writing 1 (writing 0 has no effect)
write 1 to this bit in the interrupt handler to clear the pending state
of the interrupt
//Connect the portA pin0 to external interrupt line0 //Make sure the Button is really pressed
SYSCFG->EXTICR[0] &= SYSCFG_EXTICR1_EXTI0_PA; if (!(GPIOA->IDR & (1<<0)) )
{
//Interrupt Mask count++;
EXTI->IMR |= (1<<0); }
//Enable EXTI0
NVIC_EnableIRQ(EXTI0_IRQn);}
30
Supervisor Call Instruction (SVC)
Access system resources from O/S (“privileged operations”)
SVC_Handler is defined in the interrupt vector table
SVC interrupt handler written as a C function:
void SVC_Handler()
{ your code }
SVC interrupt handler as an assembly language function:
EXTERN SVC_Handler
SVC_Handler
your code
bx lr
31
Supervisor Call instruction (SVC)
To execute SVC_Handler as a software interrupt
Assembly language syntax: SVC #imm
C syntax: __svc (imm)
imm is an “SVC number” (0-255), which indicates a particular
“service” to be performed by the handler
imm is encoded into the instruction, but ignored by the CPU
Handler can retrieve imm by using stacked PC to read the SVC
instruction code (examples provided later)
Since this is an “interrupt”, R0-R3 are pushed onto the stack:
Arguments can be passed to the handler in R0-R3
SVC handler can retrieve the arguments from the stack
SVC handler can also return results by replacing R0-R3 values in the
stack, which will be restored to R0-R3 on return from interrupt.
32
Access SVC arguments in C
}
}
33
Access SVC arguments in assembly language
; Stack contains: r0, r1, r2, r3, r12, r14, return address, xPSR
; The saved r0 is the top entry in the stack
EXPORT SVC_Handler
SVC_Handler
TST LR,#0x04 ;Test bit 2 of EXC_RETURN
ITE EQ ;Which stack pointer was used?
MRSEQ R4,MSP ;Copy Main SP to R4
MRSNE R4,PSP ;Copy Process SP to R4
LDR R1,[R4] ;Retrieve saved R0 from top of stack
LDR R2,[R4,#4] ;Retrieve saved R1 from stack
….
STR R1,[R4] ;Replace saved R0 value in stack
BX LR ;Return and restore registers from stack
34
SVC in C programs
May associate “standard” function name with the
__svc (imm) function call
May pass up to four integer arguments
May return up to four results in a “value_in_regs” structure
Syntax:
__svc(int svc_num) return-type function-name(argument-list)
svc_num (8-bit constant) = immediate value in SVC instruction
“return-type function-name(argument-list)” = C function prototype
Call the function via: function-name(argument-list);
(examples on next slide)
int call_system_func(void) {
svc_zero("String to pass to SVC handler zero"); //Execute SVC #0
svc_one("String to pass to a different OS function"); //Execute SVC #1
}