Date:2010-11-05 02:35:24 (8 years 10 months ago)
Author:Werner Almesberger
Commit:df1706fe747cecffd2b417e654c42891d9367682
Message:Added burst counter mode to cntr tool, plus assorted small improvements.

- tools/cntr/cntr.c (measure): also stop if ^C is pressed while we're
unsuccessfully trying to communicate with the device
- tools/cntr/cntr.c (main): report a usage error if an argument is given to
-i or -r
- tools/cntr/cntr.c (arm_stop, measure): moveing open-coded arming of the
SIGINT handler to separate function
- tools/cntr/cntr.c (count_bursts, usage, main): added burst counter mode
(cntr -b [timeout_s])
Files: cntr/tools/cntr/cntr.c (9 diffs)

Change Details

cntr/tools/cntr/cntr.c
8989}
9090
9191
92/* ----- measurements ------------------------------------------------------ */
92/* ---- packet reception --------------------------------------------------- */
9393
9494
9595struct sample {
...... 
9999
100100
101101static unsigned packets = 0, crc_errors = 0, inv_errors = 0;
102static volatile int stop = 0;
103
104
105static void set_stop(int sig)
106{
107    stop = 1;
108}
109102
110103
111104static int get_sample(usb_dev_handle *dev, struct sample *s)
...... 
163156}
164157
165158
159/* ---- SIGINT (Ctrl-C) ---------------------------------------------------- */
160
161
162static volatile int stop = 0;
163
164
165static void set_stop(int sig)
166{
167    stop = 1;
168}
169
170
171static void arm_stop(void)
172{
173    signal(SIGINT, set_stop);
174}
175
176
177/* ---- burst counter ------------------------------------------------------ */
178
179
180static void count_bursts(usb_dev_handle *dev, double timeout)
181{
182    struct sample start, stable, now;
183
184    arm_stop();
185
186    while (!get_sample(dev, &start));
187    stable = start;
188    while (!stop) {
189        while (!get_sample(dev, &now))
190            if (stop)
191                break;
192        if (stable.cntr != now.cntr) {
193            stable = now;
194            continue;
195        }
196        if (now.t0-stable.t1 < timeout)
197            continue;
198        if (now.cntr == start.cntr)
199            continue;
200        printf("%llu\n", (unsigned long long) now.cntr-start.cntr);
201        start = now;
202    }
203}
204
205
206/* ----- measurements ------------------------------------------------------ */
207
208
166209static void measure(usb_dev_handle *dev, double clock_dev_s, double error_goal)
167210{
168211    struct sample start, now;
...... 
171214    char *f_exp, *error_exp;
172215    int i;
173216
174    signal(SIGINT, set_stop);
217    arm_stop();
175218
176219    /*
177220     * The round-trip time for getting the first sample is one of the
...... 
190233    }
191234    while (!stop) {
192235        usleep(100000);
193        while (!get_sample(dev, &now));
236        while (!get_sample(dev, &now))
237            if (stop)
238                break;
194239        dc = now.cntr-start.cntr;
195240        dt = (now.t0+now.t1)/2.0-(start.t0+start.t1)/2.0;
196241        f = dc/dt;
...... 
248293{
249294    fprintf(stderr,
250295"usage: %s [-c clock_dev] [-d] [-v] [accuracy_ppm]\n"
296"%6s %s -b [-d] [-v] [timeout_s]\n"
251297"%6s %s -i\n"
252298"%6s %s -r\n\n"
253299" accuracy_ppm stop when specified accuracy is reached (default: never\n"
254300" stop)\n"
301" timeout_s silence period between bursts, in seconds (default: 1s )\n"
302" -b count bursts separated by silence periods\n"
255303" -c clock_dev maximum deviation of the system clock, in seconds\n"
256304" (default: %g s)\n"
257305" -d debug mode. Print counter values.\n"
258306" -i identify the CNTR board\n"
259307" -r reset the CNTR board\n"
260308" -v verbose reporting of communication errors\n"
261    , name, "", name, "", name, DEFAULT_CLOCK_DEV_S);
309    , name, "", name, "", name, "", name, DEFAULT_CLOCK_DEV_S);
262310    exit(1);
263311}
264312
...... 
266314int main(int argc, char *const *argv)
267315{
268316    usb_dev_handle *dev;
269    int c, identify = 0, reset = 0;
317    int c, burst = 0, identify = 0, reset = 0;
270318    double clock_dev_s = DEFAULT_CLOCK_DEV_S;
271319    double error_goal = 0;
272320    char *end;
273321
274    while ((c = getopt(argc, argv, "c:dir")) != EOF)
322    while ((c = getopt(argc, argv, "bc:dir")) != EOF)
275323        switch (c) {
324        case 'b':
325            burst = 1;
326            break;
276327        case 'c':
277328            clock_dev_s = strtod(argv[optind], &end);
278329            if (*end)
...... 
290341        default:
291342            usage(*argv);
292343        }
293    if (identify && reset)
344    if (burst+identify+reset > 1)
294345        usage(*argv);
295346
296347    switch (argc-optind) {
297348    case 0:
298349        break;
299350    case 1:
351        if (identify || reset)
352            usage(*argv);
300353        error_goal = strtod(argv[optind], &end)/1000000.0;
301354        if (*end)
302355            usage(*argv);
...... 
321374        return 0;
322375    }
323376
377    if (burst) {
378        count_bursts(dev, error_goal ? error_goal : 1);
379        return 0;
380    }
381
324382    measure(dev, clock_dev_s, error_goal);
325383
326384    return 0;

Archive Download the corresponding diff file



interactive