Interrupt handlers from scratch

19 Jan 2010

I am writing a more fully-functioned ADC driver that gets more closer to the hardware so I can work at faster speeds, use interrupts, use burst mode, dma etc.

In some sample code I downloaded from NXP there is a function, NVIC_EnableIRQ(ADC_IRQn). This seems to work for me with the MBED compiler but I have no idea where this function/macro is defined. I just want to understand it a bit better.

Also, nothing runs after ther point where I enable the interrupt. I am guessing this means that I got the interrupt handler name wrong and the interrupt vector is unset. I used ADC_IRQHandler(void). What name should I be using? Where are they defined? I couldn't find them in LPC17xx.h.

19 Jan 2010 . Edited: 19 Jan 2010

1) NVIC_EnableIRQ() is defined in core_cm3.h which is included from LPC17xx.h.

2) To make sure the compiler sees your handler, you need to declare it as extern:

extern "C" void ADC_IRQHandler()
{
  // handle interrupt
} 

3) Alternatively, you can use NVIC_SetVector (declared in cmsis_nvic.h), which relocates exception vectors to RAM and patches the vector with your function:

NVIC_SetVector(ADC_IRQn, (uint32_t)ADC_IRQHandler);

This option also works for 2368 model.

Here's how I did it for the USB host sample:

#ifdef TARGET_LPC2368
    NVIC->IntSelect &= ~(1 << USB_IRQn); /* Configure the ISR handler as IRQ */
    /* Set the vector address */
    NVIC_SetVector(USB_IRQn, (USB_INT32U)USB_IRQHandler);
#endif
    NVIC_SetPriority(USB_IRQn, 0);       /* highest priority */
    /* Enable the USB Interrupt */
    NVIC_EnableIRQ(USB_IRQn);

Edit: IRQ handler names are defined in the startup_LPC17xx.s file, the source of which is not provided but you can find a very close (if not the same) copy in the CMSIS package. I previously posted the list of all handler names here.

19 Jan 2010 . Edited: 19 Jan 2010

Thanks Ivor. That's very helpful :-)

I assume

NVIC_SetVector(USB_IRQn, (USB_INT32U)USB_IRQHandler)

should be

NVIC_SetVector(USB_IRQn, (uint32_t)USB_IRQHandler)
19 Jan 2010

Yes, it was typedef'ed in the USB host sources so I used that.

08 Oct 2015

https://developer.mbed.org/users/mbed_official/code/mbed-src/file/a11c0372f0ba/targets/cmsis/TARGET_NXP/TARGET_LPC176X/TOOLCHAIN_GCC_ARM/startup_LPC17xx.S

The vectors are defined in the startup code which is an assembly file. You can find it at the above link.