📄 consol.c
字号:
/*++
Module name:
CONSOL.c
Abstract:
This is the CONSOL Driver module. Supports multiple RS232 console interfaces.
Author:
Michael Anburaj
URL : http:\\embedded.n3.net Email : embeddedeng@hotmail.com
Environment:
Processor : EP73XX (32 bit ARM720T RISC core from CIRRUS)
IDE : SDT 2.51 & ADS 1.0
--*/
/* ********************************************************************* */
#include "def.h"
#include "EP7312.h"
#include "CONSOL.h"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
/* ********************************************************************* */
/* Global definitions */
/* ********************************************************************* */
/* File local definitions */
static int CONSOL_wChannel=1;
/* ********************************************************************* */
/* Local functions */
/* ********************************************************************* */
/* Global functions */
void CONSOL_Init(int wBaud,U8 bFifoFlag)
{
if(CONSOL_wChannel == 1)
{
rUBRLCR1 = ((0x03<<17)|(bFifoFlag<<16)|((115200*2)/wBaud-1));
rSYSCON1 |= (0x01<<8);
}
else
{
rUBRLCR2 = ((0x03<<17)|(bFifoFlag<<16)|((115200*2)/wBaud-1));
rSYSCON2 |= (0x01<<8);
}
}
void CONSOL_Select(int wCh)
{
CONSOL_wChannel = wCh;
}
char CONSOL_GetCh(void)
{
if(CONSOL_wChannel == 1)
{
while(rSYSFLG1 & (0x01<<22));
return rUARTDR1;
}
else
{
while(rSYSFLG2 & (0x01<<22));
return rUARTDR2;
}
}
char CONSOL_GetChar(char * pdata)
{
if(CONSOL_wChannel==1)
{
if((rSYSFLG1 & (0x01<<22)) == 0x00)
{
*pdata = rUARTDR1;
return True;
}
return False;
}
else
{
if((rSYSFLG2 & (0x01<<22)) == 0x00)
{
*pdata = rUARTDR2;
return True;
}
return False;
}
}
void CONSOL_GetString(char *string)
{
char *string2=string;
char c;
while((c=CONSOL_GetCh())!='\r')
{
if(c=='\b')
{
if((int)string2<(int)string){CONSOL_Printf("\b \b");string--;}
}
else {*string++=c;CONSOL_SendCh(c);}
}
*string='\0';
CONSOL_SendCh('\n');
}
#define __isalpha(c) (c >'9')
#define __isupper(c) !(c & 0x20)
int CONSOL_GetIntNum(void)
{
char str[30];
char *string=str;
int base=10;
int minus=0;
int lastIndex;
int result=0;
int i;
CONSOL_GetString(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( 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;
}
void CONSOL_SendCh(char data)
{
if(CONSOL_wChannel == 1)
{
if(data=='\n')
{
while(rSYSFLG1 & (0x01<<23));
// Delay(4); //because the slow response of hyper_terminal
rUARTDR1 = '\r';
}
while(rSYSFLG1 & (0x01<<23)); //Wait until THR is empty.
// Delay(4);
rUARTDR1 = data;
}
else
{
if(data=='\n')
{
while(rSYSFLG2 & (0x01<<23));
// Delay(4); //because the slow response of hyper_terminal
rUARTDR2 = '\r';
}
while(rSYSFLG2 & (0x01<<23)); //Wait until THR is empty.
// Delay(4);
rUARTDR2 = data;
}
}
void CONSOL_SendChar(char data)
{
if(CONSOL_wChannel == 1)
{
while(rSYSFLG1 & (0x01<<23)); //Wait until THR is empty.
// Delay(4);
rUARTDR1 = data;
}
else
{
while(rSYSFLG2 & (0x01<<23)); //Wait until THR is empty.
// Delay(4);
rUARTDR2 = data;
}
}
void CONSOL_SendString(char *pt)
{
while(*pt)CONSOL_SendCh(*pt++);
}
void CONSOL_Scanf(char *pcText,...)
{
va_list pArg;
char cChar;
int *pwInt;
char *pbChar;
va_start(pArg, pcText);
while((cChar=*pcText++) != '\0')
{
if(cChar != '%')continue;
switch(*pcText)
{
case 's':
case 'S':
pbChar = va_arg (pArg, char *);
CONSOL_GetString(pbChar);
break;
case 'i':
case 'I':
pwInt = va_arg (pArg, int *);
*pwInt = CONSOL_GetIntNum();
break;
case 'c':
case 'C':
pbChar = va_arg (pArg, char *);
*pbChar = CONSOL_GetCh();
break;
}
}
va_end(pArg);
}
void CONSOL_Printf(char *fmt,...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
CONSOL_SendString(string);
va_end(ap);
}
/* ********************************************************************* */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -