-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUart.cpp
More file actions
345 lines (276 loc) · 7.94 KB
/
Uart.cpp
File metadata and controls
345 lines (276 loc) · 7.94 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// Uart.cpp
// Copyright © 2003 William Sheldon Simms III
//
#include <cstdlib>
#include "Uart.h"
// public member functions
Uart::Uart ()
{
rts = LOW;
cts = LOW;
tx_idx = 0;
tx_shift = 0;
tx_length = 0;
time_index = 0;
current_time = 0;
num_listeners = 0;
txing = false;
irq_enable = false;
tx.buf = NULL;
tx.enable = DISABLED;
tx.parity = PARITY_EVEN;
rx.buf = NULL;
rx.enable = DISABLED;
rx.parity = PARITY_EVEN;
par_error = false;
frm_error = false;
ovr_error = false;
}
Uart::~Uart ()
{
if (tx.newbuf) delete[] tx.buf;
if (rx.newbuf) delete[] rx.buf;
}
void Uart::AddUartListener (UartListener * nlistener)
{
if (num_listeners < MAX_NUM_LISTENERS)
{
listener[num_listeners++] = nlistener;
nlistener->TellBaud(tx.bits_per_ksec, rx.bits_per_ksec);
nlistener->TellParity(tx.parity, rx.parity);
nlistener->TellDataBits(tx.data_bits, rx.data_bits);
nlistener->TellStopBits(tx.twice_stop_bits, rx.twice_stop_bits);
nlistener->TellIrq(tx.irq_flag, rx.irq_flag);
nlistener->TellEnabled(tx.enable, rx.enable);
nlistener->TellIrqEnabled(irq_enable, tx.irq_enable, rx.irq_enable);
nlistener->TellTxShift(tx_shift);
nlistener->TellRxShift(0);
}
}
void Uart::UpdateAll ()
{
for (unsigned int idx = 0; idx < num_listeners; ++idx)
{
listener[idx]->TellBaud(tx.bits_per_ksec, rx.bits_per_ksec);
listener[idx]->TellParity(tx.parity, rx.parity);
listener[idx]->TellDataBits(tx.data_bits, rx.data_bits);
listener[idx]->TellStopBits(tx.twice_stop_bits, rx.twice_stop_bits);
listener[idx]->TellIrq(tx.irq_flag, rx.irq_flag);
listener[idx]->TellEnabled(tx.enable, rx.enable);
listener[idx]->TellIrqEnabled(irq_enable, tx.irq_enable, rx.irq_enable);
listener[idx]->TellTxShift(tx_shift);
listener[idx]->TellRxShift(0);
}
}
unsigned char Uart::read_rx_data (unsigned short dummy)
{
if (rx.bufidx == 0) return 0;
rx.bufidx = rx.bufidx - 1;
return rx.buf[rx.bufidx];
}
void Uart::write_tx_data (unsigned short dummy, unsigned char byte)
{
if (tx.enable == ENABLED)
{
tx.buf[tx.bufidx] = byte;
tx.bufidx = tx.bufidx + 1;
if (txing == false)
{
/* start shifting all high bits immediately */
txing = true;
tx_idx = 0;
tx_shift = ~0;
for (unsigned int idx = 0; idx < num_listeners; ++idx)
listener[idx]->TellTxShift(tx_shift);
tx_length = (2 * tx.data_bits) + tx.twice_stop_bits + 2;
if (tx.parity != PARITY_NONE)
tx_length += 2;
}
}
}
bool Uart::update (unsigned long ns)
{
/* need to be able to handle the case where at some point in the middle of 'ns'
nanoseconds, the CPU disabled transmitting */
current_time += ns;
while (txing == true && tx.bit_time != 0)
{
while (tx_idx < tx_length)
{
if ((current_time - time_index) >= tx.bit_time)
{
unsigned char tx_level = tx_shift & 1;
tx_shift >>= 1;
for (unsigned int idx = 0; idx < num_listeners; ++idx)
{
listener[idx]->TxBit(time_index, tx_level);
listener[idx]->TellTxShift(tx_shift);
}
time_index += tx.bit_time;
++tx_idx;
}
else
{
return true; /* need to be called again */
}
}
if (tx.bufidx > 0)
{
tx_idx = 0;
tx.bufidx = tx.bufidx - 1;
tx_shift = build_tx_shift(tx.buf[tx.bufidx]);
for (unsigned int idx = 0; idx < num_listeners; ++idx)
{
listener[idx]->TxByte(tx.buf[tx.bufidx]);
listener[idx]->TellTxShift(tx_shift);
}
}
else
{
txing = false;
if (tx.irq_enable == ENABLED)
{
tx.irq_flag = true;
for (unsigned int idx = 0; idx < num_listeners; ++idx)
listener[idx]->TellIrq(tx.irq_flag, rx.irq_flag);
}
}
}
time_index = current_time;
return false; /* don't need to be called again */
}
void Uart::TellTime (unsigned long long current_time)
{
static unsigned long long last_time = 0;
unsigned long time_diff = current_time - last_time;
last_time = current_time;
update(time_diff);
}
// protected member functions
int Uart::SetXcvrBuf (xcvr & x, unsigned int len, unsigned char * ptr)
{
if (len < 0) return -1;
x.buflen = 0;
x.bufidx = 0;
if (ptr)
{
x.buf = ptr;
x.newbuf = false;
}
else
{
x.buf = new unsigned char [len];
x.newbuf = true;
}
if (x.buf == NULL) return -2;
x.buflen = len;
return 0;
}
void Uart::SetXcvrIrqFlag (xcvr & x, bool irq_flag)
{
x.irq_flag = irq_flag;
for (unsigned int idx = 0; idx < num_listeners; ++idx)
listener[idx]->TellIrq(tx.irq_flag, rx.irq_flag);
}
void Uart::SetXcvrIrqEnable (xcvr & x, enable_t irq_enable)
{
x.irq_enable = irq_enable;
for (unsigned int idx = 0; idx < num_listeners; ++idx)
listener[idx]->TellIrqEnabled(irq_enable, tx.irq_enable, rx.irq_enable);
}
void Uart::SetXcvrDataBits (xcvr & x, unsigned int data_bits)
{
x.data_bits = data_bits;
for (unsigned int idx = 0; idx < num_listeners; ++idx)
listener[idx]->TellDataBits(tx.data_bits, rx.data_bits);
}
void Uart::SetXcvrStopBits (xcvr & x, unsigned int stop_bits)
{
x.twice_stop_bits = stop_bits;
for (unsigned int idx = 0; idx < num_listeners; ++idx)
listener[idx]->TellDataBits(tx.twice_stop_bits, rx.twice_stop_bits);
}
void Uart::SetXcvrEnable (xcvr & x, enable_t enable)
{
x.enable = enable;
for (unsigned int idx = 0; idx < num_listeners; ++idx)
listener[idx]->TellEnabled(tx.enable, rx.enable);
}
void Uart::SetXcvrParity (xcvr & x, parity_t parity)
{
x.parity = parity;
for (unsigned int idx = 0; idx < num_listeners; ++idx)
listener[idx]->TellParity(tx.parity, rx.parity);
}
void Uart::SetXcvrBitsPerKsec (xcvr & x, unsigned long bits_per_ksec)
{
x.bits_per_ksec = bits_per_ksec;
x.bit_time = calc_bit_time(bits_per_ksec);
for (unsigned int idx = 0; idx < num_listeners; ++idx)
listener[idx]->TellBaud(tx.bits_per_ksec, rx.bits_per_ksec);
}
void Uart::SetIrqEnable (bool flag)
{
irq_enable = flag;
for (unsigned int idx = 0; idx < num_listeners; ++idx)
listener[idx]->TellIrqEnabled(irq_enable, tx.irq_enable, rx.irq_enable);
}
// private member functions
unsigned int Uart::num_one_bits (unsigned char byte)
{
static int bcnt[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
return bcnt[byte & 0xF] + bcnt[byte / 16];
}
unsigned long Uart::double_bits (unsigned char byte)
{
static unsigned long dbit[16] = { 0x00, 0x03, 0x0C, 0x0F, 0x30, 0x33, 0x3C, 0x3F,
0xC0, 0xC3, 0xCC, 0xCF, 0xF0, 0xF3, 0xFC, 0xFF };
return dbit[byte & 0xF] + (256 * dbit[byte / 16]);
}
/* builds the values that will actually be transmitted at a
rate of 2*baud in order to handle 1.5 stop bits - i.e.
builds a 20 bit value for an 8N1 character. */
unsigned long Uart::build_tx_shift (unsigned char tx_data)
{
int twice_data_bits;
unsigned long tx_shift;
/* initialize to stop bits and prepare for data & parity */
tx_shift = ~(0ul);
twice_data_bits = 2 * tx.data_bits;
if (tx.parity == PARITY_NONE)
tx_length = twice_data_bits;
else
tx_length = 2 + twice_data_bits;
tx_shift <<= tx_length;
tx_length += 2;
tx_length += tx.twice_stop_bits;
/* data bits */
tx_shift |= double_bits(tx_data) & (0xFFFFul >> (16 - twice_data_bits));
/* parity bit */
if (tx.parity == PARITY_ODD)
{
if ((num_one_bits(tx_data) & 1) == 0)
tx_shift |= (0x3 << twice_data_bits);
}
else if (tx.parity == PARITY_EVEN)
{
if ((num_one_bits(tx_data) & 1) == 1)
tx_shift |= (0x3 << twice_data_bits);
}
else if (tx.parity == PARITY_MARK)
{
tx_shift |= (0x3 << twice_data_bits);
}
/* start bits */
tx_shift <<= 2;
return tx_shift;
}
unsigned long Uart::calc_bit_time (unsigned long bits_per_ksec)
{
if (bits_per_ksec == 0) return 0;
/* this returns half the actual number of nanoseconds per bit
since bits are shifted out at twice the simulated baud rate */
return (unsigned long)((5e11 / (double)bits_per_ksec) + 0.5);
}
// end