hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/**
 * Analogy for Linux, instruction read test program
 *
 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
 * Copyright (C) 2008 Alexis Berlemont <alexis.berlemont@free.fr>
 *
 * Xenomai is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Xenomai is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Xenomai; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <getopt.h>
#include <rtdm/analogy.h>
 
#define FILENAME "analogy0"
#define BUF_SIZE 10000
#define SCAN_CNT 10
 
static unsigned char buf[BUF_SIZE];
static char *filename = FILENAME;
static int verbose;
static int idx_subd = -1;
static int idx_chan;
static int idx_rng = -1;
static unsigned int scan_size = SCAN_CNT;
static char *calibration_file = NULL;
 
struct option insn_read_opts[] = {
   {"verbose", no_argument, NULL, 'v'},
   {"device", required_argument, NULL, 'd'},
   {"subdevice", required_argument, NULL, 's'},
   {"scan-count", required_argument, NULL, 'S'},
   {"channel", required_argument, NULL, 'c'},
   {"range", required_argument, NULL, 'R'},
   {"cal", required_argument, NULL, 'y'},
   {"raw", no_argument, NULL, 'w'},
   {"help", no_argument, NULL, 'h'},
   {0},
};
 
static void do_print_usage(void)
{
   fprintf(stdout, "usage:\tinsn_read [OPTS]\n");
   fprintf(stdout, "\tOPTS:\t -v, --verbose: verbose output\n");
   fprintf(stdout,
       "\t\t -d, --device: device filename (analogy0, analogy1, ...)\n");
   fprintf(stdout, "\t\t -s, --subdevice: subdevice index\n");
   fprintf(stdout, "\t\t -S, --scan-count: count of scan to perform\n");
   fprintf(stdout, "\t\t -c, --channel: channel to use\n");
   fprintf(stdout, "\t\t -R, --range: range to use\n");
   fprintf(stdout, "\t\t -w, --raw: dump data in raw format\n");
   fprintf(stdout, "\t\t -y, --cal: /path/to/calibration.bin \n");
   fprintf(stdout, "\t\t -h, --help: print this help\n");
}
 
static int dump_raw(a4l_desc_t *dsc, unsigned char *buf, int size)
{
   return fwrite(buf, size, 1, stdout);
}
 
static int dump_text(a4l_desc_t *dsc, unsigned char *buf, int size)
{
   int err = 0, width, tmp_size = 0;
   char *fmt;
   a4l_chinfo_t *chan;
 
   /* Retrieve the subdevice data size */
   err = a4l_get_chinfo(dsc, idx_subd, idx_chan, &chan);
   if (err < 0) {
       fprintf(stderr,
           "insn_read: info for channel %d "
           "on subdevice %d not available (err=%d)\n",
           idx_chan, idx_subd, err);
       goto out;
   }
 
   width = a4l_sizeof_chan(chan);
   if (width < 0) {
       fprintf(stderr,
           "insn_read: incoherent info for channel %d\n",
           idx_chan);
       err = width;
       goto out;
   }
 
   switch(width) {
   case 1:
       fmt = "0x%02x\n";
       break;
   case 2:
       fmt = "0x%04x\n";
       break;
   case 4:
   default:
       fmt = "0x%08x\n";
       break;
   }
 
   while (size - tmp_size > 0) {
       unsigned long values[64];
       int i, tmp_cnt = ((size - tmp_size) / width > 64) ?
           64 : ((size - tmp_size) / width);
 
       err = a4l_rawtoul(chan, values, buf + tmp_size, tmp_cnt);
       if (err < 0)
           goto out;
 
       for (i = 0; i < tmp_cnt; i++)
           fprintf(stdout, fmt, values[i]);
 
       tmp_size += tmp_cnt * width;
   }
 
out:
   return err;
}
 
static int dump_converted(a4l_desc_t *dsc, unsigned char *buf, int size)
{
   int err = 0, width, tmp_size = 0;
   a4l_chinfo_t *chan;
   a4l_rnginfo_t *rng;
 
   /* Retrieve the channel info */
   err = a4l_get_chinfo(dsc, idx_subd, idx_chan, &chan);
   if (err < 0) {
       fprintf(stderr,
           "insn_read: info for channel %d "
           "on subdevice %d not available (err=%d)\n",
           idx_chan, idx_subd, err);
       goto out;
   }
 
   /* Retrieve the range info */
   err = a4l_get_rnginfo(dsc, idx_subd, idx_chan, idx_rng, &rng);
   if (err < 0) {
       fprintf(stderr,
           "insn_read: failed to recover range descriptor\n");
       goto out;
   }
 
   width = a4l_sizeof_chan(chan);
   if (width < 0) {
       fprintf(stderr,
           "insn_read: incoherent info for channel %d\n",
           idx_chan);
       err = width;
       goto out;
   }
 
   fprintf(stdout, "Non Calibrated values: \n");
   while (size - tmp_size > 0) {
       double values[64];
       int i, tmp_cnt = ((size - tmp_size) / width > 64) ?
           64 : ((size - tmp_size) / width);
 
       err = a4l_rawtod(chan, rng, values, buf + tmp_size, tmp_cnt);
       if (err < 0)
           goto out;
 
       for (i = 0; i < tmp_cnt; i++) {
           fprintf(stdout, "%F\n", values[i]);
       }
 
       tmp_size += tmp_cnt * width;
   }
 
out:
   return err;
}
 
