Important contents in the microcontroller FSP library startup file

Follow+Starred PublicNo., don’t miss exciting content

b76d789988364af8cf3fe852fa716133.gif

Reprinted from | Renesas Embedded Encyclopedia

This article mainly introduces the code description of the startup file generated by FSP under e2 studio, allowing users to understand the Renesas RA MCU Have a preliminary understanding of the startup process.

one

Introduction to FSP

The Renesas Flexible Software Package (FSP) is a flexibly configurable software package designed to provide easy-to-use, scalable, high-quality embedded system design software. It is suitable for RA series MCUs, provides a unified and intuitive API, and has detailed explanations and routine applications. It can automatically generate corresponding drivers according to different needs of customers.

two

Startup file description

The startup file is the first program executed after the program is powered on and reset. It generally includes the following:

1.Initialize the stack

2. Enable FPU

3. Locate the interrupt vector table

4. Configure the system clock

5. Enable CORTEX-M33 stack monitor

6. Initialize the C language runtime environment

7. Initialize the variable SystemCoreClock. This variable stores the frequency of the processor clock.

8. Initialize the ELC (event link control) used to trigger the NVIC interrupt

9.BSP initialization

three

Initialize the stack

3.1 Stack

A sequential data structure that satisfies the last-in-first-out principle. It is automatically allocated and released by the compiler to store function parameter values, local variable values, etc. The stack is particularly convenient for saving/restoring call scenes. When a function is called, its parameters will also be pushed onto the stack of the process that initiated the call, and after the call is completed, the return value of the function will also be stored back on the stack. If there are too many function parameters and local variables when a function is called, and the registers cannot fit in them, you need to increase the size of the stack area appropriately.

3.2 Heap

Usually allocated manually by the programmer, using malloc and free for allocation and release. Note that it needs to be released in time after use, otherwise it will easily cause memory leaks.

3.3 ​​e2Studio medium configuration

The macro "BSP_CFG_STACK_MAIN_BYTES" can be set in "RA Common" in the "BSP" property column of FSP Configuration by modifying the "Main stack size" setting. The default is 1KB (0x400 Byte).

The macro "BSP_CFG_HEAP_BYTES" can be set in "RA Common" in the "BSP" property column of FSP Configuration by modifying the "Heap size" setting. The default is 1KB (0x400 Byte). As shown in Figure 3-3-1.

81dad5714f971a2998233f4e0d0ba309.png

Figure 3-3-1 Stack area size setting

g_main_stack and g_heap represent the arrays of the main stack and heap area respectively, followed by attribute modifications.

"BSP_PLACE_IN_SECTION(BSP_SECTION_STACK)", after macro expansion is "__attribute__((section( ".stack"))) __attribute__((__used__))"

"BSP_PLACE_IN_SECTION(BSP_SECTION_HEAP)", after macro expansion is "__attribute__((section( ".heap"))) __attribute__((__used__))"

Its modification attributes are explained as follows:

__attribute__((section(“parameter name”))): Variables can be defined in the specified input section “parameter name”.

__attribute__((__used__)): The parameter "__used__" tells the compiler that this variable will be used even if it is not explicitly called during compiler optimization. This attribute prevents the compiler from optimizing out unused functions or variables and ensures that they exist in the program.

a82aca773e4f47f831483d0ed627cd70.png

Figure 3-3-2 Stack area array attribute modification

"BSP_ALIGN_VARIABLE(BSP_STACK_ALIGNMENT)" in the code, after macro expansion, is "__attribute__((aligned(8)))", which means that the stack area will be allocated according to 8-byte alignment.

__attribute__((weak)): The user can redefine a function with the same name in the user file. When the compiler finally compiles, it will select the user-defined function. If the user does not redefine this function, the compiler will execute the _weak statement. function, and the compiler will not report an error. So we can define a function with the same name elsewhere. The function cannot be in the same .C file.

6eaf2aed1dbf078234e4197e3b672439.png

Figure 3-3-2 Function attribute macro definition

Four

Interrupt vector table

Define an interrupt vector pointer array __Vectors[16], and put this table into the input section of ".fixed_vectors".

dd43bc6f49d146d17d8f708af94418aa.png

Figure 4-1 Interrupt vector table definition

five

Entry program

After the system is powered on or reset, the entry function Reset_Handler() is directly executed, including two functions: SystemInit() and main(). SystemInit() mainly uses BSP to initialize the system, and then enters the user code through the main function. The BSP is responsible for making the MCU enter the user's application program from the reset state. Before reaching the user's application, the BSP sets up the stack, clock, interrupts, C language runtime environment and stack monitor.

b2619b8a0583342e289fa2b7a9a9e4e6.png

Figure 5-1 Entry function call

six

system initialization

54db2b9ad00a80c0ee521f4359be7746.png

094bb9063f0669c2b586d63e90f14f82.png

414e8153f46a2a9effa32aa274d96cbe.png

3a9392564072f04554279e768610151d.png

2cbf17d124279312af5b79846ea561e7.png

0494e046b076430308464b8b193ad6e2.png

Figure 6-1 System function initialization

No.1

7. Memory function allocation

Open Memory Usage through e2 studio, and you can see the definition name, start and end address, occupied capacity, alignment, etc. of each segment. . You can also open the \e2studio\Debug\*.map file in the project folder.

Here is an explanation of commonly used section names:

.bss: usually refers to a memory area used to store uninitialized or initial value 0 global variables in the program. BSS is the abbreviation of English Block Started by Symbol. The BSS segment is a static memory allocation.

.data:The data segment usually refers to a memory area used to store initialized global variables in the program. The data segment is a static memory allocation.

Code segment:The code segment (code segment/text segment) usually refers to a memory area used to store program execution code. The size of this area is determined before the program is run, and the memory area is usually read-only. Some architectures also allow the code segment to be writable, which allows the program to be modified. The code segment may also contain some read-only constant variables, such as string constants, etc.

2860c9dcf7ab8ec81c5dde0fca4e7cb3.png

Figure 7-1 Memory function allocation

in conclusion

Through the introduction of this article, I believe that everyone has a certain understanding of the startup process of RA MCU and the operation of e2 studio. Interested students can also study in depth.

02611d11b05400455103527b0dfa429f.jpeg

How does MPU dynamic reconfiguration work?

a05239c050b6ed40975699936d073e4c.jpeg

MCU/MPU innovative embedded solutions

a3d4fe26db1f026b3d01930feb0e44d3.jpeg

Renesas MCUHow difficult is it to get started?

Guess you like

Origin blog.csdn.net/ybhuangfugui/article/details/134813725