Root/atrf/fw/atspi/ep0.c

Source at commit c2dd23840ce5875deca09673fcb8abb8dc3a8e7a created 9 years 1 month ago.
By Werner Almesberger, Moved fw/ and tools/ into atrf/ as well.
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}
228

Archive Download this file



interactive