I have a custom board with SAMD20 MCU with external 32kHz crystal connected to XOSC32k (PA00 and PA01).
I'm using Atmel Software Framework code (3.45) for clock initialization. I edited conf_clocks.h to use the external 32kHz crystal to feed DFLL48 to have 48MHz clock. It works well most of the time.
Unfortunately I noticed that the MCU sometime hangs. It happens specially if I play with the power supply (power down and up). It doesn't happen frequently, it's difficult to reproduce the problem (I connected a microswitch to power down the board and I have to press the switch many times). It seems this happens if I cut the power for 3-5s. If I simply reset the device with reset input pin, the MCU restart and doesn't stop anymore.
After some debugging I found the loop where the MCU hangs:
This loops waits for DFLLLCKC, DFLLLCKF and DFLLRDY bit flag. The problematic bit is fine lock (DFLLLCKF). When the problem happens, it seems this lock bit never set. I can stop the code with the debugger and run it again, but the lock bit stays zero.
I think I can solve this problem with watchdog (that I now configure after clock initialization). However I'd like to solve it in a better way. Do you have any suggestions?