altos: Send SPI message at flight state changes
[fw/altos] / src / 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
20 #if IGNITE_ON_P2
21 #define AO_IGNITER_DROGUE       P2_3
22 #define AO_IGNITER_MAIN         P2_4
23 #define AO_IGNITER_DIR          P2DIR
24 #define AO_IGNITER_DROGUE_BIT   (1 << 3)
25 #define AO_IGNITER_MAIN_BIT     (1 << 4)
26 #endif
27
28 #if IGNITE_ON_P0
29 #define AO_IGNITER_DROGUE       P0_5
30 #define AO_IGNITER_MAIN         P0_4
31 #define AO_IGNITER_DIR          P0DIR
32 #define AO_IGNITER_DROGUE_BIT   (1 << 5)
33 #define AO_IGNITER_MAIN_BIT     (1 << 4)
34 #endif
35
36 /* test these values with real igniters */
37 #define AO_IGNITER_OPEN         1000
38 #define AO_IGNITER_CLOSED       7000
39 #define AO_IGNITER_FIRE_TIME    AO_MS_TO_TICKS(50)
40 #define AO_IGNITER_CHARGE_TIME  AO_MS_TO_TICKS(2000)
41
42 struct ao_ignition {
43         uint8_t request;
44         uint8_t fired;
45         uint8_t firing;
46 };
47
48 __xdata struct ao_ignition ao_ignition[2];
49
50 void
51 ao_ignite(enum ao_igniter igniter) __critical
52 {
53         ao_ignition[igniter].request = 1;
54         ao_wakeup(&ao_ignition);
55 }
56
57 enum ao_igniter_status
58 ao_igniter_status(enum ao_igniter igniter)
59 {
60         __xdata struct ao_adc adc;
61         __pdata int16_t value;
62         __pdata uint8_t request, firing, fired;
63
64         __critical {
65                 ao_adc_get(&adc);
66                 request = ao_ignition[igniter].request;
67                 fired = ao_ignition[igniter].fired;
68                 firing = ao_ignition[igniter].firing;
69         }
70         if (firing || (request && !fired))
71                 return ao_igniter_active;
72
73         value = (AO_IGNITER_CLOSED>>1);
74         switch (igniter) {
75         case ao_igniter_drogue:
76                 value = adc.sense_d;
77                 break;
78         case ao_igniter_main:
79                 value = adc.sense_m;
80                 break;
81         }
82         if (value < AO_IGNITER_OPEN)
83                 return ao_igniter_open;
84         else if (value > AO_IGNITER_CLOSED)
85                 return ao_igniter_ready;
86         else
87                 return ao_igniter_unknown;
88 }
89
90 void
91 ao_igniter_fire(enum ao_igniter igniter) __critical
92 {
93         ao_ignition[igniter].firing = 1;
94         switch(ao_config.ignite_mode) {
95         case AO_IGNITE_MODE_DUAL:
96                 switch (igniter) {
97                 case ao_igniter_drogue:
98                         AO_IGNITER_DROGUE = 1;
99                         ao_delay(AO_IGNITER_FIRE_TIME);
100                         AO_IGNITER_DROGUE = 0;
101                         break;
102                 case ao_igniter_main:
103                         AO_IGNITER_MAIN = 1;
104                         ao_delay(AO_IGNITER_FIRE_TIME);
105                         AO_IGNITER_MAIN = 0;
106                         break;
107                 }
108                 break;
109         case AO_IGNITE_MODE_APOGEE:
110                 switch (igniter) {
111                 case ao_igniter_drogue:
112                         AO_IGNITER_DROGUE = 1;
113                         ao_delay(AO_IGNITER_FIRE_TIME);
114                         AO_IGNITER_DROGUE = 0;
115                         ao_delay(AO_IGNITER_CHARGE_TIME);
116                         AO_IGNITER_MAIN = 1;
117                         ao_delay(AO_IGNITER_FIRE_TIME);
118                         AO_IGNITER_MAIN = 0;
119                         break;
120                 }
121                 break;
122         case AO_IGNITE_MODE_MAIN:
123                 switch (igniter) {
124                 case ao_igniter_main:
125                         AO_IGNITER_DROGUE = 1;
126                         ao_delay(AO_IGNITER_FIRE_TIME);
127                         AO_IGNITER_DROGUE = 0;
128                         ao_delay(AO_IGNITER_CHARGE_TIME);
129                         AO_IGNITER_MAIN = 1;
130                         ao_delay(AO_IGNITER_FIRE_TIME);
131                         AO_IGNITER_MAIN = 0;
132                         break;
133                 }
134                 break;
135         }
136         ao_ignition[igniter].firing = 0;
137 }
138
139 void
140 ao_igniter(void)
141 {
142         __xdata enum ao_ignter 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 void
161 ao_ignite_manual(void)
162 {
163         ao_cmd_white();
164         if (!ao_match_word("DoIt"))
165                 return;
166         ao_cmd_white();
167         if (ao_cmd_lex_c == 'm') {
168                 if(ao_match_word("main"))
169                         ao_igniter_fire(ao_igniter_main);
170         } else {
171                 if(ao_match_word("drogue"))
172                         ao_igniter_fire(ao_igniter_drogue);
173         }
174 }
175
176 static __code char * __code igniter_status_names[] = {
177         "unknown", "ready", "active", "open"
178 };
179
180 void
181 ao_ignite_print_status(enum ao_igniter igniter, __code char *name) __reentrant
182 {
183         enum ao_igniter_status status = ao_igniter_status(igniter);
184         printf("Igniter: %6s Status: %s\n",
185                name,
186                igniter_status_names[status]);
187 }
188
189 void
190 ao_ignite_test(void)
191 {
192         ao_ignite_print_status(ao_igniter_drogue, "drogue");
193         ao_ignite_print_status(ao_igniter_main, "main");
194 }
195
196 __code struct ao_cmds ao_ignite_cmds[] = {
197         { ao_ignite_manual,     "i <key> {main|drogue}\0Fire igniter. <key> is doit with D&I" },
198         { ao_ignite_test,       "t\0Test igniter" },
199         { 0,    NULL },
200 };
201
202 __xdata struct ao_task ao_igniter_task;
203
204 void
205 ao_ignite_set_pins(void)
206 {
207         AO_IGNITER_DROGUE = 0;
208         AO_IGNITER_MAIN = 0;
209         AO_IGNITER_DIR |= AO_IGNITER_DROGUE_BIT | AO_IGNITER_MAIN_BIT;
210 }
211
212 void
213 ao_igniter_init(void)
214 {
215         ao_ignite_set_pins();
216         ao_cmd_register(&ao_ignite_cmds[0]);
217         ao_add_task(&ao_igniter_task, ao_igniter, "igniter");
218 }