Thread Mode and Handler Mode in ARM

Raihan Soniwala
3 min readFeb 21, 2024

--

To begin with working with ARM architecture the first step is to learn about Operational modes in ARM which are Thread mode which is also known as User mode and the second is Handler mode.

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.

Thread Mode

Thread mode also called as User mode is primary mode of operation of the Cortex M4 processor during the normal execution and controller by default will always run in thread mode. In this mode, the processor executes instructions from the programs main thread sequentially advancing through the program flow. This thread of programs can be preempted by higher-priority exception, such as interrupts. To handle the interrupt the controller saves the context that is registers and program counter and transfer the control to the appropriate exception handler.

Handler Mode

Handler Mode is a specialized mode of operation invoked when the processor encounters an interrupt or an exception that requires immediate attention. Unlike Thread Mode, which handles regular program execution, Handler Mode focuses on servicing interrupts and exceptions effectively. Basically Handler mode is used to execute exception and ISRs (Interrupt Service Routines). When an exception or interrupt occurs the processor transits from user to handler mode to execute the corresponding exception handler or ISR. Once the ISR or Exception handler has completed its execution the processor again returns to the normal mode that is thread mode to resume the normal program execution.

To summarize, Thread mode is used for executing non-critical code and application tasks whereas handler mode is used for executing critical system tasks, such as interrupts.

Now let’s see how controller switches from thread mode to handler mode.

Here in this project I have write a program for STM32F446RE micro controller which works on ARM cortex M4 processor. This program uses NVIC API to generate the interrupt. The processor supports various interrupts and here we will stimulate the watch dog interrupt.

Here you can see that our processor is running on Thread mode by default and the program is halted at the command which causes interrupt. Lets move further and stop the execution while program is at Interrupt handler.

Fantastic ! . The processor went into Handler mode to execute the interrupt handler. And after the completion of this handler function the processor will again move to thread mode.

Now you must be thinking why do we need a dedicated “Handler Mode” to execute handlers and ISRs why not execute them in Thread Mode only. No worries you will get the brief idea as soon as we advance in this lists of blogs

--

--