3.5.18 How Do I Customize the C Runtime Startup Code?

Some applications may require an application-specific version of the C runtime startup code. For instance, you may want to modify the startup code for an application loaded by a bootloader.

To customize the startup code for your application, you will first need to obtain the source code for the default startup code.

  1. Unzip the pic32m-libs.zip file located in the<install-directory>/pic32-libs/ directory. If the tar files within this archive are not automatically expanded by your archive utility, you might need to run tar -xvf or an alternative application on these tar files. The runtime startup source code can then be found at pic32m-libs/pic32m-libs/pic32m-libs/libpic32/startup/crt0.S in the unzipped directory, although note that some archive utilities might expand the files in directories with slightly different names.
  2. Make a copy of the unarchived crt0.S file, rename it, and add it to your project.
  3. Change your MPLAB X project to exclude the default startup code by enabling the Do not link startup code checkbox in the Libraries Option category under xc32-ld , as shown below. When you build your project, the MPLAB X IDE will build your new application-specific copy of the startup code rather than linking in the default code.
    Figure 3-1. Startup Code Properties Setting
  4. You can now edit the assembly code in your new copy of the runtime startup file. The default source code uses macros defined in the device-specific header files, which are included by <xc.h>. These macros define various device specific behavior. Be sure to take this device-specific code into account when customizing your copy of the code.
    Table 3-1. Device-Specific Macros
    Device-Specific MacroDescription
    __PIC32_SRS_SET_COUNTDefined to the number of Register Sets implemented on the device. The default startup code uses this value to determine how many register sets to use for $GP-register initialization.
    __PIC32_HAS_L1CACHEDefined if the device features an L1 cache.
    __PIC32_HAS_MIPS32R2Defined if the device supports the MIPS32r2 Instruction Set.
    __PIC32_HAS_MICROMIPSDefined if the device supports the microMIPS compressed Instruction Set.
    __PIC32_HAS_DSPR2Defined if the device supports the DSPr2 Application-Specific Extension.
    __PIC32_HAS_FPU64Defined if the device supports the single- and double-precision hardware Floating Point Unit.
    __PIC32_HAS_SSXDefined if the device does not require initialization of the bus matrix registers in order to support execution from data memory.
    __PIC32_HAS_MMU_MZ_FIXEDDefined if the device features a Memory Management Unit that should be pre-initialized to a standard SQI and EBI mapping.
    __PIC32_HAS_INIT_DATADefined if the device requires data initialization by copying from a template located in Flash to RAM.