1 The role of data segments and code segments
The IAR C/C-+ compiler is a world-leading standard C/C-+ compiler that supports C or C++ programming languages that conform to the ANSI C standard. After the source program is compiled, data segments and code segments containing data or code memory logical images are generated. Each segment has a segment name and a segment type that represents its memory space. The segment type CODE represents the execution code in ROM, the segment type CONST represents the data in ROM, and the segment type DATA represents the data in RAM. The segment name can be the same as the segment type, but their meanings are different and should not be confused in actual use. Table 1 lists the various segments, segment types, and their read/write attribute descriptions used by the IAR C/C++ compiler.
1.1 Data segment
Data is located in the DATA segment, including static memory, stack, heap, and located data. The DATA segment can have a suffix. For example, DATA_C is used for constant data, including text strings; DATA_Z is used for static and global variables declared without initial values or with an initial value of 0.
Global variables or declared static variables are stored in static memory space. The declared static variables are: variables with initial values of 0 or non-zero, variables located using the "@" or "#pragma" operator, variables declared as "const" and thus can be saved in ROM, and variables defined using the keyword "__no_init" that are not allowed to be initialized.
The stack is used to store local variables and other temporary data for functions. It is a continuous memory pointed to by the stack pointer register SP. The data segment used as the stack is called "CSTACK". The initialization module cstartup initializes the stack pointer to point to the end of the CSTACK segment. The stack capacity depends largely on the specific program operation details. If the given stack capacity is too small, the data in the stack will be overwritten and cause a program error; if the given stack capacity is too large, RAM space will be wasted. The ARM core processor supports 5 abnormal working modes, each mode has its own stack. The user should initialize each stack pointer separately in the startup code and perform segment positioning in the linker command file.
The heap is used to store dynamically allocated data. The data segment used as the heap is called "HEAP", and it is included in the application system only when dynamic memory allocation is used. Similar to the CSTACK segment, the size of the HEAP segment depends on the specific application. When using the standard input/output library, the HEAP capacity should be set to meet the requirements for buffering standard input/output, usually 512 bytes.
Variables with explicitly specified addresses will be located in the DATA_AC segment or the DATA_AN segment. The DATA_AC segment is used for data initialized to constants, and the DATA_AN segment is used for variables declared as "__no_init". [page]
1.2 Code segment
The code segment includes startup code, ordinary code, and exception code.
The startup code is located in the ICODE segment, including system startup (cstartup), run initialization (cmain), and system termination (cexit) codes. The ICODE segment must be located in a continuous memory space, and the -P command option cannot be used in the linker command file to locate the ICODE segment. The startup code is called through the reset vector. The
ordinary code is located in the CODE segment, which stores the execution code of ordinary functions. The CODE segment can have a suffix, such as the CODE_I segment, which stores the code initialized by the CODE_ID segment and executed in RAM. Similar to the compiler's treatment of initialized variables, the code is copied from the initialization period in the ROM memory to the RAM memory before execution when it is run. The normal code segment is also related to symbols and debugging information. The
exception vector is located in the INTVEC segment. If an instruction that jumps to the exception handler (such as the B instruction) is used at the exception vector, the exception handler must be within the range that the jump instruction can reach. This problem does not exist when using the PC load instruction (such as the LDR PC instruction).
2 Segment Positioning in Memory
The segments generated by the IAR C compiler need to be positioned in memory by the XLINK linker according to a series of command options in the link command file to ensure the normal operation of the target code. The link command file is a text file with an extension of ".xcl" that contains various XLINK command options.
The most commonly used command options are: CPU command option -C, constant definition command option D, segment positioning command option -Z or -P.
The "-c" command option is used to specify the CPU used by the user system, such as: -carm.
The "-D" command option is used to specify the starting and ending addresses of the memory, such as:
-DROMSTART=40000040
-DROMEND=40006FFF
The "-D" command option can also be used to define the stack length or other constants, such as:
-D_CSTACK——STZE=2048
-D_IRQ_STACK_SIZE=5l2
The "-Z" command option locates in the order in which the segments appear, and specifies the end point of each memory range, such as:
-Z(CONST)MYSEGMENTA,MYSEGMENTB=008000-OFFFFF
Two different types of segments can be located in the same memory area if the range of the second segment is not specified, such as:
-Z(CONST)MYSEGMENTA=008000-0FFFFF
-Z(CODE)MYCODE
The two memory ranges can overlap, allowing segments with different positioning requirements to share part of the memory space, such as:
-Z(CONST)MYSMALLSEGMENT=008000-000FFF
-Z(CONST)MYLARGESEGMENT=008000-OFFFFF
The "-P" command option performs segment positioning in a non-continuous manner, which can make full use of the memory space, such as:
-P(DATA)MYDATA=100000-101FFF, 110000-11lFFF
If the user application system also has a section of RAM located in the memory
0x10F000~Oxl0F7FF, just add this range to the above command:
-P(DATA)MYDATA=100000-10lFFF, 10F00010F7FF, 110000-11lFFF
3 Examples of link command file applications
The role of the linker command file is to inform the XLLINK linker to perform various segment positioning in the memory according to the command options given in the file; due to the wide variety of ARM core processors and the different memory configurations of each processor, the linker command file usually needs to be customized according to the specific application system hardware design to ensure that the code and data do not cross the boundary within the given memory range and cause errors.
The following takes the Philips LPC2148 ARM core processor chip as an example to explain how to customize the XLINK link command file. The LPC2148 has 32 KB on-chip SRAM and 512 KB on-chip Flash, and its memory addresses are as follows.
On-chip Flash: 0x00000000~Ox0007FFFF.
On-chip SRAM: 0x40000000~0x413007FFF.
According to the memory address range of the LPC2148, the XLINK link command file for debugging the application in the on-chip SRAM or in the on-chip Flash can be customized separately.
(1) Link command file for debugging application in on-chip SRAM
[page]
(2) Link command file for debugging applications in on-chip Flash
The above link command file can be used to debug applications in LPC2148 on-chip Flash after appropriate modifications. The main thing is to redefine the memory addresses of the code segment and data segment, and sometimes redefine the length of the stack and heap. Only the modified parts are listed below, and other identical parts are omitted:
4 Conclusion
When developing ARM embedded systems using the IAR EWARM integrated environment, it is necessary to inform the XLINK linker through the link command file how to link and locate the code and data segments generated by the C compiler. Users need to be familiar with the SRAM and Flash memory configuration of the ARM core processor used, and determine which XLINK command options to use based on the actual available address space. Only by using appropriate command options to correctly locate the code and data segments, generate reliable execution code, and finally write the execution code into Flash can the ARM embedded system design be successfully completed; otherwise, even if the written C source program is optimized, it will not play its due role.
Previous article:Design of Embedded Wireless Video Monitoring System
Next article:Programming and Application of LPC2000 Series CAN Acceptance Filter
Recommended ReadingLatest update time:2024-11-16 18:05
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Keysight Technologies' Thanksgiving Month Oscilloscope Award Photos
- Serial communication, 1-wire, TWI communication, what are the differences between these three communication methods? Who knows about it? Popular...
- How to filter out 50HZ power frequency interference introduced by active filtering
- Is it the inverter or the integrator circuit?
- I have lived for more than 30 years and my brushing posture is wrong!
- Questions about the MSP430 microcontroller architecture: MSP430, MSP430X, MSP430Xv2
- Today at 10:00 AM, live broadcast with prizes: [Infineon Intelligent Motor Drive Solution]
- Technical staff salary
- RISC-V MCU Development Practice (Part 4): Stepper Motor
- The problem that PL2303 driver cannot be used under win10