File: reg_orion.c

package info (click to toggle)
gcpegg 5.1-10
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 328 kB
  • ctags: 446
  • sloc: ansic: 3,715; makefile: 96; sh: 32; csh: 21
file content (308 lines) | stat: -rw-r--r-- 7,071 bytes parent folder | download
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
/* PROGRAM:	eggsh
 * FILE:	$Header: /home/egg/src/RCS/reg_orion.c,v 1.1 1998/12/31 22:04:39 ghn Exp $
 * PURPOSE:	PEAR (Bradish box/micro-REG) hardware interface
 * AUTHOR:	Greg Nelson
 * DATE:	98-04-12
 *
 * REVISED:	$Log: reg_orion.c,v $
 * REVISED:	Revision 1.1  1998/12/31 22:04:39  ghn
 * REVISED:	Initial revision
 * REVISED:
 * REVISED:	Revision 1.3  1998/08/01 18:50:39  ghn
 * REVISED:     Added John's byte-order-independence changes and PSEUDO support.
 * REVISED:
 * REVISED:	Revision 1.2  1998/08/01 17:13:51  ghn
 * REVISED:     Added John's Solaris support and DUMPREG option.
 * REVISED:
 * REVISED:	Revision 1.1  1998/07/21 11:37:41  ghn
 * REVISED:	Initial revision
 * REVISED:
 * Copyright 1998 - Greg Nelson
 * Redistributable under the terms of the GNU Public Licence (GPL)
 */

/* Define this to dump samples from the REG which we actually use into
   a file named dumpreg.dat.  This can be used to debug serial port
   mode problems (for example, setting the character frame to 7 bits,
   or telling it to ignore breaks, which will lose all zero bytes. */
/* #define DUMPREG */

#include <stdio.h>
#ifdef __USE_BSD
#undef __USE_BSD
#endif
#define __USE_BSD
#include <termios.h>
#undef __USE_BSD
#include <unistd.h>
#include <fcntl.h>
#define __USE_BSD
#include <errno.h>
#undef __USE_BSD

#include "global.h"
#include "genlib.h"
#include "reg.h"

#define MAXDEV	20

/*  The Orion REG is powered by the serial port into which
    it is plugged.  If the port is not providing power to
    the REG before we initialise it, the REG may deliver
    garbage data as the power comes up.  To guard against
    this we take two precautions.  First of all, we sleep
    REG_INIT seconds after initialising the serial port to
    let the REG start up and stabilise after power is
    turned on, and we further discard REG_DISCARD initial
    samples which may have been queued as the REG was
    powering up.  If either of these symbols is not defined
    or zero, the corresponding start-up action will not
    be taken.  */

#define REG_INIT	10	      /* REG power on delay */
#define REG_DISCARD	1024	      /* Bytes to discard at start-up */

static int32 oldbits[MAXDEV], bitsleft[MAXDEV];

#ifdef DUMPREG
static FILE *dumpfile;		      /* REG dump file handle */
static unsigned char dumpbuf[1024];   /* REG dump buffer */
static int dumpptr = 0; 	      /* Pointer into dump buffer */
#endif

#ifdef FLUSH
extern int flush_fd;		      /* File descriptor for auto-flush */
#endif