static int dump_calibrated(a4l_desc_t *dsc, unsigned char *buf, int size)
{
   struct a4l_calibration_data cal_info;
   struct a4l_polynomial converter;
   int err = 0, width, tmp_size = 0;
   a4l_chinfo_t *chan;
   a4l_rnginfo_t *rng;
 
 
   /* Retrieve the channel info */
   err = a4l_get_chinfo(dsc, idx_subd, idx_chan, &chan);
   if (err < 0) {
       fprintf(stderr,
           "insn_read: info for channel %d "
           "on subdevice %d not available (err=%d)\n",
           idx_chan, idx_subd, err);
       goto out;
   }
 
   /* Retrieve the range info */
   err = a4l_get_rnginfo(dsc, idx_subd, idx_chan, idx_rng, &rng);
   if (err < 0) {
       fprintf(stderr,
           "insn_read: failed to recover range descriptor\n");
       goto out;
   }
 
   width = a4l_sizeof_chan(chan);
   if (width < 0) {
       fprintf(stderr,
           "insn_read: incoherent info for channel %d\n",
           idx_chan);
       err = width;
       goto out;
   }
 
   err = a4l_read_calibration_file(calibration_file, &cal_info);
   if (err < 0) {
       fprintf(stderr,
           "insn_read: error reading the calibration file \n");
       goto out;
   }
 
   err = a4l_get_softcal_converter(&converter, idx_subd, idx_chan, idx_rng,
                               &cal_info);
   if (err < 0) {
       fprintf(stderr,
           "insn_read: failed to get the softcal converter  \n");
       goto out;
   }
 
   fprintf(stdout, "Calibrated values: \n");
   while (size - tmp_size > 0) {
       double values[64];
       int i, tmp_cnt = ((size - tmp_size) / width > 64) ?
           64 : ((size - tmp_size) / width);
 
       err = a4l_rawtodcal(chan, values, buf + tmp_size, tmp_cnt,
                       &converter);
       if (err < 0)
           goto out;
 
       for (i = 0; i < tmp_cnt; i++)
           fprintf(stdout, "%F\n", values[i]);
 
       tmp_size += tmp_cnt * width;
   }
 
out:
   return err;
}
 
