altos: Disable debug printfs and fix pad ignite time for telefire
[fw/altos] / src / drivers / ao_pad.c
1 /*
2  * Copyright © 2012 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_pad.h>
20 #include <ao_74hc497.h>
21 #include <ao_radio_cmac.h>
22
23 static __xdata uint8_t ao_pad_ignite;
24 static __xdata struct ao_pad_command    command;
25 static __xdata struct ao_pad_query      query;
26
27 #if 0
28 #define PRINTD(...) printf(__VA_ARGS__)
29 #define FLUSHD()    flush()
30 #else
31 #define PRINTD(...) 
32 #define FLUSHD()    
33 #endif
34
35 static void
36 ao_pad_run(void)
37 {
38         for (;;) {
39                 while (!ao_pad_ignite)
40                         ao_sleep(&ao_pad_ignite);
41                 /*
42                  * Actually set the pad bits
43                  */
44                 AO_PAD_PORT = (AO_PAD_PORT & (~AO_PAD_ALL_PINS)) | ao_pad_ignite;
45                 while (ao_pad_ignite) {
46                         ao_pad_ignite = 0;
47                         ao_delay(AO_PAD_FIRE_TIME);
48                 }
49                 AO_PAD_PORT &= ~(AO_PAD_ALL_PINS);
50         }
51 }
52
53 static void
54 ao_pad_monitor(void)
55 {
56         uint8_t                 c;
57         uint8_t                 sample;
58         __pdata uint8_t         prev = 0, cur = 0;
59         __pdata uint8_t         beeping = 0;
60         __xdata struct ao_data  *packet;
61
62         sample = ao_data_head;
63         for (;;) {
64                 __pdata int16_t                 pyro;
65                 ao_arch_critical(
66                         while (sample == ao_data_head)
67                                 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
68                         );
69
70                 packet = &ao_data_ring[sample];
71                 sample = ao_data_ring_next(sample);
72
73                 pyro = packet->adc.pyro;
74
75 #define VOLTS_TO_PYRO(x) ((int16_t) ((x) * 27.0 / 127.0 / 3.3 * 32767.0))
76
77                 cur = 0;
78                 if (pyro > VOLTS_TO_PYRO(4))
79                         query.arm_status = AO_PAD_ARM_STATUS_ARMED;
80                 else if (pyro < VOLTS_TO_PYRO(1))
81                         query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
82                 else
83                         query.arm_status = AO_PAD_ARM_STATUS_UNKNOWN;
84
85                 for (c = 0; c < AO_PAD_NUM; c++) {
86                         int16_t         sense = packet->adc.sense[c];
87                         uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
88
89                         if (query.arm_status == AO_PAD_ARM_STATUS_ARMED) {
90                                 /*
91                                  *      pyro is run through a divider, so pyro = v_pyro * 27 / 127 ~= v_pyro / 20
92                                  *      v_pyro = pyro * 127 / 27
93                                  *
94                                  *              v_pyro \
95                                  *      100k            igniter
96                                  *              output /        
97                                  *      100k           \
98                                  *              sense   relay
99                                  *      27k            / 
100                                  *              gnd ---   
101                                  *
102                                  *      If the relay is closed, then sense will be 0
103                                  *      If no igniter is present, then sense will be v_pyro * 27k/227k = pyro * 127 / 227 ~= pyro/2
104                                  *      If igniter is present, then sense will be v_pyro * 27k/127k ~= v_pyro / 20 = pyro
105                                  */
106
107                                 if (sense <= pyro / 8)
108                                         status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
109                                 else if (pyro / 8 * 3 <= sense && sense <= pyro / 8 * 5)
110                                         status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
111                                 else if (pyro / 8 * 7 <= sense) {
112                                         status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
113                                         cur |= AO_LED_CONTINUITY(c);
114                                 }
115                         }
116                         query.igniter_status[c] = status;
117                 }
118                 if (cur != prev) {
119                         ao_led_set_mask(cur, AO_LED_CONTINUITY_MASK);
120                         prev = cur;
121                 }
122
123                 if (pyro > VOLTS_TO_PYRO(9) && sample == 0) {
124                         beeping = 1;
125                         ao_beep(AO_BEEP_HIGH);
126                 } else if (beeping) {
127                         beeping = 0;
128                         ao_beep(0);
129                 }
130         }
131 }
132
133 static __pdata uint8_t  ao_pad_armed;
134 static __pdata uint16_t ao_pad_arm_time;
135 static __pdata uint8_t  ao_pad_box;
136 static __xdata uint8_t  ao_pad_disabled;
137
138 void
139 ao_pad_disable(void)
140 {
141         if (!ao_pad_disabled) {
142                 ao_pad_disabled = 1;
143                 ao_radio_recv_abort();
144         }
145 }
146
147 void
148 ao_pad_enable(void)
149 {
150         ao_pad_disabled = 0;
151         ao_wakeup (&ao_pad_disabled);
152 }
153
154 static void
155 ao_pad(void)
156 {
157         int16_t time_difference;
158         int8_t  ret;
159
160         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
161         ao_pad_box = 0;
162         ao_led_set(0);
163         ao_led_on(AO_LED_POWER);
164         for (;;) {
165                 FLUSHD();
166                 while (ao_pad_disabled)
167                         ao_sleep(&ao_pad_disabled);
168                 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
169                 PRINTD ("cmac_recv %d\n", ret);
170                 if (ret != AO_RADIO_CMAC_OK)
171                         continue;
172                 
173                 PRINTD ("tick %d box %d cmd %d channels %02x\n",
174                         command.tick, command.box, command.cmd, command.channels);
175
176                 switch (command.cmd) {
177                 case AO_LAUNCH_ARM:
178                         if (command.box != ao_pad_box) {
179                                 PRINTD ("box number mismatch\n");
180                                 break;
181                         }
182
183                         if (command.channels & ~(AO_PAD_ALL_PINS))
184                                 break;
185
186                         time_difference = command.tick - ao_time();
187                         PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
188                         if (time_difference < 0)
189                                 time_difference = -time_difference;
190                         if (time_difference > 10) {
191                                 PRINTD ("time difference too large %d\n", time_difference);
192                                 break;
193                         }
194                         PRINTD ("armed\n");
195                         ao_pad_armed = command.channels;
196                         ao_pad_arm_time = ao_time();
197
198                         /* fall through ... */
199
200                 case AO_LAUNCH_QUERY:
201                         if (command.box != ao_pad_box) {
202                                 PRINTD ("box number mismatch\n");
203                                 break;
204                         }
205
206                         query.tick = ao_time();
207                         query.box = ao_pad_box;
208                         query.channels = AO_PAD_ALL_PINS;
209                         query.armed = ao_pad_armed;
210                         PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
211                                 query.tick, query.box, query.channels, query.armed,
212                                 query.arm_status,
213                                 query.igniter_status[0],
214                                 query.igniter_status[1],
215                                 query.igniter_status[2],
216                                 query.igniter_status[3]);
217                         ao_radio_cmac_send(&query, sizeof (query));
218                         break;
219                 case AO_LAUNCH_FIRE:
220                         if (!ao_pad_armed) {
221                                 PRINTD ("not armed\n");
222                                 break;
223                         }
224                         if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
225                                 PRINTD ("late pad arm_time %d time %d\n",
226                                         ao_pad_arm_time, ao_time());
227                                 break;
228                         }
229                         time_difference = command.tick - ao_time();
230                         if (time_difference < 0)
231                                 time_difference = -time_difference;
232                         if (time_difference > 10) {
233                                 PRINTD ("time different too large %d\n", time_difference);
234                                 break;
235                         }
236                         PRINTD ("ignite\n");
237                         ao_pad_ignite = ao_pad_armed;
238                         ao_wakeup(&ao_pad_ignite);
239                         break;
240                 }
241         }
242 }
243
244 void
245 ao_pad_test(void)
246 {
247         uint8_t c;
248
249         printf ("Arm switch: ");
250         switch (query.arm_status) {
251         case AO_PAD_ARM_STATUS_ARMED:
252                 printf ("Armed\n");
253                 break;
254         case AO_PAD_ARM_STATUS_DISARMED:
255                 printf ("Disarmed\n");
256                 break;
257         case AO_PAD_ARM_STATUS_UNKNOWN:
258                 printf ("Unknown\n");
259                 break;
260         }
261
262         for (c = 0; c < AO_PAD_NUM; c++) {
263                 printf ("Pad %d: ");
264                 switch (query.igniter_status[c]) {
265                 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED:     printf ("No igniter. Relay closed\n"); break;
266                 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN:       printf ("No igniter. Relay open\n"); break;
267                 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN:     printf ("Good igniter. Relay open\n"); break;
268                 case AO_PAD_IGNITER_STATUS_UNKNOWN:                     printf ("Unknown\n"); break;
269                 }
270         }
271 }
272
273 void
274 ao_pad_manual(void)
275 {
276         ao_cmd_white();
277         if (!ao_match_word("DoIt"))
278                 return;
279         ao_cmd_decimal();
280         if (ao_cmd_status != ao_cmd_success)
281                 return;
282         ao_pad_ignite = 1 << ao_cmd_lex_i;
283         ao_wakeup(&ao_pad_ignite);
284 }
285
286 static __xdata struct ao_task ao_pad_task;
287 static __xdata struct ao_task ao_pad_ignite_task;
288 static __xdata struct ao_task ao_pad_monitor_task;
289
290 __code struct ao_cmds ao_pad_cmds[] = {
291         { ao_pad_test,  "t\0Test pad continuity" },
292         { ao_pad_manual,        "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
293         { 0, NULL }
294 };
295
296 void
297 ao_pad_init(void)
298 {
299 #if AO_PAD_NUM > 0
300         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
301 #endif
302 #if AO_PAD_NUM > 1
303         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
304 #endif
305 #if AO_PAD_NUM > 2
306         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
307 #endif
308 #if AO_PAD_NUM > 3
309         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
310 #endif
311         ao_cmd_register(&ao_pad_cmds[0]);
312         ao_add_task(&ao_pad_task, ao_pad, "pad listener");
313         ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
314         ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");
315 }