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