Root/tools/atrf-rssi/atrf-rssi.c

Source at commit 1fb05c221dcff657e49cc766e8ff7baa1d3434fc created 9 years 6 months ago.
By Werner Almesberger, misctxrx.c (wait_for_interrupt): let "ignore" control printing more tightly
1/*
2 * atrf-rssi/atrf-rssi.c - ben-wpan AT86RF230 spectrum scan
3 *
4 * Written 2010-2011 by Werner Almesberger
5 * Copyright 2010-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 <signal.h>
18#include <sys/time.h>
19
20#include "at86rf230.h"
21#include "atrf.h"
22#include "misctxrx.h"
23
24#ifdef HAVE_GFX
25#include "gui.h"
26#else
27#define gui(dsc) abort()
28#endif
29
30
31static struct timeval t0;
32static volatile int run = 1;
33
34
35static void sweep(struct atrf_dsc *dsc)
36{
37    int chan, rssi;
38    struct timeval t;
39
40    for (chan = 11; chan <= 26; chan++) {
41        atrf_reg_write(dsc, REG_PHY_CC_CCA, chan);
42        /* 150 us, according to AVR2001 section 3.5 */
43        wait_for_interrupt(dsc, IRQ_PLL_LOCK, IRQ_PLL_LOCK, 10, 20);
44
45        gettimeofday(&t, NULL);
46        rssi = atrf_reg_read(dsc, REG_PHY_RSSI) & RSSI_MASK;
47        t.tv_sec -= t0.tv_sec;
48        t.tv_usec -= t0.tv_usec;
49        printf("%d %f %d\n",
50            2405+(chan-11)*5,
51            (double) t.tv_sec+t.tv_usec/1000000.0,
52            -91+3*(rssi-1));
53    }
54    printf("\n");
55}
56
57
58static void die(int sig)
59{
60    run = 0;
61}
62
63
64static void usage(const char *name)
65{
66    fprintf(stderr,
67"usage: %s [-n] sweeps\n", name);
68
69#ifdef HAVE_GFX
70    fprintf(stderr,
71"%6s %s -g\n", "", name);
72#endif
73
74    exit(1);
75}
76
77
78int main(int argc, char **argv)
79{
80    struct atrf_dsc *dsc;
81    unsigned long arg = 0, i;
82    char *end;
83    int c;
84    int graphical = 0;
85
86    while ((c = getopt(argc, argv, "gn")) != EOF)
87        switch (c) {
88#ifdef HAVE_GFX
89        case 'g':
90            graphical = 1;
91
92            break;
93#endif
94        case 'n':
95            break;
96        default:
97            usage(*argv);
98        }
99
100    switch (argc-optind) {
101    case 0:
102        if (!graphical)
103            usage(*argv);
104        break;
105    case 1:
106        if (graphical)
107            usage(*argv);
108        arg = strtoul(argv[optind], &end, 0);
109        if (*end)
110            usage(*argv);
111        break;
112    default:
113        usage(*argv);
114    }
115
116    signal(SIGINT, die);
117
118    dsc = atrf_open();
119    if (!dsc)
120        return 1;
121
122    atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF);
123    atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_RX_ON);
124    atrf_reg_write(dsc, REG_IRQ_MASK, IRQ_PLL_LOCK);
125    /*
126     * We'll wait for the PLL lock after selecting the channel.
127     */
128
129    if (graphical)
130        gui(dsc);
131    else {
132        gettimeofday(&t0, NULL);
133        for (i = 0; run && i != arg; i++)
134            sweep(dsc);
135    }
136
137    atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF);
138
139    atrf_close(dsc);
140
141    return 0;
142}
143

Archive Download this file



interactive