2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #include <ao_74hc165.h>
23 #include <ao_radio_cmac.h>
25 static __xdata uint8_t ao_pad_ignite;
26 static __xdata struct ao_pad_command command;
27 static __xdata struct ao_pad_query query;
28 static __pdata uint8_t ao_pad_armed;
29 static __pdata uint16_t ao_pad_arm_time;
30 static __pdata uint8_t ao_pad_box;
31 static __xdata uint8_t ao_pad_disabled;
32 static __pdata uint16_t ao_pad_packet_time;
34 #ifndef AO_PAD_RSSI_MINIMUM
35 #define AO_PAD_RSSI_MINIMUM -90
41 static __pdata uint8_t ao_pad_debug;
42 #define PRINTD(...) (ao_pad_debug ? (printf(__VA_ARGS__), 0) : 0)
43 #define FLUSHD() (ao_pad_debug ? (flush(), 0) : 0)
53 ao_gpio_set(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, v);
56 ao_beep(v ? AO_BEEP_MID : 0);
67 ao_gpio_set(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, v);
74 #define pins_pad(pad) (*((AO_PAD_ ## pad ## _PORT) == AO_PAD_PORT_0 ? (&pins0) : (&pins1)))
76 #define pins_pad(pad) pins0
77 #define AO_PAD_PORT_0 AO_PAD_PORT
89 while (!ao_pad_ignite)
90 ao_sleep(&ao_pad_ignite);
92 * Actually set the pad bits
99 if (ao_pad_ignite & (1 << 0))
100 pins_pad(0) |= (1 << AO_PAD_PIN_0);
103 if (ao_pad_ignite & (1 << 1))
104 pins_pad(1) |= (1 << AO_PAD_PIN_1);
107 if (ao_pad_ignite & (1 << 2))
108 pins_pad(2) |= (1 << AO_PAD_PIN_2);
111 if (ao_pad_ignite & (1 << 3))
112 pins_pad(3) |= (1 << AO_PAD_PIN_3);
115 if (ao_pad_ignite & (1 << 4))
116 pins_pad(4) |= (1 << AO_PAD_PIN_4);
119 if (ao_pad_ignite & (1 << 5))
120 pins_pad(5) |= (1 << AO_PAD_PIN_5);
123 if (ao_pad_ignite & (1 << 6))
124 pins_pad(6) |= (1 << AO_PAD_PIN_6);
127 if (ao_pad_ignite & (1 << 7))
128 pins_pad(7) |= (1 << AO_PAD_PIN_7);
131 PRINTD("ignite pins 0x%x 0x%x\n", pins0, pins1);
132 ao_gpio_set_bits(AO_PAD_PORT_0, pins0);
133 ao_gpio_set_bits(AO_PAD_PORT_1, pins1);
135 PRINTD("ignite pins 0x%x\n", pins0);
136 ao_gpio_set_bits(AO_PAD_PORT_0, pins0);
138 while (ao_pad_ignite) {
141 ao_delay(AO_PAD_FIRE_TIME);
144 ao_gpio_clr_bits(AO_PAD_PORT_0, pins0);
145 ao_gpio_clr_bits(AO_PAD_PORT_1, pins1);
146 PRINTD("turn off pins 0x%x 0x%x\n", pins0, pins1);
148 ao_gpio_set_bits(AO_PAD_PORT_0, pins0);
149 PRINTD("turn off pins 0x%x\n", pins0);
154 #define AO_PAD_ARM_SIREN_INTERVAL 200
156 #ifndef AO_PYRO_R_PYRO_SENSE
157 #define AO_PYRO_R_PYRO_SENSE 100
158 #define AO_PYRO_R_SENSE_GND 27
159 #define AO_FIRE_R_POWER_FET 100
160 #define AO_FIRE_R_FET_SENSE 100
161 #define AO_FIRE_R_SENSE_GND 27
169 __pdata uint8_t prev = 0, cur = 0;
170 __pdata uint8_t beeping = 0;
171 __xdata volatile struct ao_data *packet;
172 __pdata uint16_t arm_beep_time = 0;
174 sample = ao_data_head;
176 __pdata int16_t pyro;
178 while (sample == ao_data_head)
179 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
183 packet = &ao_data_ring[sample];
184 sample = ao_data_ring_next(sample);
186 pyro = packet->adc.pyro;
188 #define VOLTS_TO_PYRO(x) ((int16_t) ((x) * ((1.0 * AO_PYRO_R_SENSE_GND) / \
189 (1.0 * (AO_PYRO_R_SENSE_GND + AO_PYRO_R_PYRO_SENSE)) / 3.3 * AO_ADC_MAX)))
192 #define VOLTS_TO_FIRE(x) ((int16_t) ((x) * ((1.0 * AO_FIRE_R_SENSE_GND) / \
193 (1.0 * (AO_FIRE_R_SENSE_GND + AO_FIRE_R_FET_SENSE)) / 3.3 * AO_ADC_MAX)))
195 /* convert ADC value to voltage in tenths, then add .2 for the diode drop */
196 query.battery = (packet->adc.batt + 96) / 192 + 2;
198 if (pyro > VOLTS_TO_PYRO(10)) {
199 query.arm_status = AO_PAD_ARM_STATUS_ARMED;
201 #if AO_FIRE_R_POWER_FET
202 } else if (pyro > VOLTS_TO_PYRO(5)) {
203 if ((ao_time() % 100) < 50)
205 query.arm_status = AO_PAD_ARM_STATUS_UNKNOWN;
209 query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
212 if ((ao_time() - ao_pad_packet_time) > AO_SEC_TO_TICKS(2))
214 else if (ao_radio_cmac_rssi < AO_PAD_RSSI_MINIMUM)
219 for (c = 0; c < AO_PAD_NUM; c++) {
220 int16_t sense = packet->adc.sense[c];
221 uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
224 * pyro is run through a divider, so pyro = v_pyro * 27 / 127 ~= v_pyro / 20
225 * v_pyro = pyro * 127 / 27
243 * If the relay is closed, then sense will be 0
244 * If no igniter is present, then sense will be v_pyro * 27k/227k = pyro * 127 / 227 ~= pyro/2
245 * If igniter is present, then sense will be v_pyro * 27k/127k ~= v_pyro / 20 = pyro
248 #if AO_FIRE_R_POWER_FET
249 if (sense <= pyro / 8) {
250 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
251 if ((ao_time() % 100) < 50)
252 cur |= AO_LED_CONTINUITY(c);
254 if (pyro / 8 * 3 <= sense && sense <= pyro / 8 * 5)
255 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
256 else if (pyro / 8 * 7 <= sense) {
257 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
258 cur |= AO_LED_CONTINUITY(c);
261 if (sense >= pyro / 8 * 5) {
262 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
263 cur |= AO_LED_CONTINUITY(c);
265 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
268 query.igniter_status[c] = status;
271 PRINTD("change leds from %02x to %02x\n",
278 if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
285 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
286 if (arm_beep_time == 0) {
287 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
292 } else if (beeping) {
303 if (!ao_pad_disabled) {
305 ao_radio_recv_abort();
313 ao_wakeup (&ao_pad_disabled);
318 ao_pad_read_box(void)
320 uint8_t byte = ao_74hc165_read();
329 #ifdef AO_PAD_SELECTOR_PORT
330 static int ao_pad_read_box(void) {
331 AO_PORT_TYPE value = ao_gpio_get_all(AO_PAD_SELECTOR_PORT);
335 for (pin = 0; pin < sizeof (AO_PORT_TYPE) * 8; pin++) {
336 if (AO_PAD_SELECTOR_PINS & (1 << pin)) {
337 if ((value & (1 << pin)) == 0)
342 return ao_config.pad_box;
346 #if HAS_FIXED_PAD_BOX
347 #define ao_pad_read_box() ao_config.pad_box
351 #define ao_pad_read_box() PAD_BOX
359 int16_t time_difference;
366 while (ao_pad_disabled)
367 ao_sleep(&ao_pad_disabled);
368 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
369 PRINTD ("cmac_recv %d %d\n", ret, ao_radio_cmac_rssi);
370 if (ret != AO_RADIO_CMAC_OK)
372 ao_pad_packet_time = ao_time();
374 ao_pad_box = ao_pad_read_box();
376 PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
377 command.tick, command.box, ao_pad_box, command.cmd, command.channels);
379 switch (command.cmd) {
381 if (command.box != ao_pad_box) {
382 PRINTD ("box number mismatch\n");
386 if (command.channels & ~(AO_PAD_ALL_CHANNELS))
389 time_difference = command.tick - ao_time();
390 PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
391 if (time_difference < 0)
392 time_difference = -time_difference;
393 if (time_difference > 10) {
394 PRINTD ("time difference too large %d\n", time_difference);
398 ao_pad_armed = command.channels;
399 ao_pad_arm_time = ao_time();
403 if (command.box != ao_pad_box) {
404 PRINTD ("box number mismatch\n");
408 query.tick = ao_time();
409 query.box = ao_pad_box;
410 query.channels = AO_PAD_ALL_CHANNELS;
411 query.armed = ao_pad_armed;
412 PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
413 query.tick, query.box, query.channels, query.armed,
415 query.igniter_status[0],
416 query.igniter_status[1],
417 query.igniter_status[2],
418 query.igniter_status[3]);
419 ao_radio_cmac_send(&query, sizeof (query));
423 PRINTD ("not armed\n");
426 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
427 PRINTD ("late pad arm_time %d time %d\n",
428 ao_pad_arm_time, ao_time());
432 ao_pad_ignite = ao_pad_armed;
433 ao_pad_arm_time = ao_time();
434 ao_wakeup(&ao_pad_ignite);
438 PRINTD ("not armed\n");
442 if (!ao_log_running) ao_log_start();
444 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
445 PRINTD ("late pad arm_time %d time %d\n",
446 ao_pad_arm_time, ao_time());
450 ao_pad_ignite = ao_pad_armed;
451 ao_pad_arm_time = ao_time();
452 ao_wakeup(&ao_pad_ignite);
454 case AO_PAD_ENDSTATIC:
468 printf ("Arm switch: ");
469 switch (query.arm_status) {
470 case AO_PAD_ARM_STATUS_ARMED:
473 case AO_PAD_ARM_STATUS_DISARMED:
474 printf ("Disarmed\n");
476 case AO_PAD_ARM_STATUS_UNKNOWN:
477 printf ("Unknown\n");
481 for (c = 0; c < AO_PAD_NUM; c++) {
482 printf ("Pad %d: ", c);
483 switch (query.igniter_status[c]) {
484 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED: printf ("No igniter. Relay closed\n"); break;
485 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN: printf ("No igniter. Relay open\n"); break;
486 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN: printf ("Good igniter. Relay open\n"); break;
487 case AO_PAD_IGNITER_STATUS_UNKNOWN: printf ("Unknown\n"); break;
498 if (!ao_match_word("DoIt"))
501 if (ao_cmd_status != ao_cmd_success)
503 ignite = 1 << ao_cmd_lex_i;
505 if (ao_cmd_status != ao_cmd_success) {
507 ao_cmd_status = ao_cmd_success;
509 repeat = ao_cmd_lex_i;
510 while (repeat-- > 0) {
511 ao_pad_ignite = ignite;
512 ao_wakeup(&ao_pad_ignite);
513 ao_delay(AO_PAD_FIRE_TIME>>1);
517 static __xdata struct ao_task ao_pad_task;
518 static __xdata struct ao_task ao_pad_ignite_task;
519 static __xdata struct ao_task ao_pad_monitor_task;
523 ao_pad_set_debug(void)
526 if (ao_cmd_status == ao_cmd_success)
527 ao_pad_debug = ao_cmd_lex_i != 0;
532 ao_pad_alarm_debug(void)
534 uint8_t which, value;
536 if (ao_cmd_status != ao_cmd_success)
538 which = ao_cmd_lex_i;
540 if (ao_cmd_status != ao_cmd_success)
542 value = ao_cmd_lex_i;
543 printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
551 __code struct ao_cmds ao_pad_cmds[] = {
552 { ao_pad_test, "t\0Test pad continuity" },
553 { ao_pad_manual, "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
555 { ao_pad_set_debug, "D <0 off, 1 on>\0Debug" },
556 { ao_pad_alarm_debug, "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
561 #ifndef AO_PAD_PORT_1
562 #define AO_PAD_0_PORT AO_PAD_PORT
563 #define AO_PAD_1_PORT AO_PAD_PORT
564 #define AO_PAD_2_PORT AO_PAD_PORT
565 #define AO_PAD_3_PORT AO_PAD_PORT
566 #define AO_PAD_4_PORT AO_PAD_PORT
567 #define AO_PAD_5_PORT AO_PAD_PORT
568 #define AO_PAD_6_PORT AO_PAD_PORT
569 #define AO_PAD_7_PORT AO_PAD_PORT
575 #ifdef AO_PAD_SELECTOR_PORT
578 for (pin = 0; pin < sizeof (AO_PORT_TYPE) * 8; pin++) {
579 if (AO_PAD_SELECTOR_PINS & (1 << pin))
580 ao_enable_input(AO_PAD_SELECTOR_PORT, pin, AO_EXTI_MODE_PULL_UP);
584 ao_enable_output(AO_PAD_0_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
587 ao_enable_output(AO_PAD_1_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
590 ao_enable_output(AO_PAD_2_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
593 ao_enable_output(AO_PAD_3_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
596 ao_enable_output(AO_PAD_4_PORT, AO_PAD_PIN_4, AO_PAD_4, 0);
599 ao_enable_output(AO_PAD_5_PORT, AO_PAD_PIN_5, AO_PAD_5, 0);
602 ao_enable_output(AO_PAD_6_PORT, AO_PAD_PIN_6, AO_PAD_6, 0);
605 ao_enable_output(AO_PAD_7_PORT, AO_PAD_PIN_7, AO_PAD_7, 0);
608 ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
611 ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
613 ao_cmd_register(&ao_pad_cmds[0]);
614 ao_add_task(&ao_pad_task, ao_pad, "pad listener");
615 ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
616 ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");