Date:2011-06-19 19:06:00 (8 years 5 months ago)
Author:Werner Almesberger
Commit:13f031be2593a6671d5197156ab9232c8e82f255
Message:tools/lib/: split non-SPI code from atusb.c in preparation for SPI-based driver

- atusb.c (atusb_error, atusb_clear_error, atusb_open, atusb_close,
atusb_reset, atusb_reset_rf, atusb_test_mode, atusb_slp_tr,
atusb_interrupt, atusb_set_clkm, atusb_dev_handle): moved to
atusb-common.c
- atusb-common.c (atusb_set_clkm): pass atusb_reg_write via
atusb_driver.reg_write instead
- atusb.c (FROM_DEV, TO_DEV, struct atusb_dsc): moved to atusb-common.h
- Makefile (OBJS): added atusb-common.o
Files: tools/lib/Makefile (1 diff)
tools/lib/atusb-common.c (1 diff)
tools/lib/atusb-common.h (1 diff)
tools/lib/atusb.c (2 diffs)

Change Details

tools/lib/Makefile
1515
1616include ../Makefile.common
1717
18OBJS_host = atusb.o usbopen.o
18OBJS_host = atusb.o atusb-common.o usbopen.o
1919OBJS_ben_jlime = atben.o
2020OBJS_ben_openwrt = atben.o
2121
tools/lib/atusb-common.c
1/*
2 * lib/atusb-common.c - ATUSB access functions shared by all ATUSB drivers
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 <stdint.h>
15#include <stdlib.h>
16#include <stdio.h>
17#include <usb.h>
18
19#include "atusb/ep0.h"
20#include "atusb/usb-ids.h"
21
22#include "usbopen.h"
23#include "driver.h"
24#include "atusb-common.h"
25
26
27/* ----- error handling ---------------------------------------------------- */
28
29
30int atusb_error(void *handle)
31{
32    struct atusb_dsc *dsc = handle;
33
34    return dsc->error;
35}
36
37
38int atusb_clear_error(void *handle)
39{
40    struct atusb_dsc *dsc = handle;
41    int ret;
42
43    ret = dsc->error;
44    dsc->error = 0;
45    return ret;
46}
47
48
49/* ----- open/close -------------------------------------------------------- */
50
51
52void *atusb_open(const char *arg)
53{
54    usb_dev_handle *dev;
55    struct atusb_dsc *dsc;
56
57    usb_unrestrict();
58    if (arg)
59        restrict_usb_path(arg);
60    dev = open_usb(USB_VENDOR, USB_PRODUCT);
61    if (!dev) {
62        fprintf(stderr, ":-(\n");
63        return NULL;
64    }
65
66    dsc = malloc(sizeof(*dsc));
67    if (!dsc) {
68        perror("malloc");
69        exit(1);
70    }
71
72    dsc->dev = dev;
73    dsc->error = 0;
74
75    return dsc;
76}
77
78
79void atusb_close(void *handle)
80{
81    /* to do */
82}
83
84
85/* ----- device mode ------------------------------------------------------- */
86
87
88void atusb_reset(void *handle)
89{
90    struct atusb_dsc *dsc = handle;
91    int res;
92
93    if (dsc->error)
94        return;
95
96    res =
97        usb_control_msg(dsc->dev, TO_DEV, ATUSB_RESET, 0, 0, NULL, 0, 1000);
98    if (res < 0) {
99        fprintf(stderr, "ATUSB_RESET: %d\n", res);
100        dsc->error = 1;
101    }
102}
103
104
105void atusb_reset_rf(void *handle)
106{
107    struct atusb_dsc *dsc = handle;
108    int res;
109
110    if (dsc->error)
111        return;
112
113    res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_RF_RESET, 0, 0, NULL,
114        0, 1000);
115    if (res < 0) {
116        fprintf(stderr, "ATUSB_RF_RESET: %d\n", res);
117        dsc->error = 1;
118    }
119}
120
121
122void atusb_test_mode(void *handle)
123{
124    struct atusb_dsc *dsc = handle;
125    int res;
126
127    if (dsc->error)
128        return;
129
130    res =
131        usb_control_msg(dsc->dev, TO_DEV, ATUSB_TEST, 0, 0, NULL, 0, 1000);
132    if (res < 0) {
133        fprintf(stderr, "ATUSB_TEST: %d\n", res);
134        dsc->error = 1;
135    }
136}
137
138
139/* ----- SLP_TR ------------------------------------------------------------ */
140
141
142void atusb_slp_tr(void *handle, int on, int pulse)
143{
144    struct atusb_dsc *dsc = handle;
145    int res;
146
147    if (dsc->error)
148        return;
149
150    if (!on || !pulse) {
151        fprintf(stderr,
152            "SLP_TR mode on=%d pulse=%d not supported\n", on, pulse);
153        return;
154    }
155
156    res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_SLP_TR, 0, 0, NULL, 0,
157        1000);
158    if (res < 0) {
159        fprintf(stderr, "ATUSB_SLP_TR: %d\n", res);
160        dsc->error = 1;
161    }
162}
163
164
165/* ----- RF interrupt ------------------------------------------------------ */
166
167
168int atusb_interrupt(void *handle)
169{
170    struct atusb_dsc *dsc = handle;
171    uint8_t buf;
172    int res;
173
174    if (dsc->error)
175        return -1;
176
177    res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_POLL_INT, 0, 0,
178        (void *) &buf, 1, 1000);
179    if (res < 0) {
180        fprintf(stderr, "ATUSB_POLL_INT: %d\n", res);
181        dsc->error = 1;
182        return -1;
183    }
184
185    return buf;
186}
187
188
189/* ----- CLKM handling ----------------------------------------------------- */
190
191
192/*
193 * ATmega32U2-based boards don't allow disabling CLKM, so we keep it at 8 MHz.
194 * We could accommodate a choice between 8 MHz and 16 MHz, but that's for
195 * later.
196 */
197
198int atusb_set_clkm(void *handle, int mhz)
199{
200    struct atusb_dsc *dsc = handle;
201    uint8_t ids[3];
202    int res;
203
204    if (dsc->error)
205        return 0;
206    res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_ID, 0, 0,
207        (void *) ids, 3, 1000);
208    if (res < 0) {
209        fprintf(stderr, "ATUSB_ID: %s\n", usb_strerror());
210        dsc->error = 1;
211        return 0;
212    }
213    switch (ids[2]) {
214    case HW_TYPE_100813:
215    case HW_TYPE_101216:
216        break;
217    case HW_TYPE_110131:
218        if (mhz == 0 || mhz == 8)
219            return 1;
220        fprintf(stderr, "this board only supports CLKM = 8 MHz\n");
221        return 0;
222    default:
223        fprintf(stderr,
224            "atusb_set_clkm: unknown hardware type 0x%02x\n", ids[2]);
225        return 0;
226    }
227    return atrf_set_clkm_generic(atusb_driver.reg_write, dsc, mhz);
228}
229
230
231/* ----- Driver-specific hacks --------------------------------------------- */
232
233
234void *atusb_dev_handle(void *handle)
235{
236    struct atusb_dsc *dsc = handle;
237
238    return dsc->dev;
239}
tools/lib/atusb-common.h
1/*
2 * lib/atusb-common.h - ATUSB access functions shared by all ATUSB drivers
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#ifndef ATUSB_COMMON_H
15#define ATUSB_COMMON_H
16
17
18#define FROM_DEV ATUSB_FROM_DEV(0)
19#define TO_DEV ATUSB_TO_DEV(0)
20
21
22struct atusb_dsc {
23    usb_dev_handle *dev;
24    int error;
25};
26
27
28int atusb_error(void *handle);
29int atusb_clear_error(void *handle);
30void *atusb_open(const char *arg);
31void atusb_close(void *handle);
32void atusb_reset(void *handle);
33void atusb_reset_rf(void *handle);
34void atusb_test_mode(void *handle);
35void atusb_slp_tr(void *handle, int on, int pulse);
36int atusb_interrupt(void *handle);
37int atusb_set_clkm(void *handle, int mhz);
38
39#endif /* !ATUSB_COMMON_H */
tools/lib/atusb.c
2121
2222#include "usbopen.h"
2323#include "driver.h"
24
25
26#define FROM_DEV ATUSB_FROM_DEV(0)
27#define TO_DEV ATUSB_TO_DEV(0)
28
29
30struct atusb_dsc {
31    usb_dev_handle *dev;
32    int error;
33};
34
35/* ----- error handling ---------------------------------------------------- */
36
37
38static int atusb_error(void *handle)
39{
40    struct atusb_dsc *dsc = handle;
41
42    return dsc->error;
43}
44
45
46static int atusb_clear_error(void *handle)
47{
48    struct atusb_dsc *dsc = handle;
49    int ret;
50
51    ret = dsc->error;
52    dsc->error = 0;
53    return ret;
54}
55
56
57/* ----- open/close -------------------------------------------------------- */
58
59
60static void *atusb_open(const char *arg)
61{
62    usb_dev_handle *dev;
63    struct atusb_dsc *dsc;
64
65    usb_unrestrict();
66    if (arg)
67        restrict_usb_path(arg);
68    dev = open_usb(USB_VENDOR, USB_PRODUCT);
69    if (!dev) {
70        fprintf(stderr, ":-(\n");
71        return NULL;
72    }
73
74    dsc = malloc(sizeof(*dsc));
75    if (!dsc) {
76        perror("malloc");
77        exit(1);
78    }
79
80    dsc->dev = dev;
81    dsc->error = 0;
82
83    return dsc;
84}
85
86
87static void atusb_close(void *handle)
88{
89    /* to do */
90}
91
92
93/* ----- device mode ------------------------------------------------------- */
94
95
96static void atusb_reset(void *handle)
97{
98    struct atusb_dsc *dsc = handle;
99    int res;
100
101    if (dsc->error)
102        return;
103
104    res =
105        usb_control_msg(dsc->dev, TO_DEV, ATUSB_RESET, 0, 0, NULL, 0, 1000);
106    if (res < 0) {
107        fprintf(stderr, "ATUSB_RESET: %d\n", res);
108        dsc->error = 1;
109    }
110}
111
112
113static void atusb_reset_rf(void *handle)
114{
115    struct atusb_dsc *dsc = handle;
116    int res;
117
118    if (dsc->error)
119        return;
120
121    res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_RF_RESET, 0, 0, NULL,
122        0, 1000);
123    if (res < 0) {
124        fprintf(stderr, "ATUSB_RF_RESET: %d\n", res);
125        dsc->error = 1;
126    }
127}
128
129
130static void atusb_test_mode(void *handle)
131{
132    struct atusb_dsc *dsc = handle;
133    int res;
134
135    if (dsc->error)
136        return;
137
138    res =
139        usb_control_msg(dsc->dev, TO_DEV, ATUSB_TEST, 0, 0, NULL, 0, 1000);
140    if (res < 0) {
141        fprintf(stderr, "ATUSB_TEST: %d\n", res);
142        dsc->error = 1;
143    }
144}
24#include "atusb-common.h"
14525
14626
14727/* ----- register access --------------------------------------------------- */
...... 
261141}
262142
263143
264/* ----- SLP_TR ------------------------------------------------------------ */
265
266
267static void atusb_slp_tr(void *handle, int on, int pulse)
268{
269    struct atusb_dsc *dsc = handle;
270    int res;
271
272    if (dsc->error)
273        return;
274
275    if (!on || !pulse) {
276        fprintf(stderr,
277            "SLP_TR mode on=%d pulse=%d not supported\n", on, pulse);
278        return;
279    }
280
281    res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_SLP_TR, 0, 0, NULL, 0,
282        1000);
283    if (res < 0) {
284        fprintf(stderr, "ATUSB_SLP_TR: %d\n", res);
285        dsc->error = 1;
286    }
287}
288
289
290/* ----- RF interrupt ------------------------------------------------------ */
291
292
293static int atusb_interrupt(void *handle)
294{
295    struct atusb_dsc *dsc = handle;
296    uint8_t buf;
297    int res;
298
299    if (dsc->error)
300        return -1;
301
302    res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_POLL_INT, 0, 0,
303        (void *) &buf, 1, 1000);
304    if (res < 0) {
305        fprintf(stderr, "ATUSB_POLL_INT: %d\n", res);
306        dsc->error = 1;
307        return -1;
308    }
309
310    return buf;
311}
312
313
314/* ----- CLKM handling ----------------------------------------------------- */
315
316
317/*
318 * ATmega32U2-based boards don't allow disabling CLKM, so we keep it at 8 MHz.
319 * We could accommodate a choice between 8 MHz and 16 MHz, but that's for
320 * later.
321 */
322
323static int atusb_set_clkm(void *handle, int mhz)
324{
325    struct atusb_dsc *dsc = handle;
326    uint8_t ids[3];
327    int res;
328
329    if (dsc->error)
330        return 0;
331    res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_ID, 0, 0,
332        (void *) ids, 3, 1000);
333    if (res < 0) {
334        fprintf(stderr, "ATUSB_ID: %s\n", usb_strerror());
335        dsc->error = 1;
336        return 0;
337    }
338    switch (ids[2]) {
339    case HW_TYPE_100813:
340    case HW_TYPE_101216:
341        break;
342    case HW_TYPE_110131:
343        if (mhz == 0 || mhz == 8)
344            return 1;
345        fprintf(stderr, "this board only supports CLKM = 8 MHz\n");
346        return 0;
347    default:
348        fprintf(stderr,
349            "atusb_set_clkm: unknown hardware type 0x%02x\n", ids[2]);
350        return 0;
351    }
352    return atrf_set_clkm_generic(atusb_reg_write, dsc, mhz);
353}
354
355
356/* ----- Driver-specific hacks --------------------------------------------- */
357
358
359void *atusb_dev_handle(void *handle)
360{
361    struct atusb_dsc *dsc = handle;
362
363    return dsc->dev;
364}
365
366
367144/* ----- driver interface -------------------------------------------------- */
368145
369146

Archive Download the corresponding diff file



interactive