Understanding the Execution of the Main Function in Microcontrollers

Follow+Star Public Account, don’t miss exciting content
Understanding the Execution of the Main Function in Microcontrollers
Author | strongerHuang
WeChat Official Account | strongerHuang
Why is the main function directly called in the reset interrupt service routine? Does that mean all programs are executed in the reset interrupt?
Understanding the Execution of the Main Function in Microcontrollers
First, Reset_Handler is an interrupt of the microcontroller, and secondly, the main function is indeed called by the Reset_Handler interrupt. So, is the main function executed in the interrupt?
When you see this question, have you ever thought about it? Could it be that our previous understanding was wrong?
To be honest, I never thought about this question. I believe that the vast majority of people have not considered this issue. So, I would like to share the content of this question here.

Operating Modes of Microcontrollers

The microcontroller here mainly refers to ARM Cortex-M core microcontrollers.
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 privilege levels:
  • Privileged Level
  • User Level
In the Cortex-M3 manual, there is a passage:
Understanding the Execution of the Main Function in Microcontrollers
The answer to the initial question is highlighted.
The official online document also mentions relevant explanations:
Understanding the Execution of the Main Function in 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 briefly compare and analyze it 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 the Main Function in Microcontrollers
At this time, it is mentioned in the document that after the reset, the processor enters Privileged Thread Mode.
2. Entering the SysTick_Handler Interrupt
When we enter a normal interrupt, such as entering the SysTick_Handler clock tick interrupt:
Understanding the Execution of the Main Function in Microcontrollers
At this time, we are entering Privileged Handler Mode.
Seeing this, you should understand.

Conclusion

This question’s key point is:
The operation modes of the reset interrupt (Reset_Handler) and the 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.
You may have never cared about this issue, and of course, we rarely encounter corresponding problems.
This is a non-issue question; it doesn’t matter if you don’t understand it. Today, you have learned a bit more about the knowledge point of the Cortex-M microcontroller core.
———— END ————
Understanding the Execution of the Main Function in Microcontrollers
● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected Tutorials from Embedded Column
Follow the public account Reply “Add Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Understanding the Execution of the Main Function in Microcontrollers
Understanding the Execution of the Main Function in Microcontrollers
Click “Read Original” to see more shares.

Leave a Comment

×