-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf.c
117 lines (106 loc) · 2.02 KB
/
printf.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
/*
* printf library for the MSP432
*
* Largely taken from and inspired from:
* http://www.msp430launchpad.com/2012/06/using-printf.html
* http://www.43oh.com/forum/viewtopic.php?f=10&t=1732
*
* See http://www.samlewis.me for an example implementation.
*/
#include "stdarg.h"
#include <stdint.h>
#include "driverlib.h"
void sendByte(char c)
{
UART_transmitData(EUSCI_A0_BASE, c);
}
static const unsigned long dv[] = {
1000000000,// +0
100000000, // +1
10000000, // +2
1000000, // +3
100000, // +4
10000, // +5
1000, // +6
100, // +7
10, // +8
1, // +9
};
void puts(char *s) {
char c;
while (c = *s++) {
sendByte(c);
}
}
void putc(unsigned b) {
sendByte(b);
}
static void xtoa(unsigned long x, const unsigned long *dp) {
char c;
unsigned long d;
if (x) {
while (x < *dp)
++dp;
do {
d = *dp++;
c = '0';
while (x >= d)
++c, x -= d;
putc(c);
} while (!(d & 1));
} else
putc('0');
}
static void puth(unsigned n) {
static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
putc(hex[n & 15]);
}
void printf(char *format, ...)
{
char c;
int i;
long n;
va_list a;
va_start(a, format);
while(c = *format++) {
if(c == '%') {
switch(c = *format++) {
case 's': // String
puts(va_arg(a, char*));
break;
case 'c':// Char
putc(va_arg(a, char));
break;
case 'd':
case 'i':// 16 bit Integer
case 'u':// 16 bit Unsigned
i = va_arg(a, int);
if(((c == 'i')|(c=='d')) & (i < 0))
{
i = -i;
putc('-');
}
xtoa((unsigned)i, dv + 5);
break;
case 'l':// 32 bit Long
case 'n':// 32 bit uNsigned loNg
n = va_arg(a, long);
if(c == 'l' && n < 0) n = -n, putc('-');
xtoa((unsigned long)n, dv);
break;
case 'x':// 16 bit heXadecimal
i = va_arg(a, int);
puth(i >> 12);
puth(i >> 8);
puth(i >> 4);
puth(i);
break;
case 0: return;
default: goto bad_fmt;
}
} else
bad_fmt: putc(c);
}
va_end(a);
}