Hi,
The code in the example just switch from DRUN to RUN1 mode while enabling the XOSC and keep system running from IRC.
So you may omit the first “while” statement, as in case of XTAL not stable it does not finish the mode transition either.
If it hangs on mode transition, check the status registers to know the reason, so look on ME_GS, ME_IS, ME_IMTS.
Before doing a first mode transition the FCCU faults, if appeared after reset, have to be cleared with the code given in the example.
If the MCU is placed in the socket, try to align it carefully again.
Regards,
Petr