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