/*****************************************************************************
* target.c: Target C file for NXP LPC23xx/24xx Family Microprocessors
*
* Copyright(C) 2006, NXP Semiconductor
* All rights reserved.
*
* History
* 2006.07.13 ver 1.00 Prelimnary version, first Release
*
*****************************************************************************/
#include <nxp/iolpc2468.h>
#include "type.h"
#include "irq.h"
#include "target.h"
#include <intrinsics.h>
#define FIN (12<<6) //32.768khz~50mhz
#define FCCO (288<<6) //275~500mhz
#define RequireFreq (60<<6)
/******************************************************************************
** Function name: GPIOResetInit
**
** Descriptions: Initialize the target board before running the main()
** function; User may change it as needed, but may not
** deleted it.
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void GPIOResetInit( void )
{
/* Reset all GPIO pins to default: primary function */
PINSEL0 = 0x00000000;
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000000;
PINSEL3 = 0x00000000;
PINSEL4 = 0x00000000;
PINSEL5 = 0x00000000;
PINSEL6 = 0x00000000;
PINSEL7 = 0x00000000;
PINSEL8 = 0x00000000;
PINSEL9 = 0x00000000;
PINSEL10 = 0x00000000;
IO0DIR = 0x00000000;
IO1DIR = 0x00000000;
FIO0DIR = 0x00000000;
FIO1DIR = 0x00000000;
FIO2DIR = 0x00000000;
FIO3DIR = 0x00000000;
FIO4DIR = 0x00000000;
FIO0MASK = 0x00000000;
FIO1MASK = 0x00000000;
FIO2MASK = 0x00000000;
FIO3MASK = 0x00000000;
FIO4MASK = 0x00000000;
SCS_bit.GPIOM = 0; //关闭0、1端口的高速IO功能
return;
}
/******************************************************************************
** Function name: ConfigurePLL
**
** Descriptions: Configure PLL switching to main OSC instead of IRC
** at power up and wake up from power down.
** This routine is used in TargetResetInit() and those
** examples using power down and wake up such as
** USB suspend to resume, ethernet WOL, and power management
** example
** parameters: None
** Returned value: None
**
******************************************************************************/
void ConfigurePLL ( void )
{
DWORD MValue, NValue;
if ( PLLSTAT & (1 << 25) )
{
PLLCON = 1; /* Enable PLL, disconnected */
PLLFEED = 0xaa;
PLLFEED = 0x55;
}
PLLCON = 0; /* Disable PLL, disconnected */
PLLFEED = 0xaa;
PLLFEED = 0x55;
SCS |= 0x20; /* Enable main OSC */
while( !(SCS & 0x40) ); /* Wait until main OSC is usable */
CLKSRCSEL = 0x1; /* select main OSC, 12MHz, as the PLL clock source */
PLLCFG = PLL_MValue | (PLL_NValue << 16);
PLLFEED = 0xaa;
PLLFEED = 0x55;
PLLCON = 1; /* Enable PLL, disconnected */
PLLFEED = 0xaa;
PLLFEED = 0x55;
CCLKCFG = CCLKDivValue; /* Set clock divider */
#if USE_USB
USBCLKCFG = USBCLKDivValue; /* usbclk = 288 MHz/6 = 48 MHz */
#endif
while ( ((PLLSTAT & (1 << 26)) == 0) ); /* Check lock bit status */
MValue = PLLSTAT & 0x00007FFF;
NValue = (PLLSTAT & 0x00FF0000) >> 16;
while ((MValue != PLL_MValue) && ( NValue != PLL_NValue) );
PLLCON = 3; /* enable and connect */
PLLFEED = 0xaa;
PLLFEED = 0x55;
while ( ((PLLSTAT & (1 << 25)) == 0) ); /* Check connect bit status */
return;
}
void SET_PLL(){
unsigned int MValue, NValue;
unsigned int MValue1, NValue1;
NValue=1;
MValue=FCCO*NValue/(2*FIN);
//(1)若PLL被连接,用馈送序列断开与PLL连接。
if((PLLSTAT & (1 << 25)) == 1){
PLLCON &= 0xfffffffe;
PLLFEED = 0xAA;
PLLFEED = 0x55;
}
//(2)用馈送序列禁止PLL
PLLCON &= 0xfffffffd;
PLLFEED = 0xAA;
PLLFEED = 0x55;
//(3)主晶振使能,并选择主时钟作为PLL时钟源
SCS |= (1 << 5);
while(SCS & (1 << 6) == 0);//等待主晶振稳定
CLKSRCSEL = 0x01;
//(4)写PLLCFG,并用馈送序列使其有效
PLLCFG = (MValue-1) | ((NValue-1)<<16);
PLLFEED = 0xAA;
PLLFEED = 0x55;
//(5)用一个馈送序列使能PLL
PLLCON |= 0x01;
PLLFEED = 0xAA;
PLLFEED = 0x55;
//(6)改变CPU时钟分频器设置使之与PLL一起操作
CCLKCFG = (FCCO / RequireFreq);
//(7)通过监控PLLSTAT寄存器的PLOCK位来等待PLL实现锁定,变为1则表明锁定
while((PLLSTAT & ( 1<<26 )) == 0);//用(1<<26)而不用0x4000
MValue1 = PLLSTAT & 0x00007FFF;
NValue1 = (PLLSTAT & 0x00FF0000) >> 16;
while ((MValue1 != ( MValue-1 )) && ( NValue1 != ( NValue-1 )) );
//(8)用一个馈送序列连接PLL
PLLCON |= 0x03;
PLLFEED = 0xAA;
PLLFEED = 0x55;
while ( ((PLLSTAT & (1 << 25)) == 0) ); /* Check connect bit status */
return;
}
/******************************************************************************
** Function name: TargetResetInit
**
** Descriptions: Initialize the target board before running the main()
** function; User may change it as needed, but may not
** deleted it.
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void TargetResetInit(void)
{
/* Set memory accelerater module*/
MAMCR = 0;
#if Fcclk < 20000000
MAMTIM = 3;
#else
#if Fcclk < 40000000
MAMTIM = 4;
#else
MAMTIM = 5;
#endif
#endif
MAMCR = 2;
#if USE_USB
PCONP |= 0x80000000; /* Turn On USB PCLK */
#endif
/* Set system timers for each component */
#if (Fpclk / (Fcclk / 4)) == 1
PCLKSEL0 = 0x00000000; /* PCLK is 1/4 CCLK */
PCLKSEL1 = 0x00000000;
#endif
#if (Fpclk / (Fcclk / 4)) == 2
PCLKSEL0 = 0xAAAAAAAA; /* PCLK is 1/2 CCLK */
PCLKSEL1 = 0xAAAAAAAA;
#endif
#if (Fpclk / (Fcclk / 4)) == 4
PCLKSEL0 = 0x55555555; /* PCLK is the same as CCLK */
PCLKSEL1 = 0x55555555;
#endif
/* Configure PLL, switch from IRC to Main OSC */
SET_PLL();
GPIOResetInit();
init_VIC();
return;
}
/******************************************************************************
** End Of File
******************************************************************************/