knick
knick

Reputation: 971

ISR in .cpp file not found

I have an embedded project containing both c files (drivers from the SDK) and c++ (.cpp) files (my own code). I'm trying to set up an interrupt service routine. I've found it works fine if the ISR is in a .c file. However, the exact same function moved into a .cpp file does not work.

In MK64F12_Vector.s;

_vectors:
    ...
    .word PORTD_MyIRQHandler

Some related initialization code that is called once at start-up;

CLOCK_EnableClock(kCLOCK_PortD); 
EnableIRQ(PORTD_IRQn);
PORT_SetPinInterruptConfig(PORTD, 0, kPORT_InterruptFallingEdge);

In .c file;

void PORTD_MyIRQHandler()
{
  while (1) ; // put breakpoint here
}

The above works fine if PORTD_MyIRQHandler() is in a .c file. But if I move it to a .cpp file (where I want it!) I get the error below. Note I am just copying & pasting it without changing anything.

(.vectors+0x138): undefined reference to `PORTD_MyIRQHandler'

Is there something else I need to do to get it to work? A compiler or linker option perhaps? I've tried the following without success;

__attribute__((interrupt)) void PORTD_MyIRQHandler() {..}

#pragma interrupt
void PORTD_MyIRQHandler() {..}

I am using Segger Embedded Studio with gcc.

Upvotes: 0

Views: 156

Answers (1)

Sami Sallinen
Sami Sallinen

Reputation: 3496

Try

extern "C" void PORTD_MyIRQHandler(){
}

C++ adds characters to the fuction name that depend on the input and return value types of the function.

This feature, type name mangling, exists to avoid linking functions with wrong kinds of parameters and to allow function overloading. Using extern "C" disables that.

Upvotes: 2

Related Questions