-
Notifications
You must be signed in to change notification settings - Fork 5
/
uart.c
86 lines (77 loc) · 1.21 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
volatile unsigned char * const UART0_BASE = (unsigned char *)0x1c090000;
void uart_send(unsigned int c)
{
*UART0_BASE = c;
if(c == '\n')
*UART0_BASE = '\r';
}
void printstr(char *s)
{
int i = 0;
while(s[i])
{
uart_send(s[i]);
i++;
}
}
void printint(int xx, int base, int sign)
{
static char digits[] = "0123456789abcdef";
char buf[16];
int i;
unsigned int x;
if(sign && (sign = xx < 0))
x = -xx;
else
x = xx;
i = 0;
do{
buf[i++] = digits[x % base];
}while((x /= base) != 0);
if(sign)
buf[i++] = '-';
while(--i >= 0)
uart_send(buf[i]);
}
void cprintf(char *fmt, ...)
{
int i, c;
unsigned int *argp;
char *s;
argp = (unsigned int*)(void*)(&fmt + 1);
for(i = 0; (c = fmt[i] & 0xff) != 0; i++)
{
if(c != '%')
{
uart_send(c);
continue;
}
c = fmt[++i] & 0xff;
if(c == 0)
break;
switch(c)
{
case 'd':
printint(*argp++, 10, 1);
break;
case 'x':
case 'p':
printint(*argp++, 16, 0);
break;
case 's':
if((s = (char*)*argp++) == 0)
s = "(null)";
for(; *s; s++)
uart_send(*s);
break;
case '%':
uart_send('%');
break;
default:
// Print unknown % sequence to draw attention.
uart_send('%');
uart_send(c);
break;
}
}
}