avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] avr-c++ can I use templates to avoid creating unneces


From: Michael Clift
Subject: Re: [avr-gcc-list] avr-c++ can I use templates to avoid creating unnecessary ISRs?
Date: Tue, 14 Sep 2010 13:34:48 +1000
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.9) Gecko/20100825 Thunderbird/3.1.3


On 13/09/2010 16:42, Robert J Lee wrote:
I'm working on a library using avr-gcc (I'm working with an Arduino, but
I think the library should be of more general use).

The problem is that the library may be used with any one (or more) of
the timer interrupts on the AVR. I want to avoid creating timer ISRs
that are never used; I need the unused timers elsewhere in my program. I
currently use only one ISR for this library, but I'd much rather have a
general solution rather than forcing it to use an arbitrary ISR (which
may clash with other libraries in future).

What I really want to do is to use a C++ function template, and only if
the template is instantiated, to then create an associated timer ISR.

eg (in c++-like pseudocode:)

template<int timerId>
void function setTimer(/*ARGS*/) {
    /*set up timer interrupt registers*/
    struct timer {
      ISR(TIMER##timerId##COMPA_vect) {   // DOES NOT WORK
         /*do timer action*/
      }
    }
}

The use of ## for string concatenation here is illegal (it's not a
#define); template specialisation could be used instead to avoid that
problem.

A bigger issue is that ISR() is a macro that defines a function (named
eg TIMER1COMPA_vect) with special decorators and C-linkage, and
according to the manual, the exact name of the function tells the
compiler which interrupt to service. Because of the C linkage
requirement, I can't use a function template even if I could somehow
manipulate it to have a name that the compiler would recognise (which
would probably involve moving it out of the template function).

Is there a way to achieve this, possibly some compile-time option to add
c++-mangled names to the compiler's vector name lookup table? If not, is
there another solution to the problem?


In the worst case I would have to do something like this:

#define USE_TIMER(id) \
ISR(TIMER##id##COMPA_vect) { \
   handleInterrupt<id>(); \
}

and have the user explicitly ask for the timers they want to use by
calling USE_TIMER repeatedly. (Which makes it *really* hard to
programatically assign timer IDs....)

Any help or pointers would be much appreciated.

Robert Lee.



_______________________________________________
AVR-GCC-list mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list



Hi Robert, just a thought...

If the user wanted to programatically assign ISR's to either your library handleInterrupt<id>() functions, or their own functions, then maybe the main application should contain ISR()'s which call function pointers? The function pointers could then be loaded with either the address of your library functions, or the users own functions.

Michael.




reply via email to

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