SlideShare uma empresa Scribd logo
1 de 62
Advanced ARM®
Microcontroller Features
Agenda
Exceptions Overview
Priority & Pre-emption
Exception Behavior
Priority Management
2
AAME TechCon 2013
TC002v02
2
Priority Management
Writing Exception Handlers
Internal Exceptions & Faults
Memory Protection Unit
SysTick Timer
Exceptions Overview
The ARM v7-M (and v6-M) exception architecture is very different from
other ARM architectures
Designed for microcontroller applications
– Support for many interrupt sources
– Efficient handling of nested interrupts
– Flexible interrupt architecture (highly configurable)
– Built in RTOS support
3
AAME TechCon 2013
TC002v02
3
– Built in RTOS support
Main Features
– Nested Vector Interrupt Controller (NVIC)
– Micro-coded architecture handles the “dirty work”
– No software overhead for Interrupt Entry/Exit and Interrupt Nesting
– Only one mode and one stack for all exception handling
– Handler mode / Main stack
– Easy to program
– All exception handlers may be coded in “C”
Interrupt Overheads
Higher Priority
IRQ1
IRQ2
ARM7TDMI
Comparison with traditional ARM7 core
4
AAME TechCon 2013
TC002v02
4
TC
16 Cycles
PopPopPush ISR 1 ISR 2Push
26 Cycles 26 Cycles16 Cycles
ARM7TDMI
Interrupt Handling
ARM Cortex®-M
Interrupt Handling
10 Cycles6 Cycles
ISR 1 ISR 2
12 Cycles
Tail-Chaining
~65% Saving
Cycle Overhead
Push Pop
Interrupt Arrival During State Restore
Traditional interrupt handling
must complete stack cycle
IRQ1
IRQ2
Higher Priority
26 cycles16 cycles
Interrupt routine Pop Push Interrupt routine
5
AAME TechCon 2013
TC002v02
5
PopCortex-M3 or Cortex-M4 may
abandon stack operations
dynamically
Load Multiple uninterruptible, and hence
the core must complete the POP and
then full stack PUSH
POP may be abandoned early if another
interrupt arrives
If POP is interrupted it only takes 6
cycles to enter ISR2 (equivalent to Tail-
chaining)
ARM7 ARM Cortex-M3 or ARM Cortex-M4
6 cycles1-10 cycles
Interrupt routine TC Interrupt routine
Exception Types
Exceptions can be caused by various events
– Internal
– External
ARM Processor
Application CodeInternal External
6
AAME TechCon 2013
TC002v02
6
Thread
Mode
Application Code
Handler
Mode
Exception Code
SVC
PendSV
Faults
SysTick
Internal
Exceptions
Reset
Interrupts
Faults
External
Exceptions
Processor Mode Usage
Processor mode may change when exceptions occur
– Thread Mode is entered on Reset
– Handler Mode is entered on all other exceptions
ARM Processor
Application Code
7
AAME TechCon 2013
TC002v02
7
Thread
Mode
Application Code
Handler
Mode
Exception Code
SVC
PendSV
Faults
SysTick
Exception
Return
Reset
Interrupts
Faults
External Interrupts
External Interrupts handled by Nested Vectored Interrupt Controller (NVIC)
– Tightly coupled with processor core
One Non-Maskable Interrupt (NMI) supported
Number of external interrupts is implementation-defined
– ARMv7-M supports up to 496 interrupts
8
AAME TechCon 2013
TC002v02
8
– ARMv7-M supports up to 496 interrupts
……
Cortex-Mx
Processor Core
INTISR[0]
…
…
INTISR[N]
INTNMI
NVIC
Cortex-Mx Integration Layer
Micro-coded Interrupt Mechanism
Interrupt architecture designed for low latency
Interrupt prioritization mechanism is built into NVIC
Interrupt entry/exit is “micro-coded” – controlled by
hardware
– Automatically saves and restores processor context
9
AAME TechCon 2013
TC002v02
9
– Allows late determination of highest priority pending interrupt
– Allows another pending interrupt to be serviced without a full
restore/save of processor state (tail-chaining)
– Interrupt Service Routines (ISRs) can be written entirely in C
Some multi-cycle instructions are interruptible for
improved interrupt latency
Exception Model
Core begins execution with a “base” execution priority level
– At reset all interrupts are disabled
– The base execution priority level has a lower priority than the lowest programmable
priority level, so any enabled interrupt will pre-empt the core
When an enabled interrupt is asserted
– Interrupt is taken (serviced by interrupt handler)
– Core runs the handler at the execution priority level of the interrupt
10
AAME TechCon 2013
TC002v02
10
– When interrupt handler completes, original priority level is restored
When an enabled interrupt is asserted with lower or equal priority
– Interrupt is “pended” to run (when it has sufficient priority)
When a disabled interrupt is asserted
– Interrupt is “pended” to run (when it is enabled and has sufficient priority)
Interrupt nesting is always enabled
– To avoid nesting set all interrupts to the same priority
Agenda
Exceptions Overview
Priority & Pre-emption
Exception Behavior
Priority Management
11
AAME TechCon 2013
TC002v02
11
Priority Management
Writing Exception Handlers
Internal Exceptions & Faults
Memory Protection Unit
SysTick Timer
Exception Priorities Overview
Name Exception Number Exception Priority No.
Interrupts #0 - #495 (N interrupts) 16 to 16 + N 0-255 (programmable)
SysTick 15 0-255 (programmable)
PendSV 14 0-255 (programmable)
SVCall 11 0-255 (programmable)
Usage Fault 6 0-255 (programmable)
Bus Fault 5 0-255 (programmable)
Lowest
12
AAME TechCon 2013
TC002v02
12
The lower the priority number, the higher the priority level
Priority level is stored in a byte-wide register, which is set to 0x0 at reset
Exceptions with the same priority level follow a fixed priority order using the exception
number
– Lower exception number has higher priority level
Bus Fault 5 0-255 (programmable)
Memory Management Fault 4 0-255 (programmable)
Hard Fault 3 -1
Non Maskable Interrupt (NMI) 2 -2
Reset 1 -3 Highest
Pre-emption
Pre-emption occurs when a task is abandoned in order to handle an
exception
The currently running instruction stream is pre-empted
Pre-emption rules
– When multiple exceptions with the same priority are pending, the one with
the lowest exception number takes place
13
AAME TechCon 2013
TC002v02
13
the lowest exception number takes place
– Once an exception is active, only exceptions with a higher priority can pre-
empt it
“Late arrival” is a little different
– In this case, a higher-priority exception takes the place of a lower one after
the exception entry sequence has started
– More later...
Pre-emption Example
Higher Priority
IRQ2
IRQ1
14
AAME TechCon 2013
TC002v02
14
Core Execution
Base CPU
Foreground ISR2 ISR1 ISR2 Foreground
Time
IRQ3
ISR3
(ISR 2 resumes)
Agenda
Exceptions Overview
Priority & Pre-emption
Exception Behavior
Priority Management
15
AAME TechCon 2013
TC002v02
15
Priority Management
Writing Exception Handlers
Internal Exceptions & Faults
Memory Protection Unit
SysTick Timer
Vector Table for ARMv7-M
First entry contains initial Main SP
All other entries are addresses for
exception handlers
– Must always have LSBit = 1 (for Thumb)
Table has up to 496 external interrupts
– Implementation-defined
– Maximum table size is 2048 bytes
16 + N
…
16
15
14
13Reserved
PendSV
SysTick
External 0
…
External N0x40 + 4*N
…
0x40
0x3C
0x38
0x34
Address Exception #
16
AAME TechCon 2013
TC002v02
16
– Maximum table size is 2048 bytes
Table may be relocated
– Use Vector Table Offset Register
– Still require minimal table entries at 0x0 for
booting the core
Each exception has an exception number
– Used in Interrupt Control and State
Register to indicate the active or pending
exception type
Table can be generated using C code
– Example provided later
Reserved (x4)
Usage Fault
Mem Manage Fault
Hard Fault
NMI
Reset
Initial Main SP
0x1C to 0x28
0x18
0x14
0x10
0x0C
0x08
0x04
0x00
12
11SVC
Debug Monitor
Bus Fault
0x30
0x2C
7-10
6
5
4
3
2
1
N/A
Reset Behavior
Reset Handler
4
3
Main
5
17
AAME TechCon 2013
TC002v02
17
1. A reset occurs (Reset input was asserted)
2. Load MSP (Main Stack Pointer) register initial value from address 0x00
3. Load reset handler vector address from address 0x04
4. Reset handler executes in Thread Mode
5. Optional: Reset handler branches to the main program
0x04
0x001 Initial value of MSP
Reset Handler Vector
r13 (MSP)
2
Exception Behavior
Exception Handler
2
3
Main
1
4
18
AAME TechCon 2013
TC002v02
18
1. Exception occurs
– Current instruction stream stops
– Processor accesses vector table
2. Vector address for the exception loaded from the vector table
3. Exception handler executes in Handler Mode
4. Exception handler returns to main (assuming no nesting – more later)
Exception Vector
Exception States
Exceptions can be in the following states
– Inactive: Not Pending or Active
– Pending: Exception event has been generated but processing has not started yet
– Active: Exception processing has been started but is not complete yet
– An exception handler can interrupt the execution of another exception handler. In
this case both exceptions are in the active state.
– Active and Pending: The exception is being serviced by the processor and there is
19
AAME TechCon 2013
TC002v02
19
– Active and Pending: The exception is being serviced by the processor and there is
a pending exception from the same source
Inactive
Pending
Active
Interrupt Service Routine Entry (1)
When receiving an interrupt the processor will finish the current instruction
for most instructions
– To minimize interrupt latency, the processor can take an interrupt during the
execution of a multi-cycle instruction - see next slide
Processor state automatically saved to the current stack
– 8 registers are pushed: PC, R0-R3, R12, LR, xPSR
– Follows ARM Architecture Procedure Calling Standard (AAPCS)
20
AAME TechCon 2013
TC002v02
20
– Follows ARM Architecture Procedure Calling Standard (AAPCS)
During (or after) state saving the address of the ISR is read from the Vector
Table
Link Register is modified for interrupt return
First instruction of ISR executed
– For Cortex-M3 or Cortex-M4 the total latency is normally 12 cycles, however,
interrupt late-arrival and interrupt tail-chaining can improve IRQ latency
ISR executes from Handler mode with Main stack
Interrupt Service Routine Entry (2)
On taking an exception during a multi-cycle instruction the processor either:
– Continues execution of the instruction after returning from the exception
– Abandons the instruction and restarts the instruction on returning from the exception
For multi-cycle instructions on the Cortex-M3 and Cortex-M4 processors:
– Divide, multiply and load/store double instructions are abandoned and restarted
– Load/store multiple instructions are treated as “exception-continuable instructions”, if
possible
21
AAME TechCon 2013
TC002v02
21
possible
– The ICI bits in the EPSR are used to hold the continuation state
– However, in some situations LDM/STM instructions are abandoned and restarted
– For example, An LDM/STM instruction within an If-Then block
Architecturally software must not use an LDM/STM to access a volatile location
– The Cortex-M3/M4 processors must not use an LDM/STM to access a volatile location if
– it executes inside an IT block
– it loads the same register as the base register
– it loads the PC
Interrupt Stacking
Foreground stack (Main or Process) used to
store the pre-interrupt state
Stack is Full-Descending
– SP automatically decremented on push
– SP always points to non-empty value
– Same behavior as other ARM cores
:
<old TOS>
Old SP
xPSR
ReturnAddress
LR
R12
SP
22
AAME TechCon 2013
TC002v02
22
The stack must be 8-byte aligned
– AAPCS compliant
– STKALIGN bit in Configuration and Control
Register should be set to 1
– Extra word padded to stack if required
Main stack active after state is saved
ISR can be coded in “C”
– No prologue code required (no overhead)
:
:
:
R12
R3
R2
R1
R0
0x0
Returning From Interrupt
Can return from interrupt with the following instructions when the PC is
loaded with “magic” value of 0xFFFF_FFFX (same format as
EXC_RETURN)
– LDR PC, …..
– LDM/POP which includes loading the PC
– BX LR (most common)
If no interrupts are pending, foreground state is restored
23
AAME TechCon 2013
TC002v02
23
If no interrupts are pending, foreground state is restored
– Stack and state specified by EXC_RETURN is used
– Context restore on ARM Cortex-M3 and ARM Cortex-M4 requires 10 cycles
If other interrupts are pending, the highest priority may be serviced
– Serviced if interrupt priority is higher than the foreground’s base priority
– Process is called Tail-Chaining as foreground state is not yet restored
– Latency for servicing new interrupt is only 6 cycles on M3/M4 (state already saved)
If state restore is interrupted, it is abandoned
– New ISR executed without state saving (original state still intact and valid)
– Must still fetch new vector and refill pipeline (6-cycle latency on M3/M4)
Nesting Example
Higher Priority
Base CPU
IRQ2
IRQ1
24
AAME TechCon 2013
TC002v02
24
Core Execution
Base CPU
Time
Foreground ISR2 ISR1 ISR2 ForegroundPop Pop
IRQ1 Active
IRQ2 Active
Pop
Interrupt entry requires 12 cycles
Interrupt exit requires 10 cycles
Push Push
Push
Tail Chaining Example
Higher Priority
Base CPU
IRQ2
IRQ1
Time
25
AAME TechCon 2013
TC002v02
25
Core Execution
Base CPU
Foreground ISR1 ISR2
Time
TC Pop
IRQ1 Active IRQ2 Active
Tail Chaining requires 6 cycles
Push Foreground
TC
Late Arriving Example
Higher Priority
Base CPU
IRQ2
IRQ1
26
AAME TechCon 2013
TC002v02
26
Core Execution
Base CPU
Time
Foreground ForegroundPopISR1 ISR2
A pending higher priority exception is handled before an already
pending lower priority exception even after exception entry sequence
has started
The lower priority exception is handled after the higher priority
exception
Push TC
Agenda
Exceptions Overview
Priority & Pre-emption
Exception Behavior
Priority Management
27
AAME TechCon 2013
TC002v02
27
Priority Management
Writing Exception Handlers
Internal Exceptions & Faults
Memory Protection Unit
SysTick Timer
Disabling Interrupts (Priority
Boosting)
Priority Boosting raises the base
level of execution priority level to 0
This disables interrupts by
preventing all programmable
exceptions from activating
The Priority Mask Register is used
Programmable
Exceptions
0x80
0xC0
Lowest
Priority
Priority
Boosting
Activation
Prevented
28
AAME TechCon 2013
TC002v02
28
The Priority Mask Register is used
to disable interrupts ...
Reset
NMI
Fault
0x00
-1
-2
-3
0x40
Highest
Priority
“Base” Execution
Priority Boosting
Three methods to set the Execution Priority via software on v7-M
BASEPRI register sets execution priority level from 1 to N (N=lowest priority)
– Acts as a general interrupt mask
– Example
– BASEPRI = 16 only interrupts with priority levels (-2 to 15) can pre-empt
– If BASEPRI = 0 masking is disabled (any enabled interrupt pre-empts the core)
29
AAME TechCon 2013
TC002v02
29
– If BASEPRI = 0 masking is disabled (any enabled interrupt pre-empts the core)
PRIMASK bit sets the execution priority level to 0
– Only allows Hard Fault and NMI to pre-empt
FAULTMASK bit sets the execution priority level to -1
– Can only be set by an exception handler (auto clears on exception return)
– Only allows NMI to pre-empt
Priority Boosting Instructions
BASEPRI is accessed like a Program Status Register
MSR{cond} BASEPRI, Rm ; write BASEPRI
MRS{cond} Rd, BASEPRI ; read BASEPRI
PRIMASK and FAULTMASK map to the classic “I” and “F” flags
CPSID I ; set PRIMASK (disable interrupts)
CPSIE I ; clear PRIMASK (enable interrupts)
CPSID F ; set FAULTMASK (disable faults and interrupts)
30
AAME TechCon 2013
TC002v02
30
CPSID F ; set FAULTMASK (disable faults and interrupts)
CPSIE F ; clear FAULTMASK (enable faults and interrupts)
RVCT Intrinsics
– PRIMASK
__enable_irq(), __disable_irq()
– FAULTMASK
__enable_fiq(), __disable_fiq()
Must be in privileged mode to modify execution priority level
v7-M Priority Grouping
The 8-bit Priority Level is divided into 2 fields
– Group Priority and Sub-Priority [GROUP.SUB]
Group Priority is the pre-empting priority level
Sub-Priority is used only if the Group Priority is the same
Exception Number is final tie-breaker
– If two interrupts have identical priority levels, lowest vector number takes priority
31
AAME TechCon 2013
TC002v02
31
Example
– 4-bit Group Priority / 4-bit Sub-Priority
– SysTick is enabled with priority = 0x30
– Timer1 is enabled with priority = 0x30
– Timer2 is enabled with priority = 0x31
– If Timer1 & Timer2 assert simultaneously Timer1 serviced, Timer2 pended
– If Timer2 is being serviced and Timer1 asserts Timer1 pended
– If SysTick & Timer1 assert simultaneously SysTick serviced, Timer1 pended
bit4
SubGroup
bit7 bit3 bit0
Group Priority / Sub-Priority Selection
PRIGROUP
Binary Point
(group.sub)
Group Priority
(Pre-empting)
Sub-Priority
Bits Levels Bits Levels
000 7.1 gggggggs 7 128 1 2
001 6.2 ggggggss 6 64 2 4
010 5.3 gggggsss 5 32 3 8
011 4.4 ggggssss 4 16 4 16
32
AAME TechCon 2013
TC002v02
32
Selection made by PRIGROUP in Application Interrupt and Reset Control Reg
System-wide configuration applies to all interrupt priorities
If less than 8 priority bits implemented, fewer Sub-Priority bits are available
– Example: 6 priority bits and PRIGROUP = 001 6 Group Bits / 0 Sub-Priority Bits
011 4.4 ggggssss 4 16 4 16
100 3.5 gggsssss 3 8 5 32
101 2.6 ggssssss 2 4 6 64
110 1.7 gsssssss 1 2 7 128
111 0.8 ssssssss 0 0 8 256
Interrupt Control and Status Bits
ENABLE bit (R/W) – is interrupt enabled or disabled?
PENDING bit (R/W) – is interrupt waiting to be serviced?
– Interrupt is pending if priority is too low or if interrupt is disabled
– Can set bit to generate an interrupt (if enabled and sufficient priority)
– Can clear bit to clear a pending interrupt (interrupt will not be serviced)
33
AAME TechCon 2013
TC002v02
33
ACTIVE bit (R only) – is interrupt active or inactive?
– If interrupts are nested, more than one active bit will be set
PRIORITY bits (R/W) – set the interrupt priority
– Lower value has higher priority (priority 0 is highest)
– ARMv7-M: 3 - 8 bits (8 - 256 levels)
– May be divided into a group (pre-empt) priority and a sub-group priority
(user defined)
Agenda
Exceptions Overview
Priority & Pre-emption
Exception Behavior
Priority Management
34
AAME TechCon 2013
TC002v02
34
Priority Management
Writing Exception Handlers
Internal Exceptions & Faults
Memory Protection Unit
SysTick Timer
Vector Table in Assembly
PRESERVE8
THUMB
IMPORT ||Image$$ARM_LIB_STACK$$ZI$$Limit||
AREA RESET, DATA, READONLY
EXPORT __Vectors
__Vectors DCD ||Image$$ARM_LIB_STACK$$ZI$$Limit|| ; Top of Stack
DCD Reset_Handler ; Reset Handler
35
AAME TechCon 2013
TC002v02
35
DCD NMI_Handler ; NMI Handler
DCD HardFault_Handler ; Hard Fault Handler
DCD MemManage_Handler ; MemManage Fault Handler
DCD BusFault_Handler ; Bus Fault Handler
DCD UsageFault_Handler ; Usage Fault Handler
DCD 0, 0, 0, 0, ; Reserved x4
DCD SVC_Handler, ; SVCall Handler
DCD Debug_Monitor ; Debug Monitor Handler
DCD 0 ; Reserved
DCD PendSV_Handler ; PendSV Handler
DCD SysTick_Handler ; SysTick Handler
; External vectors start here
The vector table at address
0x0 is minimally required to
have 4 values: stack top,
reset routine location,
NMI ISR location,
HardFault ISR location
Vector Table in C
typedef void(* const ExecFuncPtr)(void) __irq;
#pragma arm section rodata="exceptions_area”
ExecFuncPtr exception_table[] = {
(ExecFuncPtr)&Image$$ARM_LIB_STACK$$ZI$$Limit, /* Initial SP */
(ExecFuncPtr)__main, /* Initial PC */
NMIException,
HardFaultException,
36
AAME TechCon 2013
TC002v02
36
HardFaultException,
MemManageException,
BusFaultException,
UsageFaultException,
0, 0, 0, 0, /* Reserved */
SVCHandler,
DebugMonitor,
0, /* Reserved */
PendSVC,
SysTickHandler
/* Configurable interrupts start here...*/
};
#pragma arm section
Once interrupts are
enabled, the vector
table (whether at 0
or in SRAM) must
then have pointers
to all enabled (by
mask) exceptions
The SVCall ISR
location must be
populated if the SVC
instruction will be
used
Writing Interrupt Handlers (1)
Clear the interrupt source if required
– Some may not need to be cleared (like SysTick)
Interrupt nesting always enabled by default
– No modification needed to interrupt handlers
– Just be careful of Main stack overflows
37
AAME TechCon 2013
TC002v02
37
STKALIGN bit should be set to 1 to ensure 8-byte stack alignment
on exception entry
– Bit 9 in Code Configuration Register (a read-only register on latest revisions
of ARM Cortex-M3)
Writing Interrupt Handlers (2)
C Code is recommended for all handlers
– ISR function must be type void and can not accept arguments
– The __irq keyword is optional (but recommended for clarity)
__irq void SysTickHandler (void) {
}
– No special assembly language entry and exit code required
38
AAME TechCon 2013
TC002v02
38
No special assembly language entry and exit code required
Writing handlers in assembly
– Must manually save and restore any non-scratch registers which are used
– R4, R5, R6, R7, R8, R9, R10, R11, LR
– Must maintain 8-byte stack alignment if making function calls
– Must return from interrupt using EXC_RETURN with proper instruction
– LDR PC, …..
– LDM/POP which includes loading the PC
– BX LR
Agenda
Exceptions Overview
Priority & Pre-emption
Exception Behavior
Priority Management
39
AAME TechCon 2013
TC002v02
39
Priority Management
Writing Exception Handlers
Internal Exceptions & Faults
Memory Protection Unit
SysTick Timer
Internal Exceptions
SysTick Timer
– Provides system heart-beat for RTOS (or custom program executive)
– Periodic interrupt drives system task scheduling
– Better than a standard Timer Interrupt
– Integrated into the NVIC
– Does not vary between vendor implementation and Cortex-M families
System Service Call (SVC)
40
AAME TechCon 2013
TC002v02
40
System Service Call (SVC)
– Similar to “SVC” instruction on other ARM cores
– Allows non-privileged software to make system calls
– RTOS services requests from non-privileged mode
– Provides protection to important system functionality
– Examples: Open Serial Port, Remap Memory, Enter Low Power Mode, etc.
Pended System Call (PendSV)
– Operates with SVC to ease RTOS development
– Intended to be an interrupt for RTOS use
SysTick Timer
24-bit count down timer
2 clock sources
– Internal core clock / external clock (STCLK)
2 modes
– Generate interrupt / do not interrupt
41
AAME TechCon 2013
TC002v02
41
– Generate interrupt / do not interrupt
Can be used as a general timer
Programmer’s Interface
– Control and Status Register – sets mode and has self-clearing overflow bit
– Reload Value Register – sets the SysTick period
– Current Value Register – shows current SysTick value
– Calibration Value Register – for configuring a 10 msec period
Pended System Call (PendSV)
Permanently enabled interrupt
– Avoids need for long periods of interrupts disabled
Not an instruction – but instigated via Interrupt Control State
Register
– May only be written in privileged mode
– Set by PENDSVSET bit
–
42
AAME TechCon 2013
TC002v02
42
– Cleared by PENDSVCLR bit
Intended usage
– As a private, low-level interrupt for the RTOS
– SVC cannot be nested, so RTOS uses PendSV to perform specific tasks
– SVC and PendSV are given same interrupt priority…
– PendSV will only run after SVC completes (and before foreground is
returned)
– PendSV can then handle low-priority RTOS “cleanup” or perform context
switch
Fault Exceptions on v7-M
Three classes of faults
– Bus – Memory access error (Prefetch or Data Access)
– Memory Manage – MPU permissions mismatch
– Usage – Undefined instruction, CP access, illegal state transition (i.e. T=0)
– Can optionally trap divide by zero and unaligned memory accesses
Bus, Memory Manage and Usage Faults
43
AAME TechCon 2013
TC002v02
43
Bus, Memory Manage and Usage Faults
– Manually enabled in the System Handler Control and State Register
– Disabled by default – only enable if required
– User-defined priority level
Hard Fault – “Fall Back” Fault
– Always enabled with priority level “-1”
– Executes through “Fault Escalation”
Fault Escalation
Fault Escalation to Hard Fault occurs when a fault occurs but…
– The fault is not enabled or…
– The handler doesn’t have enough priority to run or…
– The fault handler encounters the same fault
Examples
– An undefined instruction is decoded
– The ARM Cortex-M3 and ARM Cortex-M4 try to signal a Usage Fault, but the
44
AAME TechCon 2013
TC002v02
44
– The ARM Cortex-M3 and ARM Cortex-M4 try to signal a Usage Fault, but the
UsageFault is not enabled
– With the MPU enabled an interrupt handler makes an illegal memory access
– The ARM Cortex-M3 and ARM Cortex-M4 try to signal a Memory Manage Fault, but
the Fault priority is lower than the priority of the running interrupt
– An undefined instruction is encountered in a UsageFault handler
– An enabled fault handler causes the same kind of fault as is being serviced
Other Interrupt & Exception
Information
You should also be familiar with…
– Instructions/functions for changing PRIMASK, FAULTMASK, BASEPRI
– Format and usage of Interrupt Enable, Active, Pending and Priority registers
– Format and interpretation of EXC_RETURN values
– Fault handling & Fault escalation
– The Lockup state
–
45
AAME TechCon 2013
TC002v02
45
– Edge and Level Sensitive Interrupt Configuration
Agenda
Exceptions Overview
Priority & Pre-emption
Exception Behavior
Priority Management
46
AAME TechCon 2013
TC002v02
46
Priority Management
Writing Exception Handlers
Internal Exceptions & Faults
Memory Protection Unit
SysTick Timer
Motivation: Memory Protection
Privileged
modes
Memory
OS
code + data
47
AAME TechCon 2013
TC002v02
47
Application code
OS
User mode
ARMxyz
Application
code + data
Default Memory Map
System
External
Peripheral
FFFF_FFFF
E000_0000
512MB
1GB
E00F_F000
E004_2000
E004_1000
E004_0000
E00F_FFFF
External Private Peripheral Bus
RESERVED
UNUSED
TPIU
ETM
ROM Table
E003_FFFF
48
AAME TechCon 2013
TC002v02
48
Code
SRAM
Peripheral
External
SRAM
2000_0000
4000_0000
6000_0000
A000_0000
0000_0000
1 GB
512MB
512MB
512MB
ITM
Internal Private Peripheral Bus
E000_E000
E000_3000
E000_2000
E000_1000
E000_0000
E000_F000
DWT
FPB
NVIC
RESERVED
RESERVED
Optional Bit-Band Regions
Memory Protection Overview
MPU architecture same as ARM Cortex-R4 - ARM v7 PMSA
Supports 8 regions
– Each region applies to both instruction and data accesses (unified regions)
Access controlled by region attributes
– Individual Instruction access, Data access and Memory attributes
– Controls Cacheability, Data/Peripheral Type selection
Regions have variable base address and size
– Base address must be aligned to the region size boundary
49
AAME TechCon 2013
TC002v02
49
Regions may overlay other regions
MPU is optional
– Absent or disabled MPU uses the default memory map
Sub-regions also possible within each region
MPU can modify some attributes of the default memory map
– Cannot modify addresses of Code or System spaces
– Top 0.5GB of the memory map is always “Execute Never”, so cannot be changed by MPU
– Bit-Band regions (if implemented) cannot be moved
– PPB is always strongly-ordered
MPU primarily provides access protection
– Can also make Peripheral memory partitions executable
Region Overview
Each region consists of:
– Base address
– Region Attribute and Size
– Memory Type and Access Control
At least one region must be defined (or PRIVDEFENA set) before enabling the MPU
– No regions defined or enabled after reset
– Accesses that do not fall into any region cause an abort, unless privileged and PRIVDEFENA is set
50
AAME TechCon 2013
TC002v02
50
– Region 0 lowest priority
– Code enabling MPU must be included in an executable region
– Otherwise, core will continually abort
– If MPU enabled, only System space (SCB) and vector table loads are guaranteed to be available
– Other accesses depend upon region settings and PRIVDEFENA
Base address must always be aligned to the region size
MPU regions are set up through the MPU registers in the SCB
Sub-Regions
Each region can be split into 8 equal sized non-overlapping sub-regions
– Allows the granularity of protection and memory attributes to be increased without
significant increase in hardware complexity
– Reduces the number of regions which must be used to align protection boundaries to
non-naturally aligned addresses
Each region has 8 sub-region disable bits
– Sub-region disable bit=0, sub region described as part of the region
51
AAME TechCon 2013
TC002v02
51
– Sub-region disable bit=0, sub region described as part of the region
– Protection and memory type attributes of the region apply to that sub-region
– Sub-region disable bit=1, sub region described as not part of the region
– Address covered by that sub-region is not matched as part of the region
Sub-regions being disabled enables backward compatibility with ARMv6
All region sizes between 256bytes and 4Gbytes support 8 sub-regions
Region sizes below 256bytes do not support sub-regions
Further MPU Information
You should also be familiar with…
– Region overlapping and sub regions
– Memory types and type extensions
– Access permissions
– MPU Alias Registers
52
AAME TechCon 2013
TC002v02
52
Agenda
Exceptions Overview
Priority & Pre-emption
Exception Behavior
Priority Management
53
AAME TechCon 2013
TC002v02
53
Priority Management
Writing Exception Handlers
Internal Exceptions & Faults
Memory Protection Unit
SysTick Timer
The SysTick Timer
Inclusion of the timer is implementation-defined in ARMv6-M
– Mandatory in ARMv7-M
Provides internal System Timer (SysTick)
– And a related SysTick exception
54
AAME TechCon 2013
TC002v02
54
– And a related SysTick exception
SysTick can be used in different ways
– Simple counter
– RTOS tick timer
– Dynamic clock management
The System Timer - SysTick
SysTick is a 24-bit counter
– Decrementing
– Clear-on-write
– Reload-on-zero
55
AAME TechCon 2013
TC002v02
55
Flexible control mechanism
– Using 4 registers
SysTick Registers
SysTick Control and Status
– Configure clock
– Enable the counter
– Enable the SysTick interrupt
– Determine counter status
56
AAME TechCon 2013
TC002v02
56
SysTick Reload value
– Counter wrap value
SysTick Current value
SysTick Calibration value
Basic SysTick Operation
1. Reload counter using Reload register
2. Enable/disable counter using Control & Status
– Current value is decremented when enabled until 0 is reached
– Can generate a counter event – e.g. SysTick Interrupt
3. Counter is auto-reloaded when value is 0 on wrap-around
57
AAME TechCon 2013
TC002v02
57
Reload
0x100000
Current
0x100000
0x000000Wrap
around
Counter
EventControl&Status
SysTick Clocks
Control & Status specifies which clock should be used by the counter
Core clock
– When no external reference clock is provided (NOREF)
External reference clock
– Can be provided by the implementation
58
AAME TechCon 2013
TC002v02
58
ARM Processor
Current
clock
Control&Status
Core
Clock
External
Reference
Clock
SysTick Calibration
Calibration can be used to allow code to measure real-time intervals in a
standard way
– Implementation-independent
2 clock sources can be implemented
– External reference clock
59
AAME TechCon 2013
TC002v02
59
– Internal clock
SysTick Calibration Value register provides information for calibration
– Reload value to count 10ms (TENMS)
– Possibility to mark the 10ms as inexact (SKEW)
– Due to rounding
Calibration - TENMS
Provides a 10ms reference timing for the SysTick counter
– Can be loaded directly into the Reload Value register
– Software can scale the counter to desired clock rates
– Reload value = (factor * (TENMS + 1)) - 1
Ensures a 10ms cycle of the SysTick counter
– Can be used for real-time applications
60
AAME TechCon 2013
TC002v02
60
If TENMS is 0
– No 10ms reference value is provided
– Reference clock is not constant
10ms (100Hz)
1kHz
2kHz
TENMS = 9, or 0x9
TENMS = 19, or 0x13
Calibration - SKEW
Provides information if the TENMS value is exact to the reference
clock
SKEW = 0
– No skew, 10ms can be exactly represented
SKEW = 1
61
AAME TechCon 2013
TC002v02
61
SKEW = 1
– 10ms cannot be exactly represented
10ms
1kHz
1.05kHz
SKEW=0
SKEW=1 9.5 TENMS
9 TENMS
Advanced ARM®
Microcontroller Features

