Root/atusb/fw/ep0.c

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

Archive Download this file



interactive