interrupt syntax for different c compilers

Go To Last Post
24 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'll start out with an imagrcraft example for a usart rx interrupt... someone kindly quote this and add a gcc exmple and a CV example? I think a side by side comparison will show how to convert from one compiler to another more easily.

//---------------------------
//imagecraft example
//required h files
#include  //

//handler declaration
#pragma interrupt_handler uart_rx_isr:iv_USART_RX
void uart_rx_isr(void){
  //rest of code
}
//------------------------

//---------------------------
//gcc example
//h files

//handler


//--------------------------


//---------------------------
//codevision example

//h files

//handler

//---------------------------

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's the AVR-GCC equivalent:

//---------------------------
//imagecraft example
//required h files
#include  //

//handler declaration
#pragma interrupt_handler uart_rx_isr:iv_USART_RX
void uart_rx_isr(void){
  //rest of code
}
//------------------------ 

//---------------------------
//gcc example
//required h files
#include 
#include  //

//handler declaration
// The vector name (USART_RXC_vect in this case)
// will vary from part to part, based on the
// name given to that vector in your part's
// datasheet.
ISR(USART_RXC_vect){ 
  //rest of code
}
//--------------------------




//---------------------------
//codevision example

//h files

//handler

//--------------------------- 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks simpler than I imagined so far.....

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//--------------------------- 
//imagecraft example 
//required h files 
#include  // 

//handler declaration 
#pragma interrupt_handler uart_rx_isr:iv_USART_RX 
void uart_rx_isr(void){ 
  //rest of code 
} 
//------------------------ 

//--------------------------- 
//gcc example 
//required h files 
#include  
#include  // 

//handler declaration 
// The vector name (USART_RXC_vect in this case) 
// will vary from part to part, based on the 
// name given to that vector in your part's 
// datasheet. 
ISR(USART_RXC_vect){ 
  //rest of code 
} 
//-------------------------- 

//--------------------------- 
//codevision example 
//h files 
#include 

//handler
// the bit that actually hooks the right vector in
// this is the USART_RXC that comes from mega32.h. As
// for GCC the names will vary from AVR to AVR 
interrupt [USART_RXC] my_name_for_rx_isr(void) {
  //rest of code
}
//--------------------------- 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cool stuff. I hope I'll be able to find this next time I try to port an example! Thanks!

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have files that will compile with PIC, 8051 and AVR. Compilers for each of these targets have different syntax for declaring an ISR.

I prefer to keep source code as portable as possible while using one common header file that copes with the differences.

However those that require #pragma make it very awkward to construct a generic #include "mcu.h" that defines correct macros for each mcu and compiler combination. Similarly a #asm is also not really allowed within a pre-processor expansion.

Would it not be nice if all C compiler vendors followed a similar syntax ! Not only this but my neighbour would love to fly his weaners to overcome the current Foot and Mouth restrictions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//---------------------------
//IAR example - AVR IAR Compiler [ICCAVR] (Current Version)
//h files
#include  // MCU include
#include  // Architecture include 
//brings in __enable_interrupt() and __disable_interrupt()

//handler declaration
#pragma vector=USART_RXC_vect
__interrupt void my_name_for_rx_isr(void) {
  //rest of code
}
//---------------------------

//---------------------------
//IAR example - A90 IAR Compiler [ICCA90] (Old Version)
//h files
#include  // MCU include
#include  // Architecture include 
// brings in _SEI() and _CLI()

//handler declaration
interrupt[USART_RXC_vect] void my_name_for_rx_isr(void) {
  //rest of code
}
//---------------------------

//---------------------------
//Rowley CrossWorks example
//h files
#include  // MCU include

//handler declaration
void my_name_for_rx_isr(void) __interrupt[UARTRX_VECTOR]
{
  //rest of code
}
//---------------------------

//---------------------------
//SPJ Systems' CAVR
//h files
#include  // MCU include

//handler declaration
interrupt(USART_RXC) void my_name_for_rx_isr(void) {
  //rest of code
}
//---------------------------

//--------------------------- 
//imagecraft example 
//required h files 
#include  // 

//handler declaration 
#pragma interrupt_handler my_name_for_rx_isr:iv_USART_RX 
void my_name_for_rx_isr(void){ 
  //rest of code 
} 
//------------------------ 

//--------------------------- 
//gcc example 
//required h files 
#include  
#include  // 

