1 #include <string.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include "fix.h"
5 #include "timer.h"
6 #include "mono.h"
7 #include "commlib.h"
8
9 #define F_1_1000 (F1_0/1000) // 1/1000
10
11 struct portparam { int base,irq; };
12 struct portparam portparams[4] = {{0x3f8, 4}, {0x2f8, 3}, {0x3e8, 4}, {0x2e8, 3}};
13
14 /* return Data Carrier Detect status */
comm_get_dcd(comm_port * port)15 int comm_get_dcd(comm_port *port) {
16 return (inportb(port->MSR) & MSR_DCD) != 0;
17 }
18
comm_printf(PORT * port,char * msg,...)19 void comm_printf(PORT *port, char *msg, ...) {
20 char buf[1024];
21 va_list vp;
22
23 va_start(vp, msg);
24 vsprintf(buf, msg, vp);
25 va_end(vp);
26 mprintf((0, "comm_printf: writing \"%s\"\n", buf));
27 WriteBuffer(port, buf, strlen(buf));
28 }
29
PortOpenGreenleafFast(int port,int baud,char parity,int databits,int stopbits)30 PORT * PortOpenGreenleafFast(int port, int baud, char parity, int databits, int stopbits)
31 {
32 PORT *com;
33 static int did_init = 0;
34
35 if (!did_init) {
36 dzcomm_init();
37 did_init = 1;
38 }
39
40 if (!(com = comm_port_init(port))) {
41 mprintf((1,"comm_port_init: %s\n", szDZCommErr));
42 return NULL;
43 }
44 com->nBaud = baud;
45 com->nPort = portparams[port].base;
46 com->nIRQ = portparams[port].irq;
47 switch (parity) {
48 case 'N': com->nParity = NO_PARITY; break;
49 case 'E': com->nParity = EVEN_PARITY; break;
50 case 'O': com->nParity = ODD_PARITY; break;
51 }
52 switch (databits) {
53 case 5: com->nData = BITS_5; break;
54 case 6: com->nData = BITS_6; break;
55 case 7: com->nData = BITS_7; break;
56 case 8: com->nData = BITS_8; break;
57 }
58 switch (stopbits) {
59 case 1: com->nStop = STOP_1; break;
60 case 2: com->nStop = STOP_2; break;
61 }
62 com->control_type = NO_CONTROL;
63 if (!comm_port_install_handler(com)) {
64 mprintf((1,"comm_port_install: %s\n", szDZCommErr));
65 comm_port_delete(com);
66 return NULL;
67 }
68 return com;
69 }
70
SetDtr(PORT * port,int state)71 void SetDtr(PORT *port,int state)
72 {
73 return;
74 }
75
SetRts(PORT * port,int state)76 void SetRts(PORT *port,int state)
77 {
78 return;
79 }
80
UseRtsCts(PORT * port,int state)81 void UseRtsCts(PORT *port,int state)
82 {
83 #if 0 /* disabled for now */
84 if (state && port->control_type != RTS_CTS) {
85 port->cts=CTS_ON;
86 port->rts=RTS_ON;
87 if (!queue_empty(port->OutBuf))
88 outportb(port->MCR,inportb(port->MCR)|0x02); //setting RTS
89 port->control_type = RTS_CTS;
90 } else if (!state && port->control_type == RTS_CTS) {
91 port->control_type = NO_CONTROL;
92 if (!queue_empty(port->OutBuf))
93 interrupt_on(port, THREINT);
94 }
95 #endif
96 return;
97 }
98
WriteChar(PORT * port,char ch)99 void WriteChar(PORT *port, char ch)
100 {
101 //mprintf((0, "(%d,%d)", port->OutBuf->head, port->OutBuf->tail));
102 //mprintf((0, ">%02x ", (unsigned char)ch));
103 comm_port_out(port, ch);
104 return;
105 }
106
ClearRXBuffer(PORT * port)107 void ClearRXBuffer(PORT *port)
108 {
109 queue_reset(port->InBuf);
110 }
111
ReadBufferTimed(PORT * port,char * buf,int length,int timeout)112 int ReadBufferTimed(PORT *port, char *buf, int length, int timeout)
113 {
114 fix stop = timer_get_fixed_seconds() + timeout * F_1_1000;
115 int c;
116
117 if (!length)
118 return 1; // ok
119 while (stop >= timer_get_fixed_seconds()) {
120 if ((c = comm_port_test(port)) >= 0) {
121 mprintf((0, "<%02x ", c));
122 *(buf++) = c;
123 if (!(--length))
124 return 1; // ok
125 }
126
127 }
128 return 0; // timeout
129 }
130
Change8259Priority(int irq)131 int Change8259Priority(int irq)
132 {
133 return ASSUCCESS;
134 }
135
FastSetPortHardware(int port,int IRQ,int baseaddr)136 int FastSetPortHardware(int port, int IRQ, int baseaddr)
137 {
138 portparams[port].irq = IRQ;
139 portparams[port].base = baseaddr;
140 return 0;
141 }
142
FastGetPortHardware(int port,int * IRQ,int * baseaddr)143 int FastGetPortHardware(int port, int *IRQ, int *baseaddr)
144 {
145 *IRQ = portparams[port].irq;
146 *baseaddr = portparams[port].base;
147 return 0;
148 }
149
FastSet16550TriggerLevel(int level)150 void FastSet16550TriggerLevel(int level)
151 {
152 return;
153 }
FastSet16550UseTXFifos(int on)154 void FastSet16550UseTXFifos(int on)
155 {
156 return;
157 }
158
FastSavePortParameters(int port)159 void FastSavePortParameters(int port)
160 {
161 }
162
PortClose(PORT * port)163 int PortClose(PORT *port)
164 {
165 comm_port_delete(port);
166 return 0;
167 }
168
FastRestorePortParameters(int port)169 void FastRestorePortParameters(int port)
170 {
171 }
172
GetCd(PORT * port)173 int GetCd(PORT *port)
174 {
175 return comm_get_dcd(port);
176 }
177
ReadCharTimed(PORT * port,int timeout)178 int ReadCharTimed(PORT *port, int timeout)
179 {
180 fix stop = timer_get_fixed_seconds() + timeout * F_1_1000;
181 int c = -1;
182
183 while (stop >= timer_get_fixed_seconds() && (c = comm_port_test(port)) < 0)
184 ;
185 if (c >= 0)
186 mprintf((0, "<%02x ", c));
187 return c;
188 }
189
ReadChar(PORT * port)190 int ReadChar(PORT *port)
191 {
192 int c = comm_port_test(port);
193 //if (c >= 0)
194 // mprintf((0, "<%02x ", c));
195 return c;
196 }
197
ClearLineStatus(PORT * port)198 void ClearLineStatus(PORT *port)
199 {
200 }
201
WriteBuffer(PORT * port,char * pbuff,int len)202 void WriteBuffer(PORT *port, char *pbuff, int len)
203 {
204 mprintf((0, ">x%d ", len));
205 while (len--)
206 comm_port_out(port, *(pbuff++));
207 }
208
209
HMInputLine(PORT * port,int timeout,char * buf,int bufsize)210 int HMInputLine(PORT *port, int timeout, char *buf, int bufsize)
211 {
212 fix stop = timer_get_fixed_seconds() + timeout * F_1_1000;
213 int c;
214 char *p = buf;
215 int bufleft = bufsize;
216
217 mprintf((1, "port->InBuf->head=%d, port->InBuf->tail=%d\n", port->InBuf->head,
218 port->InBuf->tail));
219 while (stop >= timer_get_fixed_seconds()) {
220 if ((c = comm_port_test(port)) >= 0) {
221 mprintf((0,"got char %d (%c)", c, c >= 32 ? c : '.'));
222 if (c == '\n' && p > buf) {
223 *p = 0;
224 mprintf((0,"got line \"%s\"", buf));
225 if (p - buf >= 2 && !strncmp(buf, "AT", 2)) { /* assume AT... response is echo */
226 p = buf;
227 bufleft = bufsize;
228 } else {
229 *p = 0;
230 return 1; // ok
231 }
232 }
233 if (c != '\r' && c != '\n') {
234 if (!bufleft)
235 return -1; // buffer overflow
236 *(p++) = c;
237 bufleft--;
238 }
239 }
240 }
241 mprintf((0,"hminputline: timeout\n"));
242 return 0; // timeout
243 }
244
HMWaitForOK(int timeout,char * buf)245 void HMWaitForOK(int timeout, char *buf)
246 {
247 /* what is this supposed to do without a port handle? */
248 }
249
HMSendStringNoWait(PORT * port,char * pbuf,int a)250 void HMSendStringNoWait(PORT *port, char *pbuf, int a)
251 {
252 comm_printf(port, "%s\r", pbuf);
253 }
254
HMSendString(PORT * port,char * msg)255 int HMSendString(PORT *port, char *msg)
256 {
257 // char buf[80];
258
259 HMSendStringNoWait(port, msg, -2);
260 // HMInputLine(port, 5000, buf, sizeof(buf) - 1); /* 5 sec timeout */
261 // if (!strncmp(buf, "OK", 2))
262 // return 0; // Ok
263 // return -1; // Error
264 if(HMInputLine(port, 5000, msg, sizeof(msg) - 1) >= 0)
265 return 0;
266 return -1;
267 }
268 //added on 11/5/98 by Victor Rachels to wait less
HMSendStringWait(PORT * port,char * msg,int wait)269 int HMSendStringWait(PORT *port, char *msg, int wait)
270 {
271 HMSendStringNoWait(port, msg, -2);
272 if(HMInputLine(port, wait, msg, sizeof(msg) - 1) >= 0)
273 return 0;
274 return -1;
275 }
276 //end this section addition - VR
277
HMReset(PORT * port)278 void HMReset(PORT *port)
279 {
280 }
281
HMDial(PORT * port,char * pPhoneNum)282 void HMDial(PORT *port, char *pPhoneNum)
283 {
284 comm_printf(port, "ATD%s\r", pPhoneNum);
285 }
286
HMAnswer(PORT * port)287 void HMAnswer(PORT *port)
288 {
289 comm_printf(port, "ATA\r");
290 }
291
ClearTXBuffer(PORT * port)292 void ClearTXBuffer(PORT *port)
293 {
294 queue_reset(port->OutBuf);
295 }
296
GetLineStatus(PORT * port)297 int GetLineStatus(PORT *port)
298 {
299 return 0;
300 }
301