3.5.17 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:

  1. Start with the default startup code, a copy of which is located in the pic32m-libs.zip file located at:

    <install-directory>/pic32-libs/

    Once the file is unzipped, the source code can be found at:

    pic32m-libs/libpic32/startup/crt0.S

  2. Make a copy of this 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” under XC32 xc32-ld Option categories: Libraries page 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 crt0.S 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 Macro Description
    __PIC32_SRS_SET_COUNT Defined 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_L1CACHE Defined if the device features an L1 cache.
    __PIC32_HAS_MIPS32R2 Defined if the device supports the MIPS32r2 Instruction Set.
    __PIC32_HAS_MICROMIPS Defined if the device supports the microMIPS compressed Instruction Set.
    __PIC32_HAS_DSPR2 Defined if the device supports the DSPr2 Application-Specific Extension.
    __PIC32_HAS_FPU64 Defined if the device supports the single- and double-precision hardware Floating Point Unit.
    __PIC32_HAS_SSX Defined if the device does not require initialization of the bus matrix registers in order to support execution from data memory.
    __PIC32_HAS_MMU_MZ_FIXED Defined if the device features a Memory Management Unit that should be pre-initialized to a standard SQI and EBI mapping.
    __PIC32_HAS_INIT_DATA Defined if the device requires data initialization by copying from a template located in Flash to RAM.