Root/tools/atrf-xtal/atrf-xtal.c

Source at commit 49e7c83796bc04941e9dbcec69bc0751563ff4d4 created 2 years 8 months ago.
By Werner Almesberger, atusb/: use ""VDD" symbol from kicad-libs
1/*
2 * atrf-xtal/atrf-xtal.c - AT86RF230/1 crystal diagnostic utility
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
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
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <unistd.h>
17#include <string.h>
18
19#include "atrf.h"
20
21#include "atrf-xtal.h"
22
23
24#define DEFAULT_SIZE 127
25#define DEFAULT_TRIM 8
26#define DEFAULT_SAMPLES 1000
27
28
29static void atben(struct atrf_dsc *dsc, int size, int trim, int rep,
30    int dump_raw, double base, double ppm)
31{
32#ifdef HAVE_ATBEN
33    do_atben(dsc, size, trim, rep, dump_raw, base, ppm);
34#else
35    fprintf(stderr, "not compiled with ATBEN support\n");
36    exit(1);
37#endif
38}
39
40
41static void atusb(struct atrf_dsc *dsc, int trim, int dump_raw, double ppm,
42    int sequence)
43{
44#ifdef HAVE_ATUSB
45    do_atusb(dsc, trim, dump_raw, ppm, sequence);
46#else
47    fprintf(stderr, "not compiled with ATUSB support\n");
48    exit(1);
49#endif
50}
51
52
53static void usage(const char *name)
54{
55    fprintf(stderr,
56"usage: %s [-b count [-p ppm]] [-d driver[:arg]] [-r] [-s size] [-t trim]\n"
57" %*s [repetitions]\n"
58" %s [-d driver[:arg]] [-p ppm] [-r] [-t trim] [samples]\n\n"
59"atben arguments:\n"
60" -b count base count for relative result\n"
61" -s size payload size in bytes, 0-127 (default: %d bytes)\n"
62" repetitions number of measurements (default: 1)\n\n"
63"atusb arguments:\n"
64" samples number of samples (default: %d)\n\n"
65"common options\n"
66" -d driver[:arg] use the specified driver (default: %s)\n"
67" -p ppm maximum deviation from base count\n"
68" -r instead of printing a mean value, dump the raw samples\n"
69" -t trim trim capacitor setting, 0-15 (default: %d)\n"
70    , name, (int) strlen(name), "", name,
71    DEFAULT_SIZE, DEFAULT_SAMPLES, atrf_default_driver_name(), DEFAULT_TRIM);
72    exit(1);
73}
74
75
76int main(int argc, char *const *argv)
77{
78    const char *driver = NULL;
79    struct atrf_dsc *dsc;
80    int size = DEFAULT_SIZE;
81    int trim = DEFAULT_TRIM;
82    double base = 0, ppm = 0;
83    int n = 0;
84    int dump_raw = 0;
85    char *end;
86    int c;
87
88    while ((c = getopt(argc, argv, "b:d:p:rs:t:")) != EOF)
89        switch (c) {
90        case 'b':
91            base = strtof(optarg, &end);
92            if (*end)
93                usage(*argv);
94            break;
95        case 'd':
96            driver = optarg;
97            break;
98        case 'p':
99            ppm = strtof(optarg, &end);
100            if (*end)
101                usage(*argv);
102            break;
103        case 'r':
104            dump_raw = 1;
105            break;
106        case 's':
107            size = strtoul(optarg, &end, 0);
108            if (*end)
109                usage(*argv);
110            if (size > 127)
111                usage(*argv);
112            break;
113        case 't':
114            trim = strtoul(optarg, &end, 0);
115            if (*end)
116                usage(*argv);
117            if (trim > 15)
118                usage(*argv);
119            break;
120        default:
121            usage(*argv);
122        }
123
124#if 0
125    if (ppm && !base)
126        usage(*argv);
127#endif
128
129    switch (argc-optind) {
130    case 0:
131        break;
132    case 1:
133        n = strtoul(argv[optind], &end, 0);
134        if (*end)
135            usage(*argv);
136        break;
137    default:
138        usage(*argv);
139    }
140
141    dsc = atrf_open(driver);
142    if (!dsc)
143        return 1;
144
145    if (atrf_usb_handle(dsc))
146        atusb(dsc, trim, dump_raw, ppm, n ? n : DEFAULT_SAMPLES);
147    else
148        atben(dsc, size, trim, n ? n : 1, dump_raw, base, ppm);
149
150    return 0;
151}
152

Archive Download this file



interactive