Search code examples
cinterruptavratmega

AVR programming, interrupt handling


I develop a C application with atmega168a-pu and interrupts. I use the following interrupts:

ISR(TIMER0_COMPA_vect);
ISR(TIMER0_COMPB_vect);
ISR (TIMER2_COMPA_vect);
ISR(SPI_STC_vect);
ISR(TIMER1_COMPA_vect);
ISR (PCINT1_vect);

and my code looks like

int main(void){
///initialization etc.
   sei();
   while(1){
    ///do some stuff and wait the interrupts
   }
return 0;
}

I want to block all other interrupts when an interrupt occurs and enable the interrupts just before exiting the interrupt function.

Could you please explain it on a code snippet how I can do it?

EDIT: http://www.nongnu.org/avr-libc/user-manual/optimization.html#optim_code_reorder states that such usage cause reodering problem.

function(){
  cli();
  ..
  sei();
}

Solution

  • The previous answer I posted here was based on the original question, not mentioning the reordering problem of the avr-gcc. Obviously it is too long ago, that I worked with the AVR but there was a bell ringing regarding disabling the interrupts

    Revised Answer to the question

    Protect interrupts from being interrupted

    Atmel writes about the Interrupt handling in the databook:

    When an interrupt occurs, the Global Interrupt Enable I-bit is cleared and all interrupts are disabled. The user software can write logic one to the I-bit to enable nested interrupts. All enabled interrupts can then interrupt the current interrupt routine. The I-bit is automatically set when a Return from Interrupt instruction – RETI – is executed.

    Hence the behavior you ask for is already implemented in hardware.

    Reordering issue

    I also did some investigation on this reordering issue. Obviously there is huge disagreement whether this is an error of the compiler or not. It seems that the main risk of the reordering is that the interrupts are disabled for a longer period of time than they are expected to be. During my research I did not find a solution on that except of solutions causing more load/store activity which is not really an option I think.