IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Commit Details
Date: | 2010-08-20 06:50:51 (13 years 7 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 | ||
17 | enum 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 | ||
---|---|---|
18 | 18 | #include "version.h" |
19 | 19 | |
20 | 20 | |
21 | static 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 | ||
21 | 59 | void main(void) |
22 | 60 | { |
61 | init_io(); | |
62 | ||
23 | 63 | usb_init(); |
24 | 64 | ep0_init(); |
25 | 65 |
fw/atspi/ep0.c | ||
---|---|---|
21 | 21 | //#include "uart.h" |
22 | 22 | #include "usb.h" |
23 | 23 | #include "atspi/ep0.h" |
24 | #include "at86rf230.h" | |
24 | 25 | #include "version.h" |
25 | 26 | |
26 | 27 | |
... | ... | |
29 | 30 | |
30 | 31 | |
31 | 32 | static const uint8_t id[] = { EP0ATSPI_MAJOR, EP0ATSPI_MINOR, HW_TYPE }; |
33 | static __xdata uint8_t buf[MAX_PSDU+3]; /* command, PHDR, and LQ */ | |
34 | static uint8_t size; | |
35 | ||
36 | ||
37 | static 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 | ||
49 | static 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 | ||
63 | static 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 | } | |
32 | 74 | |
33 | 75 | |
34 | 76 | static __bit my_setup(struct setup_request *setup) __reentrant |
35 | 77 | { |
36 | uint8_t size; | |
78 | uint8_t i; | |
37 | 79 | |
38 | 80 | switch (setup->bmRequestType | setup->bRequest << 8) { |
39 | 81 | case ATSPI_FROM_DEV(ATSPI_ID): |
... | ... | |
63 | 105 | RSTSRC = SWRSF; |
64 | 106 | while (1); |
65 | 107 | |
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 | ||
66 | 184 | default: |
67 | 185 | error("Unrecognized SETUP: 0x%02x 0x%02x ...\n", |
68 | 186 | setup->bmRequestType, setup->bRequest); |
fw/include/atspi/ep0.h | ||
---|---|---|
17 | 17 | /* |
18 | 18 | * Direction bRequest wValue wIndex wLength |
19 | 19 | * |
20 | * ->host ATSPI_ID 0 0 3 | |
21 | * host-> ATSPI_RESET 0 0 0 | |
20 | * ->host ATSPI_ID - - 3 | |
21 | * host-> ATSPI_RESET - - 0 | |
22 | 22 | * ->host ATSPI_BUILD_NUMBER - - 2 |
23 | 23 | * ->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 | |
24 | 33 | */ |
25 | 34 | |
26 | 35 | /* |
... | ... | |
55 | 64 | ATSPI_RESET, |
56 | 65 | ATSPI_BUILD_NUMBER, |
57 | 66 | 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, | |
58 | 74 | }; |
59 | 75 | |
60 | 76 |