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);
37 RF_CHANNR = ao_config.radio_channel;
38 ao_dma_set_transfer(ao_radio_dma,
41 sizeof (struct ao_packet),
43 DMA_CFG0_TMODE_SINGLE |
44 DMA_CFG0_TRIGGER_RADIO,
47 DMA_CFG1_PRIORITY_HIGH);
48 ao_dma_start(ao_radio_dma);
50 __critical while (!ao_radio_dma_done)
51 ao_sleep(&ao_radio_dma_done);
52 ao_mutex_put(&ao_radio_mutex);
61 ao_mutex_get(&ao_radio_mutex);
63 RF_CHANNR = ao_config.radio_channel;
64 ao_dma_set_transfer(ao_radio_dma,
67 sizeof (struct ao_packet_recv),
69 DMA_CFG0_TMODE_SINGLE |
70 DMA_CFG0_TRIGGER_RADIO,
73 DMA_CFG1_PRIORITY_HIGH);
74 ao_dma_start(ao_radio_dma);
76 __critical while (!ao_radio_dma_done)
77 if (ao_sleep(&ao_radio_dma_done) != 0) {
78 printf("recv timeout\n"); flush();
81 dma_done = ao_radio_dma_done;
82 ao_mutex_put(&ao_radio_mutex);
84 if (dma_done & AO_DMA_DONE) {
85 printf ("rssi %d status %x\n", rx_packet.rssi, rx_packet.status); flush();
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) {
91 if (rx_packet.packet.seq == rx_seq + 1 && rx_used == rx_len)
93 memcpy(rx_data, rx_packet.packet.d, rx_packet.packet.len);
95 rx_len = rx_packet.packet.len;
96 rx_seq = rx_packet.packet.seq;
97 tx_packet.ack = rx_seq;
101 if (rx_packet.packet.ack == tx_packet.seq) {
103 ao_wakeup(&tx_packet);
110 ao_packet_slave(void)
112 tx_packet.addr = ao_serial_number;
113 ao_radio_set_packet();
114 while (ao_packet_enable) {
116 ao_led_toggle(AO_LED_GREEN);
117 ao_delay(AO_MS_TO_TICKS(100));
119 ao_led_toggle(AO_LED_RED);
124 /* Thread for the master side of the packet link */
127 ao_packet_master(void)
131 ao_radio_set_packet();
132 tx_packet.addr = ao_serial_number;
133 tx_packet.len = AO_PACKET_SYN;
134 while (ao_packet_enable) {
135 ao_led_on(AO_LED_RED);
136 ao_delay(AO_MS_TO_TICKS(100));
138 ao_led_off(AO_LED_RED);
139 ao_led_on(AO_LED_GREEN);
140 ao_alarm(AO_MS_TO_TICKS(1000));
141 status = ao_packet_recv();
142 ao_led_off(AO_LED_GREEN);
143 if (status & AO_DMA_DONE) {
144 ao_packet_master_sleeping = 1;
145 ao_delay(AO_MS_TO_TICKS(1000));
146 ao_packet_master_sleeping = 0;
153 ao_packet_flush(void)
158 /* Wait for previous packet to be received
160 while (tx_packet.len)
161 ao_sleep(&tx_packet);
163 /* Prepare next packet
166 memcpy(&tx_packet.d, tx_data, tx_used);
167 tx_packet.len = tx_used;
171 if (ao_packet_master_sleeping)
172 ao_wake_task(&ao_packet_task);
177 ao_packet_putchar(char c)
179 while (tx_used == AO_PACKET_MAX && ao_packet_enable)
182 if (ao_packet_enable)
183 tx_data[tx_used++] = c;
187 ao_packet_getchar(void) __critical
189 while (rx_used == rx_len && ao_packet_enable)
192 if (!ao_packet_enable)
195 return rx_data[rx_used++];
199 ao_packet_echo(void) __reentrant
202 while (ao_packet_enable) {
203 c = ao_packet_getchar();
204 if (ao_packet_enable)
210 static __xdata struct ao_task ao_packet_echo_task;
213 ao_packet_forward(void) __reentrant
216 ao_packet_enable = 1;
219 if (ao_cmd_lex_c == 'm')
220 ao_add_task(&ao_packet_task, ao_packet_master, "master");
222 ao_add_task(&ao_packet_task, ao_packet_slave, "slave");
223 ao_add_task(&ao_packet_echo_task, ao_packet_echo, "echo");
224 while ((c = getchar()) != '~') {
225 ao_packet_putchar(c);
229 ao_packet_enable = 0;
231 while (ao_packet_echo_task.wchan || ao_packet_task.wchan) {
232 ao_wake_task(&ao_packet_echo_task);
233 ao_wake_task(&ao_packet_task);
238 __code struct ao_cmds ao_packet_cmds[] = {
239 { 'p', ao_packet_forward, "p {m|s} Remote packet link. m=master, s=slave" },
240 { 0, ao_packet_forward, NULL },
246 ao_cmd_register(&ao_packet_cmds[0]);