SlideShare a Scribd company logo
Embedded System
Programming on ARM
Cortex-m3/m4
For full video course on Microcontroller and RTOS
programming please visit : www.fastbitlab.com
All courses are hosted
on Udemy.com
Click here to enroll FREE !!!!
http://bit.ly/2P47IeX
Click here to watch free preview and enroll
http://bit.ly/2QaW0M9
Click here to watch free preview and enroll
http://bit.ly/2SA2uFO
Click here to watch free preview and enroll
http://bit.ly/2PAQRQh
Click here to watch free preview and enroll
http://bit.ly/2AEcneA
Click here to watch free preview and enroll
http://bit.ly/2zhqDrX
Click here to watch free preview and enroll
http://bit.ly/2OjgFf7
Overview
Motivation to learn ARM-Cortex-M3/M4
• It’s really cheap
• High speed
• Less Power
• More Code Density
• Better Interrupt Management
• Many more ….
Cortex
M3/M4
Micro-Controller
Many More . . . .
What will you learn at the end of the
course ??
• Architecture
• Programming model
• Memory Architecture
• Interrupt Management
• Exception handling
• Buttons, LEDs
• Programming and Debugging
using KEIL
• Debugging using Logic
analyzers
• Lab Sessions with lots of
Code implementations
Cortex M3/M4 Processor
Architecture
KIRAN NAYAK | SECTION-1
Cortex-M3/M4 in SW developers point
of view
What the firmware/Embedded Developers should be
knowing ???
 Programming model
 How exceptions are handled
 The memory map
 Peripheral interfacing
 How to use the S/W driver libraries from
Microcontroller vendors
Programmer’s Model:
Operational Modes & Access Levels
How does Operational model of the processor look like ?
Thread mode
Handler Mode
Privileged level
Non-privileged
level
Operation Modes Access Levels
Operational model
Operational modes
Thread mode
Handler Mode
 Processor executes the normal application
code
 Privileged level or un-privileged level
 On reset processor enters to thread mode
 Enters during System exceptions
/interrupts
 Code executing is privileged
 Only way user can put the processor to
handler mode is to raise an system
exception or interrupt
Access Levels
 Full access to processor resources
 Handler mode is always privileged
 Thread mode is by default privileged ,
but can be changed to un-privileged
Privileged level
Non-privileged
level
 Restricted access .
 You can make thread mode to
run in this level .
Switching between Privileged and Un-
privileged Access Level
Priv
level
Un-Priv
level
CONTROL[0] =0
Thread mode
Thread mode
Priv
level
Handler mode
Reset
Exception
Exception handler
CONTROL[0] =0
1. First Processor start with thread mode and privileged access level
2. Changing CONTROL[0]=1 move the processor into un-privileged mode
3. Its not possible to come back to priv level at this stage
4. Issue a processor exception
5. Processor executes exception handler in handler mode with privileged access level
6. making CONTROL[0]=0 makes return to privileged level
7. Otherwise processor return to un-privileged level
Application of switching between Access
levels:
In Embedded Operating system Design
The kernel of a RTOS or embedded OS can change the control
register to make an application task to run in unprivileged mode when
it gets scheduled to run.
It helps to design more secured and robust applications
Programmer’s Model:
Register Model
R0
R1
R2
R3
R4
R5
R6
R7
R8
R9
R10
R11
R12
R13(MSP)
R14
R15
R13(PSP)
General
Purpose
Registers
Main stack Pointer Process stack Pointer
Link Register(LR)
Program Counter(PC)
xPSR
PRIMASK
FAULTMASK
BASEPRI
CONTROL
Special Purpose Registers
Low
registers
High
registers
Register model
Special Purpose
Registers
 Registers R0-R12 are general purpose
registers
 R0-R7 are called low registers because,
many 16 bit instructions can only access
low registers.
 All 32 bit instructions and very few 16 bit
instructions can access high registers i.e
R8-R12
 The initial values of R0 to R12 are
undefined.
R0
R1
R2
R3
R4
R5
R6
R7
R8
R9
R10
R11
R12
Low
registers
High
registers
generalpurposeregisters
General purpose Registers
R13 stack pointer(SP)
 Physical there are 2 stack pointer exist. The MSP
and PSP.
 MSP is the default stack pointer.
 R13 just points to the value of the currently
selected stack pointer .
 You can even change the stack pointer to PSP,
by writing to CONTROL register of the processor
 When the processor is executing exception
handler, it always uses the MSP .
R13(MSP)
R13(PSP)
Main Stack Pointer
Process Stack Pointer
R14,Link register(LR)
R14
Link Register(LR)
R15,Program Counter(PC)
 Always points to next instruction to be
executed
 Always increments by 4 ( word size )
 Points to word aligned address in the
code memory .
R15
Program Counter(PC)
Programmer’s Model:
Special Purpose Registers
xPSR
PRIMASK
FAULTMASK
BASEPRI
CONTROL
Processor status register
Exception masking registers
Processor Control Register
Special Purpose Registers
APSR
EPSR
IPSR
32bits
Quiz -1
So , How do you access General Purpose/Special Purpose
registers in a ‘C’ program when they are not memory
mapped ??
EPSR
IPSR
32bits
APSR
XPSR
Negative Flag
Zero Flag
Carry/Borrow Flag
Overflow Flag
Saturation Flag
APSR
EPSR
IPSR
32bits
XPSR
APSR
Indicates the interrupted
position of a
continuable instruction
Thumb state bit
EPSR
IPSR
32bits
XPSR
APSR
IPSR
EPSR
This is the number of the current exception
0 = Thread mode
1 = Reserved
2 = NMI
3 = HardFault
.
.
16 = IRQ0
n+15 = IRQ(n-1)
PRIMASK
FAULTMASK
BASEPRI
7:1 031:8
3 bits to 8
bits
0 bit to 5
bits
Reserved
Exception masking registers
CONTROL Register
0 = Privileged
1 = Unprivileged
SPSEL
nPRIV0 = MSP
1 = PSP
CONTROL Register
 After reset, the CONTORL register is 0.
 This means , by default, Thread mode uses MSP as the stack
pointer and code runs in the privileged access level.
 Programs in privileged Thread mode can switch the stack pointer
selection or switch to unprivileged access level by writing to
CONTROL.
 Once “nPriv” is set to 1, the program running in thread mode can no
longer access the CONTROL register.
Cortex Microcontroller Software
Interface Standard
(CMSIS)
CMSIS
Why to use CMSIS ?
 CMSIS is a Hardware Abstraction Layer for Cortex-M series
microcontrollers.
 It supports developers and vendors in creating reusable software
components for ARM Cortex-M based systems.
 If a library is CMSIS compliant, it's vendor-independent, and its
easier to swap different families like Cortex M0 to Cortex M3.
Why to use CMSIS ? Contd.
 One of the attractive aspects of the ARM Cortex environment
with CMSIS is the ability to change platforms without a whole
bunch of sweat.
 If you pick a platform that doesn't buy into the CMSIS structure,
you may not be able to move around as conveniently.
Why to use CMSIS ? Contd.
 Simplifying software re-use and software portability . i.e you will
quickly able to port code written for Cortex M3 to Cortex M4 when
you want to migrate.
 Reducing the learning curve for microcontroller developers
 Reducing the time to market for new devices
Processor Reset Sequence
RESET
0x20008000 0x20001000
0x00000000 0x00000004 0x200010001
32 4
First
instruction
Next
Instruction
MSP=0x20008000 PC=0x20001000 Reset handler
1. After reset, PC is loaded with the address 0x00000000
2. Processor read the value from 0x00000000 location in to MSP
3. Then processor reads the address of the reset handler from the location
0x00000004
4. Then it jumps to reset handler and start executing the instructions
5. Then you can call your main() function from reset handler.
Main()
Memory Address
Switching between
Privileged and Un-Privileged
Access level of Execution
Points To Remember !!
 Processor always starts with Privileged Access level
 In privileged level you can access any Restricted Register
 From Privileged Level you can go to Un-Privileged access level
 In unprivileged access level you can not access Restricted
Registers
 From Un-Privileged access level you can not come back to
Privileged access level
 You can only change the access level to privileged ,from an
exception/interrupt handler
Lets Code and Understand !!
Memory System Architecture
KIRAN NAYAK| SECTION-4
Session Overview
At the end of the session you will be able to understand
 Memory system features
 Memory map of the processor
 Bus interfaces
 Bus protocols
 Aligned and un-aligned data transfer
 Bit banding and its advantages
 Code example
Memory System Features
Features
 All the Cortex-M processors have 32 bit memory addressing, as a result
there is 4GB of addressable memory space
 The memory is one unified space which is shared by code space, data
space and peripheral space.
 Harvard bus architecture . It means concurrent instruction and data
accesses using multiple bus interfaces.
 Support both little endian and big endian memory systems.
 Support for unaligned data transfers.
 Bit addressable memory spaces(bit-banding)
 MPU support
CODE
SRAM
Peripherals
External RAM
External Device
Private peri bus(internal)
Private peri bus(external)
Vendor specific
Bit-Band Region
Bit-Band Alias
Bit-Band Region
Bit-Band Alias
NVIC
External private
peripheral bus
ROM Table
0x60000000
0x1FFFFFFF
0x20000000
0x3FFFFFFF
0x40000000
0x5FFFFFFF
0x00000000
0x9FFFFFFF
0xA0000000
0xDFFFFFFF
0xE0000000
0xE003FFFF
0xE0040000
0xE00FFFFF
0xE0100000
0xFFFFFFFF
0xE000FFFF
0xE00FF000
0xE0042000
0xE000F000
0xE000E000
0x43000000
0x42000000
0x40100000
0x40000000
0x23FFFFFF
0x20100000
0x20000000
Memory map
0x22000000
CODE Region
Range : 0x00000000–0x1FFFFFFF
CODE
0x1FFFFFFF
0x00000000
512 MB
Vector table
SRAM Region
Range : 0x20000000–0x3FFFFFFF
 The SRAM(Static-RAM) region is located in the
next 512 MB of memory space after CODE region
 It is primarily for connecting SRAM, mostly on
chip SRAM.
 The first 1 MB of the SRAM region is bit
addressable.
 You can also execute program code from this
region
Bit-Band Region
Bit-Band Alias
0x20000000
0x20100000
0x21FFFFFF
0x22000000
0x23FFFFFF
0x3FFFFFFF
SRAM
Bit-Band Region
Bit-Band Alias
0x43FFFFFF
0x41FFFFFF
0x40100000
0x40000000
Peripherals
0x40000000
0x5FFFFFFF
0x42000000
Address Range : 0x40000000–0x5FFFFFFF
Peripherals Region
1MB
External RAM
0x60000000
0x9FFFFFFF
Address range : 0x60000000 to 0x9FFFFFFF
 This region is intended for either on-chip or off-chip
memory
 you can execute code in this region.
External RAM Region
External Device Region
Range : 0xA0000000 to 0xDFFFFFFF
 This region is intended for external
devices and/or shared memory
 It is a non-executable region.
External Device
0xA0000000
0xDFFFFFFF
Bus Protocols & Bus Interfaces
BUS Protocols
 AHB Lite (Main System Bus )
 APB(Peripheral bus )
BUS Protocols Contd.
 AHB Lite (Main System Bus )
 In The cortex m3 and m4 processors , the AHB lite protocol used
for the main bus interfaces
 AHB lite stands for AMBA High Performance Bus which is derived
form AMBA(Advanced Microcontroller Bus Architecture )
specification
BUS Protocols Contd.
 APB(Peripheral bus )
 The APB is an AMBA-compliant bus optimized for minimum power
and reduced interface complexity
 The AHB-APB bridge is an AHB slave that provides an interface
between the high- speed AHB domain and the low-power APB
domain.
Cortex M3/M4FLASH/ROM
AHB-APB
Bridge
APB 0
UART SPI I2C
I2S LCD GPIO
TIM0 TIM1 PMU
WDT RTC MISC
EEPROM
SRAM
High Speed
peripherals
DMA
USB
Touch
controller
APB 1
I-Code(AHB)
AHB
AHB
AHB
AHB
AHB
AHB
D-Code(AHB)
Systembus(AHBLite)
BUS interfaces
External PPB
(debug
components )
PPB(APB)
Aligned and Un-aligned data transfer
Aligned Data Transfer
Struct mydata
{
Unsigned long data1;
Char data2
Unsigned long data3
Char array[3]
Short data4;
Unsigned long data5
}
What Is Un-Aligned Data Transfer ?
Struct __packed mydata
{
Unsigned long data1;
Char data2
Unsigned long data3
Char array[3]
Short Int data4;
Unsigned long data5
}
How Unaligned Data Access Can Result ?
 Direct manipulation of pointers
 Accessing data structure with __packed attributes that result in
unaligned data
 Inline Assembly code
