Root/atusb/fw/ep0.c

Source at commit 3f74098ce28b00227010f97cd615271de29c1ecd created 8 years 4 days ago.
By Werner Almesberger, atusb/fw/ep0.c (my_setup): fixed test for ATUSB_SPI_READ2
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
129    case ATUSB_TO_DEV(ATUSB_SLP_TR):
130        debug("ATUSB_SLP_TR\n");
131        slp_tr();
132        return 1;
133
134    case ATUSB_TO_DEV(ATUSB_REG_WRITE):
135        debug("ATUSB_REG_WRITE\n");
136        spi_begin();
137        spi_send(AT86RF230_REG_WRITE | setup->wIndex);
138        spi_send(setup->wValue);
139        spi_end();
140        //ep_send_zlp(EP_CTRL);
141        return 1;
142    case ATUSB_FROM_DEV(ATUSB_REG_READ):
143        debug("ATUSB_REG_READ\n");
144        spi_begin();
145        spi_send(AT86RF230_REG_READ | setup->wIndex);
146        *buf = spi_recv();
147        spi_end();
148        usb_send(&eps[0], buf, 1, NULL, NULL);
149        return 1;
150
151    case ATUSB_TO_DEV(ATUSB_BUF_WRITE):
152        debug("ATUSB_BUF_WRITE\n");
153        if (setup->wLength < 1)
154            return 0;
155        if (setup->wLength > MAX_PSDU)
156            return 0;
157        buf[0] = AT86RF230_BUF_WRITE;
158        buf[1] = setup->wLength;
159        size = setup->wLength+2;
160        usb_recv(&eps[0], buf+2, setup->wLength, do_buf_write, NULL);
161        return 1;
162    case ATUSB_FROM_DEV(ATUSB_BUF_READ):
163        debug("ATUSB_BUF_READ\n");
164        if (setup->wLength < 2) /* PHR+LQI */
165            return 0;
166        if (setup->wLength > MAX_PSDU+2) /* PHR+PSDU+LQI */
167            return 0;
168        spi_begin();
169        spi_send(AT86RF230_BUF_READ);
170        size = spi_recv();
171        if (size >= setup->wLength)
172            size = setup->wLength-1;
173        for (i = 0; i != size+1; i++)
174            buf[i] = spi_recv();
175        spi_end();
176        usb_send(&eps[0], buf, size+1, NULL, NULL);
177        return 1;
178
179    case ATUSB_TO_DEV(ATUSB_SRAM_WRITE):
180        debug("ATUSB_SRAM_WRITE\n");
181        if (setup->wIndex > SRAM_SIZE)
182            return 0;
183        if (setup->wIndex+setup->wLength > SRAM_SIZE)
184            return 0;
185        buf[0] = AT86RF230_SRAM_WRITE;
186        buf[1] = setup->wIndex;
187        size = setup->wLength+2;
188        usb_recv(&eps[0], buf+2, setup->wLength, do_buf_write, NULL);
189        return 1;
190    case ATUSB_FROM_DEV(ATUSB_SRAM_READ):
191        debug("ATUSB_SRAM_READ\n");
192        if (setup->wIndex > SRAM_SIZE)
193            return 0;
194        if (setup->wIndex+setup->wLength > SRAM_SIZE)
195            return 0;
196        spi_begin();
197        spi_send(AT86RF230_SRAM_READ);
198        spi_send(setup->wIndex);
199        for (i = 0; i != setup->wLength; i++)
200            buf[i] = spi_recv();
201        spi_end();
202        usb_send(&eps[0], buf, setup->wLength, NULL, NULL);
203        return 1;
204
205    case ATUSB_TO_DEV(ATUSB_SPI_WRITE):
206        size = setup->wLength+2;
207        if (size > sizeof(buf))
208            return 0;
209        buf[0] = setup->wValue;
210        buf[1] = setup->wIndex;
211        if (setup->wLength)
212            usb_recv(&eps[0], buf+2, setup->wLength,
213                do_buf_write, NULL);
214        else
215            do_buf_write(NULL);
216        return 1;
217    case ATUSB_FROM_DEV(ATUSB_SPI_READ1):
218    case ATUSB_FROM_DEV(ATUSB_SPI_READ2):
219        spi_begin();
220        spi_send(setup->wValue);
221        if (req == ATUSB_FROM_DEV(ATUSB_SPI_READ2))
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    default:
230        error("Unrecognized SETUP: 0x%02x 0x%02x ...\n",
231            setup->bmRequestType, setup->bRequest);
232        return 0;
233    }
234}
235
236
237static int my_dfu_setup(const struct setup_request *setup)
238{
239    switch (setup->bmRequestType | setup->bRequest << 8) {
240    case DFU_TO_DEV(DFU_DETACH):
241        /* @@@ should use wTimeout */
242        dfu.state = appDETACH;
243        return 1;
244    default:
245        return dfu_setup_common(setup);
246    }
247}
248
249
250static void my_set_interface(int nth)
251{
252    if (nth) {
253        user_setup = my_dfu_setup;
254        user_get_descriptor = dfu_my_descr;
255        dfu.state = appIDLE;
256    } else {
257        user_setup = my_setup;
258        user_get_descriptor = sernum_get_descr;
259    }
260}
261
262
263static void my_reset(void)
264{
265    if (dfu.state == appDETACH)
266        reset_cpu();
267}
268
269
270void ep0_init(void)
271{
272    user_setup = my_setup;
273    user_set_interface = my_set_interface;
274    my_set_interface(0);
275    user_reset = my_reset;
276}
277

Archive Download this file



interactive