-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
140 lines (114 loc) · 1.76 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
#include <uart.h>
#include <stdarg.h>
void uart0_init(void)
{
u32 val;
/*set GPA0_0, GPA0_1 as UART_0_TXD and UART_0_RXD*/
val = ioread32(GPA0CON);
val &= ~0xff;
val |= 0x22;
iowrite32(val, GPA0CON);
/*UART*/
/*8N1*/
iowrite32(0x3, ULCON0);
/*set R/T mode as polling*/
iowrite32(0x5, UCON0);
/*disable FIFO, select NOFIFO MODE*/
iowrite32(0x0, UFCON0);
/*disable AFC*/
iowrite32(0x0, UMCON0);
/*set Baudrate 115200*/
iowrite32(53, UBRDIV0);
iowrite32(4, UFRACVAL0);
}
static void wait_transmit_buffer_empty(void)
{
int val;
do {
val = ioread32(UTRSTAT0);
val >>= 1;
val &= 1;
} while (!val);
}
void uputchar(int c)
{
wait_transmit_buffer_empty();
iowrite8(c, UTXH0);
if (c == '\n') {
uputchar('\r');
}
}
static void wait_receive_buffer_valid(void)
{
int val;
do {
val = ioread32(UTRSTAT0);
val &= 1;
} while (!val);
}
int ugetchar(void)
{
int c;
wait_receive_buffer_valid();
c = ioread8(URXH0);
if (c == '\r') {
c = '\n';
}
uputchar(c);
return c;
}
char *ugets(char *b)
{
int c;
char *tmp = b;
while ((c = ugetchar()) != '\n') {
*b++ = c;
}
*b = '\0';
return tmp;
}
void uputs(const char *s)
{
while (*s) {
uputchar(*s);
s++;
}
}
void uprintf(const char *fmt, ...)
{
int val;
va_list ap;
char buf[128];
va_start(ap, fmt);
while (*fmt) {
if (*fmt != '%') {
uputchar(*fmt);
} else {
fmt++;
switch (*fmt) {
case 'd':
val = va_arg(ap, int);
if (val < 0) {
uputchar('-');
val = 0 - val;
}
// itoa(val, buf);
uputs(buf);
break;
case 'x':
val = va_arg(ap, int);
// xtoa(val, buf);
uputs(buf);
break;
case 's':
uputs(va_arg(ap, char *));
break;
case 'c':
uputchar(va_arg(ap, int));
break;
}
}
fmt++;
}
va_end(ap);
}