IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
Source at commit a1fc867bf2cb75f439b6e4095eb5a192ed21eeff created 12 years 11 months ago. By Werner Almesberger, atrf-path: option -P to load a min/max profile; pass/fail indication in GUI | |
---|---|
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 | #include <string.h> |
18 | #include <ctype.h> |
19 | |
20 | #include "at86rf230.h" |
21 | |
22 | #include "misctxrx.h" |
23 | #include "cwtest.h" |
24 | #include "atrf.h" |
25 | |
26 | #include "gui.h" |
27 | #include "sweep.h" |
28 | |
29 | |
30 | #define DEFAULT_TRIM 8 |
31 | #define DEFAULT_POWER 15 |
32 | |
33 | |
34 | static void set_channel(struct atrf_dsc *dsc, int chan) |
35 | { |
36 | atrf_reg_write(dsc, REG_PHY_CC_CCA, (1 << CCA_MODE_SHIFT) | chan); |
37 | } |
38 | |
39 | |
40 | static void init_common(struct atrf_dsc *dsc, int trim) |
41 | { |
42 | atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF); |
43 | atrf_reg_write(dsc, REG_XOSC_CTRL, |
44 | (XTAL_MODE_INT << XTAL_MODE_SHIFT) | trim); |
45 | atrf_set_clkm(dsc, 0); |
46 | } |
47 | |
48 | |
49 | static void init_tx(struct atrf_dsc *dsc, int trim, int power) |
50 | { |
51 | init_common(dsc, trim); |
52 | set_power_step(dsc, power, 0); |
53 | } |
54 | |
55 | |
56 | static void init_rx(struct atrf_dsc *dsc, int trim) |
57 | { |
58 | init_common(dsc, trim); |
59 | atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_RX_ON); |
60 | } |
61 | |
62 | |
63 | static double rssi_to_dBm(double rssi) |
64 | { |
65 | return -91+3*(rssi-1); |
66 | } |
67 | |
68 | |
69 | static void sample(const struct sweep *sweep, int cont_tx, |
70 | struct sample *res, int first) |
71 | { |
72 | static int need_init = 1; |
73 | int i, rssi; |
74 | int sum = 0, min = -1, max = -1; |
75 | double offset = tx_power_step2dBm(sweep->tx, sweep->power); |
76 | |
77 | /* |
78 | * For the 230, we don't have reset-less exit from test mode (yet ?) and |
79 | * need to set up things from scratch: |
80 | * |
81 | * init_tx(sweep->tx, sweep->trim_tx, sweep->power); |
82 | * set_channel(sweep->tx, chan); |
83 | * usleep(155); / * table 7-2, tTR19 * / |
84 | */ |
85 | if (first || need_init) { |
86 | cw_test_begin(sweep->tx, cont_tx); |
87 | need_init = 0; |
88 | } else { |
89 | cw_test_resume(sweep->tx); |
90 | } |
91 | /* table 7-1, tTR10, doubling since it's a "typical" value */ |
92 | usleep(2*16); |
93 | |
94 | for (i = 0; i != sweep->samples; i++) { |
95 | /* according to 8.3.2, PHY_RSSI is updated every 2 us */ |
96 | usleep(2); |
97 | |
98 | rssi = atrf_reg_read(sweep->rx, REG_PHY_RSSI) & RSSI_MASK; |
99 | sum += rssi; |
100 | if (min == -1 || rssi < min) |
101 | min = rssi; |
102 | if (rssi > max) |
103 | max = rssi; |
104 | } |
105 | |
106 | cw_test_end(sweep->tx); |
107 | |
108 | res->avg = rssi_to_dBm((double) sum/sweep->samples)-offset; |
109 | res->min = rssi_to_dBm(min)-offset; |
110 | res->max = rssi_to_dBm(max)-offset; |
111 | } |
112 | |
113 | |
114 | static int do_half_sweep(const struct sweep *sweep, int cont_tx, |
115 | struct sample *res) |
116 | { |
117 | int i, chan; |
118 | int fail = 0; |
119 | |
120 | if (sweep->cont_tx && sweep->cont_tx != cont_tx) |
121 | return 0; |
122 | chan = 11; |
123 | for (i = 0; i != N_CHAN; i++) { |
124 | set_channel(sweep->rx, chan); |
125 | set_channel(sweep->tx, chan); |
126 | usleep(155); /* table 7-2, tTR19 */ |
127 | |
128 | sample(sweep, cont_tx, res, chan == 11 && !sweep->cont_tx); |
129 | if (res->avg > sweep->max[i]) |
130 | fail = 1; |
131 | if (!fail && res->avg < sweep->min[i]) |
132 | fail = -1; |
133 | |
134 | res += 2; |
135 | chan++; |
136 | } |
137 | return fail; |
138 | } |
139 | |
140 | |
141 | int do_sweep(const struct sweep *sweep, struct sample *res) |
142 | { |
143 | int fail1, fail2; |
144 | |
145 | fail1 = do_half_sweep(sweep, CONT_TX_M500K, res); |
146 | fail2 = do_half_sweep(sweep, CONT_TX_P500K, res+1); |
147 | if (fail1 > 0 || fail2 > 0) |
148 | return 1; |
149 | if (fail1 < 0 || fail2 < 0) |
150 | return -1; |
151 | return 0; |
152 | } |
153 | |
154 | |
155 | static void print_sweep(const struct sweep *sweep, const struct sample *res) |
156 | { |
157 | int chan; |
158 | |
159 | for (chan = 11; chan <= 26; chan++) { |
160 | if (sweep->cont_tx != CONT_TX_P500K) |
161 | printf("%.1f %.2f %.0f %.0f\n", |
162 | 2350+5*chan-0.5, res->avg, res->min, res->max); |
163 | res++; |
164 | if (sweep->cont_tx != CONT_TX_M500K) |
165 | printf("%.1f %.2f %.0f %.0f\n", |
166 | 2350+5*chan+0.5, res->avg, res->min, res->max); |
167 | res++; |
168 | } |
169 | } |
170 | |
171 | |
172 | static void do_sweeps(const struct sweep *sweep, int sweeps) |
173 | { |
174 | struct sample res[N_CHAN*2]; /* 2 offsets per channel */ |
175 | int i; |
176 | |
177 | for (i = 0; i != sweeps; i++) { |
178 | if (i) |
179 | putchar('\n'); |
180 | do_sweep(sweep, res); |
181 | print_sweep(sweep, res); |
182 | } |
183 | } |
184 | |
185 | |
186 | static int do_read_profile(const char *name, struct sweep *sweep) |
187 | { |
188 | FILE *file; |
189 | char buf[300]; |
190 | int got; |
191 | char *p; |
192 | double min = MIN_DIFF, max = MAX_DIFF; |
193 | int n = 0; |
194 | |
195 | file = fopen(name, "r"); |
196 | if (!file) { |
197 | perror(name); |
198 | exit(1); |
199 | } |
200 | while (fgets(buf, sizeof(buf), file)) { |
201 | p = strchr(buf, '\n'); |
202 | if (p) |
203 | *p = 0; |
204 | p = strchr(buf, '#'); |
205 | if (p) |
206 | *p = 0; |
207 | for (p = buf; *p && isspace(*p); p++); |
208 | if (!*p) |
209 | continue; |
210 | got = sscanf(buf, "%lf %lf", &min, &max); |
211 | switch (got) { |
212 | case 0: |
213 | fprintf(stderr, "can't parse \"%s\"\n", buf); |
214 | exit(1); |
215 | case 1: |
216 | max = MAX_DIFF; |
217 | /* fall through */ |
218 | case 2: |
219 | if (min < MIN_DIFF) { |
220 | fprintf(stderr, "minimum is %g dBm\n", |
221 | MIN_DIFF); |
222 | exit(1); |
223 | } |
224 | if (max > MAX_DIFF) { |
225 | fprintf(stderr, "maximum is %g dBm\n", |
226 | MAX_DIFF); |
227 | exit(1); |
228 | } |
229 | if (min > max) { |
230 | fprintf(stderr, "lower bound > upper bound\n"); |
231 | exit(1); |
232 | } |
233 | if (n == N_CHAN) { |
234 | fprintf(stderr, "too many channels\n"); |
235 | exit(1); |
236 | } |
237 | sweep->min[n] = min; |
238 | sweep->max[n] = max; |
239 | n++; |
240 | break; |
241 | default: |
242 | abort(); |
243 | } |
244 | } |
245 | fclose(file); |
246 | return n; |
247 | } |
248 | |
249 | |
250 | static void read_profile(const char *name, struct sweep *sweep) |
251 | { |
252 | int n = 0; |
253 | |
254 | if (name) |
255 | n = do_read_profile(name, sweep); |
256 | |
257 | while (n != N_CHAN) { |
258 | sweep->min[n] = MIN_DIFF; |
259 | sweep->max[n] = MAX_DIFF; |
260 | n++; |
261 | } |
262 | } |
263 | |
264 | |
265 | static void usage(const char *name) |
266 | { |
267 | fprintf(stderr, |
268 | "usage: %s common_args [sweeps [samples]]\n" |
269 | |
270 | #ifdef HAVE_GFX |
271 | "%6s %s -g common_args [[sweeps] samples]\n" |
272 | #endif |
273 | "\n" |
274 | " common args: [-p power] [-P profile] [-t trim_tx [-t trim_rx]]\n" |
275 | " [-T offset] driver_tx[:arg] driver_rx[:arg]\n\n" |
276 | |
277 | #ifdef HAVE_GFX |
278 | " -g display results graphically\n" |
279 | #endif |
280 | " -p power transmit power, 0 to 15 (default %d)\n" |
281 | " -P profile load profile for pass/fail decisions\n" |
282 | " -t trim trim capacitor, 0 to 15 (default %d)\n" |
283 | " -T offset constant wave offset in MHz, -0.5 or +0.5 (default: scan both)\n" |
284 | |
285 | , name, |
286 | #ifdef HAVE_GFX |
287 | "", name, |
288 | #endif |
289 | DEFAULT_POWER, DEFAULT_TRIM); |
290 | |
291 | exit(1); |
292 | } |
293 | |
294 | |
295 | int main(int argc, char **argv) |
296 | { |
297 | const char *tx_drv, *rx_drv; |
298 | struct sweep sweep = { |
299 | .trim_tx = -1, |
300 | .trim_rx = DEFAULT_TRIM, |
301 | .cont_tx = 0, |
302 | .samples = 1, |
303 | }; |
304 | int graphical = 0; |
305 | int power = DEFAULT_POWER; |
306 | const char *profile = NULL; |
307 | int sweeps = 1; |
308 | unsigned long tmp; |
309 | char *end; |
310 | int c; |
311 | |
312 | while ((c = getopt(argc, argv, "gp:P:t:T:")) != EOF) |
313 | switch (c) { |
314 | case'g': |
315 | graphical = 1; |
316 | sweeps = 0; |
317 | break; |
318 | case 'p': |
319 | tmp = strtoul(optarg, &end, 0); |
320 | if (*end || tmp > 15) |
321 | usage(*argv); |
322 | power = tmp; |
323 | break; |
324 | case 'P': |
325 | profile = optarg; |
326 | break; |
327 | case 't': |
328 | tmp = strtoul(optarg, &end, 0); |
329 | if (*end || tmp > 15) |
330 | usage(*argv); |
331 | if (sweep.trim_tx == -1) |
332 | sweep.trim_tx = tmp; |
333 | else |
334 | sweep.trim_rx = tmp; |
335 | break; |
336 | case 'T': |
337 | if (!strcmp(optarg, "-0.5")) |
338 | sweep.cont_tx = CONT_TX_M500K; |
339 | else if (!strcmp(optarg, "+0.5")) |
340 | sweep.cont_tx = CONT_TX_P500K; |
341 | else |
342 | usage(*argv); |
343 | break; |
344 | default: |
345 | usage(*argv); |
346 | } |
347 | |
348 | if (sweep.trim_tx == -1) |
349 | sweep.trim_tx = DEFAULT_TRIM; |
350 | |
351 | switch (argc-optind) { |
352 | case 4: |
353 | sweep.samples = strtoul(argv[optind+3], &end, 0); |
354 | if (*end) |
355 | usage(*argv); |
356 | /* fall through */ |
357 | case 3: |
358 | sweeps = strtoul(argv[optind+2], &end, 0); |
359 | if (*end) |
360 | usage(*argv); |
361 | if (graphical && argc-optind == 3) { |
362 | sweep.samples = sweeps; |
363 | sweeps = 0; |
364 | } |
365 | /* fall through */ |
366 | case 2: |
367 | tx_drv = argv[optind]; |
368 | rx_drv = argv[optind+1]; |
369 | break; |
370 | default: |
371 | usage(*argv); |
372 | } |
373 | |
374 | read_profile(profile, &sweep); |
375 | |
376 | sweep.tx = atrf_open(tx_drv); |
377 | if (!sweep.tx) |
378 | return 1; |
379 | sweep.rx = atrf_open(rx_drv); |
380 | if (!sweep.rx) |
381 | return 1; |
382 | |
383 | sweep.power = 15-power; |
384 | init_rx(sweep.rx, sweep.trim_rx); |
385 | init_tx(sweep.tx, sweep.trim_tx, sweep.power); |
386 | if (graphical) |
387 | gui(&sweep, sweeps); |
388 | else |
389 | do_sweeps(&sweep, sweeps); |
390 | |
391 | atrf_reg_write(sweep.tx, REG_TRX_STATE, TRX_CMD_TRX_OFF); |
392 | atrf_reg_write(sweep.rx, REG_TRX_STATE, TRX_CMD_TRX_OFF); |
393 | |
394 | atrf_close(sweep.tx); |
395 | atrf_close(sweep.rx); |
396 | |
397 | return 0; |
398 | } |
399 |