SAMD21 ( Arduino Zero MCU) not working after some time

I have a problem with my new board architecture based on SAMD21J18A. I'm working with an Arduino IDE.

The FW works properly but after some days it completely stop running.
I've also implemented a WDT based reset which occurs after 1 hour if the code is stuck, but it doesn't help in this situation.
If I look at the log, the code stop randomly and not in a particular point.

Basically in the FW's loop code does some actions every 4 seconds and it returns to sleep.
I think there is something missing in my sleep function. Could you please check it?

void SAMD_sleep()
end_debug(); //stop serial.print()

// Don't fully power down flash when in sleep
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
__DSB(); // Data sync to ensure outgoing memory accesses complete
__WFI(); // Wait for interrupt (places device in sleep mode)
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;

start_debug(); ////start serial.print()

Is that the problem or there is something related to current spikes or what?

Many thanks

Hi acutone,

The code looks good, except for the lines waiting for WDT register synchronization:


This line waits for clock synchronization after writing to either the watchdog's CTRL, CONFIG or CLEAR register, however these registers don't appear in your code.

Also, the NVMCTRL CTRLB register's SLEEPPRM bitfield only needs to be set once at the beginning of the sketch.