-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOMREDIR.C
294 lines (195 loc) · 5.16 KB
/
COMREDIR.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
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "dos.h"
/* Constants */
#define uchar char
#define uint unsigned int
#define ulong unsigned long
#define BAUD(x) ((uint)(115200/x))
#define DATA_5 0
#define DATA_6 1
#define DATA_7 2
#define DATA_8 3
#define STOP_1 0
#define STOP_2 4
#define PARITY_N 0
#define PARITY_O 8
#define PARITY_E 24
#define PARITY_M 40
#define PARITY_S 56
#define DLAB 128
#define BREAK 64
#define EN_RS_INPUT 8
#define EN_ERROR 4
#define EN_SEND 2
#define EN_RECV 1
#define ID_RS_INPUT 0
#define ID_ERROR 6
#define ID_SEND 2
#define ID_RECV 4
#define DATA 0
#define INT_ENABLE 1
#define INT_IDENT 2
#define FIFO 2
#define LINE_CONTROL 3
#define MODEM_CONTROL 4
#define LINE_STATUS 5
#define MODEM_STATUS 6
#define LOOPBACK 16
#define GP_OUT1 4
#define GP_OUT2 8
#define RTS 2
#define DTR 1
#define BUFFERSIZE 2048
#define PORTIN (*ports[portnum].inbuffer)
#define PORTOUT (*ports[portnum].outbuffer)
#define PORTSTATUS (*ports[portnum].status)
/* Structures */
struct circle_buff {
uchar *buffer;
uint in;
uint out;
uint diff;
};
struct com_status {
uchar enabled_ints;
uchar modem_control;
uchar line_stat;
uchar modem_stat;
};
struct comport {
uchar irq;
uchar intr;
uint port;
struct com_status *status;
struct circle_buff *inbuffer;
struct circle_buff *outbuffer;
};
/* Global Variables */
struct comport ports[2];
struct circle_buff buffer[2];
/* Functions */
void com_init(uchar portnum,uint baud,uchar data,uchar stop,uchar parity,void interrupt (*isr)())
{
uchar setbyte;
outportb(ports[portnum].port+INT_ENABLE,0); /* turn port "off" */
outportb(ports[portnum].port+FIFO,0);
outportb(ports[portnum].port+MODEM_CONTROL,0);
setbyte=DLAB; /* enable baud rate bytes */
outportb(ports[portnum].port+LINE_CONTROL,setbyte);
outport(ports[portnum].port+DATA,BAUD(baud));
setbyte=data+stop+parity; /* set line for 8N1 */
outportb(ports[portnum].port+LINE_CONTROL,setbyte);
setvect(ports[portnum].intr,isr); /* put handle routine into modems IRQ */
setbyte=RTS+DTR+GP_OUT2; /* Enable RTS,DTR and General Purpose output #2 */
outportb(ports[portnum].port+MODEM_CONTROL,setbyte);
setbyte=EN_RECV+EN_SEND; /* Enable interrupts */
outportb(ports[portnum].port+INT_ENABLE,setbyte);
}
struct com_status *getcom_status(uchar portnum)
{
struct com_status status;
status.enabled_ints=inportb(ports[portnum].port+INT_ENABLE);
status.modem_control=inportb(ports[portnum].port+MODEM_CONTROL);
status.line_stat=inportb(ports[portnum].port+LINE_STATUS);
status.modem_stat=inportb(ports[portnum].port+MODEM_STATUS);
return &status;
}
void rs_error(uchar portnum)
{}
void rs_input(uchar portnum)
{}
void receive_data(uchar portnum)
{
*(PORTIN.buffer+PORTIN.in)=inportb(ports[portnum].port+DATA);
printf("%c",*(PORTIN.buffer+PORTIN.in));
PORTIN.in+=1;
PORTIN.diff+=1;
if (PORTIN.in=BUFFERSIZE)
PORTIN.in=0;
/*if (PORTIN.diff>=BUFFERSIZE)
{
ports[portnum].status.modem_control=(ports[portnum].status.modem_control)^RTS;
outportb(ports[portnum].port+MODEM_CONTROL,ports[portnum].status.modem_control);
}*/
}
void send_data(uchar portnum)
{
if (PORTOUT.diff>0)
outportb(ports[portnum].port+DATA,*(PORTOUT.buffer+PORTOUT.out));
PORTOUT.in+=1;
PORTOUT.diff-=1;
if (PORTOUT.in=BUFFERSIZE)
PORTOUT.in=0;
/*if (PORTOUT.diff>=BUFFERSIZE)
{
ports[portnum].status.modem_control=(ports[portnum].status.modem_control)^RTS;
outportb(ports[portnum].port+MODEM_CONTROL,ports[portnum].status.modem_control);
}*/
}
void interrupt handle_first_com()
{
uchar getbyte,setbyte;
disable(); /* Disable all interrupts */
getbyte=inport(ports[0].port+INT_IDENT);
switch (getbyte)
{
case ID_RECV:
receive_data(0);
case ID_SEND:
send_data(0);
case ID_RS_INPUT:
rs_input(0);
case ID_ERROR:
rs_error(0);
}
enable(); /* Enable all interrupts */
}
void interrupt handle_second_com()
{
uchar getbyte,setbyte;
disable(); /* Disable all interrupts */
getbyte=inport(ports[1].port+INT_IDENT);
switch (getbyte)
{
case ID_RECV:
receive_data(1);
case ID_SEND:
send_data(1);
case ID_RS_INPUT:
rs_input(1);
case ID_ERROR:
rs_error(1);
}
enable(); /* Enable all interrupts */
}
void initialize_data()
{
/* Set up port Parameters */
ports[0].intr=0x0c; /* IRQ 4 */
ports[1].intr=0x0b; /* IRQ 3 */
ports[0].port=0x3f8; /* COM1 */
ports[1].port=0x2f8; /* COM3 */
ports[0].inbuffer=&buffer[0]; /* Assign Buffers */
ports[0].outbuffer=&buffer[1];
ports[1].inbuffer=&buffer[1];
ports[1].outbuffer=&buffer[0];
buffer[0].buffer=malloc(BUFFERSIZE);
buffer[1].buffer=malloc(BUFFERSIZE);
buffer[0].in=0;
buffer[1].in=0;
buffer[0].out=0;
buffer[1].out=0;
buffer[0].diff=0;
buffer[1].diff=0;
}
main()
{
initialize_data();
com_init(0,115200,DATA_8,STOP_1,PARITY_N,&handle_first_com);
com_init(1,115200,DATA_8,STOP_1,PARITY_N,&handle_second_com);
ports[0].status=getcom_status(0);
ports[1].status=getcom_status(1);
while (1){}
}