altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / drivers / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20
21 struct ao_packet_recv ao_rx_packet;
22 struct ao_packet ao_tx_packet;
23 uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
24
25 static uint8_t tx_data[AO_PACKET_MAX];
26 static uint8_t rx_data[AO_PACKET_MAX];
27 static uint8_t rx_seq;
28
29 struct ao_task  ao_packet_task;
30 uint8_t ao_packet_enable;
31 uint8_t ao_packet_restart;
32
33 #if PACKET_HAS_MASTER
34 uint8_t ao_packet_master_sleeping;
35 #endif
36
37 void
38 ao_packet_send(void)
39 {
40 #ifdef AO_LED_RED
41         ao_led_on(AO_LED_RED);
42 #endif
43         /* If any tx data is pending then copy it into the tx packet */
44         if (ao_packet_tx_used && ao_tx_packet.len == 0) {
45                 memcpy(&ao_tx_packet.d, tx_data, ao_packet_tx_used);
46                 ao_tx_packet.len = ao_packet_tx_used;
47                 ao_tx_packet.seq++;
48                 ao_packet_tx_used = 0;
49                 ao_wakeup(&tx_data);
50         }
51         ao_radio_send(&ao_tx_packet, sizeof (ao_tx_packet));
52 #ifdef AO_LED_RED
53         ao_led_off(AO_LED_RED);
54 #endif
55 }
56
57 uint8_t
58 ao_packet_recv(uint16_t timeout)
59 {
60         uint8_t dma_done;
61
62 #ifdef AO_LED_GREEN
63         ao_led_on(AO_LED_GREEN);
64 #endif
65         dma_done = ao_radio_recv(&ao_rx_packet, sizeof (struct ao_packet_recv), timeout);
66 #ifdef AO_LED_GREEN
67         ao_led_off(AO_LED_GREEN);
68 #endif
69 #if AO_PROFILE
70                 {
71                         extern uint32_t ao_rx_start_tick, ao_rx_packet_tick, ao_rx_done_tick, ao_rx_last_done_tick;
72                         extern uint32_t ao_fec_decode_start, ao_fec_decode_end;
73
74                         printf ("between packet: %d\n", ao_rx_start_tick - ao_rx_last_done_tick);
75                         printf ("receive start delay: %d\n", ao_rx_packet_tick - ao_rx_start_tick);
76                         printf ("decode time: %d\n", ao_fec_decode_end - ao_fec_decode_start);
77                         printf ("rx cleanup: %d\n\n", ao_rx_done_tick - ao_fec_decode_end);
78                         flush();
79                 }
80 #endif
81
82         /* Check to see if we got a valid packet */
83         if (!dma_done)
84                 return 0;
85         if (!(ao_rx_packet.status & AO_RADIO_STATUS_CRC_OK))
86                 return 0;
87
88         /* Accept packets with matching call signs, or any packet if
89          * our callsign hasn't been configured
90          */
91         if (memcmp(ao_rx_packet.packet.callsign,
92                        ao_config.callsign,
93                        AO_MAX_CALLSIGN) != 0 &&
94             memcmp(ao_config.callsign, "N0CALL", 7) != 0)
95                 return 0;
96
97         /* SYN packets carry no data */
98         if (ao_rx_packet.packet.len == AO_PACKET_SYN) {
99                 rx_seq = ao_rx_packet.packet.seq;
100                 ao_tx_packet.seq = ao_rx_packet.packet.ack;
101                 ao_tx_packet.ack = rx_seq;
102         } else if (ao_rx_packet.packet.len) {
103
104                 /* Check for incoming data at the next sequence and
105                  * for an empty data buffer
106                  */
107                 if ((ao_rx_packet.packet.seq == (uint8_t) (rx_seq + (uint8_t) 1) ||
108                      ao_packet_restart) &&
109                     ao_packet_rx_used == ao_packet_rx_len) {
110
111                         /* Copy data to the receive data buffer and set up the
112                          * offsets
113                          */
114                         memcpy(rx_data, ao_rx_packet.packet.d, ao_rx_packet.packet.len);
115                         ao_packet_rx_used = 0;
116                         ao_packet_rx_len = ao_rx_packet.packet.len;
117
118                         /* Mark the sequence that we've received to
119                          * let the sender know when we return a packet
120                          */
121                         rx_seq = ao_rx_packet.packet.seq;
122                         ao_tx_packet.ack = rx_seq;
123
124                         /* Poke anyone looking for received data */
125                         ao_wakeup(&ao_stdin_ready);
126                 }
127         }
128         ao_packet_restart = 0;
129
130         /* If the other side has seen the latest data we queued,
131          * wake up any task waiting to send data and let them go again
132          */
133         if (ao_rx_packet.packet.ack == ao_tx_packet.seq) {
134                 ao_tx_packet.len = 0;
135                 ao_wakeup(&ao_tx_packet);
136         }
137         return 1;
138 }
139
140 #if PACKET_HAS_MASTER
141 void
142 ao_packet_flush(void)
143 {
144         /* If there is data to send, and this is the master,
145          * then poke the master to send all queued data
146          */
147         if (ao_packet_tx_used && ao_packet_master_sleeping)
148                 ao_wakeup(&ao_packet_master_sleeping);
149 }
150 #endif /* PACKET_HAS_MASTER */
151
152 void
153 ao_packet_putchar(char c) 
154 {
155         /* No need to block interrupts, all variables here
156          * are only manipulated in task context
157          */
158         while (ao_packet_tx_used == AO_PACKET_MAX && ao_packet_enable) {
159 #if PACKET_HAS_MASTER
160                 ao_packet_flush();
161 #endif
162                 ao_sleep(&tx_data);
163         }
164
165         if (ao_packet_enable)
166                 tx_data[ao_packet_tx_used++] = c;
167 }
168
169 /* May be called with interrupts blocked */
170 int
171 _ao_packet_pollchar(void)
172 {
173         if (!ao_packet_enable)
174                 return AO_READ_AGAIN;
175
176         if (ao_packet_rx_used == ao_packet_rx_len)
177                 return AO_READ_AGAIN;
178
179         return rx_data[ao_packet_rx_used++];
180 }