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

Source at commit c2dd23840ce5875deca09673fcb8abb8dc3a8e7a created 9 years 1 month ago.
By Werner Almesberger, Moved fw/ and tools/ into atrf/ as well.
1/*
2 * atspi-rssi/atspi-rssi.c - ben-wpan AF86RF230 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 <usb.h>
17#include <sys/time.h>
18
19#include "at86rf230.h"
20#include "atspi/ep0.h"
21#include "atspi.h"
22
23
24#define FROM_DEV ATSPI_FROM_DEV(0)
25#define TO_DEV ATSPI_TO_DEV(0)
26
27
28static struct timeval t0;
29
30
31static void sweep(usb_dev_handle *dev)
32{
33    int chan, rssi;
34    struct timeval t;
35
36    for (chan = 11; chan <= 26; chan++) {
37        atspi_reg_write(dev, REG_PHY_CC_CCA, chan);
38        /*
39         * No need to explicitly wait for the PPL lock - going USB-SPI
40         * is pretty slow, leaving the transceiver plenty of time.
41         */
42        gettimeofday(&t, NULL);
43        rssi = atspi_reg_read(dev, REG_PHY_RSSI) & RSSI_MASK;
44        t.tv_sec -= t0.tv_sec;
45        t.tv_usec -= t0.tv_usec;
46        printf("%d %f %d\n",
47            2405+(chan-11)*5,
48            (double) t.tv_sec+t.tv_usec/1000000.0,
49            -91+3*(rssi-1));
50    }
51    printf("\n");
52}
53
54
55static void usage(const char *name)
56{
57    fprintf(stderr, "%s sweeps \n", name);
58    exit(1);
59}
60
61
62int main(int argc, const char **argv)
63{
64    usb_dev_handle *dev;
65    unsigned long sweeps, i;
66    char *end;
67
68    if (argc != 2)
69        usage(*argv);
70    sweeps = strtoul(argv[1], &end, 0);
71    if (*end)
72        usage(*argv);
73
74    dev = atspi_open();
75    if (!dev)
76        return 1;
77
78    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TRX_OFF);
79    /*
80     * No need to explicitly wait for things to stabilize - going USB-SPI
81     * is pretty slow, leaving the transceiver more than enough time.
82     */
83    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_RX_ON);
84
85    gettimeofday(&t0, NULL);
86    for (i = 0; i != sweeps; i++)
87        sweep(dev);
88
89    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TRX_OFF);
90
91    return 0;
92}
93

Archive Download this file



interactive