STM32 Chapter 2 - Start process detailed

STM32 boot process, the boot process means executes the first reset instruction starts (compiled file) from the CPU to enter the C main program function between an inlet portion (). The startup process is still relatively important, although difficult but must understand the.

1. The different series of different chip startup code.
2. Start the process is mainly done:
Here Insert Picture Description
3. Open your project, double-click on the project file. .Out file will come out to see the corresponding interrupt vector list stored in the internal flash memory.
Here Insert Picture Description
Here Insert Picture Description
4. The reset sequence
after hardware reset, after the reset switch is pressed. Sequential logic circuit within the CPU first performs the following two operation (Program Codes to an example internal flash, flash first address 0x0800 0000)

  • The 0x08000000 storage position address stored in the stack SP in the stack (MSP).
  • The 0x08000004 storage position vector address into the program counter PC.
    Remove from the physical address of the PC CPU register points to the first instruction of the execution of the program, which is the start of a reset interrupt service routine Reset_Handler. Reset interrupt service routine calls SystemInit () function to set the system clock disposed external SRAM / SDRAM bus on the FMC, and then jump to the C library functions __main. Done by the C library functions __main user program initialization (for example: variable initial values, etc.), the last call user-written functions __main by the main () function starts executing C program.

Before learning startup file first look at some assembly language instructions.
ARM assembly instructions startup files summary

Specific code analysis

The first part of the code analysis
following code implements open stack (Stack) space for local variables, function calls, function parameters.

Action stack for local variables, function calls, and so the cost function parameter, the stack size can not exceed the size of the internal SRAM. If the programming is relatively large, a lot of local variables defined, then you need to modify the stack size. If one day, you write the program appeared inexplicable strange error, and enters a hard time fault, then the next you have to consider whether or not the stack is not big enough, overflowed.
Here Insert Picture Description
Line. 7 : EQU directive is a macro definition, similar to the C language #define . Directive is meant the "instruction" does not generate a binary code, it will not cause variable space allocation. 0x00008000 expressed stack size, pay attention to here is in bytes. 0x00008000 = 32768 bytes = 32KB

Line 8 : some open space read and write data, name STACK segments, aligned in 8 bytes. ARER directive indicates will begin to define a segment of code or data segment. Here is the definition of the data segment. ARER after the keyword indicates that the property segment.

  • STACK: represents the name of this section, you can be named.
  • NOINIT: Indicates the initial data segment not need to fill.
  • READWRITE: This section represents a readable and writable.
  • ALIGN = 3: 3 represents the first address in accordance with the aligned power of 2, i.e. according to the 8-byte aligned (to address 8 I logarithmically equal to 0).

Line. 9 : the SPACE tell the assembler instruction line 0x00000800 bytes allocated to contiguous memory space STACK segments.

Line 10 : __initial_sp immediately placed SPACE statement indicating the top of the stack address. __initial_sp just a label, a label is mainly used to indicate a location of memory space, equivalent to "address" the concept of the C language. Merely indicates a position address memory space, from the point of view of the C language, the address of the variable, or a function of the entry address of the address array is not different in nature.

The second part of the code analysis
following code implements open heap (heap) space , is mainly used for dynamic memory allocation, as a function of the spatial distribution of the variable malloc, calloc, realloc and the like are on the heap.
Here Insert Picture Description
These lines and the first part of the code statement similar to the above. Allocate a contiguous memory space for the name of the section HEAP, which is allocated heap space. Heap size is 0x00000400, which is 1024 bytes = 1KB.
__heap_base start address of the heap.
__heap_limit indicates the end address of the heap.

Code analysis part 3
Here Insert Picture Description
, line 24 : PRESERVE8 for the current file holding the stack 8 byte aligned .
Line 25 : indicates that the following instruction is a THUMB THUMB set, CM4 is used THUMB - 2 instruction set.
Line 29 : AREA define a code segment, read-only segment name is RESET. READONLY for read-only, the default code section says.
30-32 rows : 3 rows EXPORT statement three labels can be declared as external references, primarily to the linker used to link library files or other documents. When the kernel in response to occurrence of an abnormality, the corresponding exception service routine (ESR) will be executed. In order to determine the entry address ESR, the kernel uses - ‖ vector table lookup mechanism. Here the use of a vector table. The vector table is actually a WORD (32 bit integers) array, each corresponding to an unusual index, the lower is the value of the standard element ESR inlet of the
address. Vector table in the address space locations can be set to indicate to the NVIC by a relocation register
address Scale. After reset, this register is zero. Thus, address 0 (i.e., FLASH address 0), the packet must
contain a vector table, an abnormality at the initial assignment. Note that here there is an alternative: not a type 0
entry address, but gives the initial value of MSP after reset.

Part 4 code analysis
Here Insert Picture Description
Vector Table
above this code is to establish the interrupt vector table, the interrupt vector table is positioned in the front section of the code. Particular physical address is determined by configuration parameters linker (IROM1 address). If the program runs in Flash, the interrupt vector table starting address is 0x08000000. In MDK example, is a configuration option:
Here Insert Picture Description
the DCD represents a 4-byte space allocation. Each row DCD generates a 4-byte binary code. Interrupt vector table
stored in the entry address is actually the interrupt service routine. When an exception (that is, an interrupt event) occurs, CPU interrupt system will set the appropriate entry address assigned to the program counter PC, and then began to execute the interrupt service routine.

Part 5 Analysis of code
Here Insert Picture Description
line 53 is : a definition of the AREA code segments, read-only segment name is .text. READONLY for read-only.
Line 56 : using PROC, ENDP the directive of this block is divided into a plurality of processes, so that the program structure plus clear.
57th row : WEAK other statements of the same name label takes precedence over the label is referenced, that is if the outside is declared, then it calls out. This statement is important because it allows us to be placed anywhere in the interrupt service routine in the C file, as long as the name of the C function and consistency to the vector table name.
Line 58 : IMPORT: reference numeral used to inform the compiler directive to be used is defined in some other source files. But to quote in the current source file, and regardless of the current source file is referenced label, the label will be added to the symbol table of the current source file.
Line 61 is : SystemInit () is a standard library function, in the general definition system_stm32f4xx.c this library. After the main role is to set the system clock call this function here, F429 system clock feature is configured to 180M.
Line 63: __ main entrance numerals denote address C / C ++ standard library functions in a real-time initialization routine to __main. A major role of this program is to initialize the stack and initialize the image file, and finally jump to the main function in a C program. This explains why all the C program must have a main function as a starting point for the program. Because it is regulated by the C / C ++ standard library in real time, and can not be changed. If we are not here to call __main, the program ultimately would not call our C file inside the main, if it is naughty users can change the name of the main function, then there IMPORT you write the main function name. This time you write in C files inside the main function name is not the main, but the __main.
LDR, BLX, BX instruction is CM4 kernel:
The kernel command

Part 6 code analysis
Here Insert Picture Description
, line 71 : infinite loop, where users can implement your own interrupt service routine. But few here realize the interrupt service routine, and more generally of the same name is a re-write interrupt service routine in other C files inside, because it is weak WEEK defined. If you do not write interrupt server program in another file, and this interrupt is enabled, enter here, the program will stuck in this place.
Line 81 : Default interrupt service routine (start)
line 92: infinite loop, if the user enable the interrupt service routine, but did not write interrupt service routine in the C file which then will enter here. For example, in a program which enabled the serial port 1 interrupt, but did not write an interrupt service routine ART1_IRQHandle, then the serial port interrupt, and will enter into this endless loop.
Line 94 : Default interrupt service routine (end).

Part 7 code analysis
last part of the startup code: Here Insert Picture Description
Line 101 : Simple assembly language IF ...... .ELSE ............ statement. If you define MICROLIB, then the program will not execute the code ELSE branch. __MICROLIB and we may not be familiar, it is set in the Target Option MDK inside.
Here Insert Picture Description
Outsider, then this step is configuration work is very important, many people will not take the serial printf function, the compiler has problems, download problems, this is the wrong configuration steps. Target selected in the micro-library "Use MicroLib", as is in the preparation of future serial port driver can use the printf function. And some applications if the floating-point unit FPU STM32, we must also open micro library, or sometimes all sorts of strange phenomena. FPU switch configuration options under the options in the micro-library "Use Single Precision", the default is on.

Published 33 original articles · won praise 21 · views 8914

Guess you like

Origin blog.csdn.net/qq_39400113/article/details/105056538