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