Go Down

Topic: ISR usage guidelines (Read 2 times) previous topic - next topic

marlumme

I had a problem using TIMER2 interrupts, because tone library already
occupies it. Even when I do not include that library. I fixed it by
modifying Tone.cpp code with the weak attribute. Now your code
can reuse the interrupt vector. Of course tone() is now unavailable.
This would be a good practice for all base library ISR vectors.

ISR(TIMER2_COMPA_vect) __attribute__ ((weak));

You can do this by editing and recompiling your project. Arduino IDE
seems to take care of recompilation of the modified tone.cpp
/Applications/Arduino/Contents/Resources/Java/hardware/cores/arduino/Tone.cpp

WizenedEE

I compiled this sketch fine:
Code: [Select]

ISR(TIMER2_COMPA_vect) {
   PIND = 0xf0;
}
void setup() {}
void loop() {}


on both 1.0.1 and the newest github version (commit 222d51e3831d77f60ae6ce1f3d037b0febb0c717) with avr-gcc 4.7.1 and avr-libc 1.8.0

What "problems" are you having?

DuaneB

Not the OP, but I have also found I cannot redefine ISRs in my sketches, I assumed this was normal, are you suggesting that we should expect to be able to override INT0 etc ?

Duane B

rcarduino.blogspot.com

WizenedEE

Even with the avr-gcc that gets downloaded with the source (version 4.3.2), this still compiles on my computer:
Code: [Select]

void setup() {}
void loop() {}
ISR(ADC_vect)               {PIND = 0x00;}
ISR(ANALOG_COMP_vect)       {PIND = 0x01;}
ISR(EE_READY_vect)          {PIND = 0x02;}
ISR(INT0_vect)              {PIND = 0x03;}
ISR(INT1_vect)              {PIND = 0x04;}
ISR(PCINT0_vect)            {PIND = 0x05;}
ISR(PCINT1_vect)            {PIND = 0x06;}
ISR(PCINT2_vect)            {PIND = 0x07;}
ISR(SPI_STC_vect)           {PIND = 0x08;}
ISR(SPM_READY_vect)         {PIND = 0x09;}
ISR(TIMER0_COMPB_vect)      {PIND = 0x0a;}
//ISR(TIMER0_OVF_vect)        {PIND = 0x0b;}
ISR(TIMER1_CAPT_vect)       {PIND = 0x0c;}
ISR(TIMER1_COMPA_vect)      {PIND = 0x0d;}
ISR(TIMER1_COMPB_vect)      {PIND = 0x0e;}
ISR(TIMER1_OVF_vect)        {PIND = 0x0f;}
ISR(TIMER2_OVF_vect)        {PIND = 0x10;}
ISR(TWI_vect)               {PIND = 0x11;}
//ISR(USART_RX_vect)          {PIND = 0x12;}
ISR(USART_TX_vect)          {PIND = 0x13;}
//ISR(USART_UDRE_vect)        {PIND = 0x14;}
ISR(WDT_vect)               {PIND = 0x15;}


The two USART ones didn't work with 4.3.2 but they did with 4.7.1, and the TIMER0_OVF one didn't work on either of them.

It would definitely make sense to make the functions weak. I tried it and it made the above sketch compile with everything uncommented. I think it should be a goal of arduino to be able to use any program that don't use arduino libraries in the IDE, and that means being able to redefine the ISRs.

DuaneB

Interestingly, that worked for me as well, I wonder if it was because I was defining the ISR in a cpp file rather than the sketch file ?

Duane B

Go Up