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 printf ("rssi %d status %x\n", rx_packet.rssi, rx_packet.status); flush();
87 if (!(rx_packet.status & PKT_APPEND_STATUS_1_CRC_OK)) {
88 printf ("bad crc\n"); flush();
89 // return AO_DMA_ABORTED;
91 if (rx_packet.packet.len) {
92 if (rx_packet.packet.seq == rx_seq + 1 && rx_used == rx_len)
94 memcpy(rx_data, rx_packet.packet.d, rx_packet.packet.len);
96 rx_len = rx_packet.packet.len;
97 rx_seq = rx_packet.packet.seq;
98 tx_packet.ack = rx_seq;
102 if (rx_packet.packet.ack == tx_packet.seq) {
104 ao_wakeup(&tx_packet);
111 ao_packet_slave(void)
113 tx_packet.addr = ao_serial_number;
114 ao_radio_set_packet();
115 while (ao_packet_enable) {
117 ao_led_toggle(AO_LED_GREEN);
118 ao_delay(AO_MS_TO_TICKS(100));
120 ao_led_toggle(AO_LED_RED);
125 /* Thread for the master side of the packet link */
128 ao_packet_master(void)
132 ao_radio_set_packet();
133 tx_packet.addr = ao_serial_number;
134 tx_packet.len = AO_PACKET_SYN;
135 while (ao_packet_enable) {
136 ao_led_on(AO_LED_RED);
137 ao_delay(AO_MS_TO_TICKS(100));
139 ao_led_off(AO_LED_RED);
140 ao_led_on(AO_LED_GREEN);
141 ao_alarm(AO_MS_TO_TICKS(1000));
142 status = ao_packet_recv();
143 ao_led_off(AO_LED_GREEN);
144 if (status & AO_DMA_DONE) {
145 ao_packet_master_sleeping = 1;
146 ao_delay(AO_MS_TO_TICKS(1000));
147 ao_packet_master_sleeping = 0;
154 ao_packet_flush(void)
159 /* Wait for previous packet to be received
161 while (tx_packet.len)
162 ao_sleep(&tx_packet);
164 /* Prepare next packet
167 memcpy(&tx_packet.d, tx_data, tx_used);
168 tx_packet.len = tx_used;
172 if (ao_packet_master_sleeping)
173 ao_wake_task(&ao_packet_task);
178 ao_packet_putchar(char c)
180 while (tx_used == AO_PACKET_MAX && ao_packet_enable)
183 if (ao_packet_enable)
184 tx_data[tx_used++] = c;
188 ao_packet_getchar(void) __critical
190 while (rx_used == rx_len && ao_packet_enable)
193 if (!ao_packet_enable)
196 return rx_data[rx_used++];
200 ao_packet_echo(void) __reentrant
203 while (ao_packet_enable) {
204 c = ao_packet_getchar();
205 if (ao_packet_enable)
211 static __xdata struct ao_task ao_packet_echo_task;
214 ao_packet_forward(void) __reentrant
217 ao_packet_enable = 1;
220 if (ao_cmd_lex_c == 'm')
221 ao_add_task(&ao_packet_task, ao_packet_master, "master");
223 ao_add_task(&ao_packet_task, ao_packet_slave, "slave");
224 ao_add_task(&ao_packet_echo_task, ao_packet_echo, "echo");
225 while ((c = getchar()) != '~') {
226 ao_packet_putchar(c);
230 ao_packet_enable = 0;
232 while (ao_packet_echo_task.wchan || ao_packet_task.wchan) {
233 ao_wake_task(&ao_packet_echo_task);
234 ao_wake_task(&ao_packet_task);
239 __code struct ao_cmds ao_packet_cmds[] = {
240 { 'p', ao_packet_forward, "p {m|s} Remote packet link. m=master, s=slave" },
241 { 0, ao_packet_forward, NULL },
247 ao_cmd_register(&ao_packet_cmds[0]);