|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2010-5-19 14:24:09
|
显示全部楼层
#include "System.h"
//===========================[ UART ]==============================
volatile unsigned int DebugUartCh=1;
void SelectDebugUart(unsigned int ch) // ch0 ~ ch3. added by junon 060530
{
DebugUartCh = ch;
if (ch>3) DebugUartCh = 1; // default UART channel 1
}
#ifdef __NANDBL
void InitDebugUart(unsigned int baud) // edited by junon 060530
{
int ch;
UART_REGS *pUartRegs;
unsigned int temp;
// select UART function
rUCON0 &= ~(0x3);//receive disable before gpio set to prevent dummy data input
rUCON1 &= ~(0x3);//receive disable
rUCON2 &= ~(0x3);//receive disable
rUCON3 &= ~(0x3);//receive disable
temp = (rGPHCON & ~0xffff ) | 0xaaaa;
rGPHCON = temp;//set uart mode, keep uart mode if it was to prevent gpio set to input, uart
//it will cause cause rx fifo dummy data input.
for (ch=0;ch<4;ch++)
{
pUartRegs = (UART_REGS *)(UART_REG_BASE+UART_REG_OFFSET*ch);
pUartRegs->rUlCon = 0x3; //Line control register : Normal,No parity,1 stop,8 bits
// 11] [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
// 1 0 0 0 , 0 1 0 0 , 01 01
// PCLK2 Pulse Pulse Disable Generate Normal Normal Interrupt or Polling
pUartRegs->rUCon = 0x845;
//pUartRegs->rUbrDiv = ((int)(PCLK/16./baud+0.5)-1); //Baud rate divisior register 0
pUartRegs->rUbrDiv = 26; //Baud rate divisior register 0
pUartRegs->rUfCon = 0x6; //UART FIFO control register, FIFO disable, fifo reset
pUartRegs->rUmCon = 0x0; //UART MODEM control register, AFC disable
}
}
#else
void InitDebugUart(unsigned int baud) // edited by junon 060530
{
int ch;
float DIV_VAL;
int UDIV_SLOT;
volatile UART_REGS *pUartRegs;
unsigned int temp;
// select UART function
rUCON0 &= ~(0x3);//receive disable before gpio set to prevent dummy data input
rUCON1 &= ~(0x3);//receive disable
rUCON2 &= ~(0x3);//receive disable
rUCON3 &= ~(0x3);//receive disable
temp = (rGPHCON & ~0xffff ) | 0xaaaa;
rGPHCON = temp;//set uart mode, keep uart mode if it was to prevent gpio set to input, uart
//it will cause cause rx fifo dummy data input.
for (ch=0;ch<4;ch++)
{
pUartRegs = (UART_REGS *)(UART_REG_BASE+UART_REG_OFFSET*ch);
pUartRegs->rUlCon = 0x3; //Line control register : Normal,No parity,1 stop,8 bits
// 11] [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
// 1 0 0 0 , 0 1 0 0 , 01 01
// PCLK2 Pulse Pulse Disable Generate Normal Normal Interrupt or Polling
/*
pUartRegs->rUCon = 0x245;
pUartRegs->rUbrDiv = ((int)(PCLK/16./baud+0.5)-1); //Baud rate divisior register 0
pUartRegs->rUfCon = 0x0; //UART FIFO control register, FIFO disable
pUartRegs->rUmCon = 0x0; //UART MODEM control register, AFC disable
pUartRegs->rUdivSlot= 0x54AA; //UART MODEM control register, AFC disable
*/
DIV_VAL = ((float)PCLK/16./baud)-1;
pUartRegs->rUCon = 0x845;
pUartRegs->rUbrDiv = (int)DIV_VAL; //Baud rate divisior register 0
pUartRegs->rUfCon = 0x6; //UART FIFO control register, FIFO disable, tx, rx fifo reset
pUartRegs->rUmCon = 0x0; //UART MODEM control register, AFC disable
UDIV_SLOT = (int)( (DIV_VAL-(int)DIV_VAL)*16 );
switch (UDIV_SLOT) {
case 0 : pUartRegs->rUdivSlot = 0; break;
case 1 : pUartRegs->rUdivSlot = 0x0080; break;
case 2 : pUartRegs->rUdivSlot = 0x0808; break;
case 3 : pUartRegs->rUdivSlot = 0x0888; break;
case 4 : pUartRegs->rUdivSlot = 0x2222; break;
case 5 : pUartRegs->rUdivSlot = 0x4924; break;
case 6 : pUartRegs->rUdivSlot = 0x4a52; break;
case 7 : pUartRegs->rUdivSlot = 0x54aa; break;
case 8 : pUartRegs->rUdivSlot = 0x5555; break;
case 9 : pUartRegs->rUdivSlot = 0xd555; break;
case 10 : pUartRegs->rUdivSlot = 0xd5d5; break;
case 11 : pUartRegs->rUdivSlot = 0xddd5; break;
case 12 : pUartRegs->rUdivSlot = 0xdddd; break;
case 13 : pUartRegs->rUdivSlot = 0xdfdd; break;
case 14 : pUartRegs->rUdivSlot = 0xdfdf; break;
case 15 : pUartRegs->rUdivSlot = 0xffdf; break;
}
}
// printf("DIV_VAL:%f , UDIV_SLOT:%d\n", DIV_VAL, UDIV_SLOT);
}
#endif
unsigned int Uart_putc(unsigned int c) // edited by junon 060530
{
volatile UART_REGS *pUartRegs;
pUartRegs = (UART_REGS *)(UART_REG_BASE+UART_REG_OFFSET*DebugUartCh);
if(c=='\n') {
while(!(pUartRegs->rUtrStat & 0x2));
pUartRegs->rUtxh = '\r';
}
while(!(pUartRegs->rUtrStat & 0x2)); //Wait until THR is empty.
pUartRegs->rUtxh = c;
return c;
}
unsigned int Uart_getc(void) // edited by junon 060530
{
unsigned int chRx;
volatile UART_REGS *pUartRegs;
pUartRegs = (UART_REGS *)(UART_REG_BASE+UART_REG_OFFSET*DebugUartCh);
while(!(pUartRegs->rUtrStat & 0x1)); //Receive data ready
chRx = pUartRegs->rUrxh;
return chRx;
}
#ifndef __NANDBL
int GetIntNum( void)
{
char str[30];
char *string = str;
int base = 10;
int minus = 0;
int result = 0;
int lastIndex;
int i,j;
gets(string);
i=0; j=0;
do {
if (string[j]==0x8) {
if (i>0) i--;
} else
string[i++]=string[j];
} while(string[j++]!=0);
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)) {
if(isupper(string))
result = (result<<4) + string - 'A' + 10;
else
result = (result<<4) + string - 'a' + 10;
}
else
result = (result<<4) + string - '0';
}
result = minus ? (-1*result):result;
}
return result;
}
void Uart_TxEmpty(void) // added 060624
{
volatile UART_REGS *pUartRegs;
pUartRegs = (UART_REGS *)(UART_REG_BASE+UART_REG_OFFSET*DebugUartCh);
while(!(pUartRegs->rUtrStat & 0x4)); //Wait until tx shifter is empty.
}
char Uart_GetKey(void) // added 060624
{
volatile UART_REGS *pUartRegs;
pUartRegs = (UART_REGS *)(UART_REG_BASE+UART_REG_OFFSET*DebugUartCh);
if(pUartRegs->rUtrStat & 0x1) //Receive data ready
return pUartRegs->rUrxh;
else
return 0;
}
// UART data download function, moved 060718
int UartDownAddress;
U32 UartDownProgramSize;
unsigned int DownloadData(void)
{
int i;
U16 checkSum=0,dnCS;
U32 fileSize=10;
U8 *downPt, key;
volatile UART_REGS *pUartRegs;
pUartRegs = (UART_REGS *)(UART_REG_BASE+UART_REG_OFFSET*DebugUartCh);
printf(" Type the download address [D:0x31000000] : ");
UartDownAddress = GetIntNum();
if(UartDownAddress == (-1))
UartDownAddress = 0x31000000;
printf("Do you want to download through Debug UART port from 0x%x? [Y/n] : ",UartDownAddress);
key=getchar();
printf("%c\n",key);
if((key=='n')||(key=='N')) return 0;
downPt=(U8 *)UartDownAddress;
printf("UartDownAddress = %x\n",UartDownAddress);
printf("Download the plain binary file(.BHC) to be written through UART1 channel\n");
printf("The file format : <n+6>(4)+(n)+CS(2)\n");
printf("To transmit .BIN file : wkocm2 xxx.BIN /1 /d:1\n");
printf("Download methods : COM:8Bit,NP,1STOP\n");
printf("\nSTATUS : ");
rINTMSK=BIT_ALLMSK;
//tmp=pUartRegs->rUrxh; //To remove overrun error state.
i=0;
while(i<fileSize)
{
while(!(pUartRegs->rUtrStat&0x1));
*(downPt+i)=pUartRegs->rUrxh;
if(i==3)
{
fileSize=*((U8 *)(UartDownAddress+0))+
(*((U8 *)(UartDownAddress+1))<<8)+
(*((U8 *)(UartDownAddress+2))<<16)+
(*((U8 *)(UartDownAddress+3))<<24);
}
if((i%1000)==0)
pUartRegs->rUtxh = '#';
i++;
}
UartDownProgramSize=fileSize-6;
for(i=4;i<(fileSize-2);i++)
{
checkSum+=*((U8 *)(i+UartDownAddress));
}
dnCS=*((U8 *)(UartDownAddress+fileSize-2))+
(*( (U8 *)(UartDownAddress+fileSize-1) )<<8);
if(checkSum!=dnCS)
{
printf("Checksum Error!!! MEM : %x DN : %x\n",checkSum,dnCS);
return 0;
}
printf("\nDownload O.K.\n");
return 1;
}
unsigned int AUTO_DownloadData(int downaddr)
{
int i;
U16 checkSum=0,dnCS;
U32 fileSize=10;
U8 *downPt, key;
volatile UART_REGS *pUartRegs;
pUartRegs = (UART_REGS *)(UART_REG_BASE+UART_REG_OFFSET*DebugUartCh);
printf(" Type the download address [D:0x31000000] : 0x%x",downaddr);
//UartDownAddress = GetIntNum();
UartDownAddress = downaddr;
if(UartDownAddress == (-1))
UartDownAddress = 0x31000000;
//printf("Do you want to download through Debug UART port from 0x%x? [Y/n] : ",UartDownAddress);
//key=getchar();
//printf("%c\n",key);
//if((key=='n')||(key=='N')) return 0;
downPt=(U8 *)UartDownAddress;
printf("UartDownAddress = %x\n",UartDownAddress);
printf("Download the plain binary file(.BHC) to be written through UART1 channel\n");
printf("The file format : <n+6>(4)+(n)+CS(2)\n");
printf("To transmit .BIN file : wkocm2 xxx.BIN /1 /d:1\n");
printf("Download methods : COM:8Bit,NP,1STOP\n");
printf("\nSTATUS : ");
rINTMSK=BIT_ALLMSK;
//tmp=pUartRegs->rUrxh; //To remove overrun error state.
i=0;
while(i<fileSize)
{
while(!(pUartRegs->rUtrStat&0x1));
*(downPt+i)=pUartRegs->rUrxh;
if(i==3)
{
fileSize=*((U8 *)(UartDownAddress+0))+
(*((U8 *)(UartDownAddress+1))<<8)+
(*((U8 *)(UartDownAddress+2))<<16)+
(*((U8 *)(UartDownAddress+3))<<24);
}
if((i%1000)==0)
pUartRegs->rUtxh = '#';
i++;
}
UartDownProgramSize=fileSize-6;
for(i=4;i<(fileSize-2);i++)
{
checkSum+=*((U8 *)(i+UartDownAddress));
}
dnCS=*((U8 *)(UartDownAddress+fileSize-2))+
(*( (U8 *)(UartDownAddress+fileSize-1) )<<8);
if(checkSum!=dnCS)
{
printf("Checksum Error!!! MEM : %x DN : %x\n",checkSum,dnCS);
return 0;
}
printf("\nDownload O.K.\n");
return 1;
}
//===========================[ BOARD LED ]================================
void Init_LED(void)
{
// LED1,2,4,8 -> GPC 0,5,6,7 in SMDK2450
rGPCCON = (rGPCCON&~(0x3<<0))|(0x1<<0);
rGPCCON = (rGPCCON&~(0x3f<<10))|(0x15<<10);
rGPCUDP &= ~((0x3f<<10)|(0x3<<0)); // pull-up/down disable
}
void Led_Display(int data)
{
rGPCDAT = (rGPCDAT & ~((0x7<<5)|(0x0<<0))) | (((data>>1) & 0x7)<<5) |((data & 0x1)<<0);
}
#endif
//===========================[ Initializing Console ]================================
void Console(unsigned int ch) // edited by junon 060530
{
SelectDebugUart(ch); // ch0 ~ ch3
InitDebugUart(115200);
#ifndef __NANDBL
SetResTDelay(1000); // Resolution of TDelay is 1000us=1ms
InitTDelayFunc();
Init_LED();
#endif
} |
|