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

Source at commit 8e842bab8efb0f6d40497e2f0ced4a41041ebd85 created 9 years 6 months ago.
By Werner Almesberger, atusb.brd: ground zone adjustments; corrected date code location
1/*
2 * atrf-id/atrf-id.c - Identify a ben-wpan AT86RF230 board
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
17#ifdef HAVE_USB
18#include <usb.h>
19#endif
20
21#include "at86rf230.h"
22#include "atusb/ep0.h"
23#include "atrf.h"
24
25
26#ifdef HAVE_USB
27
28#define FROM_DEV ATUSB_FROM_DEV(0)
29
30#define BUF_SIZE 256
31
32
33static int get_id(usb_dev_handle *dev, void *data, int size)
34{
35    int res;
36
37    res = usb_control_msg(dev, FROM_DEV, ATUSB_ID, 0, 0, data, size, 1000);
38    if (res < 0)
39        fprintf(stderr, "ATUSB_ID: %s\n", usb_strerror());
40    return res;
41}
42
43
44static int get_protocol(usb_dev_handle *dev,
45    uint8_t *major, uint8_t *minor, uint8_t *target)
46{
47    uint8_t ids[3];
48
49    if (get_id(dev, ids, 3) < 0)
50        return -1;
51    if (major)
52        *major = ids[0];
53    if (minor)
54        *minor = ids[1];
55    if (target)
56        *target = ids[2];
57
58    return 0;
59}
60 
61
62static int get_build(usb_dev_handle *dev, char *buf, size_t size)
63{
64    int res;
65
66    res = usb_control_msg(dev, FROM_DEV, ATUSB_BUILD, 0, 0, buf, size,
67        1000);
68    if (res < 0)
69        fprintf(stderr, "ATUSB_BUILD: %s\n", usb_strerror());
70    return res;
71}
72
73
74static void show_usb_info(struct atrf_dsc *dsc)
75{
76    usb_dev_handle *dev;
77    const struct usb_device *device;
78    uint8_t major, minor, target;
79    char buf[BUF_SIZE+1]; /* +1 for terminating \0 */
80    int len;
81
82    dev = atrf_usb_handle(dsc);
83    if (!dev)
84        return;
85    device = usb_device(dev);
86
87    printf("%04x:%04x ",
88        device->descriptor.idVendor, device->descriptor.idProduct);
89
90    if (get_protocol(dev, &major, &minor, &target) < 0)
91        exit(1);
92    printf("protocol %u.%u hw %u\n", major, minor, target);
93
94    len = get_build(dev, buf, sizeof(buf)-1);
95    if (len < 0)
96        exit(1);
97    buf[len] = 0;
98    printf("%10s%s\n", "", buf);
99}
100
101
102#else /* HAVE_USB */
103
104
105static void show_usb_info(struct atrf_dsc *dsc)
106{
107}
108
109
110#endif /* !HAVE_USB */
111
112
113static void show_info(struct atrf_dsc *dsc)
114{
115    uint8_t part, version, man_id_0, man_id_1;
116
117    show_usb_info(dsc);
118
119    printf("%10s", "");
120
121    switch (atrf_identify(dsc)) {
122    case atrf_unknown_chip:
123        printf("???");
124        break;
125    case artf_at86rf230:
126        printf("AT86RF230");
127        break;
128    case artf_at86rf231:
129        printf("AT86RF231");
130        break;
131    default:
132        abort();
133    }
134
135    part = atrf_reg_read(dsc, REG_PART_NUM);
136    version = atrf_reg_read(dsc, REG_VERSION_NUM);
137    man_id_0 = atrf_reg_read(dsc, REG_MAN_ID_0);
138    man_id_1 = atrf_reg_read(dsc, REG_MAN_ID_1);
139    printf(", part 0x%02x version %u manufacturer xxxx%02x%02x",
140        part, version, man_id_1, man_id_0);
141
142    printf(" (%s)\n", man_id_1 == 0 && man_id_0 == 0x1f ? "Atmel" : "???");
143}
144
145
146static void usage(const char *name)
147{
148    fprintf(stderr, "usage: %s\n", name);
149    exit(1);
150}
151
152
153int main(int argc, const char **argv)
154{
155    struct atrf_dsc *dsc;
156
157    if (argc != 1)
158        usage(*argv);
159    dsc = atrf_open();
160    if (!dsc)
161        return 1;
162
163    show_info(dsc);
164
165    atrf_close(dsc);
166
167    return 0;
168}
169

Archive Download this file



interactive