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

   sys_clock.c - Routine to set the system clock 
                      for the Lincoln 60
                      
                      by Micromint Support <support@micromint.com>    June-2011
  Copyright (c) 2008-2011 Micromint USA LLC  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
  3. Neither the name Micromint nor the names of its contributors may
     be used to endorse or promote products derived from this software
     without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/
#include "sys_clock.h"

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

 Set the System Clock

*******************************************************************************/
unsigned char set_sysclock(unsigned long ulFreq)
{
   switch (ulFreq) 
   {
     case 10000000: LPC_SC->CCLKCFG   = CLKCFG_10MHZ;
                    LPC_SC->PLL0CFG   = PLL0CFG_10MHZ; 
                    break;
     case 20000000: LPC_SC->CCLKCFG   = CLKCFG_20MHZ;
                    LPC_SC->PLL0CFG   = PLL0CFG_20MHZ; 
                    break;
     case 30000000: LPC_SC->CCLKCFG   = CLKCFG_30MHZ;
                    LPC_SC->PLL0CFG   = PLL0CFG_30MHZ; 
                    break;
     case 40000000: LPC_SC->CCLKCFG   = CLKCFG_40MHZ;
                    LPC_SC->PLL0CFG   = PLL0CFG_40MHZ; 
                    break;
     case 50000000: LPC_SC->CCLKCFG   = CLKCFG_50MHZ;
                    LPC_SC->PLL0CFG   = PLL0CFG_50MHZ; 
                    break;
     case 60000000: LPC_SC->CCLKCFG   = CLKCFG_60MHZ;
                    LPC_SC->PLL0CFG   = PLL0CFG_60MHZ; 
                    break;
     case 70000000: LPC_SC->CCLKCFG   = CLKCFG_70MHZ;
                    LPC_SC->PLL0CFG   = PLL0CFG_70MHZ; 
                    break;
     case 80000000: LPC_SC->CCLKCFG   = CLKCFG_80MHZ;
                    LPC_SC->PLL0CFG   = PLL0CFG_80MHZ; 
                    break;
     case 90000000: LPC_SC->CCLKCFG   = CLKCFG_90MHZ;
                    LPC_SC->PLL0CFG   = PLL0CFG_90MHZ; 
                    break;
     case 100000000: LPC_SC->CCLKCFG   = CLKCFG_100MHZ;
                     LPC_SC->PLL0CFG   = PLL0CFG_100MHZ; 
                     break;
     case 110000000: LPC_SC->CCLKCFG   = CLKCFG_110MHZ;
                     LPC_SC->PLL0CFG   = PLL0CFG_110MHZ; 
                     break;
     case 120000000: LPC_SC->CCLKCFG   = CLKCFG_120MHZ;
                     LPC_SC->PLL0CFG   = PLL0CFG_120MHZ; 
                    break;     
     default : return(0);
   }      
   LPC_SC->PLL0FEED  = 0xAA;
   LPC_SC->PLL0FEED  = 0x55;

   LPC_SC->PLL0CON   = 0x01;       
   LPC_SC->PLL0FEED  = 0xAA;
   LPC_SC->PLL0FEED  = 0x55;
   while (!(LPC_SC->PLL0STAT & (1<<26)));
   LPC_SC->PLL0CON   = 0x03;                      
   LPC_SC->PLL0FEED  = 0xAA;
   LPC_SC->PLL0FEED  = 0x55;
   while (!(LPC_SC->PLL0STAT & ((1<<25) | (1<<24))));
   SystemCoreClockUpdate ();     
   return(1);
}