11.5.3 Run-Time Library Support

In order to initialize variables in data memory, the data initialization template must be processed at start-up, before the application's main function takes control. For C programs, this task is performed by C start-up modules in the runtime library. Assembly language programs can also use the C start-up modules by linking with libc99-pic30-elf.a.

Multiple versions of the start-up modules are contained within the runtime library. The linker will select a startup module based on commands in the linker script. For example:

CRT0_STARTUP(crt0_standardaa.o)
CRT1_STARTUP(crt1_standardaa.o)

For each device, two start-up modules are specified: a primary module (CRT0) and an alternate module (CRT1).

To utilize a start-up module, the application must allow the run-time library to take control at device Reset. This happens automatically for C programs. The application’s main() function is invoked after the start-up module has completed its work. Assembly language programs should use the following naming conventions to specify which routine takes control at device Reset.

Table 11-3. Main Entry Points
Main Entry NameDescription
__resetTakes control immediately after device Reset
_mainTakes control after the start-up module completes its work

Note that the first entry name (__reset) includes two leading underscore characters. The second entry name (_main) includes only one leading underscore character. The linker scripts construct a GOTO __reset instruction at location 0 in program memory, which transfers control upon device Reset.

The primary start-up module is linked by default and performs the following:

  1. The stack pointer (W15) and stack pointer limit register (SPLIM) are initialized, using values provided by the linker or a custom linker script. For more information, see Stack Allocation.
  2. The data initialization template in section .dinit is read, causing all uninitialized sections to be cleared, and all initialized sections to be initialized with values read from program memory.
  3. If the application has defined user_init functions, section .user_init is called.
  4. The function main is called with no parameters.
  5. If main returns, the processor will reset.

    The alternate start-up module is linked when the --no-data-init option is specified. It performs the same operations, except for step (3), which is omitted. The alternate start-up module is much smaller than the primary module, and can be selected to conserve program memory if data initialization is not required.

    Source code for both modules is provided in the src directory of the MPLAB XC32 C Compiler for PIC32A MCU installation directory. The start-up modules may be modified if necessary. For example, if an application requires main to be called with parameters, a conditional assembly directive may be switched to provide this support.