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