Use ao_radio_done to wait for TX to completely finish with packet
[fw/altos] / src / ao_packet.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 static __xdata struct ao_packet_recv rx_packet;
21 static __xdata struct ao_packet tx_packet;
22 static __xdata char tx_data[AO_PACKET_MAX];
23 static __xdata char rx_data[AO_PACKET_MAX];
24 static __pdata uint8_t rx_len, rx_used, tx_used;
25 static __pdata uint8_t rx_seq;
26
27 static __xdata struct ao_task   ao_packet_task;
28 static __xdata uint8_t ao_packet_enable;
29 static __xdata uint8_t ao_packet_master_sleeping;
30
31 void
32 ao_packet_send(void)
33 {
34         ao_config_get();
35         ao_mutex_get(&ao_radio_mutex);
36         ao_radio_idle();
37         ao_radio_done = 0;
38         RF_CHANNR = ao_config.radio_channel;
39         ao_dma_set_transfer(ao_radio_dma,
40                             &tx_packet,
41                             &RFDXADDR,
42                             sizeof (struct ao_packet),
43                             DMA_CFG0_WORDSIZE_8 |
44                             DMA_CFG0_TMODE_SINGLE |
45                             DMA_CFG0_TRIGGER_RADIO,
46                             DMA_CFG1_SRCINC_1 |
47                             DMA_CFG1_DESTINC_0 |
48                             DMA_CFG1_PRIORITY_HIGH);
49         ao_dma_start(ao_radio_dma);
50         RFST = RFST_STX;
51         __critical while (!ao_radio_done)
52                 ao_sleep(&ao_radio_done);
53         ao_mutex_put(&ao_radio_mutex);
54 }
55
56 uint8_t
57 ao_packet_recv(void)
58 {
59         uint8_t dma_done;
60
61         ao_config_get();
62         ao_mutex_get(&ao_radio_mutex);
63         ao_radio_idle();
64         RF_CHANNR = ao_config.radio_channel;
65         ao_dma_set_transfer(ao_radio_dma,
66                             &RFDXADDR,
67                             &rx_packet,
68                             sizeof (struct ao_packet_recv),
69                             DMA_CFG0_WORDSIZE_8 |
70                             DMA_CFG0_TMODE_SINGLE |
71                             DMA_CFG0_TRIGGER_RADIO,
72                             DMA_CFG1_SRCINC_0 |
73                             DMA_CFG1_DESTINC_1 |
74                             DMA_CFG1_PRIORITY_HIGH);
75         ao_dma_start(ao_radio_dma);
76         RFST = RFST_SRX;
77         __critical while (!ao_radio_dma_done)
78                            if (ao_sleep(&ao_radio_dma_done) != 0) {
79                                    printf("recv timeout\n"); flush();
80                                    ao_radio_abort();
81                            }
82         dma_done = ao_radio_dma_done;
83         ao_mutex_put(&ao_radio_mutex);
84
85         if (dma_done & AO_DMA_DONE) {
86                 printf ("rssi %d status %x\n", rx_packet.rssi, rx_packet.status); flush();
87                 if (!(rx_packet.status & PKT_APPEND_STATUS_1_CRC_OK)) {
88                         printf ("bad crc\n"); flush();
89 //                      return AO_DMA_ABORTED;
90                 }
91                 if (rx_packet.packet.len) {
92                         if (rx_packet.packet.seq == rx_seq + 1 && rx_used == rx_len)
93                         {
94                                 memcpy(rx_data, rx_packet.packet.d, rx_packet.packet.len);
95                                 rx_used = 0;
96                                 rx_len = rx_packet.packet.len;
97                                 rx_seq = rx_packet.packet.seq;
98                                 tx_packet.ack = rx_seq;
99                                 ao_wakeup(&rx_data);
100                         }
101                 }
102                 if (rx_packet.packet.ack == tx_packet.seq) {
103                         tx_packet.len = 0;
104                         ao_wakeup(&tx_packet);
105                 }
106         }
107         return dma_done;
108 }
109
110 void
111 ao_packet_slave(void)
112 {
113         tx_packet.addr = ao_serial_number;
114         ao_radio_set_packet();
115         while (ao_packet_enable) {
116                 ao_packet_recv();
117                 ao_led_toggle(AO_LED_GREEN);
118                 ao_delay(AO_MS_TO_TICKS(100));
119                 ao_packet_send();
120                 ao_led_toggle(AO_LED_RED);
121         }
122         ao_exit();
123 }
124
125 /* Thread for the master side of the packet link */
126
127 void
128 ao_packet_master(void)
129 {
130         uint8_t status;
131
132         ao_radio_set_packet();
133         tx_packet.addr = ao_serial_number;
134         tx_packet.len = AO_PACKET_SYN;
135         while (ao_packet_enable) {
136                 ao_led_on(AO_LED_RED);
137                 ao_delay(AO_MS_TO_TICKS(100));
138                 ao_packet_send();
139                 ao_led_off(AO_LED_RED);
140                 ao_led_on(AO_LED_GREEN);
141                 ao_alarm(AO_MS_TO_TICKS(1000));
142                 status = ao_packet_recv();
143                 ao_led_off(AO_LED_GREEN);
144                 if (status & AO_DMA_DONE) {
145                         ao_packet_master_sleeping = 1;
146                         ao_delay(AO_MS_TO_TICKS(1000));
147                         ao_packet_master_sleeping = 0;
148                 }
149         }
150         ao_exit();
151 }
152
153 void
154 ao_packet_flush(void)
155 {
156         if (!tx_used)
157                 return;
158
159         /* Wait for previous packet to be received
160          */
161         while (tx_packet.len)
162                 ao_sleep(&tx_packet);
163
164         /* Prepare next packet
165          */
166         if (tx_used) {
167                 memcpy(&tx_packet.d, tx_data, tx_used);
168                 tx_packet.len = tx_used;
169                 tx_packet.seq++;
170                 tx_used = 0;
171
172                 if (ao_packet_master_sleeping)
173                         ao_wake_task(&ao_packet_task);
174         }
175 }
176
177 void
178 ao_packet_putchar(char c)
179 {
180         while (tx_used == AO_PACKET_MAX && ao_packet_enable)
181                 ao_packet_flush();
182
183         if (ao_packet_enable)
184                 tx_data[tx_used++] = c;
185 }
186
187 char
188 ao_packet_getchar(void) __critical
189 {
190         while (rx_used == rx_len && ao_packet_enable)
191                 ao_sleep(&rx_data);
192
193         if (!ao_packet_enable)
194                 return 0;
195
196         return rx_data[rx_used++];
197 }
198
199 static void
200 ao_packet_echo(void) __reentrant
201 {
202         uint8_t c;
203         while (ao_packet_enable) {
204                 c = ao_packet_getchar();
205                 if (ao_packet_enable)
206                         putchar(c);
207         }
208         ao_exit();
209 }
210
211 static __xdata struct ao_task   ao_packet_echo_task;
212
213 static void
214 ao_packet_forward(void) __reentrant
215 {
216         char c;
217         ao_packet_enable = 1;
218         ao_cmd_white();
219
220         if (ao_cmd_lex_c == 'm')
221                 ao_add_task(&ao_packet_task, ao_packet_master, "master");
222         else
223                 ao_add_task(&ao_packet_task, ao_packet_slave, "slave");
224         ao_add_task(&ao_packet_echo_task, ao_packet_echo, "echo");
225         while ((c = getchar()) != '~') {
226                 ao_packet_putchar(c);
227                 if (c == '\n')
228                         ao_packet_flush();
229         }
230         ao_packet_enable = 0;
231         ao_radio_abort();
232         while (ao_packet_echo_task.wchan || ao_packet_task.wchan) {
233                 ao_wake_task(&ao_packet_echo_task);
234                 ao_wake_task(&ao_packet_task);
235         }
236 #endif
237 }
238
239 __code struct ao_cmds ao_packet_cmds[] = {
240         { 'p',  ao_packet_forward,      "p {m|s}                            Remote packet link. m=master, s=slave" },
241         { 0,    ao_packet_forward,      NULL },
242 };
243
244 void
245 ao_packet_init(void)
246 {
247         ao_cmd_register(&ao_packet_cmds[0]);
248 }