2 * FILE: $Header: /home/egg/src/RCS/hw_pear.c,v 1.3 1998/08/01 18:50:39 ghn Exp $
3 * PURPOSE: PEAR (Bradish box/micro-REG) hardware interface
7 * REVISED: $Log: hw_pear.c,v $
8 * REVISED: Revision 1.3 1998/08/01 18:50:39 ghn
9 * REVISED: Added John's byte-order-independence changes and PSEUDO support.
11 * REVISED: Revision 1.2 1998/08/01 17:13:51 ghn
12 * REVISED: Added John's Solaris support and DUMPREG option.
14 * REVISED: Revision 1.1 1998/07/21 11:37:41 ghn
15 * REVISED: Initial revision
17 * Copyright 1998 - Greg Nelson
18 * Redistributable under the terms of the GNU Public Licence (GPL)
21 /* Define this to dump samples from the REG which we actually use into
22 a file named dumpreg.dat. This can be used to debug serial port
23 mode problems (for example, setting the character frame to 7 bits,
24 or telling it to ignore breaks, which will lose all zero bytes. */
43 int32 oldbits[MAXDEV], bitsleft[MAXDEV];
46 static FILE *dumpfile; /* REG dump file handle */
47 static unsigned char dumpbuf[1024]; /* REG dump buffer */
48 static int dumpptr = 0; /* Pointer into dump buffer */
51 #ifdef PSEUDORANDOM_TEST
53 static int pseudo = FALSE; /* Nonzero if using pseudo-REG */
56 int32 OpenDev(DevOpts *opts) {
63 dumpfile = fopen("dumpreg.dat", "w");
64 setbuf(dumpfile, NULL);
68 #ifdef PSEUDORANDOM_TEST
69 if (opts->type == 0xFF) {
71 oldbits[TTY_fd] = bitsleft[TTY_fd] = 0;
74 fprintf(stderr, "**** WARNING! This egg has been configured to\n\
75 use a pseudorandom sequence generator to\n\
76 permit testing on machines not equipped\n\
77 with an REG. Data from this egg should not\n\
78 be used for analysis.\n");
84 /* Serial ports (at least built-in ones) have names of
85 /dev/term/a, /dev/term/b, etc. Map the port numbers
86 from the RC file into this nomenclature, with "1"
87 designating /dev/term/a. */
88 sprintf(ttydev, "/dev/term/%c", 'a' + (opts->port - 1));
90 sprintf(ttydev, "/dev/REG", opts->port);
94 case 1200: baudcon = B1200; break;
95 case 2400: baudcon = B2400; break;
96 case 4800: baudcon = B4800; break;
97 case 9600: baudcon = B9600; break;
98 case 19200: baudcon = B19200; break;
99 case 38400: baudcon = B38400; break;
101 case 115200: baudcon = B115200; break;
104 printf("%s: Baud rate %ld not supported.\n", pgmname, opts->baud);
108 fprintf(stderr, "Opening %s at %ld\n", ttydev, opts->baud);
109 if ((TTY_fd = open(ttydev, O_RDWR | O_NDELAY)) < 0) {
114 if (TTY_fd >= MAXDEV) {
115 fprintf(stderr, "%s: Too many devices open.\n", pgmname);
120 res = tcgetattr(TTY_fd, &tt);
124 tt.c_cflag = baudcon | CS8 | CREAD | CLOCAL;
126 tt.c_cc[VMIN] = 1; /* This many chars satisfies reads */
127 tt.c_cc[VTIME] = 0; /* or in this many tenths of seconds */
129 res = cfsetospeed(&tt, baudcon);
131 tt.c_oflag &= (~(TABDLY | ONLCR));
133 res = tcsetattr(TTY_fd, TCSANOW, &tt);
135 oldbits[TTY_fd] = bitsleft[TTY_fd] = 0;
139 #define SAMP_PERIOD 1000 /* msec */
140 #define MARGIN .95 /* how much to headroom to allow in
143 int32 EvalSpeed(int32 dd) {
144 struct timeval start, end;
147 if (dd < 0) return -1;
149 #ifdef PSEUDORANDOM_TEST
155 gettimeofday(&start, NULL);
158 gettimeofday(&end, NULL);
159 if (deltams(&end, &start) >= SAMP_PERIOD) break;
160 samp = Sample(dd, 1);
164 return (int32)(bitct * MARGIN);
167 int32 Sample(int32 dd, uint16 bits) {
171 if (dd < 0) return -1;
176 sum += (oldbits[dd] & 0x01);
182 #ifdef PSEUDORANDOM_TEST
185 c1 = (uint8) ((random() >> 5) & 0xFF);
188 n1 = read(dd, &c1, 1);
189 } while (n1 == 0 || (n1 == -1 && errno == EAGAIN));
191 /* Fatal error occurred, die now? */
195 dumpbuf[dumpptr++] = c1;
196 if (dumpptr >= sizeof(dumpbuf)) {
197 fwrite(dumpbuf, sizeof(dumpbuf), 1, dumpfile);
209 int32 Discard(int32 dd) {
213 if (dd < 0) return -1;
218 #ifdef PSEUDORANDOM_TEST
225 n1 = read(dd, &c1, 1);
232 int32 CloseDev(int32 dd) {
233 if (dd < 0) return -1;
235 #ifdef PSEUDORANDOM_TEST