static int32 OpenDev(DevOpts *opts) {
  char ttydev[30];
  speed_t baudcon;
  struct termios tt;
  int32 TTY_fd, res;

#ifdef DUMPREG
  dumpfile = fopen("dumpreg.dat", "w");
  setbuf(dumpfile, NULL);
  dumpptr = 0;
#endif

#ifdef Solaris
  /* Serial ports (at least built-in ones) have names of
     /dev/term/a, /dev/term/b, etc.  Map the port numbers
     from the RC file into this nomenclature, with "1"
     designating /dev/term/a. */
#ifdef Irix
  sprintf(ttydev, "/dev/ttyd%d", opts->port);
#else
  sprintf(ttydev, "/dev/term/%c", 'a' + (opts->port - 1));
#endif
#else
  sprintf(ttydev, "/dev/ttyS%d", opts->port);
#endif

  switch(opts->baud) {
  case 1200: baudcon = B1200; break;
  case 2400: baudcon = B2400; break;
  case 4800: baudcon = B4800; break;
  case 9600: baudcon = B9600; break;
  case 19200: baudcon = B19200; break;
  case 38400: baudcon = B38400; break;
#ifndef Irix
  case 115200: baudcon = B115200; break;
#endif
  default:
    printf("%s: Baud rate %ld not supported.\n", pgmname, opts->baud);
    return -1;
  }

  fprintf(stderr, "Opening %s at %ld\n", ttydev, opts->baud);
  if ((TTY_fd = open(ttydev, O_RDONLY
#ifdef CPU_BOUND
				    | O_NDELAY
#endif
      )) < 0) {
    perror(pgmname);
fprintf(stderr, "Error in open(%s)\n", ttydev);
    return -1;
  }

  if (TTY_fd >= MAXDEV) {
    fprintf(stderr, "%s: Too many devices open.\n", pgmname);
    close(TTY_fd);
    return -1;
  }

  res = tcgetattr(TTY_fd, &tt);
#ifdef Solaris
  tt.c_iflag = 0;
  tt.c_oflag = 0;
#ifdef SWITCH_READ
  tt.c_cflag = baudcon | CS8 | CLOCAL;
#else
  tt.c_cflag = baudcon | CS8 | CREAD | CLOCAL;
#endif
  tt.c_lflag = 0;
  tt.c_cc[VMIN] = 1;	/* This many chars satisfies reads */
  tt.c_cc[VTIME] = 0;	/* or in this many tenths of seconds */
#else
  res = cfsetospeed(&tt, baudcon); 
  cfmakeraw(&tt);
#if !defined(__FreeBSD_kernel__)
  tt.c_oflag &= (~(TABDLY | ONLCR));
#endif
#endif
  res = tcsetattr(TTY_fd, TCSANOW, &tt);

  oldbits[TTY_fd] = bitsleft[TTY_fd] = 0;

  /* Perform start-up power on delay and data flush. */

#ifdef REG_INIT
  if (REG_INIT > 0) {
    sleep(REG_INIT);
  }
#endif

#ifdef REG_DISCARD
  if (REG_DISCARD > 0) {
    int d, n1;
    char c1;

    for (d = 0; d < REG_DISCARD; d++) {
      do {
	n1 = read(TTY_fd, &c1, 1);
      } while (n1 == 0 || (n1 == -1 && errno == EAGAIN));
    }
  }
#endif

#ifdef FLUSH
  flush_fd = TTY_fd;
#endif

  return TTY_fd;
}

static int32 Sample(int32 dd, uint16 bits) {
  int32 bc, sum, n1;
  uint8 c1;

#ifdef Solaris
#ifdef SWITCH_READ
  struct termios tt;

  tcgetattr(dd, &tt);
  tt.c_cflag |= CREAD;
  tcsetattr(dd, TCSANOW, &tt);
#endif
#endif

  if (dd < 0) return -1;

  sum = bc = 0;
  while (bc < bits) {
    if (bitsleft[dd]) {
      sum += (oldbits[dd] & 0x01);
      oldbits[dd] >>= 1;
      bitsleft[dd]--;
      bc++;
    } else {
      do {
	n1 = read(dd, &c1, 1);
      } while (n1 == 0 || (n1 == -1 && errno == EAGAIN));
      if (n1 == -1) {
	/* Fatal error occurred, die now? */
	return -1;
      }

      /* Orion REG does not compensate for first order bias.  XOR
	 each sample with 01010101b to invert the sense of
	 alternate bits in the samples. */

      c1 ^= 0x55;
#ifdef DUMPREG
      dumpbuf[dumpptr++] = c1;
      if (dumpptr >= sizeof(dumpbuf)) {
	fwrite(dumpbuf, sizeof(dumpbuf), 1, dumpfile);
	dumpptr = 0;
      }
#endif
      oldbits[dd] = c1;
      bitsleft[dd] = 8;
    }
  }

#ifdef Solaris
#ifdef SWITCH_READ
  tcgetattr(dd, &tt);
  tt.c_cflag &= ~CREAD;
  tcsetattr(dd, TCSANOW, &tt);
#endif
#endif
  return sum;
}

#define SAMP_PERIOD	1000	/* msec */
#define MARGIN		.95	/* how much to headroom to allow in
				   speed measurement */

static int32 EvalSpeed(int32 dd) {
  struct timeval start, end;
  int32 bitct, samp;

  if (dd < 0) return -1;

  gettimeofday(&start, NULL);
  bitct = 0;
  while (1) {
    gettimeofday(&end, NULL);
    if (deltams(&end, &start) >= SAMP_PERIOD) break;
    samp = Sample(dd, 1);
    bitct++;
  }

  return (int32)(bitct * MARGIN);
}

static int32 Discard(int32 dd) {
  int32 disc;
#ifdef CPU_BOUND
  int32  n1;
  uint8 c1;
#endif
  
  if (dd < 0) return -1;

  disc = bitsleft[dd];
  bitsleft[dd] = 0;

#ifdef CPU_BOUND
  do {
    n1 = read(dd, &c1, 1);
    if (n1 == 1) disc++;
  } while (n1 == 1);
#else
  tcflush(dd, TCIFLUSH);
#endif

  return disc;
}

static int32 CloseDev(int32 dd) {
  if (dd < 0) return -1;

  close(dd);
  return 0;
}

/*  Driver description table.  */

REG_driver REG_orion = {
                        "ORION", 
			1000,
			OpenDev,
			EvalSpeed,
			Sample,
			Discard,
			CloseDev
		      };