IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
Source at commit dce846293982409b4027cd93e03b686750ab6195 created 7 years 11 months ago. By Stefan Schmidt, atusb: fw: leave the FCS check to the kernel stack | |
---|---|
1 | /* |
2 | * fw/mac.c - HardMAC functions |
3 | * |
4 | * Written 2011, 2013 by Werner Almesberger |
5 | * Copyright 2011, 2013 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 | #include <stddef.h> |
14 | #include <stdbool.h> |
15 | #include <stdint.h> |
16 | |
17 | #include "usb.h" |
18 | |
19 | #include "at86rf230.h" |
20 | #include "spi.h" |
21 | #include "board.h" |
22 | #include "mac.h" |
23 | |
24 | #define RX_BUFS 3 |
25 | |
26 | |
27 | bool (*mac_irq)(void) = NULL; |
28 | |
29 | |
30 | static uint8_t rx_buf[RX_BUFS][MAX_PSDU+2]; /* PHDR+payload+LQ */ |
31 | static uint8_t tx_buf[MAX_PSDU]; |
32 | static uint8_t tx_size = 0; |
33 | static bool txing = 0; |
34 | static bool queued_tx_ack = 0; |
35 | static uint8_t next_seq, this_seq, queued_seq; |
36 | |
37 | |
38 | /* ----- Receive buffer management ----------------------------------------- */ |
39 | |
40 | |
41 | static uint8_t rx_in = 0, rx_out = 0; |
42 | |
43 | |
44 | static inline void next_buf(uint8_t *index) |
45 | { |
46 | *index = (*index+1) % RX_BUFS; |
47 | } |
48 | |
49 | |
50 | /* ----- Register access --------------------------------------------------- */ |
51 | |
52 | |
53 | static uint8_t reg_read(uint8_t reg) |
54 | { |
55 | uint8_t value; |
56 | |
57 | spi_begin(); |
58 | spi_send(AT86RF230_REG_READ | reg); |
59 | value = spi_recv(); |
60 | spi_end(); |
61 | |
62 | return value; |
63 | } |
64 | |
65 | |
66 | static void reg_write(uint8_t reg, uint8_t value) |
67 | { |
68 | spi_begin(); |
69 | spi_send(AT86RF230_REG_WRITE | reg); |
70 | spi_send(value); |
71 | spi_end(); |
72 | } |
73 | |
74 | |
75 | /* ----- Interrupt handling ------------------------------------------------ */ |
76 | |
77 | |
78 | static void rx_done(void *user); |
79 | static void tx_ack_done(void *user); |
80 | |
81 | |
82 | static void usb_next(void) |
83 | { |
84 | const uint8_t *buf; |
85 | |
86 | if (rx_in != rx_out) { |
87 | buf = rx_buf[rx_out]; |
88 | led(1); |
89 | usb_send(&eps[1], buf, buf[0]+2, rx_done, NULL); |
90 | } |
91 | |
92 | if (queued_tx_ack) { |
93 | usb_send(&eps[1], &queued_seq, 1, tx_ack_done, NULL); |
94 | queued_tx_ack = 0; |
95 | } |
96 | } |
97 | |
98 | |
99 | static void tx_ack_done(void *user) |
100 | { |
101 | usb_next(); |
102 | } |
103 | |
104 | static void change_state(uint8_t new) |
105 | { |
106 | while ((reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK) == |
107 | TRX_STATUS_TRANSITION); |
108 | reg_write(REG_TRX_STATE, new); |
109 | } |
110 | |
111 | static void rx_done(void *user) |
112 | { |
113 | led(0); |
114 | next_buf(&rx_out); |
115 | usb_next(); |
116 | #ifdef AT86RF230 |
117 | /* slap at86rf230 - reduce fragmentation issue */ |
118 | change_state(TRX_STATUS_RX_AACK_ON); |
119 | #endif |
120 | } |
121 | |
122 | |
123 | static void receive_frame(void) |
124 | { |
125 | uint8_t size; |
126 | uint8_t *buf; |
127 | |
128 | spi_begin(); |
129 | spi_io(AT86RF230_BUF_READ); |
130 | |
131 | size = spi_recv(); |
132 | if (!size || (size & 0x80)) { |
133 | spi_end(); |
134 | return; |
135 | } |
136 | |
137 | buf = rx_buf[rx_in]; |
138 | spi_recv_block(buf+1, size+1); |
139 | spi_end(); |
140 | |
141 | buf[0] = size; |
142 | next_buf(&rx_in); |
143 | |
144 | if (eps[1].state == EP_IDLE) |
145 | usb_next(); |
146 | } |
147 | |
148 | |
149 | static bool handle_irq(void) |
150 | { |
151 | uint8_t irq; |
152 | |
153 | irq = reg_read(REG_IRQ_STATUS); |
154 | if (!(irq & IRQ_TRX_END)) |
155 | return 1; |
156 | |
157 | if (txing) { |
158 | if (eps[1].state == EP_IDLE) { |
159 | usb_send(&eps[1], &this_seq, 1, tx_ack_done, NULL); |
160 | } else { |
161 | queued_tx_ack = 1; |
162 | queued_seq = this_seq; |
163 | } |
164 | txing = 0; |
165 | return 1; |
166 | } |
167 | |
168 | /* likely */ |
169 | if (eps[1].state == EP_IDLE || rx_in != rx_out) |
170 | receive_frame(); |
171 | |
172 | return 1; |
173 | } |
174 | |
175 | |
176 | /* ----- TX/RX ------------------------------------------------------------- */ |
177 | |
178 | |
179 | bool mac_rx(int on) |
180 | { |
181 | if (on) { |
182 | mac_irq = handle_irq; |
183 | reg_read(REG_IRQ_STATUS); |
184 | change_state(TRX_CMD_RX_AACK_ON); |
185 | } else { |
186 | mac_irq = NULL; |
187 | change_state(TRX_CMD_FORCE_TRX_OFF); |
188 | txing = 0; |
189 | } |
190 | return 1; |
191 | } |
192 | |
193 | |
194 | static void do_tx(void *user) |
195 | { |
196 | uint16_t timeout = 0xffff; |
197 | uint8_t status; |
198 | uint8_t i; |
199 | |
200 | /* |
201 | * If we time out here, the host driver will time out waiting for the |
202 | * TRX_END acknowledgement. |
203 | */ |
204 | do { |
205 | if (!--timeout) |
206 | return; |
207 | status = reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK; |
208 | } |
209 | while (status != TRX_STATUS_RX_ON && status != TRX_STATUS_RX_AACK_ON); |
210 | |
211 | #ifdef AT86RF231 |
212 | /* |
213 | * We use TRX_CMD_FORCE_PLL_ON instead of TRX_CMD_PLL_ON because a new |
214 | * reception may have begun while we were still working on the previous |
215 | * one. |
216 | */ |
217 | reg_write(REG_TRX_STATE, TRX_CMD_FORCE_PLL_ON); |
218 | #endif |
219 | #ifdef AT86RF230 |
220 | /* |
221 | * at86rf230 doesn't support force change, nevetherless this works |
222 | * somehow |
223 | */ |
224 | reg_write(REG_TRX_STATE, TRX_CMD_PLL_ON); |
225 | #endif |
226 | |
227 | handle_irq(); |
228 | |
229 | spi_begin(); |
230 | spi_send(AT86RF230_BUF_WRITE); |
231 | spi_send(tx_size+2); /* CRC */ |
232 | for (i = 0; i != tx_size; i++) |
233 | spi_send(tx_buf[i]); |
234 | spi_end(); |
235 | |
236 | slp_tr(); |
237 | |
238 | txing = 1; |
239 | this_seq = next_seq; |
240 | |
241 | /* |
242 | * Wait until we reach BUSY_TX, so that we command the transition to |
243 | * RX_AACK_ON which will be executed upon TX completion. |
244 | */ |
245 | change_state(TRX_CMD_RX_AACK_ON); |
246 | } |
247 | |
248 | |
249 | bool mac_tx(uint16_t flags, uint8_t seq, uint16_t len) |
250 | { |
251 | if (len > MAX_PSDU) |
252 | return 0; |
253 | tx_size = len; |
254 | next_seq = seq; |
255 | usb_recv(&eps[0], tx_buf, len, do_tx, NULL); |
256 | return 1; |
257 | } |
258 | |
259 | |
260 | void mac_reset(void) |
261 | { |
262 | mac_irq = NULL; |
263 | txing = 0; |
264 | queued_tx_ack = 0; |
265 | rx_in = rx_out = 0; |
266 | next_seq = this_seq = queued_seq = 0; |
267 | |
268 | /* enable CRC and PHY_RSSI (with RX_CRC_VALID) in SPI status return */ |
269 | reg_write(REG_TRX_CTRL_1, |
270 | TX_AUTO_CRC_ON | SPI_CMD_MODE_PHY_RSSI << SPI_CMD_MODE_SHIFT); |
271 | } |
272 |