Root/tools/atrf-trim/atrf-trim.c

Source at commit 624caeb00652f6118dc64698b7dad6fa3e5bdcc2 created 2 years 6 months ago.
By Werner Almesberger, atusb/: use VR, POWERED, and LED from kicad-libs
1/*
2 * atrf-trim/atrf-trim.c - AT86RF230 oscillator trim 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
14#include <stdlib.h>
15#include <stdio.h>
16#include <unistd.h>
17#include <sys/time.h>
18
19#include "at86rf230.h"
20#include "atrf.h"
21
22
23static void usage(const char *name)
24{
25    fprintf(stderr, "%s [-d driver[:arg]] [trim_value]\n", name);
26    exit(1);
27}
28
29
30int main(int argc, char *const *argv)
31{
32    const char *driver = NULL;
33    struct atrf_dsc *dsc;
34    int trim = -1;
35    char *end;
36    int c;
37
38    while ((c = getopt(argc, argv, "d:")) != EOF)
39        switch (c) {
40        case 'd':
41            driver = optarg;
42            break;
43        default:
44            usage(*argv);
45        }
46
47    switch (argc-optind) {
48    case 0:
49        break;
50    case 1:
51        trim = strtoul(argv[optind], &end, 0);
52        if (*end || trim > 15)
53            usage(*argv);
54        break;
55    default:
56        usage(*argv);
57    }
58
59    dsc = atrf_open(driver);
60    if (!dsc)
61        return 1;
62
63    if (trim == -1) {
64        trim = atrf_reg_read(dsc, REG_XOSC_CTRL) & XTAL_TRIM_MASK;
65        printf("%d (%d.%d pF)\n", trim, trim*3/10, trim*3 % 10);
66    } else {
67        atrf_reg_write(dsc, REG_XOSC_CTRL,
68            (XTAL_MODE_INT << XTAL_MODE_SHIFT) | trim);
69    }
70
71    return 0;
72}
73

Archive Download this file



interactive