Mais conteúdo relacionado

Mais procurados

Arm cortex-m3 by-joe_bungo_arm
Arm cortex-m3 by-joe_bungo_armArm cortex-m3 by-joe_bungo_arm
Arm cortex-m3 by-joe_bungo_arm
Prashant Ahire
 
HKG18-116 - RAS Solutions for Arm64 Servers
HKG18-116 - RAS Solutions for Arm64 ServersHKG18-116 - RAS Solutions for Arm64 Servers
HKG18-116 - RAS Solutions for Arm64 Servers
Linaro
 

Mais procurados (20)

ARM AAE - Architecture
ARM AAE - ArchitectureARM AAE - Architecture
ARM AAE - Architecture
 
ARM AAE - Intrustion Sets
ARM AAE - Intrustion SetsARM AAE - Intrustion Sets
ARM AAE - Intrustion Sets
 
Q4.11: ARM Architecture
Q4.11: ARM ArchitectureQ4.11: ARM Architecture
Q4.11: ARM Architecture
 
02 : ARM Cortex M4 Specs || IEEE SSCS AlexSC
02 : ARM Cortex M4 Specs || IEEE SSCS AlexSC 02 : ARM Cortex M4 Specs || IEEE SSCS AlexSC
02 : ARM Cortex M4 Specs || IEEE SSCS AlexSC
 
