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 88c7e64e38dcfdf85b5a47e6dfdc83a5a8b3f445 created 11 years 1 month ago. By Werner Almesberger, atusb/fw/mac.c (txing, queued_tx_ack): change from "int" to "bool" | |
---|---|
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 | spi_send(AT86RF230_BUF_READ); |
94 | size = spi_recv(); |
95 | if (!size || (size & 0x80)) { |
96 | spi_end(); |
97 | return 1; |
98 | } |
99 | |
100 | rx_buf[0] = size; |
101 | for (i = 0; i != size+1; i++) |
102 | rx_buf[i+1] = spi_recv(); |
103 | spi_end(); |
104 | led(1); |
105 | usb_send(&eps[1], rx_buf, size+2, rx_done, NULL); |
106 | return 1; |
107 | } |
108 | |
109 | |
110 | static void change_state(uint8_t new) |
111 | { |
112 | while ((reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK) == |
113 | TRX_STATUS_TRANSITION); |
114 | reg_write(REG_TRX_STATE, new); |
115 | } |
116 | |
117 | |
118 | int mac_rx(int on) |
119 | { |
120 | if (on) { |
121 | mac_irq = handle_irq; |
122 | reg_read(REG_IRQ_STATUS); |
123 | change_state(TRX_CMD_RX_ON); |
124 | } else { |
125 | mac_irq = NULL; |
126 | change_state(TRX_CMD_FORCE_TRX_OFF); |
127 | txing = 0; |
128 | } |
129 | return 1; |
130 | } |
131 | |
132 | |
133 | static void do_tx(void *user) |
134 | { |
135 | uint8_t status; |
136 | uint8_t i; |
137 | |
138 | do status = reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK; |
139 | while (status != TRX_STATUS_RX_ON && status != TRX_STATUS_RX_AACK_ON); |
140 | |
141 | /* |
142 | * We use TRX_CMD_FORCE_PLL_ON instead of TRX_CMD_PLL_ON because a new |
143 | * reception may have begun while we were still working on the previous |
144 | * one. |
145 | */ |
146 | reg_write(REG_TRX_STATE, TRX_CMD_FORCE_PLL_ON); |
147 | |
148 | handle_irq(); |
149 | |
150 | spi_begin(); |
151 | spi_send(AT86RF230_BUF_WRITE); |
152 | spi_send(tx_size+2); /* CRC */ |
153 | for (i = 0; i != tx_size; i++) |
154 | spi_send(tx_buf[i]); |
155 | spi_end(); |
156 | |
157 | reg_write(REG_TRX_STATE, TRX_CMD_TX_START); |
158 | |
159 | txing = 1; |
160 | |
161 | /* |
162 | * Wait until we reach BUSY_TX, so that we command the transition to |
163 | * RX_ON which will be executed upon TX completion. |
164 | */ |
165 | change_state(TRX_CMD_RX_ON); |
166 | } |
167 | |
168 | |
169 | int mac_tx(uint16_t flags, uint16_t len) |
170 | { |
171 | if (len > MAX_PSDU) |
172 | return 0; |
173 | tx_size = len; |
174 | usb_recv(&eps[0], tx_buf, len, do_tx, NULL); |
175 | return 1; |
176 | } |
177 | |
178 | |
179 | void mac_reset(void) |
180 | { |
181 | mac_irq = NULL; |
182 | txing = 0; |
183 | queued_tx_ack = 0; |
184 | } |
185 |