embeddedmicrocontrollerpicbootloadermicrochip

How to change codeoffset?


I know how to change the codeoffset on MPLAB XC8 easily just by going to XC8 Linker --> Additional options -- > Codeoffset

How do I change it on MPLAB V8.92 with C18 compiler? I am using PIC18F87J11 if that's helpful. I opened the linker file and changed the following but it gave me error. I don't know if I am suppose to do that or not, but all I really want is to change the codeoffset so that the hex file starts at address 10000. I have a bootloader that occupies the following memory space 0-10000. I want the application code to start at address 10000. I can do this fine with MPLAB XC8 but I don't know how to do it with MPLAB 8. Most of my projects are written with MPLAB 8 so converting to MPLAB XC8 would take some time.

Linker file changed from

CODEPAGE NAME=page START=0x0 END=0x1FFF7

To

CODEPAGE NAME=page START=0x10000 END=0x1FFF7

Build Error:

Error - section '_entry_scn' can not fit the absolute section. Section '_entry_scn' start=0x00000000, length=0x00000006

Thanks!


Solution

  • A majority of this information was taken from the microchip forum and reproduced here for convenience. There is also a lot of great information on how to utilize the linker in Microchip's MPLINK Object Linker User's Guide.

    Copy the start up file c018i.c (or whatever you are using) from C18's startup directory to your project file and add it to your project.

    Edit the line:

    #pragma code _entry_scn=0x000000
    

    to

    #pragma code _entry_scn=0x010000
    

    Next copy the linker file 18f87j11_g.lkr to your project and add that to your project.
    Edit the linker and comment out the default start up file so it will use the local copy:

    #IFDEF _CRUNTIME
        #IFDEF _EXTENDEDMODE
            //FILES c018i_e.o
            FILES clib_e.lib
            FILES p18f87j11_e.lib
    
        #ELSE
            //FILES c018i.o
            FILES clib.lib
            FILES p18f87j11.lib
        #FI
    

    If you are writing a bootloader, you will need to have a fixed place for the interrupt vectors so get the bootloader code to vector them to a fixed location and then add the following:

    #pragma code highVector=0x10008
    void HighVector(void) {
        _asm
        goto high_isr
        _endasm
    }
    
    #pragma code lowVector=0x10018
    void LowVector(void) {
        _asm goto low_isr _endasm
    }
    
    #pragma code // Return to default code section
    

    Then....

    #pragma interrupt high_isr
    void high_isr(void)
    {
        ...
    }
    

    Finally, in the linker you also have to protect the bootloader memory area by adding this line:

    CODEPAGE NAME=bootloader START=0x000000 END=0X00FFFF PROTECTED
    

    And modifying the "page" to start the program where you want it, like so:

    CODEPAGE NAME=page START=0x100000 END=0X01FFF7
    

    I was able to get a simple program to compile and link using the above steps. You can see in the image of my map file that the code is properly offset.

    Screenshot of map file