Instruction alignment in memory
BIT-BANDING
What is bit-banding ?
 It is a capability to address a single bit of a memory address
 This feature is optional
0x20000000
0x20100000
0x22000000
0x23FFFFFF
0x3FFFFFFF
Bit-Band Memory Region
Bit-Band Alias Region
SRAM
Peripheral memory
regionBit band region
Bit band alias
region
0x40000000
0x40100000
0x42000000
0x43FFFFFF
1MB
Bit-Band memory regions
Example
Lets assume we want to set the value of the 2nd bit in the
address 0x20000000
Without bit-band
Read 0x20000000
to register
Mask and set bit 2
Write back to
0x20000000
Write 1 to
0x22000008
Read data from
0x20000000
Write to 0x20000000
from buffer with bit2
set
With bit-band
Three instructions
single instruction
Mapped to 2
bus transfers
How this works ??
Alias Equivalent
0x20000000 bit[0]
0x20000000 bit[1]
0x20000000 bit[2]
…..
0x20000000 bit[31]
0x20000004 bit[0]
0x20000004 bit[1]
0x22000000 bit[0]
0x22000004 bit[0]
0x22000008 bit[0]
…..
0x2200007C bit[0]
0x22000080 bit[0]
0x22000084 bit[0]
Bit-Band Region
0X20000008
0X20000004
0X20000000
Bit band addresses
0x22000000
0x22000018
0x2200003C
0x22000080
Bit band alias addresses
Bit band alias addresses
Quiz -2
What is the value of bit-band alias address to address the 6th bit of the
address 0x20000004 ??
0x20000004 bit[6] Alias address = ??
1. reading the whole
register
2. Mask and check bit
3. Compare and branch
1. Read bit using bit-
band alias address
2. Compare and branch.
Instead of doing this Simply do this
Advantages of bit band regions
Instead of doing this
Disable the interrupt
Read the memory
Mask and change the bit
Write back
Change the value of
memory region using its
alias address
Enable the interrupt
Simply do this
Demonstrating Bit-Band Operations
Points To Remember !!
• Bit band Operations can only be operated on 2 memory regions. This is
called bit-band region
 0x20000000 to 0x20100000 (1 MB)
 0x40000000 to 0x40100000(1 MB)
• To Set/Reset any bit field in the Bit band region , we have to use Bit
band alias address in the Bit band alias region
 0x22000000 to 0x23ffffff (31 MB)
 0x42000000 to 0x43ffffff(31 MB)
Memory Remapping
Alias Equivalent
0x20000000 bit[0]
0x20000000 bit[1]
0x20000000 bit[2]
…..
0x20000000 bit[31]
0x20000004 bit[0]
0x20000004 bit[1]
…..
0x22000000 bit[0]
0x22000004 bit[0]
0x22000008 bit[0]
…..
0x2200007C bit[0]
0x22000080 bit[0]
0x22000084 bit[0]
…..
Bit-Band Region
In this Program we will change the value of BIT0, BIT1 and BIT2 of the
data stored in the memory address 0x20000000 using bit-band
operations
Lets Code and Understand
Congratulations !! 
You Have Learnt
• How to Carry-out Bit-Band Operations in C
Stacks
KIRAN NAYAK| SECTION-6
Stack memory
 Stack is a kind of memory usage mechanism that allows a portion of
memory , typically RAM to be used as Last In First Out(LIFO) data
storage buffer.
 ARM Processors have the PUSH instruction to store data in stack
and the POP instruction to retrieve data from stack.
 ALL stack operations are word aligned
Why stack is used ?
 Temporary storage of original data when a function being executed
needs to use registers for data processing.
 Passing of information to functions or subroutines
 For storing local variables
 To hold processor status and register values in the case of exception
such as an interrupt
Stack pointers
Physically there are 2 stack pointers in the Cortex-M3/M4
processors.
1. Main stack Pointer(MSP):
 This is the default stack pointer used after reset.
 used for all exception handlers.
 after power up, the processor hardware automatically initializes the MSP by
reading the vector table.
2. Process Stack Pointer(PSP)
 This is an alternate stack point that can only be used in Thread mode
 The PSP is not initialized after power up and must be initialized by the
software before being used.
Stack pointers
As mentioned Previously the selection between MSP and PSP can
be controlled by the value of SPSEL bit in the CONTROL register.
Stack Memory Model
STACK MEMORY MODEL
0x20000000
0x20007FFF
32KB
0X20007C00
SRAM
0x20008000MSP
stack
RESET
Initialize the MSP from vector
table
MSP=0x20008000
Fetch the reset handler
address from vector table
Reset handler
System init
main()
Initial Stack Init
0x20000000
0x20007FFF
32KB
0X20007C00
SRAM
0x20008000MSP
stack
Subroutine and Stack
SUBROUTINE AND STACK
As per the Procedure Call Standard Of Arm Architecture.
1. when a function is called, as per below table, registers are used for parameter passing.
2. It’s the callee “function” responsibility to push the contents of R4-R11,R13,R14 if the
function is going to change these registers(compiler takes care when coded in c )
Register Input Parameter Return value
R0 First input parameter Function return value
R1 Second input parameter Function return value if size is
64bit
R2 Third input parameter
R3 Fourth input parameter
Stacking and Un-stacking during Exception
TASK A
Exception
handler
stacking
Thread Mode Handler Mode
(using MSP )(using MSP )
exception
Used stack space
Last stacked item
MSP
Memory
address
xPSR
Return address(PC)
LR
R12
R3
R2
R1
R0
Processor does
this automatically
Stack
Frame
Stacking
TASK A
Un-stacking
Handler Mode Thread Mode
(using MSP ) (using MSP )
Exception
handler
Exit
Used stack space
Last stacked item
MSP
Memory
address
xPSR
Return address(PC)
LR
R12
R3
R2
R1
R0
Stack
Frame
Un-stacking
Exception
handler
TASK A
stacking
Thread Mode
(using MSP )
exception
Processor does
this automatically
Demonstration of Stack Operations
Using Different Stack
Pointers(MSP/PSP)
Write a program to PUSH the contents of R0,R1,R2
Registers using MSP as a stack pointer, and then POP the
contents back using PSP as a stack pointer
Points to Remember !!
 Processor always start in Privileged level and uses MSP as a
stack pointer
 If you wish, you can change the stack pointer to PSP by writing
to CONTROL register
 In C program , if you want to access MSP,PSP,CONTROL
register then you have to use either CMSIS APIs or assembly
programming.
Lets Code and Understand
System Exceptions &
Interrupts
KIRAN NAYAK| SECTION-8
What Are Exceptions ??
 Exceptions Are events, which are generated asynchronously, either
from external world or from internal system .
 When generated processor changes normal program flow to provide
the service for the generated exceptions
“In a simple way, anything which disturbs the normal flow of
execution of the processor is an exception “
What Are Interrupts ?
 Remember Interrupt is also an exception !!
 We use a word “interrupt” for the exception from external
world. E.g.: peripheral like timers, RTC,I2C,NMI, I/Os. External
means “external to the processor core”
 Courtex-M3/M4 Supports 240 interrupts
Exception types
 Exceptions are divided into 2 types
• System exceptions(internal to the processor)
• External exceptions(as already said, name for this is interrupt )
 15 system exceptions, numbered from 1 to 15.
 240 interrupts, numbered from 16 to 255
 So for e.g. the 16th exception is an interrupt#0
Exception
number
Exception type Priority Description
1 Reset -3(Highest) Reset. Pressing a reset button causes this
2 NMI -2 Non Maskable Interrupt.
3 Hard Fault -1 all fault conditions, if the corresponding fault handler
is not enabled
4 Mem Manage Fault Programmable memory manager fault: happens when try to access
illegal memory areas, MPU Violation
5 Bus Fault Programmable Bus Error. Prefetch abort or data abort on AHB bus
6 Usage Fault Programmable invalid usage of some features by the program
7-10 Reserved ---
11 svc Programmable supervisor call
12 Debug monitor Programmable debug monitor(breakpoints, watchpoints )
13 Reserved --
14 PendSV Programmable Pendable service call
15 SysTick Programmable System Tick Timer
List Of System Exceptions
List of Interrupts
Exception
Number
interrupt
number
Exception Type Priority
16 0 external interrupt 0 programmable
17 1 external interrupt 1 programmable
-- -- -- --
-- -- -- --
255 240 external interrupt 240 programmable
RESET
(e.n=1)
Priority: -
3
NMI
(e.n=2)
Priority:-
2
HARDFAULT
(e.n=3)
Priority: -1
MEMFAUL
T
e.n=4
BUSFAU
LT
e.n=5
USAGEFAUL
T
e.n=6
SYSTIC
K
e.n=15
Programmable priority and can be masked out
e.n: exception number
Interrupt
#0
Interrupt
#1
Nested Vectored Interrupt Controller
(NVIC)
“This is the HW block, which receives and manages exceptions
from various sources and delivers to the processor core as per
priority ! ”
Lets Understand how System Exceptions and
Interrupts are connected to the Processor
N
V
I
C
Processor
core
16
17
18
19
.
.
.
.
.
.
.
255
Interrupt #0
Interrupt#1
Interrupt #2
Interrupt #3
Interrupt #239
Various external
peripherals like
(Timers
ADC
Watchdog
GPIO
SPI
I2C
Etc)
Exception Number
NVIC lines
Exception #16 is ext Interrupt #0
ARM cortex-M3/M4
System
Exception
Points to Remember
 The cortex M processors have a number of programmable registers
for managing the interrupts .
 Most of these registers are inside NVIC block (and System Control
Block ).
 There are 2 ways to configure and mange the interrupt
 Directly access the NVIC registers
 User CMSIS core APIs
 The NVIC registers can only be accessed in privileged access level.
Points to remember Contd.
 After reset , all interrupts are disabled and given a priority level value
of 0. So before using any interrupts you need to .
 Set up the priority level of the required interrupt (optional)
 Enable the interrupt in the NVIC interrupt enable register.
 When the interrupt triggers, the corresponding ISR will execute (you
might need to clear the interrupt request from the peripheral with in
the handler ).
 The name of the ISR can be found inside the vector table inside the
startup code. Which is also provided by the microcontroller vendor.
Interrupt Priority
Interrupt Priority
 When an interrupt can be accepted by the processor and get
its handler executed is dependent on the priority of the
interrupt.
 A higher priority interrupt can pre-empt a lower priority
interrupt. This is also called nesting of interrupts
 Some of the exceptions(reset , NMI ,and HardFault) have
fixed priority levels. Their priority level are represented in
negative numbers to indicate that they are of higher priority
than other exceptions.
 Remember , lower numbers for priority indicates higher priority
Interrupt Priority Levels
 Different microcontroller have different levels of priority, for example 8 levels
of priority , 16 levels of priority ,etc.
 The microcontroller vendors are free to choose how many levels of priority
level they want.
 There is a register called "Priority level register". which determines how many