BUD17-209: Reliability, Availability, and Serviceability (RAS) on ARM64
BUD17-209: Reliability, Availability, and Serviceability (RAS) on ARM64 BUD17-209: Reliability, Availability, and Serviceability (RAS) on ARM64
BUD17-209: Reliability, Availability, and Serviceability (RAS) on ARM64
 
Introduction to arm architecture
Introduction to arm architectureIntroduction to arm architecture
Introduction to arm architecture
 
1: Interfacing using ARM Cortex M4 || IEEE SSCS AlexSC
1: Interfacing using ARM Cortex M4 || IEEE SSCS AlexSC 1: Interfacing using ARM Cortex M4 || IEEE SSCS AlexSC
1: Interfacing using ARM Cortex M4 || IEEE SSCS AlexSC
 
Reliability, Availability, and Serviceability (RAS) on ARM64 status - SFO17-203
Reliability, Availability, and Serviceability (RAS) on ARM64 status - SFO17-203Reliability, Availability, and Serviceability (RAS) on ARM64 status - SFO17-203
Reliability, Availability, and Serviceability (RAS) on ARM64 status - SFO17-203
 
Introduction to Processor Design and ARM Processor
Introduction to Processor Design and ARM ProcessorIntroduction to Processor Design and ARM Processor
Introduction to Processor Design and ARM Processor
 
