1
0
mirror of git://projects.qi-hardware.com/ben-wpan.git synced 2024-11-26 01:57:32 +02:00

atrf-xtal: moved atben-specific code to atben.c

- atrf-xtal.c (cmp, eval, do_atben): moved to atben.c
- atrf-xtal.c (setup, sample, cleanup): removed wrappers
- atrf-xtal.c (usage): cast strlen result, for x86-64 compatibility
- atrf-xtal.h (atben_setup, atben_sample, atben_cleanup), atrf-xtal.c:
  device interface functions are now "static"
- atrf-xtal.h (do_atben): do_atben is now our new interface
This commit is contained in:
Werner Almesberger 2011-05-30 02:56:10 -03:00
parent 5a6aae57cb
commit bdca20479b
3 changed files with 77 additions and 104 deletions

View File

@ -1,5 +1,5 @@
/*
* atrf-xtal/atben.c - ATBEN-specific low-level driver
* atrf-xtal/atben.c - ATBEN-specific driver and evaluation
*
* Written 2011 by Werner Almesberger
* Copyright 2011 Werner Almesberger
@ -30,7 +30,9 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <sys/mman.h>
#include "at86rf230.h"
@ -151,7 +153,7 @@ static void ben_setup(struct atrf_dsc *dsc)
}
/* ----- Interface --------------------------------------------------------- */
/* ----- Low-level interface ----------------------------------------------- */
void atben_setup(struct atrf_dsc *dsc, int size, int trim)
@ -209,3 +211,73 @@ void atben_cleanup(struct atrf_dsc *dsc)
{
enable_lcd();
}
/* ----- Acquisition and evaluation ---------------------------------------- */
static int cmp(const void *a, const void *b)
{
return *(unsigned *) a-*(unsigned *) b;
}
static double eval(unsigned *res, int rep)
{
double sum = 0;
int n = 0;
int i;
qsort(res, rep, sizeof(*res), cmp);
if (rep < 8)
return res[rep >> 1];
for (i = rep/8; i != rep-rep/8; i++) {
sum += res[i];
n++;
}
return (double) sum/n;
}
void do_atben(struct atrf_dsc *dsc, int size, int trim, int rep,
int dump_raw, double base, double ppm)
{
unsigned *res;
double avg, rel;
int i;
res = malloc(rep*sizeof(*res));
if (!res) {
perror("malloc");
exit(1);
}
atben_setup(dsc, size, trim);
for (i = 0; i != rep; i++)
res[i] = atben_sample(dsc);
atben_cleanup(dsc);
atrf_close(dsc);
if (dump_raw) {
for (i = 0; i != rep; i++)
printf("%u\n", res[i]);
exit(0);
}
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");
exit(1);
}
putchar('\n');
}
}

View File

@ -15,7 +15,6 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include "atrf.h"
@ -26,103 +25,6 @@
#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;
}
static double eval(unsigned *res, int rep)
{
double sum = 0;
int n = 0;
int i;
qsort(res, rep, sizeof(*res), cmp);
if (rep < 8)
return res[rep >> 1];
for (i = rep/8; i != rep-rep/8; i++) {
sum += res[i];
n++;
}
return (double) sum/n;
}
static void do_atben(struct atrf_dsc *dsc, int size, int trim, int rep,
int dump_raw, double base, double ppm)
{
unsigned *res;
double avg, rel;
int i;
res = malloc(rep*sizeof(*res));
if (!res) {
perror("malloc");
exit(1);
}
setup(dsc, size, trim);
for (i = 0; i != rep; i++)
res[i] = sample(dsc);
cleanup(dsc);
atrf_close(dsc);
if (dump_raw) {
for (i = 0; i != rep; i++)
printf("%u\n", res[i]);
exit(0);
}
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");
exit(1);
}
putchar('\n');
}
}
static void usage(const char *name)
{
fprintf(stderr,
@ -135,7 +37,7 @@ static void usage(const char *name)
" -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"
, name, strlen(name), "",
, name, (int) strlen(name), "",
atrf_default_driver_name(), DEFAULT_SIZE, DEFAULT_TRIM);
exit(1);
}

View File

@ -17,8 +17,7 @@
#include "atrf.h"
void atben_setup(struct atrf_dsc *dsc, int size, int trim);
unsigned atben_sample(struct atrf_dsc *dsc);
void atben_cleanup(struct atrf_dsc *dsc);
void do_atben(struct atrf_dsc *dsc, int size, int trim, int rep,
int dump_raw, double base, double ppm);
#endif /* !ATRF_XTAL_H */