Detailed Keil startup file

Overview:

In an embedded system, the startup file is a very critical part of the entire system. It performs some underlying initialization and builds the necessary environment for the program to run, such as stack initialization, variable initialization, and so on. If the startup file has an error, the entire system will not run, so it is necessary to study the startup file.

In keil, the startup file is written in assembly code, generally named startup_xxx.s, and xxx is a supported chip. For example, it can be lpc15xx (NXC LPC15xx series), MK60D10 (Freescale), stm32f10x ( STMicroelectronics stm32f10x Series) and other Cortext-M0/M3/M4 core chips. Their code formats are very similar, depending on the startup file code from top to bottom.

It can be divided into the following five typical parts:

1. Stack space definition;

2. Store the interrupt vector table;

3. Reset interrupt function (Reset_Handler);

4. Other interrupt exception service functions, and weak [WEAK] declarations;

5. Pass the stack address to the library function, initialize the stack with the library function, and initialize the library function itself.

The five parts are specified as follows:

1. Stack space definition

  As shown in the following figure, the stack size Stack_Size = 0X200, which is 512 bytes, is defined; the heap size Heap_Size = 0X100, 256 bytes. alsoThree labels are defined: __initial_sp (top of stack), __heap_base (heap start address), and __heap_limit (heap termination address)Their space is requested by the SPACE keywordAnd recorded as Stack_Mem and Heap_Mem.

Through these we can easily know the size of the stack, but their absolute or base address is only not available from here. After the compiler compiles the project,The base address of the stack is calculated based on the size of the generated .bss segment (such as uninitialized global variables) and the .data segment (such as the initialized global variable) and the starting address of the RAM.

(The scatter file is not used in Keil. When the ram address is specified, the default distribution is .data(RW) -> .bss(ZI) -> HEAP -> Stack. )

for example:

The starting address of a chip's RAM is 0x0200_0000, the RAM size is 0x500 bytes, the .bss section is 0x100 bytes after the program is compiled, and the .data section is 0x100 bytes. The stack size is defined as shown above. then:

A: heap start address __heap_base==Heap_Mem==0x0200_0200;

B: The heap termination address is the bottom of the stack __heap_limit==Stack_Mem==0x0200_0300;

C: Stack top address __initial_sp==0x0200_0500 (the stack is growing down, the top of the stack is at the RAM's maximum address).

In fact, I can view the size and base address of the stack in the .map file, as shown below:

 

2. Store interrupt vector table

 In the startup code, you will see a number of function entries stored in the DCD request space, the interrupt vector table, as shown in the following figure, only the parts are listed.

  The keyword DCD represents the space for applying a word, and the function name that follows is the interrupt service function entry address.In addition, the interrupt vector table is generally stored in the Flash/ROM 0 address.

3. Reset interrupt function (Reset_Handler)

  After the program is powered on, first load the SP and PC.ARM specifies that the SP is loaded from the 0 address and the PC is loaded from the address with an offset of 4 (0x00000004). Then give control of the program to the program. We know that __initial_sp is stored at address 0, and Reset_Handler is stored at address 0x00000004. After loading the PC, the program jumps to Reset_Handler and starts running. The Reset_Handler function body is shown below:

 First call the SystemInit function to initialize the various clocks of the system.Then call the __main function (implemented by the KEIL micro-library or C library)In the __main function: initialization of the .data section data ->.bss section variable clear -> set stack pointer initialization stack -> library function initialization (such as the commonly used malloc function) -> if necessary, set the main function argc and Argv two parameters -> call the user main function -> exit.

4. Other interrupt exception service functions, and weak [WEAK] declarations

As shown above, hereThe interrupt service function is weakly declared (marked by the [WEAK] keyword). The so-called weak statement, that is, if the user defines the same function, the function here fails and the user-defined interrupt service function is used. This is to prevent the user from enabling the interrupt without interrupting the service function, causing the program to crash.Assuming that the interrupt is enabled, and the user has not defined the interrupt service function, the default interrupt will be entered. As shown in the following figure, the default interrupt is an infinite loop (infinite loop and program crash are not a concept).

5. Pass the stack address to the library function

  In the third step, the __main function is called, and then __main calls the library function to initialize the stack, but the library function does not know the size of the stack, so we need to tell it, the specific method is to pass parameters or declare labels.

The following figure shows the specific behavior, you can see the first behavior:

IF      :DEF:__MICROLIB

It is a conditional compilation option. If __MICROLIB is defined, the upper part of the red line in the figure is compiled, otherwise the lower part of the red line is compiled. Then there are two kinds of situations.

The choice of the two cases can be achieved as follows:

If you select [Options for Target] -> [Target] -> [Use MicroLIB], as shown below. That is, using the micro-library, __MICROLIB will be defined, and the compiler compiles the red line code above. Declare __initial_sp, __heap_base, and __heap_limit with EXPORT.

If [Use MicroLIB] is not checked, the KEIL C library is used by default. The red line below will compile. The KEIL C library function will call __user_initial_stackheap, and the stack will be passed to the KEIL C library via R0~R3.

 

Reprint link:

Intelligent Recommendation

Configure the startup mount: fstab file detailed explanation

fstab file Introduction fstab file includes information storage device and file system on your computer. It was decided a hard disk (partition) is how to use or integrated into the entire file system....

tomcat detailed explanation of startup file catalina.bat

Run the following program, you will know what the statement is executed Reproduced in: https: //my.oschina.net/peixiaobin/blog/127866...

STM32 startup file detailed-study notes (3)

The content of the article is organized according to the wildfire learning tutorial, just learning records. Development board: Wildfire STM32F429-Challenger V2 Official firmware library version: STM32...

STM32 launching detailed process startup file

Start file 1 Introduction 2. Start the file content description 2.1. DCD instruction 2.2. B . 3.STM32 launch process 3.1. Get the top pointer of the stack 3.2. Jump to the reset interrupt function 4. ...

About keil the header file

First create a .h file in the project under such delay.h. Write function declarations in which you want to join, or some other pre-defined. Then in the same project under a new name of a .c file, head...

More Recommendation

Keil: .gitignore file configuration

Use of the environment:Keil + stm32f429 When using git Keil project management, you can exclude unnecessary files by configuring .gitignore. Here is configure my own use....

Keil generates bin file

1. Select Project-> Options for Target on the upper menu bar of Keil MDK software, as shown below: 2. Click the User tab, select an unused item under After Build / Rebuild, I use Run # 1 here, and ...

keil error:malformed via file....

I found a crazy error when I was running keil today, and I couldn't start. error:malformed via file…. This problem is rare, but I have seen some people encountered it online The meaning of malf...

KEIL debug ini file

Convenient debugging, go directly to git git add -f examples/ble_peripheral/ble_app_hrs_freertos/pca10056/s140/arm5_no_packs/JLinkSettings.ini Violent increase files! !...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top