Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[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 __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 char tx_data[AO_PACKET_MAX];
25 static __xdata char 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_master_sleeping;
31
32 void
33 ao_packet_send(void)
34 {
35         ao_led_on(AO_LED_RED);
36         ao_radio_get();
37
38         /* If any tx data is pending then copy it into the tx packet */
39         if (ao_packet_tx_used && ao_tx_packet.len == 0) {
40                 memcpy(&ao_tx_packet.d, tx_data, ao_packet_tx_used);
41                 ao_tx_packet.len = ao_packet_tx_used;
42                 ao_tx_packet.seq++;
43                 ao_packet_tx_used = 0;
44                 ao_wakeup(&tx_data);
45         }
46         ao_radio_done = 0;
47         ao_dma_set_transfer(ao_radio_dma,
48                             &ao_tx_packet,
49                             &RFDXADDR,
50                             sizeof (struct ao_packet),
51                             DMA_CFG0_WORDSIZE_8 |
52                             DMA_CFG0_TMODE_SINGLE |
53                             DMA_CFG0_TRIGGER_RADIO,
54                             DMA_CFG1_SRCINC_1 |
55                             DMA_CFG1_DESTINC_0 |
56                             DMA_CFG1_PRIORITY_HIGH);
57         ao_dma_start(ao_radio_dma);
58         RFST = RFST_STX;
59         __critical while (!ao_radio_done)
60                 ao_sleep(&ao_radio_done);
61         ao_radio_put();
62         ao_led_off(AO_LED_RED);
63 }
64
65 uint8_t
66 ao_packet_recv(void)
67 {
68         uint8_t dma_done;
69
70 #ifdef AO_LED_GREEN
71         ao_led_on(AO_LED_GREEN);
72 #endif
73         ao_radio_get();
74         ao_dma_set_transfer(ao_radio_dma,
75                             &RFDXADDR,
76                             &ao_rx_packet,
77                             sizeof (struct ao_packet_recv),
78                             DMA_CFG0_WORDSIZE_8 |
79                             DMA_CFG0_TMODE_SINGLE |
80                             DMA_CFG0_TRIGGER_RADIO,
81                             DMA_CFG1_SRCINC_0 |
82                             DMA_CFG1_DESTINC_1 |
83                             DMA_CFG1_PRIORITY_HIGH);
84         ao_dma_start(ao_radio_dma);
85         RFST = RFST_SRX;
86         __critical while (!ao_radio_dma_done)
87                            if (ao_sleep(&ao_radio_dma_done) != 0)
88                                    ao_radio_abort();
89         dma_done = ao_radio_dma_done;
90         ao_radio_put();
91 #ifdef AO_LED_GREEN
92         ao_led_off(AO_LED_GREEN);
93 #endif
94
95         if (dma_done & AO_DMA_DONE) {
96                 if (!(ao_rx_packet.status & PKT_APPEND_STATUS_1_CRC_OK))
97                         return AO_DMA_ABORTED;
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                         if (ao_rx_packet.packet.seq == (uint8_t) (rx_seq + (uint8_t) 1) && ao_packet_rx_used == ao_packet_rx_len) {
104                                 memcpy(rx_data, ao_rx_packet.packet.d, ao_rx_packet.packet.len);
105                                 ao_packet_rx_used = 0;
106                                 ao_packet_rx_len = ao_rx_packet.packet.len;
107                                 rx_seq = ao_rx_packet.packet.seq;
108                                 ao_tx_packet.ack = rx_seq;
109                                 ao_wakeup(&ao_stdin_ready);
110                         }
111                 }
112                 if (ao_rx_packet.packet.ack == ao_tx_packet.seq) {
113                         ao_tx_packet.len = 0;
114                         ao_wakeup(&ao_tx_packet);
115                 }
116         }
117         return dma_done;
118 }
119
120 #ifndef PACKET_HAS_MASTER
121 #define PACKET_HAS_MASTER 1
122 #endif
123
124 #if PACKET_HAS_MASTER
125 void
126 ao_packet_flush(void)
127 {
128         /* If there is data to send, and this is the master,
129          * then poke the master to send all queued data
130          */
131         if (ao_packet_tx_used && ao_packet_master_sleeping)
132                 ao_wake_task(&ao_packet_task);
133 }
134 #endif /* PACKET_HAS_MASTER */
135
136 void
137 ao_packet_putchar(char c) __reentrant
138 {
139         while (ao_packet_tx_used == AO_PACKET_MAX && ao_packet_enable) {
140 #if PACKET_HAS_MASTER
141                 ao_packet_flush();
142 #endif
143                 ao_sleep(&tx_data);
144         }
145
146         if (ao_packet_enable)
147                 tx_data[ao_packet_tx_used++] = c;
148 }
149
150 char
151 ao_packet_pollchar(void) __critical
152 {
153         if (!ao_packet_enable)
154                 return AO_READ_AGAIN;
155
156         if (ao_packet_rx_used == ao_packet_rx_len)
157                 return AO_READ_AGAIN;
158
159         return rx_data[ao_packet_rx_used++];
160 }