# Define utxho * (volatile int *) 0x7f005020)
# Define utrstat0 * (volatile int *) 0x7f005010)
# Define urxh0 * (volatile int *) 0x7f005024)
# Define gpacon * (volatile int *) 0x7f008000)
# Define ubrdiv0 * (volatile int *) 0x7f005028)
# Define udivsloto * (volatile int *) 0x7f00502c)
# Define ulcon0 * (volatile int *) 0x7f005000)
# Define ucon0 * (volatile int *) 0x7f005004)
Initialization
Void uart_init (void)
{
Gpacon = 0x22; // uart r/T
Ubrdiv0 = 34; // configure baud rate (integer)
Udivsloto = 0x1fff; // Divisor
Ulcon0 = 0x3; // UART Line Control
Ucon0 = 0xc05; // UART Control
}
Several basic functions of the stdio Library
Void uart_putchar (char C)
{
While (utrstat0 & (1 <2) = 0 );
Utxho = C;
}
Char uart_getchar (void)
{
While (utrstat0 & (1 <0) = 0 );
Return urxh0;
}
Void uart_putchar (char C)
{
While (utrstat0 & (1 <2) = 0 );
Utxho = C;
}
Char uart_getchar (void)
{
While (utrstat0 & (1 <0) = 0 );
Return urxh0;
}
Char * mygets (char * s)
{
Int I = 0;
While (* (S + I) = uart_getchar ())! = '\ R ')
{
If (* (S + I) = '\ B ')
{
If (I = 0)
Continue;
Uart_putchar ('\ B ');
Uart_putchar ('');
Uart_putchar ('\ B ');
I --;
}
Else
Uart_putchar (* (S + I ));
I ++;
}
Return S;
}
Convert a decimal hexadecimal string to an integer
Int hex_atoi (char * s)
{
Int num = 0, I = 0;
If (* s = 'O') & (* (S + 1) = 'X '))
{
S + = 2;
While (* s)
{
If (* S <'9') & (* s> '0 '))
{
Num * = 16;
Num + = * s;
S ++;
}
Else if (* s> 'A') & (* S <'Z '))
{
Num * = 16;
Num + = * s-'A' + 10;
S ++;
}
Else if (* s> 'A') & (* S <'Z '))
{
Num * = 16;
Num + = * s-'A' + 10;
S ++;
}
}
}
Else
{
While (* s ){
Num * = 10;
Num + = * s;
S ++;
}
}
Return num;
}
Char * myputs (char * s)
{
Int I = 0;
While (* (S + I )! = '\ 0 ')
{
If (* (S + I) = '\ B ')
{
I ++;
Continue;
}
Uart_putchar (* (S + I ));
I ++;
}
Return S;
}
Functions used to parse commands
Char ** analys (char * s)
{
Char * STR [5] = {null };
Int stat = 0, I = 0, j = 0;
While (* (S + I )! = '\ 0 ')
{
If (STAT = 0) & (* (S + I )! = Space ))
{
Stat = 1;
STR [J ++] = S + I ++;
}
Else {If (STAT = 1) & (* (S + I) = space ))
{
Stat = 0;
* (S + I ++) = '\ 0 ';
}
Else I ++;
}
}
Return STR;
}