Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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 #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                 }
118                 break;
119         case AO_IGNITE_MODE_MAIN:
120                 switch (igniter) {
121                 case ao_igniter_main:
122                         AO_IGNITER_SET_DROGUE(1);
123                         ao_delay(AO_IGNITER_FIRE_TIME);
124                         AO_IGNITER_SET_DROGUE(0);
125                         ao_delay(AO_IGNITER_CHARGE_TIME);
126                         AO_IGNITER_SET_MAIN(1);
127                         ao_delay(AO_IGNITER_FIRE_TIME);
128                         AO_IGNITER_SET_MAIN(0);
129                         break;
130                 }
131                 break;
132         }
133         ao_ignition[igniter].firing = 0;
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 #endif
158
159 void
160 ao_ignite_manual(void)
161 {
162         ao_cmd_white();
163         if (!ao_match_word("DoIt"))
164                 return;
165         ao_cmd_white();
166 #if HAS_IGNITE
167         if (ao_cmd_lex_c == 'm' && ao_match_word("main")) {
168                 ao_igniter_fire(ao_igniter_main);
169                 return;
170         }
171         if (ao_cmd_lex_c == 'd' && ao_match_word("drogue")) {
172                 ao_igniter_fire(ao_igniter_drogue);
173                 return;
174         }
175 #endif
176 #if AO_PYRO_NUM
177         if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9') {
178                 ao_pyro_manual(ao_cmd_lex_c - '0');
179                 return;
180         }
181 #endif
182         ao_cmd_status = ao_cmd_syntax_error;
183 }
184
185 __code char * __code ao_igniter_status_names[] = {
186         "unknown", "ready", "active", "open"
187 };
188
189 #if HAS_IGNITE
190 void
191 ao_ignite_print_status(enum ao_igniter igniter, __code char *name) __reentrant
192 {
193         enum ao_igniter_status status = ao_igniter_status(igniter);
194         printf("Igniter: %6s Status: %s\n",
195                name,
196                ao_igniter_status_names[status]);
197 }
198 #endif
199
200 void
201 ao_ignite_test(void)
202 {
203 #if HAS_IGNITE
204         ao_ignite_print_status(ao_igniter_drogue, "drogue");
205         ao_ignite_print_status(ao_igniter_main, "main");
206 #endif
207 #if AO_PYRO_NUM
208         ao_pyro_print_status();
209 #endif
210 }
211
212 __code struct ao_cmds ao_ignite_cmds[] = {
213         { ao_ignite_manual,     "i <key> {main|drogue}\0Fire igniter. <key> is doit with D&I" },
214         { ao_ignite_test,       "t\0Test igniter" },
215         { 0,    NULL },
216 };
217
218 #if HAS_IGNITE
219 __xdata struct ao_task ao_igniter_task;
220
221 void
222 ao_ignite_set_pins(void)
223 {
224         ao_enable_output(AO_IGNITER_DROGUE_PORT, AO_IGNITER_DROGUE_PIN, AO_IGNITER_DROGUE, 0);
225         ao_enable_output(AO_IGNITER_MAIN_PORT, AO_IGNITER_MAIN_PIN, AO_IGNITER_MAIN, 0);
226 }
227 #endif
228
229 void
230 ao_igniter_init(void)
231 {
232 #if HAS_IGNITE
233         ao_ignite_set_pins();
234         ao_add_task(&ao_igniter_task, ao_igniter, "igniter");
235 #endif
236         ao_cmd_register(&ao_ignite_cmds[0]);
237 }