Use ao_alarm for ao_delay so it can be easily interrupted
[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                 if (!(rx_packet.status & PKT_APPEND_STATUS_1_CRC_OK)) {
87                         printf ("bad crc\n"); flush();
88                         return AO_DMA_ABORTED;
89                 }
90                 if (rx_packet.packet.len == AO_PACKET_SYN) {
91                         rx_seq = rx_packet.packet.seq;
92                         tx_packet.seq = rx_packet.packet.ack;
93                         tx_packet.ack = rx_seq;
94                 } else if (rx_packet.packet.len) {
95                         if (rx_packet.packet.seq == rx_seq + 1 && rx_used == rx_len)
96                         {
97                                 printf ("rx len %3d seq %3d ack %3d\n",
98                                         rx_packet.packet.len,
99                                         rx_packet.packet.seq,
100                                         rx_packet.packet.ack);
101                                 flush();
102                                 memcpy(rx_data, rx_packet.packet.d, rx_packet.packet.len);
103                                 rx_used = 0;
104                                 rx_len = rx_packet.packet.len;
105                                 rx_seq = rx_packet.packet.seq;
106                                 tx_packet.ack = rx_seq;
107                                 ao_wakeup(&rx_data);
108                         }
109                 }
110                 if (rx_packet.packet.ack == tx_packet.seq) {
111                         tx_packet.len = 0;
112                         ao_wakeup(&tx_packet);
113                 }
114         }
115         return dma_done;
116 }
117
118 void
119 ao_packet_slave(void)
120 {
121         ao_radio_set_packet();
122         tx_packet.addr = ao_serial_number;
123         tx_packet.len = AO_PACKET_SYN;
124         while (ao_packet_enable) {
125                 ao_led_on(AO_LED_GREEN);
126                 ao_packet_recv();
127                 ao_led_off(AO_LED_GREEN);
128                 ao_led_on(AO_LED_RED);
129                 ao_delay(AO_MS_TO_TICKS(100));
130                 ao_packet_send();
131                 ao_led_off(AO_LED_RED);
132         }
133         ao_exit();
134 }
135
136 /* Thread for the master side of the packet link */
137
138 void
139 ao_packet_master(void)
140 {
141         uint8_t status;
142
143         ao_radio_set_packet();
144         tx_packet.addr = ao_serial_number;
145         tx_packet.len = AO_PACKET_SYN;
146         while (ao_packet_enable) {
147                 ao_led_on(AO_LED_RED);
148                 ao_delay(AO_MS_TO_TICKS(100));
149                 ao_packet_send();
150                 ao_led_off(AO_LED_RED);
151                 ao_led_on(AO_LED_GREEN);
152                 ao_alarm(AO_MS_TO_TICKS(1000));
153                 status = ao_packet_recv();
154                 ao_led_off(AO_LED_GREEN);
155                 if (status & AO_DMA_DONE) {
156                         ao_packet_master_sleeping = 1;
157                         ao_delay(AO_MS_TO_TICKS(1000));
158                         ao_packet_master_sleeping = 0;
159                 }
160         }
161         ao_exit();
162 }
163
164 void
165 ao_packet_flush(void)
166 {
167         if (!tx_used)
168                 return;
169
170         /* Wait for previous packet to be received
171          */
172         while (tx_packet.len)
173                 ao_sleep(&tx_packet);
174
175         /* Prepare next packet
176          */
177         if (tx_used) {
178                 memcpy(&tx_packet.d, tx_data, tx_used);
179                 tx_packet.len = tx_used;
180                 tx_packet.seq++;
181                 tx_used = 0;
182
183                 if (ao_packet_master_sleeping)
184                         ao_wake_task(&ao_packet_task);
185         }
186 }
187
188 void
189 ao_packet_putchar(char c)
190 {
191         while (tx_used == AO_PACKET_MAX && ao_packet_enable)
192                 ao_packet_flush();
193
194         if (ao_packet_enable)
195                 tx_data[tx_used++] = c;
196 }
197
198 char
199 ao_packet_getchar(void) __critical
200 {
201         while (rx_used == rx_len && ao_packet_enable) {
202                 flush();
203                 ao_sleep(&rx_data);
204         }
205
206         if (!ao_packet_enable)
207                 return 0;
208
209         return rx_data[rx_used++];
210 }
211
212 static void
213 ao_packet_echo(void) __reentrant
214 {
215         uint8_t c;
216         while (ao_packet_enable) {
217                 c = ao_packet_getchar();
218                 if (ao_packet_enable) {
219                         putchar(c);
220                         if (c == (uint8_t) '\n')
221                                 flush();
222                 }
223         }
224         ao_exit();
225 }
226
227 static __xdata struct ao_task   ao_packet_echo_task;
228
229 static void
230 ao_packet_forward(void) __reentrant
231 {
232         char c;
233         ao_packet_enable = 1;
234         ao_cmd_white();
235
236         if (ao_cmd_lex_c == 'm')
237                 ao_add_task(&ao_packet_task, ao_packet_master, "master");
238         else
239                 ao_add_task(&ao_packet_task, ao_packet_slave, "slave");
240         ao_add_task(&ao_packet_echo_task, ao_packet_echo, "echo");
241         while ((c = getchar()) != '~')
242                 ao_packet_putchar(c);
243         ao_packet_enable = 0;
244         ao_radio_abort();
245         while (ao_packet_echo_task.wchan || ao_packet_task.wchan) {
246                 ao_wake_task(&ao_packet_echo_task);
247                 ao_wake_task(&ao_packet_task);
248         }
249 }
250
251 __code struct ao_cmds ao_packet_cmds[] = {
252         { 'p',  ao_packet_forward,      "p {m|s}                            Remote packet link. m=master, s=slave" },
253         { 0,    ao_packet_forward,      NULL },
254 };
255
256 void
257 ao_packet_init(void)
258 {
259         ao_cmd_register(&ao_packet_cmds[0]);
260 }