Relocating Code in ROM
rodata_array bss_i data_i
Variable
Table 14-2. Allocating Objects (continued)
Section
.rodata
.bss
.data
0x00003938
0x40000508
0x400004d0
Address
14.7 Relocating Code in ROM
To place data and code in a specific memory location there are two general steps that must be performed.
• Use pragma compiler directives to tell the compiler which part of the code is going to be relocated.
• Tell the linker where the code will be placed within the memory map using LCF definitions.
14.7.1 Relocating Function in ROM
To put code in a specific memory section it is needed first to create the section using the section pragma directive. In the following listing a new section called .
romsymbols
is created.
All the content in this section is going to be referenced in the LCF with the name
.romsymbols
. After defining a new section you can place code in this section by using the
__declspec()
directive.
In the following listing,
__declspec() directive is used to tell the compiler that function funcInROM() is going to be placed in section romsymbols
.
Create a stationary project for any target and add the following code to your main.c
file before the main() function and have a call to this function.
Listing 14-13. Code to add in the main.c
#pragma section RX ".romsymbols" data_mode=far_abs
__declspec(section ".romsymbols") void funcInROM(int flag); //Function Prototype
void funcInROM(int flag){ if (flag > 0)
CodeWarrior Development Studio for Advanced Packet Processing Build Tools Reference Manual, Rev.
10.2, 01/2016
270 Freescale Semiconductor, Inc.
Chapter 14 Linker
{ flag ++;
}
}
14.7.2 Placing Code in ROM
You have just edited a source file to tell the compiler which code will be relocated. Next, the LCF needs to be edited to tell the linker the memory addresses where these sections are going to be allocated.
First you need to define a new Microcontroller memory segment where new sections will be allocated.
You can have just one memory segment for all the new sections or one segment for each section.
14.7.2.1 Create New ROM Segment
Below you can find the memory segment of a LCF. Notice that the segment internal_flash has been edited and its length has been reduced by
0x10000
from its original size. This memory space is taken to create the new segment. In the following listing the new segment is called myrom
, it will be located next to segment internal_flash
and its length is going to be
0x10000
. You can calculate the address where segment code ends by adding its length plus the origin address.
Edit your LCF as shown in the following listing. Ensure you edit ROM target lcf.
Listing 14-14. Memory Segment of LCF
MEMORY
{
resetvector: org = 0x00000000, len = 0x00000008
init: org = 0x00000010, len = 0x0000FFF0
exception_handlers_p0: org = 0x00010000, len = 0x00010000
internal_flash: org = 0x00030000, len = 0x001C0000
myrom: org = 0x00220000, len = 0x00010000
internal_ram: org = 0x40000000, len = 0x0007C000
heap : org = 0x4007C000, len = 0x00002000 /* z7_0 Heap start location */
CodeWarrior Development Studio for Advanced Packet Processing Build Tools Reference Manual, Rev.
10.2, 01/2016
Freescale Semiconductor, Inc.
271