Lecture 04 ARM Cortex-M4 Processor
Lecture 04 ARM Cortex-M4 Processor
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 1 / 40
Outline
1 Learning Outcome
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 2 / 40
Learning Outcome
Learning Outcome
Understand the Cortex-M4 Processor Major Components
NVIC, SysTick, FPU, Special Registers, and so on
Real-time clock
Understand the use of Cortex-M4 lower level programming:
System call, Task and Memory management
thread, Synchronization tools
Exception handling
Understanding ARM Memory map and address space
Flash memory, SRAM
Peripheral address space
Kernel stack frame and process/task stack frame
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 3 / 40
A look at Cortex-M4 Processor
Cortex-M4 Core
Nestetd Interrupt Vector (NVIC)
Memory Protection Unit (MPU)
Optional Floating Point Unit (FPU)
Flash Patch Break Point (FPBP)
Data Watch Point and Trace (DWT)
AHB Access Port (AHB-AP)
Bus Matrix
Instrumentation Trace Microcell (ITM)
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 4 / 40
Cortex-M4 Processor – FPBP
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 5 / 40
Cortex-M4 Processor Core
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 6 / 40
Instrumentation Trace Microcell (ITM)
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 7 / 40
Cortex-M4 Execution Mode
Processor mode and privilege levels for software execution
Thread mode
Used to execute application software
Enters after reset
Control Register controls: execute in privileged or unprivileged
Handler mode
Used to handle exceptions, execution always in privileged mode
returns thread mode after finishing exception procession
Privileged Level
Unprivileged
limited access: MSR and MRS instructions
no access to CPS instruction, System Timer, NVIC, SCB
restricted Access to memory or peripherals
Must use SVC call to transfer control to privileged
Privileged
Can access all instructions and resources
Can modify CONTROL register to change the privileged level
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 8 / 40
Cortex-M4 Core Registers
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 9 / 40
Cortex-M4 Core Registers
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 10 / 40
Cortex-M4 Core Registers
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 11 / 40
Program Status Register
Figure 3: xPSR
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 12 / 40
Cortex-M4 Core Registers
PRIMASK
Prevent activation of all Exception with configurable priority
Bit[0]: ‘0’: not effect; ‘1’ masking all priority configurable exception
FAULTMASK
Prevent activation of all Exception except NMI
Bit[0]:‘0’ no effect; ‘1’: prevent from activation except NMI
BASEPRI
µp does not process exception priority value greater than BASEPRI
Bits[7:4]: ‘0x00’ – no effect; NonZero: the lower priority value
CONTROL Register – controls
Stack use (MSP, or PSP)
Privilege level
Bit[2] FPCA: floating point context currently active. ‘0’: No
floating-point context active
Bit[1]: SPSEL: stack pointer section. ‘0’: MSP, ‘1’: PSP
nPRIV – Thread mode privilege level. ‘0’: privilege mode; ‘1’:
unprivileged mode
these
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 13 / 40
Contex-M4 Stack
Cortex-M4 Stack
Full descending stack, insert decrements the address
Two stack
Main Stack
Process Stack
Control Register bit[1] determines current stack usage
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 14 / 40
Cortex-M4 Memory Model
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 15 / 40
Memory Protection Unit
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 16 / 40
Odering Memory Access
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 17 / 40
Behavior of Memory access
For more, see page 30 Cortex-M4 programming manual
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 18 / 40
Cortex-M4 Core Peripheral Units: SysTick timer
Textbook Page: 312
Cortex-M4 Small integrated timer – System Tick
A part of the NVIC
Generate SysTick Exception
We need it for context-switch to execute tasks in the different time
schedule
unprivileged application cannot disable the timer
Inside the processor – for portability
SysTick Registers
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 19 / 40
SysTick – System Clock
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 20 / 40
SysTick – System Clock
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 21 / 40
SysTick – System Clock
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 22 / 40
SysTick Timer
How to use
Disable the timer before the configuration; it is optional (disable if
configured before)
SysTick CTRL register set to ‘0’
Set the reload value to SysTick Load Register, must one less the
targeted value
Set current val to ‘0’, VAL register
Enable SysTick to use processor or external clock
Finally, Enable SysTick Timer
Use bit-0 to enable and disable the counter anytime
Tick Counter
Enable interrupt to send signal/interrupt to process or current
process
A counter or variable update to count the tick or time.
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 23 / 40
Exception and Interrupt
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 24 / 40
Exception Types
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 25 / 40
Interrupt Management
A set of Registers for Interrupt Management in Cortex-M
processor
Registers are inside NVIC and SCB
Physically SCB is implemented as a part of NVIC
CMSIS-core defines these registers in separate data structure
Special register in processor core such as PRIMASK,
FAULTMASK, and BASEPRI
NVIC and SCB are located inside SCS (System Control Space)
starting from 0xE000E000 (4KB)
SCS contains SysTick, MPU, Debug registers, and so on.
privileged mode can access these registers.
However, Software Trigger Interrupt Register (STIR) can be set
up to access from an unprivileged mode
Reset disable all interrupts with priority-level ‘0’
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 26 / 40
Interrupt Management – enabling and using interrupt(s)
Before using interrupt, you need to
Set up the priority of the targeted interrupt
void NVIC SetPriority (IRQn Type IRQn,uint32 t priority)
Enable the interrupt generation control in the peripheral that
triggers the interrupt
Enable IRQ bit of a peripheral such as U SART 2− > CR1|= 1 << 7
Enable the interrupt in the NVIC
void NVIC EnableIRQ (IRQn Type IRQn)
When interrupt triggers corresponding ISR executes
USART2 Handler()
You may need to clear the interrupt in the service routine
Startup code contains ISR in the vector table
(uint32 t) &USART2 Handler
If not, detail the ISR (weak!! definition) – before using it.
void USART2 Handler(void) attribute ((weak,
alias(“Default Handler”)));
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 27 / 40
Interrupt Priority Management
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 28 / 40
Interrupt Registers ARMv7
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 29 / 40
Interrupt Priority Management
Interrupt Priority and Setting
Cortex-M4 has 1-byte (8-bits) for priority of the interrupt
Stm32F4xx implements 4-MSB of the 8-bits (LSB-3:0 is always ‘0’)
In reality we do not need more priority (256!)
Therefore, 16-priority level 0x00, 0x10, 0x20, · · · 0xF0
Each 32-bit register presents 4-Interrupt priority; thus
PRI0-PRI59, total 60 registers
Address Range 0xE000E400-0xE000E4EF: total 240 bytes
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 30 / 40
Interrupt: Grouping, Preemption and Sub-Priority
Priority Group, Pre-empt and Sub-priority
The priority bits are divided into two halve
Preempt priority and sub-priority
Above 4-bit priority: such 2-bit for preempt priority and lower 2-bit
sub-priority
the upper half also known as priority grouping level
Register AIRCR in SCB is used to determine the number of bits
for priority prouping
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 31 / 40
Interrupt Enable Register
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 32 / 40
Interrupt Clear Enable Register
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 33 / 40
Interrupt Pending and Clear Pending
Interrupt Pending and Clear Pending
Another interrupt arrives while executing a higher priority
interrupt
The interrupt is pending, and the corresponding bit of Interrupt
Set Pending Register is ‘1’
Clear the interrupt pending bit when a waiting interrupt is active.
completion of an interrupt ISR should clear the active bit
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 34 / 40
Interrupt Active Register
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 35 / 40
Interrupt Life Cycle
Exception/interrupt State
Inactive: The exception is not active or inactive. If a higher priority interrupt
preempts a lower priority active interrupt
Pending: When an interrupt waiting to finish a higher priority or the same group
interrupts in an active state
Active: Currently serving by the processor
Active and Pending: The MCU serves the exception by the processor, and there is a
pending exception from the same source
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 37 / 40
Arm Cortex-M Interrupt and Masking
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 38 / 40
ARM Cortex-M Interrupt and Masking
Disable or Masking Interrupts
FAULTMASK – same as PRIMASK however it can mask all
interrupts other than NMI
Related functions (you will write) are:
void enable fault irq(void); // Clear FAULTMASK void
disable fault irq(void); // Set FAULTMASK to disable interrupts
void set FAULTMASK(uint32 t faultMask);
uint32 t get FAULTMASK(void);
BASEPRI – 32-bit register, however, only 8-bit is currently
available
The register mask interrupts based on priority,
Writing ‘0’ cancel the masking
use to disable interrupt lower than a certain interrupt level
Writing 0x20 disable interrupts with priority value 0x20 and higher
(lower priority)
Related Functions: set BASEPRI(uint32 t value); // Disable
interrupts with priority // ≥ value
set BASEPRI(0x0); // Turn off BASEPRI masking
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 39 / 40
Interrupt Program Status Register (IPSR)
Dr. MHK Tushar (CSE, DU) CSE3201 Operating Systems August 16, 23, 2022 40 / 40