altos: Don't try to grab radio while firing MM igniters
[fw/altos] / src / core / ao_ignite.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 #include <ao_data.h>
20
21 __xdata struct ao_ignition ao_ignition[2];
22
23 void
24 ao_ignite(enum ao_igniter igniter) __critical
25 {
26         ao_ignition[igniter].request = 1;
27         ao_wakeup(&ao_ignition);
28 }
29
30 #ifndef AO_SENSE_DROGUE
31 #define AO_SENSE_DROGUE(p)      ((p)->adc.sense_d)
32 #define AO_SENSE_MAIN(p)        ((p)->adc.sense_m)
33 #endif
34
35 enum ao_igniter_status
36 ao_igniter_status(enum ao_igniter igniter)
37 {
38         __xdata struct ao_data packet;
39         __pdata int16_t value;
40         __pdata uint8_t request, firing, fired;
41
42         __critical {
43                 ao_data_get(&packet);
44                 request = ao_ignition[igniter].request;
45                 fired = ao_ignition[igniter].fired;
46                 firing = ao_ignition[igniter].firing;
47         }
48         if (firing || (request && !fired))
49                 return ao_igniter_active;
50
51         value = (AO_IGNITER_CLOSED>>1);
52         switch (igniter) {
53         case ao_igniter_drogue:
54                 value = AO_SENSE_DROGUE(&packet);
55                 break;
56         case ao_igniter_main:
57                 value = AO_SENSE_MAIN(&packet);
58                 break;
59         }
60         if (value < AO_IGNITER_OPEN)
61                 return ao_igniter_open;
62         else if (value > AO_IGNITER_CLOSED)
63                 return ao_igniter_ready;
64         else
65                 return ao_igniter_unknown;
66 }
67
68 #ifndef AO_IGNITER_SET_DROGUE
69 #define AO_IGNITER_SET_DROGUE(v)        AO_IGNITER_DROGUE = (v)
70 #define AO_IGNITER_SET_MAIN(v)          AO_IGNITER_MAIN = (v)
71 #endif
72
73 #ifndef AO_IGNITER_FIRE_TIME
74 #define AO_IGNITER_FIRE_TIME            AO_MS_TO_TICKS(50)
75 #endif
76
77 #ifndef AO_IGNITER_CHARGE_TIME
78 #define AO_IGNITER_CHARGE_TIME          AO_MS_TO_TICKS(2000)
79 #endif
80
81 void
82 ao_igniter_fire(enum ao_igniter igniter) __critical
83 {
84 #ifndef MEGAMETRUM
85         ao_mutex_get(&ao_radio_mutex);
86 #endif
87         ao_ignition[igniter].firing = 1;
88         switch(ao_config.ignite_mode) {
89         case AO_IGNITE_MODE_DUAL:
90                 switch (igniter) {
91                 case ao_igniter_drogue:
92                         AO_IGNITER_SET_DROGUE(1);
93                         ao_delay(AO_IGNITER_FIRE_TIME);
94                         AO_IGNITER_SET_DROGUE(0);
95                         break;
96                 case ao_igniter_main:
97                         AO_IGNITER_SET_MAIN(1);
98                         ao_delay(AO_IGNITER_FIRE_TIME);
99                         AO_IGNITER_SET_MAIN(0);
100                         break;
101                 }
102                 break;
103         case AO_IGNITE_MODE_APOGEE:
104                 switch (igniter) {
105                 case ao_igniter_drogue:
106                         AO_IGNITER_SET_DROGUE(1);
107                         ao_delay(AO_IGNITER_FIRE_TIME);
108                         AO_IGNITER_SET_DROGUE(0);
109                         ao_delay(AO_IGNITER_CHARGE_TIME);
110                         AO_IGNITER_SET_MAIN(1);
111                         ao_delay(AO_IGNITER_FIRE_TIME);
112                         AO_IGNITER_SET_MAIN(0);
113                         break;
114                 }
115                 break;
116         case AO_IGNITE_MODE_MAIN:
117                 switch (igniter) {
118                 case ao_igniter_main:
119                         AO_IGNITER_SET_DROGUE(1);
120                         ao_delay(AO_IGNITER_FIRE_TIME);
121                         AO_IGNITER_SET_DROGUE(0);
122                         ao_delay(AO_IGNITER_CHARGE_TIME);
123                         AO_IGNITER_SET_MAIN(1);
124                         ao_delay(AO_IGNITER_FIRE_TIME);
125                         AO_IGNITER_SET_MAIN(0);
126                         break;
127                 }
128                 break;
129         }
130         ao_ignition[igniter].firing = 0;
131 #ifndef MEGAMETRUM
132         ao_mutex_put(&ao_radio_mutex);
133 #endif
134 }
135
136 void
137 ao_igniter(void)
138 {
139         __xdata enum ao_igniter igniter;
140
141         ao_config_get();
142         for (;;) {
143                 ao_sleep(&ao_ignition);
144                 for (igniter = ao_igniter_drogue; igniter <= ao_igniter_main; igniter++) {
145                         if (ao_ignition[igniter].request && !ao_ignition[igniter].fired) {
146                                 if (igniter == ao_igniter_drogue && ao_config.apogee_delay)
147                                         ao_delay(AO_SEC_TO_TICKS(ao_config.apogee_delay));
148
149                                 ao_igniter_fire(igniter);
150                                 ao_delay(AO_IGNITER_CHARGE_TIME);
151                                 ao_ignition[igniter].fired = 1;
152                         }
153                 }
154         }
155 }
156
157 void
158 ao_ignite_manual(void)
159 {
160         ao_cmd_white();
161         if (!ao_match_word("DoIt"))
162                 return;
163         ao_cmd_white();
164         if (ao_cmd_lex_c == 'm') {
165                 if(ao_match_word("main"))
166                         ao_igniter_fire(ao_igniter_main);
167         } else {
168                 if(ao_match_word("drogue"))
169                         ao_igniter_fire(ao_igniter_drogue);
170         }
171 }
172
173 static __code char * __code igniter_status_names[] = {
174         "unknown", "ready", "active", "open"
175 };
176
177 void
178 ao_ignite_print_status(enum ao_igniter igniter, __code char *name) __reentrant
179 {
180         enum ao_igniter_status status = ao_igniter_status(igniter);
181         printf("Igniter: %6s Status: %s\n",
182                name,
183                igniter_status_names[status]);
184 }
185
186 void
187 ao_ignite_test(void)
188 {
189         ao_ignite_print_status(ao_igniter_drogue, "drogue");
190         ao_ignite_print_status(ao_igniter_main, "main");
191 }
192
193 __code struct ao_cmds ao_ignite_cmds[] = {
194         { ao_ignite_manual,     "i <key> {main|drogue}\0Fire igniter. <key> is doit with D&I" },
195         { ao_ignite_test,       "t\0Test igniter" },
196         { 0,    NULL },
197 };
198
199 __xdata struct ao_task ao_igniter_task;
200
201 void
202 ao_ignite_set_pins(void)
203 {
204         ao_enable_output(AO_IGNITER_DROGUE_PORT, AO_IGNITER_DROGUE_PIN, 0);
205         ao_enable_output(AO_IGNITER_MAIN_PORT, AO_IGNITER_MAIN_PIN, 0);
206 }
207
208 void
209 ao_igniter_init(void)
210 {
211         ao_ignite_set_pins();
212         ao_cmd_register(&ao_ignite_cmds[0]);
213         ao_add_task(&ao_igniter_task, ao_igniter, "igniter");
214 }