priority level actually supported in the microcontroller implementation
Interrupt Priority Level Register
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
Implemented Not implemented
Microcontroller Vendor XXX Microcontroller Vendor YYY
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
Implemented Not implemented
8 Levels of Priority level 16 Levels of Priority level
Write has no effect Write has no effect
0x00,0x20,0x40,0x60,
0x80,0xA0,0xC0, 0xE0
0x00,ox10,0x20,0x30,0x40,0x50,
0x60,0x70,0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe
0,0xf0
RESET
NMI
HARD FAULT
Programmable
Exceptions
0X20
0X40
0X60
0X80
0XA0
0XC0
0XE0
0XFF
0X20
0X40
0X60
0X80
0XA0
0XC0
0XE0
0X10
0X20
0X30
0X40
0X50
0X60
0X70
0X80
0X90
0XA0
0XB0
0XC0
0XD0
0XE0
0XF0
0X00
0X00
Highest Priority
-3
-2
-1
0
lowest Priority
3-bits priority width
In priority level
register
4 bits priority width
In priority level
register
Priority Grouping
Priority Group Pre-empt priority field sub-priority field
0(default) Bit[7:1] Bit[0]
1 Bit[7:2] Bit[1:0]
2 Bit[7:3] Bit[2:0]
3 Bit[7:4] Bit[3:0]
4 Bit[7:5] Bit[4:0]
5 Bit[7:6] Bit[5:0]
6 Bit[7:7] Bit[6:0]
7 None Bit[7:0]
Priority
Group
Pre-empt priority field sub-priority field
0(default) Bit[7:1] Bit[0]
1 Bit[7:2] Bit[1:0]
2 Bit[7:3] Bit[2:0]
3 Bit[7:4] Bit[3:0]
4 Bit[7:5] Bit[4:0]
5 Bit[7:6] Bit[5:0]
6 Bit[7:7] Bit[6:0]
7 None Bit[7:0]
Application Interrupt And Reset Control Register
Priority Grouping
Pre-Empt Priority : when the processor is running interrupt handler ,
and another interrupt appears, then the pre-empt priority values will
be compared and exception with higher pre-empt priority(less in
number) will be allowed to run.
Sub Priority : this value is used only when two exceptions with
same pre-empt priority level occur at the same time. In this case ,
the exception with higher sub-priority(less in number) will be handled
first .
Priority Grouping
Priority Grouping Case Study
Case 1 :
when the Priority group = 0,
As per the table ,
we have
pre-empt priority width = 7bits (128 programmable
interrupt levels )
But only 3 bits are implemented so ,8 programmable
interrupt levels
Sub-priority width = 1 ( 2 programmable sub priority
level )
Bit 0 is not implemented so no sub priority level
Bit
7
Bit
6
Bit
5
Bit
4
Bit
3
Bit
2
Bit
1
Bit 0
Pre-empt priority Pre-empt priority
Sub
priorit
y
Not implemented
priority level register
Case 2 : when the Priority_group = 5,
pre-empt priority width = 2 bits ( 4 programmable
levels )
Sub-priority width = 6 ( 64 programmable sub
priority level)
Since only 1 bit is implemented , only 2
programmable sub priority levels
Priority Grouping Case Study
Bit
7
Bit
6
Bit
5
Bit
4
Bit
3
Bit
2
Bit
1
Bit 0
Preempt
priority
Not implemented
Not implemented
Sub
pri
When the Priority group = 7, find out
pre-empt priority width = ??
Sub-priority width = ??
And give a conclusion,
How pre-emption works in the system ?
QUIZ-3
Configuring Interrupt Priority
Function void NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
Description Sets the priority grouping field using the required unlock sequence. The
parameter PriorityGroup is assigned to the field SCB->AIRCR [10:8] PRIGROUP
field.
Parameter priority group value (0...7)
Return none
Function uint32_t NVIC_GetPriorityGrouping
Description Reads the priority grouping field from the NVIC Interrupt Controller.
Parameter none
Return Priority grouping field (SCB->AIRCR [10:8] PRIGROUP field)
Function uint32_t NVIC_EncodePriority (uint32_t PriorityGroup, uint32_t
PreemptPriority, uint32_t SubPriority)
Description Encodes the priority for an interrupt with the given priority group, preemptive
priority value, and subpriority value..
Parameter PriorityGroup Used priority group.
PreemptPriority Preemptive priority value (starting from 0).
SubPriority Subpriority value (starting from 0).
Return Encoded priority. Value can be used in the function NVIC_SetPriority().
Function void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
Description Sets the priority of an interrupt.
Parameter IRQn Interrupt number.
priority Priority to set.
Return none
Configuring Interrupt Priority
Case 2 :
when the Priority_group = 7,
As per the table , in this case
pre-empt priority width = none
Sub-priority width = 8 ( 256 programmable
sub priority level )
Since only 3 bits are implemented , only 8
programmable sub priority levels
Bit
7
Bit
6
Bit
5
Bit
4
Bit
3
Bit
2
Bit
1
Bit 0
sub priority sub priority
Not implemented
Priority Grouping Case Study
Exception Activation and
De-Activation
Interrupt Activation
Function void NVIC_EnableIRQ(IRQn_Type IRQn)
Description Enables a device-specific interrupt in the NVIC interrupt controller.
parameter IRQn External interrupt number. Value cannot be negative.
return none
Function void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
Description Sets the priority of an interrupt.
parameter IRQn Interrupt number.
priority Priority to set.
return none
Global Exception Enable/Disable
7:1 031:8
3 bits to 8 bits
0 bit to 5
bits
Reserved
CMSIS APIs to handle PRIMASK register
void __enable_irq(); // Clear PRIMASK
void __disable_irq(); // Set PRIMASK
void __set_PRIMASK(uint32_t priMask); // Set PRIMASK to value
uint32_t __get_PRIMASK(void); // Read the PRIMASK value
FAULTMASK
BASEPRI
PRIMASK
Global Exception Enable/Disable
Reserved
7:1 031:8
CMSIS APIs to handle FAULTMASK register are
void __set_FAULTMASK(uint32_t faultMask);
uint32_t __get_FAULTMASK(void);
FAULTMASK
Global Exception Enable/Disable
For example, if you want to block all exceptions with priority level equal to or
lower than 0x60 , you can use this CMSIS function.
__set_BASEPRI(0x60); // Disables interrupts with priority 0x60 to 0xFF
You can also read back the value of BASEPRI:
x = __get_BASEPRI(void); // Read value of BASEPRI
BASEPRI Reserved
3 bits to 8 bits
0 bit to 5
bits
7:1 031:8
Pending Interrupt Behavior
Thread unstacking ThreadInterrupt handler
Case1: Single Pended Interrupt
Interrupt Request
Interrupt Pending
status
Processor mode
Interrupt Active
Status Bit
Processor
operation
Stacking &
Vector fetch
Exception return
Thread
Handler
Thread
1
2
3
4
5
6
7
8
9
10
11
Interrupt Request
Interrupt pending
Status
Interrupt Active
Status
Processor Mode
Thread
Handler Handler
Interrupt issued again
Interrupt pended again
Interrupt Active
Status bit goes high
ISR enter ISR exit
ISR re-enter
Interrupt Active
Status bit goes high
again
Interrupt issued
9
1
2
3
5
4
6
7
8
10
11
13
12
Case2: Double Pended Interrupt
Quiz 4
What happens when you masked out or disabled an interrupt from
particular peripheral, but the peripheral still issues an interrupt ?
Demonstrating Enable/Disable
Exceptions using PRIMASK and
BASEPRI registers
PRIMASK
BASEPRI
Available Priority levels for 4bit priority-level-
register
0x00, 0x10, 0x20, 0x30, 0x40
0x50, 0x60, 0x70, 0x80, 0x90
0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xf0
Bit
7
Bit
6
Bit
5
Bit
4
Bit
3
Bit
2
Bit
1
Bit
0
Implemented Not implemented
Priority Level Resister
implementation in STM32F4xx
You have learnt
• How to disable and enable interrupts/Exception using
PRIMASK and BASEPRI Registers
Congratulations 
Getting Started with
USB-Logic Analyzer
Hardware & Software
Software Download for Windows/MAC/Linux
https://www.saleae.com/downloads
8 Channels(Digital)
2 GND pins
Max 100 MS/s
(Million Samples/sec)
GPIO (LED)
GND
GND
Embedded Board
USB
Software
CH1
Demonstrating Interrupt Priority and
Pre-Emption
By Configuring Two Interrupts
In this program we will use Systick Timer exception and
Button interrupt to understand how pre-emption works
due to changing priorities.
SysTick Timer
 The arm cortex M3 and M4 processor has a 24-bit system timer called
sysTick.
 The counter inside the sysTick is 24 bit decrement counter. when you
start the counter by loading some value, it starts decrementing for
every processor clock cycle.
 If it reaches zero , then it will raise a sysTick timer exception and then
again reloads the value and continue.
 SysTick Timer can be used for time keeping, time measurement, or as
an interrupt source for tasks that need to be executed regularly.
First Case
Interrupt Priority Note
SysTick Timer
Button
0xF0
0x00
Low
High
Lets Code and Understand !!
Systick Timing Calculation
We are using 16Mhz internal RC oscillator
1 tick takes  1 processor cycle (1/16Mhz)
So, time for
1 tick  0.0000000625 seconds
2000 ticks  0.000125 seconds
SHP[0]SHP[1]SHP[2]SHP[3]
SHP[7]
SHP[A]SHP[B]
SHP[6] SHP[4]-
SHP[9] SHP[8]-
System
Control
Block
Second Case
Interrupt Priority Note
SysTick Timer
Button
0x00
0xF0
High
Low
Congratulations !! 
You Have Learnt
• Configuring interrupt priority
• Interrupt pre-emption in action
• Debugging interrupts by using GPIOs
• How to use USB logic analyzers
NVIC Registers
NVIC Registers for interrupt control
 Interrupt Enable Registers
 Pending State Registers
 Active State Registers
 Interrupt Mask And Unmask Registers
 Interrupt Priority Registers
NVIC Register Summary
Interrupt Enable/Disable Registers
To enable the interrupt , the bit corresponding to IRQ number must be
set in the NVIC_ISERn (n = 0 to 7) register
Address Name Type Reset
value
Description
0XE000E100 NVIC_ISER0 R/W 0 Enable the external interrupt #0 to #31
bit[0]=1 to enable interrupt #0, 0 has no effect
bit[1]=1 to enable interrupt #1, 0 has no effect
---
bit[31]=1 to enable interrupt #31, 0 has no effect
0XE000E104 NVIC_ISER1 R/W 0 Enable the external interrupt #32 to #63
bit[0]=1 to enable interrupt #32, 0 has no effect
bit[1]=1 to enable interrupt #33, 0 has no effect
---
bit[31]=1 to enable interrupt #63, 0 has no effect
Interrupt Enable/Disable Registers
To disable the interrupt ,corresponding bit in the NVIC_ICERn (n = 0
to 7) register must be set
Address Name Type Reset
value
Description
0XE000E180 NVIC_ICER0 R/W 0 Disable the external interrupt #0 to #31
bit[0]=1 to disable interrupt #0, write 0 has no effect
bit[1]=1 to disable interrupt #1, write 0 has no effect
---
bit[31]=1 to disable interrupt #31, write 0 has no effect
0XE000E184 NVIC_ICER1 R/W 0 Disable the external interrupt #32 to #63
bit[0]=1 to disable interrupt #32, write 0 has no effect
bit[1]=1 to disable interrupt #33, write 0 has no effect
---
bit[31]=1 to disable interrupt #63, write 0 has no effect
Pending State Registers
There are two sets of registers to manage pending of interrupts
1. ISPRn(n= 0 to 7)(Interrupt Set Pending Register)
• whenever any interrupt occurs , bit corresponding to its irq
number will be set in the "ISPR" register
• you can force the triggering of interrupt by setting a bit
corresponding to interrupt number
2. ICPRn(n = 0 to 7) (Interrupt Clear-Pending Register)
you can clear any pending interrupts by using this register
Interrupt Set Pending Register
Address Name Type Reset value Description
0XE000E200 NVIC_ISPR0 R/W 0 Pending for external interrupt #0 to #31
bit[0]=1 to pend interrupt #0, write 0 has no effect
bit[1]=1 to pend interrupt #1, write 0 has no effect
---
bit[31]=1 to pend interrupt #31, write 0 has no effect
Read value indicates the current status
0XE000E204 NVIC_ISPR1 R/W 0 Pending for external interrupt #32 to #63
bit[0]=1 to pend interrupt #32, write 0 has no effect
bit[1]=1 to pend interrupt #33, write 0 has no effect
---
bit[31]=1 to pend interrupt #63, write 0 has no effect
Read value indicates the current status
Interrupt Clear Pending Register
Address Name Type Reset
value
Description
0XE000E280 NVIC_ICPR0 R/W 0 Clear Pending for external interrupt #0 to #31
bit[0]=1 to Clear pend interrupt #0, write 0 has no effect
bit[1]=1 to Clear pend interrupt #1, write 0 has no effect
---
bit[31]=1 to Clear pend interrupt #31, write 0 has no effect
Read value indicates the current pending status
0XE000E284 NVIC_ICPR1 R/W 0 clear Pending for external interrupt #32 to #63
bit[0]=1 to clear pend interrupt #32, write 0 has no effect
bit[1]=1 to clear pend interrupt #33, write 0 has no effect
---
bit[31]=1 to clear pend interrupt #63, write 0 has no effect
Read value indicates the current pending status
Address Name Type Reset
value
Description
0XE000E300 NVIC_IABR0 R 0 Active status for external interrupt #0 to #31
bit[0] is 1 automatically when interrupt #0 is being serviced
bit[1] is 1 automatically when interrupt #1 is being serviced
---
bit[31] is 1 automatically when interrupt #31 is being serviced
Read value indicates the current active status of interrupts
0XE000E304 NVIC_IABR1 R 0 Active status for external interrupt #32 to #63
bit[0] is 1 automatically when interrupt #32 is being serviced
bit[1] is 1 automatically when interrupt #33 is being serviced
---
bit[31] is 1 automatically when interrupt #63 is being serviced
Read value indicates the current active status of interrupts
Interrupt Active Bit registers
Interrupt Priority Registers
Priority level field for
external interrupt #0
Priority level field for
external interrupt #236
0xE000E401 0xE000E4000xE000E4020xE000E403
0xE000E400
0xE000E400+(n*4)
0xE000E4EF
Exception Vector Table
Whenever there is a system exception or interrupts, how
does processor come to your ISR code to handle that
interrupt or system exception ?
What is Vector Table ?
 The vector table contains the initial value of the Main Stack