Reliability, Availability, and Serviceability (RAS) on ARM64 status - SAN19-118
Reliability, Availability, and Serviceability (RAS) on ARM64 status - SAN19-118Reliability, Availability, and Serviceability (RAS) on ARM64 status - SAN19-118
Reliability, Availability, and Serviceability (RAS) on ARM64 status - SAN19-118
 
ARM Processor architecture
ARM Processor  architectureARM Processor  architecture
ARM Processor architecture
 
GPIO In Arm cortex-m4 tiva-c
GPIO In Arm cortex-m4 tiva-cGPIO In Arm cortex-m4 tiva-c
GPIO In Arm cortex-m4 tiva-c
 
Arm cortex-m3 by-joe_bungo_arm
Arm cortex-m3 by-joe_bungo_armArm cortex-m3 by-joe_bungo_arm
Arm cortex-m3 by-joe_bungo_arm
 
Arm arc-2016
Arm arc-2016Arm arc-2016
Arm arc-2016
 
ARM CORTEX M3 PPT
ARM CORTEX M3 PPTARM CORTEX M3 PPT
ARM CORTEX M3 PPT
 
ARM Cortex-M3 Training
ARM Cortex-M3 TrainingARM Cortex-M3 Training
ARM Cortex-M3 Training
 
HKG18-116 - RAS Solutions for Arm64 Servers
HKG18-116 - RAS Solutions for Arm64 ServersHKG18-116 - RAS Solutions for Arm64 Servers
HKG18-116 - RAS Solutions for Arm64 Servers
 