//handler declaration 
// The vector name (USART_RXC_vect in this case) 
// will vary from part to part, based on the 
// name given to that vector in your part's 
// datasheet. 
ISR(USART_RXC_vect){ 
  //rest of code 
} 
//-------------------------- 

//--------------------------- 
//codevision example 
//h files 
#include 

//handler
// the bit that actually hooks the right vector in
// this is the USART_RXC that comes from mega32.h. As
// for GCC the names will vary from AVR to AVR 
interrupt [USART_RXC] my_name_for_rx_isr(void) {
  //rest of code
}
//--------------------------- 

Added IAR (new & old) Since many of the Atmel APP notes are based on IAR code, both new and old versions.

Also, for completeness, added Rowley CrossWorks (based on the documentation for it) as well as SPJ System's CAVR (Also based on the documentation).

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

Last Edited: Tue. Sep 18, 2007 - 09:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//---------------------------
// IAR - AVR IAR Compiler [ICCAVR] (Current Version)

// required includes
#include 
#include  // Architecture include
// brings in __enable_interrupt() and __disable_interrupt()
// not requred for the ISR itself, but will be needed to
// enable interrupts in your code

//handler declaration
#pragma vector=interrupt-vector-name
__interrupt void my_name_for_this_isr(void) {
  //rest of code
}
// NOTE: The ISR function must immediately follow the #pragma

//---------------------------

//---------------------------
// IAR - A90 IAR Compiler [ICCA90] (Old Version)

// required includes
#include 
#include  // Architecture include
// brings in _SEI() and _CLI()
// not requred for the ISR itself, but will be needed to
// enable interrupts in your code

//handler declaration
interrupt[interrupt-vector-name] my_name_for_this_isr(void) {
  //rest of code
}
//---------------------------

//---------------------------
// gcc-avr

// required includes
#include 
#include  
// be sure to set the correct processor model in the make file

//handler declaration
ISR(interrupt-vector-name){
  //rest of code
}
//--------------------------

//---------------------------
// ImageCraft ICC for AVR

// required includes
#include 

//handler declaration
#pragma interrupt_handler my_name_for_this_isr : interrupt-vector-name
void my_name_for_this_isr(void){
  //rest of code
}
// NOTE: The ISR function does not need to immediately follow the #pragma
//------------------------

//---------------------------
// HP InfoTech CodeVisionAVR

// required includes
#include 

//handler declaration
interrupt [interrupt-vector-name] my_name_for_this_isr(void) {
  //rest of code
}
//---------------------------

//---------------------------
// HP InfoTech CodeVisionAVR
// alternate

// required includes
#include 
// be sure to set the correct processor model in the project options

//handler declaration
interrupt [interrupt-vector-name] my_name_for_this_isr(void) {
  //rest of code
}
//---------------------------

//---------------------------
// Rowley CrossWorks

// required includes
#include 

//handler declaration
void my_name_for_this_isr(void) __interrupt[interrupt-vector-name]
{
  //rest of code
}
//---------------------------

//---------------------------
// SPJ Systems' CAVR

// required includes
#include 

//handler declaration
interrupt(interrupt-vector-name) void my_name_for_this_isr(void) {
  //rest of code
}
//---------------------------

Made it more generic, by abstracting out the processor, function, and vector names.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

Last Edited: Wed. Sep 19, 2007 - 03:30 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now all the EEs can get their interrupts working and call themselves Software Engineers! I hope this comparison is helpful to some poor soul that has to adapt modules from one compiler to another. Is this a great forum or what?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

//---------------------------
// HP InfoTech CodeVisionAVR

// required includes
#include
...

As an option, you can also
#include

similar to your annotation for GCC and use the file indicated by the project options.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Most Excellent thread!!
Thanks for the post Bob; and to all the responders too!!!
John

Resistance is futile…… You will be compiled!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So Bob, when you're happy with this, why not have it moved to the tutorials forum where it will find a happy and appropriate home?

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Make it so Number One!

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
Make it so Number One!

You must be refering to Ensign ABCMiniUser of the AVRFreaks Star Fleet Command...

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

As an option, you can also
#include

similar to your annotation for GCC and use the file indicated by the project options.

Lee

Added

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ensign?! More like supreme commander!

Actually, despite being the supreme commander of this here ship, I don't seem to have a commander's access card to the moderation computer of this particular forum. I'm afraid I've only got my Tutorials, Off-Topic and General Electronics ones on me. Care to repost in the Tutorials section while I go berate the underlings responsible for this lamentable oversight?

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, who is going to volunteer to do the whomping macro (somewhat akin to ISR() ) to make a generic interface? I modified one purchased commercial source library to add CV to it; it was fairly straightforward IIRC--each of the compilers has a preprocessor signature that one can access.

Example: "Prototype"

#ifdef __ICCAVR__
#pragma interrupt_handler CpCoreIntHandler:2
#endif
#ifdef __CODEVISIONAVR__
interrupt [EXT_INT0] void 	CpCoreIntHandler		(void);
#endif

Function definition:

#ifdef __ICCAVR__
void CpCoreIntHandler(void)
#endif
#ifdef __CODEVISIONAVR__
interrupt [EXT_INT0] void 	CpCoreIntHandler		(void)
#endif

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

IMHO the problem is that a macro (like GCC's ISR) cannot legally contain a preprocessor #pragma such as ImageCraft or the new version of IAR uses.

I think Jörg (dl8dtl) has written an abstraction layer that bypasses this constraint and allows common source code (within some constraints such as not allowing the use of IAR's built-in handling of the Flash address space) to be compiled on either IAR or GCC. IIRC he uses the alternative C99 Pragma() construct instead of the preprocessor equivalent, because it can be legally embedded within a macro.

IAR obviously has an implementation of this C99 form of pragma, but apparently it is somewhat finicky. Does ImageCraft?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

macro (like GCC's ISR) cannot legally contain a preprocessor #pragma

Even within a false #ifdef ? [I'd just assume it was only scanning for the matching #endif. But I just do K.I.S.S. C and preprocessor.]

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, at that level, I think it would work. But that means you couldn't wrap everything up in a neat macro like you might be able to if it weren't for those darned pragmas.

For example, I think this:

#include "compatibility.h"

COMPILER_INDEPENDENT_INTERRUPT(vector_name)
{
}

Looks nicer than this:

#ifdef __ICCAVR__
#include "something"
#elif defined(__CODEVISIONAVR__)
#include "something else"
#endif

#ifdef __ICCAVR__
#pragma interrupt_handler CpCoreIntHandler:2
void CpCoreIntHandler(void)
#elif defined(__CODEVISIONAVR__)
interrupt [EXT_INT0] void    CpCoreIntHandler      (void)
#endif
{
}

In the past, I wrote COMPILER_INDEPENDENT_INTERRUPT()-like macro that I saw to work correctly for both GCC and CodeVision. I understand that Jörg has already written something that he says worked correctly for both GCC and IAR.

But I haven't figured out how I'd write one that also encompasses ImageCraft because the C preprocessor chokes when one tries to wrap a #pragma up inside a #define.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We use a 3rd party C preprocessor that claims to be C99. So if _Pragma() doesn't work currently, it should be relatively easy to add. I will inquire.

// richard

Richard Man http://imagecraft.com

Beyond Arduino - When you're ready to get serious...
JumpStart C Tools, The Better Alternative.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Certainly you should be able to do it in a series of compiler conditionals... but as you see above it is very ugly, and tedious to code. Unfortunately this is the only option for compilers that use #pragma to set the vector, that I know of.

It might be possible to reduce the conditional set down, and have a "compiler-independent" macro that takes care of everything except the #pragma. This would make it far less cumbersome to code, and much easier to read.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For my "real" apps it isn't a biggie. Where I thought it would have value is for sample code, posted projects, and the like.

E.G., Bob G. is the resident "shouldn't this be in the GCC Forum?" police. Generic AVR C programs for a problem situation could be posted, and we all could compile them with our favourite and complain about the code generation of the others. ;)

If I'm porting an app "for real", I'm going to be doing AVR-model-specific changes and compiler's-model-of-code-generation changes, so the fussing with the few ISRs used on an app isn't really a big deal.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah, but if you're posting an example, do you really want to obfuscate it with multiple levels of compiler conditionals all over the place? Furthermore, are you going to go through the effort to add a 6 level conditional around ever interrupt service routine you use in code you're going to provide to the projects section, or for an example on the forum?

Best to document these differences, as we have. And leave it as an exercise to the reader to do the necessary conversion. They need to do a little work, if they're going to gain anything from the experience! ;)

Writing code is like having sex.... make one little mistake, and you're supporting it for life.