Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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 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         struct ao_data packet;
46         int16_t value;
47         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 #define AO_IGNITER_SET_DROGUE(v)        ao_gpio_set(AO_IGNITER_DROGUE_PORT, AO_IGNITER_DROGUE_PIN, v)
76 #define AO_IGNITER_SET_MAIN(v)          ao_gpio_set(AO_IGNITER_MAIN_PORT, AO_IGNITER_MAIN_PIN, v)
77
78 #ifndef AO_IGNITER_FIRE_TIME
79 #define AO_IGNITER_FIRE_TIME            AO_MS_TO_TICKS(50)
80 #endif
81
82 #ifndef AO_IGNITER_CHARGE_TIME
83 #define AO_IGNITER_CHARGE_TIME          AO_MS_TO_TICKS(2000)
84 #endif
85
86 void
87 ao_igniter_fire(enum ao_igniter igniter)
88 {
89         ao_ignition[igniter].firing = 1;
90         switch(ao_config.ignite_mode) {
91         case AO_IGNITE_MODE_DUAL:
92                 switch (igniter) {
93                 case ao_igniter_drogue:
94                         AO_IGNITER_SET_DROGUE(1);
95                         ao_delay(AO_IGNITER_FIRE_TIME);
96                         AO_IGNITER_SET_DROGUE(0);
97                         break;
98                 case ao_igniter_main:
99                         AO_IGNITER_SET_MAIN(1);
100                         ao_delay(AO_IGNITER_FIRE_TIME);
101                         AO_IGNITER_SET_MAIN(0);
102                         break;
103                 }
104                 break;
105         case AO_IGNITE_MODE_APOGEE:
106                 switch (igniter) {
107                 case ao_igniter_drogue:
108                         AO_IGNITER_SET_DROGUE(1);
109                         ao_delay(AO_IGNITER_FIRE_TIME);
110                         AO_IGNITER_SET_DROGUE(0);
111                         ao_delay(AO_IGNITER_CHARGE_TIME);
112                         AO_IGNITER_SET_MAIN(1);
113                         ao_delay(AO_IGNITER_FIRE_TIME);
114                         AO_IGNITER_SET_MAIN(0);
115                         break;
116                 default:
117                         break;
118                 }
119                 break;
120         case AO_IGNITE_MODE_MAIN:
121                 switch (igniter) {
122                 case ao_igniter_main:
123                         AO_IGNITER_SET_DROGUE(1);
124                         ao_delay(AO_IGNITER_FIRE_TIME);
125                         AO_IGNITER_SET_DROGUE(0);
126                         ao_delay(AO_IGNITER_CHARGE_TIME);
127                         AO_IGNITER_SET_MAIN(1);
128                         ao_delay(AO_IGNITER_FIRE_TIME);
129                         AO_IGNITER_SET_MAIN(0);
130                         break;
131                 default:
132                         break;
133                 }
134                 break;
135         }
136         ao_ignition[igniter].firing = 0;
137 }
138
139 void
140 ao_igniter(void)
141 {
142         enum ao_igniter igniter;
143
144         ao_config_get();
145         for (;;) {
146                 ao_sleep(&ao_ignition);
147                 for (igniter = ao_igniter_drogue; igniter <= ao_igniter_main; igniter++) {
148                         if (ao_ignition[igniter].request && !ao_ignition[igniter].fired) {
149                                 if (igniter == ao_igniter_drogue && ao_config.apogee_delay)
150                                         ao_delay(AO_SEC_TO_TICKS(ao_config.apogee_delay));
151
152                                 ao_igniter_fire(igniter);
153                                 ao_delay(AO_IGNITER_CHARGE_TIME);
154                                 ao_ignition[igniter].fired = 1;
155                         }
156                 }
157         }
158 }
159
160 #endif
161
162 void
163 ao_ignite_manual(void)
164 {
165         ao_cmd_white();
166         if (!ao_match_word("DoIt"))
167                 return;
168         ao_cmd_white();
169 #if HAS_IGNITE
170         if (ao_cmd_lex_c == 'm' && ao_match_word("main")) {
171                 ao_igniter_fire(ao_igniter_main);
172                 return;
173         }
174         if (ao_cmd_lex_c == 'd' && ao_match_word("drogue")) {
175                 ao_igniter_fire(ao_igniter_drogue);
176                 return;
177         }
178 #endif
179 #if AO_PYRO_NUM
180         if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9') {
181                 ao_pyro_manual(ao_cmd_lex_c - '0');
182                 return;
183         }
184 #endif
185         ao_cmd_status = ao_cmd_syntax_error;
186 }
187
188 const char * const ao_igniter_status_names[] = {
189         "unknown", "ready", "active", "open"
190 };
191
192 #if HAS_IGNITE
193 void
194 ao_ignite_print_status(enum ao_igniter igniter, const char *name) 
195 {
196         enum ao_igniter_status status = ao_igniter_status(igniter);
197         printf("Igniter: %6s Status: %s\n",
198                name,
199                ao_igniter_status_names[status]);
200 }
201 #endif
202
203 void
204 ao_ignite_test(void)
205 {
206 #if HAS_IGNITE
207         ao_ignite_print_status(ao_igniter_drogue, "drogue");
208         ao_ignite_print_status(ao_igniter_main, "main");
209 #endif
210 #if AO_PYRO_NUM
211         ao_pyro_print_status();
212 #endif
213 }
214
215 const struct ao_cmds ao_ignite_cmds[] = {
216         { ao_ignite_manual,     "i <key> {main|drogue}\0Fire igniter. <key> is doit with D&I" },
217         { ao_ignite_test,       "t\0Test igniter" },
218         { 0,    NULL },
219 };
220
221 #if HAS_IGNITE
222 struct ao_task ao_igniter_task;
223
224 void
225 ao_ignite_set_pins(void)
226 {
227         ao_enable_output(AO_IGNITER_DROGUE_PORT, AO_IGNITER_DROGUE_PIN, 0);
228         ao_enable_output(AO_IGNITER_MAIN_PORT, AO_IGNITER_MAIN_PIN, 0);
229 }
230 #endif
231
232 void
233 ao_igniter_init(void)
234 {
235 #if HAS_IGNITE
236         ao_ignite_set_pins();
237         ao_add_task(&ao_igniter_task, ao_igniter, "igniter");
238 #endif
239         ao_cmd_register(&ao_ignite_cmds[0]);
240 }