ARM 7 Detailed instruction set
ARM 7 Detailed instruction setARM 7 Detailed instruction set
ARM 7 Detailed instruction set
 
Introduction to i.MX27 Multimedia Applications Processors
Introduction to i.MX27 Multimedia Applications ProcessorsIntroduction to i.MX27 Multimedia Applications Processors
Introduction to i.MX27 Multimedia Applications Processors
 
Arm cortex-m4 programmer model
Arm cortex-m4 programmer modelArm cortex-m4 programmer model
Arm cortex-m4 programmer model
 

Destaque (9)

Android Open Accessory Protocol - Turn Your Linux machine as ADK
Android Open Accessory Protocol - Turn Your Linux machine as ADKAndroid Open Accessory Protocol - Turn Your Linux machine as ADK
Android Open Accessory Protocol - Turn Your Linux machine as ADK
 
aoa-adk-osidays-rajeshsola
aoa-adk-osidays-rajeshsolaaoa-adk-osidays-rajeshsola
aoa-adk-osidays-rajeshsola
 
Getting started with IOT Development using Fedora on ARM
Getting started with IOT Development using Fedora on ARMGetting started with IOT Development using Fedora on ARM
Getting started with IOT Development using Fedora on ARM
 
Hands on with embedded linux using zero hardware
Hands on with embedded linux using zero hardwareHands on with embedded linux using zero hardware
Hands on with embedded linux using zero hardware
 
AAME ARM Techcon2013 Intro
AAME ARM Techcon2013 IntroAAME ARM Techcon2013 Intro
AAME ARM Techcon2013 Intro
 
ARM AAE - Introduction
ARM AAE - IntroductionARM AAE - Introduction
ARM AAE - Introduction
 
Kickstarting IOT using NodeRED
Kickstarting IOT using NodeREDKickstarting IOT using NodeRED
Kickstarting IOT using NodeRED
 
Arm Lecture
Arm LectureArm Lecture
Arm Lecture
 
LPC 2148 ARM MICROCONTROLLER
LPC 2148 ARM MICROCONTROLLERLPC 2148 ARM MICROCONTROLLER
LPC 2148 ARM MICROCONTROLLER
 

Semelhante a AAME ARM Techcon2013 002v02 Advanced Features

Semelhante a AAME ARM Techcon2013 002v02 Advanced Features (20)

Arm architecture
Arm architectureArm architecture
Arm architecture
 
ARM - Advance RISC Machine
ARM - Advance RISC MachineARM - Advance RISC Machine
ARM - Advance RISC Machine
 
arm
armarm
arm
 
embedded system introduction to microcontrollers
embedded system introduction to microcontrollersembedded system introduction to microcontrollers
embedded system introduction to microcontrollers
 
Interrupts.ppt
Interrupts.pptInterrupts.ppt
Interrupts.ppt
 
Introduction to ARM Architecture
Introduction to ARM ArchitectureIntroduction to ARM Architecture
Introduction to ARM Architecture
 
Arm architecture overview
Arm architecture overviewArm architecture overview
Arm architecture overview
 
Introduction to 8085svv
Introduction to 8085svvIntroduction to 8085svv
Introduction to 8085svv
 
Intro to micro controller (Atmega16)
Intro to micro controller (Atmega16)Intro to micro controller (Atmega16)
Intro to micro controller (Atmega16)
 
Lec15 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- EPIC VLIW
Lec15 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- EPIC VLIWLec15 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- EPIC VLIW
Lec15 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- EPIC VLIW
 
Embedded system
Embedded  systemEmbedded  system
Embedded system
 
Interrupts
InterruptsInterrupts
Interrupts
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbai
 
unit 1ARM INTRODUCTION.pptx
unit 1ARM INTRODUCTION.pptxunit 1ARM INTRODUCTION.pptx
unit 1ARM INTRODUCTION.pptx
 
Assembly programming
Assembly programmingAssembly programming
Assembly programming
 
presentation on SCB,DEBUG,RESET of Arm Cortex processor
presentation on SCB,DEBUG,RESET of Arm Cortex processorpresentation on SCB,DEBUG,RESET of Arm Cortex processor
presentation on SCB,DEBUG,RESET of Arm Cortex processor
 
Nvidia tegra K1 Presentation
Nvidia tegra K1 PresentationNvidia tegra K1 Presentation
Nvidia tegra K1 Presentation
 
PIC Introduction and explained in detailed
PIC Introduction and explained in detailedPIC Introduction and explained in detailed
PIC Introduction and explained in detailed
 
Linux interrupts
Linux interruptsLinux interrupts
Linux interrupts
 
Atmega8u2 mur
Atmega8u2 murAtmega8u2 mur
Atmega8u2 mur
 

Último

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Último (20)

VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 

