📄 serial.c
字号:
/********************************************************************
*File :serial.c
*Autor:GongJun
*Date :
*Modifier:cgf
*Date:2005-8-5
*Description:some library function about target
*********************************************************************/
#include "config.h"
#include "board.h"
/******************************************************************
** serial function
*******************************************************************/
static unsigned short SerialPortSel;
unsigned short SerialSwitch( unsigned short port )
{
#ifdef SERIAL_PORTS_SWITCH
SerialPortSel = port ? 1 : 0;
#else
SerialPortSel = 0;
#endif
return SerialPortSel;
}
void SerialChgBaud( unsigned int mclk , unsigned int baud )
{
unsigned int Fdiv;
// unsigned int mclk = GetMasterClock();
//UART0 control***************************************************************
U0LCR = 0x80;
Fdiv = ( Fpclk / 16 ) / baud ; /*baud rate */
U0DLM = Fdiv / 256;
U0DLL = Fdiv % 256;
U0LCR = 0x03;
/*8,1,n*/
U0IER = 0x00; /* disable receive and send interrrupt */
U0FCR = 0x00; /* init FIFO */
//UART1 contorl ****************************************************************
U1LCR = 0x80;
U1DLM = Fdiv / 256;
U1DLL = Fdiv % 256;
U1LCR = 0x03;
/* 8,1,n */
U1IER = 0x00;
U1FCR = 0x00; /* initFIFO */
}
void SerialTxEmpty( void )
{
if ( SerialPortSel )
while ( !( U1LSR & 0x20 ) ); //sending data not end,wait
else
while ( !( U0LSR & 0x20 ) ); //sending data not end,wait
}
void SerialTxChar( char data )
{
//U16 m ;
if ( SerialPortSel )
{
if ( data == '\n' )
{
while ( !( U1LSR & 0x20 ) ); //sending data not end,wait
//for( m = 0; m < 100; ) m++ ;
U1THR = '\r' ;
}
while ( !( U1LSR & 0x20 ) ); //sending data not end,wait
//for( m = 0; m < 100; ) m++ ;
U1THR = data; // send data
}
else
{
if ( data == '\n' )
{
while ( !( U0LSR & 0x20 ) ); //sending data not end,wait
//for( m = 0; m < 100; ) m++ ;
U0THR = '\r' ;
}
while ( !( U0LSR & 0x20 ) ); //sending data not end,wait
//for( m = 0; m < 100; ) m++ ;
U0THR = data; //send data
}
}
void SerialTxString( char* s )
{
while ( *s )
SerialTxChar( *s++ );
}
int SerialRxReady( void )
{
if ( SerialPortSel )
return ( U1LSR & 0x01 ); //Receive data ready
else
return ( U0LSR & 0x01 ); //Receive data ready
}
char SerialRxKey( void )
{
if ( SerialPortSel )
{
if ( U1LSR & 0x01 ) //Receive data ready
return U1RBR ;
}
else
{
if ( U0LSR & 0x01 ) //Receive data ready
return U0RBR ;
}
return 0;
}
char SerialRxChar( void )
{
if ( SerialPortSel )
{
while ( ( U1LSR & 0x01 ) == 0 ); //don't receive data
return U1RBR ;
}
else
{
while ( ( U0LSR & 0x01 ) == 0 ); //don't receive data
return U0RBR ;
}
return 0 ;
}
int SerialRxToBuf( char* b )
{
if ( SerialPortSel )
{
if ( U1LSR & 0x01 ) //Receive data ready
*b = U1RBR ;
else
return 0;
}
else
{
if ( U0LSR & 0x01 ) //Receive data ready
*b = U0RBR ;
else
return 0;
}
return 1;
}
int strlen( const char* s )
{
int i = 0;
for ( ; *s; s++ )
i++;
return i;
}
int strncmp( const char* s1 , const char* s2 , int maxlen )
{
int i;
for ( i = 0; i < maxlen; i++ )
{
if ( s1[i] != s2[i] )
return ( ( int ) s1[i] ) - ( ( int ) s2[i] );
if ( s1[i] == 0 )
return 0;
}
return 0;
}
/*---------------------printf and support routines ---------------------*/
/* print c count times */
void PutRepChar( char c , int count )
{
while ( count-- )
putch( c );
}
/* put string reverse */
void PutStringReverse( char* s , int index )
{
while ( ( index-- ) > 0 )
putch( s[index] );
}
/*-------------------------------------------------------------------------*/
/*
prints value in radix, in a field width width, with fill
character fill
if radix is negative, print as signed quantity
if width is negative, left justify
if width is 0, use whatever is needed
if fill is 0, use ' '
*/
static void PutNumber( int value , int radix , int width , char fill )
{
char buffer[40];
int bi = 0;
int unsigned uvalue;
short int digit;
short int left = FALSE;
short int negative = FALSE;
if ( fill == 0 )
fill = ' ';
if ( width < 0 )
{
width = -width;
left = TRUE;
}
if ( width <0 || width> 80 )
width = 0;
if ( radix < 0 )
{
radix = -radix;
if ( value < 0 )
{
negative = TRUE;
value = -value;
}
}
uvalue = value;
do
{
if ( radix != 16 )
{
digit = uvalue % radix ;
uvalue = uvalue / radix ;
}
else
{
digit = uvalue & 0xf;
uvalue = uvalue >> 4;
}
buffer[bi] = digit + ( ( digit <= 9 ) ? '0' : ( 'A' - 10 ) );
bi++;
if ( uvalue != 0 )
{
if ( ( radix == 10 ) &&
( ( bi == 3 ) || ( bi == 7 ) || ( bi == 11 ) | ( bi == 15 ) ) )
{
buffer[bi++] = ',';
}
}
}
while ( uvalue != 0 );
if ( negative )
{
buffer[bi] = '-';
bi += 1;
}
if ( width <= bi )
PutStringReverse( buffer , bi );
else
{
width -= bi;
if ( !left )
PutRepChar( fill , width );
PutStringReverse( buffer , bi );
if ( left )
PutRepChar( fill , width );
}
}
/*-------------------------------------------------------------------------*/
static char* FormatItem( char* f , int a )
{
char c;
int fieldwidth = 0;
int leftjust = FALSE;
int radix = 0;
char fill = ' ';
if ( *f == '0' )
fill = '0';
while ( ( c = *f++ ) != 0 )
{
if ( c >= '0' && c <= '9' )
{
fieldwidth = ( fieldwidth * 10 ) + ( c - '0' );
}
else
switch ( c )
{
case '\000':
return( --f );
case '%':
putch( '%' );
return( f );
case '-':
leftjust = TRUE;
break;
case 'c':
{
if ( leftjust )
putch( a & 0x7f );
if ( fieldwidth > 0 )
PutRepChar( fill , fieldwidth - 1 );
if ( !leftjust )
putch( a & 0x7f );
return( f );
}
case 's':
{
if ( leftjust )
puts( ( char * ) a );
if ( fieldwidth > strlen( ( char * ) a ) )
PutRepChar( fill ,
fieldwidth - strlen( ( char * ) a ) );
if ( !leftjust )
puts( ( char * ) a );
return( f );
}
case 'd':
case 'i':
radix = -10;break;
case 'u':
radix = 10;break;
case 'x':
radix = 16;break;
case 'X':
radix = 16;break;
case 'o':
radix = 8;break;
default :
radix = 3;break;/* unknown switch! */
}
if ( radix )
break;
}
if ( leftjust )
fieldwidth = -fieldwidth;
PutNumber( a , radix , fieldwidth , fill );
return( f );
}
#define vaStart(list, param) list = (char*)((int)¶m + sizeof(param))
#define vaArg(list, type) ((type *)(list += sizeof(type)))[-1]
#define vaEnd(list)
void printf( char* f , ... ) // variable arguments
{
// U32 mode ;
char* argP;
// disable IRQs and FIQs
// mode = uHALir_ReadMode() ;
// uHALir_WriteMode(mode | NoFIQ | NoIRQ) ;
vaStart( argP , f ); // point at the end of the format string
while ( *f )
{
// this works because args are all ints
if ( *f == '%' )
f = FormatItem( f + 1 , vaArg( argP , int ) );
else
putch( *f++ );
}
vaEnd( argP );
// restore the previous mode
// uHALir_WriteMode(mode) ;
}
int getyorn( void )
{
char c;
puts( " [y/n] " );
while ( 1 )
{
c = getch();
if ( ( c == 'y' ) || ( c == 'Y' ) || ( c == 'n' ) || ( c == 'N' ) )
break;
}
putch( c );
putch( '\n' );
return c & 1; //y&Y are odd, n&N are even
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -