// if (clk_option > 3) {return 0;} //return 0 if one of the available options is not selected
// if (crystal_val > 15) {return 1;} // return 1 if one of the available crystal options is not available
//This assumes that the MCG is in default FEI mode out of reset.
reg = MCG_S;
//========FEI to FBE mode============
// Enable external oscillator, RANGE=1 (1~8M oscillator), HGO=1, EREFS=1(oscillator request), LP=0, IRCS=0
MCG_C2 = MCG_C2_RANGE(1) | MCG_C2_HGO_MASK | MCG_C2_EREFS_MASK;
// Select external oscillator and Reference Divider and clear IREFS to start ext osc
// CLKS=2(external reference clk enable), FRDIV=2, IREFS=0
MCG_C1 = MCG_C1_CLKS(2) | MCG_C1_FRDIV(2);
while (!(MCG_S & MCG_S_OSCINIT_MASK)){reg = MCG_S ;}; //wait for OSCINIT to be set wait for crystal selected by C2[EREFS] has been initialized
while (MCG_S & MCG_S_IREFST_MASK){}; // wait for Reference clock Status bit to clear
while (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0x2){}; // Wait for clock status bits to show clock source is ext ref clk