avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] C++ Interrupts


From: David Brown
Subject: Re: [avr-libc-dev] C++ Interrupts
Date: Mon, 21 Jan 2008 10:19:19 +0100
User-agent: Thunderbird 2.0.0.9 (Windows/20071031)

Markus Lampert wrote:
Hey Ron,

I don't think this will work because the this pointer isn't setup. If you try
to access any member variables of your interrupt class you'll end up in
neverwhere.

That's a good point - the class function may be defined as:

void CTimer0::TIMER0_OVF_vect(void)

but at the low level, it is really:

void _ZN7CTimer011__vector_16Ev(CTimer &self);

Interrupt functions cannot take a parameter, so everything will fall apart if you try to access data members (directly, or via member functions) of the class. And if the class does not have data members that you want to access, all you've achieved is a lot of effort for a little encapsulation.

Why not simply save yourself a great deal of effort and use a C function for the interrupt function, and have it call "Timer0.TIMER0_OVF_vect()", where "Timer0" is your (single) instance of the class? One point to note is that if you are calling a function from inside an interrupt routine, it is best to make that function inline to avoid a lot of register stacking overhead.

mvh.,

David



The only way to do it properly would have a class member function as an
interrupt handler and from there call into the approriate instance (which
you'll have to to store in a singleton pattern or similar).

But once you do that you can use a "regular" interrupt handler and access the
singlton from there.

Anyway, encapsulating this into a macro or avr-libc library function would
probably prove difficult because access to singletons isn't standardized. The
macro would need the class name, the member function and a functor (of sorts)
to get the (singleton) object.

Have fun,
Markus


--- Ron Kreymborg <address@hidden> wrote:

Joerg suggested Dean Camera (and perhaps others) on this list may be able to
help here.
I would like to find some way to automatically include interrupt handlers as
private methods in C++ classes, a not uncommon wish as can be seen after
browsing various EC++ lists. The concept goes against the basic tenets of
the language which explains the difficulty. However gcc already has the
tools necessary to implement this manually with no additional overhead over
a C interrupt handler.
The example class header:

class CTimer0
{
public:
    CTimer0();
    ~CTimer0();
private:
    void TIMER0_OVF_vect(void) __attribute__ ((signal, __INTR_ATTRS));
};

produces the message:

Timer0.h:16: warning: '_ZN7CTimer011__vector_16Ev' appears to be a
misspelled signal handler

This is the mangled name of the implementation of the above interrupt method
in the class definition file (cpp) as:

void CTimer0::TIMER0_OVF_vect(void)
{
    Flags |= TIMER0_OVERFLOW;   // indicate the event type
    TCNT0 = TIMER0_TIMEOUT;     // restart the timeout
}

Ie the whole "CTimer0::TIMER0_OVF_vect" name is mangled.

The application compiles successfully but naturally does not link the
handler. Adding the applicable ISR_ALIAS macro anywhere in the class
definition file using the mangled name provided by the above warning message
successfully links the correct handler and all is well, ie:

ISR_ALIAS(TIMER0_OVF_vect, _ZN7CTimer011__vector_16Ev);

This two stage edit process is not that much extra work, but it would be
nice to automate it. It would seem to require access to the compiler
mangling code and implemented in a macro that would look and be used
something like:

CLASS_ISR(CTimer0, TIMER0_OVF_vect)
{
    Flags |= TIMER0_OVERFLOW;   // indicate the event type
    TCNT0 = TIMER0_TIMEOUT;     // restart the timeout
}

It would expand to:

ISR_ALIAS(TIMER0_OVF_vect, _ZN7CTimer011__vector_16Ev);
void CTimer0::TIMER0_OVF_vect(void)
{
    Flags |= TIMER0_OVERFLOW;   // indicate the event type
    TCNT0 = TIMER0_TIMEOUT;     // restart the timeout
}

I have a simple but complete example application including the makefile I
can email to anyone interested. It is easily run in AVRStudio.

Ron Kreymborg







reply via email to

[Prev in Thread] Current Thread [Next in Thread]