Arduino Uno Wifi Rev 2/ATMEGA4809 : Watchdog always restarts whole system

Hello,

after i managed to set the Arduino to sleeping mode, the Watchdog restarts the whole system after 8 seconds. So far so good. But instead of restarting the complete system I would like to continue after the “sleeping line” to repeat the sleeping phase 5 more times. I already checked out the ATMEGA4809 Datasheet, but didn’t find anything to deal with this issue. On the other hand I found some sample codes from other ATMEGA microcontroler models where it seems to be possible. For example here:

Maybe it’s because I’m quite new to this topic, but there are also 2 more things I don’t understand and I would be extremely thankul if someone could also answer these two questions:

  1. In the Datasheet “WDT.STATUS” has to be set to 0 to enable the Watchdog. For me it also works if it is set to 1. So for what is this variable needed?
  2. In the Datasheet “CPU.CCP” has to be set to “IOREG”, respectevely to “0xD8” before enabling the Watchdog. For me just “CPU_CCP” worked. So instead of the dot, I had to use the underscore. Is that a mistake in the Datasheet?
    Here is the source of the datasheet I used as a reference: http://ww1.microchip.com/downloads/en/DeviceDoc/40002015A.pdf

Here is my code. Thanks in advance for your help!

Regards,

Manuel

#include <avr/sleep.h>

int count = 0;
void setup() 
{
  //Waiting time
  Serial.begin(9600);
}

void loop() 
{
  for(int i = 0; i < 5; i++){
    delay(1000);
    Serial.println(count++);
  }
  watchdogOn();
  Serial.println("Continue here with operation to set the Arduino again in sleep mode");
  //watchdogOn();
  //watchdogOn();
}

//Set Watchdog active to 8s and go to sleep
void watchdogOn() {
  WDT.STATUS = WDT.STATUS & 01111111;
  while (!(WDT.STATUS == 0)){
      Serial.println("...");
  }
  CPU_CCP = 0xD8; //unlock...
  WDT.CTRLA = 0xB; //set WDT to 8 seconds
  Serial.println("Watchdog active...and going to sleep");
  delay(100);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_mode();
  sleep_disable();
  delay(100);
}

void watchdogOff() {
  WDT.STATUS = WDT.STATUS & 01111111;
  while (!(WDT.STATUS == 0)){
      Serial.println("...");
  }
  CPU_CCP = 0xD8;
  WDT.CTRLA = 0x0;
  Serial.println("Watchdog disabled...");
}

I have also just realised from the title of your post that there is yet another Arduino board out that I had never heard of before. https://www.mouser.ch/new/arduino/arduino-uno-wifi-rev2/ I normally get to hear about these when they have been retired.

Out of curiosity, I looked at the ATMEG4809 datasheet (mainly filled with graphs) and the matching MegaAVR Series 0 manual.

The architecture is completely different to the AVR based chips used on the older Arduino boards so the example you found is not relevant.

It looks like the watch dog timer is used for forcing a system reset only and cannot not be used to wake up from a sleep mode (as it can be in the ATMEGA328 etc.)

For this chip, it appears that you have to set up the Real Time Clock (RTC) in Periodic Interrupt Timer (PIT) mode to get the device to wake up from a sleep mode. (See ch10 and ch21 in the Series 0 manual).

It looks like the watch dog timer [on ATmega4809] is used for forcing a system reset only : it appears that you have to set up the Real Time Clock (RTC) in Periodic Interrupt Timer (PIT) mode to get the device to wake up from a sleep mode.

I agree. On the bright side, I think that means that you can set the sleep timer longer than 8 seconds.

Thanks for the answers, in the meantime I also found out that I have to set up the RTC instead of using the WDT. And a big Thank you for looking into the datasheet, I really appreciate that!

Realizing the Main Clock Switch, which is necessary for the PIT, wasn’t that easy as I thought first. In the datasheet the following is written:

“All internal oscillators can be used as the main clock source for
CLK_MAIN. The main clock source is selectable from software, and can be
safely changed during normal operation.”

But I can’t agree on that sentence in the datasheet…

Another guy in a forum had the same issue:

Anyway I didn’t manage to wake up the Arduino after setting it into sleep mode. I followed the datasheet but nevertheless I didn’t found a solution therefore. If somebody is interested in the code, here you go:

#include <avr/sleep.h>
#include <avr/interrupt.h>

int count = 0;
void setup() 
{
  //Waiting time
  Serial.begin(9600);
}

void loop() 
{
  for(int i = 0; i < 5; i++){
    delay(1000);
    Serial.println(count++);
  }
  periodicInterruptTimer();
  Serial.println("Continue here with operation to set the Arduino again in sleep mode");
}

void periodicInterruptTimer() {
  // Set all pins to low power mode:

  CPU_CCP = 0xD8; //unlock...
  CLKCTRL_OSC32KCTRLA = 0b00000010; //Aktiv bleiben in Standby
  
  while (!(RTC.STATUS == 0) && !(RTC.PITSTATUS == 0)){
      Serial.println("...");
  }
  RTC.CLKSEL = RTC_CLKSEL_INT32K_gc;
  RTC.PITINTCTRL = 0b00000001;
  
  RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc;
  RTC.PITCTRLA = RTC.PITCTRLA | 0b00000001;
  while (!(RTC.STATUS == 0) && !(RTC.PITSTATUS == 0)){
      Serial.println("...");
  }
  sei();
  delay(500);
  Serial.println("PIT active...and going to sleep");
  delay(500);
  
  set_sleep_mode(SLEEP_MODE_STANDBY);
  sleep_enable();
  //sleep_cpu();
  sleep_mode();
  sleep_disable();

}

ISR(RTC_CNT_vect)
{
  Serial.println("ja");
  delay(1000);
 RTC.INTFLAGS = RTC_OVF_bm;
}


[\code]

That code works ? Your last sentence is not very clear if you did or did not find a solution. Using Serial.print in an interrupt service routine, incidentally, may give misleading results.

Are you developing code which will later be used on a different target board but still based on the ATMEG4809 ? Normally sleep modes are relevant for battery operations where you are getting down to the microamp level of power consumption. On a heavily stocked board like Uno Wifi Rev. 2 that would probably be a hopeless ambition.

Sorry, I wasn't clear enough. The code doesn't work. My intention of using the Serial.print was to check if the Arduino wakes up.

Yes, you are absolutely right. Initially I had the ambition to save power while operating with battery, but this really seems to be the wrong board therefore. But now I am just highly interested in solving the problem :-)