Follow+Starred PublicNo., don’t miss exciting content
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.
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.
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.
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".
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.
Figure 5-1 Entry function call
six
system initialization
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.
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.
How does MPU dynamic reconfiguration work?
MCU/MPU innovative embedded solutions
Renesas MCUHow difficult is it to get started?