SAMD21 ( Arduino Zero MCU) not working after some time

Hello,
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
NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val;
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
while(WDT->STATUS.bit.SYNCBUSY);
__DSB(); // Data sync to ensure outgoing memory accesses complete
__WFI(); // Wait for interrupt (places device in sleep mode)
while(WDT->STATUS.bit.SYNCBUSY);
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:

while(WDT->STATUS.bit.SYNCBUSY);

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.