Root/atusb/fw/ep0.c

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

Archive Download this file



interactive