Root/tools/atrf-path/atrf-path.c

Source at commit e3463ef8a0153a5a551d7187190bdf1abe4ee71e created 9 years 5 months ago.
By Werner Almesberger, atrf-path.c: moved tx init out of sample loop (breaks 230 support)
1/*
2 * atrf-path/atrf-path.c - Measure path characteristics
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 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
18#include "at86rf230.h"
19
20#include "misctxrx.h"
21#include "cwtest.h"
22#include "atrf.h"
23
24#include "gui.h"
25#include "sweep.h"
26
27
28#define DEFAULT_TRIM 8
29#define DEFAULT_POWER 15
30
31
32static void set_channel(struct atrf_dsc *dsc, int chan)
33{
34    atrf_reg_write(dsc, REG_PHY_CC_CCA, (1 << CCA_MODE_SHIFT) | chan);
35}
36
37
38static void init_common(struct atrf_dsc *dsc, int trim)
39{
40    atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF);
41    atrf_reg_write(dsc, REG_XOSC_CTRL,
42        (XTAL_MODE_INT << XTAL_MODE_SHIFT) | trim);
43    atrf_set_clkm(dsc, 0);
44}
45
46
47static void init_tx(struct atrf_dsc *dsc, int trim, int power)
48{
49    init_common(dsc, trim);
50    set_power_step(dsc, power, 0);
51}
52
53
54static void init_rx(struct atrf_dsc *dsc, int trim)
55{
56    init_common(dsc, trim);
57    atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_RX_ON);
58}
59
60
61static double rssi_to_dBm(double rssi)
62{
63    return -91+3*(rssi-1);
64}
65
66
67static void sample(const struct sweep *sweep, int cont_tx,
68    struct sample *res)
69{
70    int i, rssi;
71    int sum = 0, min = -1, max = -1;
72    double offset = tx_power_step2dBm(sweep->tx, sweep->power);
73
74/*
75 * For the 230, we don't have reset-less exit from test mode (yet ?) and
76 * need to set up things from scratch:
77 *
78 * init_tx(sweep->tx, sweep->trim_tx, sweep->power);
79 * set_channel(sweep->tx, chan);
80 * usleep(155); / * table 7-2, tTR19 * /
81 */
82    cw_test_begin(sweep->tx, cont_tx);
83    /* table 7-1, tTR10, doubling since it's a "typical" value */
84    usleep(2*16);
85
86    for (i = 0; i != sweep->samples; i++) {
87        /* according to 8.3.2, PHY_RSSI is updated every 2 us */
88        usleep(2);
89
90        rssi = atrf_reg_read(sweep->rx, REG_PHY_RSSI) & RSSI_MASK;
91        sum += rssi;
92        if (min == -1 || rssi < min)
93            min = rssi;
94        if (rssi > max)
95            max = rssi;
96    }
97
98    cw_test_end(sweep->tx);
99
100    res->avg = rssi_to_dBm((double) sum/sweep->samples)-offset;
101    res->min = rssi_to_dBm(min)-offset;
102    res->max = rssi_to_dBm(max)-offset;
103}
104
105
106void do_sweep(const struct sweep *sweep, struct sample *res)
107{
108    int chan;
109
110    for (chan = 11; chan <= 26; chan++) {
111        set_channel(sweep->rx, chan);
112        set_channel(sweep->tx, chan);
113        usleep(155); /* table 7-2, tTR19 */
114
115        sample(sweep, CONT_TX_M500K, res++);
116        sample(sweep, CONT_TX_P500K, res++);
117    }
118}
119
120
121static void print_sweep(const struct sweep *sweep, const struct sample *res)
122{
123    int chan;
124
125    for (chan = 11; chan <= 26; chan++) {
126        printf("%.1f %.2f %.0f %.0f\n",
127            2350+5*chan-0.5, res->avg, res->min, res->max);
128        res++;
129        printf("%.1f %.2f %.0f %.0f\n",
130            2350+5*chan+0.5, res->avg, res->min, res->max);
131        res++;
132    }
133}
134
135
136static void do_sweeps(const struct sweep *sweep, int sweeps)
137{
138    struct sample res[16*2]; /* 16 channels, 2 offsets */
139    int i;
140
141    for (i = 0; i != sweeps; i++) {
142        if (i)
143            putchar('\n');
144        do_sweep(sweep, res);
145        print_sweep(sweep, res);
146    }
147}
148
149
150static void usage(const char *name)
151{
152    fprintf(stderr,
153"usage: %s [-p power] [-t trim_tx [-t trim_rx]] driver_tx[:arg]\n"
154"%16s driver_rx[:arg] [sweeps [samples]]\n"
155
156#ifdef HAVE_GFX
157"%6s %s -g [-p power] [-t trim_tx [-t trim_rx]] driver_tx[:arg]\n"
158"%16s driver_rx[:arg] [[sweeps] samples]\n"
159#endif
160    "\n"
161
162#ifdef HAVE_GFX
163" -g display results graphically\n"
164#endif
165" -p power transmit power, 0 to 15 (default %d)\n"
166" -t trim trim capacitor, 0 to 15 (default %d)\n"
167
168    , name, "",
169#ifdef HAVE_GFX
170    "", name, "",
171#endif
172    DEFAULT_POWER, DEFAULT_TRIM);
173
174    exit(1);
175}
176
177    
178int main(int argc, char **argv)
179{
180    const char *tx_drv, *rx_drv;
181    struct sweep sweep = {
182        .trim_tx = -1,
183        .trim_rx = DEFAULT_TRIM,
184        .samples = 1,
185    };
186    int graphical = 0;
187    int power = DEFAULT_POWER;
188    int sweeps = 1;
189    unsigned long tmp;
190    char *end;
191    int c;
192
193    while ((c = getopt(argc, argv, "gp:t:")) != EOF)
194        switch (c) {
195        case'g':
196            graphical = 1;
197            sweeps = 0;
198            break;
199        case 'p':
200            tmp = strtoul(optarg, &end, 0);
201            if (*end || tmp > 15)
202                usage(*argv);
203            power = tmp;
204            break;
205        case 't':
206            tmp = strtoul(optarg, &end, 0);
207            if (*end || tmp > 15)
208                usage(*argv);
209            if (sweep.trim_tx == -1)
210                sweep.trim_tx = tmp;
211            else
212                sweep.trim_rx = tmp;
213            break;
214        default:
215            usage(*argv);
216        }
217
218    if (sweep.trim_tx == -1)
219        sweep.trim_tx = DEFAULT_TRIM;
220
221    switch (argc-optind) {
222    case 4:
223        sweep.samples = strtoul(argv[optind+3], &end, 0);
224        if (*end)
225            usage(*argv);
226        /* fall through */
227    case 3:
228        sweeps = strtoul(argv[optind+2], &end, 0);
229        if (*end)
230            usage(*argv);
231        if (graphical && argc-optind == 3) {
232            sweep.samples = sweeps;
233            sweeps = 0;
234        }
235        /* fall through */
236    case 2:
237        tx_drv = argv[optind];
238        rx_drv = argv[optind+1];
239        break;
240    default:
241        usage(*argv);
242    }
243
244    sweep.tx = atrf_open(tx_drv);
245    if (!sweep.tx)
246        return 1;
247    sweep.rx = atrf_open(rx_drv);
248    if (!sweep.rx)
249        return 1;
250
251    sweep.power = 15-power;
252    init_rx(sweep.rx, sweep.trim_rx);
253    init_tx(sweep.tx, sweep.trim_tx, sweep.power);
254    if (graphical)
255        gui(&sweep, sweeps);
256    else
257        do_sweeps(&sweep, sweeps);
258
259    atrf_reg_write(sweep.tx, REG_TRX_STATE, TRX_CMD_TRX_OFF);
260    atrf_reg_write(sweep.rx, REG_TRX_STATE, TRX_CMD_TRX_OFF);
261
262    atrf_close(sweep.tx);
263    atrf_close(sweep.rx);
264
265    return 0;
266}
267

Archive Download this file



interactive