Root/atusb/fw2/ep0.c

Source at commit dcfa689936afb14fd0fdf804ba0aad08fa03f8f9 created 9 years 7 months ago.
By Werner Almesberger, atusb/fw2/: assigned hardware revision code point; some cleanup
1/*
2 * atspi/ep0.c - EP0 extension protocol
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 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
16#include <avr/io.h>
17//#ifndef NULL
18//#define NULL 0
19//#endif
20
21//#include "regs.h"
22//#include "uart.h"
23//#include "usb.h"
24
25#include "freakusb.h"
26
27#include "at86rf230.h"
28#include "atusb/ep0.h"
29//#include "version.h"
30#include "board.h"
31#include "spi.h"
32
33
34#define HW_TYPE HW_TYPE_110131
35static const char *build_date = "today";
36static unsigned build_number = 42;
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
58static void do_usb_send(const uint8_t *data, int len)
59{
60    int i;
61
62    for (i = 0; i++ != len; data++) {
63        usb_buf_write(EP_CTRL, *data);
64        if (!(i % MAX_BUF_SZ))
65            ep_write(EP_CTRL);
66    }
67    ep_write(EP_CTRL);
68}
69
70
71static void do_usb_recv(uint8_t *data, int len, void (*fn)(void *user),
72    void *user)
73{
74    volatile usb_pcb_t *pcb = usb_pcb_get();
75    uint8_t got;
76
77    /* FreakUSB likes to do things this way. Looks dangerous ... */
78    while (len) {
79        for (got = pcb->fifo[EP_CTRL].len; got; got--) {
80            *data++ = usb_buf_read(EP_CTRL);
81            len--;
82        }
83    }
84    pcb->flags &= ~(1 << SETUP_DATA_AVAIL);
85    fn(user);
86    ep_send_zlp(EP_CTRL);
87}
88
89
90#define usb_send(ep, buf, len, arg1, arg2) do_usb_send(buf, len)
91#define usb_recv(ep, buf, len, fn, user) do_usb_recv(buf, len, fn, user)
92
93
94#define BUILD_OFFSET 7 /* '#' plus "65535" plus ' ' */
95
96
97/* keep things as similar to the original as possible for now */
98#define setup_request req_t
99#define setup req
100#define bmRequestType type
101#define bRequest req
102#define wValue val
103#define wLength len
104#define wIndex idx
105#define __reentrant
106
107
108static int my_setup(struct setup_request *setup) __reentrant
109{
110    unsigned tmp;
111    uint8_t i;
112
113    switch (setup->bmRequestType | setup->bRequest << 8) {
114    case ATUSB_FROM_DEV(ATUSB_ID):
115        debug("ATUSB_ID\n");
116        if (setup->wLength > 3)
117            return 0;
118        usb_send(&ep0, id, setup->wLength, NULL, NULL);
119        return 1;
120    case ATUSB_FROM_DEV(ATUSB_BUILD):
121        debug("ATUSB_BUILD\n");
122        tmp = build_number;
123        for (i = BUILD_OFFSET-2; tmp; i--) {
124            buf[i] = (tmp % 10)+'0';
125            tmp /= 10;
126        }
127        buf[i] = '#';
128        buf[BUILD_OFFSET-1] = ' ';
129        for (size = 0; build_date[size]; size++)
130            buf[BUILD_OFFSET+size] = build_date[size];
131        size += BUILD_OFFSET-i;
132        if (size > setup->wLength)
133            return 0;
134        usb_send(&ep0, buf+i, size, NULL, NULL);
135        return 1;
136
137#ifdef NOTYET
138    case ATUSB_TO_DEV(ATUSB_RESET):
139        debug("ATUSB_RESET\n");
140        RSTSRC = SWRSF;
141        while (1);
142#endif
143
144    case ATUSB_TO_DEV(ATUSB_RF_RESET):
145        debug("ATUSB_RF_RESET\n");
146        reset_rf();
147        ep_send_zlp(EP_CTRL);
148        return 1;
149
150    case ATUSB_FROM_DEV(ATUSB_POLL_INT):
151        debug("ATUSB_POLL_INT\n");
152        if (setup->wLength < 1)
153            return 0;
154        *buf = 0;//IRQ_RF;
155        usb_send(&ep0, buf, 1, NULL, NULL);
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(&ep0, 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(&ep0, 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(&ep0, 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(&ep0, buf+2, setup->wLength, do_buf_write, NULL);
213        return 1;
214    case ATUSB_TO_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 != size; i++)
224            buf[i] = spi_recv();
225        spi_end();
226        usb_send(&ep0, buf, size, 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 void class_init(void)
238{
239}
240
241
242static void req_handler(req_t *req)
243{
244    if (!my_setup(req))
245        ep_set_stall(EP_CTRL);
246}
247
248
249static void rx_handler(void)
250{
251}
252
253
254void ep0_init(void)
255{
256        usb_reg_class_drvr(class_init, req_handler, rx_handler);
257}
258

Archive Download this file



interactive