The MPLAB® XC32 C compiler provides several options to reserve both the program memory and the data memory.
1 The -mreserve option can be used to block certain segments of the memory, so that it won’t be used by the compiler.
Under MPLAB X IDE, this option can be specified by going to File>Project Properties>xc32>xc32-ld>Option Categories: General>Additional driver options and adding the -mreserve options based on your requirement. Some examples:
-mreserve=data@0xA0000100: 0xA0000200 reserves data memory space from 0xA0000100 - 0xA0000200. This space won’t be used by the compiler.
-mreserve=prog@0x1d002000:0x1d0020FF reserves program memory space from 0x1d002000 - 0x1d0020FF. This space won’t be used by the compiler.
2 Modify the linker script to reserve memory:
Copy the default linker script of the target device to the project folder.
Default linker scripts can be found in the compiler installation directory,
e.g., <Compiler DIR>\pic32mx\lib\proc
To reserve program memory space, modify the linker script as follows:
.resv 0x9D002000 (NOLOAD):
{
. += 0x100;
} > kseg0_program_mem
This will reserve the space in KSEG0 program memory, from 0x9D002000 - 0x9D002100. This space won’t be used by the compiler.
To reserve data memory space, modify the linker script as follows:
.resv 0xA0002000 (NOLOAD):
{
. += 0x100;
} > kseg1_data_mem
This will reserve the space in KSEG1 data memory, from 0xA0002000 - 0xA0002100. This space won’t be used by the compiler.
3 Modify the linker script to reserve memory, when the memory to be reserved is at the beginning or end of memory:
If the portion of memory is at the beginning or at the end of a memory region, the beginning and/or the length of the memory region should be decreased by the needed amount.
E.g., ram (rwx) : ORIGIN = 0x20400000, LENGTH = 0x600000
Let’s assume you want to reserve the space from 0x20400000 to 0x20500000. The new definition of the ram memory region in the linker script fiile will be: ram (rwx) : ORIGIN = 0x20500000, LENGTH = 0x500000
We recommend using mreserve to reserve memory, as it is easier to specify under linker options and easier to maintain. But, based on your requirement, you may choose one of the above 2 methods for reserving memory in Data space or Program space.
For more on mreserve and noload options refer to the section “PIC32 DEVICE-SPECIFIC OPTIONS” and “OUTPUT SECTION TYPE” in the “MPLAB XC32 C/C++ Compilers User's Guide”.