EmbeddedRelated.com
Forums

How handle Cortex-M0 Interrupts on IAR using C++?

Started by Unknown August 30, 2011
Hello everybody,

This is my first email in here, hope we can help each-other.

I'm using IAR 6.21 compiler and a LPC11C14 IAR's dev board, doing my firsts test with C++.
Also I'm simulating UART interrupts in the C-SPY simulator, all working great on C, so the problem is not the macros or whatever.

I search on the web how to handle interrupts in C++, and found that:
http://www.eetimes.c...nterrupts-in-C-

So I did this on my code:

in uart.h:
class uart
{
...
public:
static void UART0_IRQHandler( void );
}

in uart.c:
void uart::UART0_IRQHandler( void );
{
// breakpoint reading the UART receive register
}

And the vector is defined correctly in cstartup_m.s and in the cstartup_m.c (I've tested with either, one per time obviously)
the vector worked fine in C, so it's not the problem.

The problem is my static member is not even linked, the linker uses the default function provided in the vector definition source code.
I tried to figure out what I'm doing wrong and made some other tests also, but no way to establish a connection between the uart interrupt vector and mu static member function.
It works if I "mix" C with C++ using "extern C" definitions, but I believe that should be a "correct" way of doing this.

What I'm doing wrong?
Is this the best way of deal with interrupts in C++?
Thanks in advance buddies.
AndrRairan.

An Engineer's Guide to the LPC2100 Series

Am 30.08.2011 22:41, schrieb AndrRairan:

> It works if I "mix" C with C++ using "extern C" definitions, but I believe that should be a "correct" way of doing this.

This _is_ the correct way. If you refer from C or assembly to a C++
function you have to use extern "C".
(Or try to get the real name of the function with C++ name mangling).
--
42Bastian
+
| http://www.sciopta.com
| Fastest direct message passing kernel.
| IEC61508 certified.
+
This is the framework that I use with gcc toolchain:

class C_ADC {
private:

static void IRQ_Handler();

};

_irq_ void C_ADC::IRQ_Handler()

{

.
.
.
}

void C_ADC::Init(uint32_t ADC_Clk)

{

.
.
.
.
.
installVector(VIC_CH18_ADC0, (pfunction_t)&C_ADC::IRQ_Handler, 0);

}

--- In l..., AndrRairan wrote:
>
> Hello everybody,
>
> This is my first email in here, hope we can help each-other.
>
> I'm using IAR 6.21 compiler and a LPC11C14 IAR's dev board, doing my firsts test with C++.
> Also I'm simulating UART interrupts in the C-SPY simulator, all working great on C, so the problem is not the macros or whatever.
>
> I search on the web how to handle interrupts in C++, and found that:
> http://www.eetimes.c...nterrupts-in-C-
>
> So I did this on my code:
>
> in uart.h:
> class uart
> {
> ...
> public:
> static void UART0_IRQHandler( void );
> }
>
> in uart.c:
> void uart::UART0_IRQHandler( void );
> {
> // breakpoint reading the UART receive register
> }
>
> And the vector is defined correctly in cstartup_m.s and in the cstartup_m.c (I've tested with either, one per time obviously)
> the vector worked fine in C, so it's not the problem.
>
> The problem is my static member is not even linked, the linker uses the default function provided in the vector definition source code.
> I tried to figure out what I'm doing wrong and made some other tests also, but no way to establish a connection between the uart interrupt vector and mu static member function.
> It works if I "mix" C with C++ using "extern C" definitions, but I believe that should be a "correct" way of doing this.
>
> What I'm doing wrong?
> Is this the best way of deal with interrupts in C++?
>
>
> Thanks in advance buddies.
> AndrRairan.
>

>And the vector is defined correctly in cstartup_m.s and in the cstartup_m.c (I've tested with either, one per time obviously)
>the vector worked fine in C, so it's not the problem.

hi.
can you tell me how you defined the static member function URT0_IRQHandler in the cstartup_m.s and cstartup_m.c ?
can you show me the code?
I have the same problem on LPC1768. I don't know how to add the static member function to vector table.
thanks
I created a cstartup_m.cpp with all vectors and just register this vector in the stance.

On Oct 23, 2011, at 7:19 AM, l...@yahoo.com wrote:

> >And the vector is defined correctly in cstartup_m.s and in the cstartup_m.c (I've tested with either, one per time obviously)
> >the vector worked fine in C, so it's not the problem.
>
> hi.
> can you tell me how you defined the static member function URT0_IRQHandler in the cstartup_m.s and cstartup_m.c ?
> can you show me the code?
> I have the same problem on LPC1768. I don't know how to add the static member function to vector table.
> thanks
>



_irq_ does not exist on IAR for Cortex-M
On Aug 31, 2011, at 1:04 PM, Mark wrote:

>
> This is the framework that I use with gcc toolchain:
>
> class C_ADC {
>
> private:
>
> static void IRQ_Handler();
>
> };
>
> _irq_ void C_ADC::IRQ_Handler()
>
> {
>
> .
> .
> .
> }
>
> void C_ADC::Init(uint32_t ADC_Clk)
>
> {
>
> .
> .
> .
> .
> .
> installVector(VIC_CH18_ADC0, (pfunction_t)&C_ADC::IRQ_Handler, 0);
>
> }
>
> --- In l..., AndrRairan wrote:
> >
> > Hello everybody,
> >
> > This is my first email in here, hope we can help each-other.
> >
> > I'm using IAR 6.21 compiler and a LPC11C14 IAR's dev board, doing my firsts test with C++.
> > Also I'm simulating UART interrupts in the C-SPY simulator, all working great on C, so the problem is not the macros or whatever.
> >
> > I search on the web how to handle interrupts in C++, and found that:
> > http://www.eetimes.c...nterrupts-in-C-
> >
> > So I did this on my code:
> >
> > in uart.h:
> > class uart
> > {
> > ...
> > public:
> > static void UART0_IRQHandler( void );
> > }
> >
> > in uart.c:
> > void uart::UART0_IRQHandler( void );
> > {
> > // breakpoint reading the UART receive register
> > }
> >
> > And the vector is defined correctly in cstartup_m.s and in the cstartup_m.c (I've tested with either, one per time obviously)
> > the vector worked fine in C, so it's not the problem.
> >
> > The problem is my static member is not even linked, the linker uses the default function provided in the vector definition source code.
> > I tried to figure out what I'm doing wrong and made some other tests also, but no way to establish a connection between the uart interrupt vector and mu static member function.
> > It works if I "mix" C with C++ using "extern C" definitions, but I believe that should be a "correct" way of doing this.
> >
> > What I'm doing wrong?
> > Is this the best way of deal with interrupts in C++?
> >
> >
> > Thanks in advance buddies.
> > AndrRairan.
> >
>
>



Most likely this is because of name mangling.
You are writing a C++ function so the name generated will be mangled and the
linker will not match it to the required name which is defined in the arm
startup code when the vector table is established.

Try using extern "C" on the definition.

Alternatively you need to overwrite the vector entry for the ADC ISR handler
with the address of your function, this is easy enough but it assumes you
have relocated the vector table to RAM first.

Regards

Phil.

-----Original Message-----
From: l... [mailto:l...] On Behalf Of
AndrRairan
Sent: 23 October 2011 18:42
To: l...
Subject: Re: [lpc2000] How handle Cortex-M0 Interrupts on IAR using C++?

_irq_ does not exist on IAR for Cortex-M
On Aug 31, 2011, at 1:04 PM, Mark wrote:

>
> This is the framework that I use with gcc toolchain:
>
> class C_ADC {
>
> private:
>
> static void IRQ_Handler();
>
> };
>
> _irq_ void C_ADC::IRQ_Handler()
>
> {
>
> .
> .
> .
> }
>
> void C_ADC::Init(uint32_t ADC_Clk)
>
> {
>
> .
> .
> .
> .
> .
> installVector(VIC_CH18_ADC0, (pfunction_t)&C_ADC::IRQ_Handler, 0);
>
> }
>
> --- In l..., AndrRairan wrote:
> >
> > Hello everybody,
> >
> > This is my first email in here, hope we can help each-other.
> >
> > I'm using IAR 6.21 compiler and a LPC11C14 IAR's dev board, doing my
firsts test with C++.
> > Also I'm simulating UART interrupts in the C-SPY simulator, all working
great on C, so the problem is not the macros or whatever.
> >
> > I search on the web how to handle interrupts in C++, and found that:
> > http://www.eetimes.c...nterrupts-in-C-
> >
> > So I did this on my code:
> >
> > in uart.h:
> > class uart
> > {
> > ...
> > public:
> > static void UART0_IRQHandler( void );
> > }
> >
> > in uart.c:
> > void uart::UART0_IRQHandler( void );
> > {
> > // breakpoint reading the UART receive register
> > }
> >
> > And the vector is defined correctly in cstartup_m.s and in the
cstartup_m.c (I've tested with either, one per time obviously)
> > the vector worked fine in C, so it's not the problem.
> >
> > The problem is my static member is not even linked, the linker uses the
default function provided in the vector definition source code.
> > I tried to figure out what I'm doing wrong and made some other tests
also, but no way to establish a connection between the uart interrupt vector
and mu static member function.
> > It works if I "mix" C with C++ using "extern C" definitions, but I
believe that should be a "correct" way of doing this.
> >
> > What I'm doing wrong?
> > Is this the best way of deal with interrupts in C++?
> >
> >
> > Thanks in advance buddies.
> > AndrRairan.
> >
>
>



Another option that may be easier,

Look at the .map file to find out what the mangled name is, then modify the
startup.s file and insert the mangled name in place of the current name for
the ADC IRQ, this avoids trying to use extern C for the static function.



The linker will then recognize the name of your real function and
substitutes its address instead if the weak symbol definition in startup.s



Regards



Phil.



From: l... [mailto:l...] On Behalf Of
Phil Young
Sent: 23 October 2011 20:20
To: l...
Subject: RE: [lpc2000] How handle Cortex-M0 Interrupts on IAR using C++?





Most likely this is because of name mangling.
You are writing a C++ function so the name generated will be mangled and the
linker will not match it to the required name which is defined in the arm
startup code when the vector table is established.

Try using extern "C" on the definition.

Alternatively you need to overwrite the vector entry for the ADC ISR handler
with the address of your function, this is easy enough but it assumes you
have relocated the vector table to RAM first.

Regards

Phil.

-----Original Message-----
From: l...
[mailto:l... ] On
Behalf Of
AndrRairan
Sent: 23 October 2011 18:42
To: l...
Subject: Re: [lpc2000] How handle Cortex-M0 Interrupts on IAR using C++?

_irq_ does not exist on IAR for Cortex-M

On Aug 31, 2011, at 1:04 PM, Mark wrote:

>
> This is the framework that I use with gcc toolchain:
>
> class C_ADC {
>
> private:
>
> static void IRQ_Handler();
>
> };
>
> _irq_ void C_ADC::IRQ_Handler()
>
> {
>
> .
> .
> .
> }
>
> void C_ADC::Init(uint32_t ADC_Clk)
>
> {
>
> .
> .
> .
> .
> .
> installVector(VIC_CH18_ADC0, (pfunction_t)&C_ADC::IRQ_Handler, 0);
>
> }
>
> --- In l... , Andrbr />
Rairan wrote:
> >
> > Hello everybody,
> >
> > This is my first email in here, hope we can help each-other.
> >
> > I'm using IAR 6.21 compiler and a LPC11C14 IAR's dev board, doing my
firsts test with C++.
> > Also I'm simulating UART interrupts in the C-SPY simulator, all working
great on C, so the problem is not the macros or whatever.
> >
> > I search on the web how to handle interrupts in C++, and found that:
> > http://www.eetimes.c...nterrupts-in-C-
> >
> > So I did this on my code:
> >
> > in uart.h:
> > class uart
> > {
> > ...
> > public:
> > static void UART0_IRQHandler( void );
> > }
> >
> > in uart.c:
> > void uart::UART0_IRQHandler( void );
> > {
> > // breakpoint reading the UART receive register
> > }
> >
> > And the vector is defined correctly in cstartup_m.s and in the
cstartup_m.c (I've tested with either, one per time obviously)
> > the vector worked fine in C, so it's not the problem.
> >
> > The problem is my static member is not even linked, the linker uses the
default function provided in the vector definition source code.
> > I tried to figure out what I'm doing wrong and made some other tests
also, but no way to establish a connection between the uart interrupt vector
and mu static member function.
> > It works if I "mix" C with C++ using "extern C" definitions, but I
believe that should be a "correct" way of doing this.
> >
> > What I'm doing wrong?
> > Is this the best way of deal with interrupts in C++?
> >
> >
> > Thanks in advance buddies.
> > AndrRairan.
> >
>
>