Root/tools/atspi-rssi/atspi-rssi.c

Source at commit 0463bbea5d737f4385ed09997cff6f6ee9232640 created 8 years 11 months ago.
By Werner Almesberger, Added graphical display of RSSI scan results.
1/*
2 * atspi-rssi/atspi-rssi.c - ben-wpan AT86RF230 spectrum scan
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 "atspi.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 atspi_dsc *dsc)
36{
37    int chan, rssi;
38    struct timeval t;
39
40    for (chan = 11; chan <= 26; chan++) {
41        atspi_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 = atspi_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 atspi_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 = atspi_open();
119    if (!dsc)
120        return 1;
121
122    atspi_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF);
123    atspi_reg_write(dsc, REG_TRX_STATE, TRX_CMD_RX_ON);
124    /*
125     * We'll wait for the PLL lock after selecting the channel.
126     */
127
128    if (graphical)
129        gui(dsc);
130    else {
131        gettimeofday(&t0, NULL);
132        for (i = 0; run && i != arg; i++)
133            sweep(dsc);
134    }
135
136    atspi_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF);
137
138    atspi_close(dsc);
139
140    return 0;
141}
142

Archive Download this file



interactive