Pointer(MSP), and addresses of handlers for different system
exceptions and external interrupts
 When you reset the processor, processor expect the vector table to
be located in the code memory starting from address 0x00000000
Embedded System Programming on ARM Cortex M3 and M4 Course
Example Of a Vector Table Code From
STM32F4XX
Vector Table Relocating
Vector Table Offset Register(VTOR), address 0xE000ED08
Bit 6:0
Reserved
Bit 31:30 Bit 29 Bit 28:7
TBLOFF(Vector Table Base Offset)
ROM
User
FLASH
Vector table_2
0x00000000
Boot loader
Task
Vector table_1
0x00020000
Vector table
relocated
Memory
address
1
2
3
4 User application
Vector Able Reallocation Feature Case
Study
Exception Entry/Exit Sequence
Exception Entry Sequence
1. Pending bit set
2. Stacking and Vector fetch.
3. Entry into the handler and Active bit set
4. clears the pending status(processor does it automatically )
5. Now processor mode changed to handler mode.
6. Now handler code is executing .
7. The MSP will be used for any stack operations inside the handler.
Exception Exit sequence
 In Cortex-M3/M4 processors the exception return mechanism is
triggered using a special return address called EXC_RETURN.
 EXC_RETURN is generated during exception entry and is stored in
the LR.
 When EXC_RETURN is written to PC it triggers the exception return.
Thread mode Handler mode Thread mode
Process stackMain stackProcess stack
Processor
mode
stack
Thread
Main program
Handler Interrupt service routine
Stacking
using PSP
Interrupt
event
Exception return
PC = LR
Unstacking using
PSP
EXC_RETURN
LR=0XFFFFFFFD
Exception Entry/Exit Sequence
EXC_RETURN
When it is generated ??
During an exception handler entry , the value of the return address(PC)
Is not stored in the LR as it is done during calling of a normal C function.
Instead The exception mechanism stores the special value called
EXC_RETURN in LR.
EXC_RETURN Contd.
Bits Descriptions Values
31:28 EXC_RETURN indicator 0xF
27:5 Reserved(all 1) 0xEFFFFF
4 Stack frame type always 1 when floating point unit is
not available.
3 Return mode 1= return to thread mode
0 = return to handler mode
2 Return stack 1= return with PSP
0=return with MSP
1 Reserved 0
0 Reserved 1
Decoding EXC_RETURN value
Exception
return trigger
Un-stacking
using MSP
Un-stacking
using PSP
MSP selected
CONTROL[1]=0
PSP selected
CONTROL[1]=1
Resume
program
execution
EXC_RETURN[2]=0 EXC_RETURN[2]=1
EXC RETURN Contd.
EXC RETURN Possible Values
Cortex M3/M4 OS Features
KIRAN NAYAK| SECTION-12
Session Overview
At the end of the session you will be able to understand
 Use shadowed stack pointer in OS
 SVC Exception and its uses
 PendSV Exception and its uses
How CORTEX-M3/M4 Helps OS ?
Because of the OS friendly features like:
 Shadowed stack pointers
 SysTick Timer
 SVC and PendSV exceptions
Shadowed Stack Pointer
 Physically 2 stack pointers are there in cortex-M3/M4
 The SP(R13), Which is called Stack Pointer, points to the currently
selected stack pointer .
 Value of SPSEL bit in the CONTROL register determines which stack
is currently active and used.
How OS Can Benefit From Shadowed Stack
Pointers ?
USED STACK
USED STACK
USED STACK
USED STACK
Running os kernel
MSP
Kernel
stack
space
Stack
for
Task A
Stack
for
Task B
Stack
for
Task C
Running
Task A
Running
Task B
Running
Task C
PSP
PSP
PSP
SRAM
[un-privileged] [un-privileged] [un-privileged]
SVC System Exception
SVC Exception
 SVC stands for Supervisory Call
 This is triggered by SVC instruction
 The svc handler will execute right after the svc instruction(no delay !!
Unless a higher priority exception arrive at the same time )
Advantages of SVC Exception
Application
Hardware
Driver interface
Kernel
Privileged
resource
Hey, I want to
use hardware
Case of privileged system
resource access by the
application
Application
Hardware
Driver interface
Kernel
Privileged
resource
I wan to open
the hardware
Case of privileged system
resource access by the
application
Application
Hardware_V2
Driver_v2
Kernel_v2
Privileged
resource
Hey, you guys
changed your
versions
Case of Application Portability
Application
Hardware_V2
Driver_v2
Kernel_v2
Privileged
resource
Do I need to
change my code
to open the
hardware ?
Case of Application Portability
Method To Trigger SVC Exception
There are two ways
1) Direct execution of svc instruction with an immediate value
 Example : SVC 0x04 in assembly
 I will show you in the lab session how to issue through C program
 Use SVC instruction is very efficient in terms of latency
2) Setting the exception pending bit in “System Handler Control and State
Register”
 This method is not preferred and there is no reason why to use it
How To Extract The SVC Number
 When svc instruction is executed, the associated immediate
value(Service Number) will not be passed to SVC Exception Handler.
 The SVC handler need to extract the number by using the PC
value which was stored on to the stack , prior coming to the
exception handler.
TASK A
SVC handler
stacking
Thread Mode Handler Mode
(using MSP )(using MSP )
SVC
exception
Used stack
space
Last stacked item
MSP
Memory
address
xPSR
Return
address(PC)
LR
R12
R3
R2
R1
R0
Processor does this automatically
Stack
Frame
Next_ins_addr_after_svc = MSP[6];
SVC_number = *( (Next_ins_addr_after_svc ) – 2 )
How To Extract The SVC Number
+6
This gives the address,
Where svc instruction
is stored in the code memory
PendSV System Exception
PendSV Exception
 In OS designs, we need to switch between different tasks to support
multitasking .This is typically called context switching .
 Context switching is usually carried out in the PendSV exception
handler
 It is exception type 14 and has a programmable priority level.
 It is basically set to lowest priority possible
 This exception is triggered by setting its pending status by writing to
the “Interrupt Control and State Register”
Typical use of PendSV
 Typically this exception is triggered inside a higher priority
exception handler and it gets executed when the higher priority
handler finishes.
 Using this characteristic, we can schedule the PendSV exception
handler to be executed after all the other interrupt processing tasks
are done
 This is very useful for a context switching operation, which is a key
operation in various os design.
OS OS OS OS
Task A Task B Task A
SysTick Timer
Exception
Task
Context
switching
Context
switching
Context
switching
Time slot Time slot Time slot
Context Switching
OS code runs on each systick timer exception
And decides to schedule different task
Pendsv In Context Switching
 In typical OS design, the context switching operation is carried out
inside the PendSV exception handler.
 using PendSV in context switching will be more efficient in a interrupt
noisy environment.
 In a interrupt noisy environment we need to delay the context switching
until all IRQ are executed.
Pendsv In Context Switching
 To do this , the PendSV is programmed as the lowest priority exception.
 If the OS decides that the context switching is needed, it sets the
pending status of the PendSV , and carries out the context switching
within the PendSV exception.
os
1
2
3
5
6
7
8
9
10
[pends the
pendSV]
Context
switch in
Pendsv
handler
Task A Task B Task A
Time
Thread
PendSv
handler
Interrupt
SysTick
Timeout
Interrupt
occurred
ISR
Start end
ISR
os
Context
switch in
Pendsv
handler
4
Task B executes
OS pends
the pendSV
Systick
handler
Return to ISR
PendSV
handler
Scenario of PendSV in context switching
PendSV
handler
Timeout
Offloading Interrupt processing using
PendSV
If a higher priority handlers doing time consuming work, then the other
lower priority interrupts will suffer and systems responsiveness may
reduce .
Offloading Interrupt Processing Using
Pendsv
Typically interrupts are serviced in 2 halves.
1) The first half is the time critical part that needs to be executed as a part of
ISR.
2) The second half is called bottom half, is basically delayed execution where
rest of the time consuming work will be done .
So , PendSV can be used in these cases, to handle the second half execution
by triggering it in the first half.
Task A
ISR 0
PendSV
ISR 1
PendSV
Task A
IRQ #0
First half processing
(time critical)
Exit by
pending PendSV
PendSV
Handler
PendSV
Handler
Bottom half task
(time consuming
)
IRQ #1
Other ISR not delayed
Resumes bottom
half
Processing
Time
Scenario of using PendSV in offloading interrupt processing
1
2
3
5
6
7
8
9
10
[pends the
pendSV]
Context
switch in
Pendsv
handler
Task A Task B Task A
Time
Thread
SVC &
PendSv
handler
Interrupt
SysTick
Timeout
Interrupt
occurred
ISR
Start end
ISR
os
Context
switch in
Pendsv
handler
4
Task B executes
OS pends
the pendSV
SVC
handler
Return to ISR
PendSV
handler
Scenario of PendSV in context switching
PendSV
handler
My Udemy Online Courses on
Microcontroller and RTOS programming
We have several online video courses on ,
Microcontroller Programming,
RTOS Programming
Microcontroller programming with Driver Development
Embedded linux
Microcontroller DMA Programming
Mirocontroller Bootloader development
Please visit www.fastbitlab.com to enroll for the courses.
Or you can check my udemy profile here :
www.fastbitlab.com

More Related Content

PPTX
Part-2: Mastering microcontroller with embedded driver development
PPTX
STM32 Microcontroller Clocks and RCC block
PPTX
Mastering Microcontroller : TIMERS, PWM, CAN, RTC,LOW POWER
PPTX
Part-1 : Mastering microcontroller with embedded driver development
PPTX
PART-2 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PPTX
PART-1 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PPT
PDF
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
Part-2: Mastering microcontroller with embedded driver development
STM32 Microcontroller Clocks and RCC block
Mastering Microcontroller : TIMERS, PWM, CAN, RTC,LOW POWER
Part-1 : Mastering microcontroller with embedded driver development
PART-2 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PART-1 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging

What's hot (20)

PDF
ARM CORTEX M3 PPT
PDF
Unit II arm 7 Instruction Set
PPTX
SPI introduction(Serial Peripheral Interface)
PPTX
ARM Processor
PPT
Pcie drivers basics
PDF
UVM Methodology Tutorial
PDF
Introduction to arm architecture
PPTX
I2C Protocol
PDF
ARM Architecture
PPSX
LECT 1: ARM PROCESSORS
PPT
PDF
Unit 3 mpmc
DOCX
Flag register 8086 assignment
PPTX
Timer counter in arm7(lpc2148)
PDF
Arm cm3 architecture_and_programmer_model
PPTX
ARM stacks, subroutines, Cortex M3, LPC 214X
PPT
PIC Microcontrollers.ppt
DOCX
8096 microcontrollers notes
PDF
Amba bus
PPTX
Ambha axi
ARM CORTEX M3 PPT
Unit II arm 7 Instruction Set
SPI introduction(Serial Peripheral Interface)
ARM Processor
Pcie drivers basics
UVM Methodology Tutorial
Introduction to arm architecture
I2C Protocol
ARM Architecture
LECT 1: ARM PROCESSORS
Unit 3 mpmc
Flag register 8086 assignment
Timer counter in arm7(lpc2148)
Arm cm3 architecture_and_programmer_model
ARM stacks, subroutines, Cortex M3, LPC 214X
PIC Microcontrollers.ppt
8096 microcontrollers notes
Amba bus
Ambha axi
Ad

Similar to Embedded System Programming on ARM Cortex M3 and M4 Course (20)

PPTX
MPU Chp2.pptx
ODP
Programming ARM Cortex-M4 STM32 Nucleo
PDF
PPT
ARM - Advance RISC Machine
PDF
Arm architecture overview
PPTX
PPTX
ARM Architecture and Instruction set.pptx
PPTX
UNIT 2.pptx
PDF
AAME ARM Techcon2013 005v02 System Startup
PPTX
WINSEM2022-23_BECE204L_TH_VL2022230500861_2023-02-10_Reference-Material-I.pptx
PPT
ARM_2.ppt
PPTX
Arm cortex-m3 by-joe_bungo_arm
PDF
ARM Holings presentation for the worldd.pdf
PPTX
Lecture 1.pptx
PPT
Chapter01 (1).ppt
PDF
02 : ARM Cortex M4 Specs || IEEE SSCS AlexSC
PPT
AdvancedRiscMachineryss-INTRODUCTION.ppt
PPT
Arm teaching material
PPT
Arm teaching material
PPTX
Arm modes
MPU Chp2.pptx
Programming ARM Cortex-M4 STM32 Nucleo
ARM - Advance RISC Machine
Arm architecture overview
ARM Architecture and Instruction set.pptx
UNIT 2.pptx
AAME ARM Techcon2013 005v02 System Startup
WINSEM2022-23_BECE204L_TH_VL2022230500861_2023-02-10_Reference-Material-I.pptx
ARM_2.ppt
Arm cortex-m3 by-joe_bungo_arm
ARM Holings presentation for the worldd.pdf
Lecture 1.pptx
Chapter01 (1).ppt
02 : ARM Cortex M4 Specs || IEEE SSCS AlexSC
AdvancedRiscMachineryss-INTRODUCTION.ppt
Arm teaching material
Arm teaching material
Arm modes
Ad

