Root/atusb/fw/ep0.c

Source at commit f7b9a18917b8ed913f33bda55a33a87cf419c352 created 8 years 4 months ago.
By Werner Almesberger, atusb/fw/: new request ATUSB_GPIO_CLEANUP to re-enable INT0
1/*
2 * fw/ep0.c - EP0 extension protocol
3 *
4 * Written 2008-2011 by Werner Almesberger
5 * Copyright 2008-2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15#include <string.h>
16
17#include <avr/io.h>
18
19#ifndef NULL
20#define NULL 0
21#endif
22
23#include "usb.h"
24#include "dfu.h"
25
26#include "at86rf230.h"
27#include "atusb/ep0.h"
28#include "version.h"
29#include "board.h"
30#include "sernum.h"
31#include "spi.h"
32
33
34#define HW_TYPE HW_TYPE_110131
35
36#define debug(...)
37#define error(...)
38
39
40static const uint8_t id[] = { EP0ATUSB_MAJOR, EP0ATUSB_MINOR, HW_TYPE };
41static uint8_t buf[MAX_PSDU+3]; /* command, PHDR, and LQI */
42static uint8_t size;
43
44
45static void do_buf_write(void *user)
46{
47    uint8_t i;
48
49    spi_begin();
50    for (i = 0; i != size; i++)
51        spi_send(buf[i]);
52    spi_end();
53}
54
55
56#define BUILD_OFFSET 7 /* '#' plus "65535" plus ' ' */
57
58
59static int my_setup(const struct setup_request *setup)
60{
61    uint16_t req = setup->bmRequestType | setup->bRequest << 8;
62    unsigned tmp;
63    uint8_t i;
64    uint64_t tmp64;
65
66    switch (req) {
67    case ATUSB_FROM_DEV(ATUSB_ID):
68        debug("ATUSB_ID\n");
69        if (setup->wLength > 3)
70            return 0;
71        usb_send(&eps[0], id, setup->wLength, NULL, NULL);
72        return 1;
73    case ATUSB_FROM_DEV(ATUSB_BUILD):
74        debug("ATUSB_BUILD\n");
75        tmp = build_number;
76        for (i = BUILD_OFFSET-2; tmp; i--) {
77            buf[i] = (tmp % 10)+'0';
78            tmp /= 10;
79        }
80        buf[i] = '#';
81        buf[BUILD_OFFSET-1] = ' ';
82        for (size = 0; build_date[size]; size++)
83            buf[BUILD_OFFSET+size] = build_date[size];
84        size += BUILD_OFFSET-i;
85        if (size > setup->wLength)
86            return 0;
87        usb_send(&eps[0], buf+i, size, NULL, NULL);
88        return 1;
89
90    case ATUSB_TO_DEV(ATUSB_RESET):
91        debug("ATUSB_RESET\n");
92        reset_cpu();
93        while (1);
94
95    case ATUSB_TO_DEV(ATUSB_RF_RESET):
96        debug("ATUSB_RF_RESET\n");
97        reset_rf();
98        //ep_send_zlp(EP_CTRL);
99        return 1;
100
101    case ATUSB_FROM_DEV(ATUSB_POLL_INT):
102        debug("ATUSB_POLL_INT\n");
103        if (setup->wLength < 1)
104            return 0;
105        *buf = read_irq();
106        usb_send(&eps[0], buf, 1, NULL, NULL);
107        return 1;
108
109    case ATUSB_FROM_DEV(ATUSB_TIMER):
110        debug("ATUSB_TIMER\n");
111        size = setup->wLength;
112        if (size > sizeof(tmp64))
113            size = sizeof(tmp64);
114        tmp64 = timer_read();
115        memcpy(buf, &tmp64, sizeof(tmp64));
116        usb_send(&eps[0], buf, size, NULL, NULL);
117        return 1;
118
119    case ATUSB_FROM_DEV(ATUSB_GPIO):
120        debug("ATUSB_GPIO\n");
121        if (setup->wLength < 3)
122            return 0;
123        if (!gpio(setup->wIndex, setup->wValue, setup->wValue >> 8,
124            setup->wIndex >> 8, buf))
125            return 0;
126        usb_send(&eps[0], buf, 3, NULL, NULL);
127        return 1;
128    case ATUSB_TO_DEV(ATUSB_GPIO_CLEANUP):
129        gpio_cleanup();
130        return 1;
131
132    case ATUSB_TO_DEV(ATUSB_SLP_TR):
133        debug("ATUSB_SLP_TR\n");
134        slp_tr();
135        return 1;
136
137    case ATUSB_TO_DEV(ATUSB_REG_WRITE):
138        debug("ATUSB_REG_WRITE\n");
139        spi_begin();
140        spi_send(AT86RF230_REG_WRITE | setup->wIndex);
141        spi_send(setup->wValue);
142        spi_end();
143        //ep_send_zlp(EP_CTRL);
144        return 1;
145    case ATUSB_FROM_DEV(ATUSB_REG_READ):
146        debug("ATUSB_REG_READ\n");
147        spi_begin();
148        spi_send(AT86RF230_REG_READ | setup->wIndex);
149        *buf = spi_recv();
150        spi_end();
151        usb_send(&eps[0], buf, 1, NULL, NULL);
152        return 1;
153
154    case ATUSB_TO_DEV(ATUSB_BUF_WRITE):
155        debug("ATUSB_BUF_WRITE\n");
156        if (setup->wLength < 1)
157            return 0;
158        if (setup->wLength > MAX_PSDU)
159            return 0;
160        buf[0] = AT86RF230_BUF_WRITE;
161        buf[1] = setup->wLength;
162        size = setup->wLength+2;
163        usb_recv(&eps[0], buf+2, setup->wLength, do_buf_write, NULL);
164        return 1;
165    case ATUSB_FROM_DEV(ATUSB_BUF_READ):
166        debug("ATUSB_BUF_READ\n");
167        if (setup->wLength < 2) /* PHR+LQI */
168            return 0;
169        if (setup->wLength > MAX_PSDU+2) /* PHR+PSDU+LQI */
170            return 0;
171        spi_begin();
172        spi_send(AT86RF230_BUF_READ);
173        size = spi_recv();
174        if (size >= setup->wLength)
175            size = setup->wLength-1;
176        for (i = 0; i != size+1; i++)
177            buf[i] = spi_recv();
178        spi_end();
179        usb_send(&eps[0], buf, size+1, NULL, NULL);
180        return 1;
181
182    case ATUSB_TO_DEV(ATUSB_SRAM_WRITE):
183        debug("ATUSB_SRAM_WRITE\n");
184        if (setup->wIndex > SRAM_SIZE)
185            return 0;
186        if (setup->wIndex+setup->wLength > SRAM_SIZE)
187            return 0;
188        buf[0] = AT86RF230_SRAM_WRITE;
189        buf[1] = setup->wIndex;
190        size = setup->wLength+2;
191        usb_recv(&eps[0], buf+2, setup->wLength, do_buf_write, NULL);
192        return 1;
193    case ATUSB_FROM_DEV(ATUSB_SRAM_READ):
194        debug("ATUSB_SRAM_READ\n");
195        if (setup->wIndex > SRAM_SIZE)
196            return 0;
197        if (setup->wIndex+setup->wLength > SRAM_SIZE)
198            return 0;
199        spi_begin();
200        spi_send(AT86RF230_SRAM_READ);
201        spi_send(setup->wIndex);
202        for (i = 0; i != setup->wLength; i++)
203            buf[i] = spi_recv();
204        spi_end();
205        usb_send(&eps[0], buf, setup->wLength, NULL, NULL);
206        return 1;
207
208    case ATUSB_TO_DEV(ATUSB_SPI_WRITE):
209        size = setup->wLength+2;
210        if (size > sizeof(buf))
211            return 0;
212        buf[0] = setup->wValue;
213        buf[1] = setup->wIndex;
214        if (setup->wLength)
215            usb_recv(&eps[0], buf+2, setup->wLength,
216                do_buf_write, NULL);
217        else
218            do_buf_write(NULL);
219        return 1;
220    case ATUSB_FROM_DEV(ATUSB_SPI_READ1):
221    case ATUSB_FROM_DEV(ATUSB_SPI_READ2):
222        spi_begin();
223        spi_send(setup->wValue);
224        if (req == ATUSB_FROM_DEV(ATUSB_SPI_READ2))
225            spi_send(setup->wIndex);
226        for (i = 0; i != setup->wLength; i++)
227            buf[i] = spi_recv();
228        spi_end();
229        usb_send(&eps[0], buf, setup->wLength, NULL, NULL);
230        return 1;
231
232    default:
233        error("Unrecognized SETUP: 0x%02x 0x%02x ...\n",
234            setup->bmRequestType, setup->bRequest);
235        return 0;
236    }
237}
238
239
240static int my_dfu_setup(const struct setup_request *setup)
241{
242    switch (setup->bmRequestType | setup->bRequest << 8) {
243    case DFU_TO_DEV(DFU_DETACH):
244        /* @@@ should use wTimeout */
245        dfu.state = appDETACH;
246        return 1;
247    default:
248        return dfu_setup_common(setup);
249    }
250}
251
252
253static void my_set_interface(int nth)
254{
255    if (nth) {
256        user_setup = my_dfu_setup;
257        user_get_descriptor = dfu_my_descr;
258        dfu.state = appIDLE;
259    } else {
260        user_setup = my_setup;
261        user_get_descriptor = sernum_get_descr;
262    }
263}
264
265
266static void my_reset(void)
267{
268    if (dfu.state == appDETACH)
269        reset_cpu();
270}
271
272
273void ep0_init(void)
274{
275    user_setup = my_setup;
276    user_set_interface = my_set_interface;
277    my_set_interface(0);
278    user_reset = my_reset;
279}
280

Archive Download this file



interactive