SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: What are the two operational modes in ARM Cortex M3?
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm Cortex M3

What are the two operational modes in ARM Cortex M3?

Holly Lindsey
Last updated: October 5, 2023 10:08 am
Holly Lindsey 7 Min Read
Share
SHARE

The ARM Cortex-M3 processor has two main operational modes – Thread Mode and Handler Mode. Understanding these two modes is key to utilizing the Cortex-M3 processor effectively in embedded applications.

Contents
Thread ModeFeatures of Thread ModeHandler ModeFeatures of Handler ModeThread vs Handler ModeMode Switching SequenceUse CasesThread Mode Priority LevelsConclusion

Thread Mode

Thread Mode is the default operational mode in Cortex-M3. All application software code executes in Thread Mode. This includes the main() function and any other application code. When the processor resets, it always starts in Thread Mode.

In Thread Mode, the processor can access both main stack and process stack. The Main Stack is used for exception handling and contains stack frames for each exception handler. The Process Stack contains stack frames for application code and PSP (process stack pointer) points to the current top of this stack.

Application data is accessed using standard operational registers like R0-R12. Thread Mode has access to full CPU registers without any privilege restrictions. It can freely manipulate the MSP, PSP, and other CPU registers.

By default, Thread Mode does not have access to fault handling registers or privileged CPU operations like changing execution priority, interrupt handling, etc. An attempt to access any privileged resource results in a fault. This provides protection and isolates application code from critical system code.

Features of Thread Mode

  • Default operational mode on reset
  • Application software executes in Thread Mode
  • Can access Main Stack and Process Stack
  • Uses PSP register to access Process Stack
  • Full access to general purpose registers like R0-R12
  • No access to privileged registers or resources
  • Attempting to access protected resource causes exception/fault

Handler Mode

Handler Mode is used to service exceptions and interrupts. All exception handlers execute in Handler Mode. When an exception occurs, the processor switches from Thread Mode to Handler Mode to run the exception handler routine.

In Handler Mode, the processor uses the Main Stack. The MSP (main stack pointer) register is used to access this stack. Handler Mode has access to all CPU registers without any limitations. It can freely manipulate the stack pointers, control registers, and other privileged resources.

Handler Mode code executes at a higher privilege level compared to Thread Mode. This allows exception handlers to implement fault handling, interrupt processing, task switching, and other critical operations safely and securely.

After completing exception processing, the handler code returns back to Thread Mode using the special EXC_RETURN opcode. This performs the required context switching and restores the application state to seamlessly resume normal operation.

Features of Handler Mode

  • Used to service exceptions and interrupts
  • Exception handlers execute in Handler Mode
  • Uses Main Stack accessed via MSP register
  • Full access to all CPU registers and resources
  • Executes at a higher privilege level than Thread Mode
  • Allows implementing fault handlers, task switching, etc.
  • Returns to Thread Mode using EXC_RETURN opcode

Thread vs Handler Mode

Here is a comparison between the two modes:

FeatureThread ModeHandler Mode
EntryOn resetOn exception entry
ExitOn exceptionUsing EXC_RETURN
StackProcess and MainOnly Main
Stack PointerPSPMSP
Privilege LevelUnprivilegedPrivileged
RegistersR0-R12Full access
ResourcesOnly unprotectedAll

In summary:

  • Thread Mode is used to run normal application code
  • Handler Mode is used to run privileged exception handlers
  • Thread Mode provides isolation while Handler Mode provides full access
  • Mode transition occurs on exception entry/return
  • Understanding the modes is key to Cortex-M3 usage

Mode Switching Sequence

When an exception occurs, the processor automatically switches from Thread Mode to Handler Mode by performing the following steps:

  1. Saves EXC_RETURN code on Main Stack
  2. Saves current stack pointer (PSP) on Main Stack
  3. Changes to Handler Mode
  4. Switches to Main Stack
  5. Updates MSP to current Main Stack pointer
  6. Jumps to exception handler address

On exception return, the reverse operation restores the Thread Mode context:

  1. Restores MSP from Main Stack
  2. Returns to Thread Mode
  3. Restores PSP from Main Stack
  4. Pops and executes EXC_RETURN code
  5. Resumes execution in Thread Mode

This careful saving and restoring of stack pointers and privilege states ensures seamless transition between the two modes during exception processing.

Use Cases

Some example use cases of Thread and Handler modes are:

  • Application Code – Runs in Thread Mode using Process Stack and R0-R12 registers
  • System Timer ISR – Handler Mode used to quickly save context and program timer for next interval
  • Memory Fault – Handler Mode used to isolate fault and take appropriate recovery action
  • Task Switching – Handler Mode used to save and restore task contexts during RTOS scheduling
  • Supervisor Call – Optionally switch to Handler Mode to perform privileged operation

Properly utilizing both modes as per application requirements is key to designing robust and secure Cortex-M3 based systems.

Thread Mode Priority Levels

Cortex-M3 implements configurable priority levels within Thread Mode to allow differentiation between different application threads in an RTOS context. This is enabled using the BasePRI register.

BasePRI can be set to specify the threshold priority level from 0 to 255. All application threads with priority below this level get temporarily disabled when BasePRI is set to higher level.

This allows execution of high priority thread code uninterrupted by lower priority application threads. BasePRI is automatically cleared on exception entry and restored on exception return.

Using BasePRI allows efficient application level context switching without incurring the full overhead of exception processing.

Conclusion

In summary:

  • Cortex-M3 has two distinct modes – Thread and Handler
  • Thread Mode runs normal application code
  • Handler Mode runs privileged exception handlers
  • Each mode has separate stack, registers and privilege
  • Mode transition occurs during exception processing
  • Understanding the modes is key to utilizing Cortex-M3 capabilities

The dual mode architecture provides robust exception handling, application isolation and high performance. Studying the detailed operation of both modes allows developers to harness the full power of Cortex-M3 in their embedded system designs.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Reset Sequence in Cortex-M3 Microcontrollers
Next Article What is the Thread mode in Arm Cortex M3?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

Relocating the Vector Table in Cortex-M3 Boot Code

The Cortex-M3 vector table contains the reset value and exceptions…

8 Min Read

Cortex-M3 Memory Protection and Isolation (Explained)

The Cortex-M3 processor from ARM offers several features for memory…

10 Min Read

Handling Resets and Exceptions During Cortex-M3 Boot Code Execution

When a Cortex-M3 microcontroller first powers on or resets, it…

7 Min Read

Bit-band Regions for Atomic Operations in Cortex M3

The Cortex-M3 processor implements bit-band regions that allow single-bit atomic…

8 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account