-
Notifications
You must be signed in to change notification settings - Fork 199
/
uart.js
316 lines (264 loc) · 10.7 KB
/
uart.js
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
// -------------------------------------------------
// -------------------- UART -----------------------
// -------------------------------------------------
// uart16550 compatible
// the driver source is spread in drivers/tty/serial/8250/
// See
// http://www.tldp.org/HOWTO/Serial-HOWTO-18.html
// http://www.lammertbies.nl/comm/info/serial-uart.html
// http://www.freebsd.org/doc/en/articles/serial-uart/
"use strict";
var message = require('../messagehandler');
var utils = require('../utils');
// Register offsets
var UART_RXBUF = 0; /* R: Rx buffer, DLAB=0 */
var UART_TXBUF = 0; /* W: Tx buffer, DLAB=0 (also called transmitter hoilding register */
var UART_DLL = 0; /* R/W: Divisor Latch Low, DLAB=1 */
var UART_DLH = 1; /* R/W: Divisor Latch High, DLAB=1 */
var UART_IER = 1; /* R/W: Interrupt Enable Register */
var UART_IIR = 2; /* R: Interrupt ID Register */
var UART_FCR = 2; /* W: FIFO Control Register */
var UART_LCR = 3; /* R/W: Line Control Register */
var UART_MCR = 4; /* W: Modem Control Register */
var UART_LSR = 5; /* R: Line Status Register */
var UART_MSR = 6; /* R: Modem Status Register */
var UART_SCR = 7; /* R/W: Scratch Register*/
// Line Status register bits
var UART_LSR_DATA_READY = 0x01; // data available
var UART_LSR_TX_EMPTY = 0x20; // TX (THR) buffer is empty
var UART_LSR_TRANSMITTER_EMPTY = 0x40; // TX empty and line is idle
var UART_LSR_INT_ANY = 0x1E; // Any of the lsr-interrupt-triggering status bits
// Interrupt enable register bits
var UART_IER_MSI = 0x08; /* Modem Status Changed int. */
var UART_IER_RLSI = 0x04; /* Enable Break int. Enable receiver line status interrupt */
var UART_IER_THRI = 0x02; /* Enable Transmitter holding register int. */
var UART_IER_RDI = 0x01; /* Enable receiver data interrupt */
// Interrupt identification register bits
var UART_IIR_MSI = 0x00; /* Modem status interrupt (Low priority). Reset by MSR read */
var UART_IIR_NO_INT = 0x01;
var UART_IIR_THRI = 0x02; /* Transmitter holding register empty. Reset by IIR read or THR write */
var UART_IIR_RDI = 0x04; /* Receiver data interrupt. Reset by RBR read */
var UART_IIR_RLSI = 0x06; /* Receiver line status interrupt (High p.). Reset by LSR read */
var UART_IIR_CTI = 0x0c; /* Character timeout. Reset by RBR read */
// Line control register bits
var UART_LCR_DLAB = 0x80; /* Divisor latch access bit */
// Modem control register bits
var UART_MCR_DTR = 0x01; /* Data Terminal Ready - Kernel ready to receive */
var UART_MCR_RTS = 0x02; /* Request To Send - Kernel ready to receive */
// Modem status register bits
var UART_MSR_DCD = 0x80; /* Data Carrier Detect */
var UART_MSR_DSR = 0x20; /* Data set Ready */
var UART_MSR_DELTA_DSR = 0x2;
var UART_MSR_CTS = 0x10; /* Clear to Send */
var UART_MSR_DELTA_CTS = 0x1;
// register descriptions for debug mode
var MCR_BIT_DESC = ["DataTerminalReady", "RTS", "AuxOut1", "AuxOut2", "Loopback", "Autoflow"/*16750*/];
var FCR_BIT_DESC = ["FIFO enable", "Reset", "XMIT-FIFO-Reset", "DMA-Mode", "Reserved", "Reserved", "RecrTrig(LSB)", "RecrTrig(MSB)"];
var LCR_BIT_DESC = ["WordLen", "WordLen", "StopBits", "Parity", "EvenParity", "StickParity", "Break", "DivisorLatch"];
var MSR_BIT_DESC = ["DeltaCTS", "DeltaDataSetReady", "DeltaRingIndicator", "DeltaCarrierDetect", "ClearToSend", "DataSetReady", "RingIndicator", "CarrierDetect"];
var LSR_BIT_DESC = ["RxDataAvail", "OverrunErr", "ParityErr", "FrameErr", "BreakSignal", "TxEmpty", "TxEmptyLine", "BadRxFifoData"];
var IER_BIT_DESC = ["RxAvailableI", "TxEmptyI", "BreakI", "MSI"];
// constructor
function UARTDev(id, intdev, intno) {
this.intno = intno;
this.intdev = intdev;
this.id = id;
this.verboseuart = false;
message.Register("tty" + id, this.ReceiveChar.bind(this) );
this.Reset();
}
UARTDev.prototype.ToBitDescription = function(val, desc) {
val &= 0xff;
var result= ("00000000" + val.toString(2)).substr(-8)+ ":"
for(var i=0; i < desc.length; i++) {
result += " " + desc[i] + ":" + ((val>>i)&1);
}
return result;
}
UARTDev.prototype.Reset = function() {
this.LCR = 0x3; // Line Control, reset, character has 8 bits
this.LSR = UART_LSR_TRANSMITTER_EMPTY | UART_LSR_TX_EMPTY; // Transmitter serial register empty and Transmitter buffer register empty
this.MSR = UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS; // modem status register
this.ints = 0x0; // internal interrupt pending register
this.IIR = UART_IIR_NO_INT; // Interrupt Identification, no interrupt
this.IER = 0x0; //Interrupt Enable
this.DLL = 0x0;
this.DLH = 0x0;
this.FCR = 0x0; // FIFO Control;
this.MCR = 0x0; // Modem Control
this.rxbuf = new Array(); // receive fifo buffer.
this.txbuf = new Array(); // transmit fifo buffer.
}
UARTDev.prototype.Step = function() {
if(this.txbuf.length != 0) {
message.Send("tty" + this.id, this.txbuf);
this.txbuf = new Array();
}
}
// To prevent the character from being overwritten we use a javascript array-based fifo and request a character timeout.
UARTDev.prototype.ReceiveChar = function(data) {
data.forEach(function(c) {
this.rxbuf.push(c&0xFF);
}.bind(this));
if (this.verboseuart) message.Debug("Received bytes: " + this.rxbuf.length);
if (this.rxbuf.length > 0) {
this.LSR |= UART_LSR_DATA_READY;
this.ThrowInterrupt(UART_IIR_CTI);
}
}
UARTDev.prototype.CheckInterrupt = function() {
if (this.verboseuart) message.Debug("Check Interrupt " + this.ints);
if ((this.LSR & UART_LSR_INT_ANY) && (this.IER & UART_IER_RLSI)) {
this.IIR = UART_IIR_RLSI;
if (this.verboseuart) message.Debug('IIR_RLSI ' + this.intno);
this.intdev.RaiseInterrupt(this.intno);
} else
if ((this.ints & (1 << UART_IIR_CTI)) && (this.IER & UART_IER_RDI)) {
this.IIR = UART_IIR_CTI;
if (this.verboseuart) message.Debug('IIR_CTI ' + this.intno);
this.intdev.RaiseInterrupt(this.intno);
} else
if ((this.ints & (1 << UART_IIR_THRI)) && (this.IER & UART_IER_THRI)) {
this.IIR = UART_IIR_THRI;
if (this.verboseuart) message.Debug('IIR_THRI ' + this.intno);
this.intdev.RaiseInterrupt(this.intno);
} else
if ((this.ints & (1 << UART_IIR_MSI)) && (this.IER & UART_IER_MSI)) {
this.IIR = UART_IIR_MSI;
if (this.verboseuart) message.Debug('IIR_MSI ' + this.intno);
this.intdev.RaiseInterrupt(this.intno);
} else {
this.IIR = UART_IIR_NO_INT;
if (this.verboseuart) message.Debug('IIR_NO_INT ' + this.intno);
this.intdev.ClearInterrupt(this.intno);
}
};
UARTDev.prototype.ThrowInterrupt = function(line) {
this.ints |= (1 << line);
this.CheckInterrupt();
}
UARTDev.prototype.ClearInterrupt = function(line) {
this.ints &= ~(1 << line);
this.CheckInterrupt();
};
UARTDev.prototype.ReadReg8 = function(addr) {
if (this.LCR & UART_LCR_DLAB) { // Divisor latch access bit
switch (addr) {
case UART_DLL:
return this.DLL;
break;
case UART_DLH:
return this.DLH;
break;
}
}
switch (addr) {
case UART_RXBUF:
var ret = 0x0; // if the buffer is empty, return 0
if (this.rxbuf.length > 0) {
ret = this.rxbuf.shift();
}
if (this.verboseuart) message.Debug("Get RXBUF");
if (this.rxbuf.length == 0) {
this.LSR &= ~UART_LSR_DATA_READY;
this.ClearInterrupt(UART_IIR_CTI);
}
return ret & 0xFF;
break;
case UART_IER:
return this.IER & 0x0F;
break;
case UART_MSR:
var ret = this.MSR;
this.MSR &= 0xF0; // reset lowest 4 "delta" bits
if (this.verboseuart) message.Debug("Get UART_MSR " + this.ToBitDescription(ret, MSR_BIT_DESC));
return ret;
break;
case UART_IIR:
{
// the two top bits (fifo enabled) are always set
var ret = (this.IIR & 0x0F) | 0xC0;
if (this.IIR == UART_IIR_THRI) {
this.ClearInterrupt(UART_IIR_THRI);
}
return ret;
break;
}
case UART_LCR:
return this.LCR;
break;
case UART_LSR:
// This gets polled many times a second, so logging is commented out
if (this.verboseuart) message.Debug("Get UART_LSR " + this.ToBitDescription(this.LSR, LSR_BIT_DESC));
return this.LSR;
break;
default:
message.Debug("Error in ReadRegister: not supported");
message.Abort();
break;
}
};
UARTDev.prototype.WriteReg8 = function(addr, x) {
x &= 0xFF;
if (this.LCR & UART_LCR_DLAB) {
switch (addr) {
case UART_DLL:
this.DLL = x;
return;
break;
case UART_DLH:
this.DLH = x;
return;
break;
}
}
switch (addr) {
case UART_TXBUF:
// we assume here, that the fifo is on
// In the uart spec we reset UART_IIR_THRI now ...
this.LSR &= ~UART_LSR_TRANSMITTER_EMPTY;
this.LSR &= ~UART_LSR_TX_EMPTY;
this.txbuf.push(x);
if (this.verboseuart) message.Debug("send " + x);
// the data is sent immediately
this.LSR |= UART_LSR_TRANSMITTER_EMPTY | UART_LSR_TX_EMPTY; // txbuffer is empty immediately
this.ThrowInterrupt(UART_IIR_THRI);
break;
case UART_IER:
// 2 = 10b ,5=101b, 7=111b
this.IER = x & 0x0F; // only the first four bits are valid
this.ints &= ~(1 << UART_IER_THRI);
if (this.verboseuart) message.Debug("Set UART_IER " + this.ToBitDescription(x, IER_BIT_DESC));
// Check immediately if there is a interrupt pending
this.CheckInterrupt();
break;
case UART_FCR:
if (this.verboseuart) message.Debug("Set UART_FCR " + this.ToBitDescription(x, FCR_BIT_DESC));
this.FCR = x & 0xC9;
if (x & 2) {
this.ClearInterrupt(UART_IIR_CTI);
this.rxbuf = new Array(); // clear receive fifo buffer
}
if (x & 4) {
this.txbuf = new Array(); // clear transmit fifo buffer
this.ClearInterrupt(UART_IIR_THRI);
}
break;
case UART_LCR:
if (this.verboseuart) message.Debug("Set UART_LCR " + this.ToBitDescription(x, LCR_BIT_DESC));
if ((this.LCR & 3) != 3) {
message.Debug("Warning in UART: Data word length other than 8 bits are not supported");
}
this.LCR = x;
break;
case UART_MCR:
if (this.verboseuart) message.Debug("Set UART_MCR " + this.ToBitDescription(x,MCR_BIT_DESC));
this.MCR = x;
break;
default:
message.Debug("Error in WriteRegister: not supported");
message.Abort();
break;
}
};
module.exports = UARTDev;