Professional Documents
Culture Documents
All these issues could lead to failure in the programs running on the
processors.
Contd.
• Cortex-M processors have a efficient fault exception
mechanism. If a fault is detected, a fault exception is
triggered and one of the fault exception handler is executed.
• Hard faults have a fixed priority of -1, meaning they have higher
priority than any exception with configurable priority.
Registers of Programmable Model
PRIMASK, FAULTMASK, and
BASEPRI registers
• Used for exception or interrupt masking.
• These special registers are also used to mask
exceptions based on priority levels.
• This value is 32-bit, and the upper 27 bits are set to 1. Some of
the lower 4 bits are used to hold status information about the
exception sequence.(e.g., which stack was used for stacking)
Contd
• The Cortex-M3 and Cortex-M4 processors have multiple
bus interfaces.
• In parallel to the stacking operation, the processor can
also start the vector fetch (typically through the I-CODE
bus), and then start the instruction fetch.
• As a result, the Harvard bus architecture allows the
interrupt latency to be reduced because the stacking
operation and accesses of vector fetch and subsequently
instruction fetches can take place in parallel.
Contd.
Contd.
• Note that the exact order of stack accesses during
stacking is NOT the same order as the registers in the
stack frame.
• The Cortex-M3 processor stacks the PC and xPSR first,
before other registers in the register banks so that the PC
can be updated sooner with the vector fetch.
Exception Exit
• The exception return mechanism is triggered using a
special return address called EXC_RETURN. When
this value is written to the PC with one of the allowed
exception return instructions (BX LR), it triggers the
exception return sequence.
• When the exception return mechanism is triggered, the
processor accesses the previously stacked register
values in the stack memory during exception entrance
and restores them back to the register bank. This is
called unstacking.
Contd.
• To reduce the time required for the unstacking operation,
the return address (PC) value is accessed first, so that
instruction fetch can start in parallel with the rest of the
unstacking operation.
10.1 OS Supported Features
• The Cortex-M processors are designed with OS support in mind.
Currently there are over 30 different embedded OSs (including
many RealTime OS, or RTOS) available for Cortex-M
microcontrollers. A number of features are implemented in the
architecture to make OS implementation easier and more
efficient.
• SVC and PendSV exceptions: These two exception types are
essential for the operations of embedded OSs such as the
implementation of context switching.
• Shadowed stack pointer: Two stack pointers are available. In
systems with an embedded OS or RTOS, the exception handlers
use the MSP, while the application tasks use the PSP.
Contd.
• In systems with high-reliability requirements, the
application tasks can be running in unprivileged access
level, and some of the hardware resources can be set up
to be accessed in privileged level only.
• int __svc(0) add (int i1, int i2); declare as SVC function
• LDR R0,=PSP_TOP ;
MSR PSP, R0 ; Set PSP to the top of a process stack
MRS R0, CONTROL ; Read current CONTROL
ORRS R0, R0, #0x2 ; Set SPSEL
MSR CONTROL, R0 ; write to CONTROL
OS with Context Switching
• In embedded OS, the processing time is divided into a
number of time slots. For a system with only two tasks, the
two tasks are executed alternatively.
• The execution of an OS kernel can be triggered by:
• Execution of SVC instruction from application tasks. For
example, when an application task is stalled because it is
waiting for some data or event, it can call a system service
to swap in to another task.
• Periodic SysTick exception.
Contd.
• Inside the OS kernel code, the task scheduler can decide if
context switching should be carried out. The task scheduler is
triggered by a SysTick exception, and each time it decides to
switch to a different task.
• The SVC has priority zero (highest) while the ADC has
priority 1, and the Pend_SVC interrupt has priority 2 (lowest).
• Once SVC exits, the program enters into the next pending
interrupt.
• SysTick_Config(SystemCore_Clock / 100);
• NVIC_EnableIRQ (ADC1_2_IRQn);
• NVIC_SetPriorityGrouping (5);
• NVIC_SetPriority (SysTick_IRQn,4);
• NVIC_SetPriority (ADC1_2_IRQn,4);
Equivalent non-CMSIS code.