Date:2010-11-11 12:26:01 (9 years 1 month ago)
Author:Werner Almesberger
Commit:7e71f9800742a4449be8a773fd6399a48aca8529
Message:The Great ATSPI Renaming, part 1: rename atusb firmware files from atspi to atusb

- atusb/fw/atspi/atspi.c: renamed to atusb/fw/atspi/atusb.c
- atusb/fw/atspi/Makefile: track above change
- atusb/fw/include/atspi/: renamed to atusb/fw/include/atusb/
- atusb/fw/common/config.h, atusb/fw/atspi/atspi.c, atusb/fw/atspi/descr.c,
atusb/fw/atspi/ep0.c: track change of include location
- atusb/fw/atspi/: renamed to atusb
- atusb/fw/Makefile: track above change
- tools/atspi-id/atspi-id.c, tools/atspi-trim/atspi-trim.c,
tools/lib/atusb.c: track change of include location
Files: atusb/fw/Makefile (2 diffs)
atusb/fw/atspi/Makefile (1 diff)
atusb/fw/atspi/atspi.c (1 diff)
atusb/fw/atspi/descr.c (1 diff)
atusb/fw/atspi/ep0.c (1 diff)
atusb/fw/atusb/Makefile (1 diff)
atusb/fw/atusb/atusb.c (1 diff)
atusb/fw/atusb/descr.c (1 diff)
atusb/fw/atusb/ep0.c (1 diff)
atusb/fw/common/config.h (1 diff)
atusb/fw/include/atspi/ep0.h (1 diff)
atusb/fw/include/atspi/usb-ids.h (1 diff)
atusb/fw/include/atusb/ep0.h (1 diff)
atusb/fw/include/atusb/usb-ids.h (1 diff)
tools/atspi-id/atspi-id.c (1 diff)
tools/atspi-trim/atspi-trim.c (1 diff)
tools/lib/atusb.c (1 diff)

Change Details

atusb/fw/Makefile
11#
2# fw/Makefile - ATSPI firmware build
2# fw/Makefile - ATUSB firmware build
33#
44# Written 2008-2010 by Werner Almesberger
55# Copyright 2008-2010 Werner Almesberger
...... 
1111#
1212
1313
14DIRS=common boot atspi
14DIRS=common boot atusb
1515
1616
1717.PHONY: all depend install uninstall clean spotless
atusb/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
atusb/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
21static int in_test_mode = 0;
22
23
24static void set_test_mode(int on)
25{
26    in_test_mode = on;
27    if (on) {
28        TST_MODE |= 1 << TST_BIT;
29        TST = 1;
30        LED_MODE |= 1 << LED_BIT;
31    } else {
32        TST = 0;
33        TST_MODE &= ~(1 << TST_BIT);
34        LED_MODE &= ~(1 << LED_BIT);
35        LED = 0;
36    }
37}
38
39
40void reset_rf(void)
41{
42    int i;
43
44    set_test_mode(0);
45
46    nRST_RF = 0;
47    /*
48     * 11.4.12: min 625 ns.
49     * The pulse we generate is slightly longer than 4 us.
50     */
51    for (i = 0; i != 10; i++);
52    nRST_RF = 1;
53}
54
55
56void test_mode(void)
57{
58    set_test_mode(1);
59}
60
61
62static void init_io(void)
63{
64    /*
65     * Signal Mode Value
66     *
67     * MOSI push-pull 0
68     * MISO open drain 1 (input)
69     * SCLK push-pull 0
70     * nSS push-pull 1
71     * nRST_RF push-pull 1
72     * IRQ_RF open drain 1 (input)
73     * SLP_TR push-pull 0
74     * TST open drain 0
75     *
76     * LED push-pull 0 (set up by boot loader)
77     *
78     * all unused open drain 0
79     */
80
81    MOSI = 0;
82    MOSI_MODE |= 1 << MOSI_BIT;
83
84    SCLK = 0;
85    SCLK_MODE |= 1 << SCLK_BIT;
86
87    nSS_MODE |= 1 << nSS_BIT;
88
89    nRST_RF_MODE |= 1 << nRST_RF_BIT;
90
91    SLP_TR = 0;
92    SLP_TR_MODE |= 1 << SLP_TR_BIT;
93
94    P0 = 1; /* IRQ_RF = 1; LED = 0; TST = Z; the rest is unused */
95    P3 = 0;
96
97#if 0
98    /*
99     * We can *almost* disable the pull-ups. The only obstacle is that
100     * MISO is not driven when not in use. So we either need an external
101     * pull-up/down or keep all the pull-ups on.
102     */
103
104    /*
105     * Disable pull-ups
106     */
107    GPIOCN |= WEAKPUD;
108#endif
109
110    /*
111     * The manual says the reset is optional, but reality disagrees with
112     * this optimistic assessment quite violently.
113     */
114
115    reset_rf();
116}
117
118
119void main(void)
120{
121    int i;
122
123    init_io();
124
125    /*
126     * Make sure the host has enough time (2.5 us) to detect that we reset
127     * our USB stack.
128     */
129    for (i = 0; i != 10; i++);
130
131    usb_init();
132    ep0_init();
133
134    while (1) {
135        if (in_test_mode) {
136            i++;
137            LED = !(i >> 13);
138        }
139        usb_poll();
140    }
141}
atusb/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 * We're always bus-powered.
45 */
46
47const uint8_t config_descriptor[] = {
48    9, /* bLength */
49    USB_DT_CONFIG, /* bDescriptorType */
50#if 0
51    LE(9+9+7+7), /* wTotalLength */
52#else
53    LE(9+9), /* wTotalLength */
54#endif
55    1, /* bNumInterfaces */
56    1, /* bConfigurationValue (> 0 !) */
57    0, /* iConfiguration */
58    USB_ATTR_BUS_POWERED, /* bmAttributes */
59    50/2, /* bMaxPower (50 mA) */
60
61    /* Interface #0 */
62
63    9, /* bLength */
64    USB_DT_INTERFACE, /* bDescriptorType */
65    0, /* bInterfaceNumber */
66    0, /* bAlternateSetting */
67#if 0
68    2, /* bNumEndpoints */
69#else
70    0,
71#endif
72    USB_CLASS_VENDOR_SPEC, /* bInterfaceClass */
73    0, /* bInterfaceSubClass */
74    0, /* bInterfaceProtocol */
75    0, /* iInterface */
76
77#if 0
78    /* EP OUT */
79
80    7, /* bLength */
81    USB_DT_ENDPOINT, /* bDescriptorType */
82    0x01, /* bEndPointAddress */
83    0x02, /* bmAttributes (bulk) */
84    LE(EP1_SIZE), /* wMaxPacketSize */
85    0, /* bInterval */
86
87    /* EP IN */
88
89    7, /* bLength */
90    USB_DT_ENDPOINT, /* bDescriptorType */
91    0x81, /* bEndPointAddress */
92    0x02, /* bmAttributes (bulk) */
93    LE(EP1_SIZE), /* wMaxPacketSize */
94    0, /* bInterval */
95#endif
96};
atusb/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);
29extern void test_mode(void);
30
31
32#define debug(...)
33#define error(...)
34
35
36/*
37 * SDCC 2.8.0 had a number of code generation bugs that appeared in the big
38 * switch statement of my_setup. SDCC_FORCE_UPDATE forced the value of the
39 * "size" variable to be written to memory. This work-around doesn't seem
40 * to be necessary with 2.9.0, but we keep it around, just in case.
41 *
42 * Unfortunately, the setup->bRequest garbling bug is still with us. Without
43 * the evaluation forced with SDCC_FORCE_EVAL, sdcc gets confused about the
44 * value of setup->bRequest and then rejects all SETUP requests.
45 */
46
47#define SDCC_FORCE_EVAL(type, value) \
48    do { \
49    static volatile type foo; \
50    foo = value; \
51    } while (0)
52
53#define SDCC_FORCE_UPDATE(type, var) \
54    do { \
55    volatile type foo; \
56    foo = var; \
57    var = foo; \
58    } while (0)
59
60
61static const uint8_t id[] = { EP0ATSPI_MAJOR, EP0ATSPI_MINOR, HW_TYPE };
62static __xdata uint8_t buf[MAX_PSDU+3]; /* command, PHDR, and LQI */
63static uint8_t size;
64
65
66static void spi_send(uint8_t v)
67{
68    uint8_t mask;
69
70    for (mask = 0x80; mask; mask >>= 1) {
71        MOSI = !!(v & mask);
72        SCLK = 1;
73        SCLK = 0;
74    }
75}
76
77
78static uint8_t spi_recv(void)
79{
80    uint8_t res = 0;
81    uint8_t i;
82
83    for (i = 0; i != 8; i++) {
84        res = (res << 1) | MISO;
85        SCLK = 1;
86        SCLK = 0;
87    }
88    return res;
89}
90
91
92static void do_buf_write(void *user)
93{
94    uint8_t i;
95
96    user; /* suppress warning */
97    nSS = 0;
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;
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_FROM_DEV(ATSPI_POLL_INT):
148        debug("ATSPI_POLL_INT\n");
149        if (setup->wLength < 1)
150            return 0;
151        *buf = IRQ_RF;
152        usb_send(&ep0, buf, 1, NULL, NULL);
153        return 1;
154
155    case ATSPI_TO_DEV(ATSPI_TEST):
156        debug("ATSPI_TEST\n");
157        test_mode();
158        return 1;
159
160    case ATSPI_TO_DEV(ATSPI_REG_WRITE):
161        debug("ATSPI_REG_WRITE\n");
162        nSS = 0;
163        spi_send(AT86RF230_REG_WRITE | setup->wIndex);
164        spi_send(setup->wValue);
165        nSS = 1;
166        return 1;
167    case ATSPI_FROM_DEV(ATSPI_REG_READ):
168        debug("ATSPI_REG_READ\n");
169        nSS = 0;
170        spi_send(AT86RF230_REG_READ | setup->wIndex);
171        *buf = spi_recv();
172        nSS = 1;
173        usb_send(&ep0, buf, 1, NULL, NULL);
174        return 1;
175
176    case ATSPI_TO_DEV(ATSPI_BUF_WRITE):
177        debug("ATSPI_BUF_WRITE\n");
178        if (setup->wLength < 1)
179            return 0;
180        if (setup->wLength > MAX_PSDU)
181            return 0;
182        buf[0] = AT86RF230_BUF_WRITE;
183        buf[1] = setup->wLength;
184        size = setup->wLength+2;
185        usb_recv(&ep0, buf+2, setup->wLength, do_buf_write, NULL);
186        return 1;
187    case ATSPI_FROM_DEV(ATSPI_BUF_READ):
188        debug("ATSPI_BUF_READ\n");
189        if (setup->wLength < 2) /* PHR+LQI */
190            return 0;
191        if (setup->wLength > MAX_PSDU+2) /* PHR+PSDU+LQI */
192            return 0;
193        nSS = 0;
194        spi_send(AT86RF230_BUF_READ);
195        size = spi_recv();
196        if (size >= setup->wLength)
197            size = setup->wLength-1;
198        for (i = 0; i != size+1; i++)
199            buf[i] = spi_recv();
200        nSS = 1;
201        usb_send(&ep0, buf, size+1, NULL, NULL);
202        return 1;
203
204    case ATSPI_TO_DEV(ATSPI_SRAM_WRITE):
205        debug("ATSPI_SRAM_WRITE\n");
206        if (setup->wIndex > SRAM_SIZE)
207            return 0;
208        if (setup->wIndex+setup->wLength > SRAM_SIZE)
209            return 0;
210        buf[0] = AT86RF230_SRAM_WRITE;
211        buf[1] = setup->wIndex;
212        size = setup->wLength+2;
213        usb_recv(&ep0, buf+2, setup->wLength, do_buf_write, NULL);
214        return 1;
215    case ATSPI_TO_DEV(ATSPI_SRAM_READ):
216        debug("ATSPI_SRAM_READ\n");
217        if (setup->wIndex > SRAM_SIZE)
218            return 0;
219        if (setup->wIndex+setup->wLength > SRAM_SIZE)
220            return 0;
221        nSS = 0;
222        spi_send(AT86RF230_SRAM_READ);
223        spi_send(setup->wIndex);
224        for (i = 0; i != size; i++)
225            buf[i] = spi_recv();
226        nSS = 1;
227        usb_send(&ep0, buf, size, NULL, NULL);
228        return 1;
229
230    default:
231        error("Unrecognized SETUP: 0x%02x 0x%02x ...\n",
232            setup->bmRequestType, setup->bRequest);
233        return 0;
234    }
235}
236
237
238void ep0_init(void)
239{
240    user_setup = my_setup;
241}
atusb/fw/atusb/Makefile
1#
2# atusb/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 = atusb
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
atusb/fw/atusb/atusb.c
1/*
2 * atusb/atusb.c - ATUSB 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 "atusb/ep0.h"
18#include "version.h"
19
20
21static int in_test_mode = 0;
22
23
24static void set_test_mode(int on)
25{
26    in_test_mode = on;
27    if (on) {
28        TST_MODE |= 1 << TST_BIT;
29        TST = 1;
30        LED_MODE |= 1 << LED_BIT;
31    } else {
32        TST = 0;
33        TST_MODE &= ~(1 << TST_BIT);
34        LED_MODE &= ~(1 << LED_BIT);
35        LED = 0;
36    }
37}
38
39
40void reset_rf(void)
41{
42    int i;
43
44    set_test_mode(0);
45
46    nRST_RF = 0;
47    /*
48     * 11.4.12: min 625 ns.
49     * The pulse we generate is slightly longer than 4 us.
50     */
51    for (i = 0; i != 10; i++);
52    nRST_RF = 1;
53}
54
55
56void test_mode(void)
57{
58    set_test_mode(1);
59}
60
61
62static void init_io(void)
63{
64    /*
65     * Signal Mode Value
66     *
67     * MOSI push-pull 0
68     * MISO open drain 1 (input)
69     * SCLK push-pull 0
70     * nSS push-pull 1
71     * nRST_RF push-pull 1
72     * IRQ_RF open drain 1 (input)
73     * SLP_TR push-pull 0
74     * TST open drain 0
75     *
76     * LED push-pull 0 (set up by boot loader)
77     *
78     * all unused open drain 0
79     */
80
81    MOSI = 0;
82    MOSI_MODE |= 1 << MOSI_BIT;
83
84    SCLK = 0;
85    SCLK_MODE |= 1 << SCLK_BIT;
86
87    nSS_MODE |= 1 << nSS_BIT;
88
89    nRST_RF_MODE |= 1 << nRST_RF_BIT;
90
91    SLP_TR = 0;
92    SLP_TR_MODE |= 1 << SLP_TR_BIT;
93
94    P0 = 1; /* IRQ_RF = 1; LED = 0; TST = Z; the rest is unused */
95    P3 = 0;
96
97#if 0
98    /*
99     * We can *almost* disable the pull-ups. The only obstacle is that
100     * MISO is not driven when not in use. So we either need an external
101     * pull-up/down or keep all the pull-ups on.
102     */
103
104    /*
105     * Disable pull-ups
106     */
107    GPIOCN |= WEAKPUD;
108#endif
109
110    /*
111     * The manual says the reset is optional, but reality disagrees with
112     * this optimistic assessment quite violently.
113     */
114
115    reset_rf();
116}
117
118
119void main(void)
120{
121    int i;
122
123    init_io();
124
125    /*
126     * Make sure the host has enough time (2.5 us) to detect that we reset
127     * our USB stack.
128     */
129    for (i = 0; i != 10; i++);
130
131    usb_init();
132    ep0_init();
133
134    while (1) {
135        if (in_test_mode) {
136            i++;
137            LED = !(i >> 13);
138        }
139        usb_poll();
140    }
141}
atusb/fw/atusb/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 * We're always bus-powered.
45 */
46
47const uint8_t config_descriptor[] = {
48    9, /* bLength */
49    USB_DT_CONFIG, /* bDescriptorType */
50#if 0
51    LE(9+9+7+7), /* wTotalLength */
52#else
53    LE(9+9), /* wTotalLength */
54#endif
55    1, /* bNumInterfaces */
56    1, /* bConfigurationValue (> 0 !) */
57    0, /* iConfiguration */
58    USB_ATTR_BUS_POWERED, /* bmAttributes */
59    50/2, /* bMaxPower (50 mA) */
60
61    /* Interface #0 */
62
63    9, /* bLength */
64    USB_DT_INTERFACE, /* bDescriptorType */
65    0, /* bInterfaceNumber */
66    0, /* bAlternateSetting */
67#if 0
68    2, /* bNumEndpoints */
69#else
70    0,
71#endif
72    USB_CLASS_VENDOR_SPEC, /* bInterfaceClass */
73    0, /* bInterfaceSubClass */
74    0, /* bInterfaceProtocol */
75    0, /* iInterface */
76
77#if 0
78    /* EP OUT */
79
80    7, /* bLength */
81    USB_DT_ENDPOINT, /* bDescriptorType */
82    0x01, /* bEndPointAddress */
83    0x02, /* bmAttributes (bulk) */
84    LE(EP1_SIZE), /* wMaxPacketSize */
85    0, /* bInterval */
86
87    /* EP IN */
88
89    7, /* bLength */
90    USB_DT_ENDPOINT, /* bDescriptorType */
91    0x81, /* bEndPointAddress */
92    0x02, /* bmAttributes (bulk) */
93    LE(EP1_SIZE), /* wMaxPacketSize */
94    0, /* bInterval */
95#endif
96};
atusb/fw/atusb/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 "atusb/ep0.h"
24#include "at86rf230.h"
25#include "version.h"
26
27
28extern void reset_rf(void);
29extern void test_mode(void);
30
31
32#define debug(...)
33#define error(...)
34
35
36/*
37 * SDCC 2.8.0 had a number of code generation bugs that appeared in the big
38 * switch statement of my_setup. SDCC_FORCE_UPDATE forced the value of the
39 * "size" variable to be written to memory. This work-around doesn't seem
40 * to be necessary with 2.9.0, but we keep it around, just in case.
41 *
42 * Unfortunately, the setup->bRequest garbling bug is still with us. Without
43 * the evaluation forced with SDCC_FORCE_EVAL, sdcc gets confused about the
44 * value of setup->bRequest and then rejects all SETUP requests.
45 */
46
47#define SDCC_FORCE_EVAL(type, value) \
48    do { \
49    static volatile type foo; \
50    foo = value; \
51    } while (0)
52
53#define SDCC_FORCE_UPDATE(type, var) \
54    do { \
55    volatile type foo; \
56    foo = var; \
57    var = foo; \
58    } while (0)
59
60
61static const uint8_t id[] = { EP0ATSPI_MAJOR, EP0ATSPI_MINOR, HW_TYPE };
62static __xdata uint8_t buf[MAX_PSDU+3]; /* command, PHDR, and LQI */
63static uint8_t size;
64
65
66static void spi_send(uint8_t v)
67{
68    uint8_t mask;
69
70    for (mask = 0x80; mask; mask >>= 1) {
71        MOSI = !!(v & mask);
72        SCLK = 1;
73        SCLK = 0;
74    }
75}
76
77
78static uint8_t spi_recv(void)
79{
80    uint8_t res = 0;
81    uint8_t i;
82
83    for (i = 0; i != 8; i++) {
84        res = (res << 1) | MISO;
85        SCLK = 1;
86        SCLK = 0;
87    }
88    return res;
89}
90
91
92static void do_buf_write(void *user)
93{
94    uint8_t i;
95
96    user; /* suppress warning */
97    nSS = 0;
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;
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_FROM_DEV(ATSPI_POLL_INT):
148        debug("ATSPI_POLL_INT\n");
149        if (setup->wLength < 1)
150            return 0;
151        *buf = IRQ_RF;
152        usb_send(&ep0, buf, 1, NULL, NULL);
153        return 1;
154
155    case ATSPI_TO_DEV(ATSPI_TEST):
156        debug("ATSPI_TEST\n");
157        test_mode();
158        return 1;
159
160    case ATSPI_TO_DEV(ATSPI_REG_WRITE):
161        debug("ATSPI_REG_WRITE\n");
162        nSS = 0;
163        spi_send(AT86RF230_REG_WRITE | setup->wIndex);
164        spi_send(setup->wValue);
165        nSS = 1;
166        return 1;
167    case ATSPI_FROM_DEV(ATSPI_REG_READ):
168        debug("ATSPI_REG_READ\n");
169        nSS = 0;
170        spi_send(AT86RF230_REG_READ | setup->wIndex);
171        *buf = spi_recv();
172        nSS = 1;
173        usb_send(&ep0, buf, 1, NULL, NULL);
174        return 1;
175
176    case ATSPI_TO_DEV(ATSPI_BUF_WRITE):
177        debug("ATSPI_BUF_WRITE\n");
178        if (setup->wLength < 1)
179            return 0;
180        if (setup->wLength > MAX_PSDU)
181            return 0;
182        buf[0] = AT86RF230_BUF_WRITE;
183        buf[1] = setup->wLength;
184        size = setup->wLength+2;
185        usb_recv(&ep0, buf+2, setup->wLength, do_buf_write, NULL);
186        return 1;
187    case ATSPI_FROM_DEV(ATSPI_BUF_READ):
188        debug("ATSPI_BUF_READ\n");
189        if (setup->wLength < 2) /* PHR+LQI */
190            return 0;
191        if (setup->wLength > MAX_PSDU+2) /* PHR+PSDU+LQI */
192            return 0;
193        nSS = 0;
194        spi_send(AT86RF230_BUF_READ);
195        size = spi_recv();
196        if (size >= setup->wLength)
197            size = setup->wLength-1;
198        for (i = 0; i != size+1; i++)
199            buf[i] = spi_recv();
200        nSS = 1;
201        usb_send(&ep0, buf, size+1, NULL, NULL);
202        return 1;
203
204    case ATSPI_TO_DEV(ATSPI_SRAM_WRITE):
205        debug("ATSPI_SRAM_WRITE\n");
206        if (setup->wIndex > SRAM_SIZE)
207            return 0;
208        if (setup->wIndex+setup->wLength > SRAM_SIZE)
209            return 0;
210        buf[0] = AT86RF230_SRAM_WRITE;
211        buf[1] = setup->wIndex;
212        size = setup->wLength+2;
213        usb_recv(&ep0, buf+2, setup->wLength, do_buf_write, NULL);
214        return 1;
215    case ATSPI_TO_DEV(ATSPI_SRAM_READ):
216        debug("ATSPI_SRAM_READ\n");
217        if (setup->wIndex > SRAM_SIZE)
218            return 0;
219        if (setup->wIndex+setup->wLength > SRAM_SIZE)
220            return 0;
221        nSS = 0;
222        spi_send(AT86RF230_SRAM_READ);
223        spi_send(setup->wIndex);
224        for (i = 0; i != size; i++)
225            buf[i] = spi_recv();
226        nSS = 1;
227        usb_send(&ep0, buf, size, NULL, NULL);
228        return 1;
229
230    default:
231        error("Unrecognized SETUP: 0x%02x 0x%02x ...\n",
232            setup->bmRequestType, setup->bRequest);
233        return 0;
234    }
235}
236
237
238void ep0_init(void)
239{
240    user_setup = my_setup;
241}
atusb/fw/common/config.h
1515
1616#include "io.h"
1717#include "io-parts.h"
18#include "atspi/usb-ids.h"
18#include "atusb/usb-ids.h"
1919
2020
2121/* ----- Boot loader configuration ----------------------------------------- */
atusb/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 - - 0
25 * ->host ATSPI_POLL_INT - - 1
26 * host-> ATSPI_TEST - - 0
27 *
28 * host-> ATSPI_REG_WRITE value addr 0
29 * ->host ATSPI_REG_READ - addr 1
30 * host-> ATSPI_BUF_WRITE - - #bytes
31 * ->host ATSPI_BUF_READ - - #bytes
32 * host-> ATSPI_SRAM_WRITE - addr #bytes
33 * ->host ATSPI_SRAM_READ - addr #bytes
34 */
35
36/*
37 * EP0 protocol:
38 *
39 * 0.0 initial release
40 * 0.1 addition of ATSPI_TEST
41 */
42
43#define EP0ATSPI_MAJOR 0 /* EP0 protocol, major revision */
44#define EP0ATSPI_MINOR 1 /* EP0 protocol, minor revision */
45
46#define HW_TYPE_100813 0 /* 100813 */
47
48
49/*
50 * bmRequestType:
51 *
52 * D7 D6..5 D4...0
53 * | | |
54 * direction (0 = host->dev)
55 * type (2 = vendor)
56 * recipient (0 = device)
57 */
58
59
60#define ATSPI_TO_DEV(req) (0x40 | (req) << 8)
61#define ATSPI_FROM_DEV(req) (0xc0 | (req) << 8)
62
63
64enum atspi_requests {
65    ATSPI_ID = 0x00,
66    ATSPI_BUILD,
67    ATSPI_RESET,
68    ATSPI_RF_RESET = 0x10,
69    ATSPI_POLL_INT,
70    ATSPI_TEST,
71    ATSPI_REG_WRITE = 0x20,
72    ATSPI_REG_READ,
73    ATSPI_BUF_WRITE,
74    ATSPI_BUF_READ,
75    ATSPI_SRAM_WRITE,
76    ATSPI_SRAM_READ,
77};
78
79
80void ep0_init(void);
81
82#endif /* !EP0_H */
atusb/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 */
atusb/fw/include/atusb/ep0.h
1/*
2 * include/atusb/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 - - 0
25 * ->host ATSPI_POLL_INT - - 1
26 * host-> ATSPI_TEST - - 0
27 *
28 * host-> ATSPI_REG_WRITE value addr 0
29 * ->host ATSPI_REG_READ - addr 1
30 * host-> ATSPI_BUF_WRITE - - #bytes
31 * ->host ATSPI_BUF_READ - - #bytes
32 * host-> ATSPI_SRAM_WRITE - addr #bytes
33 * ->host ATSPI_SRAM_READ - addr #bytes
34 */
35
36/*
37 * EP0 protocol:
38 *
39 * 0.0 initial release
40 * 0.1 addition of ATSPI_TEST
41 */
42
43#define EP0ATSPI_MAJOR 0 /* EP0 protocol, major revision */
44#define EP0ATSPI_MINOR 1 /* EP0 protocol, minor revision */
45
46#define HW_TYPE_100813 0 /* 100813 */
47
48
49/*
50 * bmRequestType:
51 *
52 * D7 D6..5 D4...0
53 * | | |
54 * direction (0 = host->dev)
55 * type (2 = vendor)
56 * recipient (0 = device)
57 */
58
59
60#define ATSPI_TO_DEV(req) (0x40 | (req) << 8)
61#define ATSPI_FROM_DEV(req) (0xc0 | (req) << 8)
62
63
64enum atspi_requests {
65    ATSPI_ID = 0x00,
66    ATSPI_BUILD,
67    ATSPI_RESET,
68    ATSPI_RF_RESET = 0x10,
69    ATSPI_POLL_INT,
70    ATSPI_TEST,
71    ATSPI_REG_WRITE = 0x20,
72    ATSPI_REG_READ,
73    ATSPI_BUF_WRITE,
74    ATSPI_BUF_READ,
75    ATSPI_SRAM_WRITE,
76    ATSPI_SRAM_READ,
77};
78
79
80void ep0_init(void);
81
82#endif /* !EP0_H */
atusb/fw/include/atusb/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/atspi-id/atspi-id.c
1919#endif
2020
2121#include "at86rf230.h"
22#include "atspi/ep0.h"
22#include "atusb/ep0.h"
2323#include "atspi.h"
2424
2525
tools/atspi-trim/atspi-trim.c
1616#include <sys/time.h>
1717
1818#include "at86rf230.h"
19#include "atspi/ep0.h"
2019#include "atspi.h"
2120
2221
tools/lib/atusb.c
1515#include <usb.h>
1616
1717#include "f32xbase/usb.h"
18#include "atspi/ep0.h"
19#include "atspi/usb-ids.h"
18#include "atusb/ep0.h"
19#include "atusb/usb-ids.h"
2020
2121#include "driver.h"
2222

Archive Download the corresponding diff file



interactive