Root/tools/atrf-id/atrf-id.c

Source at commit 1de233a7c7fb2eca6f61682805aec0dda195a307 created 6 years 4 months ago.
By Werner Almesberger, tools/: fix artf_at86rf23[01] (should be atrf...) typo
1/*
2 * atrf-id/atrf-id.c - Identify a ben-wpan AT86RF230 board
3 *
4 * Written 2010-2011, 2013 by Werner Almesberger
5 * Copyright 2010-2011, 2013 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#ifdef HAVE_USB
19#include <usb.h>
20#endif
21
22#include "at86rf230.h"
23#include "atusb/ep0.h"
24#include "atrf.h"
25
26
27#ifdef HAVE_USB
28
29#define FROM_DEV ATUSB_FROM_DEV(0)
30
31#define BUF_SIZE 256
32
33
34static int get_id(usb_dev_handle *dev, void *data, int size)
35{
36    int res;
37
38    res = usb_control_msg(dev, FROM_DEV, ATUSB_ID, 0, 0, data, size, 1000);
39    if (res < 0)
40        fprintf(stderr, "ATUSB_ID: %s\n", usb_strerror());
41    return res;
42}
43
44
45static int get_protocol(usb_dev_handle *dev,
46    uint8_t *major, uint8_t *minor, uint8_t *target)
47{
48    uint8_t ids[3];
49
50    if (get_id(dev, ids, 3) < 0)
51        return -1;
52    if (major)
53        *major = ids[0];
54    if (minor)
55        *minor = ids[1];
56    if (target)
57        *target = ids[2];
58
59    return 0;
60}
61 
62
63static int get_build(usb_dev_handle *dev, char *buf, size_t size)
64{
65    int res;
66
67    res = usb_control_msg(dev, FROM_DEV, ATUSB_BUILD, 0, 0, buf, size,
68        1000);
69    if (res < 0)
70        fprintf(stderr, "ATUSB_BUILD: %s\n", usb_strerror());
71    return res;
72}
73
74
75static void show_usb_info(struct atrf_dsc *dsc)
76{
77    usb_dev_handle *dev;
78    const struct usb_device *device;
79    uint8_t major, minor, target;
80    const char *mcu;
81    char buf[BUF_SIZE+1]; /* +1 for terminating \0 */
82    int len;
83
84    dev = atrf_usb_handle(dsc);
85    if (!dev)
86        return;
87    device = usb_device(dev);
88
89    printf("%04x:%04x ",
90        device->descriptor.idVendor, device->descriptor.idProduct);
91
92    if (get_protocol(dev, &major, &minor, &target) < 0)
93        exit(1);
94    switch (target) {
95    case HW_TYPE_100813:
96    case HW_TYPE_101216:
97        mcu = "C8051F326";
98        break;
99    case HW_TYPE_110131:
100        mcu = "ATmega32U2";
101        break;
102    default:
103        mcu = "???";
104    }
105    printf("protocol %u.%u hw %u (%s)\n", major, minor, target, mcu);
106
107    len = get_build(dev, buf, sizeof(buf)-1);
108    if (len < 0)
109        exit(1);
110    buf[len] = 0;
111    printf("%10s%s\n", "", buf);
112}
113
114
115#else /* HAVE_USB */
116
117
118static void show_usb_info(struct atrf_dsc *dsc)
119{
120}
121
122
123#endif /* !HAVE_USB */
124
125
126static void show_info(struct atrf_dsc *dsc)
127{
128    uint8_t part, version, man_id_0, man_id_1;
129
130    show_usb_info(dsc);
131
132    printf("%10s", "");
133
134    switch (atrf_identify(dsc)) {
135    case atrf_unknown_chip:
136        printf("???");
137        break;
138    case atrf_at86rf230:
139        printf("AT86RF230");
140        break;
141    case atrf_at86rf231:
142        printf("AT86RF231");
143        break;
144    default:
145        abort();
146    }
147
148    part = atrf_reg_read(dsc, REG_PART_NUM);
149    version = atrf_reg_read(dsc, REG_VERSION_NUM);
150    man_id_0 = atrf_reg_read(dsc, REG_MAN_ID_0);
151    man_id_1 = atrf_reg_read(dsc, REG_MAN_ID_1);
152    printf(", part 0x%02x version %u manufacturer xxxx%02x%02x",
153        part, version, man_id_1, man_id_0);
154
155    printf(" (%s)\n", man_id_1 == 0 && man_id_0 == 0x1f ? "Atmel" : "???");
156}
157
158
159static void usage(const char *name)
160{
161    fprintf(stderr,
162"usage: %s [-d driver[:arg]] [-s [-s]]\n\n"
163" -d driver[:arg] use the specified driver (default: %s)\n"
164" -s print only the local driver specification\n"
165" -s -s print only the remote driver specification\n"
166    , name, atrf_default_driver_name());
167    exit(1);
168}
169
170
171int main(int argc, char *const *argv)
172{
173    const char *driver = NULL;
174    struct atrf_dsc *dsc;
175    int spec_only = 0;
176    int c;
177
178    while ((c = getopt(argc, argv, "d:s")) != EOF)
179        switch (c) {
180        case 'd':
181            driver = optarg;
182            break;
183        case 's':
184            spec_only++;
185            break;
186        default:
187            usage(*argv);
188        }
189    if (argc != optind)
190        usage(*argv);
191
192    dsc = atrf_open(driver);
193    if (!dsc)
194        return 1;
195
196    if (spec_only) {
197        const char *spec = atrf_driver_spec(dsc, spec_only > 1);
198
199        if (spec)
200            printf("%s\n", spec);
201        else {
202            fprintf(stderr, "can't obtain specification\n");
203            exit(1);
204        }
205    } else {
206        show_info(dsc);
207    }
208
209    atrf_close(dsc);
210
211    return 0;
212}
213

Archive Download this file



interactive