update changelogs for Debian build
[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 #define AO_IGNITER_DROGUE       P2_3
21 #define AO_IGNITER_MAIN         P2_4
22 #define AO_IGNITER_DIR          P2DIR
23 #define AO_IGNITER_DROGUE_BIT   (1 << 3)
24 #define AO_IGNITER_MAIN_BIT     (1 << 4)
25
26 /* test these values with real igniters */
27 #define AO_IGNITER_OPEN         1000
28 #define AO_IGNITER_CLOSED       7000
29 #define AO_IGNITER_FIRE_TIME    AO_MS_TO_TICKS(500)
30 #define AO_IGNITER_CHARGE_TIME  AO_MS_TO_TICKS(2000)
31
32 struct ao_ignition {
33         uint8_t request;
34         uint8_t fired;
35         uint8_t firing;
36 };
37
38 __xdata struct ao_ignition ao_ignition[2];
39
40 void
41 ao_ignite(enum ao_igniter igniter) __critical
42 {
43         ao_ignition[igniter].request = 1;
44         ao_wakeup(&ao_ignition[0]);
45 }
46
47 enum ao_igniter_status
48 ao_igniter_status(enum ao_igniter igniter)
49 {
50         __xdata struct ao_adc adc;
51         __xdata int16_t value;
52         __xdata uint8_t request, firing, fired;
53
54         __critical {
55                 ao_adc_sleep();
56                 ao_adc_get(&adc);
57                 request = ao_ignition[igniter].request;
58                 fired = ao_ignition[igniter].fired;
59                 firing = ao_ignition[igniter].firing;
60         }
61         if (firing || (request && !fired))
62                 return ao_igniter_active;
63
64         value = (AO_IGNITER_CLOSED>>1);
65         switch (igniter) {
66         case ao_igniter_drogue:
67                 value = adc.sense_d;
68                 break;
69         case ao_igniter_main:
70                 value = adc.sense_m;
71                 break;
72         }
73         if (value < AO_IGNITER_OPEN)
74                 return ao_igniter_open;
75         else if (value > AO_IGNITER_CLOSED)
76                 return ao_igniter_ready;
77         else
78                 return ao_igniter_unknown;
79 }
80
81 void
82 ao_igniter_fire(enum ao_igniter igniter) __critical
83 {
84         ao_ignition[igniter].firing = 1;
85         switch (igniter) {
86         case ao_igniter_drogue:
87                 AO_IGNITER_DROGUE = 1;
88                 ao_delay(AO_IGNITER_FIRE_TIME);
89                 AO_IGNITER_DROGUE = 0;
90                 break;
91         case ao_igniter_main:
92                 AO_IGNITER_MAIN = 1;
93                 ao_delay(AO_IGNITER_FIRE_TIME);
94                 AO_IGNITER_MAIN = 0;
95                 break;
96         }
97         ao_ignition[igniter].firing = 0;
98 }
99
100 void
101 ao_igniter(void)
102 {
103         __xdata enum ao_ignter igniter;
104         __xdata enum ao_igniter_status status;
105
106         for (;;) {
107                 ao_sleep(&ao_ignition);
108                 for (igniter = ao_igniter_drogue; igniter <= ao_igniter_main; igniter++) {
109                         if (ao_ignition[igniter].request && !ao_ignition[igniter].fired) {
110                                 ao_igniter_fire(igniter);
111                                 ao_delay(AO_IGNITER_CHARGE_TIME);
112                                 status = ao_igniter_status(igniter);
113                                 if (status == ao_igniter_open)
114                                         ao_ignition[igniter].fired = 1;
115                         }
116                 }
117         }
118 }
119
120 static uint8_t
121 ao_match_word(__code char *word)
122 {
123         while (*word) {
124                 if (ao_cmd_lex_c != *word) {
125                         ao_cmd_status = ao_cmd_syntax_error;
126                         return 0;
127                 }
128                 word++;
129                 ao_cmd_lex();
130         }
131         return 1;
132 }
133
134 void
135 ao_ignite_manual(void)
136 {
137         ao_cmd_white();
138         if (!ao_match_word("DoIt"))
139                 return;
140         ao_cmd_white();
141         if (ao_cmd_lex_c == 'm') {
142                 if(ao_match_word("main"))
143                         ao_igniter_fire(ao_igniter_main);
144         } else {
145                 if(ao_match_word("drogue"))
146                         ao_igniter_fire(ao_igniter_drogue);
147         }
148 }
149
150 static __code char *igniter_status_names[] = {
151         "unknown", "ready", "active", "open"
152 };
153
154 void
155 ao_ignite_print_status(enum ao_igniter igniter, __code char *name) __reentrant
156 {
157         enum ao_igniter_status status = ao_igniter_status(igniter);
158         printf("Igniter: %6s Status: %s\n",
159                name,
160                igniter_status_names[status]);
161 }
162
163 void
164 ao_ignite_test(void)
165 {
166         ao_ignite_print_status(ao_igniter_drogue, "drogue");
167         ao_ignite_print_status(ao_igniter_main, "main");
168 }
169
170 __code struct ao_cmds ao_ignite_cmds[] = {
171         { 'i',  ao_ignite_manual,       "i <key> {main|drogue}              Fire igniter. <key> is doit with D&I" },
172         { 't',  ao_ignite_test,         "t                                  Test igniter continuity" },
173         { 0,    ao_ignite_manual,       NULL },
174 };
175
176 __xdata struct ao_task ao_igniter_task;
177
178 void
179 ao_igniter_init(void)
180 {
181         AO_IGNITER_DROGUE = 0;
182         AO_IGNITER_MAIN = 0;
183         AO_IGNITER_DIR |= AO_IGNITER_DROGUE_BIT | AO_IGNITER_MAIN_BIT;
184         ao_cmd_register(&ao_ignite_cmds[0]);
185         ao_add_task(&ao_igniter_task, ao_igniter, "igniter");
186 }