www.pudn.com > new_terminal.rar > test_uart.c
/********************************************* *测试程序的显示终端,提供基本的 *显示函数。 *建立日期:2006-2-14 *文件名澹:Test_uart.c * * * * * ******************************************/ #include#include #include #include "2410addr.h" #include "2410lib.h" #include "Option.h" #include "Def.h" #include #include static int whichUart=0; static int delayLoopCount = FCLK/10000/10; char str[256]; //========================*[ Timer ]==============================** void Timer_Start(int divider) //0:16us,1:32us 2:64us 3:128us { rWTCON = ((PCLK/1000000-1)<<8)|(divider<<3); //Watch-dog timer control register rWTDAT = 0xffff; //Watch-dog timer data register rWTCNT = 0xffff; //Watch-dog count register //Watch-dog timer enable & interrupt disable // rWTCON = rWTCON |(1<<5) & !(1<<2); //? rWTCON = rWTCON | (1<<5) | ~(1<<2); //May 06, 2002 SOP } //================================================================= int Timer_Stop(void) { rWTCON = ((PCLK/1000000-1)<<8); return (0xffff - rWTCNT); } 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 64us, 200*400 cycle runtime = 64*i us delayLoopCount = 8000000/(i*64); //200*400:64*i=1*x:100 -> x=80000*100/(64*i) } } void Uart_Init(int pclk,int baud){ int i; if(pclk == 0) pclk = PCLK; rUFCON0 = 0x0; //UART channel 0 FIFO control register, FIFO disable rUFCON1 = 0x0; //UART channel 1 FIFO control register, FIFO disable rUFCON2 = 0x0; //UART channel 2 FIFO control register, FIFO disable rUMCON0 = 0x0; //UART chaneel 0 MODEM control register, AFC disable rUMCON1 = 0x0; //UART chaneel 1 MODEM control register, AFC disable //UART0 rULCON0 = 0x3; rUCON0 = 0x245; // Control register rUBRDIV0=( (int)(pclk/16./baud) -1 ); //Baud rate divisior register 0 // rUBRDIV0=( (int)(pclk/16./baud+0.5) -1 ); //Baud rate divisior register 0 //UART1 rULCON1 = 0x3; rUCON1 = 0x245; rUBRDIV1=( (int)(pclk/16./baud) -1 ); //UART2 rULCON2 = 0x3; rUCON2 = 0x245; rUBRDIV2=( (int)(pclk/16./baud) -1 ); for(i=0;i<100;i++); } void Uart_Select(int ch) { whichUart = ch; } char Uart_Getch(void) { if(whichUart==0) { while(!(rUTRSTAT0 & 0x1)); //Receive data ready return RdURXH0(); } else if(whichUart==1) { while(!(rUTRSTAT1 & 0x1)); //Receive data ready return RdURXH1(); } else if(whichUart==2) { while(!(rUTRSTAT2 & 0x1)); //Receive data ready return RdURXH2(); } } void Uart_Putch(char c) { if(whichUart==0) { while(!(rUTRSTAT0 & 0x2)); //Receive data ready WrUTXH0(c); } else if(whichUart==1) { while(!(rUTRSTAT1 & 0x2)); //Receive data ready WrUTXH0(c); } else if(whichUart==2) { while(!(rUTRSTAT2 & 0x2)); //Receive data ready WrUTXH0(c); } } void Uart_SendByte(int data) { if(whichUart==0) { if(data=='\n') { while(!(rUTRSTAT0 & 0x2)); //Delay(0); //because the slow response of hyper_terminal WrUTXH0('\r'); } while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty. //Delay(10); WrUTXH0(data); } else if(whichUart==1) { if(data=='\n') { while(!(rUTRSTAT1 & 0x2)); //Delay(0); //because the slow response of hyper_terminal rUTXH1 = '\r'; } while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty. //Delay(0); rUTXH1 = data; } else if(whichUart==2) { if(data=='\n') { while(!(rUTRSTAT2 & 0x2)); //Delay(0); //because the slow response of hyper_terminal rUTXH2 = '\r'; } while(!(rUTRSTAT2 & 0x2)); //Wait until THR is empty. //Delay(0); rUTXH2 = data; } } void Uart_SendString(char *pt) { while(*pt) Uart_SendByte(*pt++); } void Uart_Printf(char *fmt,...) { va_list ap; char string[256]; va_start(ap,fmt); vsprintf(string,fmt,ap); Uart_SendString(string); va_end(ap); } void Uart_Getchmd(char *cmd_buf, unsigned int len) { char curpos = 0; /* current position - index into cmd_buf */ char c; int cmd_echo = 1; /* Clear out the buffer */ memset(cmd_buf, 0, MAX_CMDBUF_SIZE); for (;;) { c = Uart_Getch(); switch (c) { case 0x08: case 0x06: case 0x07: case 0x7E: case 0x7F: /* backspace or delete */ /* we're not at the beginning of the line */ if (curpos) { curpos--; Uart_Putch(0x08); /* go backwards */ Uart_Putch(' '); /* overwrite the char */ Uart_Putch(0x08); /* go back again */ } cmd_buf[curpos] = '\0'; break; case '\r': case '\n': case '\0': Uart_Putch('\r'); Uart_Putch('\n'); goto end_cmd; case CTL_CH('x'): curpos = 0; break; default: if (curpos < MAX_CMDBUF_SIZE) { cmd_buf[curpos] = c; /* echo it back out to the screen */ if (cmd_echo) Uart_Putch(c); curpos++; } break; } } end_cmd: return ; } #define MAX_PROMPT_LEN 256 char prompt[MAX_PROMPT_LEN] = "Please enter the test code :"; static void exeprogram(int i) { switch (i) { case 1: Uart_Printf("\n调用LCD的DMA测试程序!\n"); break; case 2: Uart_Printf("\n调用IIS的DMA测试程序!\n"); break; case 3: Uart_Printf("\n调用cs8900的测试程序!\n"); break; default: goto handle_error; } return; handle_error: Uart_Printf("\n你输入的代码有误,查看代码命令menu\n"); } static void menu(void) { Uart_Printf("\n**************************************************\n"); Uart_Printf("**************************************************\n"); Uart_Printf("** **\n"); Uart_Printf("** 1.LCD测试 2.IIS 测试 3.cs8900测试 **\n"); Uart_Printf("** **\n"); Uart_Printf("** **\n"); Uart_Printf("**************************************************\n"); } void show(char * address_buf) { int Base_address,offset,temp,i; Base_address=(int)strtol(address_buf,(char **)NULL,16); Uart_Printf("The Base address you type here is 0x%08x \n",Base_address); Uart_Printf("===============================================================\n"); for(offset=0;offset<144;offset++) { temp=(*(volatile unsigned char *)(Base_address+offset)); Uart_Printf("%02X ",temp); if((offset+1)%16==0) { for(i=offset-15;i 47 && temp<122) Uart_Printf("%c",temp); else Uart_Printf("_"); } Uart_Printf("\n"); } } Uart_Printf("\n"); } static void execmd(char * buf) { if(!strcmp(buf,"menu")) menu(); else if (!strcmp(buf,"clear")) clear(); else if (!strncmp(buf,"show",4)) show(buf+5); //else Uart_Printf(" ",buf); } void clear(void) { int i; char cmd_buf[MAX_CMDBUF_SIZE]; for(i=0;i<25;i++) { Uart_Putch('\r'); //换行 Uart_Putch('\n'); //回车 } for(i=0;i<25;i++) { Uart_Putch(0x08); /* go backwards */ Uart_Putch(' '); /* overwrite the char */ Uart_Putch(0x08); /* go back again */// } Uart_Getchmd(cmd_buf, MAX_CMDBUF_SIZE); i=(int)strtod(cmd_buf,(char **)NULL); if (i)//如果是数字就进入主程序中去,否则看是否是命令 exeprogram(i); else if(cmd_buf[0]) execmd(cmd_buf); } static void serial_term(void) { char cmd_buf[MAX_CMDBUF_SIZE]; int i; for (;;) { Uart_Printf("%s", prompt); Uart_Getchmd(cmd_buf, MAX_CMDBUF_SIZE); i=(int)strtod(cmd_buf,(char **)NULL); if (i)//如果是数字就进入主程序中去,否则看是否是命令 exeprogram(i); else if(cmd_buf[0]) execmd(cmd_buf); } } int main (void) { Uart_Init(0,115200);//pclk 和波特率 Uart_Select(0);//串口选择 Delay(500); serial_term(); return 0; }