2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 __xdata struct ao_packet_recv ao_rx_packet;
21 __xdata struct ao_packet ao_tx_packet;
22 __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
24 static __xdata uint8_t tx_data[AO_PACKET_MAX];
25 static __xdata uint8_t rx_data[AO_PACKET_MAX];
26 static __pdata uint8_t rx_seq;
28 __xdata struct ao_task ao_packet_task;
29 __xdata uint8_t ao_packet_enable;
30 __xdata uint8_t ao_packet_restart;
33 __xdata uint8_t ao_packet_master_sleeping;
40 ao_led_on(AO_LED_RED);
42 /* If any tx data is pending then copy it into the tx packet */
43 if (ao_packet_tx_used && ao_tx_packet.len == 0) {
44 ao_xmemcpy(&ao_tx_packet.d, tx_data, ao_packet_tx_used);
45 ao_tx_packet.len = ao_packet_tx_used;
47 ao_packet_tx_used = 0;
50 ao_radio_send(&ao_tx_packet, sizeof (ao_tx_packet));
52 ao_led_off(AO_LED_RED);
62 ao_led_on(AO_LED_GREEN);
64 dma_done = ao_radio_recv(&ao_rx_packet, sizeof (struct ao_packet_recv), 0);
66 ao_led_off(AO_LED_GREEN);
70 extern uint32_t ao_rx_start_tick, ao_rx_packet_tick, ao_rx_done_tick, ao_rx_last_done_tick;
71 extern uint32_t ao_fec_decode_start, ao_fec_decode_end;
73 printf ("between packet: %d\n", ao_rx_start_tick - ao_rx_last_done_tick);
74 printf ("receive start delay: %d\n", ao_rx_packet_tick - ao_rx_start_tick);
75 printf ("decode time: %d\n", ao_fec_decode_end - ao_fec_decode_start);
76 printf ("rx cleanup: %d\n\n", ao_rx_done_tick - ao_fec_decode_end);
81 /* Check to see if we got a valid packet */
84 if (!(ao_rx_packet.status & AO_RADIO_STATUS_CRC_OK))
87 /* Accept packets with matching call signs, or any packet if
88 * our callsign hasn't been configured
90 if (ao_xmemcmp(ao_rx_packet.packet.callsign,
92 AO_MAX_CALLSIGN) != 0 &&
93 ao_xmemcmp(ao_config.callsign, CODE_TO_XDATA("N0CALL"), 7) != 0)
96 /* SYN packets carry no data */
97 if (ao_rx_packet.packet.len == AO_PACKET_SYN) {
98 rx_seq = ao_rx_packet.packet.seq;
99 ao_tx_packet.seq = ao_rx_packet.packet.ack;
100 ao_tx_packet.ack = rx_seq;
101 } else if (ao_rx_packet.packet.len) {
103 /* Check for incoming data at the next sequence and
104 * for an empty data buffer
106 if ((ao_rx_packet.packet.seq == (uint8_t) (rx_seq + (uint8_t) 1) ||
107 ao_packet_restart) &&
108 ao_packet_rx_used == ao_packet_rx_len) {
110 /* Copy data to the receive data buffer and set up the
113 ao_xmemcpy(rx_data, ao_rx_packet.packet.d, ao_rx_packet.packet.len);
114 ao_packet_rx_used = 0;
115 ao_packet_rx_len = ao_rx_packet.packet.len;
117 /* Mark the sequence that we've received to
118 * let the sender know when we return a packet
120 rx_seq = ao_rx_packet.packet.seq;
121 ao_tx_packet.ack = rx_seq;
123 /* Poke anyone looking for received data */
124 ao_wakeup(&ao_stdin_ready);
127 ao_packet_restart = 0;
129 /* If the other side has seen the latest data we queued,
130 * wake up any task waiting to send data and let them go again
132 if (ao_rx_packet.packet.ack == ao_tx_packet.seq) {
133 ao_tx_packet.len = 0;
134 ao_wakeup(&ao_tx_packet);
139 #if PACKET_HAS_MASTER
141 ao_packet_flush(void)
143 /* If there is data to send, and this is the master,
144 * then poke the master to send all queued data
146 if (ao_packet_tx_used && ao_packet_master_sleeping)
147 ao_wakeup(&ao_packet_master_sleeping);
149 #endif /* PACKET_HAS_MASTER */
152 ao_packet_putchar(char c) __reentrant
154 /* No need to block interrupts, all variables here
155 * are only manipulated in task context
157 while (ao_packet_tx_used == AO_PACKET_MAX && ao_packet_enable) {
158 #if PACKET_HAS_MASTER
164 if (ao_packet_enable)
165 tx_data[ao_packet_tx_used++] = c;
168 /* May be called with interrupts blocked */
170 _ao_packet_pollchar(void)
172 if (!ao_packet_enable)
173 return AO_READ_AGAIN;
175 if (ao_packet_rx_used == ao_packet_rx_len)
176 return AO_READ_AGAIN;
178 return rx_data[ao_packet_rx_used++];