USB Library
Changing the memory footprint of the boot loader

This section covers how to modify the size of the HID boot loader. This can be useful when adding features to the boot loader that increase the size beyond the default example or if a version of the compiler is used that doesn't provide a sufficient level of optimizations to fit the default boot loader. The boot loaders provided by default assume full optimizations and may not work with compilers that don't have access to full optimizations. 

Please read all of the other topics in the PIC24F boot loader section before proceeding in this topic. This topic will show where the modifications need to be made and how they need to match up, but will not describe what the sections that are being modified are or how they are implemented. This information is in previous sections. 

There are three places that require corresponding changes: the boot loader linker script, the application linker script, and the boot loader code. You may wish to make copies of the original files so that you preserve the original non-modified files. 

In the following examples we will be increasing the size of the boot loader from 0x1400 to 0x2400 in length. 

First start by determining the size that you want the boot loader to be. This must be a multiple of an erase page. On many PIC24F devices there is a 512 instruction word erase page (1024 addresses per page). Please insure that the address you select for the end of the boot loader corresponds to a page boundary. There are several ways to determine the size of the boot loader application. Below is an example of one method. 

1) Remove the boot loader linker script provided if it is causing link errors due either to optimization settings or added code. 

2) Build the project 

3) Open the memory window and find the last non-blank address in the program memory space. 

4) Find the next flash erase page address after this address. Add any additional buffer room that you might want for future boot loader development, growth, or changes. Use this address as your new boot loader end address. 

Once the end address of the boot loader is known, start by modifying the boot loader linker script program memory region to match that change. The boot loader linker script can either be found in the folder containing the boot loader project file or in a folder that is specified for boot loader linker scripts. In the linker script find the memory regions.

MEMORY
{
data (a!xr) : ORIGIN = 0x800, LENGTH = 0x4000
reset : ORIGIN = 0x0, LENGTH = 0x4
ivt : ORIGIN = 0x4, LENGTH = 0xFC
aivt : ORIGIN = 0x104, LENGTH = 0xFC
program (xr) : ORIGIN = 0x400, LENGTH = 0x2000
config4 : ORIGIN = 0x2ABF8, LENGTH = 0x2
config3 : ORIGIN = 0x2ABFA, LENGTH = 0x2
config2 : ORIGIN = 0x2ABFC, LENGTH = 0x2
config1 : ORIGIN = 0x2ABFE, LENGTH = 0x2
}

Change the LENGTH field of the program memory section to match the new length. Note that this is length and not the end address. To get the end address, please add LENGTH + ORIGIN. 

Next, locate the __APP_IVT_BASE definition in the linker file. Change this to equal the end address of your boot loader.

__APP_IVT_BASE = 0x2400;

Once the length of the boot loader is changed, you will need to make similar changes in the application boot loader linker script. The application boot loader linker scripts are typically found in a folder with the boot loader project. In the application linker file, locate the memory regions section. In this section there are three items that need to change.

  1. The first is the ORIGIN of the app_ivt section. This needs to be modified to match the new end address of the boot loader.
  2. Second, move the ORIGIN of the program memory section to the ORIGIN of app_ivt + the LENGTH of the app_ivt section so that the program memory starts immediately after the app_ivt section.
  3. Last, change the LENGTH field of the program section so that it goes to the end of the program memory of the device. Remember that the LENGTH field is the length starting from the origin and not the end address. An easy way to make sure that this address is correct is by just subtracting off from the LENGTH the same amount that was added to the ORIGIN.

MEMORY
{
data (a!xr) : ORIGIN = 0x800, LENGTH = 0x4000
reset : ORIGIN = 0x0, LENGTH = 0x4
ivt : ORIGIN = 0x4, LENGTH = 0xFC
aivt : ORIGIN = 0x104, LENGTH = 0xFC
app_ivt : ORIGIN = 0x2400, LENGTH = 0x110
program (xr) : ORIGIN = 0x2510, LENGTH = 0x286E8
config4 : ORIGIN = 0x2ABF8, LENGTH = 0x2
config3 : ORIGIN = 0x2ABFA, LENGTH = 0x2
config2 : ORIGIN = 0x2ABFC, LENGTH = 0x2
config1 : ORIGIN = 0x2ABFE, LENGTH = 0x2
}

The final changes that needs to be made are in the boot loader code itself. Open up the boot loader project.

  1. Find the ProgramMemStart definition in the main.c file. Change the start address to match the new address.

#define ProgramMemStart 0x00002400

  1. Next find the #ifdef section that applies to the device that you are working with. This section will contain definitions used by the boot loader to determine what memory is should erase and re-write.

#if defined(__PIC24FJ256GB110__) || defined(__PIC24FJ256GB108__) || defined(__PIC24FJ256GB106__)
#define BeginPageToErase 5 //Bootloader and vectors occupy first six 1024 word (1536 bytes due to 25% unimplemented bytes) pages
#define MaxPageToEraseNoConfigs 169 //Last full page of flash on the PIC24FJ256GB110, which does not contain the flash configuration words.
#define MaxPageToEraseWithConfigs 170 //Page 170 contains the flash configurations words on the PIC24FJ256GB110. Page 170 is also smaller than the rest of the (1536 byte) pages.
#define ProgramMemStopNoConfigs 0x0002A800 //Must be instruction word aligned address. This address does not get updated, but the one just below it does:
//IE: If AddressToStopPopulating = 0x200, 0x1FF is the last programmed address (0x200 not programmed)
#define ProgramMemStopWithConfigs 0x0002ABF8 //Must be instruction word aligned address. This address does not get updated, but the one just below it does: IE: If AddressToStopPopulating = 0x200, 0x1FF is the last programmed address (0x200 not programmed)
#define ConfigWordsStartAddress 0x0002ABF8 //0x2ABFA is start of CW3 on PIC24FJ256GB110 Family devices
#define ConfigWordsStopAddress 0x0002AC00

  1. Modify the BeginPageToErase to indicate which page is the first page it should erase. This will be the ProgramMemStart/Page Size. In this case we are starting at 0x2400 and each page is 0x400 so this should now be 9.
#define BeginPageToErase 9
  1. Locate the start of the main() function. In the first few lines of code there is a check to determine of the code should stay in the boot loader or jump to the application code. Change the address in the "goto" statement to match the new end of the boot loader and start of the application.
__asm__("goto 0x2400"); 

This should be all of the changes required in order to change the size of the HID boot loader. 

Please note that since the boot loader and the application code are developed as two separate applications, they do not need to use the same optimization settings.

MLA - USB Library Help Version : 2.16
http://www.microchip.com/mla