📄 console.c
字号:
#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
}
void InitDebugUart(unsigned int baud) // edited by junon 060530
{
int ch;
volatile UART_REGS *pUartRegs;
// select UART function
rGPHCON &= ~((0xf<<12)|(0xf<<8)|(0xf<<4)|(0xf<<0));
rGPHCON |= ((0xa<<12)|(0xa<<8)|(0xa<<4)|(0xa<<0));
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
// [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
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
}
}
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[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;
}
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;
}
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;
}
//===========================[ BOARD LED ]================================
void Init_LED(void) // edited by junon 060522
{
// LED1,2,4,8 -> EINT4,5,6,7 = GPF4,5,6,7 in SMDK2443
rGPFCON = (rGPFCON&~(0xff<<8))|(0x55<<8); // output
#ifdef __EVT1
rEXTINT0 = ((rdEXTINT0&~(1<<31)) | (rdEXTINT0&~(1<<27)) | (rdEXTINT0&~(1<<23)) | (rdEXTINT0&~(1<<19)) );
rEXTINT0 = ((1<<31) | (1<<27) | (1<<23) | (1<<19));
#else
rGPFUDP &= ~(0xaa<<8); // pull-up/down disable
#endif
}
void Led_Display(int data)
{
rGPFDAT = (rGPFDAT & ~(0xf<<4)) | ((data & 0xf)<<4) ;
}
//===========================[ Initializing Console ]================================
void Console(void) // edited by junon 060530
{
SelectDebugUart(1); // ch0 ~ ch3
InitDebugUart(115200);
SetResTDelay(1000); // Resolution of TDelay is 1000us=1ms
InitTDelayFunc();
Init_LED();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -