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