int main(int argc, char *argv[])
{
   int err = 0;
   unsigned int cnt = 0;
   a4l_desc_t dsc = { .sbdata = NULL };
   a4l_sbinfo_t *sbinfo;
   a4l_chinfo_t *chinfo;
   a4l_rnginfo_t *rnginfo;
 
   int (*dump_function) (a4l_desc_t *, unsigned char *, int) = dump_text;
 
   /* Compute arguments */
   while ((err = getopt_long(argc,
                 argv,
                 "vrd:s:S:c:R:y:wh", insn_read_opts,
                 NULL)) >= 0) {
       switch (err) {
       case 'v':
           verbose = 1;
           break;
       case 'd':
           filename = optarg;
           break;
       case 's':
           idx_subd = strtoul(optarg, NULL, 0);
           break;
       case 'S':
           scan_size = strtoul(optarg, NULL, 0);
           break;
       case 'c':
           idx_chan = strtoul(optarg, NULL, 0);
           break;
       case 'R':
           idx_rng = strtoul(optarg, NULL, 0);
           dump_function = dump_converted;
           break;
       case 'w':
           dump_function = dump_raw;
           break;
       case 'y':
           dump_function = dump_calibrated;
           calibration_file = optarg;
           break;
       case 'h':
       default:
           do_print_usage();
           return 0;
       }
   }
 
   if (isatty(STDOUT_FILENO) && dump_function == dump_raw) {
       fprintf(stderr,
           "insn_read: cannot dump raw data on a terminal\n\n");
       return -EINVAL;
   }
 
   /* Open the device */
   err = a4l_open(&dsc, filename);
   if (err < 0) {
       fprintf(stderr,
           "insn_read: a4l_open %s failed (err=%d)\n",
           filename, err);
       return err;
   }
 
   if (verbose != 0) {
       printf("insn_read: device %s opened (fd=%d)\n", filename,
              dsc.fd);
       printf("insn_read: basic descriptor retrieved\n");
       printf("\t subdevices count = %d\n", dsc.nb_subd);
       printf("\t read subdevice index = %d\n", dsc.idx_read_subd);
       printf("\t write subdevice index = %d\n", dsc.idx_write_subd);
   }
 
   /* Allocate a buffer so as to get more info (subd, chan, rng) */
   dsc.sbdata = malloc(dsc.sbsize);
   if (dsc.sbdata == NULL) {
       err = -ENOMEM;
       fprintf(stderr, "insn_read: info buffer allocation failed\n");
       goto out_insn_read;
   }
 
   /* Get this data */
   err = a4l_fill_desc(&dsc);
   if (err < 0) {
       fprintf(stderr, "insn_read: a4l_fill_desc failed (err=%d)\n",
           err);
       goto out_insn_read;
   }
 
   if (verbose != 0)
       printf("insn_read: complex descriptor retrieved\n");
 
   /* If no subdevice index was set, look for an analog input
      subdevice */
   if (idx_subd == -1)
       idx_subd = dsc.idx_read_subd;
 
   if (idx_subd == -1) {
       fprintf(stderr,
           "insn_read: no analog input subdevice available\n");
       err = -EINVAL;
       goto  out_insn_read;
   }
 
   if (verbose != 0)
       printf("insn_read: selected subdevice index = %d\n", idx_subd);
 
   /* We must check that the subdevice is really an AI one
      (in case, the subdevice index was set with the option -s) */
   err = a4l_get_subdinfo(&dsc, idx_subd, &sbinfo);
   if (err < 0) {
       fprintf(stderr,
           "insn_read: get_sbinfo(%d) failed (err = %d)\n",
           idx_subd, err);
       err = -EINVAL;
       goto out_insn_read;
   }
 
   if ((sbinfo->flags & A4L_SUBD_TYPES) != A4L_SUBD_AI) {
       fprintf(stderr,
           "insn_read: wrong subdevice selected "
           "(not an analog input)\n");
       err = -EINVAL;
       goto out_insn_read;
   }
 
   if (idx_rng >= 0) {
 
       err = a4l_get_rnginfo(&dsc,
                     idx_subd, idx_chan, idx_rng, &rnginfo);
       if (err < 0) {
           fprintf(stderr,
               "insn_read: failed to recover range descriptor\n");
           goto out_insn_read;
       }
 
       if (verbose != 0) {
           printf("insn_read: range descriptor retrieved\n");
           printf("\t min = %ld\n", rnginfo->min);
           printf("\t max = %ld\n", rnginfo->max);
       }
   }
 
   /* Retrieve the subdevice data size */
   err = a4l_get_chinfo(&dsc, idx_subd, idx_chan, &chinfo);
   if (err < 0) {
       fprintf(stderr,
           "insn_read: info for channel %d on subdevice %d not available (err=%d)\n",
           idx_chan, idx_subd, err);
       goto out_insn_read;
   }
 
   /* Set the data size to read */
   scan_size *= a4l_sizeof_chan(chinfo);
 
   if (verbose != 0) {
       printf("insn_read: channel width is %u bits\n",
              chinfo->nb_bits);
       printf("insn_read: global scan size is %u\n", scan_size);
   }
 
   while (cnt < scan_size) {
       int tmp = (scan_size - cnt) < BUF_SIZE ?
           (scan_size - cnt) : BUF_SIZE;
 
       /* Perform the synchronous read */
       err = a4l_sync_read(&dsc,
                   idx_subd, CHAN(idx_chan), 0, buf, tmp);
 
       if (err < 0) {
           fprintf(stderr,
               "insn_read: a4l_sync_read failed (err=%d)\n",
               err);
           goto out_insn_read;
       }
 
       /* Dump the read data */
       tmp = dump_function(&dsc, buf, err);
       if (tmp < 0) {
           err = tmp;
           goto out_insn_read;
       }
 
       /* Update the count */
       cnt += err;
   }
 
   if (verbose != 0)
       printf("insn_read: %u bytes successfully received\n", cnt);
 
   err = 0;
 
out_insn_read:
 
   /* Free the information buffer */
   if (dsc.sbdata != NULL)
       free(dsc.sbdata);
 
   /* Release the file descriptor */
   a4l_close(&dsc);
 
   return err;
}