altos/cc1200: Adjust bit-sync configuration
[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; 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 __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;
23
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;
27
28 __xdata struct ao_task  ao_packet_task;
29 __xdata uint8_t ao_packet_enable;
30 __xdata uint8_t ao_packet_restart;
31
32 #if PACKET_HAS_MASTER
33 __xdata uint8_t ao_packet_master_sleeping;
34 #endif
35
36 void
37 ao_packet_send(void)
38 {
39 #ifdef AO_LED_RED
40         ao_led_on(AO_LED_RED);
41 #endif
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;
46                 ao_tx_packet.seq++;
47                 ao_packet_tx_used = 0;
48                 ao_wakeup(&tx_data);
49         }
50         ao_radio_send(&ao_tx_packet, sizeof (ao_tx_packet));
51 #ifdef AO_LED_RED
52         ao_led_off(AO_LED_RED);
53 #endif
54 }
55
56 uint8_t
57 ao_packet_recv(uint16_t timeout)
58 {
59         uint8_t dma_done;
60
61 #ifdef AO_LED_GREEN
62         ao_led_on(AO_LED_GREEN);
63 #endif
64         dma_done = ao_radio_recv(&ao_rx_packet, sizeof (struct ao_packet_recv), timeout);
65 #ifdef AO_LED_GREEN
66         ao_led_off(AO_LED_GREEN);
67 #endif
68 #if AO_PROFILE
69                 {
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;
72
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);
77                         flush();
78                 }
79 #endif
80
81         /* Check to see if we got a valid packet */
82         if (!dma_done)
83                 return 0;
84         if (!(ao_rx_packet.status & AO_RADIO_STATUS_CRC_OK)) {
85                 printf("bad crc addr %d len %d seq %d ack %d callsign %8.8s\n",
86                         ao_rx_packet.packet.addr,
87                         ao_rx_packet.packet.len,
88                         ao_rx_packet.packet.seq,
89                         ao_rx_packet.packet.ack,
90                         ao_rx_packet.packet.callsign);
91                 flush();
92                 return 0;
93         }
94
95         /* Accept packets with matching call signs, or any packet if
96          * our callsign hasn't been configured
97          */
98         if (ao_xmemcmp(ao_rx_packet.packet.callsign,
99                        ao_config.callsign,
100                        AO_MAX_CALLSIGN) != 0 &&
101             ao_xmemcmp(ao_config.callsign, CODE_TO_XDATA("N0CALL"), 7) != 0) {
102                 printf ("bad call\n"); flush();
103                 return 0;
104         }
105
106         /* SYN packets carry no data */
107         if (ao_rx_packet.packet.len == AO_PACKET_SYN) {
108                 rx_seq = ao_rx_packet.packet.seq;
109                 ao_tx_packet.seq = ao_rx_packet.packet.ack;
110                 ao_tx_packet.ack = rx_seq;
111         } else if (ao_rx_packet.packet.len) {
112
113                 /* Check for incoming data at the next sequence and
114                  * for an empty data buffer
115                  */
116                 if ((ao_rx_packet.packet.seq == (uint8_t) (rx_seq + (uint8_t) 1) ||
117                      ao_packet_restart) &&
118                     ao_packet_rx_used == ao_packet_rx_len) {
119
120                         /* Copy data to the receive data buffer and set up the
121                          * offsets
122                          */
123                         ao_xmemcpy(rx_data, ao_rx_packet.packet.d, ao_rx_packet.packet.len);
124                         ao_packet_rx_used = 0;
125                         ao_packet_rx_len = ao_rx_packet.packet.len;
126
127                         /* Mark the sequence that we've received to
128                          * let the sender know when we return a packet
129                          */
130                         rx_seq = ao_rx_packet.packet.seq;
131                         ao_tx_packet.ack = rx_seq;
132
133                         /* Poke anyone looking for received data */
134                         ao_wakeup(&ao_stdin_ready);
135                 }
136         }
137         ao_packet_restart = 0;
138
139         /* If the other side has seen the latest data we queued,
140          * wake up any task waiting to send data and let them go again
141          */
142         if (ao_rx_packet.packet.ack == ao_tx_packet.seq) {
143                 ao_tx_packet.len = 0;
144                 ao_wakeup(&ao_tx_packet);
145         }
146         return 1;
147 }
148
149 #if PACKET_HAS_MASTER
150 void
151 ao_packet_flush(void)
152 {
153         /* If there is data to send, and this is the master,
154          * then poke the master to send all queued data
155          */
156         if (ao_packet_tx_used && ao_packet_master_sleeping)
157                 ao_wakeup(&ao_packet_master_sleeping);
158 }
159 #endif /* PACKET_HAS_MASTER */
160
161 void
162 ao_packet_putchar(char c) __reentrant
163 {
164         /* No need to block interrupts, all variables here
165          * are only manipulated in task context
166          */
167         while (ao_packet_tx_used == AO_PACKET_MAX && ao_packet_enable) {
168 #if PACKET_HAS_MASTER
169                 ao_packet_flush();
170 #endif
171                 ao_sleep(&tx_data);
172         }
173
174         if (ao_packet_enable)
175                 tx_data[ao_packet_tx_used++] = c;
176 }
177
178 /* May be called with interrupts blocked */
179 int
180 _ao_packet_pollchar(void)
181 {
182         if (!ao_packet_enable)
183                 return AO_READ_AGAIN;
184
185         if (ao_packet_rx_used == ao_packet_rx_len)
186                 return AO_READ_AGAIN;
187
188         return rx_data[ao_packet_rx_used++];
189 }