Recently uploaded (20)

PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
cuic standard and advanced reporting.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Advanced IT Governance
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Chapter 2 Digital Image Fundamentals.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
KodekX | Application Modernization Development
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
cuic standard and advanced reporting.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Dropbox Q2 2025 Financial Results & Investor Presentation
Transforming Manufacturing operations through Intelligent Integrations
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Advanced IT Governance
CIFDAQ's Market Insight: SEC Turns Pro Crypto
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
GamePlan Trading System Review: Professional Trader's Honest Take
Advanced Soft Computing BINUS July 2025.pdf
Chapter 2 Digital Image Fundamentals.pdf
20250228 LYD VKU AI Blended-Learning.pptx
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Understanding_Digital_Forensics_Presentation.pptx
Review of recent advances in non-invasive hemoglobin estimation
KodekX | Application Modernization Development

Embedded System Programming on ARM Cortex M3 and M4 Course

  • 2. For full video course on Microcontroller and RTOS programming please visit : www.fastbitlab.com All courses are hosted on Udemy.com
  • 3. Click here to enroll FREE !!!! http://bit.ly/2P47IeX
  • 4. Click here to watch free preview and enroll http://bit.ly/2QaW0M9
  • 5. Click here to watch free preview and enroll http://bit.ly/2SA2uFO
  • 6. Click here to watch free preview and enroll http://bit.ly/2PAQRQh
  • 7. Click here to watch free preview and enroll http://bit.ly/2AEcneA
  • 8. Click here to watch free preview and enroll http://bit.ly/2zhqDrX
  • 9. Click here to watch free preview and enroll http://bit.ly/2OjgFf7
  • 11. Motivation to learn ARM-Cortex-M3/M4 • It’s really cheap • High speed • Less Power • More Code Density • Better Interrupt Management • Many more ….
  • 13. What will you learn at the end of the course ?? • Architecture • Programming model • Memory Architecture • Interrupt Management • Exception handling • Buttons, LEDs • Programming and Debugging using KEIL • Debugging using Logic analyzers • Lab Sessions with lots of Code implementations
  • 15. Cortex-M3/M4 in SW developers point of view What the firmware/Embedded Developers should be knowing ???  Programming model  How exceptions are handled  The memory map  Peripheral interfacing  How to use the S/W driver libraries from Microcontroller vendors
  • 17. How does Operational model of the processor look like ? Thread mode Handler Mode Privileged level Non-privileged level Operation Modes Access Levels Operational model
  • 18. Operational modes Thread mode Handler Mode  Processor executes the normal application code  Privileged level or un-privileged level  On reset processor enters to thread mode  Enters during System exceptions /interrupts  Code executing is privileged  Only way user can put the processor to handler mode is to raise an system exception or interrupt
  • 19. Access Levels  Full access to processor resources  Handler mode is always privileged  Thread mode is by default privileged , but can be changed to un-privileged Privileged level Non-privileged level  Restricted access .  You can make thread mode to run in this level .
  • 20. Switching between Privileged and Un- privileged Access Level
  • 21. Priv level Un-Priv level CONTROL[0] =0 Thread mode Thread mode Priv level Handler mode Reset Exception Exception handler CONTROL[0] =0 1. First Processor start with thread mode and privileged access level 2. Changing CONTROL[0]=1 move the processor into un-privileged mode 3. Its not possible to come back to priv level at this stage 4. Issue a processor exception 5. Processor executes exception handler in handler mode with privileged access level 6. making CONTROL[0]=0 makes return to privileged level 7. Otherwise processor return to un-privileged level
  • 22. Application of switching between Access levels: In Embedded Operating system Design The kernel of a RTOS or embedded OS can change the control register to make an application task to run in unprivileged mode when it gets scheduled to run. It helps to design more secured and robust applications
  • 24. R0 R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13(MSP) R14 R15 R13(PSP) General Purpose Registers Main stack Pointer Process stack Pointer Link Register(LR) Program Counter(PC) xPSR PRIMASK FAULTMASK BASEPRI CONTROL Special Purpose Registers Low registers High registers Register model Special Purpose Registers
  • 25.  Registers R0-R12 are general purpose registers  R0-R7 are called low registers because, many 16 bit instructions can only access low registers.  All 32 bit instructions and very few 16 bit instructions can access high registers i.e R8-R12  The initial values of R0 to R12 are undefined. R0 R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 Low registers High registers generalpurposeregisters General purpose Registers
  • 26. R13 stack pointer(SP)  Physical there are 2 stack pointer exist. The MSP and PSP.  MSP is the default stack pointer.  R13 just points to the value of the currently selected stack pointer .  You can even change the stack pointer to PSP, by writing to CONTROL register of the processor  When the processor is executing exception handler, it always uses the MSP . R13(MSP) R13(PSP) Main Stack Pointer Process Stack Pointer
  • 28. R15,Program Counter(PC)  Always points to next instruction to be executed  Always increments by 4 ( word size )  Points to word aligned address in the code memory . R15 Program Counter(PC)
  • 30. xPSR PRIMASK FAULTMASK BASEPRI CONTROL Processor status register Exception masking registers Processor Control Register Special Purpose Registers APSR EPSR IPSR 32bits
  • 31. Quiz -1 So , How do you access General Purpose/Special Purpose registers in a ‘C’ program when they are not memory mapped ??
  • 32. EPSR IPSR 32bits APSR XPSR Negative Flag Zero Flag Carry/Borrow Flag Overflow Flag Saturation Flag APSR
  • 33. EPSR IPSR 32bits XPSR APSR Indicates the interrupted position of a continuable instruction Thumb state bit EPSR
  • 34. IPSR 32bits XPSR APSR IPSR EPSR This is the number of the current exception 0 = Thread mode 1 = Reserved 2 = NMI 3 = HardFault . . 16 = IRQ0 n+15 = IRQ(n-1)
  • 35. PRIMASK FAULTMASK BASEPRI 7:1 031:8 3 bits to 8 bits 0 bit to 5 bits Reserved Exception masking registers
  • 36. CONTROL Register 0 = Privileged 1 = Unprivileged SPSEL nPRIV0 = MSP 1 = PSP
  • 37. CONTROL Register  After reset, the CONTORL register is 0.  This means , by default, Thread mode uses MSP as the stack pointer and code runs in the privileged access level.  Programs in privileged Thread mode can switch the stack pointer selection or switch to unprivileged access level by writing to CONTROL.  Once “nPriv” is set to 1, the program running in thread mode can no longer access the CONTROL register.
  • 39. CMSIS
  • 40. Why to use CMSIS ?  CMSIS is a Hardware Abstraction Layer for Cortex-M series microcontrollers.  It supports developers and vendors in creating reusable software components for ARM Cortex-M based systems.  If a library is CMSIS compliant, it's vendor-independent, and its easier to swap different families like Cortex M0 to Cortex M3.
  • 41. Why to use CMSIS ? Contd.  One of the attractive aspects of the ARM Cortex environment with CMSIS is the ability to change platforms without a whole bunch of sweat.  If you pick a platform that doesn't buy into the CMSIS structure, you may not be able to move around as conveniently.
  • 42. Why to use CMSIS ? Contd.  Simplifying software re-use and software portability . i.e you will quickly able to port code written for Cortex M3 to Cortex M4 when you want to migrate.  Reducing the learning curve for microcontroller developers  Reducing the time to market for new devices
  • 44. RESET 0x20008000 0x20001000 0x00000000 0x00000004 0x200010001 32 4 First instruction Next Instruction MSP=0x20008000 PC=0x20001000 Reset handler 1. After reset, PC is loaded with the address 0x00000000 2. Processor read the value from 0x00000000 location in to MSP 3. Then processor reads the address of the reset handler from the location 0x00000004 4. Then it jumps to reset handler and start executing the instructions 5. Then you can call your main() function from reset handler. Main() Memory Address
  • 45. Switching between Privileged and Un-Privileged Access level of Execution
  • 46. Points To Remember !!  Processor always starts with Privileged Access level  In privileged level you can access any Restricted Register  From Privileged Level you can go to Un-Privileged access level  In unprivileged access level you can not access Restricted Registers  From Un-Privileged access level you can not come back to Privileged access level  You can only change the access level to privileged ,from an exception/interrupt handler
  • 47. Lets Code and Understand !!
  • 49. Session Overview At the end of the session you will be able to understand  Memory system features  Memory map of the processor  Bus interfaces  Bus protocols  Aligned and un-aligned data transfer  Bit banding and its advantages  Code example
  • 51. Features  All the Cortex-M processors have 32 bit memory addressing, as a result there is 4GB of addressable memory space  The memory is one unified space which is shared by code space, data space and peripheral space.  Harvard bus architecture . It means concurrent instruction and data accesses using multiple bus interfaces.  Support both little endian and big endian memory systems.  Support for unaligned data transfers.  Bit addressable memory spaces(bit-banding)  MPU support
  • 52. CODE SRAM Peripherals External RAM External Device Private peri bus(internal) Private peri bus(external) Vendor specific Bit-Band Region Bit-Band Alias Bit-Band Region Bit-Band Alias NVIC External private peripheral bus ROM Table 0x60000000 0x1FFFFFFF 0x20000000 0x3FFFFFFF 0x40000000 0x5FFFFFFF 0x00000000 0x9FFFFFFF 0xA0000000 0xDFFFFFFF 0xE0000000 0xE003FFFF 0xE0040000 0xE00FFFFF 0xE0100000 0xFFFFFFFF 0xE000FFFF 0xE00FF000 0xE0042000 0xE000F000 0xE000E000 0x43000000 0x42000000 0x40100000 0x40000000 0x23FFFFFF 0x20100000 0x20000000 Memory map 0x22000000
  • 53. CODE Region Range : 0x00000000–0x1FFFFFFF CODE 0x1FFFFFFF 0x00000000 512 MB Vector table
  • 54. SRAM Region Range : 0x20000000–0x3FFFFFFF  The SRAM(Static-RAM) region is located in the next 512 MB of memory space after CODE region  It is primarily for connecting SRAM, mostly on chip SRAM.  The first 1 MB of the SRAM region is bit addressable.  You can also execute program code from this region Bit-Band Region Bit-Band Alias 0x20000000 0x20100000 0x21FFFFFF 0x22000000 0x23FFFFFF 0x3FFFFFFF SRAM
  • 56. External RAM 0x60000000 0x9FFFFFFF Address range : 0x60000000 to 0x9FFFFFFF  This region is intended for either on-chip or off-chip memory  you can execute code in this region. External RAM Region
  • 57. External Device Region Range : 0xA0000000 to 0xDFFFFFFF  This region is intended for external devices and/or shared memory  It is a non-executable region. External Device 0xA0000000 0xDFFFFFFF
  • 58. Bus Protocols & Bus Interfaces
  • 59. BUS Protocols  AHB Lite (Main System Bus )  APB(Peripheral bus )
  • 60. BUS Protocols Contd.  AHB Lite (Main System Bus )  In The cortex m3 and m4 processors , the AHB lite protocol used for the main bus interfaces  AHB lite stands for AMBA High Performance Bus which is derived form AMBA(Advanced Microcontroller Bus Architecture ) specification
  • 61. BUS Protocols Contd.  APB(Peripheral bus )  The APB is an AMBA-compliant bus optimized for minimum power and reduced interface complexity  The AHB-APB bridge is an AHB slave that provides an interface between the high- speed AHB domain and the low-power APB domain.
  • 62. Cortex M3/M4FLASH/ROM AHB-APB Bridge APB 0 UART SPI I2C I2S LCD GPIO TIM0 TIM1 PMU WDT RTC MISC EEPROM SRAM High Speed peripherals DMA USB Touch controller APB 1 I-Code(AHB) AHB AHB AHB AHB AHB AHB D-Code(AHB) Systembus(AHBLite) BUS interfaces External PPB (debug components ) PPB(APB)
  • 63. Aligned and Un-aligned data transfer
  • 64. Aligned Data Transfer Struct mydata { Unsigned long data1; Char data2 Unsigned long data3 Char array[3] Short data4; Unsigned long data5 }
  • 65. What Is Un-Aligned Data Transfer ? Struct __packed mydata { Unsigned long data1; Char data2 Unsigned long data3 Char array[3] Short Int data4; Unsigned long data5 }
  • 66. How Unaligned Data Access Can Result ?  Direct manipulation of pointers  Accessing data structure with __packed attributes that result in unaligned data  Inline Assembly code
  • 69. What is bit-banding ?  It is a capability to address a single bit of a memory address  This feature is optional
  • 70. 0x20000000 0x20100000 0x22000000 0x23FFFFFF 0x3FFFFFFF Bit-Band Memory Region Bit-Band Alias Region SRAM Peripheral memory regionBit band region Bit band alias region 0x40000000 0x40100000 0x42000000 0x43FFFFFF 1MB
  • 72. Example Lets assume we want to set the value of the 2nd bit in the address 0x20000000 Without bit-band Read 0x20000000 to register Mask and set bit 2 Write back to 0x20000000 Write 1 to 0x22000008 Read data from 0x20000000 Write to 0x20000000 from buffer with bit2 set With bit-band Three instructions single instruction Mapped to 2 bus transfers
  • 73. How this works ?? Alias Equivalent 0x20000000 bit[0] 0x20000000 bit[1] 0x20000000 bit[2] ….. 0x20000000 bit[31] 0x20000004 bit[0] 0x20000004 bit[1] 0x22000000 bit[0] 0x22000004 bit[0] 0x22000008 bit[0] ….. 0x2200007C bit[0] 0x22000080 bit[0] 0x22000084 bit[0] Bit-Band Region
  • 75. Quiz -2 What is the value of bit-band alias address to address the 6th bit of the address 0x20000004 ?? 0x20000004 bit[6] Alias address = ??
  • 76. 1. reading the whole register 2. Mask and check bit 3. Compare and branch 1. Read bit using bit- band alias address 2. Compare and branch. Instead of doing this Simply do this Advantages of bit band regions
  • 77. Instead of doing this Disable the interrupt Read the memory Mask and change the bit Write back Change the value of memory region using its alias address Enable the interrupt Simply do this
  • 79. Points To Remember !! • Bit band Operations can only be operated on 2 memory regions. This is called bit-band region  0x20000000 to 0x20100000 (1 MB)  0x40000000 to 0x40100000(1 MB) • To Set/Reset any bit field in the Bit band region , we have to use Bit band alias address in the Bit band alias region  0x22000000 to 0x23ffffff (31 MB)  0x42000000 to 0x43ffffff(31 MB)
  • 80. Memory Remapping Alias Equivalent 0x20000000 bit[0] 0x20000000 bit[1] 0x20000000 bit[2] ….. 0x20000000 bit[31] 0x20000004 bit[0] 0x20000004 bit[1] ….. 0x22000000 bit[0] 0x22000004 bit[0] 0x22000008 bit[0] ….. 0x2200007C bit[0] 0x22000080 bit[0] 0x22000084 bit[0] ….. Bit-Band Region
  • 81. In this Program we will change the value of BIT0, BIT1 and BIT2 of the data stored in the memory address 0x20000000 using bit-band operations
  • 82. Lets Code and Understand
  • 83. Congratulations !!  You Have Learnt • How to Carry-out Bit-Band Operations in C
  • 85. Stack memory  Stack is a kind of memory usage mechanism that allows a portion of memory , typically RAM to be used as Last In First Out(LIFO) data storage buffer.  ARM Processors have the PUSH instruction to store data in stack and the POP instruction to retrieve data from stack.  ALL stack operations are word aligned
  • 86. Why stack is used ?  Temporary storage of original data when a function being executed needs to use registers for data processing.  Passing of information to functions or subroutines  For storing local variables  To hold processor status and register values in the case of exception such as an interrupt
  • 87. Stack pointers Physically there are 2 stack pointers in the Cortex-M3/M4 processors. 1. Main stack Pointer(MSP):  This is the default stack pointer used after reset.  used for all exception handlers.  after power up, the processor hardware automatically initializes the MSP by reading the vector table. 2. Process Stack Pointer(PSP)  This is an alternate stack point that can only be used in Thread mode  The PSP is not initialized after power up and must be initialized by the software before being used.
  • 88. Stack pointers As mentioned Previously the selection between MSP and PSP can be controlled by the value of SPSEL bit in the CONTROL register.
  • 91. RESET Initialize the MSP from vector table MSP=0x20008000 Fetch the reset handler address from vector table Reset handler System init main() Initial Stack Init 0x20000000 0x20007FFF 32KB 0X20007C00 SRAM 0x20008000MSP stack
  • 93. SUBROUTINE AND STACK As per the Procedure Call Standard Of Arm Architecture. 1. when a function is called, as per below table, registers are used for parameter passing. 2. It’s the callee “function” responsibility to push the contents of R4-R11,R13,R14 if the function is going to change these registers(compiler takes care when coded in c ) Register Input Parameter Return value R0 First input parameter Function return value R1 Second input parameter Function return value if size is 64bit R2 Third input parameter R3 Fourth input parameter
  • 94. Stacking and Un-stacking during Exception
  • 95. TASK A Exception handler stacking Thread Mode Handler Mode (using MSP )(using MSP ) exception Used stack space Last stacked item MSP Memory address xPSR Return address(PC) LR R12 R3 R2 R1 R0 Processor does this automatically Stack Frame Stacking
  • 96. TASK A Un-stacking Handler Mode Thread Mode (using MSP ) (using MSP ) Exception handler Exit Used stack space Last stacked item MSP Memory address xPSR Return address(PC) LR R12 R3 R2 R1 R0 Stack Frame Un-stacking Exception handler TASK A stacking Thread Mode (using MSP ) exception Processor does this automatically
  • 97. Demonstration of Stack Operations Using Different Stack Pointers(MSP/PSP)
  • 98. Write a program to PUSH the contents of R0,R1,R2 Registers using MSP as a stack pointer, and then POP the contents back using PSP as a stack pointer
  • 99. Points to Remember !!  Processor always start in Privileged level and uses MSP as a stack pointer  If you wish, you can change the stack pointer to PSP by writing to CONTROL register  In C program , if you want to access MSP,PSP,CONTROL register then you have to use either CMSIS APIs or assembly programming.
  • 100. Lets Code and Understand
  • 102. What Are Exceptions ??  Exceptions Are events, which are generated asynchronously, either from external world or from internal system .  When generated processor changes normal program flow to provide the service for the generated exceptions “In a simple way, anything which disturbs the normal flow of execution of the processor is an exception “
  • 103. What Are Interrupts ?  Remember Interrupt is also an exception !!  We use a word “interrupt” for the exception from external world. E.g.: peripheral like timers, RTC,I2C,NMI, I/Os. External means “external to the processor core”  Courtex-M3/M4 Supports 240 interrupts
  • 104. Exception types  Exceptions are divided into 2 types • System exceptions(internal to the processor) • External exceptions(as already said, name for this is interrupt )  15 system exceptions, numbered from 1 to 15.  240 interrupts, numbered from 16 to 255  So for e.g. the 16th exception is an interrupt#0
  • 105. Exception number Exception type Priority Description 1 Reset -3(Highest) Reset. Pressing a reset button causes this 2 NMI -2 Non Maskable Interrupt. 3 Hard Fault -1 all fault conditions, if the corresponding fault handler is not enabled 4 Mem Manage Fault Programmable memory manager fault: happens when try to access illegal memory areas, MPU Violation 5 Bus Fault Programmable Bus Error. Prefetch abort or data abort on AHB bus 6 Usage Fault Programmable invalid usage of some features by the program 7-10 Reserved --- 11 svc Programmable supervisor call 12 Debug monitor Programmable debug monitor(breakpoints, watchpoints ) 13 Reserved -- 14 PendSV Programmable Pendable service call 15 SysTick Programmable System Tick Timer List Of System Exceptions
  • 106. List of Interrupts Exception Number interrupt number Exception Type Priority 16 0 external interrupt 0 programmable 17 1 external interrupt 1 programmable -- -- -- -- -- -- -- -- 255 240 external interrupt 240 programmable
  • 108. Nested Vectored Interrupt Controller (NVIC)
  • 109. “This is the HW block, which receives and manages exceptions from various sources and delivers to the processor core as per priority ! ”
  • 110. Lets Understand how System Exceptions and Interrupts are connected to the Processor
  • 111. N V I C Processor core 16 17 18 19 . . . . . . . 255 Interrupt #0 Interrupt#1 Interrupt #2 Interrupt #3 Interrupt #239 Various external peripherals like (Timers ADC Watchdog GPIO SPI I2C Etc) Exception Number NVIC lines Exception #16 is ext Interrupt #0 ARM cortex-M3/M4 System Exception
  • 112. Points to Remember  The cortex M processors have a number of programmable registers for managing the interrupts .  Most of these registers are inside NVIC block (and System Control Block ).  There are 2 ways to configure and mange the interrupt  Directly access the NVIC registers  User CMSIS core APIs  The NVIC registers can only be accessed in privileged access level.
  • 113. Points to remember Contd.  After reset , all interrupts are disabled and given a priority level value of 0. So before using any interrupts you need to .  Set up the priority level of the required interrupt (optional)  Enable the interrupt in the NVIC interrupt enable register.  When the interrupt triggers, the corresponding ISR will execute (you might need to clear the interrupt request from the peripheral with in the handler ).  The name of the ISR can be found inside the vector table inside the startup code. Which is also provided by the microcontroller vendor.
  • 115. Interrupt Priority  When an interrupt can be accepted by the processor and get its handler executed is dependent on the priority of the interrupt.  A higher priority interrupt can pre-empt a lower priority interrupt. This is also called nesting of interrupts  Some of the exceptions(reset , NMI ,and HardFault) have fixed priority levels. Their priority level are represented in negative numbers to indicate that they are of higher priority than other exceptions.  Remember , lower numbers for priority indicates higher priority
  • 116. Interrupt Priority Levels  Different microcontroller have different levels of priority, for example 8 levels of priority , 16 levels of priority ,etc.  The microcontroller vendors are free to choose how many levels of priority level they want.  There is a register called "Priority level register". which determines how many priority level actually supported in the microcontroller implementation
  • 117. Interrupt Priority Level Register Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Implemented Not implemented Microcontroller Vendor XXX Microcontroller Vendor YYY Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Implemented Not implemented 8 Levels of Priority level 16 Levels of Priority level Write has no effect Write has no effect 0x00,0x20,0x40,0x60, 0x80,0xA0,0xC0, 0xE0 0x00,ox10,0x20,0x30,0x40,0x50, 0x60,0x70,0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe 0,0xf0
  • 119. Priority Grouping Priority Group Pre-empt priority field sub-priority field 0(default) Bit[7:1] Bit[0] 1 Bit[7:2] Bit[1:0] 2 Bit[7:3] Bit[2:0] 3 Bit[7:4] Bit[3:0] 4 Bit[7:5] Bit[4:0] 5 Bit[7:6] Bit[5:0] 6 Bit[7:7] Bit[6:0] 7 None Bit[7:0]
  • 120. Priority Group Pre-empt priority field sub-priority field 0(default) Bit[7:1] Bit[0] 1 Bit[7:2] Bit[1:0] 2 Bit[7:3] Bit[2:0] 3 Bit[7:4] Bit[3:0] 4 Bit[7:5] Bit[4:0] 5 Bit[7:6] Bit[5:0] 6 Bit[7:7] Bit[6:0] 7 None Bit[7:0] Application Interrupt And Reset Control Register Priority Grouping
  • 121. Pre-Empt Priority : when the processor is running interrupt handler , and another interrupt appears, then the pre-empt priority values will be compared and exception with higher pre-empt priority(less in number) will be allowed to run. Sub Priority : this value is used only when two exceptions with same pre-empt priority level occur at the same time. In this case , the exception with higher sub-priority(less in number) will be handled first . Priority Grouping
  • 122. Priority Grouping Case Study Case 1 : when the Priority group = 0, As per the table , we have pre-empt priority width = 7bits (128 programmable interrupt levels ) But only 3 bits are implemented so ,8 programmable interrupt levels Sub-priority width = 1 ( 2 programmable sub priority level ) Bit 0 is not implemented so no sub priority level Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Pre-empt priority Pre-empt priority Sub priorit y Not implemented priority level register
  • 123. Case 2 : when the Priority_group = 5, pre-empt priority width = 2 bits ( 4 programmable levels ) Sub-priority width = 6 ( 64 programmable sub priority level) Since only 1 bit is implemented , only 2 programmable sub priority levels Priority Grouping Case Study Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Preempt priority Not implemented Not implemented Sub pri
  • 124. When the Priority group = 7, find out pre-empt priority width = ?? Sub-priority width = ?? And give a conclusion, How pre-emption works in the system ? QUIZ-3
  • 125. Configuring Interrupt Priority Function void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) Description Sets the priority grouping field using the required unlock sequence. The parameter PriorityGroup is assigned to the field SCB->AIRCR [10:8] PRIGROUP field. Parameter priority group value (0...7) Return none Function uint32_t NVIC_GetPriorityGrouping Description Reads the priority grouping field from the NVIC Interrupt Controller. Parameter none Return Priority grouping field (SCB->AIRCR [10:8] PRIGROUP field)
  • 126. Function uint32_t NVIC_EncodePriority (uint32_t PriorityGroup, uint32_t PreemptPriority, uint32_t SubPriority) Description Encodes the priority for an interrupt with the given priority group, preemptive priority value, and subpriority value.. Parameter PriorityGroup Used priority group. PreemptPriority Preemptive priority value (starting from 0). SubPriority Subpriority value (starting from 0). Return Encoded priority. Value can be used in the function NVIC_SetPriority(). Function void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority) Description Sets the priority of an interrupt. Parameter IRQn Interrupt number. priority Priority to set. Return none Configuring Interrupt Priority
  • 127. Case 2 : when the Priority_group = 7, As per the table , in this case pre-empt priority width = none Sub-priority width = 8 ( 256 programmable sub priority level ) Since only 3 bits are implemented , only 8 programmable sub priority levels Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 sub priority sub priority Not implemented Priority Grouping Case Study
  • 129. Interrupt Activation Function void NVIC_EnableIRQ(IRQn_Type IRQn) Description Enables a device-specific interrupt in the NVIC interrupt controller. parameter IRQn External interrupt number. Value cannot be negative. return none Function void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority) Description Sets the priority of an interrupt. parameter IRQn Interrupt number. priority Priority to set. return none
  • 130. Global Exception Enable/Disable 7:1 031:8 3 bits to 8 bits 0 bit to 5 bits Reserved CMSIS APIs to handle PRIMASK register void __enable_irq(); // Clear PRIMASK void __disable_irq(); // Set PRIMASK void __set_PRIMASK(uint32_t priMask); // Set PRIMASK to value uint32_t __get_PRIMASK(void); // Read the PRIMASK value FAULTMASK BASEPRI PRIMASK
  • 131. Global Exception Enable/Disable Reserved 7:1 031:8 CMSIS APIs to handle FAULTMASK register are void __set_FAULTMASK(uint32_t faultMask); uint32_t __get_FAULTMASK(void); FAULTMASK
  • 132. Global Exception Enable/Disable For example, if you want to block all exceptions with priority level equal to or lower than 0x60 , you can use this CMSIS function. __set_BASEPRI(0x60); // Disables interrupts with priority 0x60 to 0xFF You can also read back the value of BASEPRI: x = __get_BASEPRI(void); // Read value of BASEPRI BASEPRI Reserved 3 bits to 8 bits 0 bit to 5 bits 7:1 031:8
  • 134. Thread unstacking ThreadInterrupt handler Case1: Single Pended Interrupt Interrupt Request Interrupt Pending status Processor mode Interrupt Active Status Bit Processor operation Stacking & Vector fetch Exception return Thread Handler Thread 1 2 3 4 5 6 7 8 9 10 11
  • 135. Interrupt Request Interrupt pending Status Interrupt Active Status Processor Mode Thread Handler Handler Interrupt issued again Interrupt pended again Interrupt Active Status bit goes high ISR enter ISR exit ISR re-enter Interrupt Active Status bit goes high again Interrupt issued 9 1 2 3 5 4 6 7 8 10 11 13 12 Case2: Double Pended Interrupt
  • 136. Quiz 4 What happens when you masked out or disabled an interrupt from particular peripheral, but the peripheral still issues an interrupt ?
  • 137. Demonstrating Enable/Disable Exceptions using PRIMASK and BASEPRI registers
  • 139. Available Priority levels for 4bit priority-level- register 0x00, 0x10, 0x20, 0x30, 0x40 0x50, 0x60, 0x70, 0x80, 0x90 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xf0 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Implemented Not implemented Priority Level Resister implementation in STM32F4xx
  • 140. You have learnt • How to disable and enable interrupts/Exception using PRIMASK and BASEPRI Registers Congratulations 
  • 142. Hardware & Software Software Download for Windows/MAC/Linux https://www.saleae.com/downloads 8 Channels(Digital) 2 GND pins Max 100 MS/s (Million Samples/sec)
  • 144. Demonstrating Interrupt Priority and Pre-Emption By Configuring Two Interrupts
  • 145. In this program we will use Systick Timer exception and Button interrupt to understand how pre-emption works due to changing priorities.
  • 146. SysTick Timer  The arm cortex M3 and M4 processor has a 24-bit system timer called sysTick.  The counter inside the sysTick is 24 bit decrement counter. when you start the counter by loading some value, it starts decrementing for every processor clock cycle.  If it reaches zero , then it will raise a sysTick timer exception and then again reloads the value and continue.  SysTick Timer can be used for time keeping, time measurement, or as an interrupt source for tasks that need to be executed regularly.
  • 147. First Case Interrupt Priority Note SysTick Timer Button 0xF0 0x00 Low High
  • 148. Lets Code and Understand !!
  • 149. Systick Timing Calculation We are using 16Mhz internal RC oscillator 1 tick takes  1 processor cycle (1/16Mhz) So, time for 1 tick  0.0000000625 seconds 2000 ticks  0.000125 seconds
  • 151. Second Case Interrupt Priority Note SysTick Timer Button 0x00 0xF0 High Low
  • 152. Congratulations !!  You Have Learnt • Configuring interrupt priority • Interrupt pre-emption in action • Debugging interrupts by using GPIOs • How to use USB logic analyzers
  • 154. NVIC Registers for interrupt control  Interrupt Enable Registers  Pending State Registers  Active State Registers  Interrupt Mask And Unmask Registers  Interrupt Priority Registers
  • 156. Interrupt Enable/Disable Registers To enable the interrupt , the bit corresponding to IRQ number must be set in the NVIC_ISERn (n = 0 to 7) register Address Name Type Reset value Description 0XE000E100 NVIC_ISER0 R/W 0 Enable the external interrupt #0 to #31 bit[0]=1 to enable interrupt #0, 0 has no effect bit[1]=1 to enable interrupt #1, 0 has no effect --- bit[31]=1 to enable interrupt #31, 0 has no effect 0XE000E104 NVIC_ISER1 R/W 0 Enable the external interrupt #32 to #63 bit[0]=1 to enable interrupt #32, 0 has no effect bit[1]=1 to enable interrupt #33, 0 has no effect --- bit[31]=1 to enable interrupt #63, 0 has no effect
  • 157. Interrupt Enable/Disable Registers To disable the interrupt ,corresponding bit in the NVIC_ICERn (n = 0 to 7) register must be set Address Name Type Reset value Description 0XE000E180 NVIC_ICER0 R/W 0 Disable the external interrupt #0 to #31 bit[0]=1 to disable interrupt #0, write 0 has no effect bit[1]=1 to disable interrupt #1, write 0 has no effect --- bit[31]=1 to disable interrupt #31, write 0 has no effect 0XE000E184 NVIC_ICER1 R/W 0 Disable the external interrupt #32 to #63 bit[0]=1 to disable interrupt #32, write 0 has no effect bit[1]=1 to disable interrupt #33, write 0 has no effect --- bit[31]=1 to disable interrupt #63, write 0 has no effect
  • 158. Pending State Registers There are two sets of registers to manage pending of interrupts 1. ISPRn(n= 0 to 7)(Interrupt Set Pending Register) • whenever any interrupt occurs , bit corresponding to its irq number will be set in the "ISPR" register • you can force the triggering of interrupt by setting a bit corresponding to interrupt number 2. ICPRn(n = 0 to 7) (Interrupt Clear-Pending Register) you can clear any pending interrupts by using this register
  • 159. Interrupt Set Pending Register Address Name Type Reset value Description 0XE000E200 NVIC_ISPR0 R/W 0 Pending for external interrupt #0 to #31 bit[0]=1 to pend interrupt #0, write 0 has no effect bit[1]=1 to pend interrupt #1, write 0 has no effect --- bit[31]=1 to pend interrupt #31, write 0 has no effect Read value indicates the current status 0XE000E204 NVIC_ISPR1 R/W 0 Pending for external interrupt #32 to #63 bit[0]=1 to pend interrupt #32, write 0 has no effect bit[1]=1 to pend interrupt #33, write 0 has no effect --- bit[31]=1 to pend interrupt #63, write 0 has no effect Read value indicates the current status
  • 160. Interrupt Clear Pending Register Address Name Type Reset value Description 0XE000E280 NVIC_ICPR0 R/W 0 Clear Pending for external interrupt #0 to #31 bit[0]=1 to Clear pend interrupt #0, write 0 has no effect bit[1]=1 to Clear pend interrupt #1, write 0 has no effect --- bit[31]=1 to Clear pend interrupt #31, write 0 has no effect Read value indicates the current pending status 0XE000E284 NVIC_ICPR1 R/W 0 clear Pending for external interrupt #32 to #63 bit[0]=1 to clear pend interrupt #32, write 0 has no effect bit[1]=1 to clear pend interrupt #33, write 0 has no effect --- bit[31]=1 to clear pend interrupt #63, write 0 has no effect Read value indicates the current pending status
  • 161. Address Name Type Reset value Description 0XE000E300 NVIC_IABR0 R 0 Active status for external interrupt #0 to #31 bit[0] is 1 automatically when interrupt #0 is being serviced bit[1] is 1 automatically when interrupt #1 is being serviced --- bit[31] is 1 automatically when interrupt #31 is being serviced Read value indicates the current active status of interrupts 0XE000E304 NVIC_IABR1 R 0 Active status for external interrupt #32 to #63 bit[0] is 1 automatically when interrupt #32 is being serviced bit[1] is 1 automatically when interrupt #33 is being serviced --- bit[31] is 1 automatically when interrupt #63 is being serviced Read value indicates the current active status of interrupts Interrupt Active Bit registers
  • 162. Interrupt Priority Registers Priority level field for external interrupt #0 Priority level field for external interrupt #236 0xE000E401 0xE000E4000xE000E4020xE000E403 0xE000E400 0xE000E400+(n*4) 0xE000E4EF
  • 164. Whenever there is a system exception or interrupts, how does processor come to your ISR code to handle that interrupt or system exception ?
  • 165. What is Vector Table ?  The vector table contains the initial value of the Main Stack Pointer(MSP), and addresses of handlers for different system exceptions and external interrupts  When you reset the processor, processor expect the vector table to be located in the code memory starting from address 0x00000000
  • 167. Example Of a Vector Table Code From STM32F4XX
  • 168. Vector Table Relocating Vector Table Offset Register(VTOR), address 0xE000ED08 Bit 6:0 Reserved Bit 31:30 Bit 29 Bit 28:7 TBLOFF(Vector Table Base Offset)
  • 169. ROM User FLASH Vector table_2 0x00000000 Boot loader Task Vector table_1 0x00020000 Vector table relocated Memory address 1 2 3 4 User application Vector Able Reallocation Feature Case Study
  • 171. Exception Entry Sequence 1. Pending bit set 2. Stacking and Vector fetch. 3. Entry into the handler and Active bit set 4. clears the pending status(processor does it automatically ) 5. Now processor mode changed to handler mode. 6. Now handler code is executing . 7. The MSP will be used for any stack operations inside the handler.
  • 172. Exception Exit sequence  In Cortex-M3/M4 processors the exception return mechanism is triggered using a special return address called EXC_RETURN.  EXC_RETURN is generated during exception entry and is stored in the LR.  When EXC_RETURN is written to PC it triggers the exception return.
  • 173. Thread mode Handler mode Thread mode Process stackMain stackProcess stack Processor mode stack Thread Main program Handler Interrupt service routine Stacking using PSP Interrupt event Exception return PC = LR Unstacking using PSP EXC_RETURN LR=0XFFFFFFFD Exception Entry/Exit Sequence
  • 174. EXC_RETURN When it is generated ?? During an exception handler entry , the value of the return address(PC) Is not stored in the LR as it is done during calling of a normal C function. Instead The exception mechanism stores the special value called EXC_RETURN in LR.
  • 175. EXC_RETURN Contd. Bits Descriptions Values 31:28 EXC_RETURN indicator 0xF 27:5 Reserved(all 1) 0xEFFFFF 4 Stack frame type always 1 when floating point unit is not available. 3 Return mode 1= return to thread mode 0 = return to handler mode 2 Return stack 1= return with PSP 0=return with MSP 1 Reserved 0 0 Reserved 1 Decoding EXC_RETURN value
  • 176. Exception return trigger Un-stacking using MSP Un-stacking using PSP MSP selected CONTROL[1]=0 PSP selected CONTROL[1]=1 Resume program execution EXC_RETURN[2]=0 EXC_RETURN[2]=1 EXC RETURN Contd.
  • 178. Cortex M3/M4 OS Features KIRAN NAYAK| SECTION-12
  • 179. Session Overview At the end of the session you will be able to understand  Use shadowed stack pointer in OS  SVC Exception and its uses  PendSV Exception and its uses
  • 180. How CORTEX-M3/M4 Helps OS ? Because of the OS friendly features like:  Shadowed stack pointers  SysTick Timer  SVC and PendSV exceptions
  • 181. Shadowed Stack Pointer  Physically 2 stack pointers are there in cortex-M3/M4  The SP(R13), Which is called Stack Pointer, points to the currently selected stack pointer .  Value of SPSEL bit in the CONTROL register determines which stack is currently active and used.
  • 182. How OS Can Benefit From Shadowed Stack Pointers ?
  • 183. USED STACK USED STACK USED STACK USED STACK Running os kernel MSP Kernel stack space Stack for Task A Stack for Task B Stack for Task C Running Task A Running Task B Running Task C PSP PSP PSP SRAM [un-privileged] [un-privileged] [un-privileged]
  • 185. SVC Exception  SVC stands for Supervisory Call  This is triggered by SVC instruction  The svc handler will execute right after the svc instruction(no delay !! Unless a higher priority exception arrive at the same time )
  • 186. Advantages of SVC Exception
  • 187. Application Hardware Driver interface Kernel Privileged resource Hey, I want to use hardware Case of privileged system resource access by the application
  • 188. Application Hardware Driver interface Kernel Privileged resource I wan to open the hardware Case of privileged system resource access by the application
  • 190. Application Hardware_V2 Driver_v2 Kernel_v2 Privileged resource Do I need to change my code to open the hardware ? Case of Application Portability
  • 191. Method To Trigger SVC Exception There are two ways 1) Direct execution of svc instruction with an immediate value  Example : SVC 0x04 in assembly  I will show you in the lab session how to issue through C program  Use SVC instruction is very efficient in terms of latency 2) Setting the exception pending bit in “System Handler Control and State Register”  This method is not preferred and there is no reason why to use it
  • 192. How To Extract The SVC Number  When svc instruction is executed, the associated immediate value(Service Number) will not be passed to SVC Exception Handler.  The SVC handler need to extract the number by using the PC value which was stored on to the stack , prior coming to the exception handler.
  • 193. TASK A SVC handler stacking Thread Mode Handler Mode (using MSP )(using MSP ) SVC exception Used stack space Last stacked item MSP Memory address xPSR Return address(PC) LR R12 R3 R2 R1 R0 Processor does this automatically Stack Frame Next_ins_addr_after_svc = MSP[6]; SVC_number = *( (Next_ins_addr_after_svc ) – 2 ) How To Extract The SVC Number +6 This gives the address, Where svc instruction is stored in the code memory
  • 195. PendSV Exception  In OS designs, we need to switch between different tasks to support multitasking .This is typically called context switching .  Context switching is usually carried out in the PendSV exception handler  It is exception type 14 and has a programmable priority level.  It is basically set to lowest priority possible  This exception is triggered by setting its pending status by writing to the “Interrupt Control and State Register”
  • 196. Typical use of PendSV  Typically this exception is triggered inside a higher priority exception handler and it gets executed when the higher priority handler finishes.  Using this characteristic, we can schedule the PendSV exception handler to be executed after all the other interrupt processing tasks are done  This is very useful for a context switching operation, which is a key operation in various os design.
  • 197. OS OS OS OS Task A Task B Task A SysTick Timer Exception Task Context switching Context switching Context switching Time slot Time slot Time slot Context Switching OS code runs on each systick timer exception And decides to schedule different task
  • 198. Pendsv In Context Switching  In typical OS design, the context switching operation is carried out inside the PendSV exception handler.  using PendSV in context switching will be more efficient in a interrupt noisy environment.  In a interrupt noisy environment we need to delay the context switching until all IRQ are executed.
  • 199. Pendsv In Context Switching  To do this , the PendSV is programmed as the lowest priority exception.  If the OS decides that the context switching is needed, it sets the pending status of the PendSV , and carries out the context switching within the PendSV exception.
  • 200. os 1 2 3 5 6 7 8 9 10 [pends the pendSV] Context switch in Pendsv handler Task A Task B Task A Time Thread PendSv handler Interrupt SysTick Timeout Interrupt occurred ISR Start end ISR os Context switch in Pendsv handler 4 Task B executes OS pends the pendSV Systick handler Return to ISR PendSV handler Scenario of PendSV in context switching PendSV handler Timeout
  • 201. Offloading Interrupt processing using PendSV If a higher priority handlers doing time consuming work, then the other lower priority interrupts will suffer and systems responsiveness may reduce .
  • 202. Offloading Interrupt Processing Using Pendsv Typically interrupts are serviced in 2 halves. 1) The first half is the time critical part that needs to be executed as a part of ISR. 2) The second half is called bottom half, is basically delayed execution where rest of the time consuming work will be done . So , PendSV can be used in these cases, to handle the second half execution by triggering it in the first half.
  • 203. Task A ISR 0 PendSV ISR 1 PendSV Task A IRQ #0 First half processing (time critical) Exit by pending PendSV PendSV Handler PendSV Handler Bottom half task (time consuming ) IRQ #1 Other ISR not delayed Resumes bottom half Processing Time Scenario of using PendSV in offloading interrupt processing
  • 204. 1 2 3 5 6 7 8 9 10 [pends the pendSV] Context switch in Pendsv handler Task A Task B Task A Time Thread SVC & PendSv handler Interrupt SysTick Timeout Interrupt occurred ISR Start end ISR os Context switch in Pendsv handler 4 Task B executes OS pends the pendSV SVC handler Return to ISR PendSV handler Scenario of PendSV in context switching PendSV handler
  • 205. My Udemy Online Courses on Microcontroller and RTOS programming We have several online video courses on , Microcontroller Programming, RTOS Programming Microcontroller programming with Driver Development Embedded linux Microcontroller DMA Programming Mirocontroller Bootloader development Please visit www.fastbitlab.com to enroll for the courses. Or you can check my udemy profile here :

