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 b73d5fc1af46d739e841af89bf3841fd6a84bdf7 created 10 years 8 months ago. By Werner Almesberger, atusb/fw/mac.c: remove queued_rx; buffer frames in the MCU instead | |
---|---|
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 | |
25 | #define RX_BUFS 2 |
26 | |
27 | |
28 | bool (*mac_irq)(void) = NULL; |
29 | |
30 | |
31 | static uint8_t rx_buf[RX_BUFS][MAX_PSDU+2]; /* PHDR+payload+LQ */ |
32 | static bool rx_in = 0, rx_out = 0; |
33 | static uint8_t tx_buf[MAX_PSDU]; |
34 | static uint8_t tx_size = 0; |
35 | static bool txing = 0; |
36 | static bool queued_tx_ack = 0; |
37 | static uint8_t next_seq, this_seq, queued_seq; |
38 | |
39 | |
40 | /* ----- Register access --------------------------------------------------- */ |
41 | |
42 | |
43 | static uint8_t reg_read(uint8_t reg) |
44 | { |
45 | uint8_t value; |
46 | |
47 | spi_begin(); |
48 | spi_send(AT86RF230_REG_READ | reg); |
49 | value = spi_recv(); |
50 | spi_end(); |
51 | |
52 | return value; |
53 | } |
54 | |
55 | |
56 | static void reg_write(uint8_t reg, uint8_t value) |
57 | { |
58 | spi_begin(); |
59 | spi_send(AT86RF230_REG_WRITE | reg); |
60 | spi_send(value); |
61 | spi_end(); |
62 | } |
63 | |
64 | |
65 | /* ----- Interrupt handling ------------------------------------------------ */ |
66 | |
67 | |
68 | static void rx_done(void *user); |
69 | static void tx_ack_done(void *user); |
70 | |
71 | |
72 | static void usb_next(void) |
73 | { |
74 | const uint8_t *buf; |
75 | |
76 | if (rx_in != rx_out) { |
77 | buf = rx_buf[rx_out]; |
78 | led(1); |
79 | usb_send(&eps[1], buf, buf[0]+2, rx_done, NULL); |
80 | } |
81 | |
82 | if (queued_tx_ack) { |
83 | usb_send(&eps[1], &queued_seq, 1, tx_ack_done, NULL); |
84 | queued_tx_ack = 0; |
85 | } |
86 | } |
87 | |
88 | |
89 | static void tx_ack_done(void *user) |
90 | { |
91 | usb_next(); |
92 | } |
93 | |
94 | |
95 | static void rx_done(void *user) |
96 | { |
97 | led(0); |
98 | rx_out = (rx_out+1) & (RX_BUFS-1); |
99 | usb_next(); |
100 | } |
101 | |
102 | |
103 | static void receive_frame(void) |
104 | { |
105 | uint8_t size, i; |
106 | uint8_t *buf; |
107 | |
108 | spi_begin(); |
109 | if (!(spi_io(AT86RF230_BUF_READ) & RX_CRC_VALID)) { |
110 | spi_end(); |
111 | return; |
112 | } |
113 | size = spi_recv(); |
114 | if (!size || (size & 0x80)) { |
115 | spi_end(); |
116 | return; |
117 | } |
118 | |
119 | buf = rx_buf[rx_in]; |
120 | for (i = 0; i != size+1; i++) |
121 | buf[i+1] = spi_recv(); |
122 | spi_end(); |
123 | |
124 | buf[0] = size; |
125 | rx_in = (rx_in+1) & (RX_BUFS-1); |
126 | |
127 | if (eps[1].state == EP_IDLE) |
128 | usb_next(); |
129 | } |
130 | |
131 | |
132 | static bool handle_irq(void) |
133 | { |
134 | uint8_t irq; |
135 | |
136 | irq = reg_read(REG_IRQ_STATUS); |
137 | if (!(irq & IRQ_TRX_END)) |
138 | return 1; |
139 | |
140 | if (txing) { |
141 | if (eps[1].state == EP_IDLE) { |
142 | usb_send(&eps[1], &this_seq, 1, tx_ack_done, NULL); |
143 | } else { |
144 | queued_tx_ack = 1; |
145 | queued_seq = this_seq; |
146 | } |
147 | txing = 0; |
148 | return 1; |
149 | } |
150 | |
151 | /* likely */ |
152 | if (eps[1].state == EP_IDLE || rx_in != rx_out) |
153 | receive_frame(); |
154 | |
155 | return 1; |
156 | } |
157 | |
158 | |
159 | /* ----- TX/RX ------------------------------------------------------------- */ |
160 | |
161 | |
162 | static void change_state(uint8_t new) |
163 | { |
164 | while ((reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK) == |
165 | TRX_STATUS_TRANSITION); |
166 | reg_write(REG_TRX_STATE, new); |
167 | } |
168 | |
169 | |
170 | bool mac_rx(int on) |
171 | { |
172 | if (on) { |
173 | mac_irq = handle_irq; |
174 | reg_read(REG_IRQ_STATUS); |
175 | change_state(TRX_CMD_RX_ON); |
176 | } else { |
177 | mac_irq = NULL; |
178 | change_state(TRX_CMD_FORCE_TRX_OFF); |
179 | txing = 0; |
180 | } |
181 | return 1; |
182 | } |
183 | |
184 | |
185 | static void do_tx(void *user) |
186 | { |
187 | uint16_t timeout = 0xffff; |
188 | uint8_t status; |
189 | uint8_t i; |
190 | |
191 | /* |
192 | * If we time out here, the host driver will time out waiting for the |
193 | * TRX_END acknowledgement. |
194 | */ |
195 | do { |
196 | if (!--timeout) |
197 | return; |
198 | status = reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK; |
199 | } |
200 | while (status != TRX_STATUS_RX_ON && status != TRX_STATUS_RX_AACK_ON); |
201 | |
202 | /* |
203 | * We use TRX_CMD_FORCE_PLL_ON instead of TRX_CMD_PLL_ON because a new |
204 | * reception may have begun while we were still working on the previous |
205 | * one. |
206 | */ |
207 | reg_write(REG_TRX_STATE, TRX_CMD_FORCE_PLL_ON); |
208 | |
209 | handle_irq(); |
210 | |
211 | spi_begin(); |
212 | spi_send(AT86RF230_BUF_WRITE); |
213 | spi_send(tx_size+2); /* CRC */ |
214 | for (i = 0; i != tx_size; i++) |
215 | spi_send(tx_buf[i]); |
216 | spi_end(); |
217 | |
218 | slp_tr(); |
219 | |
220 | txing = 1; |
221 | this_seq = next_seq; |
222 | |
223 | /* |
224 | * Wait until we reach BUSY_TX, so that we command the transition to |
225 | * RX_ON which will be executed upon TX completion. |
226 | */ |
227 | change_state(TRX_CMD_RX_ON); |
228 | } |
229 | |
230 | |
231 | bool mac_tx(uint16_t flags, uint8_t seq, uint16_t len) |
232 | { |
233 | if (len > MAX_PSDU) |
234 | return 0; |
235 | tx_size = len; |
236 | next_seq = seq; |
237 | usb_recv(&eps[0], tx_buf, len, do_tx, NULL); |
238 | return 1; |
239 | } |
240 | |
241 | |
242 | void mac_reset(void) |
243 | { |
244 | mac_irq = NULL; |
245 | txing = 0; |
246 | queued_tx_ack = 0; |
247 | rx_in = rx_out = 0; |
248 | next_seq = this_seq = queued_seq = 0; |
249 | |
250 | /* enable CRC and PHY_RSSI (with RX_CRC_VALID) in SPI status return */ |
251 | reg_write(REG_TRX_CTRL_1, |
252 | TX_AUTO_CRC_ON | SPI_CMD_MODE_PHY_RSSI << SPI_CMD_MODE_SHIFT); |
253 | } |
254 |