Root/atusb/fw/ep0.c

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

Archive Download this file



interactive