Skip to main content Skip to local navigation

Microcontroller Interrupts and C++

Microcontroller Interrupts and C++

As part of my examination of C++ programming on microcontrollers, I've run into an issue with implementing Interrupt Service Routines on the LPC804 and LPC802 microcontrollers.  These micros are based on the ARM Cortex M0+ core, are small, solderable and suitable for teaching and project development with undergraduate engineering and computer science students.  I'm looking at developing with C++ on these microcontrollers as the language is potentially better than C.

It turns out that the ISO C++14 compiler in MCUXpresso requires that interrupt service routines be wrapped in an "extern C" statement.  Here is it's implementation for a pushbutton switch that is tied to input/output 20 (GPIO 20 or PIO0_20) on the LPC804.

// ---------------------------------------
// ISR for the GPIO Interrupt
//
// A GPIO interrupt has been detected. 
//
// Could also check GPIO directly with
//if(GPIO->B[0][20] & 1)//GPIO 20 is the pushbutton. Is GPIO 20 a 1?
//
// for C++ https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B#Linking_C_and_C.2B.2B_code
// ---------------------------------------

extern "C" {
void PIN_INT0_IRQHandler(void)
{
// was an IRQ requested for Channel 0 of GPIO INT?
if (PINT->IST & (1<<0))
{
// remove the any IRQ flag for Channel 0 of GPIO INT
PINT->IST = (1<<0);
// TOggle the LED
GPIO->NOT[0] = (1UL<<LED_USER2);//
}
else
{
asm("NOP");// Place a breakpt here if debugging.
}
return;
}
}

for more on this see the NXP forum and the link they pointed me to: https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B#Linking_C_and_C.2B.2B_code