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