Startup code and interrupt handlers

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Startup code and interrupt handlers

10,064 Views
lpcware-support
Senior Contributor I

Overview

When you create a project using the LPCXpresso IDE project wizard, the startup code generated (cr_startup_*.c or cr_startup_*.cpp) includes a vector table containing a set of default interrupt handlers, dependent upon the target MCU. For example, the following is taken from the vector table generated for the LPC11U6x:

extern void (* const g_pfnVectors[])(void);
__attribute__ ((section(".isr_vector")))
void (* const g_pfnVectors[])(void) = {
    &_vStackTop,                     // The initial stack pointer
    ResetISR,                        // The reset handler
    NMI_Handler,                     // The NMI handler
    HardFault_Handler,               // The hard fault handler
    0,                               // Reserved
    0,                               // Reserved
    0,                               // Reserved
    0,                               // Reserved
    0,                               // Reserved
    0,                               // Reserved
    0,                               // Reserved
    SVC_Handler,                     // SVCall handler
    0,                               // Reserved
    0,                               // Reserved
    PendSV_Handler,                  // The PendSV handler
    SysTick_Handler,                 // The SysTick handler

    // LPC11U6x specific handlers
    PIN_INT0_IRQHandler,             //  0 - GPIO pin interrupt 0
    PIN_INT1_IRQHandler,             //  1 - GPIO pin interrupt 1
    PIN_INT2_IRQHandler,             //  2 - GPIO pin interrupt 2
     :
     :

The first entry is the value to be loaded automatically into the stack pointer register (r13) at reset. (The symbol normally used for this is generated by the linker script generated by LPCXpresso.)

The second entry is the address of the reset handler, ResetISR, which is located elsewhere in the startup file. This address will be loaded into the program counter at reset (i.e. it will be where the MCU starts executing from).

There are then a set of handlers, starting with NMI_Handler and ending with SysTick_Handler which are the standard default handlers for Cortex-M. The exact list will depend upon which Cortex part is being used, Cortex-M3/M4 implement more than Cortex-M0/M0+. For more details, please see ARM's Cortex documentation.

These default handlers are defined higher up the startup file as weak:

WEAK void SVC_Handler(void);

so that if your main application code provides a (non-weak) implementation of a handler function with exactly the same name, this will be used instead of the default version contained in the startup code.

Finally, the vector table contains a list of MCU specific handlers. The exact number of these, and the interrupts that these implement will depend upon what MCU is being used. For details, please see the NXP User Manaual for the MCU that you are using.

Forward declaration of the MCU specific interrupt handlers are again provided for these higher up the startup file, for example:

void PIN_INT0_IRQHandler (void) ALIAS(IntDefaultHandler);

These are weak and also aliased to the IntDefaultHandler lower down the startup code - which is a "forever" loop.

Again, if your main application code provides a (non-weak) implementation of a particular interrupt handler function with exactly the same name, this will be used, rather than the IntDefaultHandler from the startup code.

Interrupt Handlers in C++ applications

One thing to be careful of is that if your application is a C++ one, then any interrupt handlers defined in C++ files within in your main application will need to have C linkage rather than C++ linkage. To do this, make sure that you are using extern "C" { .... } around the interrupt handler within your main application code.

For more details, please see you favorite C++ text book, or an internet search will bring up lots of useful links - for example:


http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B#Linking_C_an...

Labels (1)
0 Replies