Date:2010-08-23 18:39:07 (13 years 7 months ago)
Author:Werner Almesberger
Commit:c2dd23840ce5875deca09673fcb8abb8dc3a8e7a
Message:Moved fw/ and tools/ into atrf/ as well.

- fw/: moved to atrf/fw/
- tools/: moved to atrf/tools/
- atrf/fw/atspi/Makefile, atrf/fw/boot/Makefile: updated relative path to
f32xbase
- atrf/tools/Makefile: updated relative path to Makefile.recurse
- atrf/tools/atspi-id/Makefile, atrf/tools/atspi-reset/Makefile,
atrf/tools/atspi-rssi/Makefile, atrf/tools/lib/Makefile: updated relative
path to f32xbase
- atrf/fw/include/at86rf230.h: corrected path in title
- atrf/tools/include/atspi.h: added title and copyright header
Files: atrf/fw/Makefile (1 diff)
atrf/fw/atspi/Makefile (1 diff)
atrf/fw/atspi/atspi.c (1 diff)
atrf/fw/atspi/descr.c (1 diff)
atrf/fw/atspi/ep0.c (1 diff)
atrf/fw/boot/Makefile (1 diff)
atrf/fw/common/Makefile (1 diff)
atrf/fw/common/config.h (1 diff)
atrf/fw/common/io.h (1 diff)
atrf/fw/include/at86rf230.h (1 diff)
atrf/fw/include/atspi/ep0.h (1 diff)
atrf/fw/include/atspi/usb-ids.h (1 diff)
atrf/tools/Makefile (1 diff)
atrf/tools/atspi-id/Makefile (1 diff)
atrf/tools/atspi-id/atspi-id.c (1 diff)
atrf/tools/atspi-reset/Makefile (1 diff)
atrf/tools/atspi-reset/atspi-reset.c (1 diff)
atrf/tools/atspi-rssi/Makefile (1 diff)
atrf/tools/atspi-rssi/atspi-rssi.c (1 diff)
atrf/tools/atspi-txrx/Makefile (1 diff)
atrf/tools/atspi-txrx/atspi-txrx.c (1 diff)
atrf/tools/include/atspi.h (1 diff)
atrf/tools/lib/Makefile (1 diff)
atrf/tools/lib/atspi.c (1 diff)
fw/Makefile (1 diff)
fw/atspi-boot/Makefile (1 diff)
fw/atspi/Makefile (1 diff)
fw/atspi/atspi.c (1 diff)
fw/atspi/descr.c (1 diff)
fw/atspi/ep0.c (1 diff)
fw/common/Makefile (1 diff)
fw/common/config.h (1 diff)
fw/common/io.h (1 diff)
fw/include/at86rf230.h (1 diff)
fw/include/atspi/ep0.h (1 diff)
fw/include/atspi/usb-ids.h (1 diff)
tools/Makefile (1 diff)
tools/atspi-id/Makefile (1 diff)
tools/atspi-id/atspi-id.c (1 diff)
tools/atspi-reset/Makefile (1 diff)
tools/atspi-reset/atspi-reset.c (1 diff)
tools/atspi-rssi/Makefile (1 diff)
tools/atspi-rssi/atspi-rssi.c (1 diff)
tools/atspi-txrx/Makefile (1 diff)
tools/atspi-txrx/atspi-txrx.c (1 diff)
tools/include/atspi.h (1 diff)
tools/lib/Makefile (1 diff)
tools/lib/atspi.c (1 diff)

Change Details

