Date:2010-08-20 06:50:51 (9 years 11 months ago)
Author:Werner Almesberger
Commit:5447951c6e50a42ba7f47d9bfd7beac3e3f8926e
Message:Implemented AT86RF230 reset and access modes. (Completely untested.)

- fw/atspi/at86rf230.h: AT86RF230 SPI protocol commands and buffer sizes
- fw/atspi/atspi.c (init_io): set all IOs to their initial state
- fw/include/atspi/ep0.h, fw/atspi/ep0.c (my_setup): command ATSPI_RF_RESET
to reset the AT86RF230
- fw/include/atspi/ep0.h, fw/atspi/ep0.c (my_setup): commands
ATSPI_{REG,BUF,SRAM}_{READ,WRITE} for the six SPI access modes
Files: fw/atspi/at86rf230.h (1 diff)
fw/atspi/atspi.c (1 diff)
fw/atspi/ep0.c (3 diffs)
fw/include/atspi/ep0.h (2 diffs)

Change Details

fw/atspi/at86rf230.h
1/*
2 * atspi/at86rf230.h - AT86RF230 protocol and register definitions
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#ifndef AT86RF230_H
15#define AT86RF230_H
16
17enum at86rf230_spi_cmd {
18    AT86RF230_REG_WRITE = 0xc0, /* 11... */
19    AT86RF230_REG_READ = 0x80, /* 10... */
20    AT86RF230_BUF_WRITE = 0x60, /* 011... */
21    AT86RF230_BUF_READ = 0x20, /* 001... */
22    AT86RF230_SRAM_WRITE = 0x40, /* 010... */
23    AT86RF230_SRAM_READ = 0x00 /* 000... */
24};
25
26#define MAX_PSDU 127 /* octets, see AT86RF230 manual section 8.1 */
27#define SRAM_SIZE 128
28
29#endif /* !AT86RF230_H */
fw/atspi/atspi.c
1818#include "version.h"
1919
2020
21static void init_io(void)
22{
23    /*
24     * Signal Mode Value
25     *
26     * MOSI push-pull 0
27     * MISO open drain 1 (input)
28     * SCLK push-pull 1
29     * nSS push-pull 1
30     * nRST_RF push-pull 1
31     * IRQ_RF open drain 1 (input)
32     * SLP_TR push-pull 0
33     *
34     * LED push-pull 0 (set up by boot loader)
35     *
36     * all unused open drain 0
37     */
38
39    MOSI = 0;
40    MOSI_MODE = 1;
41
42    SCLK = 0;
43    SCLK_MODE = 1;
44
45    nSS_MODE = 1;
46
47    nRST_RF_MODE = 1;
48
49    SLP_TR = 0;
50    SLP_TR_MODE = 1;
51
52    P0 &=
53        ~((1 << 0) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
54    P3 = 0;
55
56}
57
58
2159void main(void)
2260{
61    init_io();
62
2363    usb_init();
2464    ep0_init();
2565
fw/atspi/ep0.c
2121//#include "uart.h"
2222#include "usb.h"
2323#include "atspi/ep0.h"
24#include "at86rf230.h"
2425#include "version.h"
2526
2627
...... 
2930
3031
3132static const uint8_t id[] = { EP0ATSPI_MAJOR, EP0ATSPI_MINOR, HW_TYPE };
33static __xdata uint8_t buf[MAX_PSDU+3]; /* command, PHDR, and LQ */
34static uint8_t size;
35
36
37static void spi_send(uint8_t v)
38{
39    uint8_t mask;
40
41    for (mask = 0x80; mask; mask >>= 1) {
42        MOSI = !!(v & mask);
43        SCLK = 1;
44        SCLK = 0;
45    }
46}
47
48
49static uint8_t spi_recv(void)
50{
51    uint8_t res = 0;
52    uint8_t i;
53
54    for (i = 0; i != 8; i++) {
55        res = (res << 1) | MISO;
56        SCLK = 1;
57        SCLK = 0;
58    }
59    return res;
60}
61
62
63static void do_buf_write(void *user)
64{
65    uint8_t i;
66
67    user; /* suppress warning */
68    nSS = 0;
69    spi_send(AT86RF230_BUF_WRITE);
70    for (i = 0; i != size; i++)
71        spi_send(buf[i]);
72    nSS = 1;
73}
3274
3375
3476static __bit my_setup(struct setup_request *setup) __reentrant
3577{
36    uint8_t size;
78    uint8_t i;
3779
3880    switch (setup->bmRequestType | setup->bRequest << 8) {
3981    case ATSPI_FROM_DEV(ATSPI_ID):
...... 
63105        RSTSRC = SWRSF;
64106        while (1);
65107
108    case ATSPI_TO_DEV(ATSPI_RF_RESET):
109        debug("ATSPI_RF_RESET\n");
110        nRST_RF = 0;
111        /* 11.4.12 min 625 ns */
112        nRST_RF = 1;
113        return 1;
114
115    case ATSPI_TO_DEV(ATSPI_REG_WRITE):
116        debug("ATSPI_REG_WRITE\n");
117        nSS = 0;
118        spi_send(AT86RF230_REG_WRITE | setup->wIndex);
119        spi_send(setup->wValue);
120        nSS = 1;
121        return 1;
122    case ATSPI_FROM_DEV(ATSPI_REG_READ):
123        debug("ATSPI_REG_READ\n");
124        nSS = 0;
125        spi_send(AT86RF230_REG_READ | setup->wIndex);
126        *buf = spi_recv();
127        nSS = 1;
128        usb_send(&ep0, buf, 1, NULL, NULL);
129        return 1;
130
131    case ATSPI_TO_DEV(ATSPI_BUF_WRITE):
132        debug("ATSPI_BUF_WRITE\n");
133        if (setup->wLength < 1)
134            return 0;
135        if (setup->wLength > MAX_PSDU+1) /* PHR+PSDU */
136            return 0;
137        buf[0] = AT86RF230_BUF_WRITE;
138        size = setup->wLength+1;
139        usb_recv(&ep0, buf+1, setup->wLength, do_buf_write, NULL);
140        return 1;
141    case ATSPI_TO_DEV(ATSPI_BUF_READ):
142        debug("ATSPI_BUF_READ\n");
143        if (setup->wLength < 2) /* PHR+LQ */
144            return 0;
145        if (setup->wLength > MAX_PSDU+2) /* PHR+PSDU+LQ */
146            return 0;
147        nSS = 0;
148        spi_send(AT86RF230_BUF_READ);
149        size = *buf = spi_recv();
150        if (size+2 > setup->wLength)
151            size = setup->wLength-2;
152        for (i = 0; i != size+1; i++)
153            buf[i+1] = spi_recv();
154        nSS = 1;
155        usb_send(&ep0, buf, size+1, NULL, NULL);
156        return 1;
157
158    case ATSPI_TO_DEV(ATSPI_SRAM_WRITE):
159        debug("ATSPI_SRAM_WRITE\n");
160        if (setup->wIndex > SRAM_SIZE)
161            return 0;
162        if (setup->wIndex+setup->wLength > SRAM_SIZE)
163            return 0;
164        buf[0] = AT86RF230_SRAM_WRITE;
165        buf[1] = setup->wIndex;
166        size = setup->wLength+2;
167        usb_recv(&ep0, buf+2, setup->wLength, do_buf_write, NULL);
168        return 1;
169    case ATSPI_TO_DEV(ATSPI_SRAM_READ):
170        debug("ATSPI_SRAM_READ\n");
171        if (setup->wIndex > SRAM_SIZE)
172            return 0;
173        if (setup->wIndex+setup->wLength > SRAM_SIZE)
174            return 0;
175        nSS = 0;
176        spi_send(AT86RF230_SRAM_READ);
177        spi_send(setup->wIndex);
178        for (i = 0; i != size; i++)
179            buf[i] = spi_recv();
180        nSS = 1;
181        usb_send(&ep0, buf, size, NULL, NULL);
182        return 1;
183
66184    default:
67185        error("Unrecognized SETUP: 0x%02x 0x%02x ...\n",
68186            setup->bmRequestType, setup->bRequest);
fw/include/atspi/ep0.h
1717/*
1818 * Direction bRequest wValue wIndex wLength
1919 *
20 * ->host ATSPI_ID 0 0 3
21 * host-> ATSPI_RESET 0 0 0
20 * ->host ATSPI_ID - - 3
21 * host-> ATSPI_RESET - - 0
2222 * ->host ATSPI_BUILD_NUMBER - - 2
2323 * ->host ATSPI_BUILD_DATE - - #bytes
24 *
25 * host-> ATSPI_RF_RESET - -
26 *
27 * host-> ATSPI_REG_WRITE value addr 0
28 * ->host ATSPI_REG_READ - addr 1
29 * host-> ATSPI_BUF_WRITE - - #bytes
30 * ->host ATSPI_BUF_READ - - #bytes
31 * host-> ATSPI_SRAM_WRITE - addr #bytes
32 * ->host ATSPI_SRAM_READ - addr #bytes
2433 */
2534
2635/*
...... 
5564    ATSPI_RESET,
5665    ATSPI_BUILD_NUMBER,
5766    ATSPI_BUILD_DATE,
67    ATSPI_RF_RESET = 0x10,
68    ATSPI_REG_WRITE = 0x20,
69    ATSPI_REG_READ,
70    ATSPI_BUF_WRITE,
71    ATSPI_BUF_READ,
72    ATSPI_SRAM_WRITE,
73    ATSPI_SRAM_READ,
5874};
5975
6076

Archive Download the corresponding diff file



interactive