-
Notifications
You must be signed in to change notification settings - Fork 0
/
Uart.c
155 lines (135 loc) · 2.7 KB
/
Uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "2440addr.h"
#include "macro.h"
#include "option.h"
#include "device_driver.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
// Uart0 (Debug Port)
void Uart_Init(int baud)
{
// PORT GPIO initial
Macro_Write_Block(rGPHCON, 0xf, 0xa, 4);
rUFCON0 = 0x0;
rUMCON0 = 0x0;
rULCON0 = 0x3;
rUCON0 = 0x245;
rUBRDIV0= ((unsigned int)(PCLK/16./baud+0.5)-1 );
}
void Uart_Fifo_Init(void)
{
rUFCON0 = (0x3<<6)+(0x3<<4)+(1<<2)+(1<<1);
Macro_Set_Bit(rUFCON0, 0);
}
void Uart_Send_Byte(char data)
{
if(data == '\n')
{
while(Macro_Check_Bit_Clear(rUTRSTAT0,1));
rUTXH0 = '\r';
}
while(Macro_Check_Bit_Clear(rUTRSTAT0,1));
rUTXH0 = data;
}
void Uart_Send_String(char *pt)
{
while(*pt) Uart_Send_Byte(*pt++);
}
int Uart_Printf(char * fmt,...)
{
int n;
va_list ap;
char string[256];
va_start(ap,fmt);
n = vsprintf(string,fmt,ap);
Uart_Send_String(string);
va_end(ap);
return n;
}
char Uart_Get_Char(void)
{
while(!(rUTRSTAT0 & 0x1));
return RdURXH0();
}
char Uart_Get_Pressed(void)
{
if(rUTRSTAT0 & 0x1) return RdURXH0();
else return 0;
}
void Uart_GetString(char *string)
{
char *string2 = string;
char c;
while((c = Uart_Get_Char())!='\r')
{
if(c=='\b')
{
if( (int)string2 < (int)string )
{
Uart_Printf("\b \b");
string--;
}
}
else
{
*string++ = c;
Uart_Send_Byte(c);
}
}
*string='\0';
Uart_Send_Byte('\n');
}
int Uart_GetIntNum(void)
{
char str[30];
char *string = str;
int base = 10;
int minus = 0;
int result = 0;
int lastIndex;
int i;
Uart_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(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;
}