Hi, Hui,
I have tested the following code on my TWR-K20D50M board on which the 8MHz crystal is connected to the EXTAL/XTAL pins, I confirm the both of the function:static void Cpu_SetMCGModeFBE(uint8_t CLKMode) and static void Cpu_SetMCGModeFBI(uint8_t CLKMode) do not have problem. I do not use MQX and do not use VLPR mode either.
This is the mode transition FEI->FBI->FBE mode.
If you still have issue, pls develop a simple project which can duplicate your issue and paste it here so that we can test on K20 Tower board.
BR
Xiangjun Rong
This is my test code:
static void Cpu_SetMCGModeFBE(uint8_t CLKMode);
static void Cpu_SetMCGModeFBI(uint8_t CLKMode);
static int i = 0;
int main(void)
{
/* Write your code here */
//from FBI to FBE mode, the K20 is in FEI mode after reset
Cpu_SetMCGModeFBI(0);
Cpu_SetMCGModeFBE(0);
/* This for loop should be replaced. By default this loop allows a single stepping. */
for (;;) {
i++;
}
/* Never leave main */
return 0;
}
static void Cpu_SetMCGModeFBI(uint8_t CLKMode)
{
switch (CLKMode) {
case 0U:
/* Switch to FEI Mode */
/* MCG_C1: CLKS=1,FRDIV=0,IREFS=1,IRCLKEN=0,IREFSTEN=0 */
MCG->C1 = (uint8_t)0x44U;
/* MCG_C2: LOCRE0=0,??=0,RANGE0=2,HGO0=1,EREFS0=1,LP=0,IRCS=1 */
MCG->C2 = (uint8_t)0x2DU;
/* MCG_C4: DMX32=0,DRST_DRS=0 */
MCG->C4 &= (uint8_t)~(uint8_t)0xE0U;
/* OSC0_CR: ERCLKEN=1,??=0,EREFSTEN=0,??=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
OSC0->CR = (uint8_t)0x80U;
/* MCG_C7: OSCSEL=0 */
MCG->C7 &= (uint8_t)~(uint8_t)0x01U;
/* MCG_C5: ??=0,PLLCLKEN0=0,PLLSTEN0=0,PRDIV0=3 */
MCG->C5 = (uint8_t)0x03U;
/* MCG_C6: LOLIE0=0,PLLS=0,CME0=0,VDIV0=0x18 */
MCG->C6 = (uint8_t)0x18U;
while((MCG->S & MCG_S_IREFST_MASK) == 0x00U) { /* Check that the source of the FLL reference clock is the internal reference clock. */
}
while((MCG->S & 0x0CU) != 0x04U) { /* Wait until internal reference clock is selected as MCG output */
}
break;
default:
break;
}
}
static void Cpu_SetMCGModeFBE(uint8_t CLKMode)
{
switch (CLKMode) {
case 0U:
/* Switch to FBE Mode */
/* OSC0_CR: ERCLKEN=1,??=0,EREFSTEN=0,??=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
OSC0->CR = (uint8_t)0x80U;
/* MCG_C7: OSCSEL=0 */
MCG->C7 &= (uint8_t)~(uint8_t)0x01U;
/* MCG_C2: LOCRE0=0,??=0,RANGE0=2,HGO0=1,EREFS0=1,LP=0,IRCS=1 */
MCG->C2 = (uint8_t)0x2DU;
/* MCG_C1: CLKS=2,FRDIV=3,IREFS=0,IRCLKEN=0,IREFSTEN=0 */
MCG->C1 = (uint8_t)0x98U;
/* MCG_C4: DMX32=0,DRST_DRS=0 */
MCG->C4 &= (uint8_t)~(uint8_t)0xE0U;
/* MCG_C5: ??=0,PLLCLKEN0=0,PLLSTEN0=0,PRDIV0=3 */
MCG->C5 = (uint8_t)0x03U;
/* MCG_C6: LOLIE0=0,PLLS=0,CME0=0,VDIV0=0x18 */
MCG->C6 = (uint8_t)0x18U;
while((MCG->S & MCG_S_OSCINIT0_MASK) == 0x00U) { /* Check that the oscillator is running */
}
while((MCG->S & MCG_S_IREFST_MASK) != 0x00U) { /* 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 */
}
break;
default:
break;
}
}
////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////