Date:2011-04-04 03:29:59 (12 years 11 months ago)
Author:Werner Almesberger
Commit:28e7f255851fb5de8bf143f0f01d0e3c93006dda
Message: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
Files: tools/Makefile (1 diff)
tools/atrf-xtal/Makefile (1 diff)
tools/atrf-xtal/atben.c (1 diff)
tools/atrf-xtal/atrf-xtal.c (1 diff)
tools/atrf-xtal/atrf-xtal.h (1 diff)

Change Details

tools/Makefile
1111#
1212
1313
14DIRS=atrf-id atrf-reset atrf-rssi atrf-trim atrf-txrx
14DIRS=atrf-id atrf-reset atrf-rssi atrf-trim atrf-txrx \
15     atrf-xtal
1516TARGET_ONLY_DIRS=lib
1617
1718ifeq ($(TARGET),ben_jlime)
tools/atrf-xtal/Makefile
1#
2# atrf-xtal/Makefile - Build the crystal diagnostic utility
3#
4# Written 2010, 2011 by Werner Almesberger
5# Copyright 2010, 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
14ifeq ($(TARGET),)
15TARGET = host
16endif
17
18ifeq ($(TARGET),host)
19
20.PHONY: all install uninstall clean spotless
21
22all:
23
24install:
25
26uninstall:
27
28clean:
29
30spotless:
31
32else
33
34MAIN = atrf-xtal
35
36include ../Makefile.common
37
38CFLAGS += -O9
39
40OBJS_$(TARGET) = atben.o
41
42$(MAIN): $(OBJS_$(TARGET))
43
44endif
tools/atrf-xtal/atben.c
1/*
2 * atrf-xtal/atben.c - ATBEN-specific low-level driver
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 * WARNING: this program does very nasty things to the Ben and it doesn't
15 * like company. In particular, it resents:
16 *
17 * - the MMC driver - disable it with
18 * echo jz4740-mmc.0 >/sys/bus/platform/drivers/jz4740-mmc/unbind
19 * - the AT86RF230/1 kernel driver - use a kernel that doesn't have it
20 * - anything that accesses the screen - kill GUI, X server, etc.
21 * - the screen blanker - either disable it or make sure the screen stays
22 * dark, e.g., with
23 * echo 1 >/sys/devices/platform/jz4740-fb/graphics/fb0/blank
24 * - probably a fair number of other daemons and things as well - best to
25 * kill them all.
26 */
27
28
29#include <stdlib.h>
30#include <unistd.h>
31#include <sys/mman.h>
32
33#include "at86rf230.h"
34#include "atrf.h"
35
36#include "atrf-xtal.h"
37
38
39#define MAX_COUNT (1000*1000)
40
41
42static volatile void *base;
43static volatile uint32_t *clkgr;
44static uint32_t old_clkgr;
45
46
47void atben_setup(struct atrf_dsc *dsc, int size, int trim)
48{
49    static uint8_t buf[127];
50
51    atrf_reset_rf(dsc);
52    atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF);
53
54    atrf_reg_write(dsc, REG_XOSC_CTRL,
55        (XTAL_MODE_INT << XTAL_MODE_SHIFT) | trim);
56
57    /* minimum TX power, maximize delays, disable CRC */
58    switch (atrf_identify(dsc)) {
59    case artf_at86rf230:
60        atrf_reg_write(dsc, REG_PHY_TX_PWR, 0xf);
61        break;
62    case artf_at86rf231:
63        atrf_reg_write(dsc, REG_PHY_TX_PWR, 0xff);
64        atrf_reg_write(dsc, REG_TRX_CTRL_1, 0);
65        break;
66    default:
67        abort();
68    }
69
70    atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_PLL_ON);
71    usleep(200); /* nominally 180 us worst-case */
72
73    atrf_buf_write(dsc, buf, size);
74
75    //atrf_reg_write(dsc, REG_IRQ_MASK, IRQ_TRX_END);
76    atrf_reg_write(dsc, REG_IRQ_MASK, 0xff);
77
78    mlockall(MCL_CURRENT);
79
80    struct atrf_dsc {
81        void *driver;
82        void *handle;
83        int chip_id;
84    };
85    struct atben_dsc {
86        int fd;
87        void *mem;
88    };
89
90    base = ((struct atben_dsc *) ((struct atrf_dsc *) dsc)->handle)->mem;
91    clkgr = base+0x20;
92
93    old_clkgr = *clkgr;
94    *clkgr = old_clkgr | 1 << 10;
95}
96
97
98unsigned atben_sample(struct atrf_dsc *dsc)
99{
100    unsigned i = MAX_COUNT;
101
102    (void) atrf_reg_read(dsc, REG_IRQ_STATUS);
103#if 0
104    while (i) {
105        if (atrf_interrupt(dsc))
106            break;
107        i--;
108    }
109#else
110    volatile uint32_t *pdpin = base+0x10300;
111    volatile uint32_t *pddats = base+0x10314;
112    volatile uint32_t *pddatc = base+0x10318;
113    volatile uint32_t *icmr = base+0x1004;
114    volatile uint32_t *icmsr = base+0x1008;
115    volatile uint32_t *icmcr = base+0x100c;
116#if 0
117    atrf_slp_tr(dsc, 1);
118    while (i) {
119        if (*pdpin & 0x1000)
120            break;
121        i--;
122    }
123    atrf_slp_tr(dsc, 0);
124#endif
125    uint32_t old_icmr = *icmr;
126    *icmsr = 0xffffffff;
127
128    *pddats = 1 << 9;
129    *pddatc = 1 << 9;
130    do i--;
131    while (!(*pdpin & 0x1000));
132
133    *icmcr = ~old_icmr;
134#endif
135    return MAX_COUNT-i;
136}
137
138
139void atben_cleanup(struct atrf_dsc *dsc)
140{
141    *clkgr = old_clkgr;
142}
tools/atrf-xtal/atrf-xtal.c
1/*
2 * atrf-xtal/atrf-xtal.c - AT86RF230/1 crystal diagnostic utility
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 "atrf.h"
19
20#include "atrf-xtal.h"
21
22
23#define DEFAULT_SIZE 127
24#define DEFAULT_TRIM 8
25
26
27static void setup(struct atrf_dsc *dsc, int size, int trim)
28{
29#ifdef __mips__
30    atben_setup(dsc, size, trim);
31#else
32#error Only ATBEN is supported for now
33#endif
34}
35
36
37unsigned sample(struct atrf_dsc *dsc)
38{
39#ifdef __mips__
40    return atben_sample(dsc);
41#else
42#error Only ATBEN is supported for now
43#endif
44}
45
46
47void cleanup(struct atrf_dsc *dsc)
48{
49#ifdef __mips__
50    atben_cleanup(dsc);
51#else
52#error Only ATBEN is supported for now
53#endif
54}
55
56
57static int cmp(const void *a, const void *b)
58{
59    return *(unsigned *) a-*(unsigned *) b;
60}
61
62
63
64static void eval(unsigned *res, int rep)
65{
66    double sum = 0;
67    int n = 0;
68    int i;
69
70    qsort(res, rep, sizeof(*res), cmp);
71    if (rep < 8) {
72        printf("%u\n", res[rep >> 1]);
73        return;
74    }
75    for (i = rep/8; i != rep-rep/8; i++) {
76        sum += res[i];
77        n++;
78    }
79    printf("%f\n", (double) sum/n);
80}
81
82
83static void usage(const char *name)
84{
85    fprintf(stderr,
86"%s [-d] [-s size] [-t trim] [repetitions]\n"
87" -d instead of printing a mean value, dump all samples\n"
88" -s size payload size in bytes, 0-127 (default: %d bytes)\n"
89" -t trim trim capacitor setting, 0-15 (default: %d)\n"
90" repetitions number of measurements (default: 1)\n"
91  , name, DEFAULT_SIZE, DEFAULT_TRIM);
92    exit(1);
93}
94
95
96int main(int argc, char *const *argv)
97{
98    struct atrf_dsc *dsc;
99    int size = DEFAULT_SIZE;
100    int trim = DEFAULT_TRIM;
101    int rep = 1;
102    int dump = 0;
103    char *end;
104    unsigned *res;
105    int c, i;
106
107    while ((c = getopt(argc, argv, "ds:t:")) != EOF)
108        switch (c) {
109        case 'd':
110            dump = 1;
111            break;
112        case 's':
113            size = strtoul(optarg, &end, 0);
114            if (*end)
115                usage(*argv);
116            if (size > 127)
117                usage(*argv);
118            break;
119        case 't':
120            trim = strtoul(optarg, &end, 0);
121            if (*end)
122                usage(*argv);
123            if (trim > 15)
124                usage(*argv);
125            break;
126        default:
127            usage(*argv);
128        }
129
130    switch (argc-optind) {
131    case 0:
132        break;
133    case 1:
134        rep = strtoul(argv[optind], &end, 0);
135        if (*end)
136            usage(*argv);
137        break;
138    default:
139        usage(*argv);
140    }
141
142    res = malloc(rep*sizeof(*res));
143    if (!res) {
144        perror("malloc");
145        exit(1);
146    }
147
148    dsc = atrf_open();
149    if (!dsc)
150        return 1;
151
152    setup(dsc, size, trim);
153
154    for (i = 0; i != rep; i++)
155        res[i] = sample(dsc);
156
157    cleanup(dsc);
158
159    atrf_close(dsc);
160
161    if (dump) {
162        for (i = 0; i != rep; i++)
163            printf("%u\n", res[i]);
164        exit(0);
165    }
166
167    eval(res, rep);
168
169    return 0;
170}
tools/atrf-xtal/atrf-xtal.h
1/*
2 * atrf-xtal/atrf-xtal.h - AT86RF230/1 crystal diagnostic utility
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#ifndef ATRF_XTAL_H
15#define ATRF_XTAL_H
16
17#include "atrf.h"
18
19
20void atben_setup(struct atrf_dsc *dsc, int size, int trim);
21unsigned atben_sample(struct atrf_dsc *dsc);
22void atben_cleanup(struct atrf_dsc *dsc);
23
24#endif /* !ATRF_XTAL_H */

Archive Download the corresponding diff file



interactive