/*
Matt Hobson
ELEN 599
Spring 2006
File name: _HC12Setup.c
File purpose:  This is the official startup file under ImageCraft's ICC12.
               Sets the critical startup parameters for the mc9s12dp256.

*******************************************************************************/

//	Setup routine for HCS12

#include <mc9s12dp256.h>	     //DP256 header file 

#define SEI() asm("sei \n");     //Set I bit macro  - Disable Interrupts
#define CLI() asm("cli \n");     //Clear I bit macro - Enable Interrupts

//Programming clock rates for 16Mhz crystal (MiniDragon+)

//#define EEDIV 0X4A     //ECLKDIV value, 190kHz programming @ 16MHz crystal
//#define FEDIV 0X4A     //FCLKDIV value, 190kHz programming @ 16Mhz crystal

//Programming clock rates for 4Mhz crystal (Axiom board)

#define EEDIV 0X15     //ECLKDIV value, 190kHz programming @ 4MHz crystal
#define FEDIV 0X15     //FCLKDIV value, 190kHz programming @ 4Mhz crystal

//**** FOR 16MHZ REFERENCE (CRYSTAL) FREQUENCY (ON WYTEC MINIDRAGON+)

//24Mhz Eclock constants (24Mhz bus frequency using PLL)

//#define CLKX 0x02	  //SYNR setting for 24MHZ bus operation using PLL
//#define CLKY 0x01	  //REFDV setting for 24MHZ bus operation using PLL

//**** FOR 4MHZ REFERENCE (CRYSTAL) FREQUENCY (ON AXIOM BOARDS)

//8MHz Eclock constants (8MHz bus frequency using PLL)
//#define CLKX 0x01   //SYNR setting for 8MHZ bus operation using PLL
//#define CLKY 0x00   //REFDV setting for 8MHZ bus operation using PLL

//16MHz Eclock constants (16MHz bus frequency using PLL)
//#define CLKX 0x03   //SYNR setting for 16MHZ bus operation using PLL
//#define CLKY 0x00   //REFDV setting for 16MHZ bus operation using PLL

//24MHz Eclock constants (24MHz bus frequency using PLL)
#define CLKX 0x05   //SYNR setting for 24MHZ bus operation using PLL
#define CLKY 0x00	//REFDV setting for 24MHZ bus operation using PLL

//**************************** STARTUP CODE *****************************

void _HC12Setup()
{
SEI();                     //Turn off interrupts
COPCTL = 0x00;     	       //Turn off COP Watchdog timer
ECLKDIV = EEDIV;    	   //Set EEPROM clock divider
FCLKDIV = FEDIV;    	   //Set Flash clock divider
CLKSEL &= ~0x80;		   //Clear bit 7, clock derived from oscclk
PLLCTL = 0x40;			   //Turn PLL on, bit 6=1 PLL on, bit 6=0 PLL off
SYNR = CLKX;        	   //Set PLL frequency 
REFDV = CLKY;			   //Set PLL frequency (refer to CRG Block user guide)
while (!(CRGFLG & 0x08))   //Wait for PLL LOCK
{;}
CLKSEL |= 0x80;            //Use PLL as main clock
while (!(CRGFLG & 0x08))   //Double check PLL LOCK
{;}	  			  		   //Sometimes it doesn't quite lock
CLKSEL |= 0x80;            //Use PLL as main clock               
}

//***********************************************************************