Detailed MCU startup code

The startup code is usually programmed in flash. It is a program that is executed as soon as the system is powered on. It runs before any user c code. After power-on, the arm processor is in the arm state and operates in the management mode. At the same time, all the system interrupts are disabled, and the pc takes the instruction execution at address 0. An executable image file must have an entry point, and the entry address of the image file that can be placed at the beginning of the rom must also be set to 0.

In assembly language, we have already said how to define the entry point of a program. When there are multiple entry points in the project, you need to use -entry in the linker to indicate the entry point of the program. If the user-created program contains the main function, then there is also an entry point corresponding to the c library initialization code.

In general, the startup code mainly accomplishes two aspects. The first is to initialize the execution environment, such as interrupt vector table, stack, i/o, etc. The second is to initialize the C library and user applications.

In the first phase, the person who started the code can be described as:

(1) Establish an interrupt vector table; (2) Initialize memory; (3) Initialize stack registers; (4) Initialize i/o and other necessary devices; (5) Change processor state as needed.

Create an interrupt vector table

The initialization code must establish an interrupt vector table for subsequent use by the application. If the address 0 of the system is rom, then the interrupt vector table is directly a few jump instructions, and they go to the corresponding interrupt handler function execution. If the system 0 address is not rom, then the interrupt vector table is created through a dynamic way, this is mainly achieved through memory mapping: that is, after power-up, the address in the rom is mapped to the address 0, it starts first Execution to complete the initialization of the environment, the most important of which is to copy the interrupt vector table to ram, and then map the ram address to 0 by the address mapping, so that the interrupt vector in ram can be used.

Initialize the storage system

For a processor with mmu, you need to initialize the mmu correctly. If you don't need to initialize the memory controller correctly, configure the correct parameters for each bank.

Initialize the stack pointer

The initialization code must initialize the stack pointer in each mode of the processor. The stack pointer corresponding to the processor mode that is involved in all system or user programs should be initialized. Normally, undefined instructions and prefetches terminate the exception. The stack pointer for the corresponding mode does not need to be configured unless the user needs to use them as debugging.

Initialize the stack pointer

The initialization code must initialize the stack pointer in various modes of the processor. The stack pointer corresponding to the processor mode that all system or user programs will involve should be initialized. Normally, undefined instructions and prefetches terminate the exception. The stack pointer for the corresponding mode does not need to be configured unless the user needs to use them as debugging.

Initialize i/o and other necessary devices

The critical I/O modules must be configured before the interrupt is opened, such as the watchdog, or they will generate a reset signal after the system starts.

Change processor state and mode

When the startup code is running, the processor state is considered as the management mode. If the user program needs to run in the user mode, it can be switched to the user mode. After all the processors are powered on, they are in the arm state. If you need to change the processor state, you can also change the processor state. Switch to thumb mode in startup code.

After the execution environment is established, the next step is to initialize the application program. The simple point is to load the user program into their corresponding running address, initialize the data area, etc. After this stage is completed, the user's final c code area can be entered. The initialization process of the user application program includes: copying the data of rw segment to their running address, and at the same time initializing the zi segment data of the corresponding size after the rw segment, initializing them to 0, and using the program of the library function (the project has The main function) does this automatically in the library function _main.

MSR13 5000puffs Vape

Msr13 5000Puffs Vape,Disposable Vape 5000 Puffs,Oval E-Cigarette,Vaporizer Pen

Guangzhou Yunge Tianhong Electronic Technology Co., Ltd , https://www.e-cigaretteyfactory.com