Playing with watchdog and sleep mode - can't upload sketches anymore

Hi, folks,

Great platform to play with, but…

I was playing with watchdog and CPU sleep mode on Mega2560. Unfortunately, I’m stuck with a problem. I tried to upload another sketch and the arduino SW just hangs in “uploading…” mode. I did unplug the USB port and plug it back in, but no luck.

Any ideas on how to get it back to life?

/*
 * Sketch for testing sleep mode with wake up on WDT.
 * Donal Morrissey - 2011.
 *
 */
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>

#define LED_PIN (13)

volatile int f_wdt=1;
volatile int loop_count = 0;
volatile boolean oddajaj = false;
volatile int poskus = 0;
volatile int zanka = 0;


/***************************************************
 *  Name:        ISR(WDT_vect)
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Watchdog Interrupt Service. This
 *               is executed when watchdog timed out.
 *
 ***************************************************/
ISR(WDT_vect)
{
  if(f_wdt == 0)
  {
    f_wdt=1;
  }
  else
  {
    //Serial.println (poskus);
   poskus++;
 //  if (poskus > 2)
          {
            // reset function
            //Serial.println ("RESETIRAM HW");
          }
  }
}


/***************************************************
 *  Name:        enterSleep
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Enters the arduino into sleep mode.
 *
 ***************************************************/
void enterSleep(void)
{
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);   /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
  sleep_enable();
  
  /* Now enter sleep mode. */
  sleep_mode();
  
  /* The program will continue from here after the WDT timeout*/
  sleep_disable(); /* First thing to do is disable sleep. */
  
  /* Re-enable the peripherals. */
  power_all_enable();
}



/***************************************************
 *  Name:        setup
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Setup for the serial comms and the
 *                Watch dog timeout. 
 *
 ***************************************************/
void setup()
{
  Serial.begin(9600);
  Serial.println("Initialising...");
  //delay(100); //Allow for serial print to complete.

  pinMode(LED_PIN,OUTPUT);

  /*** Setup the WDT ***/
  watchdogSetup();
  
  Serial.println("Initialisation complete.");
  //delay(100); //Allow for serial print to complete.
}



/***************************************************
 *  Name:        enterSleep
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Main application loop.
 *
 ***************************************************/
void loop()
{
  Serial.println ("void loop start");
  if(f_wdt == 1)
  {
    /* Toggle the LED */
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));
    wdt_reset(); // pet the dog

    if (!oddajaj)
    {
        meritev();
    }
        /* Don't forget to clear the flag. */
        f_wdt = 0;
        
        /* Re-enter sleep mode. */
        enterSleep();
      }

}

void watchdogSetup(void)
{
//  int cas = 1;
  Serial.println ("watchdog setup");
noInterrupts(); //# # // disable all interrupts
wdt_reset();//# // reset the WDT timer just in case

// /* Clear the reset flag. */
//  MCUSR &= ~(1<<WDRF);
//  
/*
 WDTCSR con?guration:
 WDIE = 1: Interrupt Enable
 WDE = 1 :Reset Enable
 WDP3 = 0 :For 2000ms Time-out
 WDP2 = 1 :For 2000ms Time-out
 WDP1 = 1 :For 2000ms Time-out
 WDP0 = 1 :For 2000ms Time-out
*/
// Enter Watchdog Con?guration mode:
WDTCSR |= (1<<WDCE) | (1<<WDE); 

     WDTCSR = (1<<WDIE) | (0<<WDE) | (0<<WDP3) | (1<<WDP2) | (1<<WDP1) | (0<<WDP0);
   
interrupts();
Serial.println ("watchdog setup complete");
}


void meritev ()
{
 Serial.println ("izvajam meritev"); 
 delay (200);
 zanka ++;
 if (zanka >= 10)
   {
     zanka =0;     
     reportWeather ();
     
   }
}

void reportWeather ()
{
  Serial.println ("oddajam.....");
  oddajaj = true;
  watchdogSetup();
  
  
  delay (15000);
  
  
  watchdogSetup();
  Serial.println ("koncal oddajanje");
  oddajaj = false;
  poskus = 0;
}

That was a frequent problem with mega boards that had the older version of the bootloader when using the WDT. One can usually get out of the 'lock up' by holding down the reset button and then upload a new sketch and only release the reset button when th e IDE says it's starting the upload. It can take a few attempts to get the timing correct. Long term fix is to burn the latest mega bootloader that can handle WDT correctly.

Thanks. I’m trying the game with the reset button, but no luck so far…

How does one reload the bootloader on the Mega2560? Most of the AVR tools work fine on chips with up to 64k flash, but mega has 256k… ? Any suggestions?

Wow, I just managed to flash a new sketch into Meg2560... It's the simple blink example, but at least I got it out of the eternal uploading timeout loop. I did it slightly differently than suggested.

I was timing how long the compile time was and I had managed to plug the USB cable in just about half a second before the compile finished and the upload began. I guess I was lucky on the first attempt. I pushed the new sketch onto the Mega and at least for the time being my problem's gone.

Still, let's say I was unable - what tools would I need to upload a new bootloader considering that this chip has 256k of flash?

Use Nic Gammon's bootloader program and wiring, it's proven and works fine for the 2560.

http://www.gammon.com.au/forum/?id=11635

Thanks. I’ll take a look into this…