Thread Vs Handler Mode Arm Cortex-M

 

 Table of Contents

  1. Introduction
  2. Thread Mode Vs Handler Mode
  3. How to know if Arm Processor is in Thread or Handler Mode
  4. Summary

1. Introduction

In ARM Cortex-M microcontrollers, "Thread Mode" and "Handler Mode" are two distinct execution modes that determine how the processor handles and prioritizes interrupts and exceptions. These modes are crucial for managing the control flow and handling various events in embedded systems. Here's an overview of both modes:

Thread mode in ARM architecture is for normal execution of applications with both privileged and unprivileged instructions. Handler mode, on the other hand, is privileged and handles exceptions and interrupts, providing higher access for critical operations and system-level events.

2. Thread Mode Vs Handler Mode

Sno Thread Mode Handler Mode
1 Also known as "Thread" or "User Mode." Also referred to as "Handler" or "Privileged Mode."
2 This is the normal execution mode for an application's main code. Handler Mode is used to execute exception and interrupt service routines (ISRs).
3 In Thread Mode, the processor executes application code, and the thread can be preempted by higher-priority exceptions, such as interrupts. When an exception occurs, the processor transitions to Handler Mode to execute the corresponding exception handler.
4 When an exception occurs, the processor saves the current context (registers and program counter) and transfers control to the appropriate exception handler. In Handler Mode, all interrupts and exceptions are treated as non-maskable, and they can preempt the execution of lower-priority exception handlers or thread code.
5 Multiple exceptions can be pending, and the processor services them in priority order. However, lower-priority exceptions can be delayed if higher-priority exceptions are being handled.

Handler Mode has higher privileges and access to privileged system resources, while Thread Mode is more restricted in terms of system access.

6  Thread Mode is used for executing non-critical code and application tasks. Handler Mode is used for executing critical system tasks, such as interrupt service routines and operating system kernels.
7

Code Example :

Code Example :

3. How to know if Arm Processor is in Thread or Handler Mode

Determining whether an ARM Cortex-M processor is currently in Thread Mode or Handler Mode typically involves checking the value of a specific status register bit. In Cortex-M processors, this information is stored in the Control Register (CONTROL or CONTROL_Register), specifically in the :

1. nPRIV bit (0 means CPU is in Handler Mode and 1 means CPU is in Thread Mode).

2. SPSEL bit can also be checked, SPSEL inHandler mode this bit would be 0 and in Thread mode this bit would be 1.

4. Summary

In summary, Thread Mode is the normal execution mode for application code and tasks, while Handler Mode is used for handling exceptions and interrupts, especially those that are time-critical and need immediate attention. The processor switches between these modes as needed to manage the execution flow in an ARM Cortex-M microcontroller.