altos/telelco: 10ms is not enough time to get a packet back
[fw/altos] / src / drivers / ao_lco_func.c
1 /*
2  * Copyright © 2012 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 #include <ao_pad.h>
20 #include <ao_radio_cmac.h>
21 #include <ao_lco_func.h>
22
23 static __xdata struct ao_pad_command    command;
24 static __xdata uint8_t                  ao_lco_mutex;
25
26 int8_t
27 ao_lco_query(uint16_t box, struct ao_pad_query *query, uint16_t *tick_offset)
28 {
29         uint8_t         i;
30         int8_t          r;
31         uint16_t        sent_time;
32
33         ao_mutex_get(&ao_lco_mutex);
34         command.tick = ao_time() - *tick_offset;
35         command.box = box;
36         command.cmd = AO_LAUNCH_QUERY;
37         command.channels = 0;
38         ao_radio_cmac_send(&command, sizeof (command));
39         sent_time = ao_time();
40         r = ao_radio_cmac_recv(query, sizeof (*query), AO_MS_TO_TICKS(20));
41         if (r == AO_RADIO_CMAC_OK)
42                 *tick_offset = sent_time - query->tick;
43         ao_mutex_put(&ao_lco_mutex);
44         return r;
45 }
46
47 void
48 ao_lco_arm(uint16_t box, uint8_t channels, uint16_t tick_offset)
49 {
50         ao_mutex_get(&ao_lco_mutex);
51         command.tick = ao_time() - tick_offset;
52         command.box = box;
53         command.cmd = AO_LAUNCH_ARM;
54         command.channels = channels;
55         ao_radio_cmac_send(&command, sizeof (command));
56         ao_mutex_put(&ao_lco_mutex);
57 }
58
59 void
60 ao_lco_ignite(uint16_t box, uint8_t channels, uint16_t tick_offset)
61 {
62         ao_mutex_get(&ao_lco_mutex);
63         command.tick = ao_time() - tick_offset;
64         command.box = box;
65         command.cmd = AO_LAUNCH_FIRE;
66         command.channels = channels;
67         ao_radio_cmac_send(&command, sizeof (command));
68         ao_mutex_put(&ao_lco_mutex);
69 }
70