-
Notifications
You must be signed in to change notification settings - Fork 15
/
uart.c
254 lines (212 loc) · 4.54 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <signal.h>
#include <poll.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
/* Should we exit simulation on ctrl-c or pass it through? */
#define EXIT_ON_CTRL_C
#define CLOCK 50000000L
#define BAUD 115200
/* Round to nearest */
#define BITWIDTH ((CLOCK+(BAUD/2))/BAUD)
/*
* Our UART uses 16x oversampling, so at 50 MHz and 115200 baud
* each sample is: 50000000/(115200*16) = 27 clock cycles. This
* means each bit is off by 0.47% so for 8 bits plus a start and
* stop bit the errors add to be 4.7%.
*/
static double error = 0.05;
enum state {
IDLE, START_BIT, BITS, STOP_BIT, ERROR
};
static enum state tx_state = IDLE;
static unsigned long tx_countbits;
static unsigned char tx_bits;
static unsigned char tx_byte;
static unsigned char tx_prev;
/*
* Return an error if the transition is not close enough to the start or
* the end of an expected bit.
*/
static bool is_error(unsigned long bits)
{
double e = 1.0 * tx_countbits / BITWIDTH;
if ((e <= (1.0-error)) && (e >= error))
return true;
return false;
}
void uart_tx(unsigned char tx)
{
switch (tx_state) {
case IDLE:
if (tx == 0) {
tx_state = START_BIT;
tx_countbits = BITWIDTH;
tx_bits = 0;
tx_byte = 0;
}
break;
case START_BIT:
tx_countbits--;
if (tx == 1) {
if (is_error(tx_countbits)) {
printf("START_BIT error %ld %ld\n", BITWIDTH, tx_countbits);
tx_countbits = BITWIDTH*2;
tx_state = ERROR;
break;
}
}
if (tx_countbits == 0) {
tx_state = BITS;
tx_countbits = BITWIDTH;
}
break;
case BITS:
tx_countbits--;
if (tx_countbits == BITWIDTH/2) {
tx_byte = tx_byte | (tx << tx_bits);
tx_bits = tx_bits + 1;
}
if (tx != tx_prev) {
if (is_error(tx_countbits)) {
printf("BITS error %ld %ld\n", BITWIDTH, tx_countbits);
tx_countbits = BITWIDTH*2;
tx_state = ERROR;
break;
}
}
if (tx_countbits == 0) {
if (tx_bits == 8) {
tx_state = STOP_BIT;
}
tx_countbits = BITWIDTH;
}
break;
case STOP_BIT:
tx_countbits--;
if (tx == 0) {
if (is_error(tx_countbits)) {
printf("STOP_BIT error %ld %ld\n", BITWIDTH, tx_countbits);
tx_countbits = BITWIDTH*2;
tx_state = ERROR;
break;
}
/* Go straight to idle */
write(STDOUT_FILENO, &tx_byte, 1);
tx_state = IDLE;
}
if (tx_countbits == 0) {
write(STDOUT_FILENO, &tx_byte, 1);
tx_state = IDLE;
}
break;
case ERROR:
tx_countbits--;
if (tx_countbits == 0) {
tx_state = IDLE;
}
break;
}
tx_prev = tx;
}
static struct termios oldt;
static void disable_raw_mode(void)
{
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}
static void enable_raw_mode(void)
{
static bool initialized = false;
if (!initialized) {
static struct termios newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
cfmakeraw(&newt);
#ifdef EXIT_ON_CTRL_C
newt.c_lflag |= ISIG;
#endif
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
initialized = true;
atexit(disable_raw_mode);
}
}
static int nonblocking_read(unsigned char *c)
{
int ret;
unsigned long val = 0;
struct pollfd fdset[1];
enable_raw_mode();
memset(fdset, 0, sizeof(fdset));
fdset[0].fd = STDIN_FILENO;
fdset[0].events = POLLIN;
ret = poll(fdset, 1, 0);
if (ret == 0)
return false;
ret = read(STDIN_FILENO, &val, 1);
if (ret != 1) {
fprintf(stderr, "%s: read of stdin returns %d\n", __func__, ret);
exit(1);
}
if (ret == 1) {
*c = val;
return true;
} else {
return false;
}
}
static enum state rx_state = IDLE;
static unsigned char rx_char;
static unsigned long rx_countbits;
static unsigned char rx_bit;
static unsigned char rx = 1;
/* Avoid calling poll() too much */
#define RX_INTERVAL 10000
static unsigned long rx_sometimes;
unsigned char uart_rx(void)
{
unsigned char c;
switch (rx_state) {
case IDLE:
if (rx_sometimes++ >= RX_INTERVAL) {
rx_sometimes = 0;
if (nonblocking_read(&c)) {
rx_state = START_BIT;
rx_char = c;
rx_countbits = BITWIDTH;
rx_bit = 0;
rx = 0;
}
}
break;
case START_BIT:
rx_countbits--;
if (rx_countbits == 0) {
rx_state = BITS;
rx_countbits = BITWIDTH;
rx = rx_char & 1;
}
break;
case BITS:
rx_countbits--;
if (rx_countbits == 0) {
rx_bit = rx_bit + 1;
if (rx_bit == 8) {
rx = 1;
rx_state = STOP_BIT;
} else {
rx = (rx_char >> rx_bit) & 1;
}
rx_countbits = BITWIDTH;
}
break;
case STOP_BIT:
rx_countbits--;
if (rx_countbits == 0) {
rx_state = IDLE;
}
break;
}
return rx;
}