My board is running with an external clock at 50MHz. The MCG and OSC are setup in the bootloader with the following code (from Keil Uvision startup file):
#elif (CLOCK_SETUP == 2)
/* SIM_CLKDIV1: OUTDIV1=0,OUTDIV2=0,OUTDIV3=1,OUTDIV4=1,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
SIM->CLKDIV1 = (uint32_t)0x00110000u; /* Update system prescalers */
/* Switch to FBE Mode */
/* OSC->CR: ERCLKEN=0,??=0,EREFSTEN=0,??=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
OSC->CR = (uint8_t)0x00u;
/* MCG->C7: OSCSEL=0 */
MCG->C7 = (uint8_t)0x00u;
/* MCG->C2: ??=0,??=0,RANGE0=2,HGO=0,EREFS=1,LP=0,IRCS=0 */
MCG->C2 = (uint8_t)0x24u;
/* MCG->C1: CLKS=2,FRDIV=5,IREFS=0,IRCLKEN=0,IREFSTEN=0 */
MCG->C1 = (uint8_t)0xA8u;
/* MCG->C4: DMX32=0,DRST_DRS=0 */
MCG->C4 &= (uint8_t)~(uint8_t)0xE0u;
/* MCG->C5: ??=0,PLLCLKEN=0,PLLSTEN=0,PRDIV0=0 */
MCG->C5 = (uint8_t)0x00u;
/* MCG->C6: LOLIE=0,PLLS=0,CME=0,VDIV0=0 */
MCG->C6 = (uint8_t)0x00u;
while((MCG->S & MCG_S_OSCINIT0_MASK) == 0u) { /* Check that the oscillator is running */
}
while((MCG->S & MCG_S_IREFST_MASK) != 0u) { /* Check that the source of the FLL reference clock is the external reference clock. */
}
while((MCG->S & 0x0Cu) != 0x08u) { /* Wait until external reference clock is selected as MCG output */
}
/* Switch to BLPE Mode */
/* MCG->C2: ??=0,??=0,RANGE0=2,HGO=0,EREFS=1,LP=0,IRCS=0 */
MCG->C2 = (uint8_t)0x24u;
#endif
I need to disable the oscillator in the main code so that I can use the XTAL pin as GPIO. I am trying to do this by setting the EREFS bit of MCG_C2 as seen in the following code, but occasionally I get a bus fault afterwards.
MCG->C2 &= ~MCG_C2_EREFS0_MASK; //set for external clock
while((MCG->S & MCG_S_OSCINIT0_MASK ) != 0x00u); /* Wait until external reference clock is selected as MCG output */
Is there some other sequence I need to perform to do this or some other status to wait for?
thanks
Thanks for your response. The post does make things a bit clearer, but I don't see that it addresses switching from external crystal to external clock.
For now I am switching back to the FEI state and then switching to FBE, then BLPE with the EREFS bit of MCG_C2 set to 0. So far I have not had any issues with this method.
maybe this post can help you
https://community.nxp.com/t5/Kinetis-Microcontrollers/Kinetis-System-Clocks/ta-p/1149854
Regards