2011-04-04 04:29:59 +03:00
|
|
|
/*
|
|
|
|
* atrf-xtal/atrf-xtal.c - AT86RF230/1 crystal diagnostic utility
|
|
|
|
*
|
|
|
|
* Written 2011 by Werner Almesberger
|
|
|
|
* Copyright 2011 Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2011-05-18 23:32:15 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2011-04-04 04:29:59 +03:00
|
|
|
|
|
|
|
#include "atrf.h"
|
|
|
|
|
|
|
|
#include "atrf-xtal.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_SIZE 127
|
|
|
|
#define DEFAULT_TRIM 8
|
|
|
|
|
|
|
|
|
|
|
|
static void setup(struct atrf_dsc *dsc, int size, int trim)
|
|
|
|
{
|
|
|
|
#ifdef __mips__
|
|
|
|
atben_setup(dsc, size, trim);
|
|
|
|
#else
|
|
|
|
#error Only ATBEN is supported for now
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned sample(struct atrf_dsc *dsc)
|
|
|
|
{
|
|
|
|
#ifdef __mips__
|
|
|
|
return atben_sample(dsc);
|
|
|
|
#else
|
|
|
|
#error Only ATBEN is supported for now
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cleanup(struct atrf_dsc *dsc)
|
|
|
|
{
|
|
|
|
#ifdef __mips__
|
|
|
|
atben_cleanup(dsc);
|
|
|
|
#else
|
|
|
|
#error Only ATBEN is supported for now
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int cmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return *(unsigned *) a-*(unsigned *) b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-18 23:32:15 +03:00
|
|
|
static double eval(unsigned *res, int rep)
|
2011-04-04 04:29:59 +03:00
|
|
|
{
|
|
|
|
double sum = 0;
|
|
|
|
int n = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
qsort(res, rep, sizeof(*res), cmp);
|
2011-05-18 23:32:15 +03:00
|
|
|
if (rep < 8)
|
|
|
|
return res[rep >> 1];
|
2011-04-04 04:29:59 +03:00
|
|
|
for (i = rep/8; i != rep-rep/8; i++) {
|
|
|
|
sum += res[i];
|
|
|
|
n++;
|
|
|
|
}
|
2011-05-18 23:32:15 +03:00
|
|
|
return (double) sum/n;
|
2011-04-04 04:29:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2011-05-18 23:32:15 +03:00
|
|
|
"usage: %s [-b count [-p ppm]] [-d driver[:arg]] [-r] [-s size] [-t trim]\n"
|
|
|
|
" %*s [repetitions]\n\n"
|
|
|
|
" -b count base count for relative result\n"
|
tools/: added option -d driver[:arg] to all tools
- atrf-id/atrf-id.c (main), atrf-reset/atrf-reset.c (main),
atrf-trim/atrf-trim.c (main): changed command-line parsing to use
"getopt"
- atrf-id/atrf-id.c (usage, main), atrf-reset/atrf-reset.c (usage, main),
atrf-rssi/atrf-rssi.c (usage, main), atrf-trim/atrf-trim.c (usage,
main), atrf-txrx/atrf-txrx.c (init_txrx, usage, main),
atrf-xtal/atrf-xtal.c (usage, main): added option -d to select a driver
2011-04-10 13:13:53 +03:00
|
|
|
" -d driver[:arg] use the specified driver (default: %s)\n"
|
2011-05-18 23:32:15 +03:00
|
|
|
" -p ppm maximum deviation from base count\n"
|
tools/: added option -d driver[:arg] to all tools
- atrf-id/atrf-id.c (main), atrf-reset/atrf-reset.c (main),
atrf-trim/atrf-trim.c (main): changed command-line parsing to use
"getopt"
- atrf-id/atrf-id.c (usage, main), atrf-reset/atrf-reset.c (usage, main),
atrf-rssi/atrf-rssi.c (usage, main), atrf-trim/atrf-trim.c (usage,
main), atrf-txrx/atrf-txrx.c (init_txrx, usage, main),
atrf-xtal/atrf-xtal.c (usage, main): added option -d to select a driver
2011-04-10 13:13:53 +03:00
|
|
|
" -r instead of printing a mean value, dump the raw samples\n"
|
|
|
|
" -s size payload size in bytes, 0-127 (default: %d bytes)\n"
|
|
|
|
" -t trim trim capacitor setting, 0-15 (default: %d)\n"
|
|
|
|
" repetitions number of measurements (default: 1)\n"
|
2011-05-18 23:32:15 +03:00
|
|
|
, name, strlen(name), "",
|
|
|
|
atrf_default_driver_name(), DEFAULT_SIZE, DEFAULT_TRIM);
|
2011-04-04 04:29:59 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *const *argv)
|
|
|
|
{
|
tools/: added option -d driver[:arg] to all tools
- atrf-id/atrf-id.c (main), atrf-reset/atrf-reset.c (main),
atrf-trim/atrf-trim.c (main): changed command-line parsing to use
"getopt"
- atrf-id/atrf-id.c (usage, main), atrf-reset/atrf-reset.c (usage, main),
atrf-rssi/atrf-rssi.c (usage, main), atrf-trim/atrf-trim.c (usage,
main), atrf-txrx/atrf-txrx.c (init_txrx, usage, main),
atrf-xtal/atrf-xtal.c (usage, main): added option -d to select a driver
2011-04-10 13:13:53 +03:00
|
|
|
const char *driver = NULL;
|
2011-04-04 04:29:59 +03:00
|
|
|
struct atrf_dsc *dsc;
|
|
|
|
int size = DEFAULT_SIZE;
|
|
|
|
int trim = DEFAULT_TRIM;
|
2011-05-18 23:32:15 +03:00
|
|
|
double base = 0, ppm = 0;
|
|
|
|
double avg, rel;
|
2011-04-04 04:29:59 +03:00
|
|
|
int rep = 1;
|
2011-04-10 03:36:52 +03:00
|
|
|
int dump_raw = 0;
|
2011-04-04 04:29:59 +03:00
|
|
|
char *end;
|
|
|
|
unsigned *res;
|
|
|
|
int c, i;
|
|
|
|
|
2011-05-18 23:32:15 +03:00
|
|
|
while ((c = getopt(argc, argv, "b:d:p:rs:t:")) != EOF)
|
2011-04-04 04:29:59 +03:00
|
|
|
switch (c) {
|
2011-05-18 23:32:15 +03:00
|
|
|
case 'b':
|
|
|
|
base = strtof(optarg, &end);
|
|
|
|
if (*end)
|
|
|
|
usage(*argv);
|
|
|
|
break;
|
tools/: added option -d driver[:arg] to all tools
- atrf-id/atrf-id.c (main), atrf-reset/atrf-reset.c (main),
atrf-trim/atrf-trim.c (main): changed command-line parsing to use
"getopt"
- atrf-id/atrf-id.c (usage, main), atrf-reset/atrf-reset.c (usage, main),
atrf-rssi/atrf-rssi.c (usage, main), atrf-trim/atrf-trim.c (usage,
main), atrf-txrx/atrf-txrx.c (init_txrx, usage, main),
atrf-xtal/atrf-xtal.c (usage, main): added option -d to select a driver
2011-04-10 13:13:53 +03:00
|
|
|
case 'd':
|
|
|
|
driver = optarg;
|
|
|
|
break;
|
2011-05-18 23:32:15 +03:00
|
|
|
case 'p':
|
|
|
|
ppm = strtof(optarg, &end);
|
|
|
|
if (*end)
|
|
|
|
usage(*argv);
|
|
|
|
break;
|
2011-04-10 03:36:52 +03:00
|
|
|
case 'r':
|
|
|
|
dump_raw = 1;
|
2011-04-04 04:29:59 +03:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
size = strtoul(optarg, &end, 0);
|
|
|
|
if (*end)
|
|
|
|
usage(*argv);
|
|
|
|
if (size > 127)
|
|
|
|
usage(*argv);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
trim = strtoul(optarg, &end, 0);
|
|
|
|
if (*end)
|
|
|
|
usage(*argv);
|
|
|
|
if (trim > 15)
|
|
|
|
usage(*argv);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
|
2011-05-18 23:32:15 +03:00
|
|
|
if (ppm && !base)
|
|
|
|
usage(*argv);
|
|
|
|
|
2011-04-04 04:29:59 +03:00
|
|
|
switch (argc-optind) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
rep = strtoul(argv[optind], &end, 0);
|
|
|
|
if (*end)
|
|
|
|
usage(*argv);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = malloc(rep*sizeof(*res));
|
|
|
|
if (!res) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
tools/: added option -d driver[:arg] to all tools
- atrf-id/atrf-id.c (main), atrf-reset/atrf-reset.c (main),
atrf-trim/atrf-trim.c (main): changed command-line parsing to use
"getopt"
- atrf-id/atrf-id.c (usage, main), atrf-reset/atrf-reset.c (usage, main),
atrf-rssi/atrf-rssi.c (usage, main), atrf-trim/atrf-trim.c (usage,
main), atrf-txrx/atrf-txrx.c (init_txrx, usage, main),
atrf-xtal/atrf-xtal.c (usage, main): added option -d to select a driver
2011-04-10 13:13:53 +03:00
|
|
|
dsc = atrf_open(driver);
|
2011-04-04 04:29:59 +03:00
|
|
|
if (!dsc)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
setup(dsc, size, trim);
|
|
|
|
|
|
|
|
for (i = 0; i != rep; i++)
|
|
|
|
res[i] = sample(dsc);
|
|
|
|
|
|
|
|
cleanup(dsc);
|
|
|
|
|
|
|
|
atrf_close(dsc);
|
|
|
|
|
2011-04-10 03:36:52 +03:00
|
|
|
if (dump_raw) {
|
2011-04-04 04:29:59 +03:00
|
|
|
for (i = 0; i != rep; i++)
|
|
|
|
printf("%u\n", res[i]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-05-18 23:32:15 +03:00
|
|
|
avg = eval(res, rep);
|
|
|
|
if (!base)
|
|
|
|
printf("%f\n", avg);
|
|
|
|
else {
|
|
|
|
rel = (avg/base-1)*1000000;
|
|
|
|
printf("%+f ppm", rel);
|
|
|
|
if (ppm && fabs(rel) > ppm) {
|
|
|
|
printf(" (outside bounds)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2011-04-04 04:29:59 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|