atrf/fw/Makefile
1#
2# fw/Makefile - ATSPI firmware build
3#
4# Written 2008-2010 by Werner Almesberger
5# Copyright 2008-2010 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
14DIRS=common boot atspi
15
16
17.PHONY: all depend install uninstall clean spotless
18
19all:
20        for d in $(DIRS); do make -C $$d all || exit 1; done
21
22depend:
23        for d in $(DIRS); do make -C $$d depend || exit 1; done
24
25# Top-level Makefile recurses for "install" and "uninstall", which have no use
26# here. Just ignore them.
27
28install:
29
30uninstall:
31
32clean:
33        for d in $(DIRS); do make -C $$d clean || exit 1; done
34
35spotless:
36        for d in $(DIRS); do make -C $$d spotless || exit 1; done
atrf/fw/atspi/Makefile
1#
2# atspi/Makefile - Makefile for USB to SPI translator (for AT86RF230)
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14MAIN = atspi
15OBJS = $(MAIN) usb descr version ep0
16
17F32XBASE = ../../../../f32xbase
18
19include $(F32XBASE)/fw/common/Makefile.system
20include $(F32XBASE)/fw/common/Makefile.common
21
22CFLAGS += -I../common -I../include
23LDFLAGS += --code-size $(PAYLOAD_SIZE) --code-loc $(PAYLOAD_START)
24
25USB_ID = $(shell \
26  ( echo '\#include "config.h"'; echo USB_VENDOR:USB_PRODUCT; ) | \
27  cpp -I../common -I../include | sed '/^ *$$/d;/^\#/d' )
28
29.PHONY: dfu
30
31dfu:
32        dfu-util -d $(USB_ID) -D $(MAIN).bin
atrf/fw/atspi/atspi.c
1/*
2 * atspi/atspi.c - ATSPI initialization and main loop
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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 "regs.h"
15#include "io.h"
16#include "usb.h"
17#include "atspi/ep0.h"
18#include "version.h"
19
20
21void reset_rf(void)
22{
23    int i;
24
25    nRST_RF = 0;
26    /*
27     * 11.4.12: min 625 ns.
28     * The pulse we generate is slightly longer than 4 us.
29     */
30    for (i = 0; i != 10; i++);
31    nRST_RF = 1;
32}
33
34
35static void init_io(void)
36{
37    /*
38     * Signal Mode Value
39     *
40     * MOSI push-pull 0
41     * MISO open drain 1 (input)
42     * SCLK push-pull 0
43     * nSS push-pull 1
44     * nRST_RF push-pull 1
45     * IRQ_RF open drain 1 (input)
46     * SLP_TR push-pull 0
47     *
48     * LED push-pull 0 (set up by boot loader)
49     *
50     * all unused open drain 0
51     */
52
53    MOSI = 0;
54    MOSI_MODE |= 1 << MOSI_BIT;
55
56    SCLK = 0;
57    SCLK_MODE |= 1 << SCLK_BIT;
58
59    nSS_MODE |= 1 << nSS_BIT;
60
61    nRST_RF_MODE |= 1 << nRST_RF_BIT;
62
63    SLP_TR = 0;
64    SLP_TR_MODE |= 1 << SLP_TR_BIT;
65
66    P0 &=
67        ~((1 << 0) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
68        /* change 1 << 0 to 1 << 2 once 100813 boards are reworked */
69    P3 = 0;
70
71#if 0
72    /*
73     * We can *almost* disable the pull-ups. The only obstacle is that
74     * MISO is not driven when not in use. So we either need an external
75     * pull-up/down or keep all the pull-ups on.
76     */
77
78    /*
79     * Disable pull-ups
80     */
81    GPIOCN |= WEAKPUD;
82#endif
83
84    /*
85     * The manual says the reset is optional, but reality disagrees with
86     * this optimistic assessment quite violently.
87     */
88
89    reset_rf();
90}
91
92
93void main(void)
94{
95    init_io();
96
97    usb_init();
98    ep0_init();
99
100    while (1) {
101        usb_poll();
102    }
103}
atrf/fw/atspi/descr.c
1/*
2 * atspi/descr.c - USB descriptors
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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 "usb.h"
15
16#include "config.h"
17
18
19/*
20 * Device descriptor
21 */
22
23const uint8_t device_descriptor[18] = {
24    18, /* bLength */
25    USB_DT_DEVICE, /* bDescriptorType */
26    LE(0x200), /* bcdUSB */
27    USB_CLASS_VENDOR_SPEC, /* bDeviceClass */
28    0x00, /* bDeviceSubClass */
29    0x00, /* bDeviceProtocol */
30    EP0_SIZE, /* bMaxPacketSize */
31    LE(USB_VENDOR), /* idVendor */
32    LE(USB_PRODUCT), /* idProduct */
33    LE(0x0001), /* bcdDevice */
34    0, /* iManufacturer */
35    0, /* iProduct */
36    0, /* iSerialNumber */
37    1 /* bNumConfigurations */
38};
39
40
41/*
42 * Our configuration
43 *
44 * While in DFU mode, we're always bus-powered.
45 */
46
47const uint8_t config_descriptor[] = {
48    9, /* bLength */
49    USB_DT_CONFIG, /* bDescriptorType */
50    LE(9+9+7+7), /* wTotalLength */
51    1, /* bNumInterfaces */
52    1, /* bConfigurationValue (> 0 !) */
53    0, /* iConfiguration */
54// USB_ATTR_SELF_POWERED | USB_ATTR_BUS_POWERED,
55    USB_ATTR_BUS_POWERED, /* bmAttributes */
56    15, /* bMaxPower */
57
58    /* Interface #0 */
59
60    9, /* bLength */
61    USB_DT_INTERFACE, /* bDescriptorType */
62    0, /* bInterfaceNumber */
63    0, /* bAlternateSetting */
64    2, /* bNumEndpoints */
65    USB_CLASS_VENDOR_SPEC, /* bInterfaceClass */
66    0, /* bInterfaceSubClass */
67    0, /* bInterfaceProtocol */
68    0, /* iInterface */
69
70    /* EP OUT */
71
72    7, /* bLength */
73    USB_DT_ENDPOINT, /* bDescriptorType */
74    0x01, /* bEndPointAddress */
75    0x02, /* bmAttributes (bulk) */
76    LE(EP1_SIZE), /* wMaxPacketSize */
77    0, /* bInterval */
78
79    /* EP IN */
80
81    7, /* bLength */
82    USB_DT_ENDPOINT, /* bDescriptorType */
83    0x81, /* bEndPointAddress */
84    0x02, /* bmAttributes (bulk) */
85    LE(EP1_SIZE), /* wMaxPacketSize */
86    0, /* bInterval */
87
88};
atrf/fw/atspi/ep0.c
1/*
2 * atspi/ep0.c - EP0 extension protocol
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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
16#ifndef NULL
17#define NULL 0
18#endif
19
20#include "regs.h"
21//#include "uart.h"
22#include "usb.h"
23#include "atspi/ep0.h"
24#include "at86rf230.h"
25#include "version.h"
26
27
28extern void reset_rf(void);
29
30
31#define debug(...)
32#define error(...)
33
34
35/*
36 * SDCC 2.8.0 had a number of code generation bugs that appeared in the big
37 * switch statement of my_setup. SDCC_FORCE_UPDATE forced the value of the
38 * "size" variable to be written to memory. This work-around doesn't seem
39 * to be necessary with 2.9.0, but we keep it around, just in case.
40 *
41 * Unfortunately, the setup->bRequest garbling bug is still with us. Without
42 * the evaluation forced with SDCC_FORCE_EVAL, sdcc gets confused about the
43 * value of setup->bRequest and then rejects all SETUP requests.
44 */
45
46#define SDCC_FORCE_EVAL(type, value) \
47    do { \
48    static volatile type foo; \
49    foo = value; \
50    } while (0)
51
52#define SDCC_FORCE_UPDATE(type, var) \
53    do { \
54    volatile type foo; \
55    foo = var; \
56    var = foo; \
57    } while (0)
58
59
60static const uint8_t id[] = { EP0ATSPI_MAJOR, EP0ATSPI_MINOR, HW_TYPE };
61static __xdata uint8_t buf[MAX_PSDU+3]; /* command, PHDR, and LQ */
62static uint8_t size;
63
64
65static void spi_send(uint8_t v)
66{
67    uint8_t mask;
68
69    for (mask = 0x80; mask; mask >>= 1) {
70        MOSI = !!(v & mask);
71        SCLK = 1;
72        SCLK = 0;
73    }
74}
75
76
77static uint8_t spi_recv(void)
78{
79    uint8_t res = 0;
80    uint8_t i;
81
82    for (i = 0; i != 8; i++) {
83        res = (res << 1) | MISO;
84        SCLK = 1;
85        SCLK = 0;
86    }
87    return res;
88}
89
90
91static void do_buf_write(void *user)
92{
93    uint8_t i;
94
95    user; /* suppress warning */
96    nSS = 0;
97    spi_send(AT86RF230_BUF_WRITE);
98    for (i = 0; i != size; i++)
99        spi_send(buf[i]);
100    nSS = 1;
101}
102
103
104#define BUILD_OFFSET 7 /* '#' plus "65535" plus ' ' */
105
106
107static __bit my_setup(struct setup_request *setup) __reentrant
108{
109    unsigned tmp;
110    uint8_t i;
111
112    switch (setup->bmRequestType | setup->bRequest << 8) {
113    case ATSPI_FROM_DEV(ATSPI_ID):
114        debug("ATSPI_ID\n");
115        if (setup->wLength > 3)
116            return 0;
117        usb_send(&ep0, id, setup->wLength, NULL, NULL);
118        return 1;
119    case ATSPI_FROM_DEV(ATSPI_BUILD):
120        debug("ATSPI_BUILD\n");
121        tmp = build_number;
122        for (i = BUILD_OFFSET-2; tmp; i--) {
123            buf[i] = (tmp % 10)+'0';
124            tmp /= 10;
125        }
126        buf[i] = '#';
127        buf[BUILD_OFFSET-1] = ' ';
128        for (size = 0; build_date[size]; size++)
129            buf[BUILD_OFFSET+size] = build_date[size];
130        size += BUILD_OFFSET-i+1;
131        SDCC_FORCE_EVAL(uint8_t, setup->bRequest);
132        if (size > setup->wLength)
133            return 0;
134        usb_send(&ep0, buf+i, size, NULL, NULL);
135        return 1;
136
137    case ATSPI_TO_DEV(ATSPI_RESET):
138        debug("ATSPI_RESET\n");
139        RSTSRC = SWRSF;
140        while (1);
141
142    case ATSPI_TO_DEV(ATSPI_RF_RESET):
143        debug("ATSPI_RF_RESET\n");
144        reset_rf();
145        return 1;
146
147    case ATSPI_TO_DEV(ATSPI_REG_WRITE):
148        debug("ATSPI_REG_WRITE\n");
149        nSS = 0;
150        spi_send(AT86RF230_REG_WRITE | setup->wIndex);
151        spi_send(setup->wValue);
152        nSS = 1;
153        return 1;
154    case ATSPI_FROM_DEV(ATSPI_REG_READ):
155        debug("ATSPI_REG_READ\n");
156        nSS = 0;
157        spi_send(AT86RF230_REG_READ | setup->wIndex);
158        *buf = spi_recv();
159        nSS = 1;
160        usb_send(&ep0, buf, 1, NULL, NULL);
161        return 1;
162
163    case ATSPI_TO_DEV(ATSPI_BUF_WRITE):
164        debug("ATSPI_BUF_WRITE\n");
165        if (setup->wLength < 1)
166            return 0;
167        if (setup->wLength > MAX_PSDU+1) /* PHR+PSDU */
168            return 0;
169        buf[0] = AT86RF230_BUF_WRITE;
170        size = setup->wLength+1;
171        usb_recv(&ep0, buf+1, setup->wLength, do_buf_write, NULL);
172        return 1;
173    case ATSPI_TO_DEV(ATSPI_BUF_READ):
174        debug("ATSPI_BUF_READ\n");
175        if (setup->wLength < 2) /* PHR+LQ */
176            return 0;
177        if (setup->wLength > MAX_PSDU+2) /* PHR+PSDU+LQ */
178            return 0;
179        nSS = 0;
180        spi_send(AT86RF230_BUF_READ);
181        size = *buf = spi_recv();
182        if (size+2 > setup->wLength)
183            size = setup->wLength-2;
184        for (i = 0; i != size+1; i++)
185            buf[i+1] = spi_recv();
186        nSS = 1;
187        usb_send(&ep0, buf, size+1, NULL, NULL);
188        return 1;
189
190    case ATSPI_TO_DEV(ATSPI_SRAM_WRITE):
191        debug("ATSPI_SRAM_WRITE\n");
192        if (setup->wIndex > SRAM_SIZE)
193            return 0;
194        if (setup->wIndex+setup->wLength > SRAM_SIZE)
195            return 0;
196        buf[0] = AT86RF230_SRAM_WRITE;
197        buf[1] = setup->wIndex;
198        size = setup->wLength+2;
199        usb_recv(&ep0, buf+2, setup->wLength, do_buf_write, NULL);
200        return 1;
201    case ATSPI_TO_DEV(ATSPI_SRAM_READ):
202        debug("ATSPI_SRAM_READ\n");
203        if (setup->wIndex > SRAM_SIZE)
204            return 0;
205        if (setup->wIndex+setup->wLength > SRAM_SIZE)
206            return 0;
207        nSS = 0;
208        spi_send(AT86RF230_SRAM_READ);
209        spi_send(setup->wIndex);
210        for (i = 0; i != size; i++)
211            buf[i] = spi_recv();
212        nSS = 1;
213        usb_send(&ep0, buf, size, NULL, NULL);
214        return 1;
215
216    default:
217        error("Unrecognized SETUP: 0x%02x 0x%02x ...\n",
218            setup->bmRequestType, setup->bRequest);
219        return 0;
220    }
221}
222
223
224void ep0_init(void)
225{
226    user_setup = my_setup;
227}
atrf/fw/boot/Makefile
1#
2# boot/Makefile - Makefile for DFU-capable boot loader for ATSPI
3#
4# Written 2008, 2010 by Werner Almesberger
5# Copyright 2008, 2010 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
13MAIN=boot
14OBJS=$(MAIN) usb dfu version
15
16F32XBASE = ../../../../f32xbase
17
18include $(F32XBASE)/fw/common/Makefile.system
19include $(F32XBASE)/fw/common/Makefile.common
20
21CFLAGS += -I../common -I../include
22LDFLAGS += --code-size $(PAYLOAD_START)
atrf/fw/common/Makefile
1#
2# common/Makefile - Makefile for shared items
3#
4# Written 2008, 2010 by Werner Almesberger
5# Copyright 2008, 2010 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
14GEN_quiet = @echo " GENERATE " $@ &&
15
16ifeq ($(V),1)
17    GEN =
18else
19    GEN = $(GEN_quiet)
20endif
21
22
23.PHONY: all depend clean spotless
24
25all: io-parts.h
26
27io-parts.h: io.h Makefile
28        $(GEN) $(CPP) -dD $< | \
29          sed '1,/IO_H/d' | \
30          awk \
31          'BEGIN { print "/* MACHINE-GENERATED. DO NOT EDIT ! */"; \
32            print "#ifndef IO_PARTS_H"; \
33            print "#define IO_PARTS_H"; } \
34           /#define/ && $$3 != "" { \
35            split($$3, a, "_"); \
36            print $$1, $$2 "_PORT", a[1]; \
37            print $$1, $$2 "_MODE", a[1] "MDOUT"; \
38            print $$1, $$2 "_BIT", a[2]; } \
39           END { print "#endif" }' >$@ || \
40                { rm -f $@; exit 1; }
41
42
43#
44# When we do a global "make depend", we'll come here first. So we create
45# io-parts.h so that "make depend" in the other directories can pick it up.
46#
47
48depend: io-parts.h
49
50clean:
51        rm -f io-parts.h
52
53spotless: clean
atrf/fw/common/config.h
1/*
2 * common/config.h - Configuration data for boot loader and application
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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#ifndef CONFIG_H
14#define CONFIG_H
15
16#include "io.h"
17#include "io-parts.h"
18#include "atspi/usb-ids.h"
19
20
21/* ----- Boot loader configuration ----------------------------------------- */
22
23/*
24 * Make LED output push-pull so that we can output a high voltage.
25 * This turns on the LED, to indicate that we're in the boot loader.
26 */
27
28#define PLATFORM_SETUP \
29    LED_MODE |= 1 << LED_BIT;
30
31
32/*
33 * Turn off the LED when we exit the boot loader.
34 */
35
36#define PLATFORM_EXIT \
37    LED = 0
38
39
40/* ----- Application configuration ----------------------------------------- */
41
42#define HW_TYPE HW_TYPE_100813
43
44#endif /* !CONFIG_H */
atrf/fw/common/io.h
1/*
2 * common/io.h - I/O pin assignment
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 IO_H
15#define IO_H
16
17/* Diagnostic LED */
18
19#define LED P0_1
20
21/* SPI */
22
23#define MOSI P2_2
24#define MISO P2_5
25#define SCLK P2_4
26#define nSS P2_3
27
28/* Miscellaneous RF signals */
29
30#define nRST_RF P2_0
31#define IRQ_RF P0_2 /* change to P0_0 when 100813 board are reworked */
32#define SLP_TR P2_1
33
34#endif /* !IO_H */
atrf/fw/include/at86rf230.h
1/*
2 * include/at86rf230.h - AT86RF230 protocol and register definitions
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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 AT86RF230_H
15#define AT86RF230_H
16
17enum {
18    AT86RF230_REG_WRITE = 0xc0, /* 11... */
19    AT86RF230_REG_READ = 0x80, /* 10... */
20    AT86RF230_BUF_WRITE = 0x60, /* 011... */
21    AT86RF230_BUF_READ = 0x20, /* 001... */
22    AT86RF230_SRAM_WRITE = 0x40, /* 010... */
23    AT86RF230_SRAM_READ = 0x00 /* 000... */
24};
25
26#define MAX_PSDU 127 /* octets, see AT86RF230 manual section 8.1 */
27#define SRAM_SIZE 128
28
29
30/* --- Registers ----------------------------------------------------------- */
31
32enum {
33    REG_TRX_STATUS = 0x01,
34    REG_TRX_STATE = 0x02,
35    REG_TRX_CTRL_0 = 0x03,
36
37    REG_TR_PWR = 0x05,
38    REG_PHY_RSSI = 0x06,
39    REG_PHY_ED_LEVEL = 0x07,
40    REG_PHY_CC_CCA = 0x08,
41    REG_CCA_THRES = 0x09,
42
43    REG_IRQ_MASK = 0x0e,
44    REG_IRQ_STATUS = 0x0f,
45    REG_VREG_CTRL = 0x10,
46    REG_BATMON = 0x10,
47    REG_XOSC_CTRL = 0x12,
48
49    REG_PLL_CF = 0x1a,
50    REL_PLL_DCU = 0x1b,
51    REG_PART_NUM = 0x1c,
52    REG_VERSION_NUM = 0x1d,
53    REG_MAN_ID_0 = 0x1e,
54    REG_MAN_ID_1 = 0x1f,
55    REG_SHORT_ADDR_0 = 0x20,
56    REG_SHORT_ADDR_1 = 0x21,
57    REG_PAN_ID_0 = 0x22,
58    REG_PAN_ID_1 = 0x23,
59    REG_IEEE_ADDR_0 = 0x24,
60    REG_IEEE_ADDR_1 = 0x25,
61    REG_IEEE_ADDR_2 = 0x26,
62    REG_IEEE_ADDR_3 = 0x27,
63    REG_IEEE_ADDR_4 = 0x28,
64    REG_IEEE_ADDR_5 = 0x29,
65    REG_IEEE_ADDR_6 = 0x2a,
66    REG_IEEE_ADDR_7 = 0x2b,
67    REG_XAH_CTRL = 0x2c,
68    REG_CSMA_SEED_0 = 0x2d,
69    REG_CSMA_SEED_1 = 0x2e,
70};
71
72/* --- TRX_STATUS [7] ------------------------------------------------------ */
73
74#define CCA_DONE (1 << 7)
75
76/* --- TRX_STATUS [6] ------------------------------------------------------ */
77
78#define CCA_STATUS (1 << 6)
79
80/* --- TRX_STATUS [4:0] ---------------------------------------------------- */
81
82#define TRX_STATUS_SHIFT 0
83#define TRX_STATUS_MASK 0x0f
84
85enum {
86    TRX_STATUS_P_ON = 0x00, /* reset default */
87    TRX_STATUS_BUSY_RX = 0x01,
88    TRX_STATUS_BUSY_TX = 0x02,
89    TRX_STATUS_RX_ON = 0x06,
90    TRX_STATUS_TRX_OFF = 0x08,
91    TRX_STATUS_PLL_ON = 0x09,
92    TRX_STATUS_SLEEP = 0x0f,
93    TRX_STATUS_BUSY_RX_AACK = 0x11,
94    TRX_STATUS_BUSY_TX_ARET = 0x12,
95    TRX_STATUS_RX_AACK_ON = 0x16,
96    TRX_STATUS_TX_ARET_ON = 0x19,
97    TRX_STATUS_RX_ON_NOCLK = 0x1c,
98    TRX_STATUS_RX_AACK_ON_NOCLK = 0x1d,
99    TRX_STATUS_BUSY_RX_AACK_NOCLK = 0x1e,
100    TRX_STATUS_TRANSITION = 0x1f
101};
102
103/* --- TRX_STATE [7:5] ----------------------------------------------------- */
104
105#define TRAC_STATUS_SHIFT 5
106#define TRAC_STATUS_MASK 7
107
108enum {
109    TRAC_STATUS_SUCCESS = 0, /* reset default */
110    TRAC_STATUS_SUCCESS_DATA_PENDING = 1,
111    TRAC_STATUS_CHANNEL_ACCESS_FAILURE = 3,
112    TRAC_STATUS_NO_ACK = 5,
113    TRAC_STATUS_INVALID = 7
114};
115
116/* --- TRX_CMD [4:0] ----------------------------------------------------- */
117
118#define TRX_CMD_SHIFT 0
119#define TRX_CMD_MASK 7
120
121enum {
122    TRX_CMD_NOP = 0x00, /* reset default */
123    TRX_CMD_TX_START = 0x02,
124    TRX_CMD_FORCE_TRX_OFF = 0x03,
125    TRX_CMD_RX_ON = 0x06,
126    TRX_CMD_TRX_OFF = 0x08,
127    TRX_CMD_PLL_ON = 0x09,
128    TRX_CMD_RX_AACK_ON = 0x16,
129    TRX_CMD_TX_ARET_ON = 0x19,
130};
131
132/* --- TRX_CTRL_0 [7:6] ---------------------------------------------------- */
133
134#define PAD_IO_SHIFT 6
135#define PAD_IO_MASK 3
136
137enum {
138    PAD_IO_2mA, /* reset default */
139    PAD_IO_4mA,
140    PAD_IO_6mA,
141    PAD_IO_8mA
142};
143
144/* --- TRX_CTRL_0 [5:4] ---------------------------------------------------- */
145
146#define PAD_IO_CLKM_SHIFT 4
147#define PAD_IO_CLKM_MASK 3
148
149enum {
150    PAD_IO_CLKM_2mA,
151    PAD_IO_CLKM_4mA, /* reset default */
152    PAD_IO_CLKM_5mA,
153    PAD_IO_CLKM_8mA,
154};
155
156/* --- TRX_CTRL_0 [3] ------------------------------------------------------ */
157
158#define CLKM_SHA_SEL (1 << 3)
159
160/* --- TRX_CTRL_0 [2:0] ---------------------------------------------------- */
161
162#define CLKM_CTRL_SHIFT 0
163#define CLKM_CTRL_MASK 3
164
165enum {
166    CLKM_CTRL_OFF = 0,
167    CLKM_CTRL_1MHz = 1, /* reset default */
168    CLKM_CTRL_2MHz = 2,
169    CLKM_CTRL_4MHz = 3,
170    CLKM_CTRL_8MHz = 4,
171    CLKM_CTRL_16MHz = 5
172};
173
174/* --- PHY_TX_PWR [7] ------------------------------------------------------ */
175
176#define TX_AUTO_CRC_ON (1 << 7)
177
178/* --- PHY_TX_PWR [3:0] ---------------------------------------------------- */
179
180#define TX_PWR_SHIFT 0
181#define TX_PWR_MASK 0x0f
182
183/* --- PHY_RSSI [7] -------------------------------------------------------- */
184
185#define RX_CRC_VALID (1 << 7)
186
187/* --- PHY_RSSI [4:0] ------------------------------------------------------ */
188
189#define RSSI_SHIFT 0
190#define RSSI_MASK 0x1f
191
192/* --- PHY_CC_CCA [7] ------------------------------------------------------ */
193
194#define CCA_REQUEST (1 << 7)
195
196/* --- PHY_CC_CCA [6:5] ---------------------------------------------------- */
197
198#define CCA_MODE_SHIFT 5
199#define CCA_MODE_MASK 3
200
201/* --- PHY_CC_CCA [4:0] ---------------------------------------------------- */
202
203#define CHANNEL_SHIFT 0
204#define CHANNEL_MASK 0x1f
205
206/* --- CCA_THRES [3:0] ----------------------------------------------------- */
207
208#define CCA_ED_THRES_SHIFT 0
209#define CCA_ED_THRES_MASK 0x0f
210
211/* --- IRQ_MASK/IRQ_STATUS ------------------------------------------------- */
212
213enum {
214    IRQ_PLL_LOCK = 1 << 0,
215    IRQ_PLL_UNLOCK = 1 << 1,
216    IRQ_RX_START = 1 << 2,
217    IRQ_TRX_END = 1 << 3,
218    IRQ_TRX_UR = 1 << 6,
219    IRQ_BAT_LOW = 1 << 7
220};
221
222/* --- VREG_CTRL [7, 6, 3, 2] ---------------------------------------------- */
223
224#define AVREG_EXT (1 << 7)
225#define AVDD_OK (1 << 6)
226#define DVREG_EXT (1 << 3)
227#define DVDD_OK (1 << 2)
228
229/* --- BATMON [5, 4] ------------------------------------------------------- */
230
231#define BATMON_OK (1 << 5)
232#define BATMON_HR (1 << 4)
233
234/* --- BATMON [3:0] -------------------------------------------------------- */
235
236#define NATMON_VTH_SHIFT 0
237#define NATMON_VTH_MASK 0x0f
238
239/* --- XOSC_CTRL [7:4] ----------------------------------------------------- */
240
241#define XTAL_MODE_SHIFT 4
242#define XTAL_MODE_MASK 0x0f
243
244enum {
245    XTAL_MODE_OFF = 0x0,
246    XTAL_MODE_EXT = 0x4,
247    XTAL_MODE_INT = 0xf /* reset default */
248};
249
250/* --- XOSC_CTRL [3:1] ----------------------------------------------------- */
251
252#define XTAL_TRIM_SHIFT 4
253#define XTAL_TRIM_MASK 0x0f
254
255/* --- XAH_CTRL [7:4] ------------------------------------------------------ */
256
257#define MAX_FRAME_RETRIES_SHIFT 4
258#define MAX_FRAME_RETRIES_MASK 0x0f
259#define MAX_CSMA_RETRIES_SHIFT 1
260#define MAX_CSMA_RETRIES_MASK 0x07
261
262/* --- PLL_CF [7] ---------------------------------------------------------- */
263
264#define PLL_CF_START (1 << 7)
265
266/* --- PLL_DCU [8] --------------------------------------------------------- */
267
268#define PLL_DCU_START (1 << 7)
269
270/* --- CSMA_SEED_1 [7:6] --------------------------------------------------- */
271
272#define MIN_BE_SHIFT 6
273#define MIN_BE_MASK 3
274
275/* --- CSMA_SEED_1 [5] ----------------------------------------------------- */
276
277#define AACK_SET_PD (1 << 5)
278
279/* --- CSMA_SEED_1 [3] ----------------------------------------------------- */
280
281#define I_AM_COORD (1 << 3)
282
283/* --- CSMA_SEED_1 [2:0] --------------------------------------------------- */
284
285#define CSMA_SEED_1_SHIFT 0
286#define CSMA_SEED_1_MASK 7
287
288#endif /* !AT86RF230_H */
atrf/fw/include/atspi/ep0.h
1/*
2 * include/atspi/ep0.h - EP0 extension protocol
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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 EP0_H
15#define EP0_H
16
17/*
18 * Direction bRequest wValue wIndex wLength
19 *
20 * ->host ATSPI_ID - - 3
21 * ->host ATSPI_BUILD - - #bytes
22 * host-> ATSPI_RESET - - 0
23 *
24 * host-> ATSPI_RF_RESET - -
25 *
26 * host-> ATSPI_REG_WRITE value addr 0
27 * ->host ATSPI_REG_READ - addr 1
28 * host-> ATSPI_BUF_WRITE - - #bytes
29 * ->host ATSPI_BUF_READ - - #bytes
30 * host-> ATSPI_SRAM_WRITE - addr #bytes
31 * ->host ATSPI_SRAM_READ - addr #bytes
32 */
33
34/*
35 * EP0 protocol:
36 *
37 * 0.0 initial release
38 */
39
40#define EP0ATSPI_MAJOR 0 /* EP0 protocol, major revision */
41#define EP0ATSPI_MINOR 0 /* EP0 protocol, minor revision */
42
43#define HW_TYPE_100813 0 /* 100813 */
44
45
46/*
47 * bmRequestType:
48 *
49 * D7 D6..5 D4...0
50 * | | |
51 * direction (0 = host->dev)
52 * type (2 = vendor)
53 * recipient (0 = device)
54 */
55
56
57#define ATSPI_TO_DEV(req) (0x40 | (req) << 8)
58#define ATSPI_FROM_DEV(req) (0xc0 | (req) << 8)
59
60
61enum atspi_requests {
62    ATSPI_ID = 0x00,
63    ATSPI_BUILD,
64    ATSPI_RESET,
65    ATSPI_RF_RESET = 0x10,
66    ATSPI_REG_WRITE = 0x20,
67    ATSPI_REG_READ,
68    ATSPI_BUF_WRITE,
69    ATSPI_BUF_READ,
70    ATSPI_SRAM_WRITE,
71    ATSPI_SRAM_READ,
72};
73
74
75void ep0_init(void);
76
77#endif /* !EP0_H */
atrf/fw/include/atspi/usb-ids.h
1/*
2 * include/atspi/usb-ids.h - USB vendor and product IDs
3 *
4 * Written 2009, 2010 by Werner Almesberger
5 * Copyright 2009, 2010 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 USB_IDS_H
15#define USB_IDS_H
16
17/*
18 * Platform-specific settings
19 *
20 * USB_VENDOR = Qi Hardware
21 * USB_PRODUCT = 802.15.4, device 0
22 * -- - -
23 */
24
25#define USB_VENDOR 0x20b7 /* Qi Hardware */
26#define USB_PRODUCT 0x1540 /* ben-wpan, AT86RF230-based */
27
28#endif /* !USB_IDS_H */
atrf/tools/Makefile
1#
2# tools/Makefile - Build the ATSPI-specific tools
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14DIRS=atspi-id atspi-reset atspi-rssi
15
16include ../../Makefile.recurse
atrf/tools/atspi-id/Makefile
1#
2# atspi-id/Makefile - Build the ATSPI identification utility
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14F32XBASE = ../../../../f32xbase
15
16MAIN = atspi-id
17
18include $(F32XBASE)/lib/Makefile.common
19
20CFLAGS += -I../../fw/include -I../include
21LDLIBS += -L../lib -latspi
atrf/tools/atspi-id/atspi-id.c
1/*
2 * atspi-id/atspi-id.c - Identify a ben-wpan AF86RF230 board
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <usb.h>
17
18#include "at86rf230.h"
19#include "atspi/ep0.h"
20#include "atspi.h"
21
22
23#define FROM_DEV ATSPI_FROM_DEV(0)
24
25#define BUF_SIZE 256
26
27
28
29static int get_id(usb_dev_handle *dev, void *data, int size)
30{
31    int res;
32
33    res = usb_control_msg(dev, FROM_DEV, ATSPI_ID, 0, 0, data, size, 1000);
34    if (res < 0)
35        fprintf(stderr, "ATSPI_ID: %s\n", usb_strerror());
36    return res;
37}
38
39
40static int atspi_get_protocol(usb_dev_handle *dev,
41    uint8_t *major, uint8_t *minor, uint8_t *target)
42{
43    uint8_t ids[3];
44
45    if (get_id(dev, ids, 3) < 0)
46        return -1;
47    if (major)
48        *major = ids[0];
49    if (minor)
50        *minor = ids[1];
51    if (target)
52        *target = ids[2];
53
54    return 0;
55}
56
57
58static int atspi_get_build(usb_dev_handle *dev, char *buf, size_t size)
59{
60    int res;
61
62    res = usb_control_msg(dev, FROM_DEV, ATSPI_BUILD, 0, 0, buf, size,
63        1000);
64    if (res < 0)
65        fprintf(stderr, "ATSPI_BUILD: %s\n", usb_strerror());
66    return res;
67}
68
69
70static void show_info(usb_dev_handle *dev)
71{
72    const struct usb_device *device = usb_device(dev);
73    uint8_t major, minor, target;
74    char buf[BUF_SIZE+1]; /* +1 for terminating \0 */
75    int len;
76    uint8_t part, version, man_id_0, man_id_1;
77
78    printf("%04x:%04x ",
79        device->descriptor.idVendor, device->descriptor.idProduct);
80
81    if (atspi_get_protocol(dev, &major, &minor, &target) < 0)
82        exit(1);
83    printf("protocol %u.%u hw %u\n", major, minor, target);
84
85    len = atspi_get_build(dev, buf, sizeof(buf)-1);
86    if (len < 0)
87        exit(1);
88    buf[len] = 0;
89    printf("%10s%s\n", "", buf);
90
91    part = atspi_reg_read(dev, REG_PART_NUM);
92    version = atspi_reg_read(dev, REG_VERSION_NUM);
93    man_id_0 = atspi_reg_read(dev, REG_MAN_ID_0);
94    man_id_1 = atspi_reg_read(dev, REG_MAN_ID_1);
95    printf("%10spart 0x%02x version %u manufacturer xxxx%02x%02x\n", "",
96        part, version, man_id_1, man_id_0);
97}
98
99
100static void usage(const char *name)
101{
102    fprintf(stderr, "%s\n", name);
103    exit(1);
104}
105
106
107int main(int argc, const char **argv)
108{
109    usb_dev_handle *dev;
110
111    if (argc != 1)
112        usage(*argv);
113    dev = atspi_open();
114    if (!dev)
115        return 1;
116
117    show_info(dev);
118
119    return 0;
120}
atrf/tools/atspi-reset/Makefile
1#
2# atspi-reset/Makefile - Reset the transceiver or the whole board
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14F32XBASE = ../../../../f32xbase
15
16MAIN = atspi-reset
17
18include $(F32XBASE)/lib/Makefile.common
19
20CFLAGS += -I../../fw/include -I../include
21LDLIBS += -L../lib -latspi
atrf/tools/atspi-reset/atspi-reset.c
1/*
2 * atspi-rssi/atspi-rssi.c - ben-wpan AF86RF230 spectrum scan
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <string.h>
17#include <usb.h>
18
19#include "atspi.h"
20
21
22static void usage(const char *name)
23{
24    fprintf(stderr,
25"usage: %s [-a|-t]\n\n"
26" -a reset the MCU and transceiver\n"
27" -t reset transceiver (default)\n"
28    , name);
29    exit(1);
30}
31
32
33int main(int argc, const char **argv)
34{
35    usb_dev_handle *dev;
36    int txrx = 1;
37
38    switch (argc) {
39    case 1:
40        break;
41    case 2:
42        if (!strcmp(argv[1], "-t"))
43            break;
44        txrx = 0;
45        if (!strcmp(argv[1], "-a"))
46            break;
47        /* fall through */
48    default:
49        usage(*argv);
50    }
51
52    dev = atspi_open();
53    if (!dev)
54        return 1;
55
56        if (txrx)
57                atspi_reset_rf(dev);
58        else
59                atspi_reset(dev);
60        return 0;
61}
62
atrf/tools/atspi-rssi/Makefile
1#
2# atspi-rssi/Makefile - Build the spectrum scan utility
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14F32XBASE = ../../../../f32xbase
15
16MAIN = atspi-rssi
17
18include $(F32XBASE)/lib/Makefile.common
19
20CFLAGS += -I../../fw/include -I../include
21LDLIBS += -L../lib -latspi
atrf/tools/atspi-rssi/atspi-rssi.c
1/*
2 * atspi-rssi/atspi-rssi.c - ben-wpan AF86RF230 spectrum scan
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <usb.h>
17#include <sys/time.h>
18
19#include "at86rf230.h"
20#include "atspi/ep0.h"
21#include "atspi.h"
22
23
24#define FROM_DEV ATSPI_FROM_DEV(0)
25#define TO_DEV ATSPI_TO_DEV(0)
26
27
28static struct timeval t0;
29
30
31static void sweep(usb_dev_handle *dev)
32{
33    int chan, rssi;
34    struct timeval t;
35
36    for (chan = 11; chan <= 26; chan++) {
37        atspi_reg_write(dev, REG_PHY_CC_CCA, chan);
38        /*
39         * No need to explicitly wait for the PPL lock - going USB-SPI
40         * is pretty slow, leaving the transceiver plenty of time.
41         */
42        gettimeofday(&t, NULL);
43        rssi = atspi_reg_read(dev, REG_PHY_RSSI) & RSSI_MASK;
44        t.tv_sec -= t0.tv_sec;
45        t.tv_usec -= t0.tv_usec;
46        printf("%d %f %d\n",
47            2405+(chan-11)*5,
48            (double) t.tv_sec+t.tv_usec/1000000.0,
49            -91+3*(rssi-1));
50    }
51    printf("\n");
52}
53
54
55static void usage(const char *name)
56{
57    fprintf(stderr, "%s sweeps \n", name);
58    exit(1);
59}
60
61
62int main(int argc, const char **argv)
63{
64    usb_dev_handle *dev;
65    unsigned long sweeps, i;
66    char *end;
67
68    if (argc != 2)
69        usage(*argv);
70    sweeps = strtoul(argv[1], &end, 0);
71    if (*end)
72        usage(*argv);
73
74    dev = atspi_open();
75    if (!dev)
76        return 1;
77
78    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TRX_OFF);
79    /*
80     * No need to explicitly wait for things to stabilize - going USB-SPI
81     * is pretty slow, leaving the transceiver more than enough time.
82     */
83    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_RX_ON);
84
85    gettimeofday(&t0, NULL);
86    for (i = 0; i != sweeps; i++)
87        sweep(dev);
88
89    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TRX_OFF);
90
91    return 0;
92}
atrf/tools/atspi-txrx/Makefile
1#
2# atspi-txrx/Makefile - Build the TX/RX test utility
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14F32XBASE = ../../../f32xbase
15
16MAIN = atspi-txrx
17
18include $(F32XBASE)/lib/Makefile.common
19
20CFLAGS += -I../../fw/include -I../include
21LDLIBS += -L../lib -latspi
atrf/tools/atspi-txrx/atspi-txrx.c
1/*
2 * atspi-txrx/atspi-txrx.c - ben-wpan AF86RF230 TX/RX
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <usb.h>
17
18#include "at86rf230.h"
19#include "atspi/ep0.h"
20#include "atspi.h"
21
22
23#define FROM_DEV ATSPI_FROM_DEV(0)
24#define TO_DEV ATSPI_TO_DEV(0)
25
26#define BUF_SIZE 256
27
28
29static void usage(const char *name)
30{
31    fprintf(stderr, "%s\n", name);
32    exit(1);
33}
34
35
36int main(int argc, const char **argv)
37{
38    usb_dev_handle *dev;
39
40    if (argc != 1)
41        usage(*argv);
42    dev = atspi_open();
43    if (!dev)
44        return 1;
45
46    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TRX_OFF);
47    sleep(1000);
48
49    return 0;
50}
atrf/tools/include/atspi.h
1/*
2 * include/atspi.h - ATSPI access functions library
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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#ifndef ATSPI_H
14#define ATSPI_H
15
16#include <stdint.h>
17#include <usb.h>
18
19
20int atspi_error(void);
21int atspi_clear_error(void);
22
23usb_dev_handle *atspi_open(void);
24void atspi_close(usb_dev_handle *dev);
25
26void atspi_reset(usb_dev_handle *dev);
27void atspi_reset_rf(usb_dev_handle *dev);
28
29void atspi_reg_write(usb_dev_handle *dev, uint8_t reg, uint8_t value);
30uint8_t atspi_reg_read(usb_dev_handle *dev, uint8_t reg);
31
32#endif /* !ATSPI_H */
atrf/tools/lib/Makefile
1#
2# lib/Makefile - Build the ATSPI library
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14F32XBASE = ../../../../f32xbase
15
16LIB = libatspi.a
17
18CFLAGS = -Wall -I$(F32XBASE)/include -I../../fw/include -I../include
19OBJS = $(F32XBASE)/lib/usb.o atspi.o
20
21.PHONY: all clean spotless
22
23all: $(LIB)
24
25$(LIB): $(OBJS)
26        $(AR) cr $@ $^
27
28clean:
29        rm -f $(OBJS)
30
31spotless: clean
32        rm -f $(LIB)
atrf/tools/lib/atspi.c
1/*
2 * lib/atspi.c - ATSPI access functions library
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <stdio.h>
15#include <usb.h>
16
17#include "f32xbase/usb.h"
18#include "atspi/ep0.h"
19#include "atspi/usb-ids.h"
20
21#include "atspi.h"
22
23
24#define FROM_DEV ATSPI_FROM_DEV(0)
25#define TO_DEV ATSPI_TO_DEV(0)
26
27
28/* ----- error handling ---------------------------------------------------- */
29
30
31static int error;
32
33
34int atspi_error(void)
35{
36    return error;
37}
38
39
40int atspi_clear_error(void)
41{
42    int ret;
43
44    ret = error;
45    error = 0;
46    return ret;
47}
48
49
50/* ----- open/close -------------------------------------------------------- */
51
52
53usb_dev_handle *atspi_open(void)
54{
55    usb_dev_handle *dev;
56
57    dev = open_usb(USB_VENDOR, USB_PRODUCT);
58    if (dev) {
59        error = 0;
60    } else {
61        fprintf(stderr, ":-(\n");
62        error = 1;
63    }
64    return dev;
65}
66
67
68void atspi_close(usb_dev_handle *dev)
69{
70    /* to do */
71}
72
73
74/* ----- device mode ------------------------------------------------------- */
75
76
77void atspi_reset(usb_dev_handle *dev)
78{
79    int res;
80
81    if (error)
82        return;
83
84    res =
85        usb_control_msg(dev, TO_DEV, ATSPI_RESET, 0, 0, NULL, 0, 1000);
86    if (res < 0) {
87        fprintf(stderr, "ATSPI_RESET: %d\n", res);
88        error = 1;
89    }
90}
91
92
93void atspi_reset_rf(usb_dev_handle *dev)
94{
95    int res;
96
97    if (error)
98        return;
99
100    res =
101        usb_control_msg(dev, TO_DEV, ATSPI_RF_RESET, 0, 0, NULL, 0, 1000);
102    if (res < 0) {
103        fprintf(stderr, "ATSPI_RF_RESET: %d\n", res);
104        error = 1;
105    }
106}
107
108
109/* ----- register access --------------------------------------------------- */
110
111
112void atspi_reg_write(usb_dev_handle *dev, uint8_t reg, uint8_t value)
113{
114    int res;
115
116    if (error)
117        return;
118
119    res = usb_control_msg(dev, TO_DEV, ATSPI_REG_WRITE, value, reg,
120        NULL, 0, 1000);
121    if (res < 0) {
122        fprintf(stderr, "ATSPI_REG_WRITE: %d\n", res);
123        error = 1;
124    }
125}
126
127
128uint8_t atspi_reg_read(usb_dev_handle *dev, uint8_t reg)
129{
130    uint8_t value = 0;
131    int res;
132
133    if (error)
134        return 0;
135
136    res = usb_control_msg(dev, FROM_DEV, ATSPI_REG_READ, 0, reg,
137        (void *) &value, 1, 1000);
138    if (res < 0) {
139        fprintf(stderr, "ATSPI_REG_READ: %d\n", res);
140        error = 1;
141    }
142    return value;
143}
fw/Makefile
1#
2# fw/Makefile - ATSPI firmware build
3#
4# Written 2008-2010 by Werner Almesberger
5# Copyright 2008-2010 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
14DIRS=common atspi-boot atspi
15
16
17.PHONY: all depend install uninstall clean spotless
18
19all:
20        for d in $(DIRS); do make -C $$d all || exit 1; done
21
22depend:
23        for d in $(DIRS); do make -C $$d depend || exit 1; done
24
25# Top-level Makefile recurses for "install" and "uninstall", which have no use
26# here. Just ignore them.
27
28install:
29
30uninstall:
31
32clean:
33        for d in $(DIRS); do make -C $$d clean || exit 1; done
34
35spotless:
36        for d in $(DIRS); do make -C $$d spotless || exit 1; done
fw/atspi-boot/Makefile
1#
2# atspi-boot/Makefile - Makefile for DFU-capable boot loader for ATSPI
3#
4# Written 2008, 2010 by Werner Almesberger
5# Copyright 2008, 2010 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
13MAIN=boot
14OBJS=$(MAIN) usb dfu version
15
16F32XBASE = ../../../f32xbase
17
18include $(F32XBASE)/fw/common/Makefile.system
19include $(F32XBASE)/fw/common/Makefile.common
20
21CFLAGS += -I../common -I../include
22LDFLAGS += --code-size $(PAYLOAD_START)
fw/atspi/Makefile
1#
2# atspi/Makefile - Makefile for USB to SPI translator (for AT86RF230)
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14MAIN = atspi
15OBJS = $(MAIN) usb descr version ep0
16
17F32XBASE = ../../../f32xbase
18
19include $(F32XBASE)/fw/common/Makefile.system
20include $(F32XBASE)/fw/common/Makefile.common
21
22CFLAGS += -I../common -I../include
23LDFLAGS += --code-size $(PAYLOAD_SIZE) --code-loc $(PAYLOAD_START)
24
25USB_ID = $(shell \
26  ( echo '\#include "config.h"'; echo USB_VENDOR:USB_PRODUCT; ) | \
27  cpp -I../common -I../include | sed '/^ *$$/d;/^\#/d' )
28
29.PHONY: dfu
30
31dfu:
32        dfu-util -d $(USB_ID) -D $(MAIN).bin
fw/atspi/atspi.c
1/*
2 * atspi/atspi.c - ATSPI initialization and main loop
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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 "regs.h"
15#include "io.h"
16#include "usb.h"
17#include "atspi/ep0.h"
18#include "version.h"
19
20
21void reset_rf(void)
22{
23    int i;
24
25    nRST_RF = 0;
26    /*
27     * 11.4.12: min 625 ns.
28     * The pulse we generate is slightly longer than 4 us.
29     */
30    for (i = 0; i != 10; i++);
31    nRST_RF = 1;
32}
33
34
35static void init_io(void)
36{
37    /*
38     * Signal Mode Value
39     *
40     * MOSI push-pull 0
41     * MISO open drain 1 (input)
42     * SCLK push-pull 0
43     * nSS push-pull 1
44     * nRST_RF push-pull 1
45     * IRQ_RF open drain 1 (input)
46     * SLP_TR push-pull 0
47     *
48     * LED push-pull 0 (set up by boot loader)
49     *
50     * all unused open drain 0
51     */
52
53    MOSI = 0;
54    MOSI_MODE |= 1 << MOSI_BIT;
55
56    SCLK = 0;
57    SCLK_MODE |= 1 << SCLK_BIT;
58
59    nSS_MODE |= 1 << nSS_BIT;
60
61    nRST_RF_MODE |= 1 << nRST_RF_BIT;
62
63    SLP_TR = 0;
64    SLP_TR_MODE |= 1 << SLP_TR_BIT;
65
66    P0 &=
67        ~((1 << 0) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
68        /* change 1 << 0 to 1 << 2 once 100813 boards are reworked */
69    P3 = 0;
70
71#if 0
72    /*
73     * We can *almost* disable the pull-ups. The only obstacle is that
74     * MISO is not driven when not in use. So we either need an external
75     * pull-up/down or keep all the pull-ups on.
76     */
77
78    /*
79     * Disable pull-ups
80     */
81    GPIOCN |= WEAKPUD;
82#endif
83
84    /*
85     * The manual says the reset is optional, but reality disagrees with
86     * this optimistic assessment quite violently.
87     */
88
89    reset_rf();
90}
91
92
93void main(void)
94{
95    init_io();
96
97    usb_init();
98    ep0_init();
99
100    while (1) {
101        usb_poll();
102    }
103}
fw/atspi/descr.c
1/*
2 * atspi/descr.c - USB descriptors
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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 "usb.h"
15
16#include "config.h"
17
18
19/*
20 * Device descriptor
21 */
22
23const uint8_t device_descriptor[18] = {
24    18, /* bLength */
25    USB_DT_DEVICE, /* bDescriptorType */
26    LE(0x200), /* bcdUSB */
27    USB_CLASS_VENDOR_SPEC, /* bDeviceClass */
28    0x00, /* bDeviceSubClass */
29    0x00, /* bDeviceProtocol */
30    EP0_SIZE, /* bMaxPacketSize */
31    LE(USB_VENDOR), /* idVendor */
32    LE(USB_PRODUCT), /* idProduct */
33    LE(0x0001), /* bcdDevice */
34    0, /* iManufacturer */
35    0, /* iProduct */
36    0, /* iSerialNumber */
37    1 /* bNumConfigurations */
38};
39
40
41/*
42 * Our configuration
43 *
44 * While in DFU mode, we're always bus-powered.
45 */
46
47const uint8_t config_descriptor[] = {
48    9, /* bLength */
49    USB_DT_CONFIG, /* bDescriptorType */
50    LE(9+9+7+7), /* wTotalLength */
51    1, /* bNumInterfaces */
52    1, /* bConfigurationValue (> 0 !) */
53    0, /* iConfiguration */
54// USB_ATTR_SELF_POWERED | USB_ATTR_BUS_POWERED,
55    USB_ATTR_BUS_POWERED, /* bmAttributes */
56    15, /* bMaxPower */
57
58    /* Interface #0 */
59
60    9, /* bLength */
61    USB_DT_INTERFACE, /* bDescriptorType */
62    0, /* bInterfaceNumber */
63    0, /* bAlternateSetting */
64    2, /* bNumEndpoints */
65    USB_CLASS_VENDOR_SPEC, /* bInterfaceClass */
66    0, /* bInterfaceSubClass */
67    0, /* bInterfaceProtocol */
68    0, /* iInterface */
69
70    /* EP OUT */
71
72    7, /* bLength */
73    USB_DT_ENDPOINT, /* bDescriptorType */
74    0x01, /* bEndPointAddress */
75    0x02, /* bmAttributes (bulk) */
76    LE(EP1_SIZE), /* wMaxPacketSize */
77    0, /* bInterval */
78
79    /* EP IN */
80
81    7, /* bLength */
82    USB_DT_ENDPOINT, /* bDescriptorType */
83    0x81, /* bEndPointAddress */
84    0x02, /* bmAttributes (bulk) */
85    LE(EP1_SIZE), /* wMaxPacketSize */
86    0, /* bInterval */
87
88};
fw/atspi/ep0.c
1/*
2 * atspi/ep0.c - EP0 extension protocol
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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
16#ifndef NULL
17#define NULL 0
18#endif
19
20#include "regs.h"
21//#include "uart.h"
22#include "usb.h"
23#include "atspi/ep0.h"
24#include "at86rf230.h"
25#include "version.h"
26
27
28extern void reset_rf(void);
29
30
31#define debug(...)
32#define error(...)
33
34
35/*
36 * SDCC 2.8.0 had a number of code generation bugs that appeared in the big
37 * switch statement of my_setup. SDCC_FORCE_UPDATE forced the value of the
38 * "size" variable to be written to memory. This work-around doesn't seem
39 * to be necessary with 2.9.0, but we keep it around, just in case.
40 *
41 * Unfortunately, the setup->bRequest garbling bug is still with us. Without
42 * the evaluation forced with SDCC_FORCE_EVAL, sdcc gets confused about the
43 * value of setup->bRequest and then rejects all SETUP requests.
44 */
45
46#define SDCC_FORCE_EVAL(type, value) \
47    do { \
48    static volatile type foo; \
49    foo = value; \
50    } while (0)
51
52#define SDCC_FORCE_UPDATE(type, var) \
53    do { \
54    volatile type foo; \
55    foo = var; \
56    var = foo; \
57    } while (0)
58
59
60static const uint8_t id[] = { EP0ATSPI_MAJOR, EP0ATSPI_MINOR, HW_TYPE };
61static __xdata uint8_t buf[MAX_PSDU+3]; /* command, PHDR, and LQ */
62static uint8_t size;
63
64
65static void spi_send(uint8_t v)
66{
67    uint8_t mask;
68
69    for (mask = 0x80; mask; mask >>= 1) {
70        MOSI = !!(v & mask);
71        SCLK = 1;
72        SCLK = 0;
73    }
74}
75
76
77static uint8_t spi_recv(void)
78{
79    uint8_t res = 0;
80    uint8_t i;
81
82    for (i = 0; i != 8; i++) {
83        res = (res << 1) | MISO;
84        SCLK = 1;
85        SCLK = 0;
86    }
87    return res;
88}
89
90
91static void do_buf_write(void *user)
92{
93    uint8_t i;
94
95    user; /* suppress warning */
96    nSS = 0;
97    spi_send(AT86RF230_BUF_WRITE);
98    for (i = 0; i != size; i++)
99        spi_send(buf[i]);
100    nSS = 1;
101}
102
103
104#define BUILD_OFFSET 7 /* '#' plus "65535" plus ' ' */
105
106
107static __bit my_setup(struct setup_request *setup) __reentrant
108{
109    unsigned tmp;
110    uint8_t i;
111
112    switch (setup->bmRequestType | setup->bRequest << 8) {
113    case ATSPI_FROM_DEV(ATSPI_ID):
114        debug("ATSPI_ID\n");
115        if (setup->wLength > 3)
116            return 0;
117        usb_send(&ep0, id, setup->wLength, NULL, NULL);
118        return 1;
119    case ATSPI_FROM_DEV(ATSPI_BUILD):
120        debug("ATSPI_BUILD\n");
121        tmp = build_number;
122        for (i = BUILD_OFFSET-2; tmp; i--) {
123            buf[i] = (tmp % 10)+'0';
124            tmp /= 10;
125        }
126        buf[i] = '#';
127        buf[BUILD_OFFSET-1] = ' ';
128        for (size = 0; build_date[size]; size++)
129            buf[BUILD_OFFSET+size] = build_date[size];
130        size += BUILD_OFFSET-i+1;
131        SDCC_FORCE_EVAL(uint8_t, setup->bRequest);
132        if (size > setup->wLength)
133            return 0;
134        usb_send(&ep0, buf+i, size, NULL, NULL);
135        return 1;
136
137    case ATSPI_TO_DEV(ATSPI_RESET):
138        debug("ATSPI_RESET\n");
139        RSTSRC = SWRSF;
140        while (1);
141
142    case ATSPI_TO_DEV(ATSPI_RF_RESET):
143        debug("ATSPI_RF_RESET\n");
144        reset_rf();
145        return 1;
146
147    case ATSPI_TO_DEV(ATSPI_REG_WRITE):
148        debug("ATSPI_REG_WRITE\n");
149        nSS = 0;
150        spi_send(AT86RF230_REG_WRITE | setup->wIndex);
151        spi_send(setup->wValue);
152        nSS = 1;
153        return 1;
154    case ATSPI_FROM_DEV(ATSPI_REG_READ):
155        debug("ATSPI_REG_READ\n");
156        nSS = 0;
157        spi_send(AT86RF230_REG_READ | setup->wIndex);
158        *buf = spi_recv();
159        nSS = 1;
160        usb_send(&ep0, buf, 1, NULL, NULL);
161        return 1;
162
163    case ATSPI_TO_DEV(ATSPI_BUF_WRITE):
164        debug("ATSPI_BUF_WRITE\n");
165        if (setup->wLength < 1)
166            return 0;
167        if (setup->wLength > MAX_PSDU+1) /* PHR+PSDU */
168            return 0;
169        buf[0] = AT86RF230_BUF_WRITE;
170        size = setup->wLength+1;
171        usb_recv(&ep0, buf+1, setup->wLength, do_buf_write, NULL);
172        return 1;
173    case ATSPI_TO_DEV(ATSPI_BUF_READ):
174        debug("ATSPI_BUF_READ\n");
175        if (setup->wLength < 2) /* PHR+LQ */
176            return 0;
177        if (setup->wLength > MAX_PSDU+2) /* PHR+PSDU+LQ */
178            return 0;
179        nSS = 0;
180        spi_send(AT86RF230_BUF_READ);
181        size = *buf = spi_recv();
182        if (size+2 > setup->wLength)
183            size = setup->wLength-2;
184        for (i = 0; i != size+1; i++)
185            buf[i+1] = spi_recv();
186        nSS = 1;
187        usb_send(&ep0, buf, size+1, NULL, NULL);
188        return 1;
189
190    case ATSPI_TO_DEV(ATSPI_SRAM_WRITE):
191        debug("ATSPI_SRAM_WRITE\n");
192        if (setup->wIndex > SRAM_SIZE)
193            return 0;
194        if (setup->wIndex+setup->wLength > SRAM_SIZE)
195            return 0;
196        buf[0] = AT86RF230_SRAM_WRITE;
197        buf[1] = setup->wIndex;
198        size = setup->wLength+2;
199        usb_recv(&ep0, buf+2, setup->wLength, do_buf_write, NULL);
200        return 1;
201    case ATSPI_TO_DEV(ATSPI_SRAM_READ):
202        debug("ATSPI_SRAM_READ\n");
203        if (setup->wIndex > SRAM_SIZE)
204            return 0;
205        if (setup->wIndex+setup->wLength > SRAM_SIZE)
206            return 0;
207        nSS = 0;
208        spi_send(AT86RF230_SRAM_READ);
209        spi_send(setup->wIndex);
210        for (i = 0; i != size; i++)
211            buf[i] = spi_recv();
212        nSS = 1;
213        usb_send(&ep0, buf, size, NULL, NULL);
214        return 1;
215
216    default:
217        error("Unrecognized SETUP: 0x%02x 0x%02x ...\n",
218            setup->bmRequestType, setup->bRequest);
219        return 0;
220    }
221}
222
223
224void ep0_init(void)
225{
226    user_setup = my_setup;
227}
fw/common/Makefile
1#
2# common/Makefile - Makefile for shared items
3#
4# Written 2008, 2010 by Werner Almesberger
5# Copyright 2008, 2010 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
14GEN_quiet = @echo " GENERATE " $@ &&
15
16ifeq ($(V),1)
17    GEN =
18else
19    GEN = $(GEN_quiet)
20endif
21
22
23.PHONY: all depend clean spotless
24
25all: io-parts.h
26
27io-parts.h: io.h Makefile
28        $(GEN) $(CPP) -dD $< | \
29          sed '1,/IO_H/d' | \
30          awk \
31          'BEGIN { print "/* MACHINE-GENERATED. DO NOT EDIT ! */"; \
32            print "#ifndef IO_PARTS_H"; \
33            print "#define IO_PARTS_H"; } \
34           /#define/ && $$3 != "" { \
35            split($$3, a, "_"); \
36            print $$1, $$2 "_PORT", a[1]; \
37            print $$1, $$2 "_MODE", a[1] "MDOUT"; \
38            print $$1, $$2 "_BIT", a[2]; } \
39           END { print "#endif" }' >$@ || \
40                { rm -f $@; exit 1; }
41
42
43#
44# When we do a global "make depend", we'll come here first. So we create
45# io-parts.h so that "make depend" in the other directories can pick it up.
46#
47
48depend: io-parts.h
49
50clean:
51        rm -f io-parts.h
52
53spotless: clean
fw/common/config.h
1/*
2 * common/config.h - Configuration data for boot loader and application
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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#ifndef CONFIG_H
14#define CONFIG_H
15
16#include "io.h"
17#include "io-parts.h"
18#include "atspi/usb-ids.h"
19
20
21/* ----- Boot loader configuration ----------------------------------------- */
22
23/*
24 * Make LED output push-pull so that we can output a high voltage.
25 * This turns on the LED, to indicate that we're in the boot loader.
26 */
27
28#define PLATFORM_SETUP \
29    LED_MODE |= 1 << LED_BIT;
30
31
32/*
33 * Turn off the LED when we exit the boot loader.
34 */
35
36#define PLATFORM_EXIT \
37    LED = 0
38
39
40/* ----- Application configuration ----------------------------------------- */
41
42#define HW_TYPE HW_TYPE_100813
43
44#endif /* !CONFIG_H */
fw/common/io.h
1/*
2 * common/io.h - I/O pin assignment
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 IO_H
15#define IO_H
16
17/* Diagnostic LED */
18
19#define LED P0_1
20
21/* SPI */
22
23#define MOSI P2_2
24#define MISO P2_5
25#define SCLK P2_4
26#define nSS P2_3
27
28/* Miscellaneous RF signals */
29
30#define nRST_RF P2_0
31#define IRQ_RF P0_2 /* change to P0_0 when 100813 board are reworked */
32#define SLP_TR P2_1
33
34#endif /* !IO_H */
fw/include/at86rf230.h
1/*
2 * atspi/at86rf230.h - AT86RF230 protocol and register definitions
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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 AT86RF230_H
15#define AT86RF230_H
16
17enum {
18    AT86RF230_REG_WRITE = 0xc0, /* 11... */
19    AT86RF230_REG_READ = 0x80, /* 10... */
20    AT86RF230_BUF_WRITE = 0x60, /* 011... */
21    AT86RF230_BUF_READ = 0x20, /* 001... */
22    AT86RF230_SRAM_WRITE = 0x40, /* 010... */
23    AT86RF230_SRAM_READ = 0x00 /* 000... */
24};
25
26#define MAX_PSDU 127 /* octets, see AT86RF230 manual section 8.1 */
27#define SRAM_SIZE 128
28
29
30/* --- Registers ----------------------------------------------------------- */
31
32enum {
33    REG_TRX_STATUS = 0x01,
34    REG_TRX_STATE = 0x02,
35    REG_TRX_CTRL_0 = 0x03,
36
37    REG_TR_PWR = 0x05,
38    REG_PHY_RSSI = 0x06,
39    REG_PHY_ED_LEVEL = 0x07,
40    REG_PHY_CC_CCA = 0x08,
41    REG_CCA_THRES = 0x09,
42
43    REG_IRQ_MASK = 0x0e,
44    REG_IRQ_STATUS = 0x0f,
45    REG_VREG_CTRL = 0x10,
46    REG_BATMON = 0x10,
47    REG_XOSC_CTRL = 0x12,
48
49    REG_PLL_CF = 0x1a,
50    REL_PLL_DCU = 0x1b,
51    REG_PART_NUM = 0x1c,
52    REG_VERSION_NUM = 0x1d,
53    REG_MAN_ID_0 = 0x1e,
54    REG_MAN_ID_1 = 0x1f,
55    REG_SHORT_ADDR_0 = 0x20,
56    REG_SHORT_ADDR_1 = 0x21,
57    REG_PAN_ID_0 = 0x22,
58    REG_PAN_ID_1 = 0x23,
59    REG_IEEE_ADDR_0 = 0x24,
60    REG_IEEE_ADDR_1 = 0x25,
61    REG_IEEE_ADDR_2 = 0x26,
62    REG_IEEE_ADDR_3 = 0x27,
63    REG_IEEE_ADDR_4 = 0x28,
64    REG_IEEE_ADDR_5 = 0x29,
65    REG_IEEE_ADDR_6 = 0x2a,
66    REG_IEEE_ADDR_7 = 0x2b,
67    REG_XAH_CTRL = 0x2c,
68    REG_CSMA_SEED_0 = 0x2d,
69    REG_CSMA_SEED_1 = 0x2e,
70};
71
72/* --- TRX_STATUS [7] ------------------------------------------------------ */
73
74#define CCA_DONE (1 << 7)
75
76/* --- TRX_STATUS [6] ------------------------------------------------------ */
77
78#define CCA_STATUS (1 << 6)
79
80/* --- TRX_STATUS [4:0] ---------------------------------------------------- */
81
82#define TRX_STATUS_SHIFT 0
83#define TRX_STATUS_MASK 0x0f
84
85enum {
86    TRX_STATUS_P_ON = 0x00, /* reset default */
87    TRX_STATUS_BUSY_RX = 0x01,
88    TRX_STATUS_BUSY_TX = 0x02,
89    TRX_STATUS_RX_ON = 0x06,
90    TRX_STATUS_TRX_OFF = 0x08,
91    TRX_STATUS_PLL_ON = 0x09,
92    TRX_STATUS_SLEEP = 0x0f,
93    TRX_STATUS_BUSY_RX_AACK = 0x11,
94    TRX_STATUS_BUSY_TX_ARET = 0x12,
95    TRX_STATUS_RX_AACK_ON = 0x16,
96    TRX_STATUS_TX_ARET_ON = 0x19,
97    TRX_STATUS_RX_ON_NOCLK = 0x1c,
98    TRX_STATUS_RX_AACK_ON_NOCLK = 0x1d,
99    TRX_STATUS_BUSY_RX_AACK_NOCLK = 0x1e,
100    TRX_STATUS_TRANSITION = 0x1f
101};
102
103/* --- TRX_STATE [7:5] ----------------------------------------------------- */
104
105#define TRAC_STATUS_SHIFT 5
106#define TRAC_STATUS_MASK 7
107
108enum {
109    TRAC_STATUS_SUCCESS = 0, /* reset default */
110    TRAC_STATUS_SUCCESS_DATA_PENDING = 1,
111    TRAC_STATUS_CHANNEL_ACCESS_FAILURE = 3,
112    TRAC_STATUS_NO_ACK = 5,
113    TRAC_STATUS_INVALID = 7
114};
115
116/* --- TRX_CMD [4:0] ----------------------------------------------------- */
117
118#define TRX_CMD_SHIFT 0
119#define TRX_CMD_MASK 7
120
121enum {
122    TRX_CMD_NOP = 0x00, /* reset default */
123    TRX_CMD_TX_START = 0x02,
124    TRX_CMD_FORCE_TRX_OFF = 0x03,
125    TRX_CMD_RX_ON = 0x06,
126    TRX_CMD_TRX_OFF = 0x08,
127    TRX_CMD_PLL_ON = 0x09,
128    TRX_CMD_RX_AACK_ON = 0x16,
129    TRX_CMD_TX_ARET_ON = 0x19,
130};
131
132/* --- TRX_CTRL_0 [7:6] ---------------------------------------------------- */
133
134#define PAD_IO_SHIFT 6
135#define PAD_IO_MASK 3
136
137enum {
138    PAD_IO_2mA, /* reset default */
139    PAD_IO_4mA,
140    PAD_IO_6mA,
141    PAD_IO_8mA
142};
143
144/* --- TRX_CTRL_0 [5:4] ---------------------------------------------------- */
145
146#define PAD_IO_CLKM_SHIFT 4
147#define PAD_IO_CLKM_MASK 3
148
149enum {
150    PAD_IO_CLKM_2mA,
151    PAD_IO_CLKM_4mA, /* reset default */
152    PAD_IO_CLKM_5mA,
153    PAD_IO_CLKM_8mA,
154};
155
156/* --- TRX_CTRL_0 [3] ------------------------------------------------------ */
157
158#define CLKM_SHA_SEL (1 << 3)
159
160/* --- TRX_CTRL_0 [2:0] ---------------------------------------------------- */
161
162#define CLKM_CTRL_SHIFT 0
163#define CLKM_CTRL_MASK 3
164
165enum {
166    CLKM_CTRL_OFF = 0,
167    CLKM_CTRL_1MHz = 1, /* reset default */
168    CLKM_CTRL_2MHz = 2,
169    CLKM_CTRL_4MHz = 3,
170    CLKM_CTRL_8MHz = 4,
171    CLKM_CTRL_16MHz = 5
172};
173
174/* --- PHY_TX_PWR [7] ------------------------------------------------------ */
175
176#define TX_AUTO_CRC_ON (1 << 7)
177
178/* --- PHY_TX_PWR [3:0] ---------------------------------------------------- */
179
180#define TX_PWR_SHIFT 0
181#define TX_PWR_MASK 0x0f
182
183/* --- PHY_RSSI [7] -------------------------------------------------------- */
184
185#define RX_CRC_VALID (1 << 7)
186
187/* --- PHY_RSSI [4:0] ------------------------------------------------------ */
188
189#define RSSI_SHIFT 0
190#define RSSI_MASK 0x1f
191
192/* --- PHY_CC_CCA [7] ------------------------------------------------------ */
193
194#define CCA_REQUEST (1 << 7)
195
196/* --- PHY_CC_CCA [6:5] ---------------------------------------------------- */
197
198#define CCA_MODE_SHIFT 5
199#define CCA_MODE_MASK 3
200
201/* --- PHY_CC_CCA [4:0] ---------------------------------------------------- */
202
203#define CHANNEL_SHIFT 0
204#define CHANNEL_MASK 0x1f
205
206/* --- CCA_THRES [3:0] ----------------------------------------------------- */
207
208#define CCA_ED_THRES_SHIFT 0
209#define CCA_ED_THRES_MASK 0x0f
210
211/* --- IRQ_MASK/IRQ_STATUS ------------------------------------------------- */
212
213enum {
214    IRQ_PLL_LOCK = 1 << 0,
215    IRQ_PLL_UNLOCK = 1 << 1,
216    IRQ_RX_START = 1 << 2,
217    IRQ_TRX_END = 1 << 3,
218    IRQ_TRX_UR = 1 << 6,
219    IRQ_BAT_LOW = 1 << 7
220};
221
222/* --- VREG_CTRL [7, 6, 3, 2] ---------------------------------------------- */
223
224#define AVREG_EXT (1 << 7)
225#define AVDD_OK (1 << 6)
226#define DVREG_EXT (1 << 3)
227#define DVDD_OK (1 << 2)
228
229/* --- BATMON [5, 4] ------------------------------------------------------- */
230
231#define BATMON_OK (1 << 5)
232#define BATMON_HR (1 << 4)
233
234/* --- BATMON [3:0] -------------------------------------------------------- */
235
236#define NATMON_VTH_SHIFT 0
237#define NATMON_VTH_MASK 0x0f
238
239/* --- XOSC_CTRL [7:4] ----------------------------------------------------- */
240
241#define XTAL_MODE_SHIFT 4
242#define XTAL_MODE_MASK 0x0f
243
244enum {
245    XTAL_MODE_OFF = 0x0,
246    XTAL_MODE_EXT = 0x4,
247    XTAL_MODE_INT = 0xf /* reset default */
248};
249
250/* --- XOSC_CTRL [3:1] ----------------------------------------------------- */
251
252#define XTAL_TRIM_SHIFT 4
253#define XTAL_TRIM_MASK 0x0f
254
255/* --- XAH_CTRL [7:4] ------------------------------------------------------ */
256
257#define MAX_FRAME_RETRIES_SHIFT 4
258#define MAX_FRAME_RETRIES_MASK 0x0f
259#define MAX_CSMA_RETRIES_SHIFT 1
260#define MAX_CSMA_RETRIES_MASK 0x07
261
262/* --- PLL_CF [7] ---------------------------------------------------------- */
263
264#define PLL_CF_START (1 << 7)
265
266/* --- PLL_DCU [8] --------------------------------------------------------- */
267
268#define PLL_DCU_START (1 << 7)
269
270/* --- CSMA_SEED_1 [7:6] --------------------------------------------------- */
271
272#define MIN_BE_SHIFT 6
273#define MIN_BE_MASK 3
274
275/* --- CSMA_SEED_1 [5] ----------------------------------------------------- */
276
277#define AACK_SET_PD (1 << 5)
278
279/* --- CSMA_SEED_1 [3] ----------------------------------------------------- */
280
281#define I_AM_COORD (1 << 3)
282
283/* --- CSMA_SEED_1 [2:0] --------------------------------------------------- */
284
285#define CSMA_SEED_1_SHIFT 0
286#define CSMA_SEED_1_MASK 7
287
288#endif /* !AT86RF230_H */
fw/include/atspi/ep0.h
1/*
2 * include/atspi/ep0.h - EP0 extension protocol
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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 EP0_H
15#define EP0_H
16
17/*
18 * Direction bRequest wValue wIndex wLength
19 *
20 * ->host ATSPI_ID - - 3
21 * ->host ATSPI_BUILD - - #bytes
22 * host-> ATSPI_RESET - - 0
23 *
24 * host-> ATSPI_RF_RESET - -
25 *
26 * host-> ATSPI_REG_WRITE value addr 0
27 * ->host ATSPI_REG_READ - addr 1
28 * host-> ATSPI_BUF_WRITE - - #bytes
29 * ->host ATSPI_BUF_READ - - #bytes
30 * host-> ATSPI_SRAM_WRITE - addr #bytes
31 * ->host ATSPI_SRAM_READ - addr #bytes
32 */
33
34/*
35 * EP0 protocol:
36 *
37 * 0.0 initial release
38 */
39
40#define EP0ATSPI_MAJOR 0 /* EP0 protocol, major revision */
41#define EP0ATSPI_MINOR 0 /* EP0 protocol, minor revision */
42
43#define HW_TYPE_100813 0 /* 100813 */
44
45
46/*
47 * bmRequestType:
48 *
49 * D7 D6..5 D4...0
50 * | | |
51 * direction (0 = host->dev)
52 * type (2 = vendor)
53 * recipient (0 = device)
54 */
55
56
57#define ATSPI_TO_DEV(req) (0x40 | (req) << 8)
58#define ATSPI_FROM_DEV(req) (0xc0 | (req) << 8)
59
60
61enum atspi_requests {
62    ATSPI_ID = 0x00,
63    ATSPI_BUILD,
64    ATSPI_RESET,
65    ATSPI_RF_RESET = 0x10,
66    ATSPI_REG_WRITE = 0x20,
67    ATSPI_REG_READ,
68    ATSPI_BUF_WRITE,
69    ATSPI_BUF_READ,
70    ATSPI_SRAM_WRITE,
71    ATSPI_SRAM_READ,
72};
73
74
75void ep0_init(void);
76
77#endif /* !EP0_H */
fw/include/atspi/usb-ids.h
1/*
2 * include/atspi/usb-ids.h - USB vendor and product IDs
3 *
4 * Written 2009, 2010 by Werner Almesberger
5 * Copyright 2009, 2010 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 USB_IDS_H
15#define USB_IDS_H
16
17/*
18 * Platform-specific settings
19 *
20 * USB_VENDOR = Qi Hardware
21 * USB_PRODUCT = 802.15.4, device 0
22 * -- - -
23 */
24
25#define USB_VENDOR 0x20b7 /* Qi Hardware */
26#define USB_PRODUCT 0x1540 /* ben-wpan, AT86RF230-based */
27
28#endif /* !USB_IDS_H */
tools/Makefile
1#
2# tools/Makefile - Build the ATSPI-specific tools
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14DIRS=atspi-id atspi-reset atspi-rssi
15
16include ../Makefile.recurse
tools/atspi-id/Makefile
1#
2# atspi-id/Makefile - Build the ATSPI identification utility
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14F32XBASE = ../../../f32xbase
15
16MAIN = atspi-id
17
18include $(F32XBASE)/lib/Makefile.common
19
20CFLAGS += -I../../fw/include -I../include
21LDLIBS += -L../lib -latspi
tools/atspi-id/atspi-id.c
1/*
2 * atspi-id/atspi-id.c - Identify a ben-wpan AF86RF230 board
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <usb.h>
17
18#include "at86rf230.h"
19#include "atspi/ep0.h"
20#include "atspi.h"
21
22
23#define FROM_DEV ATSPI_FROM_DEV(0)
24
25#define BUF_SIZE 256
26
27
28
29static int get_id(usb_dev_handle *dev, void *data, int size)
30{
31    int res;
32
33    res = usb_control_msg(dev, FROM_DEV, ATSPI_ID, 0, 0, data, size, 1000);
34    if (res < 0)
35        fprintf(stderr, "ATSPI_ID: %s\n", usb_strerror());
36    return res;
37}
38
39
40static int atspi_get_protocol(usb_dev_handle *dev,
41    uint8_t *major, uint8_t *minor, uint8_t *target)
42{
43    uint8_t ids[3];
44
45    if (get_id(dev, ids, 3) < 0)
46        return -1;
47    if (major)
48        *major = ids[0];
49    if (minor)
50        *minor = ids[1];
51    if (target)
52        *target = ids[2];
53
54    return 0;
55}
56
57
58static int atspi_get_build(usb_dev_handle *dev, char *buf, size_t size)
59{
60    int res;
61
62    res = usb_control_msg(dev, FROM_DEV, ATSPI_BUILD, 0, 0, buf, size,
63        1000);
64    if (res < 0)
65        fprintf(stderr, "ATSPI_BUILD: %s\n", usb_strerror());
66    return res;
67}
68
69
70static void show_info(usb_dev_handle *dev)
71{
72    const struct usb_device *device = usb_device(dev);
73    uint8_t major, minor, target;
74    char buf[BUF_SIZE+1]; /* +1 for terminating \0 */
75    int len;
76    uint8_t part, version, man_id_0, man_id_1;
77
78    printf("%04x:%04x ",
79        device->descriptor.idVendor, device->descriptor.idProduct);
80
81    if (atspi_get_protocol(dev, &major, &minor, &target) < 0)
82        exit(1);
83    printf("protocol %u.%u hw %u\n", major, minor, target);
84
85    len = atspi_get_build(dev, buf, sizeof(buf)-1);
86    if (len < 0)
87        exit(1);
88    buf[len] = 0;
89    printf("%10s%s\n", "", buf);
90
91    part = atspi_reg_read(dev, REG_PART_NUM);
92    version = atspi_reg_read(dev, REG_VERSION_NUM);
93    man_id_0 = atspi_reg_read(dev, REG_MAN_ID_0);
94    man_id_1 = atspi_reg_read(dev, REG_MAN_ID_1);
95    printf("%10spart 0x%02x version %u manufacturer xxxx%02x%02x\n", "",
96        part, version, man_id_1, man_id_0);
97}
98
99
100static void usage(const char *name)
101{
102    fprintf(stderr, "%s\n", name);
103    exit(1);
104}
105
106
107int main(int argc, const char **argv)
108{
109    usb_dev_handle *dev;
110
111    if (argc != 1)
112        usage(*argv);
113    dev = atspi_open();
114    if (!dev)
115        return 1;
116
117    show_info(dev);
118
119    return 0;
120}
tools/atspi-reset/Makefile
1#
2# atspi-reset/Makefile - Reset the transceiver or the whole board
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14F32XBASE = ../../../f32xbase
15
16MAIN = atspi-reset
17
18include $(F32XBASE)/lib/Makefile.common
19
20CFLAGS += -I../../fw/include -I../include
21LDLIBS += -L../lib -latspi
tools/atspi-reset/atspi-reset.c
1/*
2 * atspi-rssi/atspi-rssi.c - ben-wpan AF86RF230 spectrum scan
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <string.h>
17#include <usb.h>
18
19#include "atspi.h"
20
21
22static void usage(const char *name)
23{
24    fprintf(stderr,
25"usage: %s [-a|-t]\n\n"
26" -a reset the MCU and transceiver\n"
27" -t reset transceiver (default)\n"
28    , name);
29    exit(1);
30}
31
32
33int main(int argc, const char **argv)
34{
35    usb_dev_handle *dev;
36    int txrx = 1;
37
38    switch (argc) {
39    case 1:
40        break;
41    case 2:
42        if (!strcmp(argv[1], "-t"))
43            break;
44        txrx = 0;
45        if (!strcmp(argv[1], "-a"))
46            break;
47        /* fall through */
48    default:
49        usage(*argv);
50    }
51
52    dev = atspi_open();
53    if (!dev)
54        return 1;
55
56        if (txrx)
57                atspi_reset_rf(dev);
58        else
59                atspi_reset(dev);
60        return 0;
61}
62
tools/atspi-rssi/Makefile
1#
2# atspi-rssi/Makefile - Build the spectrum scan utility
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14F32XBASE = ../../../f32xbase
15
16MAIN = atspi-rssi
17
18include $(F32XBASE)/lib/Makefile.common
19
20CFLAGS += -I../../fw/include -I../include
21LDLIBS += -L../lib -latspi
tools/atspi-rssi/atspi-rssi.c
1/*
2 * atspi-rssi/atspi-rssi.c - ben-wpan AF86RF230 spectrum scan
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <usb.h>
17#include <sys/time.h>
18
19#include "at86rf230.h"
20#include "atspi/ep0.h"
21#include "atspi.h"
22
23
24#define FROM_DEV ATSPI_FROM_DEV(0)
25#define TO_DEV ATSPI_TO_DEV(0)
26
27
28static struct timeval t0;
29
30
31static void sweep(usb_dev_handle *dev)
32{
33    int chan, rssi;
34    struct timeval t;
35
36    for (chan = 11; chan <= 26; chan++) {
37        atspi_reg_write(dev, REG_PHY_CC_CCA, chan);
38        /*
39         * No need to explicitly wait for the PPL lock - going USB-SPI
40         * is pretty slow, leaving the transceiver plenty of time.
41         */
42        gettimeofday(&t, NULL);
43        rssi = atspi_reg_read(dev, REG_PHY_RSSI) & RSSI_MASK;
44        t.tv_sec -= t0.tv_sec;
45        t.tv_usec -= t0.tv_usec;
46        printf("%d %f %d\n",
47            2405+(chan-11)*5,
48            (double) t.tv_sec+t.tv_usec/1000000.0,
49            -91+3*(rssi-1));
50    }
51    printf("\n");
52}
53
54
55static void usage(const char *name)
56{
57    fprintf(stderr, "%s sweeps \n", name);
58    exit(1);
59}
60
61
62int main(int argc, const char **argv)
63{
64    usb_dev_handle *dev;
65    unsigned long sweeps, i;
66    char *end;
67
68    if (argc != 2)
69        usage(*argv);
70    sweeps = strtoul(argv[1], &end, 0);
71    if (*end)
72        usage(*argv);
73
74    dev = atspi_open();
75    if (!dev)
76        return 1;
77
78    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TRX_OFF);
79    /*
80     * No need to explicitly wait for things to stabilize - going USB-SPI
81     * is pretty slow, leaving the transceiver more than enough time.
82     */
83    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_RX_ON);
84
85    gettimeofday(&t0, NULL);
86    for (i = 0; i != sweeps; i++)
87        sweep(dev);
88
89    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TRX_OFF);
90
91    return 0;
92}
tools/atspi-txrx/Makefile
1#
2# atspi-txrx/Makefile - Build the TX/RX test utility
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14F32XBASE = ../../../f32xbase
15
16MAIN = atspi-txrx
17
18include $(F32XBASE)/lib/Makefile.common
19
20CFLAGS += -I../../fw/include -I../include
21LDLIBS += -L../lib -latspi
tools/atspi-txrx/atspi-txrx.c
1/*
2 * atspi-txrx/atspi-txrx.c - ben-wpan AF86RF230 TX/RX
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <usb.h>
17
18#include "at86rf230.h"
19#include "atspi/ep0.h"
20#include "atspi.h"
21
22
23#define FROM_DEV ATSPI_FROM_DEV(0)
24#define TO_DEV ATSPI_TO_DEV(0)
25
26#define BUF_SIZE 256
27
28
29static void usage(const char *name)
30{
31    fprintf(stderr, "%s\n", name);
32    exit(1);
33}
34
35
36int main(int argc, const char **argv)
37{
38    usb_dev_handle *dev;
39
40    if (argc != 1)
41        usage(*argv);
42    dev = atspi_open();
43    if (!dev)
44        return 1;
45
46    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TRX_OFF);
47    sleep(1000);
48
49    return 0;
50}
tools/include/atspi.h
1#ifndef ATSPI_H
2#define ATSPI_H
3
4
5#include <stdint.h>
6#include <usb.h>
7
8
9int atspi_error(void);
10int atspi_clear_error(void);
11
12usb_dev_handle *atspi_open(void);
13void atspi_close(usb_dev_handle *dev);
14
15void atspi_reset(usb_dev_handle *dev);
16void atspi_reset_rf(usb_dev_handle *dev);
17
18void atspi_reg_write(usb_dev_handle *dev, uint8_t reg, uint8_t value);
19uint8_t atspi_reg_read(usb_dev_handle *dev, uint8_t reg);
20
21#endif /* !ATSPI_H */
tools/lib/Makefile
1#
2# lib/Makefile - Build the ATSPI library
3#
4# Written 2010 by Werner Almesberger
5# Copyright 2010 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
14F32XBASE = ../../../f32xbase
15
16LIB = libatspi.a
17
18CFLAGS = -Wall -I$(F32XBASE)/include -I../../fw/include -I../include
19OBJS = $(F32XBASE)/lib/usb.o atspi.o
20
21.PHONY: all clean spotless
22
23all: $(LIB)
24
25$(LIB): $(OBJS)
26        $(AR) cr $@ $^
27
28clean:
29        rm -f $(OBJS)
30
31spotless: clean
32        rm -f $(LIB)
tools/lib/atspi.c
1/*
2 * lib/atspi.c - ATSPI access functions library
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <stdio.h>
15#include <usb.h>
16
17#include "f32xbase/usb.h"
18#include "atspi/ep0.h"
19#include "atspi/usb-ids.h"
20
21#include "atspi.h"
22
23
24#define FROM_DEV ATSPI_FROM_DEV(0)
25#define TO_DEV ATSPI_TO_DEV(0)
26
27
28/* ----- error handling ---------------------------------------------------- */
29
30
31static int error;
32
33
34int atspi_error(void)
35{
36    return error;
37}
38
39
40int atspi_clear_error(void)
41{
42    int ret;
43
44    ret = error;
45    error = 0;
46    return ret;
47}
48
49
50/* ----- open/close -------------------------------------------------------- */
51
52
53usb_dev_handle *atspi_open(void)
54{
55    usb_dev_handle *dev;
56
57    dev = open_usb(USB_VENDOR, USB_PRODUCT);
58    if (dev) {
59        error = 0;
60    } else {
61        fprintf(stderr, ":-(\n");
62        error = 1;
63    }
64    return dev;
65}
66
67
68void atspi_close(usb_dev_handle *dev)
69{
70    /* to do */
71}
72
73
74/* ----- device mode ------------------------------------------------------- */
75
76
77void atspi_reset(usb_dev_handle *dev)
78{
79    int res;
80
81    if (error)
82        return;
83
84    res =
85        usb_control_msg(dev, TO_DEV, ATSPI_RESET, 0, 0, NULL, 0, 1000);
86    if (res < 0) {
87        fprintf(stderr, "ATSPI_RESET: %d\n", res);
88        error = 1;
89    }
90}
91
92
93void atspi_reset_rf(usb_dev_handle *dev)
94{
95    int res;
96
97    if (error)
98        return;
99
100    res =
101        usb_control_msg(dev, TO_DEV, ATSPI_RF_RESET, 0, 0, NULL, 0, 1000);
102    if (res < 0) {
103        fprintf(stderr, "ATSPI_RF_RESET: %d\n", res);
104        error = 1;
105    }
106}
107
108
109/* ----- register access --------------------------------------------------- */
110
111
112void atspi_reg_write(usb_dev_handle *dev, uint8_t reg, uint8_t value)
113{
114    int res;
115
116    if (error)
117        return;
118
119    res = usb_control_msg(dev, TO_DEV, ATSPI_REG_WRITE, value, reg,
120        NULL, 0, 1000);
121    if (res < 0) {
122        fprintf(stderr, "ATSPI_REG_WRITE: %d\n", res);
123        error = 1;
124    }
125}
126
127
128uint8_t atspi_reg_read(usb_dev_handle *dev, uint8_t reg)
129{
130    uint8_t value = 0;
131    int res;
132
133    if (error)
134        return 0;
135
136    res = usb_control_msg(dev, FROM_DEV, ATSPI_REG_READ, 0, reg,
137        (void *) &value, 1, 1000);
138    if (res < 0) {
139        fprintf(stderr, "ATSPI_REG_READ: %d\n", res);
140        error = 1;
141    }
142    return value;
143}

Archive Download the corresponding diff file



interactive