Root/atusb/fw/ep0.c

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

Archive Download this file



interactive