LowPowerLab Forum

Hardware support => Moteino => Topic started by: Tomega3 on December 23, 2014, 04:21:44 PM

Title: Using a Watchdog timer with the MoteinoMEGA (atmega1284p)
Post by: Tomega3 on December 23, 2014, 04:21:44 PM
Has anyone tried setting up a watchdog timer on the Moteino Mega with the 1284p processor?

I have had success with this code on the boards using the 328 and 2560 processors


// in setup()
//Set up the watchdog timer. If it isn't reset every 4 or 8 seconds, the watchdog ISR will trigger
  #ifdef WATCHDOG_EN
    #ifdef SLEEP_EN
      #ifndef USE_LOWPOWER_LIB
        WDT_Init_8();           // 8 seconds with ISR but without reboot
      #endif
    #else
      WDT_Init_4();           // 4 seconds with ISR and reboot
    #endif

    petthedog();
  #endif




#ifdef WATCHDOG_EN
//initialize watchdog timer and its interrupt
// this works for 4 second watchdog reboot and its ISR
void WDT_Init_4(void)
{
  //disable interrupts
  cli();
  //set up WDT interrupt
  WDTCSR = (1<<WDCE)|(1<<WDE);

  //Start watchdog timer with 4 second prescaller
  WDTCSR = (1<<WDIE)|(1<<WDE)|(1<<WDP3); 
       
  // this works for 4 seconds and sets up the ISR and a reboot
  //Enable global interrupts
  sei();
}

// This works for 8 second watchdog, and its ISR, no reboot on watchdog timeout
void WDT_Init_8(void)
{
   // Setup the WDT
  cli();
  // Clear the reset flag.
  MCUSR &= ~(1<<WDRF);

  // In order to change WDE or the prescaler, we need to
  // set WDCE (This will allow updates for 4 clock cycles).
  //
  WDTCSR |= (1<<WDCE) | (1<<WDE);

  // set new watchdog timeout value to 1024K cycles (~8.0 sec)
  WDTCSR = (1<<WDP3) | (1<<WDP0);

  // Enable the WD interrupt only, do not enable a reboot
  WDTCSR |= _BV(WDIE);

  sei();
}

//Watchdog timeout ISR
// for use by WDINT_4 or WDINT_8 routines without sleep
#ifndef SLEEP_EN
  ISR(WDT_vect)
  {
    Serial.println(F("WDT ISR Active, Rebooting"));
  }
#endif


/****************************************************/
// watchdog ISR for Sleep

#if defined SLEEP_EN
ISR(WDT_vect)
{
  if(f_wdt == 0)
  {
    f_wdt = 1;
  }
  else
  {
    Serial.println(F("Sleep Watchdog Overrun!!!"));
    // watch out, on the arduino mega 2560 R3 3 exclamation points in a row screws things up
  }
}
#endif


void petthedog()
{
   #ifndef SLEEP_EN  // do not pet the dog if SLEEP MODE is enabled
    wdt_reset();  //Reset the watchdog
  #endif
}
#endif // end WATCHDOG_EN



Could this code work with the 1284p processor or does it need changes?

My goal is to eventually find out if its possible to modify the LowPower lib from rocketstream to work with the 1284p processor on the Moteino Mega

Thanks

Title: Re: Using a Watchdog timer with the Moteino Mega (1284p)
Post by: Felix on December 24, 2014, 10:23:29 AM
I believe that's already achieved here: https://github.com/LowPowerLab/LowPower
Title: Re: Using a Watchdog timer with the Moteino Mega (1284p)
Post by: Tomega3 on December 24, 2014, 11:58:31 AM
Hi Felix,
That's Great.
I have been looking at Ver 1.30 of the lib from rocketstreams github page.
It does not have support for the 1284p in it, glad your does.
Thanks again,
Title: Re: Using a Watchdog timer with the MoteinoMEGA (atmega1284p)
Post by: Tomega3 on December 25, 2014, 09:47:57 AM
I think I may have found a needed enhancement to Ver 1.31 of the LowPower lib that has support for the 1284p processors.
I have read over the 1284p summary datasheet (see www.atmel.com/Images/8059s.pdf)
and see that it has 4 timers and 2 uarts.
I think the LowPower lib needs to also turn on and off timer3 and usart1 when the chip is a 1284p.

If you would like, I can make the edits and post the modified lib

Thanks again for all your work and help.
Title: Re: Using a Watchdog timer with the MoteinoMEGA (atmega1284p)
Post by: Felix on December 26, 2014, 11:35:57 AM
If you can, please do, it's a shared work and user enhancements are welcome :)
Title: Re: Using a Watchdog timer with the MoteinoMEGA (atmega1284p)
Post by: Tomega3 on December 27, 2014, 07:57:57 PM
Felix, attached is a zip file with my changes - I called it V1.32Beta
I changed the readme, the h and cpp files making a separate ifdef for the 1284p AVR chip
I have tested it with Arduino ide 1.06 and your MoteinoMega core files.

Thanks again for your help.


Title: Re: Using a Watchdog timer with the MoteinoMEGA (atmega1284p)
Post by: Felix on December 27, 2014, 09:51:39 PM
Thank you much, I will review and consider integrating the changes.