void system_clock_init(void)
{
+
+ // oscilator must be running, before PLL changes
+ SCS = SCS_GPIOM | SCS_OSCEN;
+ // wait for main clock to be ready to use
+ while (!(SCS & SCS_OSCSTAT))
+ continue;
+
+ CLKSRCSEL = 1; //source is XTAL
+
// set PLL multiplier & divisor.
// values computed from config.h
PLLCFG = PLLCFG_MSEL | PLLCFG_PSEL;
PLLCON = PLLCON_PLLE;
PLLFEED = 0xAA; // Make it happen. These two updates
PLLFEED = 0x55; // MUST occur in sequence.
-
- CCLKCFG = HCLK_DIV_CPU; //only odd values have to be used
- USBCLKCFG = HCLK_DIV_USB; //only odd values have to be used
+
+ // Change the CPU Clock Divider setting for the operation with the PLL. It's critical to do this before connecting the PLL.
+ CCLKCFG = HCLK_DIV_CPU; //only 0 and odd values have to be used
+ USBCLKCFG = HCLK_DIV_USB; //only 0 and odd values have to be used
// wait for PLL lock
while (!(PLLSTAT & PLLSTAT_LOCK))
continue;
-
+
// enable & connect PLL
PLLCON = PLLCON_PLLE | PLLCON_PLLC;
PLLFEED = 0xAA; // Make it happen. These two updates
- PLLFEED = 0x55; // MUST occur in sequence.
-
- SCS = SCS_GPIOM | SCS_OSCEN;
- // wait for main clock to be ready to use
- while (!(SCS & SCS_OSCSTAT))
- continue;
-
- CCLKCFG = 0; //only 0 and odd values have to be used
- CLKSRCSEL = 1; //source is XTAL
-
+ PLLFEED = 0x55; // MUST occur in sequence.
+
system_frequency=CCLK;
// setup & enable the MAM