Hi
Here's commented code to set up the FLL to 95.97747MHz from external 32kHz crystal taken from the uTasker project:
#define _FLL_VALUE (MCG_C4_HIGH_RANGE | MCG_C4_DMX32)
POWER_UP_ATOMIC(6, RTC); // enable access to the RTC
MCG_C7 = MCG_C7_OSCSEL_32K; // select the RTC clock as external clock input to the FLL or MCGOUTCLK
RTC_CR = (RTC_CR_OSCE); // enable RTC oscillator and output the 32.768kHz output clock so that it can be used by the MCG (the first time that it starts it can have a startup/stabilisation time but this is not critical for the FLL usage)
MCG_C1 = ((MCG_C1_CLKS_PLL_FLL | MCG_C1_FRDIV_RANGE0_1) & ~MCG_C1_IREFS); // switch the FLL input to the undivided external clock source (RTC)
SIM_CLKDIV1 = (((SYSTEM_CLOCK_DIVIDE - 1) << 28) | ((BUS_CLOCK_DIVIDE - 1) << 24) | ((FLEX_CLOCK_DIVIDE - 1) << 20) | ((FLASH_CLOCK_DIVIDE - 1) << 16)); // prepare bus clock divides ready for the final operating frequency
_WAIT_REGISTER_TRUE(MCG_S, MCG_S_IREFST); // wait until the switch to the external RTC source has completed
MCG_C4 = ((MCG_C4 & ~(MCG_C4_DMX32 | MCG_C4_HIGH_RANGE)) | (_FLL_VALUE)); // adjust FLL factor to obtain the required operating frequency
Beware however that this works on Revision 2 devices only.
If you happen to have an old Revision 1 part (or generally can be used for compatibility)
SIM_SOPT2 |= SIM_SOPT2_MCGCLKSEL; // attempt to select external source (revision 1 parts)
if ((SIM_SOPT2 & SIM_SOPT2_MCGCLKSEL) == 0) { // rev 2 parts won't set this bit so we can tell that the setting must be performed in the MCG module instead
MCG_C7 = MCG_C7_OSCSEL_32K; // select the RTC clock as external clock input to the FLL or MCGOUTCLK
}
can be used in place of the second line
MCG_C7 = MCG_C7_OSCSEL_32K; // select the RTC clock as external clock input to the FLL or MCGOUTCLK
If your code is hanging waiting for the RTC output to be selected as input to the FLL it will usually mean that the RTC clock isn't operating. See also https://www.utasker.com/kinetis/MCG.html for a practical discussion of the MCG.
Regards
Mark
uTasker project developer for Kinetis and i.MX RT]
Contact me by personal message or on the uTasker web site to discuss professional training or product development requirements
