/****************************************************************
NAME: u2440mon.c
DESC: u2440mon entry point,menu,download
****************************************************************/
#define GLOBAL_CLK 1
#include <stdlib.h>
#include <string.h>
#include "def.h" ////声明数据类型
#include "option.h" ////声明系统时钟的寄存器 PCLK,FCLK,UCLK等
#include "2440addr.h" ////声明寄存器,中断源,中断清除函数
#include "2440lib.h" ////声明UARTDelay,蜂鸣器,改变系统时钟等函数?
#include "2440slib.h" ////MMU
#include "mmu.h"
#include "profile.h" ////声明了timer的几个函数
#include "memtest.h" ////声明了mentest这个函数
extern char Image$$RO$$Limit[];
extern char Image$$RO$$Base[];
extern char Image$$RW$$Limit[];
extern char Image$$RW$$Base[];
extern char Image$$ZI$$Limit[];
extern char Image$$ZI$$Base[];
void Isr_Init(void);
/*
void HaltUndef(void);
void HaltSwi(void);
void HaltPabort(void);
void HaltDabort(void);
*/
int consoleNum;
/////////////////syetem clock//////////////////////////////
static U32 cpu_freq;
static U32 UPLL;
void SetSysClock(U8 uClockType,U8 *pkey,U32 *pmpll_val)
{
switch ( uClockType )
{
case 0: //200
*pkey= 12;
*pmpll_val= (92<<12)|(4<<4)|(1);
break;
case 1: //300
*pkey= 13;
*pmpll_val = (67<<12)|(1<<4)|(1);
break;
case 2: //400
*pkey= 14;
*pmpll_val= (92<<12)|(1<<4)|(1);
break;
case 3: //440!!!
*pkey = 14;
*pmpll_val = (102<<12)|(1<<4)|(1);
break;
default:
*pkey = 14;
*pmpll_val = (92<<12)|(1<<4)|(1);
break;
}
}
static void CalCpuBusClk(U32 *pFCLK, U32 *pHCLK ,U32 *pPCLK ,U32 *pUCLK,U32 *pcpu_freq,U32 *pUPLL)
{
U32 val;
U8 m, p, s;
val = rMPLLCON;
m = (val>>12)&0xff;
p = (val>>4)&0x3f;
s = val&3;
//(m+8)*FIN*2 不要超出32位数!
*pFCLK = ((m+8)*(FIN/100)*2)/((p+2)*(1<<s))*100;
val = rCLKDIVN;
m = (val>>1)&3;
p = val&1;
val = rCAMDIVN;
s = val>>8;
switch (m)
{
case 0:
*pHCLK = *pFCLK;
break;
case 1:
*pHCLK = *pFCLK>>1;
break;
case 2:
if(s&2)
*pHCLK =*pFCLK>>3;
else
*pHCLK = *pFCLK>>2;
break;
case 3:
if(s&1)
*pHCLK = *pFCLK/6;
else
*pHCLK = *pFCLK/3;
break;
default:break;
}
if(p)
*pPCLK = *pHCLK>>1;
else
*pPCLK = *pHCLK;
if(s&0x10)
*pcpu_freq =*pHCLK;
else
*pcpu_freq = *pFCLK;
val = rUPLLCON;
m = (val>>12)&0xff;
p = (val>>4)&0x3f;
s = val&3;
*pUPLL = ((m+8)*FIN)/((p+2)*(1<<s));
*pUCLK = (rCLKDIVN&8)?(*pUPLL>>1):*pUPLL;
}
////////////////////////////LED///////////////////////////////////
#define GPB_PIN5 (1<<5) //LED1
#define GPB_PIN6 (1<<6) //LED2
#define GPB_PIN7 (1<<7) //LED3
#define GPB_PIN8 (1<<8) //LED4
void Delay2(void)
{
int i;
for(i=0;i<300000;i++)
{;}
}
void set_pin_GPB(U32 pin_num)
{
rGPBDAT|=pin_num;
}
void clr_pin_GPB(U32 pin_num)
{
rGPBDAT&=~(pin_num);
}
static void __irq Key_ISR0(void)
{
U32 r;
EnterCritical(&r);
ClearPending(BIT_EINT0);
Uart_Printf(" interrupt occur\n");
clr_pin_GPB(GPB_PIN5);
Delay2();
ExitCritical(&r);
}
void config_GPB5678_output(void)
{
rGPBCON&=~((3<<10)|(3<<12)|(3<<14)|(3<<16));
rGPBCON|=(1<<10)|(1<<12)|(1<<14)|(1<<16);
rGPBUP&=~((1<<5)|(1<<6)|(1<<7)|(1<<8));
}
///////////////////////Extern Inerrupt 0/////////////////////
void config_CPF0_int(void)
{
rGPFCON&=~(3<<0);
rGPFCON|=(2<<0); //设置GPF0为外部中断
rEXTINT0&=~(7<<0); //设置为低电平触发
ClearPending(BIT_EINT0);
pISR_EINT0=(U32)Key_ISR0;
EnableIrq(BIT_EINT0);
}
//////////////////////key//////////////////////////////////////
#define ERROR 0xFF
#define GPF_PIN0 0
#define GPF_PIN1 1
#define GPF_PIN2 2
#define GPF_PIN4 4
void config_GPF0124_input(void)
{
rGPFCON&=~((3<<0)|(3<<2)|(3<<4)|(3<<8));
rGPFUP&=~((1<<0)|(1<<1)|(1<<2)|(1<<4));
}
U8 read_pin_GPF0124(U8 pin_num)
{
U8 temp;
switch(pin_num)
{
case 0:return rGPFDAT&0x01;
case 1:return (rGPFDAT&0x02)>>1;
case 2:return (rGPFDAT&0x04)>>2;
case 4:return (rGPFDAT&0x10)>>4;
default:return ERROR;
}
}
//////////////////////TIMER0//////////////////////////////
/*
#define pISR_TIMER0 (*(unsigned *)(_ISR_STARTADDRESS+0x48))
#define rTCFG0 (*(volatile unsigned *)0x51000000) //Timer 0 configuration
#define rTCFG1 (*(volatile unsigned *)0x51000004) //Timer 1 configuration
#define rTCON (*(volatile unsigned *)0x51000008) //Timer control
#define rTCNTB0 (*(volatile unsigned *)0x5100000c) //Timer count buffer 0
#define rTCMPB0 (*(volatile unsigned *)0x51000010) //Timer compare buffer 0
#define rTCNTO0 (*(volatile unsigned *)0x51000014) //Timer count observation 0
#define BIT_TIMER0 (0x1<<10)
*/
static void __irq Timer4_ISR()
{
U32 r;
EnterCritical(&r);
ClearPending(BIT_TIMER4);
clr_pin_GPB(GPB_PIN7);
Delay2();
ExitCritical(&r);
}
static void __irq Timer0_ISR()
{
U32 r;
static U8 flag=TRUE;
EnterCritical(&r);
ClearPending(BIT_TIMER0);
if(flag)
{
clr_pin_GPB(GPB_PIN5);
}
else
{
set_pin_GPB(GPB_PIN5);
}
flag=!flag;
ExitCritical(&r);
}
void ConfigTimer4()
{
ClearPending(BIT_TIMER4);
EnableIrq(BIT_TIMER4);
rTCFG0 &= 0xFF00FF;
rTCFG0 |= 0xf900; // prescaler等于249+1
rTCFG1 &= ~0xF0000;
rTCFG1 |= 0x20000; //divider等于8,则设置定时器4的时钟频率为25kHz
rTCNTB4 = 12500; //让定时器4每隔0.5秒中断一次
rTCON &= ~0xF00000;
rTCON |= 0x700000;
rTCON &= ~0x200000 ; //定时器4开始工作
pISR_TIMER4 = (U32)Timer4_ISR;
}
void ConfigTimer0()
{
ClearPending(BIT_TIMER0);
EnableIrq(BIT_TIMER0);
rTCFG0 &= ~0x0000ff;
rTCFG0 |= 0x0000f9; //prescaler = 249+1
rTCFG1 &= ~0x00000f;
rTCFG1 |= 0x000002;//mux = 1/16
rTCNTB0 =12500*2;
rTCON &= ~0x00001f;
rTCON |= 0x00000f;
rTCON &= ~0x000002; //clear manual update bit
pISR_TIMER0=(U32)Timer0_ISR;
}
void Main(void)
{
char *mode;
int i;
static U8 key;
static U32 mpll_val = 0 ;
int SendDataSum=0;
//U32 divn_upll = 0 ;
U8 KeyNewStatue=FALSE;
U8 KeyOldStatue=FALSE;
//******************dont remove**********************//
rDSC0 = 0x2aa;
rDSC1 = 0x2aaaaaaa;
//Enable NAND, USBD, PWM TImer, UART0,1 and GPIO clock,
//the others must be enabled in OS!!!
rCLKCON = 0xfffff0;
MMU_Init();
//**************************************************//
Port_Init();
i = 2 ; //don't use 100M!
SetSysClock(i,&key,&mpll_val); //key= 14; mpll_val= (92<<12)|(1<<4)|(1); FLCK=400MHz
//init FCLK=400M, so change MPLL first
ChangeMPllValue((mpll_val>>12)&0xff, (mpll_val>>4)&0x3f, mpll_val&3);///
ChangeClockDivider(key, 12); //key=14, FCLK:HCLK:PCLK=1:4:8
//cal_cpu_bus_clk();
CalCpuBusClk(&FCLK, &HCLK ,&PCLK ,&UCLK,&cpu_freq,&UPLL) ;
consoleNum = 0; // Uart 1 select for debug.
Uart_Init( 0,115200 );
Uart_Select( consoleNum );
//ConfigTimer0(200);
// Beep(2000, 100);
//Buzzer_Freq_Set(200);
/*
rSRCPND = rSRCPND | (0x1<<14);
rINTPND = rINTPND | (0x1<<14);
rINTMSK = ~(0x1<<14); //打开定时器4中断
*/
// ConfigTimer4();
ConfigTimer0();
config_GPB5678_output();
Uart_Printf("\nFCLK is %10d,HCLK is %10d,PCLK is %d,UCLK is %d",FCLK,HCLK,PCLK,UCLK);
while(1)
{
set_pin_GPB(GPB_PIN6|GPB_PIN7|GPB_PIN8);
Delay(500);
clr_pin_GPB(GPB_PIN6|GPB_PIN7|GPB_PIN8);
Delay(500);
// Uart_Printf("\n%d", rTCNTO0&0xffff);
}
}
void Isr_Init(void)
{
/*
pISR_UNDEF=(unsigned)HaltUndef;
pISR_SWI =(unsigned)HaltSwi;
pISR_PABORT=(unsigned)HaltPabort;
pISR_DABORT=(unsigned)HaltDabort;
*/
rINTMOD=0x0; // All=IRQ mode
rINTMSK=BIT_ALLMSK; // All interrupt is masked.
}
- 1
- 2
前往页