-
Notifications
You must be signed in to change notification settings - Fork 2
/
MSPIO.c
144 lines (127 loc) · 2.96 KB
/
MSPIO.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
#include "MSPIO.h"
void PrintChar(uint32_t UART, char c)
{
/*Send the char through the selected UART*/
UART_Write(UART, (uint8_t*)&c, 1);
}
void PrintString(uint32_t UART, char *string)
{
/*while the string is not the null character*/
while(*string)
{
PrintChar(UART, *string);
string++;
}
}
/*Thanks! https://github.com/grbl/grbl/blob/master/grbl/print.c */
void PrintInteger(uint32_t UART, int integer)
{
if(integer == 0)
{
PrintChar(UART, '0');
}
if(integer < 0)
{
PrintChar(UART, '-');
}
char b[10];
int digit = integer;
uint8_t i = 0;
while(digit)
{
b[i++] = digit % 10;
digit /= 10;
}
while(i)
{
PrintChar(UART, '0' + b[i-1]);
i--;
}
}
/*A basic printf for the MSP432. In order to use it properly you need to initialize the correct UART peripheral.
* The following formats are supported:
* %c = for char variables
* %s = for string variables
* %i = for unsigned integers
* USAGE...
*
* MSPrintf(EUSCI_A0_BASE, "Formated string %c, %s, %i", character, string, integer)*/
void MSPrintf(uint32_t UART, const char *fs, ...)
{
va_list valist;
va_start(valist, fs);
int i;
char *s;
while(*fs)
{
if(*fs != '%')
{
UART_Write(UART, (uint8_t*)fs, 1);
fs++;
}
else
{
switch(*++fs)
{
case 'c':
i = va_arg(valist, int);
PrintChar(UART, (char)i);
break;
case 's':
s = va_arg(valist, char*);
PrintString(UART, s);
break;
case 'i':
i = va_arg(valist, int);
PrintInteger(UART, i);
break;
}
++fs;
}
}
}
/*A basic gets for the MSP432. In order to use it properly you need to initialize the correct UART peripheral.
* USAGE...
*
* MSPgets(EUSCI_A0_BASE, InputBuffer, SizeOfInputBuffer)*/
int MSPgets(uint32_t UART, char *b, int size)
{
char c;
uint32_t i = 0;
while(1)
{
if(UART_Read(UART, (uint8_t*)&c, 1) != 0)
{
/*put a '\n' and '\r' if it fits on the buffer*/
if(c == '\n' || c == '\r')
{
if(i + 3 > size)
{
return size + 1;
}
b[i++] = '\r';
b[i++] = '\n';
b[i++] = 0;
return i;
}
/*erase data from buffer if backspace is received*/
else if(c == 127 || c == 8)
{
i--;
b[i] = 0;
}
/*store character on the buffer*/
else
{
if(i < size)
{
b[i++] = c;
}
else
{
return size + 1;
}
}
}
}
}