Editor's Notes

  • #14: Programming and Debugging using Keil Debugging using Logic analyzers And many ….
  • #22: First Processor start with thread mode and privileged access level Changing CONTROL[0]=1 move the processor into un-privileged mode Its not possible to come back to priv level at this stage Issue a processor exception Processor executes exception handler in handler mode with privileged access level making CONTROL[0]=0 makes return to privileged level Otherwise processor return to un-privileged level
  • #28: This is used to hold the return address when calling a function or subroutine. When a function call or subroutine call is made the value of LR Is updated automatically with the address of the instruction where it needs to return . LR also used to trigger a return from exception handling
  • #33: The APSR contains the Status flags of integer operations (N-Z-C-V bits) resulted from previous instruction exaction
  • #35: The IPSR contains the exception type number of the currently executing handler
  • #36: used to mask/unmask exceptions globally They can only be accessed in the privileged access
  • #37: The selection of stack pointer (MSP/PSP) nPRIV bit indicates in which level the processor currently executing. The CONTROL register can only be accessed in the privileged access level
  • #40: CMSIS
  • #54: A 512 MB memory space primarily for program code, including the default vector table
  • #56: The peripheral memory region also has the size of 512MB, used mostly for on-chip peripherals. Similar to SRAM region, the first 1MB of the peripheral region is bit addressable if the optional bit-band feature is included. You can not execute instruction code from here
  • #65:  Aligned Transfer, means that the data's address value is a multiple of the data's type-size (in bytes). WORD size transfers are carried out to addresses that are a multiple of 4. i.e. 0x00000000, 0x00000004, 0x00000008, … Half Word size transfers are carried out to addresses that are a multiple of 2: 0x00000000, 0x00000002, 0x00000004, … SRAM Space Wasted
  • #66: When an unaligned transfer is issued by the processor they are actually converted in to multiple aligned transfer by the processor bus interface unit. Since it is broken into several separate aligned transfers and as a result it takes more clock cycles for a single data access and might not be good in situations in which high performance is required . Effective utilization of SRAM space
  • #68: Instruction will be fetched in a word boundary since all Thumb instructions are half-word aligned in memory, two Thumb instructions are fetched at a time. It it’s a 32 bit instruction then, single instruction will be fetched at a time.
  • #72: Bit band Operations can only be operated on 2 memory regions. This is called bit-band region 0x20000000 to 0x20100000 (1 MB) 0x40000000 to 0x40100000(1 MB) To Set/Reset any bit field in the Bit band region , we have to use Bit band alias address in the Bit band alias region 0x22000000 to 0x23ffffff (31 MB) 0x42000000 to 0x43ffffff(31 MB)
  • #73: 0x22000008 is called alias address for bit2 of the address 0x20000000
  • #74: Each individual bit of the bit-band region can be accessed separately by using the least significant bit of each word-aligned bit band alias address Memory rampaging is performed during bus transfer No special instruction being used.
  • #77: This can be used to simplify branch decisions. For example if a branch should be carried based on one single bit in a status register in a peripheral
  • #78: Avoid race condition : When you want to change the value of a shared memory or register , using bit band operation is a good choice !!! Its atomic : when you are accessing the bit band region , by means of its bit band alias address , no interrupt can disturb it until it finishes.
  • #91: The processor uses a full descending stack model When the processor pushes a new item onto the stack, it first decrements the stack pointer and then writes the item to the new memory location. This means the stack pointer holds the address of the last stacked item in memory
  • #107: External interrupts : External means, external to the processor core. When we integrate the processor in micro controller , external interrupts are usually generated by the peripherals , like timers, GPIOs, serial protocols like SPI,I2C, etc.
  • #120: The priority level register is divided into 2 parts pre-empt priority field (the upper half) Sub-priority field (the bottom half)
  • #121: 8 bits which are shared among those fields is decided by the field “priority group” in the "application interrupt and Reset Control register"
  • #123: so, when priority group is 0 (default) then we have 8 levels of pre-empt priority and no sub priority
  • #124: That means for every pre empt level there are two levels of sub priority
  • #126: CMSIS core Provides Various API's to Configure interrupt Priorities. Remember it is highly recommended to use CMSIS api's instead of hardcoding Registers directly, it makes your code Portable
  • #130: You can enable any interrupt in 2 easy steps Set up the priority level of the required interrupt (optional) II. Enable the interrupt in the NVIC.
  • #131: The PRIMASK register is used to disable all exceptions except NMI and hard fault
  • #132: FAULTMASK is just like PRIMASK except that, it blocks even Hard-Fault exception (RESET, NMI cant be blocked)
  • #133: In some cases, you might want to disable interrupts only with priority lower than a certain level. In this case, you could use the BASEPRI register.
  • #135: Interrupt Request Caused pending bit to be Set
  • #148: When the SysTick timer Exception handler is running, if you issue button interrupt it will pre-empt the sysTick handler SysTick Handler will run when the Button ISR has finished If the button ISR is running, when the SysTick Exception occurs, then it cant pre-empt the Button ISR.
  • #150: SysTick Exception will be raised for every 125 micro seconds
  • #152: When the SysTick timer exception handler is running, if you issue button interrupt it wont preempt the first one Button ISR will run when the Systick handler has finished If the button ISR is running, when the SysTick Exception occurs, then Button ISR Will be Pre-empted.
  • #162: The NVIC_IABR0 to NVIC_IABR7 registers indicate which interrupts are active at given point in time. Each external interrupt has an active status bit. When the processor starts the interrupt handler, the bit is set to 1 automatically and cleared automatically when the interrupt return is executed These registers are Read Only.
  • #163: The NVIC_IPR0 to NVIC_IPR59 registers provide an 8-bit priority field for each interrupt and each register support setting priority for 4 interrupts These registers are byte-accessible.
  • #165: “welcome to the world of vector table “
  • #167: On system reset, the vector table is fixed at address 0x00000000 at the code memory Privileged code can write to the VTOR to relocate the vector table start address to a different memory location, in the range 0x00000080 to 0x3FFFFF80
  • #169: The vector table relocation feature provides a programmable register called the Vector Table Offset Register(VTOR). This register defines the starting address of the memory being used as the vector table. The VTOR reset value zero.
  • #170: After the reset always processor excepts vector table to be at 0x00000000 The boot code in the ROM code ,executes and does required setup Then the boot loader relocate the vector table to new vector table address in user flash and calls the reset handler of the new vector table Application in user flash now uses new vector table
  • #186: As the
  • #189: With a svc instruction application can ask the kernel to get services Different services may have different number which application pass with a SVC instruction
  • #191: SVC makes application tasks to be developed independently of the OS Application tasks do not need to know the programming details of the underlying hardware . The application tasks only need to know the SVC service number and the parameters that the os services requires.
  • #194: SVC_number = (uint8_t) *( (Next_ins_addr_after_svc ) – 2 ) +6 This gives the address Where svc insturction is stored in the code memory
  • #199: The PendSV exception solves the problem by delaying the context switching request until all the IRQ handlers have completed their processing