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

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#include "gui.h"
25
26
27static struct timeval t0;
28static volatile int run = 1;
29
30
31static void sweep(struct atrf_dsc *dsc)
32{
33    int chan, rssi;
34    struct timeval t;
35
36    for (chan = 11; chan <= 26; chan++) {
37        atrf_reg_write(dsc, REG_PHY_CC_CCA, chan);
38        /* 150 us, according to AVR2001 section 3.5 */
39        wait_for_interrupt(dsc, IRQ_PLL_LOCK, IRQ_PLL_LOCK, 1);
40
41        gettimeofday(&t, NULL);
42        rssi = atrf_reg_read(dsc, REG_PHY_RSSI) & RSSI_MASK;
43        t.tv_sec -= t0.tv_sec;
44        t.tv_usec -= t0.tv_usec;
45        printf("%d %f %d\n",
46            2405+(chan-11)*5,
47            (double) t.tv_sec+t.tv_usec/1000000.0,
48            -91+3*(rssi-1));
49    }
50    printf("\n");
51}
52
53
54static void die(int sig)
55{
56    run = 0;
57}
58
59
60static void usage(const char *name)
61{
62    fprintf(stderr,
63"usage: %s [-d driver[:arg]] [-n] sweeps\n", name);
64
65#ifdef HAVE_GFX
66    fprintf(stderr,
67"%6s %s [-d driver[:arg]] -g\n", "", name);
68#endif
69
70    exit(1);
71}
72
73
74int main(int argc, char **argv)
75{
76    const char *driver = NULL;
77    struct atrf_dsc *dsc;
78    unsigned long arg = 0, i;
79    char *end;
80    int c;
81    int graphical = 0;
82
83    while ((c = getopt(argc, argv, "d:gn")) != EOF)
84        switch (c) {
85        case 'd':
86            driver = optarg;
87            break;
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(driver);
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