Debian Codegraph @

src/win32/link_ser.cc

raw
1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3
4 /* libticables2 - link cable library, a part of the TiLP project
5 * Copyright (C) 1999-2005 Romain Lievin
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /* "Home-made serial" link & "Black TIGraphLink" link unit (low-level I/O routines) */
23
24 #include <stdio.h>
25
26 #include "../ticables.h"
27 #include "../logging.h"
28 #include "../error.h"
29 #include "../gettext.h"
30 #include "../internal.h"
31 #include "detect.h"
32 #include "ioports.h"
33
34 #define com_out (h->address + 4)
35 #define com_in (h->address + 6)
36
37 #define hCom (HANDLE)(h->priv)
38 #define BUFFER_SIZE 1024
39
40 static int ser_prepare(CableHandle *h)
41 {
42 const char * device;
43 switch(h->port)
44 {
45 case PORT_1: h->address = 0x3f8; device = "COM1"; break;
46 case PORT_2: h->address = 0x2f8; device = "COM2"; break;
47 case PORT_3: h->address = 0x3e8; device = "COM3"; break;
48 case PORT_4: h->address = 0x3e8; device = "COM4"; break;
49 default: return ERR_ILLEGAL_ARG;
50 }
51
52 if (h->device == NULL)
53 {
54 h->device = strdup(device);
55 }
56
57 // detect OS
58 if(win32_check_os() == WIN_NT)
59 {
60 if(win32_check_dha())
61 {
62 free(h->device); h->device = NULL;
63 return ERR_DHA_NOT_FOUND;
64 }
65 }
66 else if(win32_check_os() == WIN_64)
67 {
68 if(win32_check_rwp())
69 {
70 free(h->device); h->device = NULL;
71 return ERR_DHA_NOT_FOUND;
72 }
73 }
74
75 return 0;
76 }
77
78 static int ser_open(CableHandle *h)
79 {
80 // Under Win2k: if we do not open the serial device as a COM
81 // port, it may be impossible to transfer data.
82 // This problem exists with Win2k and some UARTs.
83 // It seems I got the same problem as FlashZ when I changed my
84 // motherboard. Are some ports broken ?
85 int ret;
86 HANDLE hcom;
87 ret = win32_comport_open(h->device, &hcom);
88 if (!ret)
89 {
90 h->priv = (void *)hcom;
91 ret = io_open(com_out);
92 if (!ret)
93 {
94 ret = io_open(com_in);
95 }
96 }
97
98 return ret;
99 }
100
101 static int ser_close(CableHandle *h)
102 {
103 int ret;
104 ret = io_close(com_out);
105 if (!ret)
106 {
107 ret = io_close(com_in);
108 if (!ret)
109 {
110 HANDLE hcom = hCom;
111 ret = win32_comport_close(&hcom);
112 if (!ret)
113 {
114 h->priv = (void *)hcom;
115 }
116 }
117 }
118
119 return ret;
120 }
121
122 static int ser_reset(CableHandle *h)
123 {
124 tiTIME clk;
125
126 // wait for releasing of lines
127 TO_START(clk);
128 do
129 {
130 io_wr(com_out, 3);
131 if (TO_ELAPSED(clk, h->timeout))
132 return 0;
133 }
134 while ((io_rd(com_in) & 0x30) != 0x30);
135
136 return 0;
137 }
138
139 static int ser_probe(CableHandle *h)
140 {
141 unsigned int timeout = 1;
142 tiTIME clk;
143
144 // 1
145 io_wr(com_out, 2);
146 TO_START(clk);
147 do
148 {
149 if (TO_ELAPSED(clk, timeout))
150 {
151 return ERR_WRITE_TIMEOUT;
152 }
153 } while ((io_rd(com_in) & 0x10));
154
155 io_wr(com_out, 3);
156 TO_START(clk);
157 do
158 {
159 if (TO_ELAPSED(clk, timeout))
160 {
161 return ERR_WRITE_TIMEOUT;
162 }
163 } while ((io_rd(com_in) & 0x10) == 0x00);
164
165 // 0
166 io_wr(com_out, 1);
167 TO_START(clk);
168 do
169 {
170 if (TO_ELAPSED(clk, timeout))
171 {
172 return ERR_WRITE_TIMEOUT;
173 }
174 } while (io_rd(com_in) & 0x20);
175
176 io_wr(com_out, 3);
177 TO_START(clk);
178 do
179 {
180 if (TO_ELAPSED(clk, timeout))
181 {
182 return ERR_WRITE_TIMEOUT;
183 }
184 } while ((io_rd(com_in) & 0x20) == 0x00);
185
186 return 0;
187 }
188
189 static int ser_put(CableHandle *h, uint8_t *data, uint32_t len)
190 {
191 int bit;
192 unsigned int i;
193 unsigned int j;
194 tiTIME clk;
195
196 for (j = 0; j < len; j++)
197 {
198 uint8_t curbyte = data[j];
199
200 for (bit = 0; bit < 8; bit++)
201 {
202 if (curbyte & 1)
203 {
204 io_wr(com_out, 2);
205 TO_START(clk);
206 do
207 {
208 if (TO_ELAPSED(clk, h->timeout))
209 {
210 return ERR_WRITE_TIMEOUT;
211 }
212 } while ((io_rd(com_in) & 0x10));
213
214 io_wr(com_out, 3);
215 TO_START(clk);
216 do
217 {
218 if (TO_ELAPSED(clk, h->timeout))
219 {
220 return ERR_WRITE_TIMEOUT;
221 }
222 } while ((io_rd(com_in) & 0x10) == 0x00);
223 }
224 else
225 {
226 io_wr(com_out, 1);
227 TO_START(clk);
228 do
229 {
230 if (TO_ELAPSED(clk, h->timeout))
231 {
232 return ERR_WRITE_TIMEOUT;
233 }
234 } while (io_rd(com_in) & 0x20);
235
236 io_wr(com_out, 3);
237 TO_START(clk);
238 do
239 {
240 if (TO_ELAPSED(clk, h->timeout))
241 {
242 return ERR_WRITE_TIMEOUT;
243 }
244 } while ((io_rd(com_in) & 0x20) == 0x00);
245 }
246
247 curbyte >>= 1;
248 for (i = 0; i < h->delay; i++)
249 {
250 io_rd(com_in);
251 }
252 }
253 }
254
255 return 0;
256 }
257
258 static int ser_get(CableHandle *h, uint8_t *data, uint32_t len)
259 {
260 int bit;
261 unsigned int i;
262 unsigned int j;
263 tiTIME clk;
264
265 for(j = 0; j < len; j++)
266 {
267 uint8_t v, curbyte = 0;
268
269 for (bit = 0; bit < 8; bit++)
270 {
271 TO_START(clk);
272 while ((v = io_rd(com_in) & 0x30) == 0x30)
273 {
274 if (TO_ELAPSED(clk, h->timeout))
275 {
276 return ERR_READ_TIMEOUT;
277 }
278 }
279
280 if (v == 0x10)
281 {
282 curbyte = (curbyte >> 1) | 0x80;
283 io_wr(com_out, 1);
284
285 TO_START(clk);
286 while ((io_rd(com_in) & 0x20) == 0x00)
287 {
288 if (TO_ELAPSED(clk, h->timeout))
289 {
290 return ERR_READ_TIMEOUT;
291 }
292 }
293 io_wr(com_out, 3);
294 }
295 else
296 {
297 curbyte = (curbyte >> 1) & 0x7F;
298 io_wr(com_out, 2);
299
300 TO_START(clk);
301 while ((io_rd(com_in) & 0x10) == 0x00)
302 {
303 if (TO_ELAPSED(clk, h->timeout))
304 {
305 return ERR_READ_TIMEOUT;
306 }
307 }
308 io_wr(com_out, 3);
309 }
310
311 for (i = 0; i < h->delay; i++)
312 {
313 io_rd(com_in);
314 }
315 }
316
317 data[j] = curbyte;
318 }
319
320 return 0;
321 }
322
323 static int ser_check(CableHandle *h, int *status)
324 {
325 *status = STATUS_NONE;
326
327 if (!((io_rd(com_in) & 0x30) == 0x30))
328 {
329 *status = STATUS_RX;
330 }
331
332 return 0;
333 }
334
335 #define swap_bits(a) (((a&2)>>1) | ((a&1)<<1)) // swap the 2 lowest bits
336
337 static int ser_set_red_wire(CableHandle *h, int b)
338 {
339 int v = swap_bits(io_rd(com_in) >> 4);
340
341 if (b)
342 {
343 io_wr(com_out, v | 0x02);
344 }
345 else
346 {
347 io_wr(com_out, v & ~0x02);
348 }
349
350 return 0;
351 }
352
353 static int ser_set_white_wire(CableHandle *h, int b)
354 {
355 int v = swap_bits(io_rd(com_in) >> 4);
356
357 if (b)
358 {
359 io_wr(com_out, v | 0x01);
360 }
361 else
362 {
363 io_wr(com_out, v & ~0x01);
364 }
365
366 return 0;
367 }
368
369 static int ser_get_red_wire(CableHandle *h)
370 {
371 return ((0x10 & io_rd(com_in)) ? 1 : 0);
372 }
373
374 static int ser_get_white_wire(CableHandle *h)
375 {
376 return ((0x20 & io_rd(com_in)) ? 1 : 0);
377 }
378
379 static int ser_set_raw(CableHandle *h, int state)
380 {
381 io_wr(com_out, swap_bits(state));
382 return 0;
383 }
384
385 static int ser_get_raw(CableHandle *h, int *state)
386 {
387 *state = (io_rd(com_in) >> 4) & 3;
388 return 0;
389 }
390
391 static int ser_set_device(CableHandle *h, const char * device)
392 {
393 if (device != NULL)
394 {
395 char * device2 = strdup(device);
396 if (device2 != NULL)
397 {
398 free(h->device);
399 h->device = device2;
400 }
401 else
402 {
403 ticables_warning(_("unable to set device %s.\n"), device);
404 }
405 return 0;
406 }
407 return ERR_ILLEGAL_ARG;
408 }
409
410 extern const CableFncts cable_ser =
411 {
412 CABLE_BLK,
413 "BLK",
414 N_("BlackLink"),
415 N_("BlackLink or home-made serial cable"),
416 !0,
417 &ser_prepare,
418 &ser_open, &ser_close, &ser_reset, &ser_probe, NULL,
419 &ser_put, &ser_get, &ser_check,
420 &ser_set_red_wire, &ser_set_white_wire,
421 &ser_get_red_wire, &ser_get_white_wire,
422 &ser_set_raw, &ser_get_raw,
423 &ser_set_device,
424 NULL
425 };