Understanding the STM32 startup file

Publisher:炫酷骑士Latest update time:2016-02-25 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
    In the process of embedded application development, due to the use of C language programming, the execution process of the underlying registers of the machine is rarely involved. Generally, the code is written directly in the main function. It seems that main has become the starting point for granted, although from the perspective of C programs, the program is executed directly from the main function. However, after the MCU is powered on, the question of how to find and execute the main function is naturally ignored! In fact, the microcontroller cannot locate the entry address of the main function from the hardware, because after using C language as the development language, the address of the variable/function is automatically assigned by the compiler during compilation, so the entry address of the main function is not necessarily an absolute address after compilation. How does the MCU find this entry address after powering on? In the past, whether it was PIC, AVR, MSP430 or 51, the configuration of the startup file was not involved. Only the fuse bit or configuration word needed to be set according to the actual configuration. In fact, it is not that there is no startup file, but because most development environments often automatically and completely provide this startup file, there is no need for developers to intervene in the startup process. You only need to start the application design from the main function. However, when it comes to embedded kernels, such as the Linux system transplantation process, the "bootloader" is an important and indispensable link. In fact, every microcontroller, regardless of its performance, structure, or price, must have a startup file to work properly. Its role is similar to that of the "bootloader". The startup file completes the necessary startup configuration of the microcontroller from "reset" to "starting to execute the main function".

    In STM32, if you create a project under MDK, there is usually a prompt whether to add the Star up Code file, which is the startup file. There is a misunderstanding here. Generally speaking, for beginners, it is easy to mistakenly think that the STM32F10x.s startup file is a universal startup file for all types of STM32 chips, so naturally they will not pay attention to its role. In fact, this startup file is only for some STM32 series. If you look carefully at its startup code, you will find that many interrupt function definitions are not available, and some of them are even different from the functions in STM32F10x_it.c. If you happen to use the default interrupt service subroutine, once the program runs to the interrupt, it will not find the entry address, so you will inexplicably not know where the problem is. STM32F10x.s is the startup code provided by MDK. From the content inside, it only defines 3 serial ports and 4 timers. In fact, the STM32 series products have models with 5 serial ports, and only models with 2 serial ports, and timers as well, with up to 8 timers. For example, if you use STM32F103ZET6, and the startup file uses STM32F10x.s, you can use the interrupts of serial ports 1 to 3 normally, but the interrupts of serial ports 4 and 5 cannot be used normally. Therefore, STM32F10x.s is not suitable for all STM32 models. For different models of STM32, the correct approach is to choose different startup files. ST provides 3 startup files: startup_stm32f10x_ld.s
/startup_stm32f10x_md.s/startup_stm32f10x_hd.s, which are respectively suitable for small capacity/medium capacity/large capacity STM32 chips. The specific judgment method is as follows:

                    Small capacity: FLASH ≤ 32K
                    Medium capacity: 64K ≤ FLASH ≤ 128K
                    Large capacity: 256K ≤ FLASH
In the startup code, add a few points:

Explanation of the two statements in the startup code:

1. PROC is the start of a subroutine, and ENDP is the end of a subroutine

2. [weak] means that the priority of this function is relatively weak. If a function with the same name is defined elsewhere, this function will be replaced. The syntax format is EXPORT label {[WEAK]}. EXPORT can be replaced by GLOBAL.

Understanding of the _main function:

In fact, _main and main are two completely different functions! The _main code is automatically created by the compiler, so it cannot be found. There is a statement in the MDK documentation: it is automatically craated by the linker when it sees a definition of main() . The general meaning can be understood as: when the compiler finds that the main function is defined, it will automatically create _main.

The relationship between _main and main

_main mainly does two things: first, it gets the resources needed by C; second, it calls the main function. This makes it easy to understand why the startup code calls _main, but the main function is finally executed.

 

Understanding the AREA Directive

The AREA directive is a pseudo-instruction used for segment definition. The ARM assembler consists of segments, which are relatively independent units of instructions or data. Each segment is defined by the AREA pseudo-instruction, which defines the attributes of the segment.

AREA parameter description:

STACK——A parameter of the AREA instruction, defining the segment name

NOINIT - A parameter of the AREA instruction, which specifies that only the internal unit is retained in this data segment, and the initial value of the sentence is written into the memory unit. At this time, the memory unit value is initialized to 0

READWRITE - Specifies that this segment is readable and writable. The data segment defaults to READWRITE.

READWRITE (read and write), READONLY (read only)

 ALIGN——Also a pseudo-instruction that specifies the alignment. There are two options for the alignment value of the ALIGN n instruction: n or 2^n

Example: Create a stack segment named STACK, define it as readable and writable, initialize the memory unit to 0, and align it to 8 bytes.

AREA STACK,NOINIT,READWRITE,ALIGN=3


Keywords:STM32 Reference address:Understanding the STM32 startup file

Previous article:STM32 interrupt priority problem
Next article:Torque Loading Controller Design with RTX and TM4C Microcontrollers

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号