📄 2460lib.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "type.h"
#include "Option.h"
#include "2460addr.h"
#include "2460slib.h"
//===========================[ SYSTEM ]===================================================
static int delayLoopCount;
unsigned int MPLL;
unsigned int ARMCLK, HCLK, PCLK;
unsigned int ARMCLKdiv, HCLKdiv, PCLKdiv;
void GlobalCLK( void)
{
unsigned int mdiv, pdiv, sdiv;
mdiv = (rMPLLCON>>16)&0xff;
pdiv = (rMPLLCON>>8)&0x3f;
sdiv = (rMPLLCON&0x3);
MPLL = (mdiv+8)*FIN / (pdiv+2);
MPLL >>= sdiv;
ARMCLKdiv = (rCLKDIVCON>>16)&0x1;
HCLKdiv = rCLKDIVCON&0x3;
PCLKdiv = (rCLKDIVCON>>2)&0x1;
ARMCLK = MPLL >> ARMCLKdiv;
HCLK = ARMCLK / (HCLKdiv+1);
PCLK = HCLK >> PCLKdiv;
}
void Delay(int time)
{
// time=0: adjust the Delay function by WatchDog timer.
// time>0: the number of loop time
// resolution of time is 100us.
int i,adjust=0;
if(time==0) {
time = 200;
adjust = 1;
delayLoopCount = 400;
//PCLK/1M,Watch-dog disable,1/64,interrupt disable,reset disable
rWTCON = ((PCLK/1000000-1)<<8)|(2<<3);
rWTDAT = 0xffff; //for first update
rWTCNT = 0xffff; //resolution=64us @any PCLK
rWTCON = ((PCLK/1000000-1)<<8)|(2<<3)|(1<<5); //Watch-dog timer start
}
for(;time>0;time--)
for(i=0;i<delayLoopCount;i++);
if(adjust==1) {
rWTCON = ((PCLK/1000000-1)<<8)|(2<<3); //Watch-dog timer stop
i = 0xffff - rWTCNT; // 1count->64us, 200*400 cycle runtime = 64*i us
delayLoopCount = 8000000/(i*64); //200*400:64*i=1*x:100 -> x=80000*100/(64*i)
}
}
//=====================================================================
int GetIntNum(void)
{
char str[30];
char *string = str;
int base = 10;
int minus = 0;
int result = 0;
int lastIndex;
int i;
gets(string);
if(string[0]=='-') {
minus = 1;
string++;
}
if(string[0]=='0' && (string[1]=='x' || string[1]=='X')) {
base = 16;
string += 2;
}
lastIndex = strlen(string) - 1;
if(lastIndex<0)
return -1;
if(string[lastIndex]=='h' || string[lastIndex]=='H' ) {
base = 16;
string[lastIndex] = 0;
lastIndex--;
}
if(base==10) {
result = atoi(string);
result = minus ? (-1*result):result;
}
else {
for(i=0;i<=lastIndex;i++) {
if(isalpha(string[i])) {
if(isupper(string[i]))
result = (result<<4) + string[i] - 'A' + 10;
else
result = (result<<4) + string[i] - 'a' + 10;
}
else
result = (result<<4) + string[i] - '0';
}
result = minus ? (-1*result):result;
}
return result;
}
//========================**[ BOARD LED ]=================================
void Init_LED( void)
{
rGPJCON = (rGPJCON&0xff0000)|0x550000;
}
void Led_Display(int data)
{
rGPJDAT = (rGPJDAT & ~(0xf00)) | ((data & 0xf)<<8) ;
}
void Led_Display_Temp()
{
static char var=0;
if(var==0)
{
rGPJDAT &= ~ 0x100;
var = 1;
}
else
{
rGPJDAT |= 0x100;
var = 0;
}
}
//===========================[ UART ]==============================
void Uart_Init(int pclk,int baud)
{
rGPHCON = 0xa;
if(pclk == 0) pclk = PCLK;
rUFCON0 = 0x0; //UART channel 0 FIFO control register, FIFO disable
rUMCON0 = 0x0; //UART chaneel 0 MODEM control register, AFC disable
rULCON0 = 0x3; //Line control register : Normal,No parity,1 stop,8 bits
// [10] [9] [8] [7] [6] [5] [4] [3:2] [1:0]
// Clock Sel Tx Int Rx Int Rx Time Out Rx err Loop-back Send break Transmit Mode Receive Mode
// 0 1 0 0 1 0 0 01 01
// PCLK Level Pulse Disable Generate Normal Normal Interrupt or Polling
rUCON0 = 0x245; // Control register
rUBRDIV0=( (int)(pclk/16./baud+0.5) -1 ); //Baud rate divisior register 0
//rUBRDIV0=( (int)(pclk/16./baud) -1 ); //Baud rate divisior register 0, in 5410
// rUDIVSLOT0 = 0x6db7;
}
int Uart_putc(int c)
{
if(c=='\n') {
while(!(rUTRSTAT0 & 0x2));
Delay(10); //because the slow response of hyper_terminal
WrUTXH0('\r');
}
while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
Delay(10);
WrUTXH0(c);
return c;
}
int Uart_puts( const char *s)
{
while(*s!=0) Uart_putc( *s++);
return 0;
}
int Uart_getc( void)
{
int ch;
while(!(rUTRSTAT0 & 0x1)); //Receive data ready
ch=RdURXH0();
return ch;
}
int Uart_GetKey( void)
{
if(rUTRSTAT0 & 0x1) //Receive data ready
return RdURXH0();
else
return 0;
}
//===================================================================
void HaltUndef(void)
{
printf("Undefined instruction exception.\n");
while(1);
}
//===================================================================
void HaltSwi(void)
{
printf("SWI exception.\n");
while(1);
}
//===================================================================
void HaltPabort(void)
{
printf("IFSR=0x%x\n",MMU_ReadIFSR());
printf("FAR=0x%x\n",MMU_ReadFAR());
printf("Pabort exception.\n");
while(1);
}
//===================================================================
void HaltDabort(void)
{
printf("DFSR=0x%x\n",MMU_ReadDFSR());
printf("FAR=0x%x\n",MMU_ReadFAR());
printf("Dabort exception.\n");
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -