-
Notifications
You must be signed in to change notification settings - Fork 9
/
serport.c
463 lines (393 loc) · 11.7 KB
/
serport.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/***********************************************************************
// Clifford Kelley <cwkelley@earthlink.net>
// This program is licensed under BSD LICENSE
Version 0.4 Added baud rate selection and NMEA sentence selection capablity
Version 0.3 Turned off hardware flow control
Version 0.2 Fixed SV in view bug
Version 0.1 Initial release
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef __linux__
#include <bios.h>
#include <conio.h>
#include <io.h>
#include <dos.h>
#else
#define far
#endif
#ifdef DJGPP
#include <pc.h>
#endif
#define timeout 10000 /* Readln_com times out at 10000 milliseconds */
#define max_buffer 1000 /* Circular buffer size */
#define near_full 900 /* When buffer_length exceeds near_full */
/* RTS line is disabled */
#define near_empty 100 /* When buffer drops below near_empty */
/* RTS line is enabled */
#if (defined VCPP)
#define Interrupt_Enable() _enable()
#define Interrupt_Disable() _disable()
#else
#define Interrupt_Enable() enable()
#define Interrupt_Disable() disable()
#endif
int com_flag = 0; /*Com open flag */
int overrun_flag;
char com_buffer[max_buffer]; /*Circular com buffer */
int com_port; /* Current Com port (0 or 1) */
int intlev; /* Hardware interrut level for com port */
int buffer_in; /* Pointer for input to com buffer */
int buffer_out; /* Pointer for output from com buffer */
int buffer_length; /* Current number of chars in com buffer */
int bf_hndshk; /* Handshake flag for control lines */
int thr, rbr, ier, lcr, mcr, lsr, msr; /*Async com board registers */
void *oldfunc; /* Holds old interrupt vector */
#if (defined VCPP)
void _interrupt _far com_isr (void); /* PGB MS */
#elif (defined __TURBOC__)
void interrupt com_isr (void);
#elif (defined BCPP)
void interrupt com_isr (...); /* New IRQ0 interrupt handler */
#elif (defined DJGPP)
_go32_dpmi_seginfo com_isr;
#endif
/*void delay(int d)
/* d = delay in milliseconds on 10 MHz AT */
/*{ int i;
int j;
int k;
j = 0;
for (k=0; k < d; k++)
for (i=1; i < 200; i++ )
j += 1;
}*/
#if (defined VCPP)
void interrupt far
com_isr ()
#elif (defined __TURBOC__)
void interrupt
com_isr (void)
#elif (defined BCPP)
void interrupt
com_isr (...)
#elif (defined DJGPP)
void interrupt
com_isr (void)
#else
void
com_isr (void)
#endif
{
if (com_flag == 1) {
/*Get character - store in circular buffer */
com_buffer[buffer_in] = inp (rbr);
/*Increment buffer_in pointer */
buffer_in += 1;
/*Wrap buffer pointer around to start if > max_buffer */
if (buffer_in == max_buffer)
buffer_in = 0;
/*Current number of characters in buffer incremented 1 */
buffer_length += 1;
if (buffer_length > max_buffer) {
buffer_length = max_buffer;
overrun_flag = 1;
}
/*Disable RTS if buffer_length exceeds near_full constant */
if (buffer_length > near_full) {
outp (mcr, 9); /*Disable rts , leave dtr and out2 set */
bf_hndshk = 1; /*Buffer full handshake = true */
}
}
outp (0x20, 0x20); /* End of Interrupt to 8259 */
}
void
reset_buffer ()
/* This procedure will reset the com buffer */
{
bf_hndshk = 0; /*Buffer full handshake false */
buffer_in = 0; /*Set circular buffer input to 0 */
buffer_out = 0; /*Set circular buffer output to 0 */
buffer_length = 0; /*Set buffer_length to 0 */
overrun_flag = 0; /*Set overrun flag to false */
}
void
open_com (int Cport, /*Com port # - 0 or 1 */
unsigned int baud, /*baud rate - 110,150,300..9600 */
int parity, /*parity 0 = no parity */
/* 1 = even parity */
/* 2 = odd parity */
int stopbits, /*stop bits - 1 or 2 */
int numbits, /*word length - 7 or 8 */
int *error_code)
{
int comdata;
int ptemp;
*error_code = 0;
com_port = Cport;
comdata = 0;
if ((numbits == 7) || (numbits == 8))
comdata = comdata | (numbits - 5);
else
*error_code = 5;
if ((stopbits == 2) || (stopbits == 1))
comdata = comdata | ((stopbits - 1) << 2);
else
*error_code = 4;
if ((parity == 1) || (parity == 3) || (parity == 0))
comdata = comdata | (parity << 3);
else
*error_code = 3;
switch (baud) {
case 110:
comdata = comdata | 0x00;
break;
case 150:
comdata = comdata | 0x20;
break;
case 300:
comdata = comdata | 0x40;
break;
case 600:
comdata = comdata | 0x60;
break;
case 1200:
comdata = comdata | 0x80;
break;
case 2400:
comdata = comdata | 0xA0;
break;
case 4800:
comdata = comdata | 0xC0;
break;
case 9600:
comdata = comdata | 0xE0;
break;
case 38400L:
{
goto HighSpeedLinkSetting;
/* break; */
}
default:
*error_code = 2;
break;
}
if ((Cport < 0) || (Cport > 1)) {
*error_code = 1;
}
if (*error_code == 0) {
#ifdef __TURBOC__
bioscom (0, comdata, Cport);
#else
_bios_serialcom (0, Cport, comdata);
#endif
}
/**
* Need this kludge because _bios_serialcom does
* not support any baud rate over 9600.
**/
HighSpeedLinkSetting:
if (baud == 38400L) {
/**
* Setup for baud rate programming
**/
outp (0x3fb, 0x80);
/* Baud rate 38,400 */
outp (0x3f8, 0x03);
outp (0x3f9, 0x00);
outp (0x3fb, 0x00);
/**
* Program the Data Format reg
**/
outp (0x3fb, 0x07); /* 8-N-1 */
}
if (Cport == 0) {
thr = 0x3f8; /* Tx Register 0 */
rbr = 0x3f8; /* Rx Register 0 */
ier = 0x3f9; /* Interrupt enable 1 */
/* 0x3fa Interrupt ID 2 */
lcr = 0x3fb; /* Data format 3 */
mcr = 0x3fc; /* Modem control 4 */
lsr = 0x3fd; /* Serialization status 5 */
msr = 0x3fe; /* Modem status 6 */
}
else {
thr = 0x2f8; /*Set register variables */
rbr = 0x2f8; /*for port locations of */
ier = 0x2f9; /*serial com port #2 */
lcr = 0x2fb;
mcr = 0x2fc;
lsr = 0x2fd;
msr = 0x2fe;
}
intlev = 0xC - Cport;
#if (defined VCPP)
oldfunc = _dos_getvect (intlev);
_dos_setvect (intlev, com_isr);
Interrupt_Disable (); /* No interrupts */
#elif ((defined BCPP) || (defined __TURBOC__))
oldfunc = getvect (intlev);
setvect (intlev, com_isr);
Interrupt_Disable ();
#endif
ptemp = inp (lcr) & 0x7f;
outp (lcr, ptemp);
ptemp = inp (lsr); /* Reset any pending errors */
ptemp = inp (rbr); /* Read any pending character */
if (Cport == 0) { /* Set irq on 8259 controller */
ptemp = inp (0x21) & 0xef;
outp (0x21, ptemp);
}
else {
ptemp = inp (0x21) & 0xf7;
outp (0x21, ptemp);
}
outp (ier, 1); /* Enable data ready interrupt */
ptemp = inp (mcr) | 0xb;
outp (mcr, ptemp);
Interrupt_Enable (); /*Turn on interrupts */
*error_code = 0; /*Set error code to 0 */
com_flag = 1; /*Com inititalization flag true */
reset_buffer ();
}
void
close_com ()
/* This procedure disables the com port interrupt. */
{
int ptemp;
if (com_flag == 1) {
Interrupt_Disable (); /* No interrupts */
ptemp = inp (0x21) | 0x18;
outp (0x21, ptemp); /* Set mask register to turn off interrupt */
ptemp = inp (lcr) | 0x7f;
outp (lcr, ptemp); /* Turn off 8250 data ready interrupt */
outp (ier, 0);
outp (mcr, 0); /* Disable out2 on 8250 */
/* return to old interrupt vector */
/* PGB _dos_setvect( intlev, oldfunc );*/
Interrupt_Enable (); /* Turn on interrupts */
com_flag = 0;
}
}
/**
* This procedure returns 1 character from the com_buffer, at the
* array element pointed to by the circular buffer pointer buffer_out.
**/
/* 0 = no error */
/* 6 = no character available */
/* 7 = buffer overflow */
/* 10 = com port not initialized */
void
check_com (char *c, int *error_code)
{ /* error code for check_com */
if (com_flag == 0) {
/*Make sure com port has been initialized */
*error_code = 10;
}
else {
if (buffer_length == 0) {
/*Check to see if any characters in buffer */
*error_code = 6;
}
else {
if (overrun_flag == 1) {
/*buffer overflow */
*error_code = 7;
}
else {
*error_code = 0;
}
*c = (com_buffer[buffer_out]); /*Get charater out of buffer */
buffer_out += 1; /*Increment buffer_out_pointer */
/*Wrap buffer_out pointer around if > */
/*max_buffer */
if (buffer_out == max_buffer) {
buffer_out = 0;
}
buffer_length -= 1; /*Decrement buffer_length */
/*Enable RTS if buffer_length < near_empty */
if (bf_hndshk && (buffer_length < near_empty)) {
outp (mcr, 0xb);
bf_hndshk = 0;
}
}
}
}
void
send_com (char c, /*Character to send out com port */
int *error_code)
{
/* Error code for send_com */
/* 0 = no error */
/* 8 = time out error */
/* 10 = com port not initialized */
/* This procedure sends a character out the com port. */
int handshake;
int counter;
if (com_flag == 0) {
/*Make sure com port has been initialized */
*error_code = 10;
}
else {
counter = 0; /* Initialize time out counter */
handshake = 0; /* 0x30; */
/* Use the following handshake values: */
/* 0x0 no handshake */
/* 0x10 CTS handshaking */
/* 0x20 DSR handshaking */
/* 0x30 CTS and DSR handshaking */
do {
counter += 1;
/* delay 1 millisecond - causes timeout at 10 seconds*/
/* delay( 1 ); */
} while ((((inp (msr) & handshake) != handshake) || /*Check handshake */
((inp (lsr) & 0x20) != 0x20)) && /*Check that xmit reg empty */
(counter < timeout));
/* Give up after 10 seconds */
if (counter == timeout) {
*error_code = 8;
}
else {
Interrupt_Disable (); /* No interrpts */
outp (thr, c); /* Transmit character */
Interrupt_Enable (); /* Interrupts on */
*error_code = 0;
}
}
}
int
ComPortWrite (unsigned char *str, /* string to send out com port */
int NumberOfBytes)
{ /* error code for writeln_com */
int error_code;
int i;
for (i = 0; i < NumberOfBytes; i++) {
send_com (str[i], &error_code);
}
return 1;
}
int
readln_com (char *str, /* string to received from com port */
int *error_code)
{ /* error code for writeln_com */
int i = 0;
char c;
int counter = 0;
do {
check_com (&c, error_code);
if (*error_code == 0) {
str[i] = c;
i += 1;
}
else {
delay (1);
counter += 1;
}
} while ((i < 255) && (c != 13) && (counter < timeout));
if (counter == timeout) {
*error_code = 8;
}
str[i] = 0;
return i;
}