AAME ARM Techcon2013 002v02 Advanced Features

  • 2. Agenda Exceptions Overview Priority & Pre-emption Exception Behavior Priority Management 2 AAME TechCon 2013 TC002v02 2 Priority Management Writing Exception Handlers Internal Exceptions & Faults Memory Protection Unit SysTick Timer
  • 3. Exceptions Overview The ARM v7-M (and v6-M) exception architecture is very different from other ARM architectures Designed for microcontroller applications – Support for many interrupt sources – Efficient handling of nested interrupts – Flexible interrupt architecture (highly configurable) – Built in RTOS support 3 AAME TechCon 2013 TC002v02 3 – Built in RTOS support Main Features – Nested Vector Interrupt Controller (NVIC) – Micro-coded architecture handles the “dirty work” – No software overhead for Interrupt Entry/Exit and Interrupt Nesting – Only one mode and one stack for all exception handling – Handler mode / Main stack – Easy to program – All exception handlers may be coded in “C”
  • 4. Interrupt Overheads Higher Priority IRQ1 IRQ2 ARM7TDMI Comparison with traditional ARM7 core 4 AAME TechCon 2013 TC002v02 4 TC 16 Cycles PopPopPush ISR 1 ISR 2Push 26 Cycles 26 Cycles16 Cycles ARM7TDMI Interrupt Handling ARM Cortex®-M Interrupt Handling 10 Cycles6 Cycles ISR 1 ISR 2 12 Cycles Tail-Chaining ~65% Saving Cycle Overhead Push Pop
  • 5. Interrupt Arrival During State Restore Traditional interrupt handling must complete stack cycle IRQ1 IRQ2 Higher Priority 26 cycles16 cycles Interrupt routine Pop Push Interrupt routine 5 AAME TechCon 2013 TC002v02 5 PopCortex-M3 or Cortex-M4 may abandon stack operations dynamically Load Multiple uninterruptible, and hence the core must complete the POP and then full stack PUSH POP may be abandoned early if another interrupt arrives If POP is interrupted it only takes 6 cycles to enter ISR2 (equivalent to Tail- chaining) ARM7 ARM Cortex-M3 or ARM Cortex-M4 6 cycles1-10 cycles Interrupt routine TC Interrupt routine
  • 6. Exception Types Exceptions can be caused by various events – Internal – External ARM Processor Application CodeInternal External 6 AAME TechCon 2013 TC002v02 6 Thread Mode Application Code Handler Mode Exception Code SVC PendSV Faults SysTick Internal Exceptions Reset Interrupts Faults External Exceptions
  • 7. Processor Mode Usage Processor mode may change when exceptions occur – Thread Mode is entered on Reset – Handler Mode is entered on all other exceptions ARM Processor Application Code 7 AAME TechCon 2013 TC002v02 7 Thread Mode Application Code Handler Mode Exception Code SVC PendSV Faults SysTick Exception Return Reset Interrupts Faults
  • 8. External Interrupts External Interrupts handled by Nested Vectored Interrupt Controller (NVIC) – Tightly coupled with processor core One Non-Maskable Interrupt (NMI) supported Number of external interrupts is implementation-defined – ARMv7-M supports up to 496 interrupts 8 AAME TechCon 2013 TC002v02 8 – ARMv7-M supports up to 496 interrupts …… Cortex-Mx Processor Core INTISR[0] … … INTISR[N] INTNMI NVIC Cortex-Mx Integration Layer
  • 9. Micro-coded Interrupt Mechanism Interrupt architecture designed for low latency Interrupt prioritization mechanism is built into NVIC Interrupt entry/exit is “micro-coded” – controlled by hardware – Automatically saves and restores processor context 9 AAME TechCon 2013 TC002v02 9 – Allows late determination of highest priority pending interrupt – Allows another pending interrupt to be serviced without a full restore/save of processor state (tail-chaining) – Interrupt Service Routines (ISRs) can be written entirely in C Some multi-cycle instructions are interruptible for improved interrupt latency
  • 10. Exception Model Core begins execution with a “base” execution priority level – At reset all interrupts are disabled – The base execution priority level has a lower priority than the lowest programmable priority level, so any enabled interrupt will pre-empt the core When an enabled interrupt is asserted – Interrupt is taken (serviced by interrupt handler) – Core runs the handler at the execution priority level of the interrupt 10 AAME TechCon 2013 TC002v02 10 – When interrupt handler completes, original priority level is restored When an enabled interrupt is asserted with lower or equal priority – Interrupt is “pended” to run (when it has sufficient priority) When a disabled interrupt is asserted – Interrupt is “pended” to run (when it is enabled and has sufficient priority) Interrupt nesting is always enabled – To avoid nesting set all interrupts to the same priority
  • 11. Agenda Exceptions Overview Priority & Pre-emption Exception Behavior Priority Management 11 AAME TechCon 2013 TC002v02 11 Priority Management Writing Exception Handlers Internal Exceptions & Faults Memory Protection Unit SysTick Timer
  • 12. Exception Priorities Overview Name Exception Number Exception Priority No. Interrupts #0 - #495 (N interrupts) 16 to 16 + N 0-255 (programmable) SysTick 15 0-255 (programmable) PendSV 14 0-255 (programmable) SVCall 11 0-255 (programmable) Usage Fault 6 0-255 (programmable) Bus Fault 5 0-255 (programmable) Lowest 12 AAME TechCon 2013 TC002v02 12 The lower the priority number, the higher the priority level Priority level is stored in a byte-wide register, which is set to 0x0 at reset Exceptions with the same priority level follow a fixed priority order using the exception number – Lower exception number has higher priority level Bus Fault 5 0-255 (programmable) Memory Management Fault 4 0-255 (programmable) Hard Fault 3 -1 Non Maskable Interrupt (NMI) 2 -2 Reset 1 -3 Highest
  • 13. Pre-emption Pre-emption occurs when a task is abandoned in order to handle an exception The currently running instruction stream is pre-empted Pre-emption rules – When multiple exceptions with the same priority are pending, the one with the lowest exception number takes place 13 AAME TechCon 2013 TC002v02 13 the lowest exception number takes place – Once an exception is active, only exceptions with a higher priority can pre- empt it “Late arrival” is a little different – In this case, a higher-priority exception takes the place of a lower one after the exception entry sequence has started – More later...
  • 14. Pre-emption Example Higher Priority IRQ2 IRQ1 14 AAME TechCon 2013 TC002v02 14 Core Execution Base CPU Foreground ISR2 ISR1 ISR2 Foreground Time IRQ3 ISR3 (ISR 2 resumes)
  • 15. Agenda Exceptions Overview Priority & Pre-emption Exception Behavior Priority Management 15 AAME TechCon 2013 TC002v02 15 Priority Management Writing Exception Handlers Internal Exceptions & Faults Memory Protection Unit SysTick Timer
  • 16. Vector Table for ARMv7-M First entry contains initial Main SP All other entries are addresses for exception handlers – Must always have LSBit = 1 (for Thumb) Table has up to 496 external interrupts – Implementation-defined – Maximum table size is 2048 bytes 16 + N … 16 15 14 13Reserved PendSV SysTick External 0 … External N0x40 + 4*N … 0x40 0x3C 0x38 0x34 Address Exception # 16 AAME TechCon 2013 TC002v02 16 – Maximum table size is 2048 bytes Table may be relocated – Use Vector Table Offset Register – Still require minimal table entries at 0x0 for booting the core Each exception has an exception number – Used in Interrupt Control and State Register to indicate the active or pending exception type Table can be generated using C code – Example provided later Reserved (x4) Usage Fault Mem Manage Fault Hard Fault NMI Reset Initial Main SP 0x1C to 0x28 0x18 0x14 0x10 0x0C 0x08 0x04 0x00 12 11SVC Debug Monitor Bus Fault 0x30 0x2C 7-10 6 5 4 3 2 1 N/A
  • 17. Reset Behavior Reset Handler 4 3 Main 5 17 AAME TechCon 2013 TC002v02 17 1. A reset occurs (Reset input was asserted) 2. Load MSP (Main Stack Pointer) register initial value from address 0x00 3. Load reset handler vector address from address 0x04 4. Reset handler executes in Thread Mode 5. Optional: Reset handler branches to the main program 0x04 0x001 Initial value of MSP Reset Handler Vector r13 (MSP) 2
  • 18. Exception Behavior Exception Handler 2 3 Main 1 4 18 AAME TechCon 2013 TC002v02 18 1. Exception occurs – Current instruction stream stops – Processor accesses vector table 2. Vector address for the exception loaded from the vector table 3. Exception handler executes in Handler Mode 4. Exception handler returns to main (assuming no nesting – more later) Exception Vector
  • 19. Exception States Exceptions can be in the following states – Inactive: Not Pending or Active – Pending: Exception event has been generated but processing has not started yet – Active: Exception processing has been started but is not complete yet – An exception handler can interrupt the execution of another exception handler. In this case both exceptions are in the active state. – Active and Pending: The exception is being serviced by the processor and there is 19 AAME TechCon 2013 TC002v02 19 – Active and Pending: The exception is being serviced by the processor and there is a pending exception from the same source Inactive Pending Active
  • 20. Interrupt Service Routine Entry (1) When receiving an interrupt the processor will finish the current instruction for most instructions – To minimize interrupt latency, the processor can take an interrupt during the execution of a multi-cycle instruction - see next slide Processor state automatically saved to the current stack – 8 registers are pushed: PC, R0-R3, R12, LR, xPSR – Follows ARM Architecture Procedure Calling Standard (AAPCS) 20 AAME TechCon 2013 TC002v02 20 – Follows ARM Architecture Procedure Calling Standard (AAPCS) During (or after) state saving the address of the ISR is read from the Vector Table Link Register is modified for interrupt return First instruction of ISR executed – For Cortex-M3 or Cortex-M4 the total latency is normally 12 cycles, however, interrupt late-arrival and interrupt tail-chaining can improve IRQ latency ISR executes from Handler mode with Main stack
  • 21. Interrupt Service Routine Entry (2) On taking an exception during a multi-cycle instruction the processor either: – Continues execution of the instruction after returning from the exception – Abandons the instruction and restarts the instruction on returning from the exception For multi-cycle instructions on the Cortex-M3 and Cortex-M4 processors: – Divide, multiply and load/store double instructions are abandoned and restarted – Load/store multiple instructions are treated as “exception-continuable instructions”, if possible 21 AAME TechCon 2013 TC002v02 21 possible – The ICI bits in the EPSR are used to hold the continuation state – However, in some situations LDM/STM instructions are abandoned and restarted – For example, An LDM/STM instruction within an If-Then block Architecturally software must not use an LDM/STM to access a volatile location – The Cortex-M3/M4 processors must not use an LDM/STM to access a volatile location if – it executes inside an IT block – it loads the same register as the base register – it loads the PC
  • 22. Interrupt Stacking Foreground stack (Main or Process) used to store the pre-interrupt state Stack is Full-Descending – SP automatically decremented on push – SP always points to non-empty value – Same behavior as other ARM cores : <old TOS> Old SP xPSR ReturnAddress LR R12 SP 22 AAME TechCon 2013 TC002v02 22 The stack must be 8-byte aligned – AAPCS compliant – STKALIGN bit in Configuration and Control Register should be set to 1 – Extra word padded to stack if required Main stack active after state is saved ISR can be coded in “C” – No prologue code required (no overhead) : : : R12 R3 R2 R1 R0 0x0
  • 23. Returning From Interrupt Can return from interrupt with the following instructions when the PC is loaded with “magic” value of 0xFFFF_FFFX (same format as EXC_RETURN) – LDR PC, ….. – LDM/POP which includes loading the PC – BX LR (most common) If no interrupts are pending, foreground state is restored 23 AAME TechCon 2013 TC002v02 23 If no interrupts are pending, foreground state is restored – Stack and state specified by EXC_RETURN is used – Context restore on ARM Cortex-M3 and ARM Cortex-M4 requires 10 cycles If other interrupts are pending, the highest priority may be serviced – Serviced if interrupt priority is higher than the foreground’s base priority – Process is called Tail-Chaining as foreground state is not yet restored – Latency for servicing new interrupt is only 6 cycles on M3/M4 (state already saved) If state restore is interrupted, it is abandoned – New ISR executed without state saving (original state still intact and valid) – Must still fetch new vector and refill pipeline (6-cycle latency on M3/M4)
  • 24. Nesting Example Higher Priority Base CPU IRQ2 IRQ1 24 AAME TechCon 2013 TC002v02 24 Core Execution Base CPU Time Foreground ISR2 ISR1 ISR2 ForegroundPop Pop IRQ1 Active IRQ2 Active Pop Interrupt entry requires 12 cycles Interrupt exit requires 10 cycles Push Push Push
  • 25. Tail Chaining Example Higher Priority Base CPU IRQ2 IRQ1 Time 25 AAME TechCon 2013 TC002v02 25 Core Execution Base CPU Foreground ISR1 ISR2 Time TC Pop IRQ1 Active IRQ2 Active Tail Chaining requires 6 cycles Push Foreground TC
  • 26. Late Arriving Example Higher Priority Base CPU IRQ2 IRQ1 26 AAME TechCon 2013 TC002v02 26 Core Execution Base CPU Time Foreground ForegroundPopISR1 ISR2 A pending higher priority exception is handled before an already pending lower priority exception even after exception entry sequence has started The lower priority exception is handled after the higher priority exception Push TC
  • 27. Agenda Exceptions Overview Priority & Pre-emption Exception Behavior Priority Management 27 AAME TechCon 2013 TC002v02 27 Priority Management Writing Exception Handlers Internal Exceptions & Faults Memory Protection Unit SysTick Timer
  • 28. Disabling Interrupts (Priority Boosting) Priority Boosting raises the base level of execution priority level to 0 This disables interrupts by preventing all programmable exceptions from activating The Priority Mask Register is used Programmable Exceptions 0x80 0xC0 Lowest Priority Priority Boosting Activation Prevented 28 AAME TechCon 2013 TC002v02 28 The Priority Mask Register is used to disable interrupts ... Reset NMI Fault 0x00 -1 -2 -3 0x40 Highest Priority “Base” Execution
  • 29. Priority Boosting Three methods to set the Execution Priority via software on v7-M BASEPRI register sets execution priority level from 1 to N (N=lowest priority) – Acts as a general interrupt mask – Example – BASEPRI = 16 only interrupts with priority levels (-2 to 15) can pre-empt – If BASEPRI = 0 masking is disabled (any enabled interrupt pre-empts the core) 29 AAME TechCon 2013 TC002v02 29 – If BASEPRI = 0 masking is disabled (any enabled interrupt pre-empts the core) PRIMASK bit sets the execution priority level to 0 – Only allows Hard Fault and NMI to pre-empt FAULTMASK bit sets the execution priority level to -1 – Can only be set by an exception handler (auto clears on exception return) – Only allows NMI to pre-empt
  • 30. Priority Boosting Instructions BASEPRI is accessed like a Program Status Register MSR{cond} BASEPRI, Rm ; write BASEPRI MRS{cond} Rd, BASEPRI ; read BASEPRI PRIMASK and FAULTMASK map to the classic “I” and “F” flags CPSID I ; set PRIMASK (disable interrupts) CPSIE I ; clear PRIMASK (enable interrupts) CPSID F ; set FAULTMASK (disable faults and interrupts) 30 AAME TechCon 2013 TC002v02 30 CPSID F ; set FAULTMASK (disable faults and interrupts) CPSIE F ; clear FAULTMASK (enable faults and interrupts) RVCT Intrinsics – PRIMASK __enable_irq(), __disable_irq() – FAULTMASK __enable_fiq(), __disable_fiq() Must be in privileged mode to modify execution priority level
  • 31. v7-M Priority Grouping The 8-bit Priority Level is divided into 2 fields – Group Priority and Sub-Priority [GROUP.SUB] Group Priority is the pre-empting priority level Sub-Priority is used only if the Group Priority is the same Exception Number is final tie-breaker – If two interrupts have identical priority levels, lowest vector number takes priority 31 AAME TechCon 2013 TC002v02 31 Example – 4-bit Group Priority / 4-bit Sub-Priority – SysTick is enabled with priority = 0x30 – Timer1 is enabled with priority = 0x30 – Timer2 is enabled with priority = 0x31 – If Timer1 & Timer2 assert simultaneously Timer1 serviced, Timer2 pended – If Timer2 is being serviced and Timer1 asserts Timer1 pended – If SysTick & Timer1 assert simultaneously SysTick serviced, Timer1 pended bit4 SubGroup bit7 bit3 bit0
  • 32. Group Priority / Sub-Priority Selection PRIGROUP Binary Point (group.sub) Group Priority (Pre-empting) Sub-Priority Bits Levels Bits Levels 000 7.1 gggggggs 7 128 1 2 001 6.2 ggggggss 6 64 2 4 010 5.3 gggggsss 5 32 3 8 011 4.4 ggggssss 4 16 4 16 32 AAME TechCon 2013 TC002v02 32 Selection made by PRIGROUP in Application Interrupt and Reset Control Reg System-wide configuration applies to all interrupt priorities If less than 8 priority bits implemented, fewer Sub-Priority bits are available – Example: 6 priority bits and PRIGROUP = 001 6 Group Bits / 0 Sub-Priority Bits 011 4.4 ggggssss 4 16 4 16 100 3.5 gggsssss 3 8 5 32 101 2.6 ggssssss 2 4 6 64 110 1.7 gsssssss 1 2 7 128 111 0.8 ssssssss 0 0 8 256
  • 33. Interrupt Control and Status Bits ENABLE bit (R/W) – is interrupt enabled or disabled? PENDING bit (R/W) – is interrupt waiting to be serviced? – Interrupt is pending if priority is too low or if interrupt is disabled – Can set bit to generate an interrupt (if enabled and sufficient priority) – Can clear bit to clear a pending interrupt (interrupt will not be serviced) 33 AAME TechCon 2013 TC002v02 33 ACTIVE bit (R only) – is interrupt active or inactive? – If interrupts are nested, more than one active bit will be set PRIORITY bits (R/W) – set the interrupt priority – Lower value has higher priority (priority 0 is highest) – ARMv7-M: 3 - 8 bits (8 - 256 levels) – May be divided into a group (pre-empt) priority and a sub-group priority (user defined)
  • 34. Agenda Exceptions Overview Priority & Pre-emption Exception Behavior Priority Management 34 AAME TechCon 2013 TC002v02 34 Priority Management Writing Exception Handlers Internal Exceptions & Faults Memory Protection Unit SysTick Timer
  • 35. Vector Table in Assembly PRESERVE8 THUMB IMPORT ||Image$$ARM_LIB_STACK$$ZI$$Limit|| AREA RESET, DATA, READONLY EXPORT __Vectors __Vectors DCD ||Image$$ARM_LIB_STACK$$ZI$$Limit|| ; Top of Stack DCD Reset_Handler ; Reset Handler 35 AAME TechCon 2013 TC002v02 35 DCD NMI_Handler ; NMI Handler DCD HardFault_Handler ; Hard Fault Handler DCD MemManage_Handler ; MemManage Fault Handler DCD BusFault_Handler ; Bus Fault Handler DCD UsageFault_Handler ; Usage Fault Handler DCD 0, 0, 0, 0, ; Reserved x4 DCD SVC_Handler, ; SVCall Handler DCD Debug_Monitor ; Debug Monitor Handler DCD 0 ; Reserved DCD PendSV_Handler ; PendSV Handler DCD SysTick_Handler ; SysTick Handler ; External vectors start here
  • 36. The vector table at address 0x0 is minimally required to have 4 values: stack top, reset routine location, NMI ISR location, HardFault ISR location Vector Table in C typedef void(* const ExecFuncPtr)(void) __irq; #pragma arm section rodata="exceptions_area” ExecFuncPtr exception_table[] = { (ExecFuncPtr)&Image$$ARM_LIB_STACK$$ZI$$Limit, /* Initial SP */ (ExecFuncPtr)__main, /* Initial PC */ NMIException, HardFaultException, 36 AAME TechCon 2013 TC002v02 36 HardFaultException, MemManageException, BusFaultException, UsageFaultException, 0, 0, 0, 0, /* Reserved */ SVCHandler, DebugMonitor, 0, /* Reserved */ PendSVC, SysTickHandler /* Configurable interrupts start here...*/ }; #pragma arm section Once interrupts are enabled, the vector table (whether at 0 or in SRAM) must then have pointers to all enabled (by mask) exceptions The SVCall ISR location must be populated if the SVC instruction will be used
  • 37. Writing Interrupt Handlers (1) Clear the interrupt source if required – Some may not need to be cleared (like SysTick) Interrupt nesting always enabled by default – No modification needed to interrupt handlers – Just be careful of Main stack overflows 37 AAME TechCon 2013 TC002v02 37 STKALIGN bit should be set to 1 to ensure 8-byte stack alignment on exception entry – Bit 9 in Code Configuration Register (a read-only register on latest revisions of ARM Cortex-M3)
  • 38. Writing Interrupt Handlers (2) C Code is recommended for all handlers – ISR function must be type void and can not accept arguments – The __irq keyword is optional (but recommended for clarity) __irq void SysTickHandler (void) { } – No special assembly language entry and exit code required 38 AAME TechCon 2013 TC002v02 38 No special assembly language entry and exit code required Writing handlers in assembly – Must manually save and restore any non-scratch registers which are used – R4, R5, R6, R7, R8, R9, R10, R11, LR – Must maintain 8-byte stack alignment if making function calls – Must return from interrupt using EXC_RETURN with proper instruction – LDR PC, ….. – LDM/POP which includes loading the PC – BX LR
  • 39. Agenda Exceptions Overview Priority & Pre-emption Exception Behavior Priority Management 39 AAME TechCon 2013 TC002v02 39 Priority Management Writing Exception Handlers Internal Exceptions & Faults Memory Protection Unit SysTick Timer
  • 40. Internal Exceptions SysTick Timer – Provides system heart-beat for RTOS (or custom program executive) – Periodic interrupt drives system task scheduling – Better than a standard Timer Interrupt – Integrated into the NVIC – Does not vary between vendor implementation and Cortex-M families System Service Call (SVC) 40 AAME TechCon 2013 TC002v02 40 System Service Call (SVC) – Similar to “SVC” instruction on other ARM cores – Allows non-privileged software to make system calls – RTOS services requests from non-privileged mode – Provides protection to important system functionality – Examples: Open Serial Port, Remap Memory, Enter Low Power Mode, etc. Pended System Call (PendSV) – Operates with SVC to ease RTOS development – Intended to be an interrupt for RTOS use
  • 41. SysTick Timer 24-bit count down timer 2 clock sources – Internal core clock / external clock (STCLK) 2 modes – Generate interrupt / do not interrupt 41 AAME TechCon 2013 TC002v02 41 – Generate interrupt / do not interrupt Can be used as a general timer Programmer’s Interface – Control and Status Register – sets mode and has self-clearing overflow bit – Reload Value Register – sets the SysTick period – Current Value Register – shows current SysTick value – Calibration Value Register – for configuring a 10 msec period
  • 42. Pended System Call (PendSV) Permanently enabled interrupt – Avoids need for long periods of interrupts disabled Not an instruction – but instigated via Interrupt Control State Register – May only be written in privileged mode – Set by PENDSVSET bit – 42 AAME TechCon 2013 TC002v02 42 – Cleared by PENDSVCLR bit Intended usage – As a private, low-level interrupt for the RTOS – SVC cannot be nested, so RTOS uses PendSV to perform specific tasks – SVC and PendSV are given same interrupt priority… – PendSV will only run after SVC completes (and before foreground is returned) – PendSV can then handle low-priority RTOS “cleanup” or perform context switch
  • 43. Fault Exceptions on v7-M Three classes of faults – Bus – Memory access error (Prefetch or Data Access) – Memory Manage – MPU permissions mismatch – Usage – Undefined instruction, CP access, illegal state transition (i.e. T=0) – Can optionally trap divide by zero and unaligned memory accesses Bus, Memory Manage and Usage Faults 43 AAME TechCon 2013 TC002v02 43 Bus, Memory Manage and Usage Faults – Manually enabled in the System Handler Control and State Register – Disabled by default – only enable if required – User-defined priority level Hard Fault – “Fall Back” Fault – Always enabled with priority level “-1” – Executes through “Fault Escalation”
  • 44. Fault Escalation Fault Escalation to Hard Fault occurs when a fault occurs but… – The fault is not enabled or… – The handler doesn’t have enough priority to run or… – The fault handler encounters the same fault Examples – An undefined instruction is decoded – The ARM Cortex-M3 and ARM Cortex-M4 try to signal a Usage Fault, but the 44 AAME TechCon 2013 TC002v02 44 – The ARM Cortex-M3 and ARM Cortex-M4 try to signal a Usage Fault, but the UsageFault is not enabled – With the MPU enabled an interrupt handler makes an illegal memory access – The ARM Cortex-M3 and ARM Cortex-M4 try to signal a Memory Manage Fault, but the Fault priority is lower than the priority of the running interrupt – An undefined instruction is encountered in a UsageFault handler – An enabled fault handler causes the same kind of fault as is being serviced
  • 45. Other Interrupt & Exception Information You should also be familiar with… – Instructions/functions for changing PRIMASK, FAULTMASK, BASEPRI – Format and usage of Interrupt Enable, Active, Pending and Priority registers – Format and interpretation of EXC_RETURN values – Fault handling & Fault escalation – The Lockup state – 45 AAME TechCon 2013 TC002v02 45 – Edge and Level Sensitive Interrupt Configuration
  • 46. Agenda Exceptions Overview Priority & Pre-emption Exception Behavior Priority Management 46 AAME TechCon 2013 TC002v02 46 Priority Management Writing Exception Handlers Internal Exceptions & Faults Memory Protection Unit SysTick Timer
  • 47. Motivation: Memory Protection Privileged modes Memory OS code + data 47 AAME TechCon 2013 TC002v02 47 Application code OS User mode ARMxyz Application code + data
  • 48. Default Memory Map System External Peripheral FFFF_FFFF E000_0000 512MB 1GB E00F_F000 E004_2000 E004_1000 E004_0000 E00F_FFFF External Private Peripheral Bus RESERVED UNUSED TPIU ETM ROM Table E003_FFFF 48 AAME TechCon 2013 TC002v02 48 Code SRAM Peripheral External SRAM 2000_0000 4000_0000 6000_0000 A000_0000 0000_0000 1 GB 512MB 512MB 512MB ITM Internal Private Peripheral Bus E000_E000 E000_3000 E000_2000 E000_1000 E000_0000 E000_F000 DWT FPB NVIC RESERVED RESERVED Optional Bit-Band Regions
  • 49. Memory Protection Overview MPU architecture same as ARM Cortex-R4 - ARM v7 PMSA Supports 8 regions – Each region applies to both instruction and data accesses (unified regions) Access controlled by region attributes – Individual Instruction access, Data access and Memory attributes – Controls Cacheability, Data/Peripheral Type selection Regions have variable base address and size – Base address must be aligned to the region size boundary 49 AAME TechCon 2013 TC002v02 49 Regions may overlay other regions MPU is optional – Absent or disabled MPU uses the default memory map Sub-regions also possible within each region MPU can modify some attributes of the default memory map – Cannot modify addresses of Code or System spaces – Top 0.5GB of the memory map is always “Execute Never”, so cannot be changed by MPU – Bit-Band regions (if implemented) cannot be moved – PPB is always strongly-ordered MPU primarily provides access protection – Can also make Peripheral memory partitions executable
  • 50. Region Overview Each region consists of: – Base address – Region Attribute and Size – Memory Type and Access Control At least one region must be defined (or PRIVDEFENA set) before enabling the MPU – No regions defined or enabled after reset – Accesses that do not fall into any region cause an abort, unless privileged and PRIVDEFENA is set 50 AAME TechCon 2013 TC002v02 50 – Region 0 lowest priority – Code enabling MPU must be included in an executable region – Otherwise, core will continually abort – If MPU enabled, only System space (SCB) and vector table loads are guaranteed to be available – Other accesses depend upon region settings and PRIVDEFENA Base address must always be aligned to the region size MPU regions are set up through the MPU registers in the SCB
  • 51. Sub-Regions Each region can be split into 8 equal sized non-overlapping sub-regions – Allows the granularity of protection and memory attributes to be increased without significant increase in hardware complexity – Reduces the number of regions which must be used to align protection boundaries to non-naturally aligned addresses Each region has 8 sub-region disable bits – Sub-region disable bit=0, sub region described as part of the region 51 AAME TechCon 2013 TC002v02 51 – Sub-region disable bit=0, sub region described as part of the region – Protection and memory type attributes of the region apply to that sub-region – Sub-region disable bit=1, sub region described as not part of the region – Address covered by that sub-region is not matched as part of the region Sub-regions being disabled enables backward compatibility with ARMv6 All region sizes between 256bytes and 4Gbytes support 8 sub-regions Region sizes below 256bytes do not support sub-regions
  • 52. Further MPU Information You should also be familiar with… – Region overlapping and sub regions – Memory types and type extensions – Access permissions – MPU Alias Registers 52 AAME TechCon 2013 TC002v02 52
  • 53. Agenda Exceptions Overview Priority & Pre-emption Exception Behavior Priority Management 53 AAME TechCon 2013 TC002v02 53 Priority Management Writing Exception Handlers Internal Exceptions & Faults Memory Protection Unit SysTick Timer
  • 54. The SysTick Timer Inclusion of the timer is implementation-defined in ARMv6-M – Mandatory in ARMv7-M Provides internal System Timer (SysTick) – And a related SysTick exception 54 AAME TechCon 2013 TC002v02 54 – And a related SysTick exception SysTick can be used in different ways – Simple counter – RTOS tick timer – Dynamic clock management
  • 55. The System Timer - SysTick SysTick is a 24-bit counter – Decrementing – Clear-on-write – Reload-on-zero 55 AAME TechCon 2013 TC002v02 55 Flexible control mechanism – Using 4 registers
  • 56. SysTick Registers SysTick Control and Status – Configure clock – Enable the counter – Enable the SysTick interrupt – Determine counter status 56 AAME TechCon 2013 TC002v02 56 SysTick Reload value – Counter wrap value SysTick Current value SysTick Calibration value
  • 57. Basic SysTick Operation 1. Reload counter using Reload register 2. Enable/disable counter using Control & Status – Current value is decremented when enabled until 0 is reached – Can generate a counter event – e.g. SysTick Interrupt 3. Counter is auto-reloaded when value is 0 on wrap-around 57 AAME TechCon 2013 TC002v02 57 Reload 0x100000 Current 0x100000 0x000000Wrap around Counter EventControl&Status
  • 58. SysTick Clocks Control & Status specifies which clock should be used by the counter Core clock – When no external reference clock is provided (NOREF) External reference clock – Can be provided by the implementation 58 AAME TechCon 2013 TC002v02 58 ARM Processor Current clock Control&Status Core Clock External Reference Clock
  • 59. SysTick Calibration Calibration can be used to allow code to measure real-time intervals in a standard way – Implementation-independent 2 clock sources can be implemented – External reference clock 59 AAME TechCon 2013 TC002v02 59 – Internal clock SysTick Calibration Value register provides information for calibration – Reload value to count 10ms (TENMS) – Possibility to mark the 10ms as inexact (SKEW) – Due to rounding
  • 60. Calibration - TENMS Provides a 10ms reference timing for the SysTick counter – Can be loaded directly into the Reload Value register – Software can scale the counter to desired clock rates – Reload value = (factor * (TENMS + 1)) - 1 Ensures a 10ms cycle of the SysTick counter – Can be used for real-time applications 60 AAME TechCon 2013 TC002v02 60 If TENMS is 0 – No 10ms reference value is provided – Reference clock is not constant 10ms (100Hz) 1kHz 2kHz TENMS = 9, or 0x9 TENMS = 19, or 0x13
  • 61. Calibration - SKEW Provides information if the TENMS value is exact to the reference clock SKEW = 0 – No skew, 10ms can be exactly represented SKEW = 1 61 AAME TechCon 2013 TC002v02 61 SKEW = 1 – 10ms cannot be exactly represented 10ms 1kHz 1.05kHz SKEW=0 SKEW=1 9.5 TENMS 9 TENMS