STG Captronic 05 - CM3-Exceptions
STG Captronic 05 - CM3-Exceptions
EXCEPTION BEHAVIOR
Exception Types
Reset : Invoked on power up or on a warm reset
Fault :
Hard fault : Occurs because of an error during exception processing, or because of a fault
escalation (an error in another fault)
Memory management fault : Occurs because of a memory protection related fault
Bus fault : Occurs because of a memory related fault for an instruction or data memory
transaction (might be from an error detected on a bus) in the memory system
Usage fault : Exception that occurs because of a fault related to instruction execution
System Call :
SVCall : A supervisor call is an exception triggered by the SVC instruction (formerly SWI)
PendSV : Interrupt-driven request for system-level service
Interrupt :
NMI : A non maskable interrupt can be signalled by a peripheral or triggered by software
SysTick : The system timer generates an exception when it reaches zero
Interrupt (IRQ) : Exception signalled by a peripheral, or generated by a software request.
All interrupts are asynchronous to instruction execution (used by peripherals to
communicate with the processor) depends on the NVIC implementation
Debug Monitor :
Entered when a debug event occurs while no probe is connected (less intrusive debug
than halting the core)
Exception assertion will cause the exception to be pended on the next clock edge
IRQi
InterruptSetEnable 1
InterruptSetPending 0 1 0
InterruptActive 0 1 0
Entry/ISR/Exit
IRQi Interrupt
requested exit
Level-Triggered IRQ
IRQi
InterruptSetEnable 0 1
InterruptSetPending 0 1 0
InterruptActive 0 1 0
Entry/ISR/Exit
CPU is taking a
pulse-triggered
exception
Interrupt is level-
Exception handler has been executed triggered and the
(and exited) interrupt handler didn’t
clear the peripheral
An exception has: interrupt flag
A priority level
An exception number, depending on the exception line number in the vector table
A vector in the vector table
EXCEPTION BEHAVIOR
Exception Configuration
Each exception input can have several registers to control it (especially for the
peripheral interrupts):
Enable bit (RW): Enable or mask the exception
Pending bit (RW): If the pending bit is set, then the exception is pending
An exception can be “pended” by setting the pending bit
Active bit (RO):
Set if the exception is executing or “active-stacked”
Active-stacked means the interrupt was executing, but was pre-empted by another
higher-priority exception
Priority field: 3 to 8 bits of priority for each configurable exception
Core exception configuration is done through the System Control block (SCB) registers
There are not a kind of Enable, Pending or Active bit for all core Exceptions
Some core exceptions don’t have a configurable priority
EXCEPTION BEHAVIOR
Vector Table
Address Vector
0x000 Initial Main SP The vector table contains handler
0x004 Reset addresses (addresses of the
0x008 NMI exception routine codes)
0x00c Hard Fault But not branch instructions)
0x010 Memory Manage
0x014 Bus Fault
0x018 Usage Fault
0x01C Reserved
16 Core exception vectors
0x020 Reserved
0x024 Reserved
Exception Handler can be written in C language
0x028 Reserved The return instruction is the same as a
0x02C SVCall function return instruction
0x030 Debug Monitor
The vector table has to be located
0x034 Reserved
at the address 0x0
0x038 PendSV
(probably at the beginning of the
0x03c SysTick
flash located at 0x0)
0x040 IRQ0
… … 1-240 Exception vectors
0x3BC IRQ239 (Peripheral Interrupts)
Unlike traditional ARM vector table, the Cortex-M vector table contains the address of
exception handlers (not instructons)
The first vector will be loaded into Main_SP when the processor leaves the reset state
For the other vectors, the address must be odd, because it is loaded into the PC when
the corresponding exception is taken (so the bit T in the xPSR register remains set)
Vector table size (Implementation defined, depends on the number of peripheral IRQ)
The VTOR register in the SCB can be used to relocate the Vector table
The base address must be multiple of 128
EXCEPTION BEHAVIOR
Vector Table
A pending exception can only be taken if it is enabled and it has sufficient priority to run
Late determination of interrupt wich is pending, enabled, and having the highest priority
Tail-chaining
Interrupts can be processed back-to-back, minimizing the inter-ISR gap
Automatic nesting
The priority mechanism will enable the nesting of interrupt having a higher priority than the current one
No software overhead
INTNMI
NVIC Cortex-M3
IRQ[N-1:0] core
N < 496
FCLK
IRQ[2]
Current Priority 2
First ISR instr
in execute
stage
Exception Status No status Vector fetch and stack push Entry No Stat
Exception number 18
of the current
execution context
12-clock entry latency
Note:
0x48 = 18 x 4 = IRQ2
STG-Captronic 5. 15 Cortex-M3 Exceptions
FCLK
Exception Exception
exit return
Exception Status No Status Exit Vector fetch and stack pop Return 0b000
Exception number 34 0
of the current
execution context
IRQ[1]
IRQ[2]
Exceptions is Exception is
cleared cleared
µcode µcode
Tail
Exception ISR IRQ1 ISR IRQ2 Exception
chaining
entry return
1 2 3 4 5 6 7 8 9 10 11
FCLK
IRQ[2]
Exception Status No status Exit Vector fetch and stack pop Return
Exception number 17 18
of the current
execution context
Exception Exception
exit entry
IRQ[2] was programmed with a priority level lower than the current IRQ
Otherwise a nesting would have occurred
IRQ[1]
IRQ[2]
Exception is Exception is
cleared cleared
1-12
12 clocks clocks 6 clocks 12 clocks
FCLK
Increasing
priority
IRQ[2]
IRQ[8]
IRQ[9]
HADDRI[31:0] 0x48 0x100 0x104 0x108 0x60 0x500 0x504 0x64 0x600 0x604 0x608
HRDATAI[31:0] 0x101 Word0 Word1 Word2 0x501 Word0 Word1 0x501 Word0 Word1 Word2
Exception Status
No Status Vector fetch and stack push Entry No Sta
Exception 18 24 25
number of the
current
execution
context
During the entry microcoded sequence, another exception can be set pending
When the Exception handler execution has begun, it is no longer possible to achieve a
late preemption
If a new Exception is requested, there are 2 cases
It has a higher priority than the current one
A nesting occurs
It has a lower or equal priority
A tail chaing will occur at the end of the current ISR
Synchronous exceptions are serviced after the instruction causing the exception and
before the following instruction (except if the fault is imprecise)
An imprecise bus fault could occur many cycles after the store instruction which
caused it for example
When a synchronous exception occurred, it has to be executed immediately
If it has not a priority higher than the current exception (if the core is already in
Handler Mode) or if it is masked by one of the core exception masks, a HardFault
exception occurs
0x0000_0000
SPi+1 > IC
R0
R1
R2
R3
DC
R12 Flash
LR
PC
xPSR SRAM
SPi > SYS
0xFFFF_FFFF
PRIORITY MANAGEMENT
The priority mechanism is based on a level that the user asigns to each interrupt
source
This level is determined by
A hardware setting : the number of bits to encode the priority level
From 3 to 8 bits
A software setting : the boundary between group priority and sub-group priority
(control of nesting)
AIRCR[PRIGROUP]
WHEN 2 IRQS ARE PROGRAMMED WITH THE SAME GROUP AND SUB-GROUP, THEIR
HARDWIRED NUMBER IS USED A THIRD PRIORITY INDICATION
IRQ3 has a higher priority than IRQ26
PRIORITY MANAGEMENT
Nesting occurs when a interrupt is requested with a higher group priority level
than the current IRQ (lower value)
PRIORITY MANAGEMENT
Group priority and Sub-group priority
Nesting occurs on the condition that IRQ_B has a higher group priority than IRQ_A
ISR B
The number of Priority Groups will determine the size that must be allocated in Main
stack
Tail
ISR A ISR B
chain
PRIORITY MANAGEMENT
Execution priority
The execution priority is defined to be the maximum priority of all active exceptions
This definition of execution priority prevents priority inversion
When an exception becomes active because its priority is sufficiently higher than the
execution priority
Its exception handler preempts the currently running instruction stream
Its priority becomes the execution priority
To set / clear PRIMASK and FAULTMASK registers, atomic instructions are supported
PRIORITY MANAGEMENT
Special Purpose Registers
InterruptEnable[31]
&
IRQ31
PRIMASK[I]
INT &
Priority
resolver
InterruptEnable[0]
&
IRQ0
NVIC CORE
PRIORITY MANAGEMENT
BASEPRI
A maskable exception is taken when its preemption priority is strictly lower than the
execution priority
ExecutionPriority
IRQA_PendingBit 0 1 0
IRQB_PendingBit 0 1 0
Thread mode Hdlr mode Thread mode Hdlr mode Thread mode
t
IRQA asserted IRQB asserted BASEPRI ← 7
Priority=6 Priority=1
(not taken) (taken)
EXCEPTION BEHAVIOR
Fault Management
If a fault occurs but its handler is not enabled or it does not have enough priority to run,
the HardFault handler will be invoked instead
This is called “Fault Escalation”
FAULT MANAGEMENT
Application
LDR R4,=0x20000000
; even pointer
BX R4
Usage Fault Handler
LDR R4,=0x20000000
; even pointer
BX R4
Hard Fault Handler
LDR R4,=0x20000000
; even pointer
BX R4
Lockup
EXCEPTION BEHAVIOR
Exception Types: OS Service calls and task switching
SVCall is a synchronous exception
As far as the calling thread is concerned, the SVCall handler will run after the SVC instruction and before
the instruction immediately following it
SVCall can be used from Thread mode to request system services from the OS
SVCall and PendSV are designed to work together to allow OS context switching without masking
interrupts
In many operating systems, context switches form the largest critical region where interrupt are masked
This directly affects the system maximum interrupt latency
Problem is that a context switch could be interrupted by another interrupt or exception requiring a context
switch
OS probably cannot nest context switches
t
SVC #0xN
Hard fault
t
SVC #0xN
t
InterruptControlState[PendsvSet] ← 1 BASEPRI ← 0b11.000000