Understanding the Execution of main Function in Interrupts of Microcontrollers

Recently, I came across a piece of content shared by a tech enthusiast: Why is the main function called directly in the reset interrupt service routine? Does that mean all programs are executed within the reset interrupt?

Understanding the Execution of main Function in Interrupts of Microcontrollers

First of all, Reset_Handler is an interrupt of the microcontroller, and indeed, the main function is called by the Reset_Handler interrupt. So, isn’t the main function executed within an interrupt?

When you see this question, have you ever wondered about it? Could it be that our previous understanding was wrong?

To be honest, I’ve never thought about this question, and I believe that the vast majority of people haven’t either. Therefore, I would like to share some insights on this topic.

Operating Modes of Microcontrollers

The microcontroller referred to here mainly points to the ARM Cortex-M core microcontroller.
To answer the initial question, we need to mention the operating modes of the microcontroller, taking the Cortex-M3 microcontroller as an example. The Cortex-M3 supports two modes and two privilege levels.
Two Modes:
  • Handler Mode
  • Thread Mode
Two Levels:
  • Privilege Level
  • User Level

There is a section in the Cortex-M3 manual that mentions:

Understanding the Execution of main Function in Interrupts of Microcontrollers

I have highlighted the answer to the initial question.

The official online documentation also mentions relevant explanations:

Understanding the Execution of main Function in Interrupts of Microcontrollers

Address:

https://developer.arm.com/documentation/dui0552/a/the-cortex-m3-processor/exception-model/exception-types

Example Analysis

The answer is highlighted above, but you may still be confused. Here, I will provide a simple comparative analysis using Keil MDK + STM32 project code.
1. Entering the Reset_Handler Interrupt
When we are debugging online, in the reset state, we enter the Reset_Handler reset interrupt:

Understanding the Execution of main Function in Interrupts of Microcontrollers

At this point, it is mentioned in the documentation: after reset, the processor enters Privilege Level Thread Mode.
2. Entering the SysTick_Handler Interrupt
When we enter a normal interrupt, such as the SysTick_Handler clock tick interrupt:

Understanding the Execution of main Function in Interrupts of Microcontrollers

At this point, we are in Privilege Level Handler Mode.

Seeing this, you should understand now.

Conclusion

The key point of this question is:
The operation modes of the reset interrupt (Reset_Handler) and normal interrupt (SysTick_Handler) are different.
Other IDEs, such as Keil, GCC, etc., have similar principles. This issue mainly lies in the core, which is determined by the core itself.
Perhaps you have never cared about this issue, and indeed, we rarely encounter corresponding problems.
This is a question that is not really a problem. It’s okay if you don’t understand it; you have gained a further understanding of a knowledge point of the Cortex-M microcontroller core today.
Understanding the Execution of main Function in Interrupts of Microcontrollers

END

Source: strongerHuang
Copyright belongs to the original author. If there is any infringement, please contact to delete.
Recommended Reading
The internal structure of the Titan missile guidance computer is exposed!
Why are embedded salaries much lower than pure software salaries?
Why are Chinese programmers less creative than foreign programmers?
→ Follow for more updates ←

Leave a Comment