1
0
mirror of git://projects.qi-hardware.com/ben-wpan.git synced 2024-11-05 04:48:26 +02:00

tools/atrf-xtal: new utility to measure the crystal frequency

- Makefile (DIRS): added atrf-xtal
- atrf-xtal/Makefile: build atrf-xtal for the Ben or do nothing elsewhere
- atrf-xtal/atrf-xtal.h, atrf-xtal/atrf-xtal.c: diagnostic utility to
  determine the transceiver's crystal frequency
- atrf-xtal/atben.c: dirty low-level driver for ATBEN
This commit is contained in:
Werner Almesberger 2011-04-03 22:29:59 -03:00
parent fe2720e63a
commit 28e7f25585
5 changed files with 382 additions and 1 deletions

View File

@ -11,7 +11,8 @@
#
DIRS=atrf-id atrf-reset atrf-rssi atrf-trim atrf-txrx
DIRS=atrf-id atrf-reset atrf-rssi atrf-trim atrf-txrx \
atrf-xtal
TARGET_ONLY_DIRS=lib
ifeq ($(TARGET),ben_jlime)

44
tools/atrf-xtal/Makefile Normal file
View File

@ -0,0 +1,44 @@
#
# atrf-xtal/Makefile - Build the crystal diagnostic utility
#
# Written 2010, 2011 by Werner Almesberger
# Copyright 2010, 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.
#
ifeq ($(TARGET),)
TARGET = host
endif
ifeq ($(TARGET),host)
.PHONY: all install uninstall clean spotless
all:
install:
uninstall:
clean:
spotless:
else
MAIN = atrf-xtal
include ../Makefile.common
CFLAGS += -O9
OBJS_$(TARGET) = atben.o
$(MAIN): $(OBJS_$(TARGET))
endif

142
tools/atrf-xtal/atben.c Normal file
View File

@ -0,0 +1,142 @@
/*
* atrf-xtal/atben.c - ATBEN-specific low-level driver
*
* 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.
*/
/*
* WARNING: this program does very nasty things to the Ben and it doesn't
* like company. In particular, it resents:
*
* - the MMC driver - disable it with
* echo jz4740-mmc.0 >/sys/bus/platform/drivers/jz4740-mmc/unbind
* - the AT86RF230/1 kernel driver - use a kernel that doesn't have it
* - anything that accesses the screen - kill GUI, X server, etc.
* - the screen blanker - either disable it or make sure the screen stays
* dark, e.g., with
* echo 1 >/sys/devices/platform/jz4740-fb/graphics/fb0/blank
* - probably a fair number of other daemons and things as well - best to
* kill them all.
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include "at86rf230.h"
#include "atrf.h"
#include "atrf-xtal.h"
#define MAX_COUNT (1000*1000)
static volatile void *base;
static volatile uint32_t *clkgr;
static uint32_t old_clkgr;
void atben_setup(struct atrf_dsc *dsc, int size, int trim)
{
static uint8_t buf[127];
atrf_reset_rf(dsc);
atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF);
atrf_reg_write(dsc, REG_XOSC_CTRL,
(XTAL_MODE_INT << XTAL_MODE_SHIFT) | trim);
/* minimum TX power, maximize delays, disable CRC */
switch (atrf_identify(dsc)) {
case artf_at86rf230:
atrf_reg_write(dsc, REG_PHY_TX_PWR, 0xf);
break;
case artf_at86rf231:
atrf_reg_write(dsc, REG_PHY_TX_PWR, 0xff);
atrf_reg_write(dsc, REG_TRX_CTRL_1, 0);
break;
default:
abort();
}
atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_PLL_ON);
usleep(200); /* nominally 180 us worst-case */
atrf_buf_write(dsc, buf, size);
//atrf_reg_write(dsc, REG_IRQ_MASK, IRQ_TRX_END);
atrf_reg_write(dsc, REG_IRQ_MASK, 0xff);
mlockall(MCL_CURRENT);
struct atrf_dsc {
void *driver;
void *handle;
int chip_id;
};
struct atben_dsc {
int fd;
void *mem;
};
base = ((struct atben_dsc *) ((struct atrf_dsc *) dsc)->handle)->mem;
clkgr = base+0x20;
old_clkgr = *clkgr;
*clkgr = old_clkgr | 1 << 10;
}
unsigned atben_sample(struct atrf_dsc *dsc)
{
unsigned i = MAX_COUNT;
(void) atrf_reg_read(dsc, REG_IRQ_STATUS);
#if 0
while (i) {
if (atrf_interrupt(dsc))
break;
i--;
}
#else
volatile uint32_t *pdpin = base+0x10300;
volatile uint32_t *pddats = base+0x10314;
volatile uint32_t *pddatc = base+0x10318;
volatile uint32_t *icmr = base+0x1004;
volatile uint32_t *icmsr = base+0x1008;
volatile uint32_t *icmcr = base+0x100c;
#if 0
atrf_slp_tr(dsc, 1);
while (i) {
if (*pdpin & 0x1000)
break;
i--;
}
atrf_slp_tr(dsc, 0);
#endif
uint32_t old_icmr = *icmr;
*icmsr = 0xffffffff;
*pddats = 1 << 9;
*pddatc = 1 << 9;
do i--;
while (!(*pdpin & 0x1000));
*icmcr = ~old_icmr;
#endif
return MAX_COUNT-i;
}
void atben_cleanup(struct atrf_dsc *dsc)
{
*clkgr = old_clkgr;
}

170
tools/atrf-xtal/atrf-xtal.c Normal file
View File

@ -0,0 +1,170 @@
/*
* 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>
#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;
}
static void eval(unsigned *res, int rep)
{
double sum = 0;
int n = 0;
int i;
qsort(res, rep, sizeof(*res), cmp);
if (rep < 8) {
printf("%u\n", res[rep >> 1]);
return;
}
for (i = rep/8; i != rep-rep/8; i++) {
sum += res[i];
n++;
}
printf("%f\n", (double) sum/n);
}
static void usage(const char *name)
{
fprintf(stderr,
"%s [-d] [-s size] [-t trim] [repetitions]\n"
" -d instead of printing a mean value, dump all 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"
, name, DEFAULT_SIZE, DEFAULT_TRIM);
exit(1);
}
int main(int argc, char *const *argv)
{
struct atrf_dsc *dsc;
int size = DEFAULT_SIZE;
int trim = DEFAULT_TRIM;
int rep = 1;
int dump = 0;
char *end;
unsigned *res;
int c, i;
while ((c = getopt(argc, argv, "ds:t:")) != EOF)
switch (c) {
case 'd':
dump = 1;
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);
}
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);
}
dsc = atrf_open();
if (!dsc)
return 1;
setup(dsc, size, trim);
for (i = 0; i != rep; i++)
res[i] = sample(dsc);
cleanup(dsc);
atrf_close(dsc);
if (dump) {
for (i = 0; i != rep; i++)
printf("%u\n", res[i]);
exit(0);
}
eval(res, rep);
return 0;
}

View File

@ -0,0 +1,24 @@
/*
* atrf-xtal/atrf-xtal.h - 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.
*/
#ifndef ATRF_XTAL_H
#define ATRF_XTAL_H
#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);
#endif /* !ATRF_XTAL_H */