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 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;
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;
35 ao_mutex_get(&ao_radio_mutex);
38 RF_CHANNR = ao_config.radio_channel;
39 ao_dma_set_transfer(ao_radio_dma,
42 sizeof (struct ao_packet),
44 DMA_CFG0_TMODE_SINGLE |
45 DMA_CFG0_TRIGGER_RADIO,
48 DMA_CFG1_PRIORITY_HIGH);
49 ao_dma_start(ao_radio_dma);
51 __critical while (!ao_radio_done)
52 ao_sleep(&ao_radio_done);
53 ao_mutex_put(&ao_radio_mutex);
62 ao_mutex_get(&ao_radio_mutex);
64 RF_CHANNR = ao_config.radio_channel;
65 ao_dma_set_transfer(ao_radio_dma,
68 sizeof (struct ao_packet_recv),
70 DMA_CFG0_TMODE_SINGLE |
71 DMA_CFG0_TRIGGER_RADIO,
74 DMA_CFG1_PRIORITY_HIGH);
75 ao_dma_start(ao_radio_dma);
77 __critical while (!ao_radio_dma_done)
78 if (ao_sleep(&ao_radio_dma_done) != 0) {
79 printf("recv timeout\n"); flush();
82 dma_done = ao_radio_dma_done;
83 ao_mutex_put(&ao_radio_mutex);
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;
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)
97 printf ("rx len %3d seq %3d ack %3d\n",
100 rx_packet.packet.ack);
102 memcpy(rx_data, rx_packet.packet.d, rx_packet.packet.len);
104 rx_len = rx_packet.packet.len;
105 rx_seq = rx_packet.packet.seq;
106 tx_packet.ack = rx_seq;
110 if (rx_packet.packet.ack == tx_packet.seq) {
112 ao_wakeup(&tx_packet);
119 ao_packet_slave(void)
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);
127 ao_led_off(AO_LED_GREEN);
128 ao_led_on(AO_LED_RED);
129 ao_delay(AO_MS_TO_TICKS(100));
131 ao_led_off(AO_LED_RED);
136 /* Thread for the master side of the packet link */
139 ao_packet_master(void)
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));
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;
165 ao_packet_flush(void)
170 /* Wait for previous packet to be received
172 while (tx_packet.len)
173 ao_sleep(&tx_packet);
175 /* Prepare next packet
178 memcpy(&tx_packet.d, tx_data, tx_used);
179 tx_packet.len = tx_used;
183 if (ao_packet_master_sleeping)
184 ao_wake_task(&ao_packet_task);
189 ao_packet_putchar(char c)
191 while (tx_used == AO_PACKET_MAX && ao_packet_enable)
194 if (ao_packet_enable)
195 tx_data[tx_used++] = c;
199 ao_packet_getchar(void) __critical
201 while (rx_used == rx_len && ao_packet_enable) {
206 if (!ao_packet_enable)
209 return rx_data[rx_used++];
213 ao_packet_echo(void) __reentrant
216 while (ao_packet_enable) {
217 c = ao_packet_getchar();
218 if (ao_packet_enable) {
220 if (c == (uint8_t) '\n')
227 static __xdata struct ao_task ao_packet_echo_task;
230 ao_packet_forward(void) __reentrant
233 ao_packet_enable = 1;
236 if (ao_cmd_lex_c == 'm')
237 ao_add_task(&ao_packet_task, ao_packet_master, "master");
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;
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);
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 },
259 ao_cmd_register(&ao_packet_cmds[0]);