Electronic – AVR bootloader interrupt issue

atmegaavrbootloaderinterrupts

For a quite while, I've been using an avr ATmega324PA controller and Atmel studio to write the programs. we wrote a TCP bootloader. It works fine without any interrupt. Later we did add an external inetrrupt to that program and now controller always restarts when jumping to the ISR. Here is the code

void main()
{
  init_interrupt();
  init_uart();
  while(1);
}

void init_interrupt()
{
  /* PortA3 as input for external interrupt*/
 DDRB|=(0<<2);
  /*Pulll up disable*/
 PORTB &=~ (1<<PORTB2);
  /*Interrupt_vector change enable*/
 MCUCR|=(1<<IVCE);
  /*Interrupt vector change to boot section*/
 MCUCR|=(1<<IVSEL);
  /*Interrupt_vector change enable*/
 MCUCR&=~(1<<IVSEL);
 _delay_ms(5000);
  /* Enable external interrupt-2*/
 EIMSK |= (1<<INT2);
  /* Enable rising edge of the input signal as interrupt*/
 EICRA |= (1<<ISC21)|(1<<ISC20);
  /* Enable global interrupt*/
 sei();
}

ISR(INT2_vect)
{
    uart_send("Print on interrupt");
}

Here i am trying to change the interrupt vector to boot section.

NOTE: BOOTRST fuse is already set

EDITED:
After changing the MCUCR value as shown below, i sent it through UART instead of '01', the output is displayed as "A0 2E 57 C9 D1 95 91 FF"

    MCUCR|=(1<<IVCE);

Must be a problem with the MCUCR register

Best Answer

Atlast this is how to make work the interrupts in bootloader, U should set the Interrupt Vector Change Enable(IVCE) bit as below

   MCUCR|=(1<<IVCE); // or GICR|=(1<<IVCE) in some controllers

And within four cycles u have to set the IVSEL and unset the IVCE bit.

   MCUCR=0x02; // or GICR=0x02;  IVSEL=1, IVCE=0 at same time

What went wrong in the above code is below

     /*Interrupt_vector change enable*/
    MCUCR|=(1<<IVCE);
    /*Interrupt vector change to boot section*/
    MCUCR|=(1<<IVSEL);
    /*Interrupt_vector change enable*/
    MCUCR&=~(1<<IVSEL);

The former method might finish within four cycles i think. However i can now use the TCP bootloader with interrupts to load the application code.