<>STM32 Single chip microcomputer startup process
Just contact ARM of cortex-m Series single chip microcomputer , Told everything from main() Function start , To write the program in main() In function . And the simulation also seems to be from main()
Function start , with STM32F103 take as an example .
Later, I learned that the global variable is in main() Function previously initialized .MDK By default, the following options are checked , Skipped the assembly part of the startup code , Directly into main() function .
Actual starting position
stay cortex-m In the series , The interrupt vector table is stored in Flash
Start part ,Flash The first word in the stack stores the top pointer , The second word stores the entry address of the reset interrupt service function , The entry addresses of other interrupt service functions are stored in the Flash in .
MCU After power on , Will Flash The first word in is loaded into R13 MSP Register , The second word is loaded into R15 PC Register .
<>R13: Stack top pointer register SP
SP There are two registers ,MSP perhaps PSP,PSP For RTOS Specially set , If used RTOS, Then in RTOS It is used in the task of PSP, In the interrupt service function MSP. If not applicable RTOS, It is always used by default PSP.
stay MCU in ,RAM Only responsible for temporarily storing data , The real operation is done in registers , For example, add two variables , You need to change the values of the two variables from RAM Take it out and store it in the register , Then operate the register for calculation , Finally, the calculation results are stored in the RAM in . Therefore, it is often said that the scene needs to be saved when entering the sub function or interrupting the service function , In fact, it is to save the value in the register , Save register values on the stack .
MSP Register store stack top pointer , Store local variables in the stack , Function parameters and entry sub functions , The value of the register before the interrupt service function , When jumping out of a subfunction or interrupt service function , Register values are loaded from the stack , That is, restore the scene , Ensure that the program can be executed normally . Usually try not to use recursion in order to prevent multiple calls to itself , Stack overflow caused by saving the scene multiple times .
The code is automatically generated by the compiler for stack in and stack out operations , However, by default, only R0-R3 Push , If the interrupt service function is too complex , The compiler will also R4-R11 Push , This is one of the reasons why interrupt service functions should be kept as short as possible .
<>R15: Program count register PC
PC The register points to the current program address . If you change its value , Can change the execution flow of the program ( Many advanced skills are here )
Load the second word in the interrupt vector table into the PC Register , That is, let the program jump to the reset interrupt service function .
However, the reset interrupt service function is an assembly function .
The interrupt service function is called. SystemInit() function , This function is mainly used to set the offset address of the interrupt vector table . That is, the position of the interrupt vector table is variable , When used BOOT after , You need to be APP Modify the offset address .
Then the reset interrupt service function jumps to __main() In function ,__main() As we usually say main() Functions are different .
RAM Data loss due to power failure , After power on ,RAM The data in is uncertain , Running main() Function RAM Data initialization in , That is the process from left to right in the figure below , take flash Medium RW Load data into RAM in , And will RAM Medium ZI Initialize segment data .MDK in __main() Function helps us automate this operation , That is the so-called preparation C Language environment ,C When the locale is ready, it jumps to
main() function .
last , In fact, there is one less , After power on, the first execution is actually the system bootLoad( When leaving the factory , A piece of code officially solidified in MCU , User cannot modify ). stay STM32 in , Common serial port download ,DFU Is the system bootLoad Functions in , system bootLoad What we said above is only after the implementation is completed , The figure in the article comes from 《Cortex-M3
Authoritative guide 》
Technology
Daily Recommendation